Esempio n. 1
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        if t.data.data_type == 'volume':
            dataFn = pyhrf.get_data_file_name('dummySmallBOLD.nii.gz')
            maskFn = pyhrf.get_data_file_name('dummySmallMask.nii.gz')
            sd = FMRISessionVolumicData(bold_file=dataFn)
            t.set_init_param('fmri_data',
                             FmriData.from_vol_ui(mask_file=maskFn,
                                                  sessions_data=[sd]))

        elif t.data.data_type == 'surface':
            fn = 'real_data_surf_tiny_bold.gii'
            dataFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_parcellation.gii'
            maskFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_mesh.gii'
            meshFn = pyhrf.get_data_file_name(fn)
            sd = FMRISessionSurfacicData(bold_file=dataFn)
            t.set_init_param('fmri_data',
                             FmriData.from_surf_ui(mask_file=maskFn,
                                                   mesh_file=meshFn,
                                                   sessions_data=[sd]))
        else:
            raise Exception('Unsupported class ... todo')

        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Esempio n. 2
0
    def test_load_fmri_surf_data(self):
        """ Test surfacic data loading
        """
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        # boldFn = pyhrf.get_data_file_name('localizer_surface_bold.tex')
        # roiMaskFn = pyhrf.get_data_file_name('roimask_gyrii.tex')
        # meshFn = pyhrf.get_data_file_name('right_hemisphere.mesh')
        #g, b, ss, m, h = load_fmri_surf_data([boldFn, boldFn],  meshFn,
        #                                     roiMaskFn)


        # graph, bold, session_scans, mask, edge lengthes
        #pyhrf.verbose.set_verbosity(3)
        g, b, ss, m, el = load_fmri_surf_data([bold_file, bold_file],
                                              mesh_file,
                                              parcel_file)
        assert len(g) == len(np.unique(m))

        if 0:
            first_parcel = g.keys()[0]
            print len(g), 'g[%d]:'%first_parcel, len(g[first_parcel])
            print 'edge lengthes of roi %d:' %first_parcel
            print el[first_parcel]
            print b[first_parcel].shape
            print ss[0][0],'-',ss[0][-1],',',ss[1][0],'-',ss[1][-1]
            print m.shape, _np.unique(m)
Esempio n. 3
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, 'r')
        xml = f.read()
        t = xmlio.from_xml(xml)
        if t.data.data_type == 'volume':
            dataFn = pyhrf.get_data_file_name('dummySmallBOLD.nii.gz')
            maskFn = pyhrf.get_data_file_name('dummySmallMask.nii.gz')
            sd = FMRISessionVolumicData(bold_file=dataFn)
            t.set_init_param(
                'fmri_data',
                FmriData.from_vol_ui(mask_file=maskFn, sessions_data=[sd]))

        elif t.data.data_type == 'surface':
            fn = 'real_data_surf_tiny_bold.gii'
            dataFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_parcellation.gii'
            maskFn = pyhrf.get_data_file_name(fn)
            fn = 'real_data_surf_tiny_mesh.gii'
            meshFn = pyhrf.get_data_file_name(fn)
            sd = FMRISessionSurfacicData(bold_file=dataFn)
            t.set_init_param(
                'fmri_data',
                FmriData.from_surf_ui(mask_file=maskFn,
                                      mesh_file=meshFn,
                                      sessions_data=[sd]))
        else:
            raise Exception('Unsupported class ... todo')

        f = open(xmlFile, 'w')
        f.write(xmlio.to_xml(t))
        f.close()
Esempio n. 4
0
    def setDummyInputData(self, xmlFile):

        f = open(xmlFile, "r")
        xml = f.read()
        t = xmlio.fromXML(xml)
        if t.data.data_type == "volume":
            dataFn = pyhrf.get_data_file_name("dummySmallBOLD.nii.gz")
            maskFn = pyhrf.get_data_file_name("dummySmallMask.nii.gz")
            sd = FMRISessionVolumicData(bold_file=dataFn)
            t.set_init_param("fmri_data", FmriData.from_vol_ui(mask_file=maskFn, sessions_data=[sd]))

        elif t.data.data_type == "surface":
            fn = "real_data_surf_tiny_bold.gii"
            dataFn = pyhrf.get_data_file_name(fn)
            fn = "real_data_surf_tiny_parcellation.gii"
            maskFn = pyhrf.get_data_file_name(fn)
            fn = "real_data_surf_tiny_mesh.gii"
            meshFn = pyhrf.get_data_file_name(fn)
            sd = FMRISessionSurfacicData(bold_file=dataFn)
            t.set_init_param("fmri_data", FmriData.from_surf_ui(mask_file=maskFn, mesh_file=meshFn, sessions_data=[sd]))
        else:
            raise Exception("Unsupported class ... todo")

        f = open(xmlFile, "w")
        f.write(xmlio.toXML(t, handler=NumpyXMLHandler()))
        f.close()
Esempio n. 5
0
    def test_ward_spatial_real_data(self):
        from pyhrf.glm import glm_nipy_from_files

        fn = 'subj0_parcellation.nii.gz'
        mask_file = pyhrf.get_data_file_name(fn)

        bold = 'subj0_bold_session0.nii.gz'
        bold_file = pyhrf.get_data_file_name(bold)

        paradigm_csv_file = pyhrf.get_data_file_name('paradigm_loc_av.csv')
        output_dir = self.tmp_dir
        output_file = op.join(output_dir,
                              'parcellation_output_test_real_data.nii')

        tr = 2.4
        bet = glm_nipy_from_files(bold_file, tr,
                                  paradigm_csv_file, output_dir,
                                  mask_file, session=0,
                                  contrasts=None,
                                  hrf_model='Canonical',
                                  drift_model='Cosine', hfcut=128,
                                  residuals_model='spherical',
                                  fit_method='ols', fir_delays=[0])[0]

        logger.info('betas_files: %s', ' '.join(bet))

        cmd = 'pyhrf_parcellate_glm -m %s %s -o %s -v %d -n %d '\
            '-t ward_spatial ' \
            % (mask_file, ' '.join(bet), output_file,
               logger.getEffectiveLevel(), 10)

        if os.system(cmd) != 0:
            raise Exception('"' + cmd + '" did not execute correctly')
        logger.info('cmd: %s', cmd)
Esempio n. 6
0
    def test_read_default_real_data_tiny(self):
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        cor, tri, mesh_gii = pio.read_mesh(mesh_file)
        bold, bold_gii = pio.read_texture(bold_file)
        parcellation, parcel_gii = pio.read_texture(parcel_file)
