示例#1
0
    outdir = os.path.join(basedir, 'ic_files')
    subic_globstr = os.path.join(basedir, '*sub*_component_ica_s1_.nii') # search string for sub ic files
    ic4d_globstr = os.path.join(outdir, 'dr_stage2_ic*_4D.nii.gz')
    subid_pattern = u'sub[0-9]{3}'
    #######################################

    # Make output directory
    if os.path.isdir(outdir)==False:
        os.mkdir(outdir)
    else:
        raise OSError('%s exists, remove to re-run'%outdir)

    # Split files of each subject containing all ic's
    infiles = glob(subic_globstr)
    for subfile in infiles:
        subid = pydr.get_subid(subfile, pattern=subid_pattern)
        allic = pydr.split_components(subfile, subid, outdir)
    
    # Merge files of each ic across subjects  
    for cn, item in enumerate(allic): #Search through ics using last subject's output
        datadir, ic = os.path.split(item)       
        subid = pydr.get_subid(item, pattern=subid_pattern)
        globstr = ic.replace(subid, '*')
        mergefile, subject_order = pydr.merge_components(datadir,
                                                      globstr = globstr,
                                                      subid_pattern = subid_pattern)
        outfile = os.path.join(outdir, 'subject_order_ic%04d'%cn)
        with open(outfile, 'w+') as fid: 
            fid.write('\n'.join(subject_order)) #Write out subject order for each ic
    
    # Rename files with ic count starting at 1 and unzip      
                                'B*.ica', # subject speccific
                                'reg_standard', # registered to standard
                                'filtered_func_data.nii.gz') # data
        infiles = glob(globstr)
        infiles.sort()



    ### RUN DUAL REGRESSION
    ############################
    startdir = os.getcwd()
    os.chdir(outdir)
    
    subd = {}
    for tmpf in infiles: #subject-wise
        subid = pydr.get_subid(tmpf)
        ###Run dr_stage1
        txtf = pydr.template_timeseries_sub(tmpf, template, mask, outdir)

        ###Run dr_stage2
        ## If you want to add movement params & spike regressors to stage2 of model
        ## mvtfile = <confound file> 
        ## Must change input of mvt parameter from None in dr_stage2 below
        sub_icadir = ''.join([subid, sub_icadirname]) 
        mvtfile = os.path.join(basedir,
                            subid,
                            'func',
                            'confound_regressors_6mm.txt') #Name of confound file
                            
        ## If you want residuals to be output, change out_res to True below
        stage2_ts, stage2_tsz = pydr.sub_spatial_map(tmpf, txtf, mask, outdir,