Exemplo n.º 1
0
    def input_volume(self, filename, n_dimensions=MI_ORIGINAL_TYPE,
                 dim_names=None,
                 nc_data_type=MI_ORIGINAL_TYPE,
                 signed_flag=MI_ORIGINAL_TYPE, voxel_min=MI_ORIGINAL_TYPE,
                 voxel_max=MI_ORIGINAL_TYPE, create_flag=1):
        """construct Volume object from existing MINC file

Usage: input_volume(filename, n_dimensions, dim_names, nc_data_type,
                      signed_flag, voxel_min, voxel_max, create_flag)

Arguments:                      
    string                    filename         (required)
    integer                   n_dimensions
    tuple of strings | None   dim_names    
    integer                   nc_data_type
    integer                   signed_flag
    floating point            voxel_min
    floating point            voxel_max
    integer                   create_flag
"""
        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError, \
                  'volume_io_obj can only be created by a constructor'
        self.volume_io_obj, self.input_info, self.attributes = \
                            VolumeIO._start_volume_input(filename, n_dimensions,
                                                   dim_names, nc_data_type,
                                                   signed_flag, float(voxel_min),
                                                   float(voxel_max), create_flag)
        if self.volume_io_obj is None:
            raise IOError, "input of volume %s failed" % filename

        Volume._prune_header(self)

        VolumeIO._finish_volume_input(self.volume_io_obj, self.input_info)
        del self.input_info
Exemplo n.º 2
0
 def set_cache_output_parameters(self, filename, history="",
                                 nc_data_type=MI_ORIGINAL_TYPE,
                                 signed_flag=MI_ORIGINAL_TYPE,
                                 voxel_min=MI_ORIGINAL_TYPE,
                                 voxel_max=MI_ORIGINAL_TYPE,
                                 original_filename=None):
     VolumeIO._set_cache_output_volume_parameters(self.volume_io_obj, 
              filename, nc_data_type, signed_flag, float(voxel_min), float(voxel_max),
              original_filename, history)
Exemplo n.º 3
0
 def __del__(self):
     if hasattr(self, 'input_info'):
         VolumeIO._delete_volume_input(self.input_info)
     if hasattr(self, 'volume_io_obj'):
         if VolumeIO_a.volume_is_cached(self.volume_io_obj) and \
            not VolumeIO_a.volume_is_alloced(self.volume_io_obj):
             # this is memory leak; however, there is a bug in
             #  delete_volume that can't handle this case
             pass
         else:
             VolumeIO_a.delete_volume(self.volume_io_obj)
Exemplo n.º 4
0
 def output(self, filename, comment = "Tag file created by py_minc"):
     if self.n_volumes == 2:
         VolumeIO._output_tag_file(filename, comment, self.n_volumes, self.number(),
                                   numpy.array(self.locations[0]), numpy.array(self.locations[1]),
                                   self.weights, self.structure_ids, self.patient_ids,
                                   self.labels)
     else:
         VolumeIO._output_tag_file(filename, comment, self.n_volumes, len(self.weights),
                                   numpy.array(self.locations), numpy.array([]),
                                   self.weights, self.structure_ids, self.patient_ids,
                                   self.labels)
Exemplo n.º 5
0
 def __del__(self):
     if hasattr(self, 'input_info'):
         VolumeIO._delete_volume_input(self.input_info)
     if hasattr(self, 'volume_io_obj'):
         if VolumeIO_a.volume_is_cached(self.volume_io_obj) and \
            not VolumeIO_a.volume_is_alloced(self.volume_io_obj):
             # this is memory leak; however, there is a bug in
             #  delete_volume that can't handle this case
             pass
         else:
             VolumeIO_a.delete_volume(self.volume_io_obj)
Exemplo n.º 6
0
 def set_cache_output_parameters(self,
                                 filename,
                                 history="",
                                 nc_data_type=MI_ORIGINAL_TYPE,
                                 signed_flag=MI_ORIGINAL_TYPE,
                                 voxel_min=MI_ORIGINAL_TYPE,
                                 voxel_max=MI_ORIGINAL_TYPE,
                                 original_filename=None):
     VolumeIO._set_cache_output_volume_parameters(self.volume_io_obj,
                                                  filename, nc_data_type,
                                                  signed_flag,
                                                  float(voxel_min),
                                                  float(voxel_max),
                                                  original_filename,
                                                  history)
