Skip to content

Commit

Permalink
Merge pull request #45 from pnlbwh/py3k
Browse files Browse the repository at this point in the history
More Python 3 updates, fix #41
  • Loading branch information
demianw authored May 8, 2019
2 parents f956c09 + 814efcd commit d4a88aa
Show file tree
Hide file tree
Showing 32 changed files with 192 additions and 299 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ htmlcov
## The Pycharm internal directory.
.idea
build
cover
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
language: python
matrix:
include:
python: "2.7"
allow_failures:
- python: "2.7"
- python: "3.5"
- python: "3.6"

Expand Down
55 changes: 0 additions & 55 deletions doc/generated/tract_querier.query_processor.rst

This file was deleted.

43 changes: 0 additions & 43 deletions doc/generated/tract_querier.tract_label_indices.rst

This file was deleted.

38 changes: 0 additions & 38 deletions doc/generated/tract_querier.tractography.rst

This file was deleted.

2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
numpy>=1.6
nose>=1.2
nibabel>=2.0
six>=1.10
numpydoc
six
18 changes: 7 additions & 11 deletions scripts/tract_math
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import traceback

import warnings


def custom_formatwarning(msg, *a):
# ignore everything except the message
return '\nWarning: ' + str(msg) + '\n'

warnings.formatwarning = custom_formatwarning
import os, sys
# sys.path.append(os.path.join(os.path.dirname(__file__), '..'))