Esempio n. 7
0
    def test_read_default_real_data_tiny(self):
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        cor, tri, mesh_gii = pio.read_mesh(mesh_file)
        bold, bold_gii = pio.read_texture(bold_file)
        parcellation, parcel_gii = pio.read_texture(parcel_file)
Esempio n. 8
0
    def setUp(self):
        tag = "subj0_%s.nii.gz"
        self.func_file = pyhrf.get_data_file_name(tag % "bold_session0")
        self.anatomy_file = pyhrf.get_data_file_name(tag % "anatomy")
        self.roi_mask_file = pyhrf.get_data_file_name(tag % "parcellation")

        self.ax_slice = 24
        self.sag_slice = 7
        self.cor_slice = 34

        self.tmp_dir = pyhrf.get_tmp_path()  #'./'
Esempio n. 9
0
    def setUp(self):
        tag = 'subj0_%s.nii.gz'
        self.func_file = pyhrf.get_data_file_name(tag % 'bold_session0')
        self.anatomy_file = pyhrf.get_data_file_name(tag % 'anatomy')
        self.roi_mask_file = pyhrf.get_data_file_name(tag % 'parcellation')

        self.ax_slice = 24
        self.sag_slice = 7
        self.cor_slice = 34

        self.tmp_dir = pyhrf.get_tmp_path()
Esempio n. 10
0
    def test_load_fmri_surf_data(self):
        """ Test surfacic data loading
        """
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        g, b, ss, m, el = pio.load_fmri_surf_data([bold_file, bold_file],
                                                  mesh_file, parcel_file)
        assert len(g) == len(np.unique(m))
Esempio n. 11
0
    def setUp(self):
        tag = 'subj0_%s.nii.gz'
        self.func_file = pyhrf.get_data_file_name(tag%'bold_session0')
        self.anatomy_file = pyhrf.get_data_file_name(tag%'anatomy')
        self.roi_mask_file = pyhrf.get_data_file_name(tag%'parcellation')

        self.ax_slice = 24
        self.sag_slice = 7
        self.cor_slice = 34

        self.tmp_dir = pyhrf.get_tmp_path() #'./'
Esempio n. 12
0
 def test_frmi_vol(self):
     """ Test volumic data loading
     """
     boldFn = get_data_file_name('subj0_bold_session0.nii.gz')
     roiMaskFn = get_data_file_name('subj0_parcellation.nii.gz')
     g, b, ss, m, h = pio.load_fmri_vol_data([boldFn, boldFn], roiMaskFn)
     if 0:
         print len(g), g[1]
         print b[1].shape
         print ss
         print m.shape, np.unique(m)
         print h
Esempio n. 13
0
    def test_load_fmri_surf_data(self):
        """ Test surfacic data loading
        """
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        g, b, ss, m, el = pio.load_fmri_surf_data([bold_file, bold_file],
                                                  mesh_file,
                                                  parcel_file)
        assert len(g) == len(np.unique(m))
Esempio n. 14
0
 def test_frmi_vol(self):
     """ Test volumic data loading
     """
     boldFn = get_data_file_name('subj0_bold_session0.nii.gz')
     roiMaskFn = get_data_file_name('subj0_parcellation.nii.gz')
     g, b, ss, m, h = pio.load_fmri_vol_data([boldFn, boldFn], roiMaskFn)
     if 0:
         print len(g), g[1]
         print b[1].shape
         print ss
         print m.shape, np.unique(m)
         print h
Esempio n. 15
0
    def test_read_default_real_data_tiny(self):
        mesh_file = pyhrf.get_data_file_name('real_data_surf_tiny_mesh.gii')
        bold_file = pyhrf.get_data_file_name('real_data_surf_tiny_bold.gii')
        fn = 'real_data_surf_tiny_parcellation.gii'
        parcel_file = pyhrf.get_data_file_name(fn)

        cor, tri, mesh_gii = read_mesh(mesh_file)
        bold, bold_gii = read_texture(bold_file)
        parcellation, parcel_gii = read_texture(parcel_file)

        if 0:
            print 'cor:', cor.shape, cor.dtype
            print 'tri:', tri.shape, tri.dtype
            print 'bold:', bold.shape, bold.dtype
            print 'parcellation:', parcellation.shape, parcellation.dtype
Esempio n. 16
0
    def _test_spm_option_parse(self, spm_ver):
        """
        Test parsing of option "-s SPM.mat" with given SPM version (int)
        """
        spm_file = op.join(self.tmp_dir, 'SPM_v{}.mat'.format(spm_ver))
        tools._io._zip.gunzip(pyhrf.get_data_file_name('SPM_v%d.mat.gz' % spm_ver),
                              outFileName=spm_file)

        options = ['-s', spm_file]
        from optparse import OptionParser
        parser = OptionParser()
        ptr.append_common_treatment_options(parser)

        fd = ptr.parse_data_options(parser.parse_args(options)[0])

        self.assertEqual(fd.tr, 2.4)  # nb sessions
        p = fd.paradigm
        # nb sessions
        self.assertEqual(len(p.stimOnsets[p.stimOnsets.keys()[0]]), 2)
        npt.assert_almost_equal(p.stimOnsets['audio'][0],
                                ppar.onsets_loc_av['audio'][0])
        npt.assert_almost_equal(p.stimOnsets['audio'][1],
                                ppar.onsets_loc_av['audio'][0])
        npt.assert_almost_equal(p.stimOnsets['video'][1],
                                ppar.onsets_loc_av['video'][0])
Esempio n. 17
0
def load_many_hrf_territories(nb_hrf_territories):
    from pyhrf.tools._io import read_volume
    fn = pyhrf.get_data_file_name('simu_hrf_%d_territories.nii' %
                                  nb_hrf_territories)
    assert op.exists(fn)
    territories = read_volume(fn)[0]
    return territories[np.where(np.ones_like(territories))]
Esempio n. 18
0
def load_many_hrf_territories(nb_hrf_territories):
    from pyhrf.tools._io import read_volume

    fn = pyhrf.get_data_file_name("simu_hrf_%d_territories.nii" % nb_hrf_territories)
    assert op.exists(fn)
    territories = read_volume(fn)[0]
    return territories[np.where(np.ones_like(territories))]
Esempio n. 19
0
 def test_paradigm_csv2(self):
     pfn = get_data_file_name('paradigm_loc_av.csv')
     o, d = pio.load_paradigm_from_csv(pfn, " ")
     if 0:
         print 'onsets:'
         print o
         print 'durations:'
         print d
Esempio n. 20
0
    def test_split4DVol(self):
        s = 'subj0_bold_session0.nii.gz'
        bfn = pyhrf.get_data_file_name(s)
        bold_files = pio.split4DVol(bfn, output_dir=self.tmp_dir)
        i, meta = pio.read_volume(bold_files[0])

        for bf in bold_files:
            os.remove(bf)
