예제 #1
0
 def data(self):
     if not self.grp.get('data'): return None            # no data exists
     attrs = self.grp['data'].attrs
     if attrs.get('reference'):
         d =  load_nii_or_npy(attrs['reference'])
         return AttrArray(d, attrs)                      # data from external npy or nifti
     else:
         return self.grp['data']                         # data from hdf5
예제 #2
0
    def create_cond(self, condname, run=None, group=None, offset=0, max_len=False, threshold=0, audio_env=None, 
                    base_dir="", nii_files=None, dry_run=False, reference=False, **kwargs):
        """Create condition.

        Generally used by passing cond params from setup, but can be called manually.
        All parameters set as default for each run in the condition.

        Parameters:
            reference: should the data be copied in, or use a reference to data instead?
        """
        cond = self.f['conds'].create_group(condname)
        cond.attrs['offset'] = offset
        cond.attrs['max_len'] = max_len or False     #can't store None
        cond.attrs['threshold'] = threshold
        cond.attrs['prop_pass_thresh'] = .7
        cond.attrs['run'] = run or condname
        cond.attrs['reference'] = reference
        if group: cond.attrs['group'] = group
        cond.create_group('blocks')
        cond.create_group('correlations')
        cond.create_group('analyses')
        
        #Load conditions in from config
        
        if audio_env:
            aud_path = os.path.join(base_dir, audio_env)
            print aud_path
            cond.create_dataset('audio_env', data=load_nii_or_npy(aud_path))
        
        #Don't attempt to load data when nii_files is blank (dummy cond)
        if not nii_files: return

        #Create subject data
        full_query = os.path.join(base_dir, nii_files)
        for m in self.get_subject_files(full_query):
            fname = m.group()
            sub_id = m.groupdict()['sub_id'] #TODO add run_id to mix
            print fname, sub_id
            if not dry_run: self.create_subrun(sub_id, condname, fname, **cond.attrs)
            #have option to do threshold?
            self.f.flush()

        #TODO blocks (pandas)
        if dry_run:
            for k, v in cond.attrs.iteritems(): print k, ':\t\t', v
            del self.f[cond.name]
        return cond
예제 #3
0
 def create_dataset(self, data, overwrite=False, reference=False, compression='gzip', chunks=(1,), **kwargs):
     """Data can be np.ndarray, nifti, or file name. Remaining dimensions for chunks are inferred from data
     
     Even if ref is True, still loads data (to ensure it exists)
     """
     #TODO should fill attributes be mandatory?
     #TODO ref and overwrite args
     if self.data and not overwrite:
         raise BaseException('data already exists')
     if type(data) is str: np_arr = load_nii_or_npy(data)                #string
     elif type(data) is nib.nifti1.Nifti1Image: np_arr = data.get_data() #nifti
     else: np_arr = data                                                 #np array
     #rdy_arr = np_arr.reshape([-1, np_arr.shape[-1]])
     chunks += np_arr.shape[len(chunks):]             #fill in rest of chunk information
     if reference:
        self.grp.create_group('data')                 #make dummy group for attrs 
        reference = data                              # file path
     else:
         self.grp.create_dataset('data', data=np_arr, chunks=chunks, compression=compression)
     if kwargs: self.fill_attributes(reference=reference, **kwargs)
     self.grp.file.flush()
예제 #4
0
 def create_roi(self, roiname, fname):
     """Add new roi to hdf5 in /rois group"""
     roi_data = load_nii_or_npy(fname)
     roi = self.f['rois'].create_dataset(roiname, data=roi_data.astype(bool))
     return roi