~matthew-brett/nipy/volumeimages

« back to all changes in this revision

Viewing changes to volumeimages/nifti1.py

  • Committer: Matthew Brett
  • Date: 2009-04-11 23:55:20 UTC
  • Revision ID: matthew.brett@gmail.com-20090411235520-5oze2nny7pndp519
Prettier output of strings for codes; more private files

Show diffs side-by-side

added added

removed removed

Lines of Context:
562
562
            info = info | (((slice+1) & 3) << 4)
563
563
        self._header_data['dim_info'] = info
564
564
 
 
565
    def get_intent_code(self, code_repr='label'):
 
566
        ''' Return representation of intent code
 
567
 
 
568
        Parameters
 
569
        ----------
 
570
        code_repr : string
 
571
           string giving output form of intent code representation.
 
572
           Default is 'label'; use 'code' for integer representation.
 
573
 
 
574
        Returns
 
575
        -------
 
576
        intent_code : string or integer
 
577
            string label for intent code or code
 
578
 
 
579
        Examples
 
580
        --------
 
581
        >>> hdr = Nifti1Header()
 
582
        >>> hdr.set_intent('t test', (10,), name='some score')
 
583
        >>> hdr.get_intent_code()
 
584
        't test'
 
585
        '''
 
586
        return self._get_code_field(
 
587
            code_repr,
 
588
            'intent_code',
 
589
            self._intent_codes)
 
590
 
565
591
    def get_intent(self, code_repr='label'):
566
592
        ''' Get intent code, parameters and name
567
593
 
591
617
        '''
592
618
        hdr = self._header_data
593
619
        code = int(hdr['intent_code'])
594
 
        recode = self._get_code_field(
595
 
            code_repr,
596
 
            'intent_code',
597
 
            self._intent_codes)
 
620
        recode = self.get_intent_code(code_repr)
598
621
        n_params = len(self._intent_codes.parameters[code])
599
622
        params = (float(hdr['intent_p%d' % (i+1)]) for i in range(n_params))
600
623
        return recode, tuple(params), str(hdr['intent_name'])
744
767
        >>> hdr.set_dim_info(slice=2)
745
768
        >>> hdr.set_data_shape((1, 1, 7))
746
769
        >>> hdr.set_slice_duration(0.1)
747
 
        >>> hdr['slice_code'] = slice_order_codes.code['sequential increasing']
748
770
 
749
771
        We need a function to print out the Nones and floating point
750
 
        values in a predictable way, for the tests below
 
772
        values in a predictable way, for the tests below.
751
773
 
752
774
        >>> _stringer = lambda val: val is not None and '%2.1f' % val or None
753
 
        >>> printable_seq = lambda s: map(_stringer, s)
754
 
 
755
 
        The following examples are from the nifti1.h documentation
756
 
 
757
 
        >>> printable_seq(hdr.get_slice_times())
 
775
        >>> _print_me = lambda s: map(_stringer, s)
 
776
 
 
777
        The following examples are from the nifti1.h documentation.
 
778
 
 
779
        >>> hdr['slice_code'] = slice_order_codes['sequential increasing']
 
780
        >>> _print_me(hdr.get_slice_times())
758
781
        ['0.0', '0.1', '0.2', '0.3', '0.4', '0.5', '0.6']
759
782
        >>> hdr['slice_start'] = 1
760
783
        >>> hdr['slice_end'] = 5
761
 
        >>> printable_seq(hdr.get_slice_times())
 
784
        >>> _print_me(hdr.get_slice_times())
762
785
        [None, '0.0', '0.1', '0.2', '0.3', '0.4', None]
763
 
        >>> hdr['slice_code'] = slice_order_codes.code['sequential decreasing']
764
 
        >>> printable_seq(hdr.get_slice_times())
 
786
        >>> hdr['slice_code'] = slice_order_codes['sequential decreasing']
 
787
        >>> _print_me(hdr.get_slice_times())
765
788
        [None, '0.4', '0.3', '0.2', '0.1', '0.0', None]
