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
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)
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
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
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
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
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
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
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
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
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
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
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;
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))
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)
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
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
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)
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
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
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
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)))
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
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)
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
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
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
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;
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
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