def export(flnm, p): if not pimms.is_nparray(p): p = np.asarray(p.dataobj) flnm = flnm + '.' + volume_format dt = np.int32 if np.issubdtype(p.dtype, np.dtype(int).type) else np.float32 img = nib.Nifti1Image(np.asarray(p, dtype=dt), affmatrix) img.to_filename(flnm) return flnm
def export(flnm, d): if not pimms.is_nparray(d): d = np.asarray(d.dataobj) flnm = flnm + '.' + volume_format dt = np.int32 if np.issubdtype(d.dtype, np.dtype(int).type) else np.float32 img = fsmgh.MGHImage(np.asarray(d, dtype=dt), affmatrix) img.to_filename(flnm) return flnm
def is_npimage(img): ''' is_npimage(img) yields True if img is an image object and its dataobj member is a numpy array-- i.e., img is not a pointer to an array-proxy object (e.g., when the image is cached on disk); yields False otherwise. ''' if not is_image(img): return False elif not pimms.is_nparray(img.dataobj): return False else: return True
def is_pimage(img): ''' is_pimage(img) yields True if img is an image object and it contains a persistent dataobj (i.e., the dataobj is a numpy array with the writeable flag set to False); otherwise yields False. ''' if not is_image(img): return False elif not pimms.is_nparray(img.dataobj): return False elif img.dataobj.flags['WRITEABLE'] is True: return False else: return True
def to_nifti(obj, like=None, header=None, affine=None, extensions=Ellipsis, version=None): ''' to_nifti(obj) yields a Nifti2Image object that is as equivalent as possible to the given object obj. If obj is a Nifti2Image already, then it is returned unmolested; other deduction rules are described below. The following options are accepted: * like (default: None) may be provided to give a guide for the various header- and meta-data that is included in the image. If this is a nifti image object, its meta-data are used; if this is a subject, then the meta-data are deduced from the subject's voxel and native orientation matrices. All other specific options below override anything deduced from the like argument. * header (default: None) may be a Nifti1 or Niti2 image header to be used as the nifti header or to replace the header in a new image. * affine (default: None) may specify the affine transform to be given to the image object. * extensions (default: Ellipsis) may specify a nifti extensions object that should be included in the header. The default value, Ellipsis, indicates that the extensions should not be changed, and that None should be used if extensions are not implied in obj (if, for example, obj is a data array rather than an image object with a header already. * version (default: None) may be specified as 1 or 2 for a Nifti1Image or Nifti2Image object, respectively; if the option None is passed, then any object that is already a nifti1 or nifti2 object is kept as the same version, otherwise nifti1 is used when possible and nifti2 when not possible. ''' from neuropythy.mri import (Subject, to_image) obj0 = obj # First go from like to explicit versions of affine and header: if like is not None: if isinstance(like, nib.analyze.AnalyzeHeader) or \ isinstance(like, nib.freesurfer.mghformat.MGHHeader): if header is None: header = like elif isinstance(like, nib.analyze.SpatialImage): if header is None: header = like.header if affine is None: affine = like.affine elif isinstance(like, Subject): if affine is None: affine = like.images['brain'].affine else: raise ValueError('Could not interpret like argument with type %s' % type(like)) # Figure out what the data is if isinstance(obj, nib.analyze.SpatialImage): obj = obj.dataobj elif not pimms.is_nparray(obj): obj = np.asarray(obj) if len(obj.shape) == 3: obj = np.reshape(obj, obj.shape + (1, )) elif len(obj.shape) == 2: obj = np.reshape(obj, obj.shape + (1, 1)) elif len(obj.shape) == 1: obj = np.reshape(obj, obj.shape + (1, 1, 1)) elif len(obj.shape) != 4: raise ValueError('nifti objects must be 1D, 2D, 3D, or 4D') # figure out version if needed if version is None: if isinstance(obj0, nib.nifti2.Nifti2Image): version = 2 elif any(sh > 32767 for sh in obj.shape): version = 2 else: version = 1 # check to make sure that we have to change something: elif ((version == 1 and (isinstance(obj0, nib.nifti1.Nifti1Image) and not isinstance(obj0, nib.nifti2.Nifti2Image))) or (version == 2 and isinstance(obj0, nib.nifti2.Nifti2Image))): if ((header is None or obj0.header is header) and (extensions is Ellipsis or extensions is obj0.header.extensions or (extensions is None and len(obj0.header.extensions) == 0))): return obj0 # okay, now look at the header and affine etc. if header is None: if isinstance(obj0, nib.analyze.SpatialImage): header = obj0.header else: header = nib.nifti1.Nifti1Header( ) if version == 1 else nib.nifti2.Nifti2Header() if affine is None: if isinstance(obj0, nib.analyze.SpatialImage): affine = obj0.affine else: affine = np.eye(4) if extensions is None: extensions = nib.nifti1.Nifti1Extensions() # Okay, make a new object now... if version == 1: obj = nib.nifti1.Nifti1Image(obj, affine, header) elif version == 2: obj = nib.nifti2.Nifti2Image(obj, affine, header) else: raise ValueError('invalid version given (should be 1 or 2): %s' % version) # add the extensions if they're needed if extensions is not Ellipsis and (len(extensions) > 0 or len(obj.header.extensions) > 0): obj.header.extensions = extensions # Okay, that's it! return obj
def test_predicates(self): ''' test_predicates ensures that the various pimms functions of the form is_<type>(obj) are working properly. ''' # some arbitrary values to use in testing qr = pimms.quant([1.5, 3.3, 9.5, 10.4, 6.2, 0.1], 'mm') qi = pimms.quant([1, 3, 9, 10, 6, 0], 'seconds') mr = np.random.rand(10, 3) vi = np.random.randint(0, 5000, size=12) sr = np.array(10.0) si = np.array(2) * pimms.units.deg l = [1, 2.0, 'abc'] lx = [[1, 1], [2.0, 2.0], [4, 7.7]] u = u'a unicode string of stuff' b = b'a byte string of stuff' f0 = lambda: np.linspace(0, 100, 117) f1 = lambda x: x**2 + 1 d = { 'a': 12, 'b': None, 'c': f0, 'd': f1, 'e': lambda: 'some string', 'f': lambda: None } pm = pyr.pmap(d) lm = pimms.lazy_map(d) # a function for testing predicates def tpred(p, tvals, fvals): for s in tvals: self.assertTrue(p(s)) for s in fvals: self.assertFalse(p(s)) # Map types tpred(pimms.is_lazy_map, [lm], [qr, qi, mr, vi, sr, si, l, u, b, f0, f1, d, pm]) tpred(pimms.is_map, [lm, d, pm], [qr, qi, mr, vi, sr, si, l, u, b, f0, f1]) tpred(pimms.is_pmap, [lm, pm], [qr, qi, mr, vi, sr, si, l, u, b, f0, f1, d]) # Numpy types require a little attention due to their optional arguments and the # complexities of the type relationships tpred(pimms.is_nparray, [qr, qi, mr, vi, sr], [l, lx, u, b, f0, f1, d, pm, lm]) self.assertTrue(pimms.is_nparray(qr, 'real')) self.assertTrue(pimms.is_nparray(qr, 'any', 1)) self.assertFalse(pimms.is_nparray(qr, 'any', 2)) self.assertFalse(pimms.is_nparray(qi, 'string')) self.assertTrue(pimms.is_nparray(qi, ('real', 'int'), (1, 3))) self.assertFalse(pimms.is_nparray(qi, ('real', 'int'), 2)) self.assertFalse( pimms.is_nparray(qr, ('string', 'bool', 'bytes'), (2, 3))) self.assertTrue(pimms.is_nparray(mr, None, 2)) self.assertFalse(pimms.is_nparray(mr, None, 1)) self.assertFalse(pimms.is_nparray(vi, 'int', 2)) tpred(pimms.is_npscalar, [sr], [qr, qi, mr, vi, l, lx, u, b, f0, f1, d, pm, lm]) self.assertTrue(pimms.is_npscalar(np.array(12.0), 'real')) self.assertFalse(pimms.is_npscalar(np.array(12.0), 'string')) self.assertTrue(pimms.is_npscalar(np.array(12.0), ('real', 'complex'))) tpred(pimms.is_npmatrix, [mr, pimms.quant(mr, 'm/s')], [sr, si, qr, qi, vi, l, lx, u, b, f0, f1, d, pm, lm]) self.assertTrue(pimms.is_npmatrix(mr, ('int', 'real', 'string'))) self.assertTrue(pimms.is_npmatrix(mr, 'number')) self.assertFalse(pimms.is_npmatrix(mr, ('bool', 'string'))) tpred(pimms.is_npvector, [qr, qi, vi, vi * pimms.units.mol, qr, qi], [sr, si, mr, l, lx, u, b, f0, f1, d, pm, lm]) self.assertTrue(pimms.is_npvector(vi, 'real')) self.assertTrue(pimms.is_npvector(qi, 'int')) self.assertFalse(pimms.is_npvector(qr, ('bool', 'string'))) self.assertTrue(pimms.is_npvalue('abc', 'string')) self.assertTrue(pimms.is_npvalue(u'abc', ('unicode', 'real'))) self.assertFalse(pimms.is_npvalue(np.array(5.6), ('unicode', 'real'))) self.assertFalse(pimms.is_npvalue(np.array(5.6), ('unicode', 'bool'))) self.assertFalse(pimms.is_npvalue(np.array([5.6]), ('unicode', 'real'))) # Also the un-nump'ified versions tpred(pimms.is_array, [qr, qi, vi, sr, si, mr, qr, qi, l, lx, u, b, f0, f1], [d, pm, lm]) self.assertTrue(pimms.is_array(qr, 'real')) self.assertTrue(pimms.is_array(qr, 'any', 1)) self.assertTrue(pimms.is_array(qr, 'any', 1)) self.assertFalse(pimms.is_array(qi, 'string')) self.assertTrue(pimms.is_array(qi, ('real', 'int'), (1, 3))) self.assertFalse(pimms.is_array(qi, ('real', 'int'), 2)) self.assertFalse( pimms.is_array(qr, ('string', 'bool', 'bytes'), (2, 3))) self.assertTrue(pimms.is_array(mr, None, 2)) self.assertFalse(pimms.is_array(mr, None, 1)) self.assertFalse(pimms.is_array(vi, 'int', 2)) self.assertFalse(pimms.is_array(l, 'number', 1)) self.assertTrue(pimms.is_array(lx, 'any', (1, 2))) tpred(pimms.is_scalar, [u, b, f0, f1, sr, si], [qr, qi, vi, mr, l, d, pm, lm, lx]) tpred(pimms.is_int, [vi[0], si, 1, 10], [u, b, f0, f1, d, pm, lm, sr, qr, mr]) tpred(pimms.is_real, [vi[0], si, 1, 10, sr], [4j, u, b, f0, f1, d, pm, lm, lx, mr, qr]) tpred(pimms.is_complex, [vi[0], si, 1, 10, sr, 4j], [u, b, f0, f1, d, pm, lm, lx, mr, qr]) tpred(pimms.is_number, [vi[0], si, 1, 10, sr], [u, b, f0, f1, d, pm, lm, lx, mr, qr]) tpred(pimms.is_str, ['abc'], [vi, si, 1, 10, sr, qr, f0, f1, d, pm, lm, lx, mr]) tpred(pimms.is_class, [str, int], [vi, si, 1, 10, sr, qr, u, b, f0, f1, d, pm, lm, lx, mr]) tpred(pimms.is_quantity, [qr, qi, si], [vi, 10, sr, u, b, f0, f1, d, pm, lm, lx, mr]) tpred(pimms.is_unit, ('seconds', 's', 'mm', 'deg', pimms.units.seconds, pimms.units.s, pimms.units.mm, pimms.units.deg), (1, 10.0, np.asarray([10]), None, 'nonunitstring', qr)) self.assertTrue(pimms.is_nparray(mr, np.inexact)) self.assertFalse(pimms.is_nparray(vi, np.inexact))