Пример #1
0
 def get_info(self):
     """Return the geom info as string"""
     handle = pysirf.cSIRF_GeomInfo_get(self.handle)
     check_status(handle)
     info = pyiutil.charDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return info
Пример #2
0
 def __init__(self, file=None):
     self.handle = None
     self.sorted = False
     self.info = None
     if file is not None:
         self.handle = pygadgetron.cGT_ISMRMRDAcquisitionsFromFile(file)
         check_status(self.handle)
Пример #3
0
    def __div__(self, other):
        '''
        Overloads / for data containers division by a scalar or (elementwise)
        another data container (Python 2.*)

        Returns the ratio self/other if other is a scalar
        or the elementwise ratio if other is of the same type as self.
        other: DataContainer or a (real or complex) scalar
        '''
        assert self.handle is not None

        if type(self) == type(other):
            return self.divide(other)

        if isinstance(other, Number):
            z = self.same_object()
            other = 1.0 / other
            a = numpy.asarray([other.real, other.imag], dtype=numpy.float32)
            zero = numpy.zeros((2, ), dtype=numpy.float32)
            z.handle = pysirf.cSIRF_axpby \
                (a.ctypes.data, self.handle, zero.ctypes.data, self.handle)
            check_status(z.handle)
            return z

        return NotImplemented
Пример #4
0
 def is_complex(self):
     assert self.handle is not None
     handle = pysirf.cSIRF_isComplex(self.handle)
     check_status(handle)
     i = pyiutil.intDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return i != 0
Пример #5
0
    def axpby(self, a, b, y, out=None, **kwargs):
        '''
        Addition for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        y: DataContainer
        out:   DataContainer to store the result to.
        '''
        # if isinstance(other , ( Number, int, float, numpy.float32 )):
        #     tmp = other + numpy.zeros(self.as_array().shape)
        #     other = self.copy()
        #     other.fill(tmp)

        assert_validities(self, y)
        alpha = numpy.asarray([a.real, a.imag], dtype=numpy.float32)
        beta = numpy.asarray([b.real, b.imag], dtype=numpy.float32)

        if out is None:
            z = self.same_object()
            z.handle = pysirf.cSIRF_axpby \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle)
        else:
            assert_validities(self, out)
            z = out
            try_calling(pysirf.cSIRF_axpbyAlt \
                (alpha.ctypes.data, self.handle, beta.ctypes.data, y.handle, z.handle))
        check_status(z.handle)
        return z
Пример #6
0
    def subtract(self, other, out=None):
        '''
        Overloads - for data containers.

        Returns the difference of the container data with another container 
        data viewed as vectors.
        other: DataContainer
        '''
        if not isinstance(other, (DataContainer, Number)):
            return NotImplemented
        if isinstance(other, Number):
            tmp = other + numpy.zeros(self.shape, self.dtype)
            other = self.copy()
            other.fill(tmp)
        assert_validities(self, other)
        pl_one = numpy.asarray([1.0, 0.0], dtype=numpy.float32)
        mn_one = numpy.asarray([-1.0, 0.0], dtype=numpy.float32)
        if out is None:
            z = self.same_object()
            z.handle = pysirf.cSIRF_axpby \
                (pl_one.ctypes.data, self.handle, mn_one.ctypes.data, other.handle)
            check_status(z.handle)
        else:
            assert_validities(self, out)
            z = out
            try_calling(pysirf.cSIRF_axpbyAlt \
                (pl_one.ctypes.data, self.handle, mn_one.ctypes.data, other.handle, z.handle))
        return z
Пример #7
0
def _int_pars(handle, set, par, n):
    h = pygadgetron.cGT_parameter(handle, set, par)
    check_status(h)
    for i in range(n):
        value += (pyiutil.intDataItemFromHandle(h, i), )
    pyiutil.deleteDataHandle(h)
    return value
Пример #8
0
    def __mul__(self, other):
        '''
        Overloads * for data containers multiplication by a scalar or another
        data container.

        Returns the product self*other if other is a scalar
        or the elementwise product if other is of the same type as self.
        other: DataContainer or a (real or complex) scalar
        '''
        assert self.handle is not None

        if type(self) == type(other):
            return self.multiply(other)

        if isinstance(other, Number):
            z = self.same_object()
            a = numpy.asarray([other.real, other.imag], dtype=numpy.float32)
            zero = numpy.zeros((2, ), dtype=numpy.float32)
            z.handle = pysirf.cSIRF_axpby \
                (a.ctypes.data, self.handle, zero.ctypes.data, self.handle)
            z.src = 'mult'
            check_status(z.handle)
            return z

        return NotImplemented
Пример #9
0
 def get_storage_scheme():
     '''Returns acquisition data storage scheme.
     '''
     handle = pygadgetron.cGT_getAcquisitionsStorageScheme()
     check_status(handle)
     scheme = pyiutil.charDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return scheme