def tract_math_operation(help_text, needs_one_tract=True):
Expand Down Expand Up @@ -66,7 +62,7 @@ def main():
"""

operations_names = operations.keys()
operations_names.sort()
operations_names = sorted(operations_names)
for f in operations_names:
usage += '\t%s %s\n' % (f, operations[f].help_text)

Expand Down Expand Up @@ -103,19 +99,19 @@ def main():
# filename, tractography instance) to tractography list.
tractography.append((f.name, tractography_from_files(f.name)))
except IOError as e:
print >>sys.stderr, "Error reading file ", f.name, "(%s)" % repr(e)
# print >>sys.stderr, "Error reading file ", f.name, "(%s)" % repr(e)
print(sys.stderr, "Error reading file ", f.name, "(%s)" % repr(e))

if args.operation in operations:
try:
operations[args.operation](
tractography, *args.operation_parameters)
except TractMathWrongArgumentsError, e:
except TractMathWrongArgumentsError as e:
traceback.print_exc(file=sys.stdout)
parser.error('\n\n' + str(e))
except TypeError:
traceback.print_exc(file=sys.stdout)
parser.error(
"\n\nError: Wrong number of parameters for the operation")
parser.error("\n\nError: Wrong number of parameters for the operation")

else:
parser.error("\n\nOperation not found")
Expand Down
24 changes: 12 additions & 12 deletions scripts/tract_querier
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ def main():
bounding_box_affine_transform = np.fromstring(
options.bounding_box_affine_transform, sep=','
).reshape(4, 4)
print "Affine transform:"
print bounding_box_affine_transform
print("Affine transform:")
print(bounding_box_affine_transform)
except ValueError:
parser.error("Matrix format not valid")
else:
bounding_box_affine_transform = None

print "Loading files"
print("Loading files")

# Search order precidence for .qry files
# 1. Command line options specified are respected first
Expand Down Expand Up @@ -106,7 +106,7 @@ def main():

try:
if os.path.exists(options.queries_string):
query_script = file(options.queries_string).read()
query_script = open(options.queries_string).read()
query_filename = options.queries_string
else:
found = False
Expand All @@ -116,7 +116,7 @@ def main():
found = True
break
if found:
query_script = file(file_).read()
query_script = open(file_).read()
query_filename = file_
else:
query_script = options.queries_string
Expand All @@ -129,10 +129,10 @@ def main():
)

tract_querier.queries_syntax_check(query_file_body)
except tract_querier.TractQuerierSyntaxError as e:
parser.error(e.value)
except tract_querier.TractographySpatialIndexing as e:
except tract_querier.TractQuerierSyntaxError or tract_querier.TractographySpatialIndexing as e:
parser.error(e.value)
# except tract_querier.TractographySpatialIndexing as e:
# parser.error(e.value)

labels_nii = nibabel.load(options.atlas_file_name)
img = labels_nii.get_data()
Expand Down Expand Up @@ -169,7 +169,7 @@ def main():
else:
tractography_extra_kwargs = {}

print "Calculating labels and crossings"
print("Calculating labels and crossings")
affine_ijk_2_ras = labels_nii.get_affine()
tracts = tr.tracts()

Expand All @@ -186,13 +186,13 @@ def main():
)

if not options.interactive:
print "Computing queries"
print("Computing queries")
evaluated_queries = tract_querier.eval_queries(
query_file_body,
tractography_spatial_indexing,
)

query_names = evaluated_queries.keys()
query_names = list(evaluated_queries.keys())
if options.query_selection != '':
selected_queries = set(options.query_selection.lower().split(','))
query_names = list(set(query_names) & set(selected_queries))
Expand Down Expand Up @@ -225,7 +225,7 @@ def main():

def save_query(query_name, tractography, options, evaluated_queries, extension='.vtk', extra_kwargs={}):
tract_numbers = evaluated_queries[query_name]
print "\tQuery %s: %.6d" % (query_name, len(tract_numbers))
print("\tQuery %s: %.6d" % (query_name, len(tract_numbers)))
if tract_numbers:
filename = options.output_file_name + "_" + query_name + extension
save_tractography_file(
Expand Down
6 changes: 3 additions & 3 deletions tract_querier/aabb.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def buildTree_old(self, allboxes, indices=None, leafPointers=None, parent=None,
boxes = allboxes[indices, :]

if verbose:
print '*******************************************'
print('*******************************************')

dimensions = len(boxes[0]) / 2

Expand Down Expand Up @@ -265,8 +265,8 @@ def buildTree_old(self, allboxes, indices=None, leafPointers=None, parent=None,
rightIndices = indices[n:]

if verbose:
print "Left: ", leftIndices
print "Right: ", rightIndices
print(("Left: ", leftIndices))
print(("Right: ", rightIndices))
n = self.node(box, indices.copy(), parent=parent)
n.left = self.buildTree(
allboxes, leftIndices, parent=n, leafPointers=leafPointers)
Expand Down
17 changes: 13 additions & 4 deletions tract_querier/code_util/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from six import add_metaclass


class DocStringInheritor(type):

'''A variation on
Expand Down Expand Up @@ -33,8 +36,9 @@ class Foo(object):
def frobnicate(self):
pass

@add_metaclass(DocStringInheritor)
class Bar(Foo):
__metaclass__ = DocStringInheritor
pass
self.assertEqual(Bar.__doc__, object.__doc__)
self.assertEqual(Bar().__doc__, object.__doc__)
self.assertEqual(Bar.frobnicate.__doc__, None)
Expand All @@ -46,8 +50,10 @@ class Foo(object):

def frobnicate(self):
'Frobnicate this gonk.'

@add_metaclass(DocStringInheritor)
class Bar(Foo):
__metaclass__ = DocStringInheritor
pass
self.assertEqual(Foo.__doc__, 'Foo')
self.assertEqual(Foo().__doc__, 'Foo')
self.assertEqual(Bar.__doc__, 'Foo')
Expand All @@ -64,8 +70,9 @@ def frobnicate(self):
class Bar(Foo):
pass

@add_metaclass(DocStringInheritor)
class Baz(Bar):
__metaclass__ = DocStringInheritor
pass
self.assertEqual(Baz.__doc__, 'Foo')
self.assertEqual(Baz().__doc__, 'Foo')
self.assertEqual(Baz.frobnicate.__doc__, 'Frobnicate this gonk.')
Expand All @@ -77,8 +84,10 @@ class Foo(object):

def frobnicate(self):
'Frobnicate this gonk.'

@add_metaclass(DocStringInheritor)
class Bar(Foo):
__metaclass__ = DocStringInheritor
pass

class Baz(Bar):
pass
Expand Down
4 changes: 2 additions & 2 deletions tract_querier/nipype/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import os
import glob
from itertools import izip


from nipype.interfaces.base import (TraitedSpec, traits)
from nipype.interfaces.ants import utils
Expand Down Expand Up @@ -218,7 +218,7 @@ def _format_arg(self, name, spec, value):
'must be 1 or the same as '
'input images'
)
for template, reference, weight in izip(
for template, reference, weight in zip(
self._internal['reference_images'],
self._internal['input_images'],
self._internal['metric_weights'],
Expand Down
Loading

0 comments on commit d4a88aa

Please sign in to comment.