From 2fa3a0dbb4fd9184dbe03d7fde965bbb29dfca98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Cl=C3=A9net?= <117362283+bclenet@users.noreply.github.com> Date: Tue, 26 Sep 2023 08:53:13 +0200 Subject: [PATCH 1/2] Q6O0 reproduction (#68) * [Q6O0] refactoring pipeline * [Q6O0] updating list of implemented pipelines * [TEST] change implemented pipeline test * [CI] code quality change * [CI] code quality change * [CI] record pylint logs in case of failure only * [CI] install whole package before linting * [CI] adding higher timeouts to self-hosted jobs * [BUG] adding inputs to subject info node * [BUG] changes in pipeline workflow * [BUG] unnecessary commented line in pipeline * [REFAC] remove distance as unused in model design * [TEST] init a test for conftest.py * [BUG] inside unit_tests workflow * [TEST] testing the conftest module * Issue with parameters dir creation * Bug with makedirs import * [TEST] sorting results before computing correlations --- .github/workflows/code_quality.yml | 13 +- .github/workflows/pipeline_tests.yml | 2 +- .github/workflows/test_changes.yml | 2 +- narps_open/pipelines/__init__.py | 2 +- narps_open/pipelines/team_Q6O0.py | 1351 ++++++++++++++------------ tests/conftest.py | 2 +- tests/pipelines/test_pipelines.py | 8 +- tests/pipelines/test_team_Q6O0.py | 68 ++ 8 files changed, 798 insertions(+), 650 deletions(-) create mode 100644 tests/pipelines/test_team_Q6O0.py diff --git a/.github/workflows/code_quality.yml b/.github/workflows/code_quality.yml index 2804e813..a9248671 100644 --- a/.github/workflows/code_quality.yml +++ b/.github/workflows/code_quality.yml @@ -8,9 +8,11 @@ on: push: paths: - '**.py' + - '.github/workflows/code_quality.yml' pull_request: paths: - '**.py' + - '.github/workflows/code_quality.yml' # Jobs that define the workflow jobs: @@ -33,22 +35,23 @@ jobs: - uses: actions/cache@v3 with: path: ~/.cache/pip - key: ${{ runner.os }}-pip-pylint + key: ${{ runner.os }}-pip-${{ hashFiles('setup.py') }} restore-keys: | - ${{ runner.os }}-pip-pylint + ${{ runner.os }}-pip- - name: Install dependencies run: | python -m pip install --upgrade pip - pip install pylint + pip install .[tests] - name: Analyse the code with pylint run: | - pylint --exit-zero narps_open > pylint_report_narps_open.txt - pylint --exit-zero tests > pylint_report_tests.txt + pylint --fail-under 8 --ignore-paths narps_open/pipelines/ narps_open > pylint_report_narps_open.txt + pylint --fail-under 8 tests > pylint_report_tests.txt - name: Archive pylint results uses: actions/upload-artifact@v3 + if: failure() # Only if previous step failed with: name: pylint-reports-python path: | diff --git a/.github/workflows/pipeline_tests.yml b/.github/workflows/pipeline_tests.yml index 66e91a91..132a7662 100644 --- a/.github/workflows/pipeline_tests.yml +++ b/.github/workflows/pipeline_tests.yml @@ -47,7 +47,7 @@ jobs: echo "tests=$test_files" >> $GITHUB_OUTPUT echo "teams=$teams" >> $GITHUB_OUTPUT - # A job to identify and run the tests + # A job to run the tests pytest: needs: identify-tests runs-on: self-hosted diff --git a/.github/workflows/test_changes.yml b/.github/workflows/test_changes.yml index 8c582197..e0608011 100644 --- a/.github/workflows/test_changes.yml +++ b/.github/workflows/test_changes.yml @@ -38,7 +38,7 @@ jobs: echo $test_files echo "tests=$test_files" >> $GITHUB_OUTPUT - # A job to list the tests to be run + # A job to run the tests pytest: needs: identify-tests runs-on: self-hosted diff --git a/narps_open/pipelines/__init__.py b/narps_open/pipelines/__init__.py index c3834fb6..6c5239ca 100644 --- a/narps_open/pipelines/__init__.py +++ b/narps_open/pipelines/__init__.py @@ -62,7 +62,7 @@ 'O6R6': None, 'P5F3': None, 'Q58J': None, - 'Q6O0': None, + 'Q6O0': 'PipelineTeamQ6O0', 'R42Q': None, 'R5K7': None, 'R7D1': None, diff --git a/narps_open/pipelines/team_Q6O0.py b/narps_open/pipelines/team_Q6O0.py index 5eb0462a..69cacc3c 100755 --- a/narps_open/pipelines/team_Q6O0.py +++ b/narps_open/pipelines/team_Q6O0.py @@ -1,643 +1,720 @@ -from nipype.interfaces.spm import (Smooth, OneSampleTTestDesign, EstimateModel, EstimateContrast, Level1Design, - TwoSampleTTestDesign) -from nipype.interfaces.spm import Threshold as Analysis_Threshold # to distinguish with FSL Threshold interface -from nipype.algorithms.modelgen import SpecifySPMModel +#!/usr/bin/python +# coding: utf-8 + +""" Write the work of NARPS' team Q6O0 using Nipype """ + +from os.path import join +from itertools import product + +from nipype import Workflow, Node, MapNode from nipype.interfaces.utility import IdentityInterface, Function from nipype.interfaces.io import SelectFiles, DataSink +from nipype.interfaces.spm import ( + Smooth, + Level1Design, OneSampleTTestDesign, TwoSampleTTestDesign, + EstimateModel, EstimateContrast, Threshold + ) +from nipype.algorithms.modelgen import SpecifySPMModel from nipype.algorithms.misc import Gunzip -from nipype import Workflow, Node, MapNode -from nipype.interfaces.base import Bunch - -from os.path import join as opj -import os -import json - -def get_subject_infos_gain(event_files): - ''' - Create Bunchs for specifySPMModel. - Here, the team wanted to concatenate runs and used RT (response time) for duration except for NoResponse trials - for which the duration was set to 4. - Gain and loss amounts were used as parametric regressors. - - Parameters : - - event_files : list of files containing events information for each run - - Returns : - - subject_info : list of Bunch for 1st level analysis. - ''' - from os.path import join as opj - from nipype.interfaces.base import Bunch - - cond_names = ['trial'] - onset = {} - duration = {} - weights_gain = {} - weights_loss = {} - weights_distance = {} - runs = ['01', '02', '03', '04'] - - for r in range(len(runs)): # Loop over number of runs. - onset.update({s + '_run' + str(r+1) : [] for s in cond_names}) # creates dictionary items with empty lists - duration.update({s + '_run' + str(r+1) : [] for s in cond_names}) - weights_gain.update({'gain_run' + str(r+1) : []}) - weights_loss.update({'loss_run' + str(r+1) : []}) - weights_distance.update({'distance_run' + str(r+1) : []}) - - # subject_id = '001' - # file = sub-001_func_sub-001_task-MGT_run-01_events.tsv - for r, f_events in enumerate(event_files): - with open(f_events, 'rt') as f: - next(f) # skip the header - - for line in f: - info = line.strip().split() - - for cond in cond_names: - val = cond + '_run' + str(r+1) # trial_run1 - val_gain = 'gain_run' + str(r+1) # gain_run1 - val_loss = 'loss_run' + str(r+1) # loss_run1 - val_distance = 'distance_run' + str(r+1) - onset[val].append(float(info[0])) # onsets for trial_run1 - duration[val].append(float(4)) # durations for trial : 4 - weights_gain[val_gain].append(float(info[2])) # weights gain for trial_run1 - weights_loss[val_loss].append(float(info[3])) # weights loss for trial_run1 - weights_distance[val_distance].append(abs(0.5*(float(info[2])) - float(info[3]))) - - # Bunching is done per run, i.e. trial_run1, trial_run2, etc. - # But names must not have '_run1' etc because we concatenate runs - subject_info = [] - for r in range(len(runs)): - - cond = [c + '_run' + str(r+1) for c in cond_names] - gain = 'gain_run' + str(r+1) - loss = 'loss_run' + str(r+1) - distance = 'distance_run' + str(r+1) - - subject_info.insert(r, - Bunch(conditions=cond_names, - onsets=[onset[k] for k in cond], - durations=[duration[k] for k in cond], - amplitudes=None, - tmod=None, - pmod=[Bunch(name=['loss', 'gain'], - poly=[1, 1], - param=[weights_loss[loss], - weights_gain[gain]])], - regressor_names=None, - regressors=None)) - - return subject_info - -def get_subject_infos_loss(event_files): - ''' - Create Bunchs for specifySPMModel. - Here, the team wanted to concatenate runs and used RT (response time) for duration except for NoResponse trials - for which the duration was set to 4. - Gain and loss amounts were used as parametric regressors. - - Parameters : - - event_files : list of files containing events information for each run - - Returns : - - subject_info : list of Bunch for 1st level analysis. - ''' - from os.path import join as opj - from nipype.interfaces.base import Bunch - - cond_names = ['trial'] - onset = {} - duration = {} - weights_gain = {} - weights_loss = {} - weights_distance = {} - runs = ['01', '02', '03', '04'] - - for r in range(len(runs)): # Loop over number of runs. - onset.update({s + '_run' + str(r+1) : [] for s in cond_names}) # creates dictionary items with empty lists - duration.update({s + '_run' + str(r+1) : [] for s in cond_names}) - weights_gain.update({'gain_run' + str(r+1) : []}) - weights_loss.update({'loss_run' + str(r+1) : []}) - weights_distance.update({'distance_run' + str(r+1) : []}) - - # subject_id = '001' - # file = sub-001_func_sub-001_task-MGT_run-01_events.tsv - for r, f_events in enumerate(event_files): - with open(f_events, 'rt') as f: - next(f) # skip the header - - for line in f: - info = line.strip().split() - - for cond in cond_names: - val = cond + '_run' + str(r+1) # trial_run1 - val_gain = 'gain_run' + str(r+1) # gain_run1 - val_loss = 'loss_run' + str(r+1) # loss_run1 - val_distance = 'distance_run' + str(r+1) - onset[val].append(float(info[0])) # onsets for trial_run1 - duration[val].append(float(4)) # durations for trial : 4 - weights_gain[val_gain].append(float(info[2])) # weights gain for trial_run1 - weights_loss[val_loss].append(float(info[3])) # weights loss for trial_run1 - weights_distance[val_distance].append(abs(0.5*(float(info[2])) - float(info[3]))) - - # Bunching is done per run, i.e. trial_run1, trial_run2, etc. - # But names must not have '_run1' etc because we concatenate runs - subject_info = [] - for r in range(len(runs)): - - cond = [c + '_run' + str(r+1) for c in cond_names] - gain = 'gain_run' + str(r+1) - loss = 'loss_run' + str(r+1) - distance = 'distance_run' + str(r+1) - - subject_info.insert(r, - Bunch(conditions=cond_names, - onsets=[onset[k] for k in cond], - durations=[duration[k] for k in cond], - amplitudes=None, - tmod=None, - pmod=[Bunch(name=['gain', 'loss'], - poly=[1, 1], - param=[weights_gain[gain], - weights_loss[loss]])], - regressor_names=None, - regressors=None)) - - return subject_info - -def get_contrasts_gain(subject_id): - ''' - Create the list of tuples that represents contrasts. - Each contrast is in the form : - (Name,Stat,[list of condition names],[weights on those conditions]) - - Parameters: - - subject_id: str, ID of the subject - - Returns: - - contrasts: list of tuples, list of contrasts to analyze - ''' - # list of condition names - conditions = ['trialxgain^1'] - - # create contrasts - pos_effect_gain = ('pos_effect_of_gain', 'T', conditions, [1]) - - neg_effect_gain = ('neg_effect_of_gain', 'T', conditions, [-1]) - - # contrast list - contrasts = [pos_effect_gain] - - return contrasts - -def get_contrasts_loss(subject_id): - ''' - Create the list of tuples that represents contrasts. - Each contrast is in the form : - (Name,Stat,[list of condition names],[weights on those conditions]) - - Parameters: - - subject_id: str, ID of the subject - - Returns: - - contrasts: list of tuples, list of contrasts to analyze - ''' - # list of condition names - conditions = ['trialxloss^1'] - - # create contrasts - pos_effect_loss = ('pos_effect_of_loss', 'T', conditions, [1]) - - neg_effect_loss = ('neg_effect_of_loss', 'T', conditions, [-1]) - - # contrast list - contrasts = [pos_effect_loss] - - return contrasts - -def get_parameters_file(filepaths, subject_id, result_dir, working_dir): - ''' - Create new tsv files with only desired parameters per subject per run. - The six motion parameters, the 5 aCompCor parameters, the global white matter and - cerebral spinal fluid signals were included as nuisance regressors/ - - Parameters : - - filepaths : paths to subject parameters file (i.e. one per run) - - subject_id : subject for whom the 1st level analysis is made - - result_dir: str, directory where results will be stored - - working_dir: str, name of the sub-directory for intermediate results - - Return : - - parameters_file : paths to new files containing only desired parameters. - ''' - import pandas as pd - import numpy as np - from os.path import join as opj - import os - - if not isinstance(filepaths, list): - filepaths = [filepaths] - parameters_file = [] - - for i, file in enumerate(filepaths): - df = pd.read_csv(file, sep = '\t', header=0) - temp_list = np.array([df['X'], df['Y'], df['Z'], - df['RotX'], df['RotY'], df['RotZ'], df['aCompCor00'], - df['aCompCor01'], df['aCompCor02'], df['aCompCor03'], - df['aCompCor04'], df['aCompCor05'], df['WhiteMatter'], - df['CSF']]) # Parameters we want to use for the model - retained_parameters = pd.DataFrame(np.transpose(temp_list)) - new_path =opj(result_dir, working_dir, 'parameters_file', - f"parameters_file_sub-{subject_id}_run0{str(i+1)}.tsv") - if not os.path.isdir(opj(result_dir, working_dir, 'parameters_file')): - os.mkdir(opj(result_dir, working_dir, 'parameters_file')) - writer = open(new_path, "w") - writer.write(retained_parameters.to_csv(sep = '\t', index = False, header = False, na_rep = '0.0')) - writer.close() - - parameters_file.append(new_path) - - return parameters_file - -def rm_gunzip_files(files, subject_id, result_dir, working_dir): - import shutil - from os.path import join as opj - - gunzip_dir = opj(result_dir, working_dir, 'l1_analysis', f"_subject_id_{subject_id}", 'gunzip_func') - - try: - shutil.rmtree(gunzip_dir) - except OSError as e: - print(e) - else: - print("The directory is deleted successfully") - - return files - -def rm_smoothed_files(files, subject_id, result_dir, working_dir): - import shutil - from os.path import join as opj - - smooth_dir = opj(result_dir, working_dir, 'l1_analysis', f"_subject_id_{subject_id}", 'smooth') - - try: - shutil.rmtree(smooth_dir) - except OSError as e: - print(e) - else: - print("The directory is deleted successfully") - - return files - - -def get_l1_analysis(subject_list, TR, fwhm, run_list, exp_dir, result_dir, working_dir, output_dir): - """ - Returns the first level analysis workflow. - - Parameters: - - exp_dir: str, directory where raw data are stored - - result_dir: str, directory where results will be stored - - working_dir: str, name of the sub-directory for intermediate results - - output_dir: str, name of the sub-directory for final results - - subject_list: list of str, list of subject for which you want to do the analysis - - run_list: list of str, list of runs for which you want to do the analysis - - fwhm: float, fwhm for smoothing step - - TR: float, time repetition used during acquisition - - Returns: - - l1_analysis : Nipype WorkFlow - """ - # Infosource Node - To iterate on subjects - infosource = Node(IdentityInterface(fields = ['subject_id']), name = 'infosource') - infosource.iterables = [('subject_id', subject_list)] - - # Templates to select files node - func_file = opj('derivatives', 'fmriprep', 'sub-{subject_id}', 'func', - 'sub-{subject_id}_task-MGT_run-*_bold_space-MNI152NLin2009cAsym_preproc.nii.gz') - - event_file = opj('sub-{subject_id}', 'func', - 'sub-{subject_id}_task-MGT_run-*_events.tsv') - - param_file = opj('derivatives', 'fmriprep', 'sub-{subject_id}', 'func', - 'sub-{subject_id}_task-MGT_run-*_bold_confounds.tsv') - - template = {'param' : param_file, 'event' : event_file, 'func' : func_file} - - # SelectFiles node - to select necessary files - selectfiles = Node(SelectFiles(template, base_directory=exp_dir), name = 'selectfiles') - - # DataSink Node - store the wanted results in the wanted repository - datasink = Node(DataSink(base_directory=result_dir, container=output_dir), name='datasink') - - # GUNZIP NODE : SPM do not use .nii.gz files - gunzip_func = MapNode(Gunzip(), name = 'gunzip_func', iterfield = ['in_file']) - - ## Smoothing node - smooth = Node(Smooth(fwhm = fwhm), name = 'smooth') - - # Get Subject Info - get subject specific condition information - subject_infos_gain = Node(Function(input_names=['event_files'], - output_names=['subject_info'], - function=get_subject_infos_gain), - name='subject_infos_gain') - - subject_infos_loss = Node(Function(input_names=['event_files'], - output_names=['subject_info'], - function=get_subject_infos_loss), - name='subject_infos_loss') - - # Node parameters to get parameters files - parameters = Node(Function(function=get_parameters_file, - input_names=['filepaths', 'subject_id', 'result_dir', 'working_dir'], - output_names=['parameters_file']), - name='parameters') - - parameters.inputs.result_dir = result_dir - parameters.inputs.working_dir = working_dir - - # SpecifyModel - Generates SPM-specific Model - specify_model_gain = Node(SpecifySPMModel(concatenate_runs = True, input_units = 'secs', output_units = 'secs', - time_repetition = TR, high_pass_filter_cutoff = 128), - name='specify_model_gain') - - specify_model_loss = Node(SpecifySPMModel(concatenate_runs = True, input_units = 'secs', output_units = 'secs', - time_repetition = TR, high_pass_filter_cutoff = 128), - name='specify_model_loss') - - # Level1Design - Generates an SPM design matrix - l1_design_gain = Node(Level1Design(bases = {'hrf': {'derivs': [0, 0]}}, timing_units = 'secs', - interscan_interval = TR, model_serial_correlations = 'AR(1)'), name='l1_design_gain') - - l1_design_loss = Node(Level1Design(bases = {'hrf': {'derivs': [0, 0]}}, timing_units = 'secs', - interscan_interval = TR, model_serial_correlations = 'AR(1)'), name='l1_design_loss') - - # EstimateModel - estimate the parameters of the model - l1_estimate_gain = Node(EstimateModel(estimation_method={'Classical': 1}), - name="l1_estimate_gain") - - l1_estimate_loss = Node(EstimateModel(estimation_method={'Classical': 1}), - name="l1_estimate_loss") - - # Node contrasts to get contrasts - contrasts_gain = Node(Function(function=get_contrasts_gain, - input_names=['subject_id'], - output_names=['contrasts']), - name='contrasts_gain') - - contrasts_loss = Node(Function(function=get_contrasts_loss, - input_names=['subject_id'], - output_names=['contrasts']), - name='contrasts_loss') - - # EstimateContrast - estimates contrasts - contrast_estimate_gain = Node(EstimateContrast(), name="contrast_estimate_gain") - - contrast_estimate_loss = Node(EstimateContrast(), name="contrast_estimate_loss") - - remove_gunzip_files = Node(Function(input_names = ['files', 'subject_id', 'result_dir', 'working_dir'], - output_names = ['files'], - function = rm_gunzip_files), name = 'remove_gunzip_files') - - remove_gunzip_files.inputs.result_dir = result_dir - remove_gunzip_files.inputs.working_dir = working_dir - - remove_smoothed_files = Node(Function(input_names = ['files', 'subject_id', 'result_dir', 'working_dir'], - output_names = ['files'], - function = rm_smoothed_files), name = 'remove_smoothed_files') - - remove_smoothed_files.inputs.result_dir = result_dir - remove_smoothed_files.inputs.working_dir = working_dir - - # Create l1 analysis workflow and connect its nodes - l1_analysis = Workflow(base_dir = opj(result_dir, working_dir), name = "l1_analysis") - - l1_analysis.connect([(infosource, selectfiles, [('subject_id', 'subject_id')]), - (selectfiles, subject_infos_gain, [('event','event_files')]), - (selectfiles, subject_infos_loss, [('event','event_files')]), - (selectfiles, parameters, [('param', 'filepaths')]), - (infosource, parameters, [('subject_id', 'subject_id')]), - (infosource, contrasts_gain, [('subject_id', 'subject_id')]), - (infosource, contrasts_loss, [('subject_id', 'subject_id')]), - (infosource, remove_gunzip_files, [('subject_id', 'subject_id')]), - (infosource, remove_smoothed_files, [('subject_id', 'subject_id')]), - (subject_infos_gain, specify_model_gain, [('subject_info', 'subject_info')]), - (subject_infos_loss, specify_model_loss, [('subject_info', 'subject_info')]), - (contrasts_gain, contrast_estimate_gain, [('contrasts', 'contrasts')]), - (contrasts_loss, contrast_estimate_loss, [('contrasts', 'contrasts')]), - (selectfiles, gunzip_func, [('func', 'in_file')]), - (gunzip_func, smooth, [('out_file', 'in_files')]), - (smooth, remove_gunzip_files, [('smoothed_files', 'files')]), - (remove_gunzip_files, specify_model_gain, [('files', 'functional_runs')]), - (remove_gunzip_files, specify_model_loss, [('files', 'functional_runs')]), - (parameters, specify_model_gain, [('parameters_file', 'realignment_parameters')]), - (parameters, specify_model_loss, [('parameters_file', 'realignment_parameters')]), - (specify_model_gain, l1_design_gain, [('session_info', 'session_info')]), - (specify_model_loss, l1_design_loss, [('session_info', 'session_info')]), - (l1_design_gain, l1_estimate_gain, [('spm_mat_file', 'spm_mat_file')]), - (l1_design_loss, l1_estimate_loss, [('spm_mat_file', 'spm_mat_file')]), - (l1_estimate_gain, contrast_estimate_gain, [('spm_mat_file', 'spm_mat_file'), - ('beta_images', 'beta_images'), - ('residual_image', 'residual_image')]), - (l1_estimate_loss, contrast_estimate_loss, [('spm_mat_file', 'spm_mat_file'), - ('beta_images', 'beta_images'), - ('residual_image', 'residual_image')]), - (contrast_estimate_gain, datasink, [('con_images', 'l1_analysis_gain.@con_images'), - ('spmT_images', 'l1_analysis_gain.@spmT_images'), - ('spm_mat_file', 'l1_analysis_gain.@spm_mat_file')]), - (contrast_estimate_loss, datasink, [('con_images', 'l1_analysis_loss.@con_images'), - ('spmT_images', 'l1_analysis_loss.@spmT_images'), - ('spm_mat_file', 'l1_analysis_loss.@spm_mat_file')]), - (contrast_estimate_gain, remove_smoothed_files, [('spmT_images', 'files')]) - ]) - - return l1_analysis - -def get_subset_contrasts(file_list, method, subject_list, participants_file): - ''' - Parameters : - - file_list : original file list selected by selectfiles node - - subject_list : list of subject IDs that are in the wanted group for the analysis - - participants_file: str, file containing participants characteristics - - method: str, one of "equalRange", "equalIndifference" or "groupComp" - - This function return the file list containing only the files belonging to subject in the wanted group. - ''' - equalIndifference_id = [] - equalRange_id = [] - equalIndifference_files = [] - equalRange_files = [] - - with open(participants_file, 'rt') as f: - next(f) # skip the header - - for line in f: + +from narps_open.pipelines import Pipeline + +class PipelineTeamQ6O0(Pipeline): + """ A class that defines the pipeline of team Q6O0. """ + + def __init__(self): + super().__init__() + self.fwhm = 8.0 + self.team_id = 'Q6O0' + self.contrast_list = ['0001'] + self.model_list = ['gain', 'loss'] + + def get_preprocessing(self): + """ No preprocessing has been done by team Q6O0 """ + return None + + def get_run_level_analysis(self): + """ No run level analysis has been done by team Q6O0 """ + return None + + # @staticmethod # Starting python 3.10, staticmethod should be used here + # Otherwise it produces a TypeError: 'staticmethod' object is not callable + def get_subject_infos(event_files, runs, model): + """ Create Bunchs for specifySPMModel. + Here, the team wanted to concatenate runs and used RT (response time) + for duration except for NoResponse trials for which the duration was set to 4. + Gain and loss amounts were used as parametric regressors. + + Parameters : + - event_files : list of files containing events information for each run + - runs: list of str, list of runs to use + - model: str, either 'gain' or 'loss'. + + Returns : + - subject_info : list of Bunch for 1st level analysis. + """ + from nipype.interfaces.base import Bunch + + condition_names = ['trial'] + onset = {} + duration = {} + weights_gain = {} + weights_loss = {} + + for run_id in range(len(runs)): # Loop over number of runs. + # creates dictionary items with empty lists + onset.update({s + '_run' + str(run_id + 1) : [] for s in condition_names}) + duration.update({s + '_run' + str(run_id + 1) : [] for s in condition_names}) + weights_gain.update({'gain_run' + str(run_id + 1) : []}) + weights_loss.update({'loss_run' + str(run_id + 1) : []}) + + for run_id, event_file in enumerate(event_files): + with open(event_file, 'rt') as file: + next(file) # skip the header + + for line in file: + info = line.strip().split() + + for condition in condition_names: + val = condition + '_run' + str(run_id + 1) # trial_run1 + val_gain = 'gain_run' + str(run_id + 1) # gain_run1 + val_loss = 'loss_run' + str(run_id + 1) # loss_run1 + onset[val].append(float(info[0])) # onsets for trial_run1 + duration[val].append(float(4)) # durations for trial : 4 + weights_gain[val_gain].append(float(info[2])) # weights gain for trial_run1 + weights_loss[val_loss].append(float(info[3])) # weights loss for trial_run1 + + # Bunching is done per run, i.e. trial_run1, trial_run2, etc. + # But names must not have '_run1' etc because we concatenate runs + subject_info = [] + for run_id in range(len(runs)): + + conditions = [c + '_run' + str(run_id + 1) for c in condition_names] + gain = 'gain_run' + str(run_id + 1) + loss = 'loss_run' + str(run_id + 1) + + if model == 'gain': + parametric_modulation_bunch = Bunch( + name = ['loss', 'gain'], + poly = [1, 1], + param = [weights_loss[loss], weights_gain[gain]] + ) + elif model == 'loss': + parametric_modulation_bunch = Bunch( + name = ['gain', 'loss'], + poly = [1, 1], + param = [weights_gain[gain], weights_loss[loss]] + ) + else: + raise AttributeError('Model must be gain or loss.') + + subject_info.insert( + run_id, + Bunch( + conditions = condition_names, + onsets=[onset[k] for k in conditions], + durations = [duration[k] for k in conditions], + amplitudes = None, + tmod = None, + pmod = [parametric_modulation_bunch], + regressor_names = None, + regressors = None) + ) + + return subject_info + + def get_contrasts_gain(subject_id): + """ + Create the list of tuples that represents contrasts. + Each contrast is in the form : + (Name,Stat,[list of condition names],[weights on those conditions]) + + Parameters: + - subject_id: str, ID of the subject + + Returns: + - contrasts: list of tuples, list of contrasts to analyze + """ + # List of condition names + conditions = ['trialxgain^1'] + + # Create contrasts + positive_effect_gain = ('positive_effect_gain', 'T', conditions, [1]) + + # Return contrast list + return [positive_effect_gain] + + def get_contrasts_loss(subject_id): + """ + Create the list of tuples that represents contrasts. + Each contrast is in the form : + (Name,Stat,[list of condition names],[weights on those conditions]) + + Parameters: + - subject_id: str, ID of the subject + + Returns: + - contrasts: list of tuples, list of contrasts to analyze + """ + # List of condition names + conditions = ['trialxloss^1'] + + # Create contrasts + positive_effect_loss = ('positive_effect_loss', 'T', conditions, [1]) + + # Return contrast list + return [positive_effect_loss] + + def get_parameters_file(filepaths, subject_id, working_dir): + """ + Create new tsv files with only desired parameters per subject per run. + The six motion parameters, the 5 aCompCor parameters, the global white matter and + cerebral spinal fluid signals were included as nuisance regressors/ + + Parameters : + - filepaths : paths to subject parameters file (i.e. one per run) + - subject_id : subject for whom the 1st level analysis is made + - working_dir: str, name of the sub-directory for intermediate results + + Return : + - parameters_file : paths to new files containing only desired parameters. + """ + from os import makedirs + from os.path import join, isdir + + import pandas as pd + import numpy as np + + # Handle the case where filepaths is a single path (str) + if not isinstance(filepaths, list): + filepaths = [filepaths] + + # Create the parameters files + parameters_file = [] + for file_id, file in enumerate(filepaths): + data_frame = pd.read_csv(file, sep = '\t', header=0) + + # Extract parameters we want to use for the model + temp_list = np.array([ + data_frame['X'], data_frame['Y'], data_frame['Z'], + data_frame['RotX'], data_frame['RotY'], data_frame['RotZ'], + data_frame['aCompCor00'], data_frame['aCompCor01'], data_frame['aCompCor02'], + data_frame['aCompCor03'], data_frame['aCompCor04'], data_frame['aCompCor05'], + data_frame['WhiteMatter'], data_frame['CSF']]) + retained_parameters = pd.DataFrame(np.transpose(temp_list)) + + # Write parameters to a parameters file + # TODO : warning !!! filepaths must be ordered (1,2,3,4) for the following code to work + new_path =join(working_dir, 'parameters_file', + f'parameters_file_sub-{subject_id}_run-{str(file_id + 1).zfill(2)}.tsv') + + makedirs(join(working_dir, 'parameters_file'), exist_ok = True) + + with open(new_path, 'w') as writer: + writer.write(retained_parameters.to_csv( + sep = '\t', index = False, header = False, na_rep = '0.0')) + + parameters_file.append(new_path) + + return parameters_file + + def remove_gunzip_files(_, subject_id, working_dir): + """ + This method is used in a Function node to fully remove + the files generated by the gunzip node, once they aren't needed anymore. + + Parameters: + - _: Node input only used for triggering the Node + - subject_id: str, TODO + - working_id: str, TODO + """ + from shutil import rmtree + from os.path import join + + try: + rmtree(join(working_dir, 'l1_analysis', f'_subject_id_{subject_id}', 'gunzip_func')) + except OSError as error: + print(error) + else: + print('The directory is deleted successfully') + + def remove_smoothed_files(_, subject_id, working_dir): + """ + This method is used in a Function node to fully remove + the files generated by the smoothing node, once they aren't needed anymore. + + Parameters: + - _: Node input only used for triggering the Node + - subject_id: str, TODO + - working_id: str, TODO + """ + from shutil import rmtree + from os.path import join + + try: + rmtree(join(working_dir, 'l1_analysis', f'_subject_id_{subject_id}', 'smooth')) + except OSError as error: + print(error) + else: + print('The directory is deleted successfully') + + def get_subject_level_analysis(self): + """ + Create the subject level analysis workflow. + + Returns: + - l1_analysis : nipype.WorkFlow + """ + # Infosource Node - To iterate on subjects + infosource = Node(IdentityInterface( fields = ['subject_id']), + name = 'infosource') + infosource.iterables = [('subject_id', self.subject_list)] + + # Templates to select files node + template = { + 'param' : join('derivatives', 'fmriprep', 'sub-{subject_id}', 'func', + 'sub-{subject_id}_task-MGT_run-*_bold_confounds.tsv'), + 'event' : join('sub-{subject_id}', 'func', + 'sub-{subject_id}_task-MGT_run-*_events.tsv'), + 'func' : join('derivatives', 'fmriprep', 'sub-{subject_id}', 'func', + 'sub-{subject_id}_task-MGT_run-*_bold_space-MNI152NLin2009cAsym_preproc.nii.gz') + } + + # SelectFiles - to select necessary files + selectfiles = Node(SelectFiles(template, base_directory = self.directories.dataset_dir), + name = 'selectfiles') + + # DataSink - store the wanted results in the wanted repository + datasink = Node(DataSink(base_directory = self.directories.output_dir), + name='datasink') + + # Gunzip - gunzip files because SPM do not use .nii.gz files + gunzip_func = MapNode(Gunzip(), name = 'gunzip_func', iterfield = ['in_file']) + + # Smooth - smoothing node + smooth = Node(Smooth(fwhm = self.fwhm), + name = 'smooth') + + # Function node get_subject_infos - get subject specific condition information + subject_infos_gain = Node(Function( + function = self.get_subject_infos, + input_names = ['event_files', 'runs', 'model'], + output_names=['subject_info']), + name='subject_infos_gain') + subject_infos_gain.inputs.runs = self.run_list + subject_infos_gain.inputs.model = 'gain' + + subject_infos_loss = Node(Function( + function = self.get_subject_infos, + input_names = ['event_files', 'runs', 'model'], + output_names = ['subject_info']), + name='subject_infos_loss') + subject_infos_loss.inputs.runs = self.run_list + subject_infos_loss.inputs.model = 'loss' + + # Function node get_parameters_file - get parameters files + parameters = Node(Function( + function = self.get_parameters_file, + input_names = ['filepaths', 'subject_id', 'working_dir'], + output_names = ['parameters_file']), + name = 'parameters') + parameters.inputs.working_dir = self.directories.working_dir + + # SpecifyModel - Generates SPM-specific Model + specify_model_gain = Node(SpecifySPMModel( + concatenate_runs = True, input_units = 'secs', output_units = 'secs', + time_repetition = self.tr, high_pass_filter_cutoff = 128), + name='specify_model_gain') + + specify_model_loss = Node(SpecifySPMModel( + concatenate_runs = True, input_units = 'secs', output_units = 'secs', + time_repetition = self.tr, high_pass_filter_cutoff = 128), + name='specify_model_loss') + + # Level1Design - Generates an SPM design matrix + l1_design_gain = Node(Level1Design( + bases = {'hrf': {'derivs': [0, 0]}}, timing_units = 'secs', + interscan_interval = self.tr, model_serial_correlations = 'AR(1)'), + name='l1_design_gain') + + l1_design_loss = Node(Level1Design( + bases = {'hrf': {'derivs': [0, 0]}}, timing_units = 'secs', + interscan_interval = self.tr, model_serial_correlations = 'AR(1)'), + name='l1_design_loss') + + # EstimateModel - estimate the parameters of the model + l1_estimate_gain = Node(EstimateModel( + estimation_method = {'Classical': 1}), + name = 'l1_estimate_gain') + + l1_estimate_loss = Node(EstimateModel( + estimation_method = {'Classical': 1}), + name = 'l1_estimate_loss') + + # Function nodes get_contrasts_* - get the contrasts + contrasts_gain = Node(Function( + function = self.get_contrasts_gain, + input_names = ['subject_id'], + output_names = ['contrasts']), + name = 'contrasts_gain') + + contrasts_loss = Node(Function( + function = self.get_contrasts_loss, + input_names = ['subject_id'], + output_names = ['contrasts']), + name = 'contrasts_loss') + + # EstimateContrast - estimates contrasts + contrast_estimate_gain = Node(EstimateContrast(), + name = 'contrast_estimate_gain') + + contrast_estimate_loss = Node(EstimateContrast(), + name = 'contrast_estimate_loss') + + # Function node remove_gunzip_files - remove output of the gunzip node + remove_gunzip_files = Node(Function( + function = self.remove_gunzip_files, + input_names = ['_', 'subject_id', 'working_dir'], + output_names = []), + name = 'remove_gunzip_files') + remove_gunzip_files.inputs.working_dir = self.directories.working_dir + + # Function node remove_smoothed_files - remove output of the smooth node + remove_smoothed_files = Node(Function( + function = self.remove_smoothed_files, + input_names = ['_', 'subject_id', 'working_dir'], + output_names = []), + name = 'remove_smoothed_files') + remove_smoothed_files.inputs.working_dir = self.directories.working_dir + + # Create l1 analysis workflow and connect its nodes + l1_analysis = Workflow(base_dir = self.directories.working_dir, name = 'l1_analysis') + l1_analysis.connect([ + (infosource, selectfiles, [('subject_id', 'subject_id')]), + (selectfiles, subject_infos_gain, [('event','event_files')]), + (selectfiles, subject_infos_loss, [('event','event_files')]), + (selectfiles, parameters, [('param', 'filepaths')]), + (infosource, parameters, [('subject_id', 'subject_id')]), + (infosource, contrasts_gain, [('subject_id', 'subject_id')]), + (infosource, contrasts_loss, [('subject_id', 'subject_id')]), + (infosource, remove_gunzip_files, [('subject_id', 'subject_id')]), + (infosource, remove_smoothed_files, [('subject_id', 'subject_id')]), + (subject_infos_gain, specify_model_gain, [('subject_info', 'subject_info')]), + (subject_infos_loss, specify_model_loss, [('subject_info', 'subject_info')]), + (contrasts_gain, contrast_estimate_gain, [('contrasts', 'contrasts')]), + (contrasts_loss, contrast_estimate_loss, [('contrasts', 'contrasts')]), + (selectfiles, gunzip_func, [('func', 'in_file')]), + (gunzip_func, smooth, [('out_file', 'in_files')]), + (smooth, remove_gunzip_files, [('smoothed_files', '_')]), + (smooth, specify_model_gain, [('smoothed_files', 'functional_runs')]), + (smooth, specify_model_loss, [('smoothed_files', 'functional_runs')]), + (parameters, specify_model_gain, [('parameters_file', 'realignment_parameters')]), + (parameters, specify_model_loss, [('parameters_file', 'realignment_parameters')]), + (specify_model_gain, l1_design_gain, [('session_info', 'session_info')]), + (specify_model_loss, l1_design_loss, [('session_info', 'session_info')]), + (l1_design_gain, l1_estimate_gain, [('spm_mat_file', 'spm_mat_file')]), + (l1_design_loss, l1_estimate_loss, [('spm_mat_file', 'spm_mat_file')]), + (l1_estimate_gain, contrast_estimate_gain, [ + ('spm_mat_file', 'spm_mat_file'), + ('beta_images', 'beta_images'), + ('residual_image', 'residual_image')]), + (l1_estimate_loss, contrast_estimate_loss, [ + ('spm_mat_file', 'spm_mat_file'), + ('beta_images', 'beta_images'), + ('residual_image', 'residual_image')]), + (contrast_estimate_gain, datasink, [ + ('con_images', 'l1_analysis_gain.@con_images'), + ('spmT_images', 'l1_analysis_gain.@spmT_images'), + ('spm_mat_file', 'l1_analysis_gain.@spm_mat_file')]), + (contrast_estimate_loss, datasink, [ + ('con_images', 'l1_analysis_loss.@con_images'), + ('spmT_images', 'l1_analysis_loss.@spmT_images'), + ('spm_mat_file', 'l1_analysis_loss.@spm_mat_file')]), + (contrast_estimate_gain, remove_smoothed_files, [('spmT_images', '_')]) + ]) + + return l1_analysis + + def get_subject_level_outputs(self): + """ Return the names of the files the subject level analysis is supposed to generate. """ + + # Generate a list of parameter sets for further templates formatting + parameters = { + 'contrast_id': self.contrast_list, + 'model_type': self.model_list, + 'subject_id': self.subject_list + } + # Combining all possibilities + # Here we use a list because the itertools.product is an iterator objects that + # is meant for a single-use iteration only. + parameter_sets = list(product(*parameters.values())) + + # Contrat maps + contrast_map_template = join( + self.directories.output_dir, + 'l1_analysis_{model_type}', '_subject_id_{subject_id}', 'con_{contrast_id}.nii' + ) + + # SPM.mat file + mat_file_template = join( + self.directories.output_dir, + 'l1_analysis_{model_type}', '_subject_id_{subject_id}', 'SPM.mat' + ) + + # spmT maps + spmt_file_template = join( + self.directories.output_dir, + 'l1_analysis_{model_type}', '_subject_id_{subject_id}', 'spmT_{contrast_id}.nii' + ) + + # Formatting templates and returning it as a list of files + output_files = [contrast_map_template.format(**dict(zip(parameters.keys(), parameter_values)))\ + for parameter_values in parameter_sets] + output_files += [mat_file_template.format(**dict(zip(parameters.keys(), parameter_values)))\ + for parameter_values in parameter_sets] + output_files += [spmt_file_template.format(**dict(zip(parameters.keys(), parameter_values)))\ + for parameter_values in parameter_sets] + + return output_files + + def get_subset_contrasts(file_list, subject_list, participants_file): + """ + Parameters : + - file_list : original file list selected by selectfiles node + - subject_list : list of subject IDs that are in the wanted group for the analysis + - participants_file: str, file containing participants characteristics + + Returns: + - The file list containing only the files belonging to subject in the wanted group. + """ + equal_indifference_id = [] + equal_range_id = [] + equal_indifference_files = [] + equal_range_files = [] + + with open(participants_file, 'rt') as file: + next(file) # skip the header + for line in file: info = line.strip().split() - if info[0][-3:] in subject_list and info[1] == "equalIndifference": - equalIndifference_id.append(info[0][-3:]) + equal_indifference_id.append(info[0][-3:]) elif info[0][-3:] in subject_list and info[1] == "equalRange": - equalRange_id.append(info[0][-3:]) - - for file in file_list: - sub_id = file.split('/') - if sub_id[-2][-3:] in equalIndifference_id: - equalIndifference_files.append(file) - elif sub_id[-2][-3:] in equalRange_id: - equalRange_files.append(file) - - return equalIndifference_id, equalRange_id, equalIndifference_files, equalRange_files - - -def get_l2_analysis(subject_list, n_sub, model_list, method, exp_dir, result_dir, working_dir, output_dir): - """ - Returns the 2nd level of analysis workflow. - - Parameters: - - exp_dir: str, directory where raw data are stored - - result_dir: str, directory where results will be stored - - working_dir: str, name of the sub-directory for intermediate results - - output_dir: str, name of the sub-directory for final results - - subject_list: list of str, list of subject for which you want to do the preprocessing - - model_list: list of str, list of models to use for the analysis - - contrast_list: list of str, list of contrasts to analyze - - n_sub: float, number of subjects used to do the analysis - - method: one of "equalRange", "equalIndifference" or "groupComp" - - Returns: - - l2_analysis: Nipype WorkFlow - """ - # Infosource - a function free node to iterate over the list of subject names - infosource_groupanalysis = Node(IdentityInterface(fields=['subjects', 'model_type'], - subjects = subject_list), - name="infosource_groupanalysis") - - infosource_groupanalysis.iterables = [('model_type', model_list)] - - # SelectFiles - contrast_file = opj(result_dir, output_dir, "l1_analysis_{model_type}", "_subject_id_*", "con_0001.nii") - - participants_file = opj(exp_dir, 'participants.tsv') - - templates = {'contrast' : contrast_file, 'participants' : participants_file} - - selectfiles_groupanalysis = Node(SelectFiles(templates, base_directory=result_dir, force_list= True), - name="selectfiles_groupanalysis") - - # Datasink node : to save important files - datasink_groupanalysis = Node(DataSink(base_directory = result_dir, container = output_dir), - name = 'datasink_groupanalysis') - - # Node to select subset of contrasts - sub_contrasts = Node(Function(input_names = ['file_list', 'method', 'subject_list', 'participants_file'], - output_names = ['equalIndifference_id', 'equalRange_id', 'equalIndifference_files', 'equalRange_files'], - function = get_subset_contrasts), - name = 'sub_contrasts') - - sub_contrasts.inputs.method = method - - ## Estimate model - estimate_model = Node(EstimateModel(estimation_method={'Classical':1}), name = "estimate_model") - - ## Estimate contrasts - estimate_contrast = Node(EstimateContrast(group_contrast=True), - name = "estimate_contrast") - - ## Create thresholded maps - threshold = MapNode(Analysis_Threshold(use_fwe_correction=True, - height_threshold_type='p-value', - force_activation = False), name = "threshold", iterfield = ["stat_image", "contrast_index"]) - - l2_analysis = Workflow(base_dir = opj(result_dir, working_dir), name = 'l2_analysis') - - l2_analysis.connect([(infosource_groupanalysis, selectfiles_groupanalysis, [('model_type', 'model_type')]), - (infosource_groupanalysis, sub_contrasts, [('subjects', 'subject_list')]), - (selectfiles_groupanalysis, sub_contrasts, [('contrast', 'file_list'), ('participants', 'participants_file')]), - (estimate_model, estimate_contrast, [('spm_mat_file', 'spm_mat_file'), - ('residual_image', 'residual_image'), - ('beta_images', 'beta_images')]), - (estimate_contrast, threshold, [('spm_mat_file', 'spm_mat_file'), - ('spmT_images', 'stat_image')]), - (estimate_model, datasink_groupanalysis, [('mask_image', f"l2_analysis_{method}_nsub_{n_sub}.@mask")]), - (estimate_contrast, datasink_groupanalysis, [('spm_mat_file', f"l2_analysis_{method}_nsub_{n_sub}.@spm_mat"), - ('spmT_images', f"l2_analysis_{method}_nsub_{n_sub}.@T"), - ('con_images', f"l2_analysis_{method}_nsub_{n_sub}.@con")]), - (threshold, datasink_groupanalysis, [('thresholded_map', f"l2_analysis_{method}_nsub_{n_sub}.@thresh")])]) - - if method=='equalRange' or method=='equalIndifference': - contrasts = [('Group', 'T', ['mean'], [1]), ('Group', 'T', ['mean'], [-1])] - ## Specify design matrix - one_sample_t_test_design = Node(OneSampleTTestDesign(), name = "one_sample_t_test_design") - - l2_analysis.connect([(sub_contrasts, one_sample_t_test_design, [(f"{method}_files", 'in_files')]), - (one_sample_t_test_design, estimate_model, [('spm_mat_file', 'spm_mat_file')])]) - - threshold.inputs.contrast_index = [1, 2] - threshold.synchronize = True - - elif method == 'groupComp': - contrasts = [('Eq range vs Eq indiff in loss', 'T', ['Group_{1}', 'Group_{2}'], [1, -1])] - # Node for the design matrix - two_sample_t_test_design = Node(TwoSampleTTestDesign(), name = 'two_sample_t_test_design') - - l2_analysis.connect([(sub_contrasts, two_sample_t_test_design, [('equalRange_files', "group1_files"), - ('equalIndifference_files', 'group2_files')]), - (two_sample_t_test_design, estimate_model, [("spm_mat_file", "spm_mat_file")])]) - - threshold.inputs.contrast_index = [1] - threshold.synchronize = True - - estimate_contrast.inputs.contrasts = contrasts - - return l2_analysis - - -def reorganize_results(result_dir, output_dir, n_sub, team_ID): - """ - Reorganize the results to analyze them. - - Parameters: - - result_dir: str, directory where results will be stored - - output_dir: str, name of the sub-directory for final results - - n_sub: float, number of subject used for the analysis - - team_ID: str, ID of the team to reorganize results - - """ - from os.path import join as opj - import os - import shutil - import gzip - - h1 = opj(result_dir, output_dir, f"l2_analysis_equalIndifference_nsub_{n_sub}", '_model_type_gain') - h2 = opj(result_dir, output_dir, f"l2_analysis_equalRange_nsub_{n_sub}", '_model_type_gain') - h3 = opj(result_dir, output_dir, f"l2_analysis_equalIndifference_nsub_{n_sub}", '_model_type_gain') - h4 = opj(result_dir, output_dir, f"l2_analysis_equalRange_nsub_{n_sub}", '_model_type_gain') - h5 = opj(result_dir, output_dir, f"l2_analysis_equalIndifference_nsub_{n_sub}", '_model_type_loss') - h6 = opj(result_dir, output_dir, f"l2_analysis_equalRange_nsub_{n_sub}", '_model_type_loss') - h7 = opj(result_dir, output_dir, f"l2_analysis_equalIndifference_nsub_{n_sub}", '_model_type_loss') - h8 = opj(result_dir, output_dir, f"l2_analysis_equalRange_nsub_{n_sub}", '_model_type_loss') - h9 = opj(result_dir, output_dir, f"l2_analysis_groupComp_nsub_{n_sub}", '_model_type_loss') - - h = [h1, h2, h3, h4, h5, h6, h7, h8, h9] - - repro_unthresh = [opj(filename, "spmT_0002.nii") if i in [4, 5] else opj(filename, "spmT_0001.nii") for i, filename in enumerate(h)] - - repro_thresh = [opj(filename, "_threshold1", - "spmT_0002_thr.nii") if i in [4, 5] else opj(filename, "_threshold0", "spmT_0001_thr.nii") for i, filename in enumerate(h)] - - if not os.path.isdir(opj(result_dir, "NARPS-reproduction")): - os.mkdir(opj(result_dir, "NARPS-reproduction")) - - for i, filename in enumerate(repro_unthresh): - f_in = filename - f_out = opj(result_dir, "NARPS-reproduction", f"team_{team_ID}_nsub_{n_sub}_hypo{i+1}_unthresholded.nii") - shutil.copyfile(f_in, f_out) - - for i, filename in enumerate(repro_thresh): - f_in = filename - f_out = opj(result_dir, "NARPS-reproduction", f"team_{team_ID}_nsub_{n_sub}_hypo{i+1}_thresholded.nii") - shutil.copyfile(f_in, f_out) - - print(f"Results files of team {team_ID} reorganized.") - - - - + equal_range_id.append(info[0][-3:]) + + for file in file_list: + sub_id = file.split('/') + if sub_id[-2][-3:] in equal_indifference_id: + equal_indifference_files.append(file) + elif sub_id[-2][-3:] in equal_range_id: + equal_range_files.append(file) + + return equal_indifference_id, equal_range_id, equal_indifference_files, equal_range_files + + def get_group_level_analysis(self): + """ + Return all workflows for the group level analysis. + + Returns; + - a list of nipype.WorkFlow + """ + + methods = ['equalRange', 'equalIndifference', 'groupComp'] + return [self.get_group_level_analysis_sub_workflow(method) for method in methods] + + def get_group_level_analysis_sub_workflow(self, method): + """ + Return a workflow for the group level analysis. + + Parameters: + - method: one of 'equalRange', 'equalIndifference' or 'groupComp' + + Returns: + - l2_analysis: nipype.WorkFlow + """ + # Compute the number of participants used to do the analysis + nb_subjects = len(self.subject_list) + + # Infosource - iterate over the list of contrasts + infosource_groupanalysis = Node( + IdentityInterface( + fields=['subjects', 'model_type'], + subjects = self.subject_list), + name='infosource_groupanalysis') + infosource_groupanalysis.iterables = [('model_type', self.model_list)] + + # SelectFiles + templates = { + 'contrast' : join(self.directories.output_dir, + 'l1_analysis_{model_type}', '_subject_id_*', 'con_0001.nii'), + 'participants' : join(self.directories.dataset_dir, 'participants.tsv') + } + + selectfiles_groupanalysis = Node(SelectFiles( + templates, + base_directory = self.directories.results_dir, + force_list= True), + name="selectfiles_groupanalysis") + + # Datasink - save important files + datasink_groupanalysis = Node(DataSink( + base_directory = str(self.directories.output_dir) + ), + name = 'datasink_groupanalysis') + + # Function node get_subset_contrasts - select subset of contrasts + sub_contrasts = Node(Function( + function = self.get_subset_contrasts, + input_names = ['file_list', 'subject_list', 'participants_file'], + output_names = [ + 'equalIndifference_id', + 'equalRange_id', + 'equalIndifference_files', + 'equalRange_files']), + name = 'sub_contrasts') + + # Estimate model + estimate_model = Node(EstimateModel( + estimation_method={'Classical':1}), + name = "estimate_model") + + # Estimate contrasts + estimate_contrast = Node(EstimateContrast( + group_contrast=True), + name = "estimate_contrast") + + # Create thresholded maps + threshold = MapNode(Threshold( + use_fwe_correction = True, + height_threshold_type = 'p-value', + force_activation = False), + name = "threshold", iterfield = ['stat_image', 'contrast_index']) + + l2_analysis = Workflow( + base_dir = self.directories.working_dir, + name = f'l2_analysis_{method}_nsub_{nb_subjects}') + + l2_analysis.connect([ + (infosource_groupanalysis, selectfiles_groupanalysis, [ + ('model_type', 'model_type')]), + (infosource_groupanalysis, sub_contrasts, [ + ('subjects', 'subject_list')]), + (selectfiles_groupanalysis, sub_contrasts, [ + ('contrast', 'file_list'), + ('participants', 'participants_file')]), + (estimate_model, estimate_contrast, [ + ('spm_mat_file', 'spm_mat_file'), + ('residual_image', 'residual_image'), + ('beta_images', 'beta_images')]), + (estimate_contrast, threshold, [ + ('spm_mat_file', 'spm_mat_file'), + ('spmT_images', 'stat_image')]), + (estimate_model, datasink_groupanalysis, [ + ('mask_image', f"l2_analysis_{method}_nsub_{nb_subjects}.@mask")]), + (estimate_contrast, datasink_groupanalysis, [ + ('spm_mat_file', f"l2_analysis_{method}_nsub_{nb_subjects}.@spm_mat"), + ('spmT_images', f"l2_analysis_{method}_nsub_{nb_subjects}.@T"), + ('con_images', f"l2_analysis_{method}_nsub_{nb_subjects}.@con")]), + (threshold, datasink_groupanalysis, [ + ('thresholded_map', f"l2_analysis_{method}_nsub_{nb_subjects}.@thresh")])]) + + if method in ('equalRange', 'equalIndifference'): + contrasts = [('Group', 'T', ['mean'], [1]), ('Group', 'T', ['mean'], [-1])] + + # Specify design matrix + one_sample_t_test_design = Node(OneSampleTTestDesign(), + name = 'one_sample_t_test_design') + + l2_analysis.connect([ + (sub_contrasts, one_sample_t_test_design, [(f"{method}_files", 'in_files')]), + (one_sample_t_test_design, estimate_model, [('spm_mat_file', 'spm_mat_file')])]) + + threshold.inputs.contrast_index = [1, 2] + threshold.synchronize = True + + elif method == 'groupComp': + contrasts = [( + 'Eq range vs Eq indiff in loss', 'T', ['Group_{1}', 'Group_{2}'], [1, -1])] + + # Specify design matrix + two_sample_t_test_design = Node(TwoSampleTTestDesign(), + name = 'two_sample_t_test_design') + + l2_analysis.connect([ + (sub_contrasts, two_sample_t_test_design, [ + ('equalRange_files', "group1_files"), + ('equalIndifference_files', 'group2_files')]), + (two_sample_t_test_design, estimate_model, [("spm_mat_file", "spm_mat_file")])]) + + threshold.inputs.contrast_index = [1] + threshold.synchronize = True + + estimate_contrast.inputs.contrasts = contrasts + + return l2_analysis + + def get_group_level_outputs(self): + """ Return all names for the files the group level analysis is supposed to generate. """ + + # Handle equalRange and equalIndifference + parameters = { + 'model_type': self.model_list, + 'method': ['equalRange', 'equalIndifference'], + 'file': [ + 'con_0001.nii', 'con_0002.nii', 'mask.nii', 'SPM.mat', + 'spmT_0001.nii', 'spmT_0002.nii', + join('_threshold0', 'spmT_0001_thr.nii'), join('_threshold1', 'spmT_0002_thr.nii') + ], + 'nb_subjects': [str(len(self.subject_list))] + } + parameter_sets = product(*parameters.values()) + template = join( + self.directories.output_dir, + 'l2_analysis_{method}_nsub_{nb_subjects}', + '_model_type_{model_type}', + '{file}' + ) + + return_list = [template.format(**dict(zip(parameters.keys(), parameter_values)))\ + for parameter_values in parameter_sets] + + # Handle groupComp + parameters = { + 'model_type': ['loss'], + 'method': ['groupComp'], + 'file': [ + 'con_0001.nii', 'mask.nii', 'SPM.mat', 'spmT_0001.nii', + join('_threshold0', 'spmT_0001_thr.nii') + ], + 'nb_subjects' : [str(len(self.subject_list))] + } + parameter_sets = product(*parameters.values()) + + return_list += [template.format(**dict(zip(parameters.keys(), parameter_values)))\ + for parameter_values in parameter_sets] + + return return_list + + def get_hypotheses_outputs(self): + """ Return all hypotheses output file names. """ + nb_sub = len(self.subject_list) + files = [ + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_gain', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_gain', 'spmT_0001.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_gain', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_gain', 'spmT_0001.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_gain', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_gain', 'spmT_0001.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_gain', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_gain', 'spmT_0001.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_loss', '_threshold1', 'spmT_0002_thr.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_loss', 'spmT_0002.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_loss', '_threshold1', 'spmT_0002_thr.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_loss', 'spmT_0002.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_loss', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalIndifference_nsub_{nb_sub}', '_model_type_loss', 'spmT_0001.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_loss', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_equalRange_nsub_{nb_sub}', '_model_type_loss', 'spmT_0001.nii'), + join(f'l2_analysis_groupComp_nsub_{nb_sub}', '_model_type_loss', '_threshold0', 'spmT_0001_thr.nii'), + join(f'l2_analysis_groupComp_nsub_{nb_sub}', '_model_type_loss', 'spmT_0001.nii') + ] + return [join(self.directories.output_dir, f) for f in files] diff --git a/tests/conftest.py b/tests/conftest.py index e1530e48..7c57c1f9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -92,7 +92,7 @@ def test_pipeline_execution( # Retrieve the paths to the results files collection = ResultsCollection(team_id) - results_files = [join(collection.directory, f) for f in collection.files.keys()] + results_files = [join(collection.directory, f) for f in sorted(collection.files.keys())] results_files = [results_files[i] for i in indices] # Compute the correlation coefficients diff --git a/tests/pipelines/test_pipelines.py b/tests/pipelines/test_pipelines.py index 9016aeb7..c38cf36a 100644 --- a/tests/pipelines/test_pipelines.py +++ b/tests/pipelines/test_pipelines.py @@ -135,8 +135,8 @@ class TestUtils: @mark.unit_test def test_utils(): """ Test the utils methods of PipelineRunner """ - # 1 - Get number of not implemented pipelines - assert len(get_not_implemented_pipelines()) == 69 + # 1 - Get not implemented pipelines + assert '1K0E' in get_not_implemented_pipelines() - # 2 - Get number of implemented pipelines - assert len(get_implemented_pipelines()) == 1 + # 2 - Get implemented pipelines + assert '2T6S' in get_implemented_pipelines() diff --git a/tests/pipelines/test_team_Q6O0.py b/tests/pipelines/test_team_Q6O0.py new file mode 100644 index 00000000..639f609e --- /dev/null +++ b/tests/pipelines/test_team_Q6O0.py @@ -0,0 +1,68 @@ +#!/usr/bin/python +# coding: utf-8 + +""" Tests of the 'narps_open.pipelines.team_Q6O0' module. + +Launch this test with PyTest + +Usage: +====== + pytest -q test_team_Q6O0.py + pytest -q test_team_Q6O0.py -k +""" + +from pytest import helpers, mark +from nipype import Workflow + +from narps_open.pipelines.team_Q6O0 import PipelineTeamQ6O0 + +class TestPipelinesTeamQ6O0: + """ A class that contains all the unit tests for the PipelineTeamQ6O0 class.""" + + @staticmethod + @mark.unit_test + def test_create(): + """ Test the creation of a PipelineTeamQ6O0 object """ + + pipeline = PipelineTeamQ6O0() + + # 1 - check the parameters + assert pipeline.fwhm == 8.0 + assert pipeline.team_id == 'Q6O0' + + # 2 - check workflows + assert pipeline.get_preprocessing() is None + assert pipeline.get_run_level_analysis() is None + assert isinstance(pipeline.get_subject_level_analysis(), Workflow) + + group_level = pipeline.get_group_level_analysis() + assert len(group_level) == 3 + for sub_workflow in group_level: + assert isinstance(sub_workflow, Workflow) + + @staticmethod + @mark.unit_test + def test_outputs(): + """ Test the expected outputs of a PipelineTeamQ6O0 object """ + pipeline = PipelineTeamQ6O0() + # 1 - 1 subject outputs + pipeline.subject_list = ['001'] + assert len(pipeline.get_preprocessing_outputs()) == 0 + assert len(pipeline.get_run_level_outputs()) == 0 + assert len(pipeline.get_subject_level_outputs()) == 6 + assert len(pipeline.get_group_level_outputs()) == 37 + assert len(pipeline.get_hypotheses_outputs()) == 18 + + # 2 - 4 subjects outputs + pipeline.subject_list = ['001', '002', '003', '004'] + assert len(pipeline.get_preprocessing_outputs()) == 0 + assert len(pipeline.get_run_level_outputs()) == 0 + assert len(pipeline.get_subject_level_outputs()) == 24 + assert len(pipeline.get_group_level_outputs()) == 37 + assert len(pipeline.get_hypotheses_outputs()) == 18 + + @staticmethod + @mark.pipeline_test + def test_execution(): + """ Test the execution of a PipelineTeamQ6O0 and compare results """ + helpers.test_pipeline_evaluation('Q6O0') From a3e0a5f77a37cb55258cce53b294bd406c1c3fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Boris=20Cl=C3=A9net?= <117362283+bclenet@users.noreply.github.com> Date: Wed, 27 Sep 2023 13:41:23 +0200 Subject: [PATCH 2/2] Fixing the `narps_open.utils.status` module (#109) * [BUG] inside unit_tests workflow * Browsing all issues pages from Github API * Get all pages of GitHub issues * [TEST] Updating test for status module * [TEST] fetch several issues --- docs/status.md | 2 +- narps_open/utils/status.py | 18 ++++-- tests/utils/test_status.py | 123 +++++++++++++++++++++++-------------- 3 files changed, 92 insertions(+), 51 deletions(-) diff --git a/docs/status.md b/docs/status.md index f323cc8f..7fde8239 100644 --- a/docs/status.md +++ b/docs/status.md @@ -36,7 +36,7 @@ print(pipeline_info['status']) report.markdown() # Returns a string containing the markdown ``` -You can also use the command-line tool as so. Option `-t` is for the team id, option `-d` allows to print only one of the sub parts of the description among : `general`, `exclusions`, `preprocessing`, `analysis`, and `categorized_for_analysis`. +You can also use the command-line tool as so. ```bash python narps_open/utils/status -h diff --git a/narps_open/utils/status.py b/narps_open/utils/status.py index cc4eb8a7..fef7708d 100644 --- a/narps_open/utils/status.py +++ b/narps_open/utils/status.py @@ -18,10 +18,20 @@ def get_opened_issues(): """ Return a list of opened issues and pull requests for the NARPS Open Pipelines project """ request_url = 'https://api.github.com/repos/Inria-Empenn/narps_open_pipelines/issues' - response = get(request_url, timeout = 2) - response.raise_for_status() - - return response.json() + request_url += '?page={page_number}?per_page=100' + + issues = [] + page = True # Will later be replaced by a table + page_number = 1 # According to the doc, first page is not page 0 + # https://docs.github.com/en/rest/issues/issues#list-repository-issues + while bool(page) is True : # Test if the page is empty + response = get(request_url.format(page_number = str(page_number)), timeout = 2) + response.raise_for_status() + page = response.json() + issues += page + page_number += 1 + + return issues def get_teams_with_pipeline_files(): """ Return a set of teams having a file for their pipeline in the repository """ diff --git a/tests/utils/test_status.py b/tests/utils/test_status.py index 0e98ef83..6c16279b 100644 --- a/tests/utils/test_status.py +++ b/tests/utils/test_status.py @@ -34,38 +34,62 @@ def mock_api_issue(mocker): which is actually imported as `get` inside the `narps_open.utils.status` module. Hence, we patch the `narps_open.utils.status.get` method. """ - response = Response() - response.status_code = 200 - def json_func(): - return [ - { - "html_url": "url_issue_2", - "number": 2, - "title" : "Issue for pipeline UK24", - "body" : "Nothing to add here." - }, - { - "html_url": "url_pull_3", - "number": 3, - "title" : "Pull request for pipeline 2T6S", - "pull_request" : {}, - "body" : "Work has been done." - }, - { - "html_url": "url_issue_4", - "number": 4, - "title" : None, - "body" : "This is a malformed issue about C88N." - }, - { - "html_url": "url_issue_5", - "number": 5, - "title" : "Issue about 2T6S", - "body" : "Something about 2T6S." - } - ] - response.json = json_func - mocker.patch('narps_open.utils.status.get', return_value = response) + + # Create a method to mock requests.get + def mocked_requests_get(url, params=None, **kwargs): + + response = Response() + response.status_code = 200 + def json_func_page_0(): + return [ + { + "html_url": "url_issue_2", + "number": 2, + "title" : "Issue for pipeline UK24", + "body" : "Nothing to add here." + }, + { + "html_url": "url_pull_3", + "number": 3, + "title" : "Pull request for pipeline 2T6S", + "pull_request" : {}, + "body" : "Work has been done." + } + ] + + json_func_page_1 = json_func_page_0 + + def json_func_page_2(): + return [ + { + "html_url": "url_issue_4", + "number": 4, + "title" : None, + "body" : "This is a malformed issue about C88N." + }, + { + "html_url": "url_issue_5", + "number": 5, + "title" : "Issue about 2T6S", + "body" : "Something about 2T6S." + } + ] + + def json_func_page_3(): + return [] + + if '?page=1' in url: + response.json = json_func_page_1 + elif '?page=2' in url: + response.json = json_func_page_2 + elif '?page=3' in url: + response.json = json_func_page_3 + else: + response.json = json_func_page_0 + + return response + + mocker.patch('narps_open.utils.status.get', side_effect = mocked_requests_get) mocker.patch( 'narps_open.utils.status.get_teams_with_pipeline_files', return_value = ['2T6S', 'UK24', 'Q6O0'] @@ -94,8 +118,6 @@ def test_get_issues(mocker): which is actually imported as `get` inside the `narps_open.utils.status` module. Hence, we patch the `narps_open.utils.status.get` method. """ - get_opened_issues() - # Create a mock API response for 404 error response = Response() response.status_code = 404 @@ -114,18 +136,27 @@ def json_func(): assert len(get_opened_issues()) == 0 # Create a mock API response for the general usecase - response = Response() - response.status_code = 200 - def json_func(): - return [ - { - "html_url": "urls", - "number": 2, - } - ] - response.json = json_func - - mocker.patch('narps_open.utils.status.get', return_value = response) + def mocked_requests_get(url, params=None, **kwargs): + response = Response() + response.status_code = 200 + def json_func_page_1(): + return [ + { + "html_url": "urls", + "number": 2, + } + ] + def json_func_page_2(): + return [] + + if '?page=2' in url: + response.json = json_func_page_2 + else: + response.json = json_func_page_1 + + return response + + mocker.patch('narps_open.utils.status.get', side_effect = mocked_requests_get) issues = get_opened_issues() assert len(issues) == 1 assert issues[0]['html_url'] == 'urls'