Exemplo n.º 7
0
 def output(self, filename, comment="Tag file created by py_minc"):
     if self.n_volumes == 2:
         VolumeIO._output_tag_file(filename, comment, self.n_volumes,
                                   self.number(),
                                   numpy.array(self.locations[0]),
                                   numpy.array(self.locations[1]),
                                   self.weights, self.structure_ids,
                                   self.patient_ids, self.labels)
     else:
         VolumeIO._output_tag_file(filename, comment, self.n_volumes,
                                   len(self.weights),
                                   numpy.array(self.locations),
                                   numpy.array([]), self.weights,
                                   self.structure_ids, self.patient_ids,
                                   self.labels)
Exemplo n.º 8
0
    def output(self, filename, history="", nc_data_type=MI_ORIGINAL_TYPE,
               signed_flag=MI_ORIGINAL_TYPE, voxel_min=MI_ORIGINAL_TYPE,
               voxel_max=MI_ORIGINAL_TYPE):
        """output(filename, history, nc_data_type, signed_flag, voxel_min, voxel_max)
        
Arguments:
    string              filename         
    string              history
    integer             nc_data_type
    integer             signed_flag
    floating point      voxel_min
    floating point      voxel_max

   Returns:
    None
"""

        VolumeIO._output_volume(filename, self.volume_io_obj,
                                history, nc_data_type, signed_flag,
                                float(voxel_min), float(voxel_max))
Exemplo n.º 9
0
    def input_volume(self,
                     filename,
                     n_dimensions=MI_ORIGINAL_TYPE,
                     dim_names=None,
                     nc_data_type=MI_ORIGINAL_TYPE,
                     signed_flag=MI_ORIGINAL_TYPE,
                     voxel_min=MI_ORIGINAL_TYPE,
                     voxel_max=MI_ORIGINAL_TYPE,
                     create_flag=1):
        """construct Volume object from existing MINC file

Usage: input_volume(filename, n_dimensions, dim_names, nc_data_type,
                      signed_flag, voxel_min, voxel_max, create_flag)

Arguments:                      
    string                    filename         (required)
    integer                   n_dimensions
    tuple of strings | None   dim_names    
    integer                   nc_data_type
    integer                   signed_flag
    floating point            voxel_min
    floating point            voxel_max
    integer                   create_flag
"""
        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError, \
                  'volume_io_obj can only be created by a constructor'
        self.volume_io_obj, self.input_info, self.attributes = \
                            VolumeIO._start_volume_input(filename, n_dimensions,
                                                   dim_names, nc_data_type,
                                                   signed_flag, float(voxel_min),
                                                   float(voxel_max), create_flag)
        if self.volume_io_obj is None:
            raise IOError, "input of volume %s failed" % filename

        Volume._prune_header(self)

        VolumeIO._finish_volume_input(self.volume_io_obj, self.input_info)
        del self.input_info
Exemplo n.º 10
0
    def copy_volume(self, existing_volume, starts=None, sizes=None):
        """create a Volume object by copying an existing Volume

Usage: copy_volume(existing_volume [, start=(s0,s1, ...), sizes=(n0,n1,...)])

Arguments:                      
    Volume        :  existing_volume
    start         :  extract a subvolume starting at given voxel (optional)
    size          :  extract a subvolume of given dimensions (optional)
"""
        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError, \
                  'volume_io_obj can only be created by a constructor'

        if existing_volume.__class__ is not Volume:
            raise TypeError, ("Cannot create volume of type %s from type Volume."
                              + "  Try copy='definition' instead.") \
                              % existing_volume.__class

        # copy the whole volume
        if starts is None and sizes is None:
            self.volume_io_obj = VolumeIO_a.copy_volume(
                existing_volume.volume_io_obj)
        # or copy only a subvolume
        else:
            Volume._copy_sub_volume_definition_no_alloc(
                self, existing_volume, starts, sizes)
            VolumeIO_a.alloc_volume_data(self.volume_io_obj)
            hyperslab = VolumeIO._get_real_subvolume(
                existing_volume.volume_io_obj, tuple(starts), tuple(sizes))
            VolumeIO._set_real_subvolume(self.volume_io_obj, (0, ) *
                                         existing_volume.get_n_dimensions(),
                                         hyperslab)

        if self.volume_io_obj is None:
            raise RuntimeError

        self._copy_attributes(existing_volume.attributes)
