diff --git a/astrodendro/analysis.py b/astrodendro/analysis.py index 6c9788f..76bc972 100644 --- a/astrodendro/analysis.py +++ b/astrodendro/analysis.py @@ -10,6 +10,7 @@ from astropy import units as u from astropy.wcs import WCS +from . import six from .structure import Structure __all__ = ['ppv_catalog', 'pp_catalog'] @@ -181,7 +182,7 @@ def __init__(self, key, description, default=None, strict=False): If True, raise KeyError if metadata not provided. This overrides default """ - if not isinstance(key, basestring): + if not isinstance(key, six.string_types): raise TypeError("Key is", key, type(key)) self.key = key self.description = description or 'no description' diff --git a/astrodendro/dendrogram.py b/astrodendro/dendrogram.py index cd68e91..64fdf20 100644 --- a/astrodendro/dendrogram.py +++ b/astrodendro/dendrogram.py @@ -10,7 +10,7 @@ from .progressbar import AnimatedProgressBar from .io import IO_FORMATS from . import pruning - +from . import six class Dendrogram(object): """ @@ -272,7 +272,7 @@ def next_idx(): print("") # newline # Create trunk from objects with no ancestors - self.trunk = [structure for structure in structures.itervalues() if structure.parent is None] + self.trunk = [structure for structure in six.itervalues(structures) if structure.parent is None] # Remove orphan leaves that aren't large enough leaves_in_trunk = [structure for structure in self.trunk if structure.is_leaf] @@ -305,7 +305,7 @@ def _index(self): # add dendrogram index ti = TreeIndex(self) - for s in self._structures_dict.itervalues(): + for s in six.itervalues(self._structures_dict): s._tree_index = ti @@ -365,7 +365,7 @@ def leaves(self): """ A flattened list of all leaves in the dendrogram """ - return [i for i in self._structures_dict.itervalues() if i.is_leaf] + return [i for i in six.itervalues(self._structures_dict) if i.is_leaf] def to_newick(self): #this caches newicks, and prevents too much recursion diff --git a/astrodendro/io/util.py b/astrodendro/io/util.py index 6704110..320e4f6 100644 --- a/astrodendro/io/util.py +++ b/astrodendro/io/util.py @@ -1,5 +1,6 @@ import numpy as np +from .. import six def parse_newick(string): @@ -95,7 +96,7 @@ def _construct_tree(repr): # What we do is look at the heights of this branch's # 1st child as stored in the newick representation, and then # work backwards to compute the merge level of this branch - first_child_repr = sub_structures_repr.itervalues().next() + first_child_repr = six.next(six.itervalues(sub_structures_repr)) if type(first_child_repr) == tuple: height = first_child_repr[1] else: diff --git a/astrodendro/structure.py b/astrodendro/structure.py index 2c26110..de3c173 100644 --- a/astrodendro/structure.py +++ b/astrodendro/structure.py @@ -57,10 +57,14 @@ def __init__(self, indices, values, children=[], idx=None): self._indices = [indices] self._values = [values] self._vmin, self._vmax = values, values - else: # values are for a sequence of pixels + elif isinstance(indices, list) and isinstance(values, list): self._indices = indices self._values = values self._vmin, self._vmax = min(values), max(values) + else: # could be an array or iterator + self._indices = [x for x in indices] + self._values = [x for x in values] + self._vmin, self._vmax = min(values), max(values) self.idx = idx @@ -314,7 +318,7 @@ def descendants(self): to_add = [self] # branches with children we will need to add to the list while True: children = [] - map(children.extend, [branch.children for branch in to_add]) + list(map(children.extend, [branch.children for branch in to_add])) self._descendants.extend(children) # Then proceed, essentially recursing through child branches: to_add = [b for b in children if not b.is_leaf] diff --git a/astrodendro/test/test_compute.py b/astrodendro/test/test_compute.py index b9081fb..a1fe0a8 100644 --- a/astrodendro/test/test_compute.py +++ b/astrodendro/test/test_compute.py @@ -173,7 +173,7 @@ def test_4dim(self): assert leaf.vmin == 2 assert leaf.get_npix() == 1 + 6 + 2 # Contains 1x '5', 6x '3', and 2x '2'. The other '2' should be in the branch # Check that the only pixel in the branch is a '2' at [0,0,2,2] - assert (zip(*branches[0].indices(subtree=False)), branches[0].values(subtree=False)) == ([(0, 0, 2, 2), ], [2., ]) + assert (list(zip(*branches[0].indices(subtree=False))), branches[0].values(subtree=False)) == ([(0, 0, 2, 2), ], [2., ]) from .build_benchmark import BENCHMARKS diff --git a/astrodendro/test/test_index.py b/astrodendro/test/test_index.py index 07fe928..e866151 100644 --- a/astrodendro/test/test_index.py +++ b/astrodendro/test/test_index.py @@ -14,8 +14,8 @@ def assert_permuted_fancyindex(x, y): raise TypeError("Second argument not a fancy index: %s" % y) dtype = [('%i' % i, 'i') for i in range(len(x))] - x = np.array(zip(*x), dtype=dtype) - y = np.array(zip(*y), dtype=dtype) + x = np.array(list(zip(*x)), dtype=dtype) + y = np.array(list(zip(*y)), dtype=dtype) np.testing.assert_array_equal(np.sort(x), np.sort(y)) diff --git a/setup.py b/setup.py index 6023531..e9643cb 100755 --- a/setup.py +++ b/setup.py @@ -2,10 +2,7 @@ from setuptools import setup, Command -try: # Python 3.x - from distutils.command.build_py import build_py_2to3 as build_py -except ImportError: # Python 2.x - from distutils.command.build_py import build_py +from distutils.command.build_py import build_py class DendroTest(Command):