Пример #10
0
 def __neg__(self):
     zero = numpy.asarray([0.0, 0.0], dtype=numpy.float32)
     mn_one = numpy.asarray([-1.0, 0.0], dtype=numpy.float32)
     z = self.same_object()
     z.handle = pysirf.cSIRF_axpby \
         (mn_one.ctypes.data, self.handle, zero.ctypes.data, self.handle)
     check_status(z.handle)
     return z
Пример #11
0
def uint16_pars(handle, group, par, n):
    h = parameter(handle, group, par)
    check_status(h)
    value = ()
    for i in range(n):
        value += (pyiutil.uint16DataItemFromHandle(h, i),)
    pyiutil.deleteDataHandle(h)
    return value
Пример #12
0
 def number(self):
     '''
     Returns the number of items in the container.
     '''
     assert self.handle is not None
     handle = pysirf.cSIRF_dataItems(self.handle)
     check_status(handle)
     n = pyiutil.intDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return n
Пример #13
0
 def norm(self):
     '''
     Returns the 2-norm of the container data viewed as a vector.
     '''
     assert self.handle is not None
     handle = pysirf.cSIRF_norm(self.handle)
     check_status(handle)
     r = pyiutil.floatDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return r;
Пример #14
0
 def __init__(self, list=None):
     self.handle = None
     self.handle = pygadgetron.cGT_newObject('ImagesReconstructor')
     check_status(self.handle)
     self.input_data = None
     if list is None:
         return
     for i in range(len(list)):
         label, name = label_and_name(list[i])
         self.add_gadget(label, Gadget(name))
Пример #15
0
 def __init__(self, acqs=None, imgs=None):
     self.handle = None
     if acqs == None:
         self.handle = pygadgetron.cGT_newObject('AcquisitionModel')
     else:
         assert_validity(acqs, AcquisitionData)
         assert_validity(imgs, ImageData)
         self.handle = \
             pygadgetron.cGT_AcquisitionModel(acqs.handle, imgs.handle)
     check_status(self.handle)
Пример #16
0
 def data_type(self, im_num):
     '''
     Returns the data type for a specified image (see 8 data types above).
     im_num: image (slice) 
     '''
     assert self.handle is not None
     handle = pygadgetron.cGT_imageDataType(self.handle, im_num)
     check_status(handle)
     n = pyiutil.intDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return n
Пример #17
0
 def divide(self, other):
     '''
     Returns the elementwise ratio of this and another container 
     data viewed as vectors.
     other: DataContainer
     '''
     assert_validities(self, other)
     z = self.same_object()
     z.handle = pysirf.cSIRF_divide(self.handle, other.handle)
     check_status(z.handle)
     return z
Пример #18
0
 def __init__(self, name):
     '''
     Creates a gadget of specified type and properties.
     name: a string of the form gadget_type(property1=value1, ...)
     '''
     self.handle = None
     name, prop = name_and_parameters(name)
     self.handle = pygadgetron.cGT_newObject(name)
     check_status(self.handle)
     if prop is not None:
         self.set_properties(prop)
Пример #19
0
 def backward(self, ad):
     '''
     Back-projects acquisition data into image space using a complex
     transpose of the forward projection.
     ad: AcquisitionData
     '''
     assert_validity(ad, AcquisitionData)
     image = ImageData()
     image.handle = pygadgetron.cGT_AcquisitionModelBackward\
         (self.handle, ad.handle)
     check_status(image.handle)
     return image
Пример #20
0
 def select(self, attr, value):
     '''
     Creates an images container with images from self with the specified
     value of specified attribute.
     attr : the name of the attribute (Python string)
     value: the value of the attribute (Python string)
     '''
     assert self.handle is not None
     images = ImageData()
     images.handle = pygadgetron.cGT_selectImages(self.handle, attr, value)
     check_status(images.handle)
     return images
Пример #21
0
 def dot(self, other):
     '''
     Returns the dot product of the container data with another container 
     data viewed as vectors.
     other: DataContainer
     '''
     assert_validities(self, other)
     handle = pysirf.cSIRF_dot(self.handle, other.handle)
     check_status(handle)
     r = pyiutil.floatDataFromHandle(handle)
     pyiutil.deleteDataHandle(handle)
     return r
