def plot_cv_tc(epi_data, session_ids, subject_id, do_plot=True, write_image=True, mask=True, bg_image=False, plot_diff=True, _output_dir=None, cv_tc_plot_outfile=None): """ Compute coefficient of variation of the data and plot it Parameters ---------- epi_data: list of strings, input fMRI 4D images session_ids: list of strings of the same length as epi_data, session indexes (for figures) subject_id: string, id of the subject (for figures) do_plot: bool, optional, should we plot the resulting time course write_image: bool, optional, should we write the cv image mask: bool or string, optional, (string) path of a mask or (bool) should we mask the data bg_image: bool or string, optional, (string) pasth of a background image for display or (bool) should we compute such an image as the mean across inputs. if no, an MNI template is used (works for normalized data) """ if _output_dir is None: if not cv_tc_plot_outfile is None: _output_dir = os.path.dirname(cv_tc_plot_outfile) else: _output_dir = tempfile.mkdtemp() cv_tc_ = [] if isinstance(mask, basestring): mask_array = nibabel.load(mask).get_data() > 0 elif mask == True: mask_array = compute_mask_files(epi_data[0]) else: mask_array = None for (session_id, fmri_file) in zip(session_ids, epi_data): nim = do_3Dto4D_merge(fmri_file, output_dir=_output_dir) affine = nim.get_affine() if len(nim.shape) == 4: # get the data data = nim.get_data() else: raise TypeError("Expecting 4D image!") pass # compute the CV for the session cache_dir = os.path.join(_output_dir, "CV") if not os.path.exists(cache_dir): os.makedirs(cache_dir) mem = joblib.Memory(cachedir=cache_dir, verbose=5) cv = mem.cache(compute_cv)(data, mask_array=mask_array) if write_image: # write an image nibabel.save(nibabel.Nifti1Image(cv, affine), os.path.join(_output_dir, 'cv_%s.nii' % session_id)) if bg_image == False: try: viz.plot_map( cv, affine, threshold=.01, cmap=viz.cm.cold_hot) except IndexError: print traceback.format_exc() else: if isinstance(bg_image, basestring): _tmp = nibabel.load(bg_image) anat, anat_affine = ( _tmp.get_data(), _tmp.get_affine()) else: anat, anat_affine = data.mean(-1), affine try: viz.plot_map( cv, affine, threshold=.01, cmap=viz.cm.cold_hot, anat=anat, anat_affine=anat_affine) except IndexError: print traceback.format_exc() # compute the time course of cv cv_tc_sess = np.median( np.sqrt((data[mask_array > 0].T / data[mask_array > 0].mean(-1) - 1) ** 2), 1) cv_tc_.append(cv_tc_sess) cv_tc = np.concatenate(cv_tc_) if do_plot: # plot the time course of cv for different subjects pl.figure() pl.plot(cv_tc, label=subject_id) pl.legend() pl.xlabel('time(scans)') pl.ylabel('Median coefficient of variation') pl.axis('tight') if not cv_tc_plot_outfile is None: pl.savefig(cv_tc_plot_outfile, bbox_inches="tight", dpi=200) return cv_tc
dataset_description=DATASET_DESCRIPTION, do_shutdown_reloaders=False, ) """collect preprocessed data""" """collect preprocessed data""" fmri_files = results[0]['func'] anat_file = results[0]['anat'] import nibabel as ni if isinstance(fmri_files, basestring): fmri_img = ni.load(fmri_files) else: output_filename = '/tmp/spm_auditory.nii.gz' fmri_img = do_3Dto4D_merge(fmri_files, output_filename=output_filename) fmri_files = output_filename """construct design matrix""" frametimes = np.linspace(0, (n_scans - 1) * tr, n_scans) drift_model = 'Cosine' hrf_model = 'Canonical With Derivative' design_matrix = make_dmtx(frametimes, paradigm, hrf_model=hrf_model, drift_model=drift_model, hfcut=hfcut) """show design matrix""" ax = design_matrix.show() ax.set_position([.05, .25, .9, .65]) ax.set_title('Design matrix') dmat_outfile = os.path.join(subject_data.output_dir, 'design_matrix.png')