766
 
        >>> hdr['slice_code'] = slice_order_codes.code['alternating increasing']
767
 
        >>> printable_seq(hdr.get_slice_times())
 
789
        >>> hdr['slice_code'] = slice_order_codes['alternating increasing']
 
790
        >>> _print_me(hdr.get_slice_times())
768
791
        [None, '0.0', '0.3', '0.1', '0.4', '0.2', None]
769
 
        >>> hdr['slice_code'] = slice_order_codes.code['alternating decreasing']
770
 
        >>> printable_seq(hdr.get_slice_times())
 
792
        >>> hdr['slice_code'] = slice_order_codes['alternating decreasing']
 
793
        >>> _print_me(hdr.get_slice_times())
771
794
        [None, '0.2', '0.4', '0.1', '0.3', '0.0', None]
772
 
        >>> hdr['slice_code'] = slice_order_codes.code['alternating increasing 2']
773
 
        >>> printable_seq(hdr.get_slice_times())
 
795
        >>> hdr['slice_code'] = slice_order_codes['alternating increasing 2']
 
796
        >>> _print_me(hdr.get_slice_times())
774
797
        [None, '0.2', '0.0', '0.3', '0.1', '0.4', None]
775
 
        >>> hdr['slice_code'] = slice_order_codes.code['alternating decreasing 2']
776
 
        >>> printable_seq(hdr.get_slice_times())
 
798
        >>> hdr['slice_code'] = slice_order_codes['alternating decreasing 2']
 
799
        >>> _print_me(hdr.get_slice_times())
777
800
        [None, '0.4', '0.1', '0.3', '0.0', '0.2', None]
778
801
        '''
779
802
        hdr = self._header_data
1063
1086
        >>> data = np.zeros((2,3,4))
1064
1087
        >>> affine = np.diag([1.0,2.0,3.0,1.0])
1065
1088
        >>> img = Nifti1Image(data, affine)
 
1089
 
 
1090
        At this stage the image files are not defined, and there are no
 
1091
        checks we should run on the nifti magic and offset
 
1092
        
1066
1093
        >>> meta = img.get_metadata()
1067
1094
        >>> str(meta['magic'])
1068
1095
        'n+1'
1075
1102
        True
1076
1103
        >>> str(meta['magic'])
1077
1104
        'n+1'
1078
 
        >>> img.files = {'header':'tmp.nii','image':'tmp.nii'}
1079
 
        >>> img.update_metadata()
 
1105
 
 
1106
        Now we change the files, to reveal the checks.
 
1107
 
 
1108
        >>> from StringIO import StringIO
 
1109
        >>> str_io = StringIO()
 
1110
        >>> files = {'header':str_io,'image':str_io}
 
1111
        >>> img.to_files(files) # implies img.update_metadata()
1080
1112
        >>> str(meta['magic'])
1081
1113
        'n+1'
1082
1114
        >>> int(meta['vox_offset'])
1083
1115
        352
1084
 
        >>> img.files = {'header':'tmp.hdr','image':'tmp.img'}
1085
 
        >>> img.update_metadata()
 
1116
        >>> str_io2 = StringIO()
 
1117
        >>> files = {'header':str_io,'image':str_io2}
 
1118
        >>> img.to_files(files) # implies img.update_metadata()
1086
1119
        >>> str(meta['magic'])
1087
1120
        'ni1'
1088
1121
        >>> int(meta['vox_offset'])
1093
1126
        if not self._affine is None:
1094
1127
            hdr.set_sform(self._affine)
1095
1128
            hdr.set_qform(self._affine)
1096
 
        if not self.files:
 
1129
        if not self._files:
1097
1130
            return
1098
 
        if self.files['header'] == self.files['image']:
 
1131
        if self._files['header'] == self._files['image']:
1099
1132
            # one file version
1100
1133
            if hdr['magic'] == 'n+1':
1101
1134
                return
1114
1147
 
1115
1148
def save(img, filespec):
1116
1149
    img = Nifti1Image.from_image(img)
1117
 
    img.to_files(filespec)
 
1150
    img.to_filespec(filespec)