Пример #22
0
 def calculate(self, data, method=None):
     '''
     Calculates coil sensitivity maps from coil images or sorted 
     acquisitions.
     data  : either AcquisitionData or CoilImages
     method: either SRSS (Square Root of the Sum of Squares, default) or 
             Inati
     '''
     if isinstance(data, AcquisitionData):
         if data.is_sorted() is False:
             print('WARNING: acquisitions may be in a wrong order')
     if self.handle is not None:
         pyiutil.deleteDataHandle(self.handle)
     self.handle = pygadgetron.cGT_CoilSensitivities('')
     check_status(self.handle)
     if method is not None:
         method_name, parm_list = name_and_parameters(method)
         parm = parse_arglist(parm_list)
     else:
         method_name = 'SRSS'
         parm = {}
     if isinstance(data, AcquisitionData):
         assert data.handle is not None
         _set_int_par\
             (self.handle, 'coil_sensitivity', 'smoothness', self.smoothness)
         try_calling(pygadgetron.cGT_computeCoilSensitivities\
             (self.handle, data.handle))
     elif isinstance(data, CoilImageData):
         assert data.handle is not None
         if method_name == 'Inati':
             #                if not HAVE_ISMRMRDTOOLS:
             try:
                 from ismrmrdtools import coils
             except:
                 raise error('Inati method requires ismrmrd-python-tools')
             nz = data.number()
             for z in range(nz):
                 ci = numpy.squeeze(data.as_array(z))
                 (csm, rho) = coils.calculate_csm_inati_iter(ci)
                 self.append(csm.astype(numpy.complex64))
         elif method_name == 'SRSS':
             if 'niter' in parm:
                 nit = int(parm['niter'])
                 _set_int_par\
                     (self.handle, 'coil_sensitivity', 'smoothness', nit)
             try_calling(pygadgetron.cGT_computeCSMsFromCIs\
                 (self.handle, data.handle))
         else:
             raise error('Unknown method %s' % method_name)
     else:
         raise error('Cannot calculate coil sensitivities from %s' % \
                     repr(type(data)))
Пример #23
0
 def get_geometrical_info(self):
     """Get the image's geometrical info."""
     try:
         import sirf.STIR
         if isinstance(self, sirf.STIR.ImageData):
             warnings.warn(
                 "geometrical info for STIR.ImageData might be incorrect")
     except:
         pass
     geom_info = GeometricalInfo()
     geom_info.handle = pysirf.cSIRF_ImageData_get_geom_info(self.handle)
     check_status(geom_info.handle)
     return geom_info
Пример #24
0
 def get_output(self, subset=None):
     '''
     Returns specified subset of the output ImageData. If no subset is 
     specified, returns all output.
     subset: the name of the subset (e.g. images, gfactors,...)
     '''
     output = ImageData()
     output.handle = pygadgetron.cGT_reconstructedImages(self.handle)
     check_status(output.handle)
     if subset is None:
         return output
     else:
         return output.select('GADGETRON_DataRole', subset)
Пример #25
0
 def forward(self, image):
     '''
     Projects an image into (simulated) acquisitions space.
     The resulting acquisition data simulates the actual data
     expected to be received from the scanner.
     image: ImageData
     '''
     assert_validity(image, ImageData)
     ad = AcquisitionData()
     ad.handle = pygadgetron.cGT_AcquisitionModelForward\
         (self.handle, image.handle)
     check_status(ad.handle)
     return ad
Пример #26
0
    def equal(self, other):
        '''
        Overloads == for ImageData.

        other: ImageData
        '''
        assert_validity(self, ImageData)
        assert_validity(other, ImageData)
        handle = pysirf.cSIRF_equalImages(self.handle, other.handle)
        check_status(handle)
        same = pyiutil.intDataFromHandle(handle)
        pyiutil.deleteDataHandle(handle)
        return same
Пример #27
0
 def reconstruct(self, input_data):
     '''
     Returns the output from the chain for specified input.
     input_data: AcquisitionData
     '''
     assert_validity(input_data, AcquisitionData)
     handle = pygadgetron.cGT_reconstructImages\
          (self.handle, input_data.handle)
     check_status(handle)
     pyiutil.deleteDataHandle(handle)
     images = ImageData()
     images.handle = pygadgetron.cGT_reconstructedImages(self.handle)
     check_status(images.handle)
     return images
Пример #28
0
    def __add__(self, other):
        '''
        Overloads + for data containers.

        Returns the sum of the container data with another container 
        data viewed as vectors.
        other: DataContainer
        '''
        assert_validities(self, other)
        one = numpy.asarray([1.0, 0.0], dtype = numpy.float32)
        z = self.same_object()
        z.handle = pysirf.cSIRF_axpby \
            (one.ctypes.data, self.handle, one.ctypes.data, other.handle)
        check_status(z.handle)
        return z;
Пример #29
0
 def process(self, input_data=None):
     '''
     Returns the output from the chain.
     input_data: ImageData
     '''
     if input_data is not None:
         self.set_input(input_data)
     if self.input_data is None:
         raise error('input data not set')
     assert_validity(self.input_data, ImageData)
     image = ImageData()
     image.handle = pygadgetron.cGT_processImages\
          (self.handle, self.input_data.handle)
     check_status(image.handle)
     self.output_data = image
     return image
Пример #30
0
 def process(self, input_data=None):
     '''
     Returns the output from the chain for specified input.
     input_data: AcquisitionData
     '''
     if input_data is not None:
         self.set_input(input_data)
     if self.input_data is None:
         raise error('input data not set')
     assert_validity(self.input_data, AcquisitionData)
     acquisitions = AcquisitionData()
     acquisitions.handle = pygadgetron.cGT_processAcquisitions\
          (self.handle, self.input_data.handle)
     check_status(acquisitions.handle)
     self.output_data = acquisitions
     return acquisitions