|
14 | 14 |
|
15 | 15 | from nipype import config
|
16 | 16 | config.enable_provenance()
|
17 |
| -from nipype.external import six |
18 | 17 |
|
| 18 | +from nipype.external import six |
19 | 19 |
|
20 | 20 | from glob import glob
|
21 | 21 | import os
|
@@ -174,8 +174,8 @@ def create_reg_workflow(name='registration'):
|
174 | 174 | reg.inputs.winsorize_upper_quantile = 0.995
|
175 | 175 | reg.inputs.args = '--float'
|
176 | 176 | reg.inputs.output_warped_image = 'output_warped_image.nii.gz'
|
177 |
| - reg.inputs.num_threads = 4 |
178 |
| - reg.plugin_args = {'qsub_args': '-l nodes=1:ppn=4'} |
| 177 | + reg.inputs.num_threads = 2 |
| 178 | + reg.plugin_args = {'qsub_args': '-pe orte 2'} |
179 | 179 | register.connect(stripper, 'out_file', reg, 'moving_image')
|
180 | 180 | register.connect(inputnode,'target_image_brain', reg,'fixed_image')
|
181 | 181 |
|
@@ -361,25 +361,45 @@ def analyze_openfmri_dataset(data_dir, subject=None, model_id=None,
|
361 | 361 | """
|
362 | 362 | Return data components as anat, bold and behav
|
363 | 363 | """
|
364 |
| - |
365 |
| - datasource = pe.Node(nio.DataGrabber(infields=['subject_id', 'run_id', |
| 364 | + contrast_file = os.path.join(data_dir, 'models', 'model%03d' % model_id, |
| 365 | + 'task_contrasts.txt') |
| 366 | + has_contrast = os.path.exists(contrast_file) |
| 367 | + if has_contrast: |
| 368 | + datasource = pe.Node(nio.DataGrabber(infields=['subject_id', 'run_id', |
366 | 369 | 'task_id', 'model_id'],
|
367 | 370 | outfields=['anat', 'bold', 'behav',
|
368 | 371 | 'contrasts']),
|
369 | 372 | name='datasource')
|
| 373 | + else: |
| 374 | + datasource = pe.Node(nio.DataGrabber(infields=['subject_id', 'run_id', |
| 375 | + 'task_id', 'model_id'], |
| 376 | + outfields=['anat', 'bold', 'behav']), |
| 377 | + name='datasource') |
370 | 378 | datasource.inputs.base_directory = data_dir
|
371 | 379 | datasource.inputs.template = '*'
|
372 |
| - datasource.inputs.field_template = {'anat': '%s/anatomy/highres001.nii.gz', |
373 |
| - 'bold': '%s/BOLD/task%03d_r*/bold.nii.gz', |
374 |
| - 'behav': ('%s/model/model%03d/onsets/task%03d_' |
375 |
| - 'run%03d/cond*.txt'), |
376 |
| - 'contrasts': ('models/model%03d/' |
377 |
| - 'task_contrasts.txt')} |
378 |
| - datasource.inputs.template_args = {'anat': [['subject_id']], |
| 380 | + |
| 381 | + if has_contrast: |
| 382 | + datasource.inputs.field_template = {'anat': '%s/anatomy/highres001.nii.gz', |
| 383 | + 'bold': '%s/BOLD/task%03d_r*/bold.nii.gz', |
| 384 | + 'behav': ('%s/model/model%03d/onsets/task%03d_' |
| 385 | + 'run%03d/cond*.txt'), |
| 386 | + 'contrasts': ('models/model%03d/' |
| 387 | + 'task_contrasts.txt')} |
| 388 | + datasource.inputs.template_args = {'anat': [['subject_id']], |
379 | 389 | 'bold': [['subject_id', 'task_id']],
|
380 | 390 | 'behav': [['subject_id', 'model_id',
|
381 | 391 | 'task_id', 'run_id']],
|
382 | 392 | 'contrasts': [['model_id']]}
|
| 393 | + else: |
| 394 | + datasource.inputs.field_template = {'anat': '%s/anatomy/highres001.nii.gz', |
| 395 | + 'bold': '%s/BOLD/task%03d_r*/bold.nii.gz', |
| 396 | + 'behav': ('%s/model/model%03d/onsets/task%03d_' |
| 397 | + 'run%03d/cond*.txt')} |
| 398 | + datasource.inputs.template_args = {'anat': [['subject_id']], |
| 399 | + 'bold': [['subject_id', 'task_id']], |
| 400 | + 'behav': [['subject_id', 'model_id', |
| 401 | + 'task_id', 'run_id']]} |
| 402 | + |
383 | 403 | datasource.inputs.sort_filelist = True
|
384 | 404 |
|
385 | 405 | """
|
@@ -412,9 +432,11 @@ def get_highpass(TR, hpcutoff):
|
412 | 432 |
|
413 | 433 | def get_contrasts(contrast_file, task_id, conds):
|
414 | 434 | import numpy as np
|
415 |
| - contrast_def = np.genfromtxt(contrast_file, dtype=object) |
416 |
| - if len(contrast_def.shape) == 1: |
417 |
| - contrast_def = contrast_def[None, :] |
| 435 | + import os |
| 436 | + contrast_def = [] |
| 437 | + if os.path.exists(contrast_file): |
| 438 | + with open(contrast_file, 'rt') as fp: |
| 439 | + contrast_def.extend([np.array(row.split()) for row in fp.readlines() if row.strip()]) |
418 | 440 | contrasts = []
|
419 | 441 | for row in contrast_def:
|
420 | 442 | if row[0] != 'task%03d' % task_id:
|
@@ -448,22 +470,32 @@ def get_contrasts(contrast_file, task_id, conds):
|
448 | 470 | name="modelspec")
|
449 | 471 | modelspec.inputs.input_units = 'secs'
|
450 | 472 |
|
451 |
| - def check_behav_list(behav): |
452 |
| - out_behav = [] |
| 473 | + def check_behav_list(behav, conds): |
| 474 | + from nipype.external import six |
| 475 | + import numpy as np |
| 476 | + num_conds = len(conds) |
453 | 477 | if isinstance(behav, six.string_types):
|
454 | 478 | behav = [behav]
|
455 |
| - for val in behav: |
456 |
| - if not isinstance(val, list): |
457 |
| - out_behav.append([val]) |
458 |
| - else: |
459 |
| - out_behav.append(val) |
460 |
| - return out_behav |
| 479 | + behav_array = np.array(behav).flatten() |
| 480 | + num_elements = behav_array.shape[0] |
| 481 | + return behav_array.reshape(num_elements/num_conds, num_conds).tolist() |
| 482 | + |
| 483 | + reshape_behav = pe.Node(niu.Function(input_names=['behav', 'conds'], |
| 484 | + output_names=['behav'], |
| 485 | + function=check_behav_list), |
| 486 | + name='reshape_behav') |
461 | 487 |
|
462 | 488 | wf.connect(subjinfo, 'TR', modelspec, 'time_repetition')
|
463 |
| - wf.connect(datasource, ('behav', check_behav_list), modelspec, 'event_files') |
| 489 | + wf.connect(datasource, 'behav', reshape_behav, 'behav') |
| 490 | + wf.connect(subjinfo, 'conds', reshape_behav, 'conds') |
| 491 | + wf.connect(reshape_behav, 'behav', modelspec, 'event_files') |
| 492 | + |
464 | 493 | wf.connect(subjinfo, 'TR', modelfit, 'inputspec.interscan_interval')
|
465 | 494 | wf.connect(subjinfo, 'conds', contrastgen, 'conds')
|
466 |
| - wf.connect(datasource, 'contrasts', contrastgen, 'contrast_file') |
| 495 | + if has_contrast: |
| 496 | + wf.connect(datasource, 'contrasts', contrastgen, 'contrast_file') |
| 497 | + else: |
| 498 | + contrastgen.inputs.contrast_file = '' |
467 | 499 | wf.connect(infosource, 'task_id', contrastgen, 'task_id')
|
468 | 500 | wf.connect(contrastgen, 'contrasts', modelfit, 'inputspec.contrasts')
|
469 | 501 |
|
|
0 commit comments