Skip to content
Snippets Groups Projects
IDP_func_task_activation.py 4.73 KiB
#!/usr/bin/env python
#
# IDP_func_task_activation.py - Generating IDP file with tfMRI activation
#
# Author: Fidel Alfaro Almagro <fidel.alfaroalmagro@ndcn.ox.ac.uk>
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
# Author: Michiel Cottaar <michiel.cottaar@ndcn.ox.ac.uk>
#
# pylint: disable=C0103,E0602,C0114,C0115,C0116,R0913,R0914,R0915
#

import os
import json
import shutil
import logging
from fsl import wrappers
from pipe_tree import In, Out, Ref
from bip.utils.log_utils import redirect_logging

log = logging.getLogger(__name__)

def run(ctx,
        T1_to_MNI_warp:            In,
        T1_to_MNI_warp_coef_inv:   In,
        tfMRI_feat:                In,
        tfMRI_cope1:               In,
        tfMRI_cope2:               In,
        tfMRI_cope5:               In,
        tfMRI_zstat1:              In,
        tfMRI_zstat2:              In,
        tfMRI_zstat5:              In,
        highres2standard_warp:     Ref,
        highres2standard_warp_inv: Ref,
        logs_dir:                  Ref,
        tfMRI_featquery_1_dir:     Out,
        tfMRI_featquery_2_dir:     Out,
        tfMRI_featquery_5_dir:     Out,
        tfMRI_featquery_5a_dir:    Out,
        tfMRI_featquery_1_report:  Out,
        tfMRI_featquery_2_report:  Out,
        tfMRI_featquery_5_report:  Out,
        tfMRI_featquery_5a_report: Out,
        IDP_func_task_activation:  Out):
    
    with redirect_logging('IDP_func_task_activation', outdir=logs_dir):
        if not os.path.exists(highres2standard_warp):
            os.symlink(src=os.getcwd() + "/" + T1_to_MNI_warp, 
                       dst=highres2standard_warp)

        if not os.path.exists(highres2standard_warp_inv):
            os.symlink(src=os.getcwd() + "/" + T1_to_MNI_warp_coef_inv,
                       dst=highres2standard_warp_inv)

        for dir_name in [tfMRI_featquery_1_dir, tfMRI_featquery_2_dir,
                         tfMRI_featquery_5_dir, tfMRI_featquery_5a_dir]:
            if os.path.exists(dir_name):
                shutil.rmtree(dir_name)

        group_mask_1  = ctx.get_data("tfMRI_group/groupMask1.nii.gz")
        group_mask_2  = ctx.get_data("tfMRI_group/groupMask2.nii.gz")
        group_mask_5  = ctx.get_data("tfMRI_group/groupMask5.nii.gz")
        group_mask_5a = ctx.get_data("tfMRI_group/groupMask5a.nii.gz")

        tfMRI_feat += "/"

        N_tfMRI_cope1 = tfMRI_cope1.replace(tfMRI_feat, "")
        N_tfMRI_cope2 = tfMRI_cope2.replace(tfMRI_feat, "")
        N_tfMRI_cope5 = tfMRI_cope5.replace(tfMRI_feat, "")

        N_tfMRI_zstat1 = tfMRI_zstat1.replace(tfMRI_feat, "")
        N_tfMRI_zstat2 = tfMRI_zstat2.replace(tfMRI_feat, "")
        N_tfMRI_zstat5 = tfMRI_zstat5.replace(tfMRI_feat, "")

        N_tfMRI_featquery_1_dir  = tfMRI_featquery_1_dir.replace(tfMRI_feat, "")
        N_tfMRI_featquery_2_dir  = tfMRI_featquery_2_dir.replace(tfMRI_feat, "")
        N_tfMRI_featquery_5_dir  = tfMRI_featquery_5_dir.replace(tfMRI_feat, "")
        N_tfMRI_featquery_5a_dir = tfMRI_featquery_5a_dir.replace(tfMRI_feat,"")


        wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", 
                           stats1=[N_tfMRI_cope1, N_tfMRI_zstat1], 
                           outputRootName=N_tfMRI_featquery_1_dir, 
                           mask=group_mask_1)
        wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", 
                           stats1=[N_tfMRI_cope2, N_tfMRI_zstat2], 
                           outputRootName=N_tfMRI_featquery_2_dir, 
                           mask=group_mask_2)
        wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", 
                           stats1=[N_tfMRI_cope5, N_tfMRI_zstat5], 
                           outputRootName=N_tfMRI_featquery_5_dir, 
                           mask=group_mask_5)
        wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", 
                           stats1=[N_tfMRI_cope5, N_tfMRI_zstat5], 
                           outputRootName=N_tfMRI_featquery_5a_dir, 
                           mask=group_mask_5a)

        result="" 
        
        for file_name in [tfMRI_featquery_1_report, tfMRI_featquery_2_report,
                          tfMRI_featquery_5_report, tfMRI_featquery_5a_report]:       
            with open(file_name, "r", encoding="utf-8") as f:
                report = f.readlines()

            a = round(float(report[0].split()[6]) / 100, 6)
            b = round(float(report[0].split()[7]) / 100, 6)
            c = round(float(report[-1].split()[6]), 6)
            d = round(float(report[-1].split()[7]), 6)

            result += " " + str(a) + " " + str(b) + " " + str(c) + " " + str(d)

        result = result.strip()

        with open(IDP_func_task_activation, 'wt', encoding="utf-8") as f:
            f.write(f'{result}\n')