コード例 #1
0
def find_resource(where):
    """ Return the absolute file name of a resource inside the project.
        This function is necessary as bbfreeze cannot embed arbitrary files.
    """

    # Try first as a local application using MediPy
    command_name = sys.argv[0]
    abs_path = os.path.realpath(command_name)
    dirname = os.path.dirname(abs_path)
    elements = dirname.split(os.path.sep)

    resource_path = None
    while len(elements) > 0:
        path = os.path.sep.join(elements)
        if os.path.exists(os.path.join(path, where)):
            resource_path = os.path.join(path, where)
            break
        else:
            elements.pop()
    if resource_path is not None:
        return resource_path

    # Try in MediPy and plugins
    import medipy
    for path in medipy.__path__:
        resource_path = os.path.join(path, where)
        if os.path.exists(resource_path):
            return resource_path

    # Resource was not found, raise an exception
    raise exception.Exception("Cannot find resource %s" % where)
コード例 #2
0
    def _default_direction(self):
        """ Return the default image direction, according to the data type.
        """

        dim = None
        if self.data_type == "scalar":
            dim = self.data.ndim
        elif self.data_type == "vector":
            dim = self.data.ndim - 1
        elif self.data_type == "matrix":
            dim = self.data.ndim - 2
        else:
            raise exception.Exception("Unknown data_type : %s" %
                                      self.data_type)
        return numpy.identity(dim, dtype=numpy.float32)
コード例 #3
0
    def remove_observer(self, event, observer):
        """Remove an observer from the object.
        Return False if the observer was not in the list. Otherwise return True
        """

        self._remove_dead_observers()

        if not self.is_allowed_event(event):
            raise exception.Exception("Event " + event +
                                      " is not allowed for type " +
                                      str(type(self)))

        if "im_self" in dir(observer) and "im_func" in dir(observer):
            return self._remove_bound_method_observer(event, observer)
        else:
            return self._remove_function_observer(event, observer)
コード例 #4
0
    def __init__(self,
                 shape=(0, ),
                 dtype=numpy.single,
                 spacing=None,
                 origin=None,
                 direction=None,
                 data_type="scalar",
                 image_type="normal",
                 annotations=None,
                 metadata=None,
                 **kwargs):
        """ Create an image. Extra arguments may contain :
              * data or value (mutually exclusive)
              * dti
              * any other argument of numpy.ndarray.__init__
        """

        ##################
        # Public members #
        ##################

        # Numpy array holding the voxel values
        self.data = None

        # Type/dimensionality of the data : can be one of scalar, vector, or matrix
        self.data_type = data_type

        # Acquisition modality of the image : can be normal or spectroscopy. TODO : use metadata
        self.image_type = image_type

        # Annotations on the image. TODO : store them in metadata
        self.annotations = ObservableList(annotations or [])

        # Metadata
        self.metadata = metadata or {}

        ############################
        # Property-related members #
        ############################

        # Spacing between pixels, in mm
        self._spacing = None

        # Origin of the image, in mm
        self._origin = None

        # Matrix of direction cosines that specify the direction between samples
        self._direction = None

        self._index_to_physical_matrix = None
        self._physical_to_index_matrix = None

        ##################
        # Initialization #
        ##################
        Observable.__init__(self, ["modified"])

        # Modify the shape of DTI images
        if "dti" in kwargs:
            self.data_type = "vector"
            if kwargs["dti"] == "tensor_2":
                self.image_type = "tensor_2"
                shape = list(shape)
                shape.append(6)
#            elif kwargs["dti"] == "tensor_4" :
#                pass
            else:
                raise exception.Exception("Unkown DTI model : %s" %
                                          kwargs["dti"])
            del kwargs["dti"]

        # Array initialization : either data or value
        if "data" in kwargs and "value" in kwargs:
            raise exception.Exception("Only one of data and value is allowed")
        if "data" in kwargs:
            self.data = numpy.asarray(kwargs["data"])
            del kwargs["data"]
        else:
            if "value" in kwargs:
                value = kwargs["value"]
                del kwargs["value"]
            else:
                value = None
            self.data = numpy.ndarray(shape, dtype, **kwargs)
            if value is not None:
                self.data.fill(value)

        if spacing is None:
            self._set_spacing(self._default_spacing())
        else:
            self._set_spacing(spacing)

        if origin is None:
            self._set_origin(self._default_origin())
        else:
            self._set_origin(origin)

        if direction is None:
            self._set_direction(self._default_direction())
        else:
            self._set_direction(direction)