def prep_cwas_workflow(c, subject_infos):
    print('Preparing CWAS workflow')
    p_id, s_ids, scan_ids, s_paths = (list(tup) for tup in zip(*subject_infos))
    print('Subjects', s_ids)

    wf = pe.Workflow(name='cwas_workflow')
    wf.base_dir = c.workingDirectory

    from CPAC.cwas import create_cwas
    import numpy as np
    regressor = np.loadtxt(c.cwasRegressorFile)

    cw = create_cwas()
    cw.inputs.inputspec.roi         = c.cwasROIFile
    cw.inputs.inputspec.subjects    = s_paths
    cw.inputs.inputspec.regressor   = regressor
    cw.inputs.inputspec.cols        = c.cwasRegressorCols
    cw.inputs.inputspec.f_samples   = c.cwasFSamples
    cw.inputs.inputspec.strata      = c.cwasRegressorStrata # will stay None?
    cw.inputs.inputspec.parallel_nodes = c.cwasParallelNodes
    
    ds = pe.Node(nio.DataSink(), name='cwas_sink')
    out_dir = os.path.dirname(s_paths[0]).replace(s_ids[0], 'cwas_results')
    ds.inputs.base_directory = out_dir
    ds.inputs.container = ''

    wf.connect(cw, 'outputspec.F_map',
               ds, 'F_map')
    wf.connect(cw, 'outputspec.p_map',
               ds, 'p_map')


    wf.run(plugin='MultiProc',
                         plugin_args={'n_procs': c.numCoresPerSubject})
Exemplo n.º 2
0
def prep_cwas_workflow(c, subject_infos):
    print 'Preparing CWAS workflow'
    p_id, s_ids, scan_ids, s_paths = (list(tup) for tup in zip(*subject_infos))
    print 'Subjects', s_ids

    wf = pe.Workflow(name='cwas_workflow')
    wf.base_dir = c.workingDirectory

    from CPAC.cwas import create_cwas
    import numpy as np
    regressor = np.loadtxt(c.cwasRegressorFile)

    cw = create_cwas()
    cw.inputs.inputspec.roi = c.cwasROIFile
    cw.inputs.inputspec.subjects = s_paths
    cw.inputs.inputspec.regressor = regressor
    cw.inputs.inputspec.cols = c.cwasRegressorCols
    cw.inputs.inputspec.f_samples = c.cwasFSamples
    cw.inputs.inputspec.strata = c.cwasRegressorStrata  # will stay None?
    cw.inputs.inputspec.parallel_nodes = c.cwasParallelNodes

    ds = pe.Node(nio.DataSink(), name='cwas_sink')
    out_dir = os.path.dirname(s_paths[0]).replace(s_ids[0], 'cwas_results')
    ds.inputs.base_directory = out_dir
    ds.inputs.container = ''

    wf.connect(cw, 'outputspec.F_map', ds, 'F_map')
    wf.connect(cw, 'outputspec.p_map', ds, 'p_map')

    wf.run(plugin='MultiProc', plugin_args={'n_procs': c.numCoresPerSubject})
Exemplo n.º 3
0
def prep_cwas_workflow(c, subject_infos):
    from CPAC.cwas import create_cwas
    import numpy as np
    
    try:
        import mkl
        mkl.set_num_threads(c.cwasThreads)
    except ImportError:
        pass
    
    print 'Preparing CWAS workflow'
    p_id, s_ids, scan_ids, s_paths = (list(tup) for tup in zip(*subject_infos))
    print 'Subjects', s_ids
    
    # Read in list of subject functionals
    lines   = open(c.cwasFuncFiles).readlines()
    spaths  = [ l.strip().strip('"') for l in lines ]
    
    # Read in design/regressor file
    regressor = np.loadtxt(c.cwasRegressorFile)

    wf = pe.Workflow(name='cwas_workflow')
    wf.base_dir = c.workingDirectory
    
    cw = create_cwas()
    cw.inputs.inputspec.roi         = c.cwasROIFile
    cw.inputs.inputspec.subjects    = spaths
    cw.inputs.inputspec.regressor   = regressor
    cw.inputs.inputspec.cols        = c.cwasRegressorCols
    cw.inputs.inputspec.f_samples   = c.cwasFSamples
    cw.inputs.inputspec.strata      = c.cwasRegressorStrata # will stay None?
    cw.inputs.inputspec.parallel_nodes = c.cwasParallelNodes
    cw.inputs.inputspec.memory_limit = c.cwasMemory
    cw.inputs.inputspec.dtype       = c.cwasDtype
    
    ds = pe.Node(nio.DataSink(), name='cwas_sink')
    out_dir = os.path.dirname(s_paths[0]).replace(s_ids[0], 'cwas_results')
    ds.inputs.base_directory = out_dir
    ds.inputs.container = ''

    wf.connect(cw, 'outputspec.F_map',
               ds, 'F_map')
    wf.connect(cw, 'outputspec.p_map',
               ds, 'p_map')

    wf.run(plugin='MultiProc',
                         plugin_args={'n_procs': c.numCoresPerSubject})