Esempio n. 21
0
    def test_split4DVol(self):
        s = 'subj0_bold_session0.nii.gz'
        bfn = pyhrf.get_data_file_name(s)
        bold_files = pio.split4DVol(bfn, output_dir=self.tmp_dir)
        i, meta = pio.read_volume(bold_files[0])

        for bf in bold_files:
            os.remove(bf)
Esempio n. 22
0
 def test_paradigm_csv2(self):
     pfn = get_data_file_name('paradigm_loc_av.csv')
     o, d = pio.load_paradigm_from_csv(pfn, " ")
     if 0:
         print 'onsets:'
         print o
         print 'durations:'
         print d
Esempio n. 23
0
 def _test_load_regnames(self, spm_ver):
     spm_file = op.join(pyhrf.get_tmp_path(), 'SPM.mat')
     pio._zip.gunzip(pyhrf.get_data_file_name('SPM_v%d.mat.gz' % spm_ver),
                     outFileName=spm_file)
     expected = ['Sn(1) audio*bf(1)', 'Sn(1) video*bf(1)',
                 'Sn(2) audio*bf(1)', 'Sn(2) video*bf(1)',
                 'Sn(1) constant', 'Sn(2) constant']
     self.assertEqual(pio.spmio.load_regnames(spm_file), expected)
Esempio n. 24
0
 def _test_load_regnames(self, spm_ver):
     spm_file = op.join(self.tmp_dir, 'SPM_v{}.mat'.format(spm_ver))
     pio._zip.gunzip(pyhrf.get_data_file_name('SPM_v%d.mat.gz' % spm_ver),
                     outFileName=spm_file)
     expected = [
         'Sn(1) audio*bf(1)', 'Sn(1) video*bf(1)', 'Sn(2) audio*bf(1)',
         'Sn(2) video*bf(1)', 'Sn(1) constant', 'Sn(2) constant'
     ]
     self.assertEqual(pio.spmio.load_regnames(spm_file), expected)
Esempio n. 25
0
def load_drawn_labels(name):
    fn = pyhrf.get_data_file_name('simu_labels_%s.png' % name)
    if not op.exists(fn):
        raise Exception('Unknown label map %s (%s)' % (name, fn))

    from scipy.misc import fromimage
    from PIL import Image
    labels = fromimage(Image.open(fn))
    return labels[np.newaxis, :, :]
Esempio n. 26
0
def load_drawn_labels(name):
    fn = pyhrf.get_data_file_name('simu_labels_%s.png' %name)
    if not op.exists(fn):
        raise Exception('Unknown label map %s (%s)' %(name,fn))

    from scipy.misc import fromimage
    from PIL import Image
    labels = fromimage(Image.open(fn))
    return labels[np.newaxis,:,:]
Esempio n. 27
0
    def setUp(self):
        tag = 'subj0_%s.nii.gz'
        func_file = pyhrf.get_data_file_name(tag % 'bold_session0')
        anatomy_file = pyhrf.get_data_file_name(tag % 'anatomy')
        roi_mask_file = pyhrf.get_data_file_name(tag % 'parcellation')

        islice = 24
        cfunc = xndarray.load(func_file).sub_cuboid(time=0, axial=islice)
        cfunc.set_orientation(['coronal', 'sagittal'])
        self.func_data = cfunc.data

        canat = xndarray.load(anatomy_file).sub_cuboid(axial=islice*3)
        canat.set_orientation(['coronal', 'sagittal'])
        self.anat_data = canat.data

        croi_mask = xndarray.load(roi_mask_file).sub_cuboid(axial=islice)
        croi_mask.set_orientation(['coronal', 'sagittal'])
        self.roi_data = croi_mask.data
Esempio n. 28
0
    def setUp(self):
        tag = 'subj0_%s.nii.gz'
        func_file = pyhrf.get_data_file_name(tag%'bold_session0')
        anatomy_file = pyhrf.get_data_file_name(tag%'anatomy')
        roi_mask_file = pyhrf.get_data_file_name(tag%'parcellation')

        islice = 24
        cfunc = xndarray.load(func_file).sub_cuboid(time=0,axial=islice)
        cfunc.set_orientation(['coronal', 'sagittal'])
        self.func_data = cfunc.data

        canat = xndarray.load(anatomy_file).sub_cuboid(axial=islice*3)
        canat.set_orientation(['coronal', 'sagittal'])
        self.anat_data = canat.data

        croi_mask = xndarray.load(roi_mask_file).sub_cuboid(axial=islice)
        croi_mask.set_orientation(['coronal', 'sagittal'])
        self.roi_data = croi_mask.data
Esempio n. 29
0
    def setUp(self):
        tag = "subj0_%s.nii.gz"
        func_file = pyhrf.get_data_file_name(tag % "bold_session0")
        anatomy_file = pyhrf.get_data_file_name(tag % "anatomy")
        roi_mask_file = pyhrf.get_data_file_name(tag % "parcellation")

        islice = 24
        cfunc = xndarray.load(func_file).sub_cuboid(time=0, axial=islice)
        cfunc.set_orientation(["coronal", "sagittal"])
        self.func_data = cfunc.data

        canat = xndarray.load(anatomy_file).sub_cuboid(axial=islice * 3)
        canat.set_orientation(["coronal", "sagittal"])
        self.anat_data = canat.data

        croi_mask = xndarray.load(roi_mask_file).sub_cuboid(axial=islice)
        croi_mask.set_orientation(["coronal", "sagittal"])
        self.roi_data = croi_mask.data
Esempio n. 30
0
def load_hrf_territories(nb_hrf_territories=0, hrf_territories_name=None):
    if hrf_territories_name is not None:
        fn = pyhrf.get_data_file_name('simu_hrf_%s.png' % hrf_territories_name)
        if not op.exists(fn):  # HACK #TODO improve this
            fn = pyhrf.get_data_file_name(hrf_territories_name)

    else:
        fn = pyhrf.get_data_file_name('simu_hrf_%d_territories.png' %
                                      nb_hrf_territories)
    assert op.exists(fn)
    from scipy.misc import fromimage
    from PIL import Image
    territories = fromimage(Image.open(fn))
    territories = territories[np.newaxis, :, :]
    t = territories[np.where(np.ones_like(territories))]
    labels = np.unique(territories)
    for ilab, lab in enumerate(labels):
        t[np.where(t == lab)] = ilab
    return t
