示例#1
0
 def unit_to_name(self, u):
     for name in ('meter', 'mm', 'sec', 'msec', 'usec'):
         if u == pimms.unit(name):
             return name
     # no current support for ppm designation
     if u == pimms.unit('um'): return 'micron'
     elif u == pimms.unit('1/sec'): return 'hz'
     elif u == pimms.unit('Hz'): return 'hz'
     elif u == pimms.unit('radian'): return 'rads'
     else: return 'unknown'
示例#2
0
 def test_units(self):
     '''
     test_units ensures that the various pimms functions related to pint integration work
     correctly; these functions include pimms.unit, .mag, .quant, .is_quantity, etc.
     '''
     # make a few pieces of data with types
     x = np.asarray([1.0, 2.0, 3.0, 4.0]) * pimms.units.mm
     y = pimms.quant([2, 4, 6, 8], 'sec')
     for u in [x, y]:
         self.assertTrue(pimms.is_quantity(u))
     for u in ('abc', 123, 9.0, []):
         self.assertFalse(pimms.is_quantity(u))
     for u in [x, y]:
         self.assertFalse(pimms.is_quantity(pimms.mag(u)))
     self.assertTrue(pimms.like_units(pimms.unit(x), pimms.unit('yards')))
     self.assertTrue(pimms.like_units(pimms.unit(y), pimms.unit('minutes')))
     self.assertFalse(pimms.like_units(pimms.unit(y), pimms.unit('mm')))
     z = x / y
     self.assertTrue(pimms.is_vector(x, 'real'))
     self.assertTrue(pimms.is_vector(y, 'real'))
     self.assertFalse(pimms.is_vector(x, 'int'))
     self.assertTrue(pimms.is_vector(y, 'int'))
     self.assertTrue(pimms.is_vector(y, 'float'))
     self.assertTrue(pimms.is_vector(z, 'real'))
示例#3
0
 def postprocess_image(self, img, d):
     from nibabel.nifti1 import slice_order_codes
     hdr = img.header
     # dimension information:
     for k in ['dimension_information', 'dim_info', 'diminfo']:
         try:
             hdr.set_dim_info(*d[k])
             break
         except Exception:
             pass
     try:
         hdr.set_intent(d['intent'])
     except Exception:
         pass
     # xyzt_units:
     try:
         sunit = self.unit_to_name(pimms.unit(d['voxel_size']))
     except Exception:
         try:
             sunit = self.unit_to_name(pimms.unit(d['voxel_unit']))
         except Exception:
             sunit = 'unknown'
     try:
         tunit = self.unit_to_name(pimms.unit(d['slice_duration']))
     except Exception:
         try:
             tunit = self.unit_to_name(pimms.unit(d['time_unit']))
         except Exception:
             tunit = 'unknown'
     try:
         hdr.set_xyzt_units(sunit, tunit)
     except Exception:
         pass
     # qform and sform
     try:
         try:
             q = to_affine(d['qform'])
         except Exception:
             q = to_affine(d['affine'])
         qc = d.get('qform_code', None)
         hdr.set_qform(q, qc)
     except Exception:
         pass
     try:
         try:
             s = to_affine(d['sform'])
         except Exception:
             s = to_affine(d['affine'])
         sc = d.get('sform_code', None)
         hdr.set_sform(s, sc)
     except Exception:
         pass
     # slice code
     try:
         hdr['slice_code'] = slice_order_codes[d['slice_order']]
     except Exception:
         pass
     # slice duration
     try:
         dur = d['slice_duration']
         if pimms.is_quantity(dur):
             if tunit == 'unknown': dur = pimms.mag(dur)
             else: dur = pimms.mag(dur, tunit)
         hdr.set_slice_duration(dur)
     except Exception:
         pass
     # slice timing
     try:
         ts = d['slice_times']
         if pimms.is_quantity(ts):
             if tunit == 'unknown': ts = pimms.mag(ts)
             else: ts = pimms.mag(ts, tunit)
         hdr.set_slice_duration([None if np.isnan(t) else t for t in ts])
     except Exception:
         pass
     # slope / intercept
     try:
         hdr.set_slope_inter(d.get('data_slope', None),
                             d.get('data_offset', None))
     except Exception:
         pass
     # calibration
     try:
         (cmn, cmx) = d['calibration']
         hdr['cal_min'] = cmn
         hdr['cal_max'] = cmx
     except Exception:
         pass
     # time offset
     try:
         t0 = d['time_offset']
         if pimms.is_quantity(t0):
             if tunits != 'unknown': t0 = pimms.mag(t0, tunits)
             else: t0 = pimms.mag(t0)
         hdr['toffset'] = t0
     except Exception:
         pass
     # description
     try:
         hdr['descrip'] = d['description']
     except Exception:
         pass
     # auxiliary filename
     try:
         hdr['aux_file'] = d['auxiliary_filename']
     except Exception:
         pass
     return img