-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Dendrogram class #274
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Dendrogram class #274
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3d6dbb9
Added Dendrogram class and tests
merenlin 2d9b88c
Added a small validator
merenlin 819e41e
updates after renaming
merenlin 3781906
Moved dendrogram tests to optional
merenlin 31efa33
Protected scipy imports
merenlin 02beb6c
Fixed typos, docstring and return value
merenlin bfd3cca
Typo
merenlin ff98cb7
Fixing default colors mapping
merenlin 4afc8c9
Minor styling changes and fixes
merenlin 57e15bd
pep8 and docsrings
merenlin b7d3d31
pep8 in tests
merenlin 44cdece
Styling and imports
merenlin 16c74c1
Cleanup of labels, axis and tests
merenlin ebff53e
Added more tests and fixed axis reference
merenlin 0de0349
another fix for axis confusion
merenlin 20fc40c
Added colorscale test
merenlin 88842b1
cow comments
merenlin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -105,3 +105,135 @@ def test_simple_streamline(self): | |
self.assertListEqual(strln['data'][0]['x'][0:100], | ||
expected_strln_0_100['x']) | ||
|
||
|
||
class TestDendrogram(TestCase): | ||
|
||
def test_default_dendrogram(self): | ||
dendro = tls.FigureFactory.create_dendrogram(X=np.array([[1, 2, 3, 4], | ||
[1, 1, 3, 4], | ||
[1, 2, 1, 4], | ||
[1, 2, 3, 1]])) | ||
expected_data = [{'marker': {'color': 'rgb(255,133,27)'}, | ||
'mode': 'lines', 'xaxis': 'xs', | ||
'yaxis': 'y', | ||
'y': np.array([0., 1., 1., 0.]), | ||
'x': np.array([25., 25., 35., 35.]), | ||
'type': u'scatter'}, | ||
{'marker': {'color': 'rgb(255,133,27)'}, | ||
'mode': 'lines', | ||
'xaxis': 'x', | ||
'yaxis': 'y', | ||
'y': np.array([0., 2.23606798, | ||
2.23606798, 1.]), | ||
'x': np.array([15., 15., 30., 30.]), | ||
'type': u'scatter'}, | ||
{'marker': {'color': 'blue'}, | ||
'mode': 'lines', | ||
'xaxis': 'x', | ||
'yaxis': 'y', | ||
'y': np.array([0., 3.60555128, | ||
3.60555128, 2.23606798]), | ||
'x': np.array([5., 5., 22.5, 22.5]), | ||
'type': u'scatter'}] | ||
expected_layout = {'width': '100%', | ||
'showlegend': False, | ||
'autoscale': False, | ||
'xaxis': {'showticklabels': True, | ||
'tickmode': 'array', | ||
'ticks': 'outside', | ||
'showgrid': False, | ||
'mirror': 'allticks', | ||
'zeroline': False, | ||
'showline': True, | ||
'ticktext': np.array(['3', '2', | ||
'0', '1'], | ||
dtype='|S1'), | ||
'rangemode': 'tozero', | ||
'type': 'linear', | ||
'tickvals': np.array([5.0, 15.0, | ||
25.0, 35.0])}, | ||
'yaxis': {'showticklabels': True, | ||
'ticks': 'outside', | ||
'showgrid': False, | ||
'mirror': 'allticks', | ||
'zeroline': False, | ||
'showline': True, | ||
'rangemode': 'tozero', | ||
'type': 'linear'}, | ||
'hovermode': 'closest'} | ||
|
||
# Make sure data is as expected | ||
self.assertEqual(len(dendro['data']), len(expected_data)) | ||
for i in range(1, len(dendro['data'])): | ||
self.assertTrue(np.allclose(dendro['data'][i]['x'], | ||
expected_data[i]['x'])) | ||
self.assertTrue(np.allclose(dendro['data'][i]['y'], | ||
expected_data[i]['y'])) | ||
|
||
# Make sure layout is as expected | ||
self.assertTrue(np.array_equal(dendro['layout']['xaxis']['ticktext'], | ||
expected_layout['xaxis']['ticktext'])) | ||
self.assertTrue(np.array_equal(dendro['layout']['xaxis']['tickvals'], | ||
expected_layout['xaxis']['tickvals'])) | ||
self.assertEqual(dendro['layout']['xaxis']['ticks'], 'outside') | ||
self.assertEqual(dendro['layout']['yaxis']['ticks'], 'outside') | ||
self.assertEqual(dendro['layout']['width'], expected_layout['width']) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also test that the output There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added layout test in ebff53e |
||
def test_dendrogram_random_matrix(self): | ||
# create a random uncorrelated matrix | ||
X = np.random.rand(5, 5) | ||
# variable 2 is correlated with all the other variables | ||
X[2, :] = sum(X, 0) | ||
|
||
names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark'] | ||
dendro = tls.FigureFactory.create_dendrogram(X, labels=names) | ||
|
||
# Check that 2 is in a separate cluster and it's labelled correctly | ||
self.assertEqual(dendro['layout']['xaxis']['ticktext'][0], 'John') | ||
|
||
def test_dendrogram_orientation(self): | ||
X = np.random.rand(5, 5) | ||
|
||
dendro_left = tls.FigureFactory.create_dendrogram( | ||
X, orientation='left') | ||
self.assertEqual(len(dendro_left['layout']['yaxis']['ticktext']), 5) | ||
tickvals_left = np.array(dendro_left['layout']['yaxis']['tickvals']) | ||
self.assertTrue((tickvals_left <= 0).all()) | ||
|
||
dendro_right = tls.FigureFactory.create_dendrogram( | ||
X, orientation='right') | ||
tickvals_right = np.array(dendro_right['layout']['yaxis']['tickvals']) | ||
self.assertTrue((tickvals_right >= 0).all()) | ||
|
||
dendro_bottom = tls.FigureFactory.create_dendrogram( | ||
X, orientation='bottom') | ||
self.assertEqual(len(dendro_bottom['layout']['xaxis']['ticktext']), 5) | ||
tickvals_bottom = np.array(dendro_bottom['layout']['xaxis']['tickvals']) | ||
self.assertTrue((tickvals_bottom >= 0).all()) | ||
|
||
dendro_top = tls.FigureFactory.create_dendrogram(X, orientation='top') | ||
tickvals_top = np.array(dendro_top['layout']['xaxis']['tickvals']) | ||
self.assertTrue((tickvals_top <= 0).all()) | ||
|
||
def test_dendrogram_orientation(self): | ||
X = np.array([[1, 2, 3, 4], | ||
[1, 1, 3, 4], | ||
[1, 2, 1, 4], | ||
[1, 2, 3, 1]]) | ||
greyscale = [ | ||
'rgb(0,0,0)', # black | ||
'rgb(05,105,105)', # dim grey | ||
'rgb(128,128,128)', # grey | ||
'rgb(169,169,169)', # dark grey | ||
'rgb(192,192,192)', # silver | ||
'rgb(211,211,211)', # light grey | ||
'rgb(220,220,220)', # gainsboro | ||
'rgb(245,245,245)'] # white smoke | ||
|
||
dendro = tls.FigureFactory.create_dendrogram(X, colorscale=greyscale) | ||
self.assertEqual(dendro["data"][0]['marker']['color'], | ||
'rgb(128,128,128)') | ||
self.assertEqual(dendro["data"][1]['marker']['color'], | ||
'rgb(128,128,128)') | ||
self.assertEqual(dendro["data"][2]['marker']['color'], | ||
'rgb(0,0,0)') |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you make sure that:
label
argument in works as expectedorientation
in works as expectedcolorscale
in works as expected. In particular, it's unclear if thecolorscale
used here is the same or different format from thecolorscale
used in Plotly JSON.It'd be best if these were tested separately to keep these unit tests from overlapping as much as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@theengineear right now the format of colorscale is different from plotly JSON, as is the goal of using it. Colorscale in this case only replaces default colors that scipy assigns to cluster elements. There is no control over to which element the color will be assigned as it is usually is with Plotly's colorscale key-value pairs.
Should I perhaps communicate this in a docstring/example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added a test 20fc40c