Esempio n. 31
0
def load_hrf_territories(nb_hrf_territories=0, hrf_territories_name=None):
    if hrf_territories_name is not None:
        fn = pyhrf.get_data_file_name("simu_hrf_%s.png" % hrf_territories_name)
        if not op.exists(fn):  # HACK #TODO improve this
            fn = pyhrf.get_data_file_name(hrf_territories_name)

    else:
        fn = pyhrf.get_data_file_name("simu_hrf_%d_territories.png" % nb_hrf_territories)
    assert op.exists(fn)
    from scipy.misc import fromimage
    from PIL import Image

    territories = fromimage(Image.open(fn))
    territories = territories[np.newaxis, :, :]
    t = territories[np.where(np.ones_like(territories))]
    labels = np.unique(territories)
    for ilab, lab in enumerate(labels):
        t[np.where(t == lab)] = ilab
    return t
Esempio n. 32
0
    def test_glm_with_files(self):

        output_dir = self.tmp_dir

        bold_name = 'subj0_bold_session0.nii.gz'
        bold_file = pyhrf.get_data_file_name(bold_name)
        tr = 2.4

        paradigm_name = 'paradigm_loc_av.csv'
        paradigm_file = pyhrf.get_data_file_name(paradigm_name)

        mask_name = 'subj0_parcellation.nii.gz'
        mask_file = pyhrf.get_data_file_name(mask_name)

        from pyhrf.glm import glm_nipy_from_files
        glm_nipy_from_files(bold_file, tr, paradigm_file, output_dir,
                            mask_file)

        self.assertTrue(op.exists(output_dir))
Esempio n. 33
0
    def test_glm_with_files(self):

        output_dir = self.tmp_dir

        bold_name = 'subj0_bold_session0.nii.gz'
        bold_file = pyhrf.get_data_file_name(bold_name)
        tr = 2.4

        paradigm_name = 'paradigm_loc_av.csv'
        paradigm_file = pyhrf.get_data_file_name(paradigm_name)

        mask_name = 'subj0_parcellation.nii.gz'
        mask_file = pyhrf.get_data_file_name(mask_name)

        from pyhrf.glm import glm_nipy_from_files
        glm_nipy_from_files(bold_file, tr, paradigm_file, output_dir,
                            mask_file)

        self.assertTrue(op.exists(output_dir))
Esempio n. 34
0
def load_drawn_labels(name):
    fn = pyhrf.get_data_file_name("simu_labels_%s.png" % name)
    if not op.exists(fn):
        raise Exception("Unknown label map %s (%s)" % (name, fn))

    from scipy.misc import fromimage
    from PIL import Image

    labels_image = Image.open(fn)
    labels_image = labels_image.convert("L")
    labels = fromimage(labels_image)
    return labels[np.newaxis, :, :]
Esempio n. 35
0
    def test_split4DVol(self):
        s = 'subj0_bold_session0.nii.gz'
        bfn = pyhrf.get_data_file_name(s)
        bold_files = split4DVol(bfn, output_dir=self.tmp_dir)
        #print bold_files
        i,meta = read_volume(bold_files[0])

        if 0:
            affine, header = meta
            print ''
            print 'one vol shape:'
            print i.shape
            print 'header:'
            pprint.pprint(dict(header))

        for bf in bold_files:
            os.remove(bf)
Esempio n. 36
0
    def from_cmd_options(self, options):

        if options.func_data_files is None:
            options.func_data_files = [DEFAULT_BOLD]

        if op.splitext(options.paradigm)[-1] == '.mat' or \
          op.splitext(options.paradigm)[-1] == '.csv':
            paradigm_file = options.paradigm
        else:
            if options.paradigm not in builtin_paradigms:
                raise Exception('Paradigm label is not in built in paradigms. '\
                                'Choices are: (%s)' %','.join(builtin_paradigms))

            paradigm_file = pyhrf.get_data_file_name('paradigm_%s.csv' \
                                                     %options.paradigm)

        msdui = MultiSessionsDataUI(paradigm_file, options.func_data_files)
        return msdui
Esempio n. 37
0
    def test_process_history_extension(self):
        nii_fn = pyhrf.get_data_file_name(
            'real_data_vol_4_regions_mask.nii.gz')

        nii_fn_out = op.join(self.tmp_dir, 'proc_ext_test.nii')
        input_pname = 'dummy_proc_test'
        input_pparams = {'my_param': 5.5, 'input_file': '/home/blh'}

        pio.append_process_info(nii_fn, input_pname, input_pparams,
                                img_output_fn=nii_fn_out)

        i2, (aff, header) = pio.read_volume(nii_fn_out)

        reloaded_pinfo = pio.get_process_info(nii_fn_out)
        self.assertNotEqual(reloaded_pinfo, None)
        self.assertEqual(reloaded_pinfo[0]['process_name'], input_pname)
        self.assertEqual(reloaded_pinfo[0]['process_inputs'], input_pparams)
        self.assertEqual(reloaded_pinfo[0]['process_version'], None)
        self.assertEqual(reloaded_pinfo[0]['process_id'], None)
Esempio n. 38
0
    def test_process_history_extension(self):
        nii_fn = pyhrf.get_data_file_name(
            'real_data_vol_4_regions_mask.nii.gz')

        nii_fn_out = op.join(self.tmp_dir, 'proc_ext_test.nii')
        input_pname = 'dummy_proc_test'
        input_pparams = {'my_param': 5.5, 'input_file': '/home/blh'}

        pio.append_process_info(nii_fn,
                                input_pname,
                                input_pparams,
                                img_output_fn=nii_fn_out)

        i2, (aff, header) = pio.read_volume(nii_fn_out)

        reloaded_pinfo = pio.get_process_info(nii_fn_out)
        self.assertNotEqual(reloaded_pinfo, None)
        self.assertEqual(reloaded_pinfo[0]['process_name'], input_pname)
        self.assertEqual(reloaded_pinfo[0]['process_inputs'], input_pparams)
        self.assertEqual(reloaded_pinfo[0]['process_version'], None)
        self.assertEqual(reloaded_pinfo[0]['process_id'], None)