Exemplo n.º 11
0
    def copy_volume(self, existing_volume, starts=None, sizes=None):
        """create a Volume object by copying an existing Volume

Usage: copy_volume(existing_volume [, start=(s0,s1, ...), sizes=(n0,n1,...)])

Arguments:                      
    Volume        :  existing_volume
    start         :  extract a subvolume starting at given voxel (optional)
    size          :  extract a subvolume of given dimensions (optional)
"""
        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError, \
                  'volume_io_obj can only be created by a constructor'

        if existing_volume.__class__ is not Volume:
            raise TypeError, ("Cannot create volume of type %s from type Volume."
                              + "  Try copy='definition' instead.") \
                              % existing_volume.__class
            
        # copy the whole volume
        if starts is None and sizes is None:
            self.volume_io_obj = VolumeIO_a.copy_volume(existing_volume.volume_io_obj)
        # or copy only a subvolume
        else:
            Volume._copy_sub_volume_definition_no_alloc(self, existing_volume, starts, sizes)
            VolumeIO_a.alloc_volume_data(self.volume_io_obj)
            hyperslab = VolumeIO._get_real_subvolume(existing_volume.volume_io_obj,
                                                     tuple(starts), tuple(sizes))
            VolumeIO._set_real_subvolume(self.volume_io_obj,
                                         (0,) * existing_volume.get_n_dimensions(),
                                         hyperslab)

        if self.volume_io_obj is None:
            raise RuntimeError

        self._copy_attributes(existing_volume.attributes)
Exemplo n.º 12
0
    def output(self,
               filename,
               history="",
               nc_data_type=MI_ORIGINAL_TYPE,
               signed_flag=MI_ORIGINAL_TYPE,
               voxel_min=MI_ORIGINAL_TYPE,
               voxel_max=MI_ORIGINAL_TYPE):
        """output(filename, history, nc_data_type, signed_flag, voxel_min, voxel_max)
        
Arguments:
    string              filename         
    string              history
    integer             nc_data_type
    integer             signed_flag
    floating point      voxel_min
    floating point      voxel_max

   Returns:
    None
"""

        VolumeIO._output_volume(filename, self.volume_io_obj, history,
                                nc_data_type, signed_flag, float(voxel_min),
                                float(voxel_max))
Exemplo n.º 13
0
    def copy_volume_definition(self,
                               existing_volume,
                               nc_data_type=MI_ORIGINAL_TYPE,
                               signed_flag=MI_ORIGINAL_TYPE,
                               voxel_min=MI_ORIGINAL_TYPE,
                               voxel_max=MI_ORIGINAL_TYPE,
                               starts=None,
                               sizes=None):
        """create a Volume object based on the definition of an existing Volume

Usage: copy_volume_definition(existing_volume, nc_data_type,
                      signed_flag, voxel_min, voxel_max, start, size

Arguments:                      
    Volume                    existing_volume :  (required)
    integer                   nc_data_type
    integer                   signed_flag
    floating point            voxel_min
    floating point            voxel_max
    tuple                     starts          : voxel coordinate of subvolume starts
    tuple                     size            : voxel dimensions of subvolume
"""

        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError,\
                  'volume_io_obj can only be created by a constructor'
        # copy the whole volume
        if starts is None and sizes is None:
            self.volume_io_obj = VolumeIO._copy_volume_definition(
                existing_volume.volume_io_obj, nc_data_type, signed_flag,
                float(voxel_min), float(voxel_max))
        else:
            Volume._copy_sub_volume_definition_no_alloc(
                self, existing_volume, starts, sizes, nc_data_type,
                signed_flag, voxel_min, voxel_max)
            VolumeIO_a.alloc_volume_data(self.volume_io_obj)

        if self.volume_io_obj is None:
            raise RuntimeError

        self.attributes = {}
Exemplo n.º 14
0
    def copy_volume_definition(self, existing_volume,
                               nc_data_type=MI_ORIGINAL_TYPE,
                               signed_flag=MI_ORIGINAL_TYPE,
                               voxel_min=MI_ORIGINAL_TYPE,
                               voxel_max=MI_ORIGINAL_TYPE,
                               starts=None, sizes=None):
        """create a Volume object based on the definition of an existing Volume

Usage: copy_volume_definition(existing_volume, nc_data_type,
                      signed_flag, voxel_min, voxel_max, start, size

Arguments:                      
    Volume                    existing_volume :  (required)
    integer                   nc_data_type
    integer                   signed_flag
    floating point            voxel_min
    floating point            voxel_max
    tuple                     starts          : voxel coordinate of subvolume starts
    tuple                     size            : voxel dimensions of subvolume
"""

        if hasattr(self, 'volume_io_obj'):
            raise RuntimeError,\
                  'volume_io_obj can only be created by a constructor'
        # copy the whole volume
        if starts is None and sizes is None:
            self.volume_io_obj = VolumeIO._copy_volume_definition(
                existing_volume.volume_io_obj, nc_data_type, signed_flag,
                float(voxel_min), float(voxel_max))
        else:
            Volume._copy_sub_volume_definition_no_alloc(self, existing_volume, starts, sizes,
                                                 nc_data_type, signed_flag,
                                                 voxel_min, voxel_max)
            VolumeIO_a.alloc_volume_data(self.volume_io_obj)
            
        if self.volume_io_obj is None:
            raise RuntimeError

        self.attributes = {}
