Skip to content

Loading…

BUG: allow ClusterNode.pre_order() for non-root nodes #3394

Closed
wants to merge 3 commits into from

3 participants

@rgommers
SciPy member

Merged in b54a499 with a minor cleanup. Thanks @jamestwebber

@rgommers rgommers closed this
@rgommers rgommers added the defect label
@rgommers rgommers modified the milestone: 0.15.0, 0.14.0
@jamestwebber jamestwebber deleted the jamestwebber:patch-1 branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 26, 2014
  1. @jamestwebber

    Modified ClusterNode.pre_order() for non-root

    jamestwebber committed
    Fixes #2177 by replacing the boolean vectors with sets.
  2. @jamestwebber

    TST: added test for subtree pre_order

    jamestwebber committed
    Tests that pre_order works for subtree of ClusterNode root
Commits on Feb 27, 2014
  1. @jamestwebber
This page is out of date. Refresh to see the latest.
Showing with 14 additions and 6 deletions.
  1. +6 −6 scipy/cluster/hierarchy.py
  2. +8 −0 scipy/cluster/tests/test_hierarchy.py
View
12 scipy/cluster/hierarchy.py
@@ -795,8 +795,8 @@ def pre_order(self, func=(lambda x: x.id)):
n = self.count
curNode = [None] * (2 * n)
- lvisited = np.zeros((2 * n,), dtype=bool)
- rvisited = np.zeros((2 * n,), dtype=bool)
+ lvisited = set()
+ rvisited = set()
curNode[0] = self
k = 0
preorder = []
@@ -807,13 +807,13 @@ def pre_order(self, func=(lambda x: x.id)):
preorder.append(func(nd))
k = k - 1
else:
- if not lvisited[ndid]:
+ if ndid not in lvisited:
curNode[k + 1] = nd.left
- lvisited[ndid] = True
+ lvisited.add(ndid)
k = k + 1
- elif not rvisited[ndid]:
+ elif ndid not in rvisited:
curNode[k + 1] = nd.right
- rvisited[ndid] = True
+ rvisited.add(ndid)
k = k + 1
# If we've visited the left and right of this non-leaf
# node already, go up in the tree.
View
8 scipy/cluster/tests/test_hierarchy.py
@@ -812,6 +812,14 @@ def test_leaves_list_iris_average(self):
node = to_tree(Z)
self.assertTrue((node.pre_order() == leaves_list(Z)).all())
+ def test_iris_subtree_pre_order(self):
+ "Tests that pre_order() works when called on sub-trees."
+ X = eo['iris']
+ Y = pdist(X)
+ Z = linkage(X, 'single')
+ node = to_tree(Z)
+ self.assertTrue(node.pre_order() == (node.get_left().pre_order() + node.get_right().pre_order()))
+
class TestCorrespond(TestCase):
def test_correspond_empty(self):
Something went wrong with that request. Please try again.