Esempio n. 39
0
    def test_voronoi_with_seeds(self):

        import os.path as op
        from pyhrf.ndarray import xndarray
        import pyhrf
        fn = 'subj0_parcellation.nii.gz'
        mask_file = pyhrf.get_data_file_name(fn)

        orientation = ['axial', 'coronal', 'sagittal']
        seeds = xndarray.xndarray_like(
            xndarray.load(mask_file)).reorient(orientation)

        seed_coords = np.array([[24, 35, 8],  # axial, coronal, sagittal
                                [27, 35, 5],
                                [27, 29, 46],
                                [31, 28, 46]])

        seeds.data[:] = 0
        seeds.data[tuple(seed_coords.T)] = 1

        seed_file = op.join(self.tmp_dir, 'voronoi_seeds.nii')
        seeds.save(seed_file, set_MRI_orientation=True)

        output_file = op.join(self.tmp_dir, 'voronoi_parcellation.nii')
        cmd = 'pyhrf_parcellate_spatial %s -m voronoi -c %s -o %s -v %d' \
            % (mask_file, seed_file, output_file, logger.getEffectiveLevel())

        if os.system(cmd) != 0:
            raise Exception('"' + cmd + '" did not execute correctly')

        logger.info('cmd: %s', cmd)

        assert op.exists(output_file)
        parcellation = xndarray.load(output_file)

        n_parcels = len(np.unique(parcellation.data)) - 1

        self.assertEqual(n_parcels, len(seed_coords))
Esempio n. 40
0
def plot_detection_results(fig_dir, poi, condition, coi, parcellation_file,
                           plot_label, jde_output_dir, glm_hcano_rs_output_dir,
                           fig_dpi=100):
    """
    coi (str): contrast of interest
    poi (dict): defines the point of interest for plots of HRFs and maps
    """
    if condition == 'audio':
        condition = 'phraseaudio'

    orientation = ['coronal',  'sagittal']
    axial_slice =  poi['axial']

    anat_file = get_data_file_name('real_data_vol_4_regions_anatomy.nii.gz')

    parcellation = xndarray.load(parcellation_file)
    parcellation = parcellation.sub_cuboid(axial=axial_slice)
    parcellation = parcellation.reorient(orientation)

    ## Detection maps
    detection_plots_params = []

    #JDE NRLs
    fn = op.join(jde_output_dir, 'jde_mcmc_nrl_pm.nii.gz')

    slice_def = {'axial':axial_slice, 'condition':condition}
    fig_fn = op.join(fig_dir, 'real_data_jde_mcmc_nrls_%s.png' %condition)
    detection_plots_params.append({'fn':fn, 'slice_def':slice_def,
                                   'mask': parcellation.data,
                                   'output_fig_fn':fig_fn})

    #GLM hcano
    fn = op.join(glm_hcano_rs_output_dir, 'glm_hcano_rs_beta_%s.nii.gz' %condition)

    slice_def = {'axial':axial_slice}

    fig_fn = op.join(fig_dir, 'real_data_glm_hcano_rs_%s.png'%condition)

    detection_plots_params.append({'fn':fn, 'slice_def':slice_def,
                                   'mask': (parcellation.data != 0),
                                   'output_fig_fn':fig_fn})


    perf_norm = plot_maps(detection_plots_params, anat_file,
                          {'axial':axial_slice*3},
                          fig_dir, orientation=orientation,
                          crop_extension=None, plot_anat=True)

    palette_fig_fn = op.join(fig_dir, 'real_data_detection_%s_palette.png' \
                             %condition)
    plot_palette(cmap, perf_norm, 45)
    plt.savefig(palette_fig_fn, dpi=fig_dpi)
    autocrop(palette_fig_fn)


    #JDE Contrast
    fn = op.join(jde_output_dir, 'jde_mcmc_nrl_contrasts.nii.gz')

    slice_def = {'axial':axial_slice, 'contrast':coi}
    fig_fn = op.join(fig_dir, 'real_data_jde_mcmc_con_%s.png' %coi)
    detection_plots_params.append({'fn':fn, 'slice_def':slice_def,
                                   'mask': parcellation.data,
                                   'output_fig_fn':fig_fn})

    #GLM hcano
    fn = op.join(glm_hcano_rs_output_dir, 'glm_hcano_rs_con_effect_%s.nii.gz'%coi)

    slice_def = {'axial':axial_slice}

    fig_fn = op.join(fig_dir, 'real_data_glm_hcano_rs_con_%s.png' %coi)

    detection_plots_params.append({'fn':fn, 'slice_def':slice_def,
                                   'mask': (parcellation.data != 0),
                                   'output_fig_fn':fig_fn})


    perf_norm = plot_maps(detection_plots_params, anat_file,
                          {'axial':axial_slice*3},
                          fig_dir, orientation=orientation,
                          crop_extension=None, plot_anat=True)

    palette_fig_fn = op.join(fig_dir, 'real_data_detection_con_%s_palette.png'
                             %coi)
    plot_palette(cmap, perf_norm, 45)
    plt.savefig(palette_fig_fn, dpi=fig_dpi)
    autocrop(palette_fig_fn)
Esempio n. 41
0
 def test_split_ext(self):
     bfn = pyhrf.get_data_file_name('subj0_bold_session0.nii.gz')
     pio.split_ext_safe(bfn)
Esempio n. 42
0
 def test_read_tex_gii_label(self):
     tex_fn = 'real_data_surf_tiny_parcellation.gii'
     tex_fn = pyhrf.get_data_file_name(tex_fn)
     t, tgii = pio.read_texture(tex_fn)
Esempio n. 43
0
# -*- coding: utf-8 -*-
#
"""
Compute the mean of BOLD signal within parcels.

This is an example of several operations for xndarray:
- explosion of data according to a parcellation mask
- mean over voxel
- merge of several xndarray objects
"""
import os.path as op
from pyhrf import get_data_file_name, get_tmp_path
from pyhrf.ndarray import xndarray, merge

func_data = xndarray.load(get_data_file_name('subj0_bold_session0.nii.gz'))
parcellation = xndarray.load(get_data_file_name('subj0_parcellation.nii.gz'))
parcel_fdata = func_data.explode(parcellation)
parcel_means = dict((parcel_id, d.copy().fill(d.mean('position')))
                    for parcel_id, d in parcel_fdata.items())
parcel_means = merge(parcel_means, parcellation, axis='position')
output_fn = op.join(get_tmp_path(), './subj0_bold_parcel_means.nii')
print 'File saved to:', output_fn
parcel_means.save(output_fn)
#TODO test full script
Esempio n. 44
0
 def test_split_ext(self):
     bfn = pyhrf.get_data_file_name('subj0_bold_session0.nii.gz')
     pio.split_ext_safe(bfn)
