Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base: 6ae62e035c
...
compare: d8ebae1855
Checking mergeability… Don't worry, you can still create the pull request.
  • 4 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
Commits on Jan 18, 2012
@matthew-brett matthew-brett RF - update optpkg from nibabel copy
Importing of submodules, doctests better defended from Py3
c8bdcf0
@matthew-brett matthew-brett RF - make traits optional for gui interfaces
A hackish traits-like shell for when traits cannot be imported
4655b7c
Commits on Jan 31, 2012
@Garyfallidis Garyfallidis Merge pull request #17 from matthew-brett/optional-traits
Optional traits
4e29e20
@matthew-brett matthew-brett BF - changed wrong nibabel import in doctest
This was cruft from copying optpkg from nibabel
d8ebae1
View
30 dipy/tracking/gui_tools.py
@@ -1,16 +1,24 @@
from warnings import warn
-warn("The gui_tools module is very new and not well tested, please use it" +
-"with care and help us make it better")
-from dipy.tracking.interfaces import ShmTrackingInterface, InputData
-from traitsui.api import Item, Group, View, ArrayEditor
-from traits.api import File
+warn("The gui_tools module is very new and not well tested, please use it "
+ "with care and help us make it better")
-I = InputData()
-iview = I.trait_view()
-iview.resizable = True
-iview.width = 600
-I.trait_view('traits_view', iview)
+# Import traits as optional package
+from ..utils.optional_traits import tapi, tuapi, have_traits, setup_module
+# Import names to top level; done here in case we don't have actual traits but
+# only the traits shell from the optional_traits module
+File = tapi.File
+Item, Group, View, ArrayEditor = (tuapi.Item, tuapi.Group, tuapi.View,
+ tuapi.ArrayEditor)
+
+from ..tracking.interfaces import InputData
+
+if have_traits:
+ I = InputData()
+ iview = I.trait_view()
+ iview.resizable = True
+ iview.width = 600
+ I.trait_view('traits_view', iview)
main_view = View(Group(Group(
Item( 'dwi_images' ),
@@ -59,7 +67,7 @@ def gui_track(interface=None):
if not interface.configure_traits(view=main_view):
return
if interface.save_streamlines_to == '' and interface.save_counts_to == '':
- raiseIOError('must provide filename where to save results')
+ raise IOError('must provide filename where to save results')
streamlines = list(interface.track_shm())
if interface.save_streamlines_to != '':
interface.save_streamlines(streamlines, interface.save_streamlines_to)
View
40 dipy/tracking/interfaces.py
@@ -1,34 +1,38 @@
from warnings import warn
-warn("The interfaces module is very new and not well tested, please use it" +
-"with care and help us make it better")
+
+warn("The interfaces module is very new and not well tested, please use it "
+ "with care and help us make it better")
import pickle
import string
import os.path as path
-import numpy as np
-import nibabel as nib
+import numpy as np
from scipy.ndimage import convolve
-import traits.api as T
+
+# Import traits as optional package
+from ..utils.optional_traits import tapi as T, setup_module
+
+import nibabel as nib
from nibabel.trackvis import write, empty_header
-from dipy.reconst.shm import SlowAdcOpdfModel, MonoExpOpdfModel, \
- QballOdfModel, normalize_data, ClosestPeakSelector, \
- ResidualBootstrapWrapper, hat, lcr_matrix, bootstrap_data_array
-from dipy.reconst.interpolate import TriLinearInterpolator, \
- NearestNeighborInterpolator
-from dipy.core.triangle_subdivide import create_half_unit_sphere, \
- disperse_charges
-from dipy.tracking.integration import BoundryIntegrator, FixedStepIntegrator, \
- generate_streamlines
-from dipy.tracking.utils import seeds_from_mask, target, merge_streamlines, \
- density_map
-from dipy.io.bvectxt import read_bvec_file, orientation_to_string, \
- reorient_bvec
+from ..reconst.shm import (SlowAdcOpdfModel, MonoExpOpdfModel, QballOdfModel,
+ normalize_data, ClosestPeakSelector,
+ ResidualBootstrapWrapper, hat, lcr_matrix,
+ bootstrap_data_array)
+from ..reconst.interpolate import (TriLinearInterpolator,
+ NearestNeighborInterpolator)
+from ..core.triangle_subdivide import (create_half_unit_sphere, disperse_charges)
+from ..tracking.integration import (BoundryIntegrator, FixedStepIntegrator,
+ generate_streamlines)
+from ..tracking.utils import (seeds_from_mask, target, merge_streamlines,
+ density_map)
+from ..io.bvectxt import (read_bvec_file, orientation_to_string, reorient_bvec)
nifti_file = T.File(filter=['Nifti Files', '*.nii.gz',
'Nifti Pair or Analyze Files', '*.img.gz',
'All Files', '*'])
+
def read_roi(file, threshold=0, shape=None):
img = nib.load(file)
if shape is not None:
View
46 dipy/utils/optional_traits.py
@@ -0,0 +1,46 @@
+""" Classes to provide and API like traits, even if traits is absent """
+
+from ..utils.optpkg import optional_package
+
+# Import traits as optional package
+tapi, have_traits, setup_module = optional_package('traits.api')
+
+# Import traitsui as optional package
+tuapi, _, _ = optional_package('traitsui.api')
+
+if not have_traits:
+ class FlexiTrip(object):
+ """ Class for which instances raise error on attribute access
+
+ Like ``tripwire.TripWire`` but allowing any init arguments
+ """
+ def __init__(self, *args, **kwargs):
+ pass
+
+ def __getattr__(self, attr_name):
+ raise RuntimeError("You need 'traits' for this object to work")
+
+ def decotrip(func):
+ ''' Decorator to return tripping instance '''
+ return FlexiTrip
+
+ tapi.HasTraits = FlexiTrip
+ tapi.HasStrictTraits = FlexiTrip
+ tapi.File = FlexiTrip
+ tapi.String = FlexiTrip
+ tapi.Float = FlexiTrip
+ tapi.Int = FlexiTrip
+ tapi.Bool = FlexiTrip
+ tapi.List = FlexiTrip
+ tapi.Tuple = FlexiTrip
+ tapi.Range = FlexiTrip
+ tapi.Array = FlexiTrip
+ tapi.DelegatesTo = FlexiTrip
+ tapi.Instance = FlexiTrip
+ tapi.Enum = FlexiTrip
+ tapi.on_trait_change = decotrip
+ # UI
+ tuapi.Item = FlexiTrip
+ tuapi.Group = FlexiTrip
+ tuapi.View = FlexiTrip
+ tuapi.ArrayEditor = FlexiTrip
View
23 dipy/utils/optpkg.py
@@ -7,7 +7,7 @@
else:
have_nose = True
-from .tripwire import TripWire, is_tripwire
+from .tripwire import TripWire
def optional_package(name, trip_msg=None):
""" Return package-like thing and module setup for package `name`
@@ -27,7 +27,7 @@ def optional_package(name, trip_msg=None):
If we can import the package, return it. Otherwise return an object
raising an error when accessed
have_pkg : bool
- True if import for package was succesful, false otherwise
+ True if import for package was successful, false otherwise
module_setup : function
callable usually set as ``setup_module`` in calling namespace, to allow
skipping tests.
@@ -47,16 +47,31 @@ def optional_package(name, trip_msg=None):
and
- >>> pkg.some_function()
+ >>> pkg.some_function() #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TripWireError: We need package not_a_package for these functions, but ``import not_a_package`` raised an ImportError
+
+ If the module does exist - we get the module
+
+ >>> pkg, _, _ = optional_package('os')
+ >>> hasattr(pkg, 'path')
+ True
+
+ Or a submodule if that's what we asked for
+
+ >>> subpkg, _, _ = optional_package('os.path')
+ >>> hasattr(subpkg, 'dirname')
+ True
"""
+ # fromlist=[''] results in submodule being returned, rather than the top
+ # level module. See help(__import__)
try:
- pkg = __import__(name)
+ pkg = __import__(name, fromlist=[''])
except ImportError:
pass
else: # import worked
+ # top level module
return pkg, True, lambda : None
if trip_msg is None:
trip_msg = ('We need package %s for these functions, but '
View
2  dipy/utils/tripwire.py
@@ -35,7 +35,7 @@ class TripWire(object):
... import silly_module_name
... except ImportError:
... silly_module_name = TripWire('We do not have silly_module_name')
- >>> silly_module_name.do_silly_thing('with silly string')
+ >>> silly_module_name.do_silly_thing('with silly string') #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
TripWireError: We do not have silly_module_name

No commit comments for this range

Something went wrong with that request. Please try again.