Exemplo n.º 4
0
def test_cwas():
    from CPAC.cwas import create_cwas
    import numpy as np
    import os, glob
    import time
    
    g_string = '/home/data/Projects/ADHD200/Manuel_sink/*/func/scan_id_rest_1/scan_id_anat_1/csf_threshold_0.4/gm_threshold_0.2/wm_threshold_0.66/nc_5/selector_0.2.3.7/bandpass_freqs_0.009.0.1/rest_residual_filtered_4mm.nii.gz'
    roi_file = '/usr/share/fsl/4.1/data/standard/MNI152_T1_4mm_brain_mask.nii.gz'
    subjects_list = glob.glob(g_string)
    
    c = create_cwas()
    c.inputs.inputspec.roi = roi_file
    c.inputs.inputspec.subjects = subjects_list[:5]
    c.inputs.inputspec.regressor = np.arange(len(subjects_list[:5]))[:,np.newaxis]
    c.inputs.inputspec.f_samples = 5000
    c.inputs.inputspec.parallel_nodes = 10
    c.base_dir = '/home/data/Projects/cwas_tests/results_fs%i_pn%i' % (c.inputs.inputspec.f_samples,
                                                                       c.inputs.inputspec.parallel_nodes)
    start = time.clock()
    c.run(plugin='MultiProc', plugin_args={'n_procs' : 10})
    end = time.clock()
    print "%.2gs" % (end-start)
Exemplo n.º 5
0
    def run_cwas(self):
        import os

        os.chdir("%s/C-PAC" % self.base)

        from CPAC.cwas import create_cwas
        import numpy as np
        import time
        from os import path as op

        ###
        # Paths and Other Inputs
        # - ideally this section can be changed with the rest staying the same
        #   however, we aren't there yet
        ###

        # File with initial grey matter mask
        roi_file = op.join(self.base, "rois/grey_matter_4mm.nii.gz")

        # File with list of subject functionals
        sfile = op.join(self.base, "configs", "%s_funcpaths_4mm.txt" % self.name)

        # File with regressors
        rfile = op.join(self.base, "configs", "%s_regressors.txt" % self.name)

        # Column(s) of interest in regressors
        cols = [1]

        # Number of permutations
        nperms = 1000

        ###
        # Setup
        ###

        # Read in list of subject functionals
        subjects_list = [l.strip().strip('"') for l in open(sfile).readlines()]

        # Read in design/regressor file
        regressors = np.loadtxt(rfile)

        # Setup the inputs
        c = create_cwas()
        c.inputs.inputspec.roi = roi_file
        c.inputs.inputspec.subjects = subjects_list
        c.inputs.inputspec.cols = cols
        c.inputs.inputspec.regressor = regressors
        c.inputs.inputspec.f_samples = nperms
        c.inputs.inputspec.parallel_nodes = 4
        # c.base_dir = op.join(obase, 'results_fs%i_pn%i' % \
        #                (c.inputs.inputspec.f_samples, c.inputs.inputspec.parallel_nodes))
        c.base_dir = op.join(self.base, "results_%s.py" % self.name)

        # export MKL_NUM_THREADS=X # in command line
        # import mkl
        # mkl.set_num_threads(X)

        # try:
        #     import mkl
        #     mkl.set_num_threads(nthreads)
        # except ImportError:
        #     pass

        # Run it!
        start = time.clock()
        c.run(plugin="MultiProc", plugin_args={"n_procs": 4})
        end = time.clock()
        print "time: %.2gs" % (end - start)
Exemplo n.º 6
0
def cwas_workflow(c):
    from CPAC.cwas import create_cwas
    from string import Template
    import numpy as np
    import time
        
    if isinstance(c.cwas, dict):
        c.cwas = Configuration(c.cwas)
    
    try:
        import mkl
        mkl.set_num_threads(c.cwas.threads)
    except ImportError:
        pass
        
    # Auto-complete base
    fields = ["prior_mask_file", "file_with_functional_paths", "regressor_file"]
    for field in fields:
        s = Template(getattr(c.cwas, field))
        c.cwas.update(field, s.substitute(base=c.cwas.base))
    
    # Read in list of subject functionals
    lines   = open(c.cwas.file_with_functional_paths).readlines()
    spaths  = [ l.strip().strip('"') for l in lines ]
    
    # Read in design/regressor file
    regressor = np.loadtxt(c.cwas.regressor_file)
    
    # Load workflow
    wf = pe.Workflow(name='cwas_workflow')
    wf.base_dir = c.workingDirectory
    
    # Setup CWAS set of commands
    cw = create_cwas()
    cw.inputs.inputspec.roi         = c.cwas.prior_mask_file
    cw.inputs.inputspec.subjects    = spaths
    cw.inputs.inputspec.regressor   = regressor
    cw.inputs.inputspec.cols        = c.cwas.regressors_of_interest
    cw.inputs.inputspec.f_samples   = c.cwas.n_permutations
    cw.inputs.inputspec.strata      = c.cwas.strata
    cw.inputs.inputspec.parallel_nodes = c.cwas.parallel_nodes
    cw.inputs.inputspec.memory_limit = c.cwas.memory_limit
    cw.inputs.inputspec.dtype       = c.cwas.dtype
    
    # Output directory
    ds = pe.Node(nio.DataSink(), name='cwas_sink')
    ds.inputs.base_directory = os.path.join(c.outputDirectory, "cwas_results")
    ds.inputs.container = ''
    
    # Link F-stats and P-values
    wf.connect(cw, 'outputspec.F_map',
               ds, 'F_map')
    wf.connect(cw, 'outputspec.p_map',
               ds, 'p_map')
    
    # Run CWAS
    start   = time.time()
    wf.run(plugin='MultiProc',
                         plugin_args={'n_procs': c.numCoresPerSubject})
    end     = time.time()
    
    # Return time it took
    print 'It took', end-start, 'seconds.'
    return (end-start)