Esempio n. 45
0
        def setUp(self):
            np.random.seed(8652761)

            tmpDir = tempfile.mkdtemp(prefix='pyhrf_tests',
                                      dir=pyhrf.cfg['global']['tmp_path'])
            self.tmp_dir = tmpDir
            self.clean_tmp = True

            bf = 'subj0_bold_session0.nii.gz'
            self.boldFiles = [pyhrf.get_data_file_name(bf)]
            pf = 'subj0_parcellation.nii.gz'
            self.parcelFile = pyhrf.get_data_file_name(pf)
            self.tr = 2.4
            self.dt = .6
            self.onsets = pyhrf.onsets_loc_av
            self.durations = None
            self.nbIt = 3
            self.pfMethod = 'es'

            simu = simulate_bold(self.tmp_dir, spatial_size='random_small')
            self.data_simu = FmriData.from_simulation_dict(simu)

            self.sampler_params_for_single_test = {
                BG.P_NB_ITERATIONS: 100,
                BG.P_SMPL_HIST_PACE: 1,
                BG.P_OBS_HIST_PACE: 1,
                # level of spatial correlation = beta
                BG.P_BETA: BS({
                    BS.P_SAMPLE_FLAG: False,
                    BS.P_USE_TRUE_VALUE: False,
                    BS.P_VAL_INI: np.array([0.6]),
                }),
                # HRF
                BG.P_HRF: HRFARSampler({
                    HS.P_SAMPLE_FLAG: False,
                    HS.P_USE_TRUE_VALUE: True,
                    HS.P_PRIOR_TYPE: 'singleHRF',
                }),
                # HRF variance
                BG.P_RH: HVS({
                    HVS.P_USE_TRUE_VALUE: True,
                    HVS.P_SAMPLE_FLAG: False,
                }),
                # neural response levels (stimulus-induced effects)
                BG.P_NRLS: NRLARSampler({
                    NS.P_USE_TRUE_NRLS: True,
                    NS.P_USE_TRUE_LABELS: True,
                    NS.P_SAMPLE_FLAG: False,
                    NS.P_SAMPLE_LABELS: False,
                }),
                BG.P_MIXT_PARAM: BGMS({
                    BGMS.P_SAMPLE_FLAG: False,
                    BGMS.P_USE_TRUE_VALUE: True,
                }),
                BG.P_NOISE_VAR: NoiseVarianceARSampler({
                    NoiseVarianceARSampler.P_SAMPLE_FLAG: False,
                    NoiseVarianceARSampler.P_USE_TRUE_VALUE: True,
                }),
                BG.P_NOISE_ARP: NoiseARParamsSampler({
                    NoiseARParamsSampler.P_SAMPLE_FLAG: False,
                    NoiseARParamsSampler.P_USE_TRUE_VALUE: True,
                }),

                BG.P_CHECK_FINAL_VALUE: 'raise',  # print or raise
            }

            self.sampler_params_for_full_test = {
                BG.P_NB_ITERATIONS: 500,
                BG.P_SMPL_HIST_PACE: 1,
                BG.P_OBS_HIST_PACE: 1,
                # level of spatial correlation = beta
                BG.P_BETA: BS({
                    BS.P_SAMPLE_FLAG: True,
                    BS.P_USE_TRUE_VALUE: False,
                    BS.P_VAL_INI: np.array([0.6]),
                }),
                # HRF
                BG.P_HRF: HRFARSampler({
                    HS.P_SAMPLE_FLAG: True,
                    HS.P_USE_TRUE_VALUE: False,
                    HS.P_NORMALISE: 1.,
                }),
                # HRF variance
                BG.P_RH: HVS({
                    HVS.P_USE_TRUE_VALUE: False,
                    HVS.P_SAMPLE_FLAG: True,
                }),
                # neural response levels (stimulus-induced effects)
                BG.P_NRLS: NRLARSampler({
                    NS.P_USE_TRUE_NRLS: False,
                    NS.P_USE_TRUE_LABELS: False,
                    NS.P_SAMPLE_FLAG: True,
                    NS.P_SAMPLE_LABELS: True,
                }),
                BG.P_MIXT_PARAM: BGMS({
                    BGMS.P_SAMPLE_FLAG: True,
                    BGMS.P_USE_TRUE_VALUE: False,
                }),
                BG.P_NOISE_VAR: NoiseVarianceARSampler({
                    NoiseVarianceARSampler.P_SAMPLE_FLAG: True,
                    NoiseVarianceARSampler.P_USE_TRUE_VALUE: False,
                }),
                BG.P_NOISE_ARP: NoiseARParamsSampler({
                    NoiseARParamsSampler.P_SAMPLE_FLAG: True,
                    NoiseARParamsSampler.P_USE_TRUE_VALUE: False,
                }),
                BG.P_CHECK_FINAL_VALUE: 'print',  # print or raise
            }
Esempio n. 46
0
 def test_paradigm_csv4(self):
     pfn = get_data_file_name('paradigm_loc_av_comma.csv')
     o, d = pio.load_paradigm_from_csv(pfn, ",")
Esempio n. 47
0
 def setUp(self):
     pf = 'subj0_parcellation.nii.gz'
     fnm = pyhrf.get_data_file_name(pf)
     m, mh = read_volume(fnm)
     self.graph = parcels_to_graphs(m.astype(int), kerMask3D_6n,
                                    toDiscard=[0])[1]
Esempio n. 48
0
    def setUp(self):
        np.random.seed(8652761)
        tmpDir = tempfile.mkdtemp(prefix='pyhrf_tests',
                                  dir=pyhrf.cfg['global']['tmp_path'])
        self.tmp_dir = tmpDir
        self.clean_tmp = True
        bf = 'subj0_bold_session0.nii.gz'
        self.boldFiles = [pyhrf.get_data_file_name(bf)]
        pf = 'subj0_parcellation.nii.gz'
        self.parcelFile = pyhrf.get_data_file_name(pf)
        self.tr = 2.4
        self.dt = .6
        self.onsets = pyhrf.paradigm.onsets_loc_av
        self.durations = None
        self.nbIt = 3
        self.pfMethod = 'es'

        simu = simulate_bold(self.tmp_dir, spatial_size='random_small')
        self.data_simu = FmriData.from_simulation_dict(simu)

        self.sampler_params_for_single_test = {
            'nb_iterations': 100,
            'smpl_hist_pace': 1,
            'obs_hist_pace': 1,
            # level of spatial correlation = beta
            'beta': BS({
                'do_sampling': False,
                'use_true_value': False,
                'val_ini': np.array([0.6]),
            }),
            # HRF
            'hrf': HS({
                'do_sampling': False,
                'use_true_value': True,
                'prior_type': 'singleHRF',
            }),
            # HRF variance
            'hrf_var': HVS({
                'use_true_value': True,
                'do_sampling': False,
            }),
            # neural response levels (stimulus-induced effects)
            'nrl': NS({
                'use_true_nrls': True,
                'use_true_labels': True,
                'do_sampling': False,
                'do_label_sampling': False,
            }),
            'mixt_params': BGMS({
                'do_sampling': False,
                'use_true_value': True,
            }),
            'noise_var': NoiseVarianceSampler({
                'do_sampling': False,
                'use_true_value': True,
            }),
            'check_final_value_close_to_true': 'raise',  # print or raise
        }

        self.sampler_params_for_full_test = {
            'nb_iterations': 500,
            'smpl_hist_pace': 1,
            'obs_hist_pace': 1,
            # level of spatial correlation = beta
            'beta': BS({
                'do_sampling': True,
                'use_true_value': False,
                'val_ini': np.array([0.6]),
            }),
            # HRF
            'hrf': HS({
                'do_sampling': True,
                'use_true_value': False,
                'normalise': 1.,
            }),
            # HRF variance
            'hrf_var': HVS({
                'use_true_value': False,
                'do_sampling': True,
            }),
            # neural response levels (stimulus-induced effects)
            'nrl': NS({
                'use_true_nrls': False,
                'use_true_labels': False,
                'do_sampling': True,
                'do_label_sampling': True,
            }),
            'mixt_params': BGMS({
                'do_sampling': True,
                'use_true_value': False,
            }),
            'noise_var': NoiseVarianceSampler({
                'do_sampling': True,
                'use_true_value': False,
            }),
            'check_final_value_close_to_true': 'print',  # print or raise
        }