Exemplo n.º 15
0
    def __init__(self, arg0):
        "If arg0 is a filename, read tags from file.  Otherwise, if arg0 is 1, 2 create an empty tags structure appropriate for the specified number of volumes"
        if type(arg0) is types.StringType:
            r = VolumeIO._input_tag_file(arg0)

            self.n_volumes = r[0]
            if(self.n_volumes == 1):
                self.locations = r[1]
                r = r[2:]
            else:
                self.locations = [r[1], r[2]]
                r = r[3:]
            self.weights, self.structure_ids, self.patient_ids, self.labels = r

        elif arg0 in [1, 2]:
            self.n_volumes = arg0
            self.locations = ([], [[], []])[arg0 == 2]
            self.weights = []
            self.structure_ids = []
            self.patient_ids = []
            self.labels = []
        else:
            raise TypeError, "Unknown argument type for VolumeTags"
Exemplo n.º 16
0
    def __init__(self, arg0):
        "If arg0 is a filename, read tags from file.  Otherwise, if arg0 is 1, 2 create an empty tags structure appropriate for the specified number of volumes"
        if type(arg0) is types.StringType:
            r = VolumeIO._input_tag_file(arg0)

            self.n_volumes = r[0]
            if (self.n_volumes == 1):
                self.locations = r[1]
                r = r[2:]
            else:
                self.locations = [r[1], r[2]]
                r = r[3:]
            self.weights, self.structure_ids, self.patient_ids, self.labels = r

        elif arg0 in [1, 2]:
            self.n_volumes = arg0
            self.locations = ([], [[], []])[arg0 == 2]
            self.weights = []
            self.structure_ids = []
            self.patient_ids = []
            self.labels = []
        else:
            raise TypeError, "Unknown argument type for VolumeTags"
Exemplo n.º 17
0
 def fill(self, value):
     "fill all voxels with given value"
     VolumeIO._fill_volume_real_value(self.volume_io_obj, value)
Exemplo n.º 18
0
 def set_hyperslab(self, start, data):
     "set an n dimensional array of real values in the volume."
     VolumeIO._set_real_subvolume(self.volume_io_obj,
                                         tuple(start), data)
Exemplo n.º 19
0
 def get_hyperslab(self, start, size, typecode=numpy.float_):
     "get an n dimensional array of real values from the volume"
     return self._cast(VolumeIO._get_real_subvolume(self.volume_io_obj,
                                         tuple(start), tuple(size)), typecode)
Exemplo n.º 20
0
    def get_all_values(self, typecode=numpy.float_):
        """Return a numpy array of the specified type with all values from the volume.
Note that integer types are produced by rounding using floor(x+0.5).
"""
        return VolumeIO._get_volume_all_real_values(self.volume_io_obj, numpy.dtype(typecode).char)
Exemplo n.º 21
0
 def get_dimension_names(self):
     "get_dimension_names()"
     return VolumeIO._get_volume_dimension_names(self.volume_io_obj)
Exemplo n.º 22
0
 def get_dimension_names(self):
     "get_dimension_names()"
     return VolumeIO._get_volume_dimension_names(self.volume_io_obj)
Exemplo n.º 23
0
    def get_all_values(self, typecode=numpy.float_):
        """Return a numpy array of the specified type with all values from the volume.
Note that integer types are produced by rounding using floor(x+0.5).
"""
        return VolumeIO._get_volume_all_real_values(self.volume_io_obj,
                                                    numpy.dtype(typecode).char)
Exemplo n.º 24
0
 def get_hyperslab(self, start, size, typecode=numpy.float_):
     "get an n dimensional array of real values from the volume"
     return self._cast(
         VolumeIO._get_real_subvolume(self.volume_io_obj, tuple(start),
                                      tuple(size)), typecode)
Exemplo n.º 25
0
 def set_hyperslab(self, start, data):
     "set an n dimensional array of real values in the volume."
     VolumeIO._set_real_subvolume(self.volume_io_obj, tuple(start), data)
Exemplo n.º 26
0
 def fill(self, value):
     "fill all voxels with given value"
     VolumeIO._fill_volume_real_value(self.volume_io_obj, value)