Esempio n. 49
0
    graph = property(get_graph)

    def _store_mask_sparse(self, mask):

        self.np_mask = np.where(mask != self.bg_label)
        self.roi_ids_in_mask = mask[self.np_mask]
        self.nb_voxels_in_mask = len(self.roi_ids_in_mask)
        self.spatial_shape = mask.shape



from pyhrf import get_data_file_name
from pyhrf.tools.io import read_mesh
from pyhrf.graph import graph_from_mesh
DEFAULT_DATA_TYPE = 'volume'
DEFAULT_MASK = get_data_file_name('subj0_parcellation.nii.gz')
DEFAULT_TR = 2.4
DEFAULT_ONSETS = OrderedDict(
    (('audio' , [np.array([ 15.,20.7,29.7,35.4,44.7,48.,83.4,89.7,108.,
                            119.4, 135., 137.7, 146.7, 173.7, 191.7, 236.7,
                            251.7, 284.4, 293.4, 296.7])]),
     ('video' , [np.array([ 0., 2.4, 8.7,33.,39.,41.7, 56.4, 59.7, 75., 96.,
                            122.7, 125.4, 131.4, 140.4, 149.4, 153., 156., 159.,
                           164.4, 167.7, 176.7, 188.4, 195., 198., 201., 203.7,
                            207., 210., 218.7, 221.4, 224.7, 234., 246., 248.4,
                            260.4, 264., 266.7, 269.7, 278.4, 288. ])])
     )
    )
DEFAULT_STIM_DURATIONS = OrderedDict(
    (('audio', [np.array([])]),
     ('video', [np.array([])]))
Esempio n. 50
0
def load_vol_bold_and_mask(bold_files, mask_file):

    from pyhrf.tools._io import read_volume, discard_bad_data

    # Handle mask
    if not op.exists(mask_file):
        logger.warning(
            'Mask file %s does not exist. Mask is '
            'computed from BOLD ...', mask_file)

        bold_file = 'subj0_bold_session0.nii.gz'
        # HACK
        if bold_files[0] == pyhrf.get_data_file_name(bold_file):
            max_frac = .99999  # be sure to keep non zero voxels
            connect_component = False  # default BOLD vol has 2 ROIs
        else:
            max_frac = .9
            connect_component = True
        compute_mask_files(bold_files[0],
                           mask_file,
                           False,
                           .4,
                           max_frac,
                           cc=connect_component)
        mask_loaded_from_file = False
    else:
        mask_loaded_from_file = True
        logger.info('Assuming orientation for mask file: ' +
                    string.join(MRI3Daxes, ','))

    logger.info('Read mask from: %s', mask_file)
    mask, mask_meta_obj = read_volume(mask_file)

    if not np.allclose(np.round(mask), mask):
        raise Exception("Mask is not n-ary (%s)" % mask_file)
    mask = np.round(mask).astype(np.int32)

    logger.info(
        'Mask has shape %s\nMask min value: %d\n'
        'Mask max value: %d\nMask has %d parcels', str(mask.shape), mask.min(),
        mask.max(), len(np.unique(mask)))

    if mask.min() == -1:
        mask += 1

    # Load BOLD:
    last_scan = 0
    session_scans = []
    bolds = []
    logger.info('Assuming orientation for BOLD files: ' +
                string.join(MRI4Daxes, ','))

    if type(bold_files[0]) is list:
        bold_files = bold_files[0]

    for bold_file in bold_files:
        if not op.exists(bold_file):
            raise Exception('File not found: ' + bold_file)

        bold, _ = read_volume(bold_file)
        bolds.append(bold)
        session_scans.append(
            np.arange(last_scan, last_scan + bold.shape[TIME_AXIS], dtype=int))
        last_scan += bold.shape[TIME_AXIS]

    bold = np.concatenate(tuple(bolds), axis=TIME_AXIS)

    logger.info('BOLD has shape %s', str(bold.shape))
    discard_bad_data(bold, mask)

    return mask, mask_meta_obj, mask_loaded_from_file, bold, session_scans
Esempio n. 51
0
 def test_read_tex_gii_label(self):
     tex_fn = 'real_data_surf_tiny_parcellation.gii'
     tex_fn = pyhrf.get_data_file_name(tex_fn)
     t, tgii = pio.read_texture(tex_fn)
Esempio n. 52
0
 def test_paradigm_csv4(self):
     pfn = get_data_file_name('paradigm_loc_av_comma.csv')
     o, d = pio.load_paradigm_from_csv(pfn, ",")
Esempio n. 53
0
def load_vol_bold_and_mask(bold_files, mask_file):

    from pyhrf.tools._io import read_volume, discard_bad_data

    # Handle mask
    if not op.exists(mask_file):
        logger.warning('Mask file %s does not exist. Mask is '
                       'computed from BOLD ...', mask_file)

        bold_file = 'subj0_bold_session0.nii.gz'
        # HACK
        if bold_files[0] == pyhrf.get_data_file_name(bold_file):
            max_frac = .99999  # be sure to keep non zero voxels
            connect_component = False  # default BOLD vol has 2 ROIs
        else:
            max_frac = .9
            connect_component = True
        compute_mask_files(bold_files[0], mask_file, False, .4,
                           max_frac, cc=connect_component)
        mask_loaded_from_file = False
    else:
        mask_loaded_from_file = True
        logger.info('Assuming orientation for mask file: ' +
                    string.join(MRI3Daxes, ','))

    logger.info('Read mask from: %s', mask_file)
    mask, mask_meta_obj = read_volume(mask_file)

    if not np.allclose(np.round(mask), mask):
        raise Exception("Mask is not n-ary (%s)" % mask_file)
    mask = np.round(mask).astype(np.int32)

    logger.info('Mask has shape %s\nMask min value: %d\n'
                'Mask max value: %d\nMask has %d parcels',
                str(mask.shape), mask.min(), mask.max(), len(np.unique(mask)))

    if mask.min() == -1:
        mask += 1

    # Load BOLD:
    last_scan = 0
    session_scans = []
    bolds = []
    logger.info('Assuming orientation for BOLD files: ' +
                string.join(MRI4Daxes, ','))

    if type(bold_files[0]) is list:
        bold_files = bold_files[0]

    for bold_file in bold_files:
        if not op.exists(bold_file):
            raise Exception('File not found: ' + bold_file)

        bold, _ = read_volume(bold_file)
        bolds.append(bold)
        session_scans.append(np.arange(last_scan,
                                       last_scan + bold.shape[TIME_AXIS],
                                       dtype=int))
        last_scan += bold.shape[TIME_AXIS]

    bold = np.concatenate(tuple(bolds), axis=TIME_AXIS)

    logger.info('BOLD has shape %s', str(bold.shape))
    discard_bad_data(bold, mask)

    return mask, mask_meta_obj, mask_loaded_from_file, bold, session_scans
Esempio n. 54
0
def main():

    pyhrf.verbose.set_verbosity(1)

    output_dir = './'
    bold_file = get_data_file_name('real_data_vol_4_regions_BOLD.nii.gz')
    mask_file = get_data_file_name('real_data_vol_4_regions_mask.nii.gz')
    paradigm_file = get_data_file_name('paradigm_loc.csv')

    contrasts = pyhrf.paradigm.default_contrasts_loc

    experiments = [ ('audio', 'audio-video',
                     {'axial':17, 'coronal':42, 'sagittal':9}, 1,
                     'temporal', -1., 1.05),
                     ('calculaudio', 'computation-sentences',
                     {'axial':30, 'coronal':25, 'sagittal':53}, 11,
                     'parietal', -1.55, 1.05),
                   ]


    parcellation_dir = get_dir(output_dir, 'parcellation')

    # Load data
    fdata = FmriData.from_vol_files(mask_file=mask_file,
                                    bold_files=[bold_file],
                                    paradigm_csv_file=paradigm_file)

    glm_hcano_output_dir = get_dir(output_dir, 'glm_cano')
    if 1:
        # GLM with canonical HRF
        print 'GLM with canonical HRF'
        glm_analyse(fdata, contrasts, output_dir=glm_hcano_output_dir,
                    output_prefix='glm_hcano_')

    if 1:
        # GLM with basis set
        print 'GLM with basis set'
        glm_hderiv_output_dir = get_dir(output_dir, 'glm_hderivs')
        glm_analyse(fdata, contrasts, hrf_model="Canonical with Derivative",
                    output_dir=glm_hderiv_output_dir,
                    output_prefix='glm_hderivs_')

    parcellation_file =  op.join(parcellation_dir,  'parcellation_func.nii.gz')
    if 1:
        # parcellation from results of GLM basis set

        mask_file = op.join(glm_hderiv_output_dir, 'glm_hderivs_mask.nii.gz')
        beta_files = [op.join(glm_hderiv_output_dir,'glm_hderivs_beta_%s.nii.gz'%c)\
                              for c in fdata.get_condition_names()]
        make_parcellation_from_files(beta_files, mask_file, parcellation_file,
                                     nparcels=20, method='ward_and_gkm')
    jde_output_dir = get_dir(output_dir, 'jde')
    if 1:
        # JDE
        print 'JDE'
        fdata_parc = FmriData.from_vol_files(mask_file=parcellation_file,
                                             bold_files=[bold_file],
                                             paradigm_csv_file=paradigm_file)
        jde_analyse(fdata_parc, contrasts, jde_output_dir)

    glm_hcano_rs_output_dir = get_dir(output_dir,'glm_hcano_rescaled_on_jde')
    if 1:
        # GLM hcano rescaled onto JDE (provide the same results as normal
        # GLM hcano but with effects resized so that there is a consistency btw
        # X^m.h in JDE and the corresponding column of the design matrix in GLM
        print 'GLM rescaled'
        rescale_factor_file = op.join(jde_output_dir, 'scale_factor_for_glm.nii.gz')
        compute_jde_glm_rescaling(jde_output_dir, glm_hcano_output_dir,
                                  rescale_factor_file)

        glm_analyse(fdata, contrasts, output_dir=glm_hcano_rs_output_dir,
                    output_prefix='glm_hcano_rs_',
                    rescale_factor_file=rescale_factor_file)

    ## Outputs
    for condition_of_interest, contrast_of_interest, point_of_interest, \
        parcel_of_interest, plot_label, ymin, ymax  in experiments:

        if 'temporal' in plot_label:
            paradigm_tag = 'loc_av'
        else:
            paradigm_tag = 'loc'

        paradigm_file = get_data_file_name('paradigm_%s.csv' %paradigm_tag)
        fir_mask = op.join(parcellation_dir, 'mask_single_voxel_for_fir_%s.nii.gz'\
                           %paradigm_tag)
        make_mask_from_points([point_of_interest], mask_file, fir_mask)
        fdata_fir = FmriData.from_vol_files(mask_file=fir_mask,
                                            bold_files=[bold_file],
                                            paradigm_csv_file=paradigm_file)

        glm_fir_output_dir = get_dir(output_dir, 'glm_fir_%s' %paradigm_tag)
        if 1:
            # GLM FIR
            print 'GLM FIR'
            glm_analyse(fdata_fir, contrasts={}, hrf_model="FIR",
                        output_dir=glm_fir_output_dir, output_prefix='glm_fir_',
                        fir_delays=range(11))



        rfir_output_dir = get_dir(output_dir, 'rfir_%s' %paradigm_tag)
        if 1:
            # Regularized FIR
            print 'RFIR'
            rfir_analyse(fdata_fir, output_dir=rfir_output_dir)


        fig_dir = get_dir(output_dir, 'figs')
        plot_detection_results(fig_dir, point_of_interest, condition_of_interest,
                               contrast_of_interest, parcellation_file,
                               plot_label, jde_output_dir,
                               glm_hcano_rs_output_dir)

        plot_estimation_results(fig_dir, point_of_interest, parcel_of_interest,
                                condition_of_interest, plot_label,
                                glm_fir_output_dir, rfir_output_dir,
                                jde_output_dir, ymin, ymax)