Cole et al. (2021) fMRI data analysis

Analyses used in: Cole MW, Ito T, Cocuzza C, Sanchez-Romero R (In Press). "The functional relevance of task-state functional connectivity". Journal of Neuroscience. doi:10.1523/JNEUROSCI.1713-20.2021 https://doi.org/10.1523/JNEUROSCI.1713-20.2021

This makes heavy use of the Actflow Toolbox: https://colelab.github.io/ActflowToolbox/

Github repository for this code: https://github.com/ColeLab/TaskFCActflow_release

More information on the Cole Neurocognition Lab's work: https://www.colelab.org/

Purpose of this project: Test the advantages (and validity) of using task FC with activity flow mapping

In [1]:
#Load dependencies
import numpy as np
import nibabel as nib
import matplotlib.pyplot as plt
import seaborn as sns
import h5py
from scipy import stats
import statsmodels.api as sm
from sklearn.linear_model import LinearRegression
import sys
sys.path.insert(0, 'glmScripts/parcellated_postproc/')
import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp
import nuisanceRegressionPipeline_GlasserParcels as nrp
import os.path
import importlib
from functools import partial

import ActflowToolbox as actflow

#Useful for code development; reloads modules as files are changed
%load_ext autoreload
%autoreload 2

%matplotlib inline
plt.rcParams['image.interpolation'] = 'none'
plt.rcParams['figure.figsize'] = 6,4
plt.rcParams.update({'font.size': 14})
plt.rcParams['image.aspect'] = 'equal'
plt.rcParams['image.cmap'] = 'seismic'

#Set general variables
basedir='/projects/f_mc1689_1/TaskFCActflow/'
preprocdatadir=basedir+'/data/hcppreprocessedmsmall/parcellated_postproc/'
preprocdatadir_GLMs=basedir+'/data/hcppreprocessedmsmall/parcellated_postproc/'
preprocdatadir_vertexwise=basedir+'/data/hcppreprocessedmsmall/vertexWise/'
datadir=preprocdatadir
resultsdir=basedir+'data/results_final/'
figuredir=basedir+'/docs/figures/'

import multiprocessing as mp
numWorkers=mp.cpu_count()
print("Number of CPU cores available: "+str(numWorkers))
#numWorkers=int(round(numWorkers/2))
print("Number of CPU cores being used: "+str(numWorkers))
Number of CPU cores available: 20
Number of CPU cores being used: 20
In [2]:
print('Some version info:')
sm.show_versions()
Some version info:

INSTALLED VERSIONS
------------------
Python: 3.7.3.final.0
OS: Linux 3.10.0-1160.11.1.el7.x86_64 #1 SMP Fri Dec 18 16:34:56 UTC 2020 x86_64
byteorder: little
LC_ALL: None
LANG: None

statsmodels
===========

Installed: 0.11.1 (/home/mc1689/.local/lib/python3.7/site-packages/statsmodels)

Required Dependencies
=====================

cython: 0.29.6 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/Cython)
numpy: 1.17.0 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/numpy)
scipy: 1.2.1 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/scipy)
pandas: 0.24.2 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/pandas)
    dateutil: 2.8.0 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/dateutil)
patsy: 0.5.1 (/home/mc1689/.local/lib/python3.7/site-packages/patsy)

Optional Dependencies
=====================

matplotlib: 3.0.3 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/matplotlib)
    backend: module://ipykernel.pylab.backend_inline 
cvxopt: Not installed
joblib: 0.13.2 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/joblib)

Developer Tools
================

IPython: 7.5.0 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/IPython)
    jinja2: 2.10 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/jinja2)
sphinx: 1.8.5 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/sphinx)
    pygments: 2.3.1 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/pygments)
pytest: 4.3.1 (/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages)
virtualenv: Not installed


In [3]:
#Data: HCP352 subjects (selected subset of S1200 HCP release)

subjNums = ['100206','108020','117930','126325','133928','143224','153934','164636','174437',
            '183034','194443','204521','212823','268749','322224','385450','463040','529953',
            '587664','656253','731140','814548','877269','978578','100408','108222','118124',
            '126426','134021','144832','154229','164939','175338','185139','194645','204622',
            '213017','268850','329844','389357','467351','530635','588565','657659','737960',
            '816653','878877','987074','101006','110007','118225','127933','134324','146331',
            '154532','165638','175742','185341','195445','205119','213421','274542','341834',
            '393247','479762','545345','597869','664757','742549','820745','887373','989987',
            '102311','111009','118831','128632','135528','146432','154936','167036','176441',
            '186141','196144','205725','213522','285345','342129','394956','480141','552241',
            '598568','671855','744553','826454','896879','990366','102513','112516','118932',
            '129028','135629','146533','156031','167440','176845','187850','196346','205826',
            '214423','285446','348545','395756','481042','553344','599671','675661','749058',
            '832651','899885','991267','102614','112920','119126','129129','135932','147636',
            '157336','168745','177645','188145','198350','208226','214726','286347','349244',
            '406432','486759','555651','604537','679568','749361','835657','901442','992774',
            '103111','113316','120212','130013','136227','148133','157437','169545','178748',
            '188549','198451','208327','217429','290136','352738','414229','497865','559457',
            '615744','679770','753150','837560','907656','993675','103414','113619','120414',
            '130114','136833','150726','157942','171330']

### REPLICATION SUBJECTS
subjNums_replication = ['178950','189450','199453','209228','220721','298455','356948','419239','499566','561444','618952','680452','757764','841349','908860',
            '103818','113922','121618','130619','137229','151829','158035','171633','179346','190031','200008','210112','221319','299154','361234',
            '424939','500222','570243','622236','687163','769064','845458','911849','104416','114217','122317','130720','137532','151930','159744',
            '172029','180230','191235','200614','211316','228434','300618','361941','432332','513130','571144','623844','692964','773257','857263',
            '926862','105014','114419','122822','130821','137633','152427','160123','172938','180432','192035','200917','211417','239944','303119',
            '365343','436239','513736','579665','638049','702133','774663','865363','930449','106521','114823','123521','130922','137936','152831',
            '160729','173334','180533','192136','201111','211619','249947','305830','366042','436845','516742','580650','645450','715041','782561',
            '871762','942658','106824','117021','123925','131823','138332','153025','162026','173536','180735','192439','201414','211821','251833',
            '310621','371843','445543','519950','580751','647858','720337','800941','871964','955465','107018','117122','125222','132017','138837',
            '153227','162329','173637','180937','193239','201818','211922','257542','314225','378857','454140','523032','585862','654350','725751',
            '803240','872562','959574','107422','117324','125424','133827','142828','153631','164030','173940','182739','194140','202719','212015',
            '257845','316633','381543','459453','525541','586460','654754','727553','812746','873968','966975']


# Using JiEtAl2019 partition
networkdef_dir='/projects/f_mc1689_1/AnalysisTools/ColeAnticevicNetPartition/'
sys.path.insert(0, networkdef_dir)
networkdef = np.loadtxt('/projects/f_mc1689_1/AnalysisTools/ColeAnticevicNetPartition/cortex_parcel_network_assignments.txt')
#networkdef = np.loadtxt('/projects/AnalysisTools/ColeAnticevicNetPartition/cortex_parcel_network_assignments.txt')
networkorder = np.asarray(sorted(range(len(networkdef)), key=lambda k: networkdef[k]))
networkorder.shape = (len(networkorder),1)
netorder=networkorder[:,0]
# network mappings for final partition set
networkmappings = {'fpn':7, 'vis1':1, 'vis2':2, 'smn':3, 'aud':8, 'lan':6, 'dan':5, 'con':4, 'dmn':9, 
                   'pmulti':10, 'vmm':11, 'ora':12}
networks = networkmappings.keys()

xticks = {}
reorderednetworkaffil = networkdef[networkorder]
for net in networks:
    netNum = networkmappings[net]
    netind = np.where(reorderednetworkaffil==netNum)[0]
    tick = np.max(netind)
    xticks[tick] = net

## General parameters/variables
nParcels = 360
nSubjs = len(subjNums)
nTasks = 7
nConds = 24

sortednets = np.sort(list(xticks.keys()))
orderednetworks = []
for net in sortednets: orderednetworks.append(xticks[net])
    
networkpalette = ['royalblue','slateblue','paleturquoise','darkorchid','limegreen',
                  'lightseagreen','yellow','orchid','r','peru','orange','olivedrab']
networkpalette = np.asarray(networkpalette)

OrderedNetworks = ['VIS1','VIS2','SMN','CON','DAN','LAN','FPN','AUD','DMN','PMM','VMM','ORA']

tasks_condmappings = {'EMOTION':[0,1],
     'GAMBLING':[2,3],
     'LANGUAGE':[4,5],
     'MOTOR':[6,7,8,9,10,11],
     'RELATIONAL':[12,13],
     'SOCIAL':[14,15],
     'WM':[16,17,18,19,20,21,22,23]}
taskNames = ['EMOTION', 'GAMBLING', 'LANGUAGE', 'MOTOR', 'RELATIONAL', 'SOCIAL', 'WM']
TRsPerRun = [176,176,253,253,316,316,284,284,232,232,274,274,405,405]
restRuns = ['rfMRI_REST1_RL', 'rfMRI_REST1_LR','rfMRI_REST2_RL', 'rfMRI_REST2_LR']
taskNameList = ['tfMRI_EMOTION', 'tfMRI_GAMBLING','tfMRI_LANGUAGE','tfMRI_MOTOR','tfMRI_RELATIONAL','tfMRI_SOCIAL','tfMRI_WM']
taskRuns= ['tfMRI_EMOTION_RL','tfMRI_EMOTION_LR','tfMRI_GAMBLING_RL','tfMRI_GAMBLING_LR',
           'tfMRI_LANGUAGE_RL','tfMRI_LANGUAGE_LR','tfMRI_MOTOR_RL','tfMRI_MOTOR_LR',
           'tfMRI_RELATIONAL_RL','tfMRI_RELATIONAL_LR','tfMRI_SOCIAL_RL','tfMRI_SOCIAL_LR','tfMRI_WM_RL','tfMRI_WM_LR']
taskConditions = ['EMOTION:fear','EMOTION:neut','GAMBLING:win','GAMBLING:loss','LANGUAGE:story','LANGUAGE:math',
                  'MOTOR:cue','MOTOR:lf','MOTOR:rf','MOTOR:lh','MOTOR:rh','MOTOR:t','REASONING:rel',
                  'REASONING:match','SOCIAL:mental','SOCIAL:rnd','WM 0bk:body','WM 0bk:faces','WM 0bk:places',
                  'WM 0bk:tools','WM 2bk:body','WM 2bk:faces','WM 2bk:places','WM 2bk:tools']

#24 Task condition order:
# 1. EV1_EMOTION_fear
# 2. EV2_EMOTION_neut
# 3. EV3_GAMBLING_win
# 4. EV4_GAMBLING_loss
# 5. EV5_LANGUAGE_story
# 6. EV6_LANGUAGE_math
# 7. EV7_MOTOR_cue
# 8. EV8_MOTOR_lf
# 9. EV9_MOTOR_rf
# 10. EV10_MOTOR_lh
# 11. EV11_MOTOR_rh
# 12. EV12_MOTOR_t
# 13. EV13_RELATIONAL_relation
# 14. EV14_RELATIONAL_match
# 15. EV15_SOCIAL_mental
# 16. EV16_SOCIAL_rnd
# 17. EV17_WM_0bk_body
# 18. EV18_WM_0bk_faces
# 19. EV19_WM_0bk_places
# 20. EV20_WM_0bk_tools
# 21. EV21_WM_2bk_body
# 22. EV22_WM_2bk_faces
# 23. EV23_WM_2bk_places
# 24. EV24_WM_2bk_tools

Run preprocessing and GLMs

In [4]:
## Running data preprocessing (resting-state and task-state nuisance) and task canonical GLM

runRestTaskPreproc_and_TaskGLM=False
if runRestTaskPreproc_and_TaskGLM:
    ## Using SLURM cluster command (in parallel)
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import nuisanceRegressionPipeline_GlasserParcels as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        file_python.write("nrp.step2_nuisanceRegression(subj_nums=[str(" + str(subj) + ")],nproc=numWorkers, model='24pXaCompCorXVolterra',spikeReg=False,zscore=False)\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='canonical', byRun=False, zscore=False, verbose=True, nproc=numWorkers)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:2:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")
In [5]:
## Task FIR + canonical GLM for task-state FC

runTaskFCPreproc=False
if runTaskFCPreproc:
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import nuisanceRegressionPipeline_GlasserParcels as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='FIR_plus_canonical', byRun=False, zscore=False, verbose=True, nproc=numWorkers, nuisance_reg_simultaneous=False)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:5:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")
In [6]:
## Running data preprocessing variant: Canonical GLM each run separate

runTaskGLM_byrun=False
if runTaskGLM_byrun:
    ## Using SLURM cluster command (in parallel)
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import nuisanceRegressionPipeline_GlasserParcels as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        #file_python.write("nrp.step2_nuisanceRegression(subj_nums=[str(" + str(subj) + ")],nproc=numWorkers, model='24pXaCompCorXVolterra',spikeReg=False,zscore=False,just_regressors_for_taskGLM=False)\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='canonical', byRun=True, zscore=False, verbose=True, nproc=numWorkers, nuisance_reg_simultaneous=False)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:5:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")
In [7]:
## VERTEXWISE VERSION: Running data preprocessing (resting-state and task-state) and task canonical GLM

runRestTaskPreproc_and_TaskGLM_vertexwise=False
if runRestTaskPreproc_and_TaskGLM_vertexwise:
    ## Using SLURM cluster command (in parallel)
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/vertexwise_postproc/')\n")
        file_python.write("import taskGLMPipeline_VertexWise as tgp; import nuisanceRegressionPipeline_VertexWise as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        file_python.write("nrp.step2_nuisanceRegression(subj_nums=[str(" + str(subj) + ")],nproc=numWorkers, model='24pXaCompCorXVolterra',spikeReg=False,zscore=False)\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='canonical', byRun=False, zscore=False)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:3:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")
In [8]:
## VERTEXWISE VERSION: Task FIR + canonical GLM with nuisance regression for task-state FC

runTaskFCPreproc_vertexwise=False
if runTaskFCPreproc_vertexwise:
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/vertexwise_postproc/')\n")
        file_python.write("import taskGLMPipeline_VertexWise as tgp; import nuisanceRegressionPipeline_VertexWise as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='FIR_plus_canonical', byRun=False, zscore=False, verbose=True, nproc=numWorkers)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:2:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")
In [9]:
## Test: Task FIR (no canonical GLM) with nuisance regression for task-state FC

runTaskFCPreproc_FIRonly=False
if runTaskFCPreproc_FIRonly:
    job_sent_to_scheduler=False
    import os
    scount = 0
    for subj in subjNums:
        #print('Subject ' + subj)
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')

        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")

        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import nuisanceRegressionPipeline_GlasserParcels as nrp; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("import multiprocessing as mp; numWorkers=mp.cpu_count()\n")
        file_python.write("tgp.runSingleSubjTaskGLM(str("+str(subj)+"), nuisModel='24pXaCompCorXVolterra', taskModel='FIR', byRun=False, zscore=False, verbose=True, nproc=numWorkers)\n")

        file_python.close()

        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:3:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()

        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

        scount += 1

    if job_sent_to_scheduler:
        print("***WAIT FOR SLURM JOBS TO FINISH, THEN LOAD DATA***")

Define functions for loading preprocessed data

In [11]:
def loadRestResiduals(subj,model='24pXaCompCorXVolterra',zscore=False,FIR=False,vertexVoxelWise=False,restRuns=restRuns):
    """
    Loads in nuisance regressed HCP resting-state data

    Parameters:
        subj        :       single subject number as a string
        model       :       nuisance regression model (currently only acompcor version)   -   '24pXaCompCorXVolterra','24pXaCompCorXVolterra_zscore' (data zscored prior to running regression)
        zscore      :       (Default=False) - If False, returns data as-is. If True, return data where each run is z-normalized.
        FIR         :       (Default=False) - If True, applies the same task FIR regression on rest data (to directly compare rest and task)

    Returns: Concatenated region x time resting-state time series data for all 4 HCP runs

    """
    if vertexVoxelWise:
        vertexVoxelWiseStr='64k_'
        datadirectory=preprocdatadir_vertexwise
    else:
        vertexVoxelWiseStr=''
        datadirectory=datadir
        
    datafile = datadirectory + subj + '_glmPlusNuisRegOutput_' + vertexVoxelWiseStr + 'data.h5' 
    h5f = h5py.File(datafile,'r')
    data = []
    
    if FIR:
        dataid = 'rfMRI_REST_' + model + '_taskReg_resid_FIR'
        data = h5f['taskRegression'][dataid][:]
        if zscore:
            # Zscore each run separately
            runstart = 0
            for run in range(4):
                runend = runstart + 1195
                data[:,runstart:runend] = stats.zscore(data[:,runstart:runend],axis=1)
                runstart += 1195
                
            #            # Now z-score rest time series as if it were task
            #            trcount = 0
            #            for ntrs in TRsPerRun:
            #                trstart = trcount
            #                trend = trcount + ntrs
            #                data[:,trstart:trend] = stats.zscore(data[:,trstart:trend],axis=1)
            #
            #                trcount += ntrs

        data = data.T
    else:
        for run in restRuns:
            dataid = run + '/nuisanceReg_resid_' + model
            tmp = h5f[dataid][:]
            if zscore:
                tmp = stats.zscore(tmp,axis=1)
            data.extend(tmp.T)
    data = np.asarray(data).T
    h5f.close()
    return data


def loadTaskActivity(subj,model='24pXaCompCorXVolterra',zscore=False,regressors='byCondition',preprocdatadir_GLMs=preprocdatadir_GLMs, vertexVoxelWise=False):
    """
    Loads in nuisance regressed HCP resting-state data

    Parameters:
        subj        :       single subject number as a string
        model       :       nuisance regression model (currently only acompcor version)   -   '24pXaCompCorXVolterra','24pXaCompCorXVolterra_zscore' (data zscored prior to running regression)
        regressors  :       ['byCondition', 'byTask', 'byTaskByRun', 'byConditionByRun'] - regression for each task condition (24 task regressors) / by each task (7 task regressors) / by each task for each run separately

    Returns: 
        betas       :       Concatenated region x activations task condition beta coefficients  data for all 7 HCP tasks (24 conditions)
        taskkeys    :       key-value mappings indicating which tasks are associated with each column vector

    """
    if vertexVoxelWise:
        vertexVoxelWiseStr='64k_'
        datadirectory=preprocdatadir_vertexwise
    else:
        vertexVoxelWiseStr=''
        datadirectory=preprocdatadir_GLMs
    
    datafile = datadirectory + subj + '_glmPlusNuisRegOutput_' + vertexVoxelWiseStr + 'data.h5' 

    #print(datafile)
    h5f = h5py.File(datafile,'r')
    betas = []
    for task in taskNames:
        if regressors=='byCondition':
            dataid = 'tfMRI_' + task + '_' + model + '_taskReg_betas_canonical'
            if zscore:
                dataid = dataid +'_zscore'
            tmp = h5f['taskRegression'][dataid][:]
            betas.extend(tmp[:,1:].T)
        elif regressors=='byTask':
            dataid = 'tfMRI_' + task + '_' + model + '_taskReg_7TaskRegs_betas_canonical'
            tmp = h5f['taskRegression'][dataid][:]
            betas.extend(tmp[:,1:].T)
        elif regressors=='byTaskByRun':
            dataid_rl = 'tfMRI_' + task + '_RL_' + model + '_taskReg_7TaskRegs_betas_canonical'
            dataid_lr = 'tfMRI_' + task + '_LR_' + model + '_taskReg_7TaskRegs_betas_canonical'
            tmp_rl = h5f['taskRegression'][dataid_rl][:]
            tmp_lr = h5f['taskRegression'][dataid_lr][:]
            betas.extend(tmp_rl[:,1:].T)
            betas.extend(tmp_lr[:,1:].T)
        elif regressors=='byConditionByRun':
            dataid_rl = 'tfMRI_' + task + '_RL_' + model + '_taskReg_betas_canonical'
            dataid_lr = 'tfMRI_' + task + '_LR_' + model + '_taskReg_betas_canonical'
            if zscore:
                dataid_rl = dataid_rl +'_zscore'
                dataid_lr = dataid_lr +'_zscore'
            tmp_rl = h5f['taskRegression'][dataid_rl][:]
            tmp_lr = h5f['taskRegression'][dataid_lr][:]
            betas.extend(tmp_rl[:,1:].T)
            betas.extend(tmp_lr[:,1:].T)

    betas = np.asarray(betas).T
    h5f.close()

    if regressors=='byCondition':
        # task condition indices (identifies which columns are associated with each beta/condition)
        taskkeys = {'EMOTION':[0,1],
                    'GAMBLING':[2,3],
                    'LANGUAGE':[4,5],
                    'MOTOR':[6,7,8,9,10,11],
                    'RELATIONAL':[12,13],
                    'SOCIAL':[14,15],
                    'WM':[16,17,18,19,20,21,22,23]}
    elif regressors=='byTask':
        # Order of task regressors
        taskkeys = ['EMOTION', 'GAMBLING', 'LANGUAGE', 'MOTOR', 'RELATIONAL', 'SOCIAL', 'WM']
    elif regressors=='byTaskByRun':
        taskkeys = ['EMOTION_RL', 'EMOTION_LR', 'GAMBLING_RL', 'GAMBLING_LR', 'LANGUAGE_RL', 'LANGUAGE_LR',
                    'MOTOR_RL', 'MOTOR_LR', 'RELATIONAL_RL', 'RELATION_LR', 'SOCIAL_RL', 'SOCIAL_LR', 'WM_RL', 'WM_LR']
    elif regressors=='byConditionByRun':
        # task condition indices (identifies which columns are associated with each beta/condition)
        taskkeys = {'EMOTION_RL':[0,1],
                    'EMOTION_LR':[2,3],
                    'GAMBLING_RL':[4,5],
                    'GAMBLING_LR':[6,7],
                    'LANGUAGE_RL':[8,9],
                    'LANGUAGE_LR':[10,11],
                    'MOTOR_RL':[12,13,14,15,16,17],
                    'MOTOR_LR':[18,19,20,21,22,23],
                    'RELATIONAL_RL':[24,25],
                    'RELATIONAL_LR':[26,27],
                    'SOCIAL_RL':[28,29],
                    'SOCIAL_LR':[30,31],
                    'WM_RL':[32,33,34,35,36,37,38,39],
                    'WM_LR':[40,41,42,43,44,45,46,47]}

    return betas, taskkeys

def loadTaskResiduals(subj, model='24pXaCompCorXVolterra', taskModel='FIR', zscore=False, vertexVoxelWise=False):
    """
    Loads in nuisance regressed HCP task-state data, removing mean-evoked task condition responses using FIR modeling

    Parameters:
        subj        :       single subject number as a string
        model       :       nuisance regression model (currently only acompcor version)   -   '24pXaCompCorXVolterra','24pXaCompCorXVolterra_zscore' (data zscored prior to running regression)
        zscore      :       (Default=False) - If False, returns data as-is. If True, return data where each run is z-normalized.
        FIR         :       (Default=False) - If True, applies the same task FIR regression on rest data (to directly compare rest and task)

    Returns: Concatenated region x time resting-state time series data for all 4 HCP runs

    """
    
    if vertexVoxelWise:
        vertexVoxelWiseStr='64k_'
        datadirectory=preprocdatadir_vertexwise
    else:
        vertexVoxelWiseStr=''
        datadirectory=datadir
        
    datafile = datadirectory + subj + '_glmPlusNuisRegOutput_' + vertexVoxelWiseStr + 'data.h5' 

    h5f = h5py.File(datafile,'r')
    resids = []
    for task in taskNames:
        dataid = 'tfMRI_' + task + '_' + model + '_taskReg_resid_' + taskModel
        tmp = h5f['taskRegression'][dataid][:]
        if zscore:
            nTRsPerRun = int(tmp.shape[1]/2) # divide by 2 - fir regression is run on concatenated task scans, but we want to z-normalize each run separately
            tmp[:,:nTRsPerRun] = stats.zscore(tmp[:,:nTRsPerRun],axis=1)
            tmp[:,nTRsPerRun:] = stats.zscore(tmp[:,nTRsPerRun:],axis=1)
        resids.extend(tmp.T)
    resids = np.asarray(resids).T
    h5f.close()
    return resids

Loading task activation data

In [12]:
#Load task activity (GLM betas) data
activations_bycond=np.zeros((nParcels,nConds,nSubjs))
scount = 0
for subj  in subjNums:
    activations_bycond[:,:,scount] = loadTaskActivity(subj,model='24pXaCompCorXVolterra',regressors='byCondition',zscore=False)[0]
    scount += 1
In [14]:
#Visualize mean activity patterns by task
ax = sns.heatmap(np.mean(activations_bycond,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (24 conditions)')
Out[14]:
Text(0.5, 0.98, 'Actual activations (24 conditions)')
In [15]:
#Convert to 7-task data order by averaging 24-condition betas
activations_bytask=np.zeros((nParcels,nTasks,nSubjs))
for tasknum in range(nTasks):
    condnums_fortask=tasks_condmappings[taskNames[tasknum]]
    activations_bytask[:,tasknum,:] = np.mean(activations_bycond[:,condnums_fortask,:],axis=1)
In [16]:
#Visualize mean activity patterns by task

ax = sns.heatmap(np.mean(activations_bytask,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (7 tasks)')
Out[16]:
Text(0.5, 0.98, 'Actual activations (7 tasks)')
In [17]:
#Calculate similarities across tasks
corrmat=np.corrcoef(np.transpose(np.mean(activations_bytask[netorder,:,:],axis=2)))
#Set matrix diagonal to 0s
np.fill_diagonal(corrmat,0)
plt.imshow(corrmat,origin='lower')
plt.colorbar()
upperTIndices_corrmat=np.triu_indices(np.shape(corrmat)[0],k=1)
print('Mean similarities across tasks: r=' + str(np.mean(np.mean(corrmat[upperTIndices_corrmat]))))
Mean similarities across tasks: r=0.48747924521319713

Calculate repeat reliability (noise ceiling) of task activations

In [57]:
#Load task activity (GLM betas) data
activations_bycondbyrun=np.zeros((nParcels,nConds*2,nSubjs))
scount = 0
for subj  in subjNums:
    activations_bycondbyrun[:,:,scount] = loadTaskActivity(subj,model='24pXaCompCorXVolterra',regressors='byConditionByRun',zscore=False)[0]
    scount += 1
In [58]:
#Get condition index numbers by run
betas,taskkeys=loadTaskActivity(subjNums[0],model='24pXaCompCorXVolterra',regressors='byConditionByRun',zscore=False)
run1_cond_indices=np.concatenate([value for key, value in taskkeys.items() if '_RL' in key])
run2_cond_indices=np.concatenate([value for key, value in taskkeys.items() if '_LR' in key])
run1_cond_indices_mask=np.zeros(nConds*2)>1
run1_cond_indices_mask[run1_cond_indices]=True
run2_cond_indices_mask=np.zeros(nConds*2)>1
run2_cond_indices_mask[run2_cond_indices]=True

#Reorganize data to 2 node x condition x subject matrices
actvect_splitbyrun_group_run1s = activations_bycondbyrun[:,run1_cond_indices,:]
actvect_splitbyrun_group_run2s = activations_bycondbyrun[:,run2_cond_indices,:]
In [20]:
#Visualize mean activity patterns by task, run 1 data
print('Run 1 data')
ax = sns.heatmap(np.mean(actvect_splitbyrun_group_run1s,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (24 conditions)')
Run 1 data
Out[20]:
Text(0.5, 0.98, 'Actual activations (24 conditions)')
In [21]:
#Visualize mean activity patterns by task, run 2 data
print('Run 2 data')
ax = sns.heatmap(np.mean(actvect_splitbyrun_group_run2s,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (24 conditions)')
Run 2 data
Out[21]:
Text(0.5, 0.98, 'Actual activations (24 conditions)')
In [22]:
#Run repeat reliability (noise ceiling) analysis
print("==Repeat reliability across 24 task conditions")
noiseceiling_output=actflow.actflowcomp.noiseceilingcalc(actvect_splitbyrun_group_run1s, actvect_splitbyrun_group_run2s, full_report=True, print_report=True, avgthencomp_fixedeffects=True)
==Repeat reliability across 24 task conditions
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.52, t-value vs. 0: 65.65, p-value vs. 0: 3.363851818711326e-125
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.04
 
Mean MAE (mean absolute error) = 13.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.41, t-value vs. 0: 59.66, p-value vs. 0: 3.0701404216308764e-118
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.58
 
Mean MAE (mean absolute error) = 13.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Mean Pearson r=0.89
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=3.28
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.58, t-value vs. 0: 79.48, p-value vs. 0: 2.918730673163599e-139
By task condition:
Condition 1: r=0.27, t-value vs. 0: 16.55, p-value vs. 0: 1.2532971465334806e-37
Condition 2: r=0.21, t-value vs. 0: 12.26, p-value vs. 0: 2.419696178467991e-25
Condition 3: r=0.70, t-value vs. 0: 46.80, p-value vs. 0: 7.102597509713228e-101
Condition 4: r=0.70, t-value vs. 0: 47.09, p-value vs. 0: 2.5930509897408238e-101
Condition 5: r=0.41, t-value vs. 0: 34.71, p-value vs. 0: 2.154697363341862e-80
Condition 6: r=0.38, t-value vs. 0: 31.47, p-value vs. 0: 5.893004678912021e-74
Condition 7: r=0.62, t-value vs. 0: 46.81, p-value vs. 0: 6.879339427524525e-101
Condition 8: r=0.34, t-value vs. 0: 21.61, p-value vs. 0: 2.9096260228091686e-51
Condition 9: r=0.31, t-value vs. 0: 18.20, p-value vs. 0: 3.2336460062500375e-42
Condition 10: r=0.27, t-value vs. 0: 17.18, p-value vs. 0: 2.1197536857253173e-39
Condition 11: r=0.30, t-value vs. 0: 19.66, p-value vs. 0: 3.687987938103671e-46
Condition 12: r=0.30, t-value vs. 0: 19.56, p-value vs. 0: 6.466502567823137e-46
Condition 13: r=0.74, t-value vs. 0: 53.18, p-value vs. 0: 5.908481094092243e-110
Condition 14: r=0.72, t-value vs. 0: 49.44, p-value vs. 0: 9.249899065775062e-105
Condition 15: r=0.72, t-value vs. 0: 55.53, p-value vs. 0: 4.673266029067365e-113
Condition 16: r=0.70, t-value vs. 0: 48.36, p-value vs. 0: 3.47180102346113e-103
Condition 17: r=0.62, t-value vs. 0: 38.31, p-value vs. 0: 5.217828024776912e-87
Condition 18: r=0.58, t-value vs. 0: 36.85, p-value vs. 0: 2.1874362159517435e-84
Condition 19: r=0.74, t-value vs. 0: 52.42, p-value vs. 0: 6.322839177491341e-109
Condition 20: r=0.67, t-value vs. 0: 44.03, p-value vs. 0: 1.3353835762507402e-96
Condition 21: r=0.70, t-value vs. 0: 51.39, p-value vs. 0: 1.6719949693247025e-107
Condition 22: r=0.69, t-value vs. 0: 46.32, p-value vs. 0: 3.7645584263204094e-100
Condition 23: r=0.72, t-value vs. 0: 49.98, p-value vs. 0: 1.567076483648941e-105
Condition 24: r=0.75, t-value vs. 0: 57.05, p-value vs. 0: 5.270489766722505e-115
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean r=0.95
By task condition:
Condition 1: r=0.78
Condition 2: r=0.69
Condition 3: r=0.97
Condition 4: r=0.96
Condition 5: r=0.92
Condition 6: r=0.83
Condition 7: r=0.95
Condition 8: r=0.90
Condition 9: r=0.91
Condition 10: r=0.84
Condition 11: r=0.94
Condition 12: r=0.89
Condition 13: r=0.99
Condition 14: r=0.99
Condition 15: r=0.95
Condition 16: r=0.98
Condition 17: r=0.98
Condition 18: r=0.93
Condition 19: r=0.97
Condition 20: r=0.96
Condition 21: r=0.93
Condition 22: r=0.95
Condition 23: r=0.97
Condition 24: r=0.98
In [23]:
# from importlib import reload
# actflow.tools = reload(actflow.tools)

#Save condition-wise accuracy for each node to surface map
#taskcorrFC
datamat=np.mean(noiseceiling_output['corr_conditionwise_compthenavg_bynode'],axis=1)
datamat=datamat[:,np.newaxis]
outfilename='figures/NoiseCeiling_condwise_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)

#Max and min values
print('Maximum of map: ', np.max(datamat[datamat!=0]))
print('Minimum of map: ', np.min(datamat[datamat!=0]))
Command:
wb_command -cifti-convert -from-text figures/NoiseCeiling_condwise_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii figures/NoiseCeiling_condwise_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:figures/NoiseCeiling_condwise_discovery176.dscalar.nii
Maximum of map:  0.8094196524591349
Minimum of map:  0.003770126323336996
In [24]:
print('Plots of average-then-compare node-wise repeat reliability by condition:')
mat1=noiseceiling_output['corr_nodewise_avgthencomp_bycond']
print(mat1)
mat_mod = np.expand_dims(mat1, axis=0)
plt.imshow(mat_mod,aspect='auto',origin='lower')
plt.colorbar()
plt.figure()
plt.hist(mat1)
Plots of average-then-compare node-wise repeat reliability by condition:
[0.7800847472761331, 0.6919557557416793, 0.9727472384559246, 0.9609874718258535, 0.9155932220970873, 0.8315895969368915, 0.9511722375451304, 0.8980222412610352, 0.906957506179867, 0.8412120234134247, 0.9427129304605585, 0.8859011738616529, 0.988210923205291, 0.9887346124498112, 0.9548386397395022, 0.9805079010337925, 0.9774801372325627, 0.9273549291454624, 0.9675639463640276, 0.9553066819070588, 0.9302958859223042, 0.9519410162172812, 0.9698750299242442, 0.9812218272307669]
Out[24]:
(array([1., 0., 1., 0., 1., 1., 2., 3., 6., 9.]),
 array([0.69195576, 0.72163364, 0.75131153, 0.78098941, 0.8106673 ,
        0.84034518, 0.87002307, 0.89970096, 0.92937884, 0.95905673,
        0.98873461]),
 <a list of 10 Patch objects>)
In [25]:
print('Plots of compare-then-average node-wise repeat reliability by condition:')
mat1=np.mean(noiseceiling_output['corr_nodewise_compthenavg_bycond'],axis=1)
print(mat1)
mat_mod = np.expand_dims(mat1, axis=0)
plt.imshow(mat_mod,aspect='auto',origin='lower')
plt.colorbar()
plt.figure()
plt.hist(mat1)
Plots of compare-then-average node-wise repeat reliability by condition:
[0.25542581 0.19939568 0.6790289  0.67593907 0.39941032 0.36874858
 0.60315951 0.33077973 0.29488357 0.26310333 0.28854238 0.28840662
 0.72003803 0.69662168 0.70287375 0.68171921 0.60147654 0.5563762
 0.72250567 0.65022568 0.68638826 0.66576733 0.70360302 0.73764336]
Out[25]:
(array([1., 5., 1., 2., 0., 0., 1., 2., 5., 7.]),
 array([0.19939568, 0.25322044, 0.30704521, 0.36086998, 0.41469475,
        0.46851952, 0.52234429, 0.57616906, 0.62999383, 0.6838186 ,
        0.73764336]),
 <a list of 10 Patch objects>)
In [26]:
print('Plots of average-then-compare condition-wise repeat reliability by parcel:')
mat1=np.array(noiseceiling_output['corr_conditionwise_avgthencomp_bynode'])[netorder]
mat_mod = np.expand_dims(mat1, axis=0)
plt.imshow(mat_mod,aspect='auto',origin='lower')
plt.colorbar()
plt.figure()
plt.hist(mat1)
Plots of average-then-compare condition-wise repeat reliability by parcel:
Out[26]:
(array([  1.,   0.,   0.,   2.,   6.,  11.,  21.,  40., 137., 142.]),
 array([0.18459193, 0.26458555, 0.34457916, 0.42457277, 0.50456638,
        0.58455999, 0.6645536 , 0.74454722, 0.82454083, 0.90453444,
        0.98452805]),
 <a list of 10 Patch objects>)
In [27]:
print('Plots of condition-wise repeat reliability by parcel:')
mat1=np.mean(np.array(noiseceiling_output['corr_conditionwise_compthenavg_bynode'])[netorder,:],axis=1)
mat_mod = np.expand_dims(mat1, axis=0)
plt.imshow(mat_mod,aspect='auto',origin='lower')
plt.colorbar()
plt.figure()
plt.hist(mat1)
#plt.figure()
#plt.hist(np.mean(noiseceiling_output['repeat_corr_conditionwise_compthenavg_bynode'][netorder,:],axis=0))
Plots of condition-wise repeat reliability by parcel:
Out[27]:
(array([10., 24., 50., 79., 69., 54., 34., 17., 11., 12.]),
 array([0.00377013, 0.08433508, 0.16490003, 0.24546498, 0.32602994,
        0.40659489, 0.48715984, 0.56772479, 0.64828975, 0.7288547 ,
        0.80941965]),
 <a list of 10 Patch objects>)

Correlation-based resting-state FC

Calculating correlation-based resting-state FC

In [136]:
#Load rest residuals (post-preprocessing) data in and run correlation for each subject
restFC_corr_bysubj=np.zeros((nParcels,nParcels,nSubjs))
scount = 0
for subj  in subjNums:
    restdata = loadRestResiduals(subj,model='24pXaCompCorXVolterra',zscore=False,FIR=False)
    restFC_corr_bysubj[:,:,scount] = actflow.connectivity_estimation.corrcoefconn(restdata)
    scount += 1
In [29]:
# plt.imshow(np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'})
ax.figure.suptitle('Resting-state correlation FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'restFC_discovery176.pdf')

Activity flow mapping with correlation-based resting-state FC

In [30]:
#Run actflow predictions
print("==Activity flow mapping results, correlation-based resting-state FC, 7 tasks==")
actflowOutput_restFCCorr = actflow.actflowcomp.actflowtest(activations_bytask, restFC_corr_bysubj)
==Activity flow mapping results, correlation-based resting-state FC, 7 tasks==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 7 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.60, t-value vs. 0: 84.21, p-value vs. 0: 1.5380140644250529e-143
 
Mean % variance explained (R^2 score, coeff. of determination) = -877.11
 
Mean MAE (mean absolute error) = 307.31
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [137]:
#Run actflow predictions, by condition
print("==Activity flow mapping results, correlation-based resting-state FC, 24 conditions==")
actflowOutput_restFCCorr_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_corr_bysubj, avgthencomp_fixedeffects=True)
==Activity flow mapping results, correlation-based resting-state FC, 24 conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.59, t-value vs. 0: 90.76, p-value vs. 0: 4.1935660731125815e-149
 
Mean % variance explained (R^2 score, coeff. of determination) = -838.07
 
Mean MAE (mean absolute error) = 332.44
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Mean Pearson r=0.62
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=244.36
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean r=0.70
By task condition:
Condition 1: r=0.71
Condition 2: r=0.70
Condition 3: r=0.77
Condition 4: r=0.78
Condition 5: r=0.40
Condition 6: r=0.34
Condition 7: r=0.79
Condition 8: r=0.72
Condition 9: r=0.69
Condition 10: r=0.58
Condition 11: r=0.55
Condition 12: r=0.70
Condition 13: r=0.79
Condition 14: r=0.81
Condition 15: r=0.76
Condition 16: r=0.80
Condition 17: r=0.71
Condition 18: r=0.65
Condition 19: r=0.69
Condition 20: r=0.72
Condition 21: r=0.76
Condition 22: r=0.71
Condition 23: r=0.67
Condition 24: r=0.73
In [32]:
#Run actflow predictions, by condition, split-half (noise ceiling)
print("==Activity flow mapping results, correlation-based resting-state FC, 24 conditions, split-half==")
print("==Comparing actflow predictions of Run 2 (based on Run 1 data) vs. actual Run 1 activations (noise ceiling/repeat reliability)==")
actflowOutput_restFCCorr_bycond_splithalf = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, restFC_corr_bysubj, actVect_group_test=actvect_splitbyrun_group_run2s)
==Activity flow mapping results, correlation-based resting-state FC, 24 conditions, split-half==
==Comparing actflow predictions of Run 2 (based on Run 1 data) vs. actual Run 1 activations (noise ceiling/repeat reliability)==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.31, t-value vs. 0: 35.69, p-value vs. 0: 3.100340549076402e-82
 
Mean % variance explained (R^2 score, coeff. of determination) = -867.49
 
Mean MAE (mean absolute error) = 384.86
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.

Activity flow mapping with LOCALLY-NON-CIRCULAR correlation-based resting-state FC

In [33]:
## Calculating with SLURM cluster command (in parallel), loading from data if saved
#Calculate locally-non-circular activations (excluding vertices near to-be-predicted ROIs)

loaddata_ifavailable=True
datasuffix='_taskbetas24cond_noncirc_data'

activations_noncirc_bycond=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    outname1 = 'taskbeta'
    
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        activations_noncirc_bycond[:,:,:,scount] = h5f[outname1][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/')\n")
        file_python.write("import loadTaskActivity_byscript as loadt; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("activitydata = loadt.loadTaskActivity('" + str(subj) + "',model='24pXaCompCorXVolterra',regressors='byCondition',zscore=False,preprocdatadir_GLMs='" + preprocdatadir_vertexwise + "',vertexVoxelWise=True)\n")
        file_python.write("activations_noncirc_bycond = actflow.connectivity_estimation.calcactivity_parcelwise_noncircular(activitydata, dilated_parcels=True, verbose=False)\n")

        file_python.write("#Save data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'taskbeta'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=activations_noncirc_bycond)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=activations_noncirc_bycond)\n")
        file_python.write("h5f.close()")
        file_python.close()                
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:01:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [34]:
#Convert to 7-task data
activations_noncirc_bytask=np.zeros((nParcels,nParcels,nTasks,nSubjs))
for tasknum in range(nTasks):
    condnums_fortask=tasks_condmappings[taskNames[tasknum]]
    activations_noncirc_bytask[:,:,tasknum,:] = np.mean(activations_noncirc_bycond[:,:,condnums_fortask,:],axis=2)
In [35]:
#Pull out vertex-averaged GLM activations (unmodified by local-non-circularity calculations)

activations_vertexavg_bycond=np.zeros((nParcels,nConds,nSubjs))
# extract the diagonal of the 360 x 360 matrix for each condition.
# The diagonal corresponds to the original activation for the target regions
for region_num in range(nParcels):
    for cond_num in range(nConds):
        for subj_num in range(nSubjs):
            activations_vertexavg_bycond[region_num,cond_num,subj_num] = activations_noncirc_bycond[region_num,region_num,cond_num,subj_num]

            
flip_hemisphere_indices=np.concatenate([np.arange(180,360), np.arange(0,180)])

#Visualize mean activity patterns by task
ax = sns.heatmap(np.mean(activations_vertexavg_bycond,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (24 conditions), based on vertex avg')

plt.figure()
ax = sns.heatmap(np.mean(activations_bycond,axis=2)[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
ax.figure.suptitle('Actual activations (24 conditions), parcellated data')

#Save to dscalar file
# ActflowToolbox.tools.map_to_surface = importlib.reload(ActflowToolbox.tools.map_to_surface)
# #import ActflowToolbox.tools.map_to_surface
# ActflowToolbox.tools.map_to_surface.map_to_surface(activations_vertexavg_bycond_avg,outputdir+'activations_vertexavg_bycond_avg')

# inputdata_flipped=np.zeros(np.shape(activations_vertexavg_bycond))
# inputdata_flipped[0:180,:,:]=activations_vertexavg_bycond[180:360,:,:]
# inputdata_flipped[180:360,:,:]=activations_vertexavg_bycond[0:180,:,:]
# np.corrcoef(inputdata_flipped.flatten(),activations_bycond.flatten())

np.corrcoef(activations_vertexavg_bycond.flatten(),activations_bycond.flatten())
Out[35]:
array([[1., 1.],
       [1., 1.]])
In [36]:
#Test: Calculate activations from vertices (GLM first, then average)

dlabelfile_forparcels='/projects/f_mc1689_1/AnalysisTools/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii'
#dlabelfile_forparcels='/projects/f_mc1689_1/AnalysisTools/ActflowToolbox/tools/Q1-Q6_RelatedParcellation210.LR.CorticalAreas_dil_Colors.32k_fs_RL.dlabel.nii'
#dlabelfile_forparcels='/projects/f_mc1689_1/TaskFCActflow/docs/scripts/final_analyses/ActflowToolbox/tools/Q1-Q6_RelatedValidation210.CorticalAreas_dil_Final_Final_Areas_Group_Colors.32k_fs_LR.dlabel.nii'
dlabels = np.squeeze(nib.load(dlabelfile_forparcels).get_data())

activations_vertexmean=np.zeros((nParcels,nConds,nSubjs))
scount = 0
for subj in subjNums:
    #print(subj + ', ' + str(scount))
    activitydata = loadTaskActivity(subj,model='24pXaCompCorXVolterra',regressors='byCondition',zscore=False,preprocdatadir_GLMs=preprocdatadir_vertexwise,vertexVoxelWise=True)[0]
    
    for parcel_num in range(360):
        # get all target ROI indices
        target_ind = np.where(dlabels==(parcel_num+1))[0] # Find target parcel indices (from dlabel file)
        activations_vertexmean[parcel_num,:,scount] = np.nanmean(np.real(activitydata[target_ind,:]),axis=0)
    
    scount += 1

#activations_vertexmean_avg = np.mean(activations_vertexmean[flip_hemisphere_indices,:,:],axis=2)
activations_vertexmean_avg = np.mean(activations_vertexmean,axis=2)

#plt.imshow(activations_vertexmean_avg[netorder,:],aspect='auto')
ax = sns.heatmap(activations_vertexmean_avg[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
#plt.colorbar()
plt.figure()


origActivationsMean=np.mean(activations_bycond,axis=2)
ax = sns.heatmap(origActivationsMean[netorder,:],center=0,cmap='seismic',cbar=True,yticklabels=50,xticklabels=3)
#plt.imshow(origActivationsMean[netorder,:],aspect='auto')
#plt.colorbar()

#Compare to noncircular code derived version
numValues=np.product(np.shape(activations_vertexmean_avg))

np.corrcoef(np.reshape(activations_vertexmean_avg,numValues),np.reshape(origActivationsMean,numValues))
/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/ipykernel_launcher.py:6: DeprecationWarning: get_data() is deprecated in favor of get_fdata(), which has a more predictable return type. To obtain get_data() behavior going forward, use numpy.asanyarray(img.dataobj).

* deprecated from version: 3.0
* Will raise <class 'nibabel.deprecator.ExpiredDeprecationError'> as of version: 5.0
  
Out[36]:
array([[1.        , 0.99876794],
       [0.99876794, 1.        ]])
In [37]:
#Plotting images in-line, motor:lh

from wbplot import pscalar
import matplotlib.image as mpimg

condlabel='motor:lh'
condnum=9

#Actual
inputdata=np.mean(activations_vertexavg_bycond[:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('Actual '+condlabel)
plt.imshow(img)
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Out[37]:
<matplotlib.image.AxesImage at 0x7faf2442dd30>
In [38]:
## Calculating with SLURM cluster command (in parallel), loading from data if saved
#Load rest residuals (post-preprocessing) data in and run correlation for each subject, run non-circular code

loaddata_ifavailable=True
datasuffix='_corrRestFC_noncirc_data'

restFC_noncirc_corr_bysubj=np.zeros((nParcels,nParcels,nSubjs))
outname1 = 'restFCcorr'
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        restFC_noncirc_corr_bysubj[:,:,scount] = h5f[outname1][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/')\n")
        file_python.write("import loadRestResiduals_byscript; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,FIR=False,vertexVoxelWise=True, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "', restRuns=['rfMRI_REST1_RL', 'rfMRI_REST1_LR','rfMRI_REST2_RL', 'rfMRI_REST2_LR'])\n")
        file_python.write("restFC_noncirc_corr_bysubj=actflow.connectivity_estimation.calcconn_parcelwise_noncircular(restdata,connmethod='pearsoncorr',dilated_parcels=True, verbose=False)\n")
        file_python.write("#Save data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'restFCcorr'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_noncirc_corr_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_noncirc_corr_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:03:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [39]:
# plt.imshow(np.mean(restFC_noncirc_corr_bysubj[netorder,:,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_noncirc_corr_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'})
ax.figure.suptitle('Resting-state correlation FC, noncirc')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[39]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [40]:
orig_restFC_mean=np.mean(restFC_corr_bysubj,axis=2)
# plt.imshow(orig_restFC_mean[netorder,:][:,netorder],origin='lower')
# plt.colorbar()
# plt.title('Original (potentially local circular) rest FC')
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'})
ax.figure.suptitle('Original (potentially local circular) rest FC')
ax.set(xlabel='Nodes',ylabel='Nodes')

#Compare to noncircular code derived version
noncirc_restFC_mean=np.mean(restFC_noncirc_corr_bysubj,axis=2)
numValues=np.product(np.shape(orig_restFC_mean))
np.corrcoef(np.reshape(noncirc_restFC_mean,numValues),np.reshape(orig_restFC_mean,numValues))
Out[40]:
array([[1.        , 0.97111732],
       [0.97111732, 1.        ]])
In [41]:
#Run actflow predictions
print("==Activity flow mapping results, NON-CIRCULAR, correlation-based resting-state FC, 24 task conditions==")
actflowOutput_noncirc_restFCCorr = actflow.actflowcomp.actflowtest(activations_noncirc_bycond, restFC_noncirc_corr_bysubj, separate_activations_bytarget=True)
==Activity flow mapping results, NON-CIRCULAR, correlation-based resting-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.57, t-value vs. 0: 87.46, p-value vs. 0: 2.348150306642009e-146
 
Mean % variance explained (R^2 score, coeff. of determination) = -805.57
 
Mean MAE (mean absolute error) = 325.62
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.

Multiple-regression-based resting-state FC

Activity flow mapping with multiple-regression-based resting-state FC

In [13]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregRestFC_data'

restFC_mreg_bysubj=np.zeros((nParcels,nParcels,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        restFC_mreg_bysubj[:,:,scount] = h5f['restFCRegression'][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/')\n")
        file_python.write("import loadRestResiduals_byscript; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,FIR=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "', restRuns=['rfMRI_REST1_RL', 'rfMRI_REST1_LR','rfMRI_REST2_RL', 'rfMRI_REST2_LR'])\n")
        file_python.write("restFC_mreg_bysubj=actflow.connectivity_estimation.multregconn(restdata)\n")
        file_python.write("#Save multiple-regression task FC data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'restFCRegression'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_mreg_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_mreg_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:05:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [14]:
# plt.imshow(np.mean(restFC_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#plt.savefig('matrix.png',dpi=600,transparent=True, bbox_inches='tight')
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[14]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [15]:
#Run actflow predictions
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions==")
#actflowOutput_restFCMReg_bytask = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bysubj, avgthencomp_fixedeffects=True)
actflowOutput_restFCMReg_bytask = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bysubj, avgthencomp_fixedeffects=False)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.89, t-value vs. 0: 165.91, p-value vs. 0: 2.1294740292018985e-194
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.78
 
Mean MAE (mean absolute error) = 5.38
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [138]:
print("===Compare Rest multregFC actflow predictions to Rest corr (all time points) actflow prediction===")
model_compare_RestMultRegFCVsRestCorrActflow = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_bytask['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_bytask['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCCorr_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Rest multregFC actflow predictions to Rest corr (all time points) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.89
Model2 mean Pearson r=0.59
R-value difference = 0.30
Model1 vs. Model2 T-value: 92.01, p-value: 3.977144562351872e-150
 
Model1 mean % predicted variance explained R^2=0.78
Model2 mean % predicted variance explained R^2=-838.07
R^2 difference = 838.86
 
Model1 mean MAE = 5.38
Model2 mean MAE = 332.44
Model1 vs. Model2 mean MAE difference = -327.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.89
Model2 mean Pearson r=0.63
R-value difference = 0.26
Model1 vs. Model2 T-value: 89.09, p-value: 1.007401495471616e-147
 
Model1 mean % predicted variance explained R^2=0.56
Model2 mean % predicted variance explained R^2=-2066.10
R^2 difference = 2066.67
 
Model1 mean MAE = 5.38
Model2 mean MAE = 332.44
Model1 vs. Model2 mean MAE difference = -327.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.87
Model2 mean Pearson r=0.58
R-value difference = 0.28
Model1 vs. Model2 T-value: 109.67, p-value: 3.039855359861395e-163
By task condition:
Condition 1: Model 1 r=0.78, Model 2 r=0.54, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 42.38, p-value vs. 0: 6.082425632786399e-94
Condition 2: Model 1 r=0.75, Model 2 r=0.50, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 41.56, p-value vs. 0: 1.3763563253097527e-92
Condition 3: Model 1 r=0.90, Model 2 r=0.66, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 78.85, p-value vs. 0: 1.1373811284393655e-138
Condition 4: Model 1 r=0.90, Model 2 r=0.66, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 70.92, p-value vs. 0: 7.402796531405334e-131
Condition 5: Model 1 r=0.82, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.38, t-value Model1 vs. Model2: 41.32, p-value vs. 0: 3.355469424106882e-92
Condition 6: Model 1 r=0.81, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.40, t-value Model1 vs. Model2: 43.21, p-value vs. 0: 2.738131884674741e-95
Condition 7: Model 1 r=0.88, Model 2 r=0.64, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 65.95, p-value vs. 0: 1.5554242381809965e-125
Condition 8: Model 1 r=0.78, Model 2 r=0.49, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 39.82, p-value vs. 0: 1.188730803957676e-89
Condition 9: Model 1 r=0.78, Model 2 r=0.48, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 42.34, p-value vs. 0: 6.882608797728978e-94
Condition 10: Model 1 r=0.78, Model 2 r=0.49, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 45.41, p-value vs. 0: 9.256940416332246e-99
Condition 11: Model 1 r=0.78, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 41.28, p-value vs. 0: 3.9137877068715734e-92
Condition 12: Model 1 r=0.78, Model 2 r=0.49, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 42.33, p-value vs. 0: 7.249000032186875e-94
Condition 13: Model 1 r=0.91, Model 2 r=0.68, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 64.39, p-value vs. 0: 8.671648454283713e-124
Condition 14: Model 1 r=0.91, Model 2 r=0.69, Model 1 vs. 2 R-value difference =0.21, t-value Model1 vs. Model2: 58.02, p-value vs. 0: 3.175244240397878e-116
Condition 15: Model 1 r=0.92, Model 2 r=0.67, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 78.40, p-value vs. 0: 2.9968540830571397e-138
Condition 16: Model 1 r=0.91, Model 2 r=0.69, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 68.95, p-value vs. 0: 8.725653837894133e-129
Condition 17: Model 1 r=0.89, Model 2 r=0.61, Model 1 vs. 2 R-value difference =0.28, t-value Model1 vs. Model2: 67.12, p-value vs. 0: 8.002016956305683e-127
Condition 18: Model 1 r=0.86, Model 2 r=0.56, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 66.45, p-value vs. 0: 4.4175735549854914e-126
Condition 19: Model 1 r=0.91, Model 2 r=0.62, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 69.11, p-value vs. 0: 5.873012634805614e-129
Condition 20: Model 1 r=0.90, Model 2 r=0.63, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 66.76, p-value vs. 0: 1.990491556075811e-126
Condition 21: Model 1 r=0.91, Model 2 r=0.65, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 73.84, p-value vs. 0: 7.919180748832413e-134
Condition 22: Model 1 r=0.88, Model 2 r=0.58, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 68.94, p-value vs. 0: 8.845273264671682e-129
Condition 23: Model 1 r=0.90, Model 2 r=0.58, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 70.47, p-value vs. 0: 2.1719928577811755e-130
Condition 24: Model 1 r=0.91, Model 2 r=0.63, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 74.18, p-value vs. 0: 3.660302627896697e-134
In [139]:
#Values for effect size calculation; nodewise average across conditions; using https://memory.psych.mun.ca/models/stats/effect_size.shtml
std_mreg=np.std(np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond'],axis=0))
print("std_mreg=",std_mreg)
std_corr=np.std(np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond_model2'],axis=0))
print("std_corr=",std_corr)
rval_mreg_corr=np.corrcoef(np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond'],axis=0), np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond_model2'],axis=0))[0,1]
print("rval_mreg_corr=",rval_mreg_corr)
std_diffs=np.std(np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond'],axis=0)-np.mean(model_compare_RestMultRegFCVsRestCorrActflow['corr_nodewise_compthenavg_bycond_model2'],axis=0))
print('std_diffs=',std_diffs)
std_mreg= 0.031587204157440416
std_corr= 0.06778533362389111
rval_mreg_corr= 0.7286866431215429
std_diffs= 0.04972071314620424
In [140]:
#Visualize predicted and actual activations
data1=stats.zscore(np.mean(actflowOutput_restFCMReg_bytask['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
data2=stats.zscore(np.mean(activations_bycond,axis=2)[netorder,:],axis=None)
#data3=stats.zscore(np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
datamin=min(np.array([min(data1.flatten()),min(data2.flatten())]))
datamax=max(np.array([max(data1.flatten()),max(data2.flatten())]))
fig, axn = plt.subplots(1, 2, sharex=True, sharey=True, figsize=[10,5])
cbar_ax = fig.add_axes([.90, .3, .015, .5])
ax1 = sns.heatmap(data1,ax=axn[0],center=0,cmap='seismic',cbar=False,yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax1.title.set_text('Pred: Rest multreg FC')
ax2 = sns.heatmap(data2,ax=axn[1],center=0,cmap='seismic',cbar=True,cbar_ax=cbar_ax,cbar_kws={'label': 'Activation (z-score)'},yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax2.title.set_text('Actual activations')
#ax3 = sns.heatmap(data3,ax=axn[2],center=0,cmap='seismic',cbar=True,cbar_ax=cbar_ax,cbar_kws={'label': 'Activation (z-score)'},yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
#ax3.title.set_text('Pred: Task multreg FC')
ax1.set_xticklabels(ax1.get_xmajorticklabels(), fontsize = 12)
ax2.set_xticklabels(ax2.get_xmajorticklabels(), fontsize = 12)
#ax3.set_xticklabels(ax3.get_xmajorticklabels(), fontsize = 12)
fig.tight_layout(rect=[0, 0, .9, 1])
# fig=ax.get_figure()
fig.savefig(figuredir+'restActflowPred_MultregFCAllData_discovery176.pdf')
/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py:2369: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "

Activity flow mapping with LOCALLY-NON-CIRCULAR multiple-regression-based resting-state FC

In [48]:
## Calculating NON-CIRCULAR multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregRestFC_noncirc_data'

restFC_noncirc_mreg_bysubj=np.zeros((nParcels,nParcels,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        restFC_noncirc_mreg_bysubj[:,:,scount] = h5f['restFCRegression'][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/')\n")
        file_python.write("import loadRestResiduals_byscript; import h5py; import ActflowToolbox as actflow\n")
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,FIR=False,vertexVoxelWise=True, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "', restRuns=['rfMRI_REST1_RL', 'rfMRI_REST1_LR','rfMRI_REST2_RL', 'rfMRI_REST2_LR'])\n")
        file_python.write("restFC_noncirc_mreg_bysubj=actflow.connectivity_estimation.calcconn_parcelwise_noncircular_surface(restdata,dilated_parcels=True,verbose=True)\n")
        file_python.write("#Save multiple-regression task FC data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'restFCRegression'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_noncirc_mreg_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_noncirc_mreg_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:03:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [49]:
# plt.imshow(np.mean(restFC_noncirc_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#plt.savefig('matrix.png',dpi=600,transparent=True, bbox_inches='tight')
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_noncirc_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC, noncirc')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[49]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [50]:
#Run actflow predictions
print("==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based resting-state FC, 24 task conditions==")
actflowOutput_restFCMReg_noncirc_bycond = actflow.actflowcomp.actflowtest(activations_noncirc_bycond, restFC_noncirc_mreg_bysubj, separate_activations_bytarget=True)
==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based resting-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.87, t-value vs. 0: 152.16, p-value vs. 0: 7.217917110376571e-188
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.74
 
Mean MAE (mean absolute error) = 5.87
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [51]:
print("===Compare multregFC actflow predictions to corrFC actflow prediction, NON-CIRCULAR===")
model_compare_multRegVsCorrFCActflow = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_noncirc_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_noncirc_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_noncirc_restFCCorr['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare multregFC actflow predictions to corrFC actflow prediction, NON-CIRCULAR===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.87
Model2 mean Pearson r=0.57
R-value difference = 0.30
Model1 vs. Model2 T-value: 83.66, p-value: 4.706611779022778e-143
 
Model1 mean % predicted variance explained R^2=0.74
Model2 mean % predicted variance explained R^2=-805.57
R^2 difference = 806.31
 
Model1 mean MAE = 5.87
Model2 mean MAE = 325.62
Model1 vs. Model2 mean MAE difference = -319.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.87
Model2 mean Pearson r=0.61
R-value difference = 0.25
Model1 vs. Model2 T-value: 79.22, p-value: 5.1369079192521886e-139
 
Model1 mean % predicted variance explained R^2=0.49
Model2 mean % predicted variance explained R^2=-2013.55
R^2 difference = 2014.04
 
Model1 mean MAE = 5.87
Model2 mean MAE = 325.62
Model1 vs. Model2 mean MAE difference = -319.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.84
Model2 mean Pearson r=0.55
R-value difference = 0.29
Model1 vs. Model2 T-value: 99.53, p-value: 5.538215053271558e-156
By task condition:
Condition 1: Model 1 r=0.74, Model 2 r=0.50, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 41.02, p-value vs. 0: 1.083458261561869e-91
Condition 2: Model 1 r=0.72, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 39.52, p-value vs. 0: 3.8217607471869667e-89
Condition 3: Model 1 r=0.88, Model 2 r=0.63, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 74.35, p-value vs. 0: 2.454137858667424e-134
Condition 4: Model 1 r=0.88, Model 2 r=0.63, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 67.50, p-value vs. 0: 3.0984190324567573e-127
Condition 5: Model 1 r=0.79, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.39, t-value Model1 vs. Model2: 40.31, p-value vs. 0: 1.740157141629463e-90
Condition 6: Model 1 r=0.79, Model 2 r=0.38, Model 1 vs. 2 R-value difference =0.41, t-value Model1 vs. Model2: 42.13, p-value vs. 0: 1.5333779919437804e-93
Condition 7: Model 1 r=0.85, Model 2 r=0.62, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 58.95, p-value vs. 0: 2.2878047553295664e-117
Condition 8: Model 1 r=0.74, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 36.31, p-value vs. 0: 2.1915006458076593e-83
Condition 9: Model 1 r=0.74, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 38.69, p-value vs. 0: 1.1115315421672697e-87
Condition 10: Model 1 r=0.74, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 43.33, p-value vs. 0: 1.7135645761630808e-95
Condition 11: Model 1 r=0.74, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 37.83, p-value vs. 0: 3.70777608657449e-86
Condition 12: Model 1 r=0.75, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 41.45, p-value vs. 0: 2.0345642752209996e-92
Condition 13: Model 1 r=0.89, Model 2 r=0.65, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 61.10, p-value vs. 0: 5.7513292112753206e-120
Condition 14: Model 1 r=0.89, Model 2 r=0.68, Model 1 vs. 2 R-value difference =0.21, t-value Model1 vs. Model2: 54.11, p-value vs. 0: 3.4140337794385606e-111
Condition 15: Model 1 r=0.90, Model 2 r=0.64, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 73.28, p-value vs. 0: 2.889241135439009e-133
Condition 16: Model 1 r=0.89, Model 2 r=0.67, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 62.09, p-value vs. 0: 3.900695842202814e-121
Condition 17: Model 1 r=0.86, Model 2 r=0.57, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 63.20, p-value vs. 0: 2.008025359766707e-122
Condition 18: Model 1 r=0.83, Model 2 r=0.52, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 61.98, p-value vs. 0: 5.2684210805711045e-121
Condition 19: Model 1 r=0.88, Model 2 r=0.58, Model 1 vs. 2 R-value difference =0.31, t-value Model1 vs. Model2: 63.53, p-value vs. 0: 8.321008525410184e-123
Condition 20: Model 1 r=0.88, Model 2 r=0.59, Model 1 vs. 2 R-value difference =0.28, t-value Model1 vs. Model2: 63.10, p-value vs. 0: 2.603401881242966e-122
Condition 21: Model 1 r=0.89, Model 2 r=0.62, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 69.03, p-value vs. 0: 7.090356754651093e-129
Condition 22: Model 1 r=0.85, Model 2 r=0.54, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 64.76, p-value vs. 0: 3.358123267059825e-124
Condition 23: Model 1 r=0.87, Model 2 r=0.54, Model 1 vs. 2 R-value difference =0.33, t-value Model1 vs. Model2: 62.25, p-value vs. 0: 2.500112543848733e-121
Condition 24: Model 1 r=0.88, Model 2 r=0.59, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 67.96, p-value vs. 0: 9.900357954900533e-128

Correlation-based task-state FC

In [52]:
%%time
#Load task residuals (post-preprocessing) data in and run correlation for each subject

loaddata_ifavailable=True
datasuffix='_taskFC_corr_data'

taskFC_corr_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    outname1 = 'taskFCcorr'

    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        taskFC_corr_bycond_bysubj[:,:,:,scount] = h5f[outname1][:]
        h5f.close()
    else:
        #Compute FCs
        taskdata = loadTaskResiduals(subj,model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False)
        tasktiming_bycond=tgp.loadTaskTimingForAllTasks(subj,taskModel='canonical')['taskRegressors']>.5
        for condnum in range(24):
            tasktiming=tasktiming_bycond[:,condnum]
            taskFC_corr_bycond_bysubj[:,:,condnum,scount] = np.corrcoef(taskdata[:,tasktiming],rowvar=1)
            #Set FC matrix diagonal to 0s
            np.fill_diagonal(taskFC_corr_bycond_bysubj[:,:,condnum,scount],0)
        
        #Save multiple-regression task FC data to file
        h5f = h5py.File(file_path,'a')
        try:
            h5f.create_dataset(outname1,data=taskFC_corr_bycond_bysubj[:,:,:,scount])
        except:
            del h5f[outname1]
            h5f.create_dataset(outname1,data=taskFC_corr_bycond_bysubj[:,:,:,scount])
        h5f.close()
        
    scount += 1
CPU times: user 6.71 s, sys: 2.75 s, total: 9.46 s
Wall time: 28.7 s
In [53]:
#Visualize FC matrix - mean task FC; same scale as rest data
restFCMin=np.min(np.min(np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2)))
restFCMax=np.max(np.max(np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2)))
ax = sns.heatmap(np.mean(np.mean(taskFC_corr_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder],square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'},vmin=restFCMin,vmax=restFCMax)
ax.figure.suptitle('Mean task-state correlation FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meantaskFC_discovery176.pdf')
In [54]:
taskFCmeanMat=np.mean(np.mean(taskFC_corr_bycond_bysubj,axis=3),axis=2)
restFCmeanMat=np.mean(restFC_corr_bysubj,axis=2)
upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat)[0],k=1)
resttask_mattcorr=np.corrcoef(taskFCmeanMat[upperTIndices_corrmat], restFCmeanMat[upperTIndices_corrmat])
print('Mean task to rest FC similarity, corr: ', str(resttask_mattcorr))
Mean task to rest FC similarity, corr:  [[1.        0.9044012]
 [0.9044012 1.       ]]
In [55]:
#Visualize FC matrix - mean task FC vs. rest FC; same scale as rest data
restFCData=np.mean(restFC_corr_bysubj[netorder,:,:][:,netorder,:],axis=2)
restFCMin=np.min(np.min(restFCData))
restFCMax=np.max(np.max(restFCData))
ax = sns.heatmap(np.mean(np.mean(taskFC_corr_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]-restFCData,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'},vmin=restFCMin,vmax=restFCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Task - rest correlation FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meantaskFCVsRestFC_discovery176.pdf')
In [56]:
#Analysis of task vs. rest matrix
diffMat=np.mean(np.mean(taskFC_corr_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]-restFCData
print('Minimum value: ' + str(min(diffMat.flatten())))
print('Maximum value: ' + str(max(diffMat.flatten())))
print('Mean value: ' + str(np.mean(diffMat.flatten())))
print('Stdev value: ' + str(np.std(diffMat.flatten())))

#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
diffMat_allsubj=np.mean(taskFC_corr_bycond_bysubj,axis=2)-restFC_corr_bysubj
upperTIndices_corrmat=np.triu_indices(np.shape(diffMat_allsubj)[0],k=1)
diffMat_allsubj_upperT=np.transpose([diffMat_allsubj[:,:,subjN][upperTIndices_corrmat] for subjN in np.arange(nSubjs)])

nConnections=int(nParcels*(nParcels-1)/2)

#FDR
# tstats = [stats.ttest_1samp(diffMat_allsubj_upperT[connnum,:],0.0) for connnum in np.arange(nConnections)]
# tvals=np.array([tstats[connnum][0] for connnum in np.arange(nConnections)])
# pvals=[tstats[connnum][1] for connnum in np.arange(nConnections)]
# pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
# fdr_sigvector=pvals_FDRcorrected<0.05
#Bonferroni
# bonferroni_sigvector=pvals<(0.05/nConnections)

#MaxT
maxt_output=actflow.tools.max_t(diffMat_allsubj_upperT)
maxTThreshold = maxt_output[1]
maxt_sigvector=np.array(maxt_output[2])<0.05
tvals=maxt_output[0]
#maxt_sigvector=np.abs(tvals)>maxTThreshold

percenttotal=100*np.sum(maxt_sigvector)/nConnections
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nConnections
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nConnections
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', percenttotal)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)
Minimum value: -0.2999960284313767
Maximum value: 0.19600922475200527
Mean value: -0.04352699817145004
Stdev value: 0.04773918195095355
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 67.84122562674095
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 4.401114206128134
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 63.440111420612816
In [57]:
#Run actflow predictions
print("==Activity flow mapping results, correlation-based task-state FC, 24 conditionss==")
actflowOutput_taskFCCorr = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_corr_bycond_bysubj, full_report=True)
==Activity flow mapping results, correlation-based task-state FC, 24 conditionss==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.66, t-value vs. 0: 133.01, p-value vs. 0: 9.847723014368333e-178
 
Mean % variance explained (R^2 score, coeff. of determination) = -950.31
 
Mean MAE (mean absolute error) = 355.08
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.64, t-value vs. 0: 109.01, p-value vs. 0: 8.603695565398098e-163
 
Mean % variance explained (R^2 score, coeff. of determination) = -2179.55
 
Mean MAE (mean absolute error) = 355.08
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.67, t-value vs. 0: 121.65, p-value vs. 0: 5.07392285153564e-171
By task condition:
Condition 1: r=0.63, t-value vs. 0: 60.16, p-value vs. 0: 7.558726900284918e-119
Condition 2: r=0.58, t-value vs. 0: 48.68, p-value vs. 0: 1.1671842348154553e-103
Condition 3: r=0.83, t-value vs. 0: 72.42, p-value vs. 0: 2.1164984370664604e-132
Condition 4: r=0.83, t-value vs. 0: 75.60, p-value vs. 0: 1.458072901065006e-135
Condition 5: r=0.56, t-value vs. 0: 42.49, p-value vs. 0: 4.004652018723497e-94
Condition 6: r=0.51, t-value vs. 0: 32.95, p-value vs. 0: 6.00178184243681e-77
Condition 7: r=0.63, t-value vs. 0: 47.42, p-value vs. 0: 8.291835382612006e-102
Condition 8: r=0.51, t-value vs. 0: 36.85, p-value vs. 0: 2.1520317052715606e-84
Condition 9: r=0.49, t-value vs. 0: 36.29, p-value vs. 0: 2.362642688052489e-83
Condition 10: r=0.50, t-value vs. 0: 39.28, p-value vs. 0: 1.005091297400319e-88
Condition 11: r=0.47, t-value vs. 0: 32.47, p-value vs. 0: 5.5212714183354844e-76
Condition 12: r=0.50, t-value vs. 0: 36.88, p-value vs. 0: 1.9138694498942842e-84
Condition 13: r=0.85, t-value vs. 0: 91.54, p-value vs. 0: 9.558504478306807e-150
Condition 14: r=0.84, t-value vs. 0: 76.68, p-value vs. 0: 1.3124767952805297e-136
Condition 15: r=0.85, t-value vs. 0: 97.15, p-value vs. 0: 3.560036068551309e-154
Condition 16: r=0.84, t-value vs. 0: 87.45, p-value vs. 0: 2.433375672570632e-146
Condition 17: r=0.63, t-value vs. 0: 54.49, p-value vs. 0: 1.0651441118131204e-111
Condition 18: r=0.57, t-value vs. 0: 47.51, p-value vs. 0: 6.1918877041057e-102
Condition 19: r=0.65, t-value vs. 0: 59.89, p-value vs. 0: 1.5987371693037267e-118
Condition 20: r=0.63, t-value vs. 0: 62.06, p-value vs. 0: 4.22886621399543e-121
Condition 21: r=0.71, t-value vs. 0: 58.23, p-value vs. 0: 1.7355540287790973e-116
Condition 22: r=0.62, t-value vs. 0: 52.11, p-value vs. 0: 1.7007844139540067e-108
Condition 23: r=0.62, t-value vs. 0: 53.03, p-value vs. 0: 9.307136192242341e-110
Condition 24: r=0.69, t-value vs. 0: 59.60, p-value vs. 0: 3.6641701934401612e-118

Match resting-state FC data to task-state FC data (amount of data, task timing regression, timing, etc)

In [58]:
%%time
#Load rest residuals (post-preprocessing) data in and run correlation for each subject
#Match rest analysis to each task as much as possible

loaddata_ifavailable=True
datasuffix='_restFCMatchedTime_corr_data'

restFC_corr_matchtaskFC_bytask_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    outname1 = 'restFCcorrMatchedtime'

    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        restFC_corr_matchtaskFC_bytask_bysubj[:,:,:,scount] = h5f[outname1][:]
        h5f.close()
    else:
        #Compute FCs
        restdata = loadRestResiduals(subj,model='24pXaCompCorXVolterra',zscore=False,FIR=False)
        tasktiming_bycond=tgp.loadTaskTimingForAllTasks(subj,taskModel='canonical')['taskRegressors']>.5
        #Restrict to the number of task time points
        restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]
        for condnum in range(24):
            tasktiming=tasktiming_bycond[:,condnum]
            restFC_corr_matchtaskFC_bytask_bysubj[:,:,condnum,scount] = np.corrcoef(restdata[:,tasktiming],rowvar=1)
            #Set FC matrix diagonal to 0s
            np.fill_diagonal(restFC_corr_matchtaskFC_bytask_bysubj[:,:,condnum,scount],0)
        
        #Save multiple-regression task FC data to file
        h5f = h5py.File(file_path,'a')
        try:
            h5f.create_dataset(outname1,data=restFC_corr_matchtaskFC_bytask_bysubj[:,:,:,scount])
        except:
            del h5f[outname1]
            h5f.create_dataset(outname1,data=restFC_corr_matchtaskFC_bytask_bysubj[:,:,:,scount])
        h5f.close()
        
    scount += 1
CPU times: user 6.74 s, sys: 2.66 s, total: 9.4 s
Wall time: 29 s
In [59]:
print("Mean number of time points per condition:")
tasktiming_bycond=tgp.loadTaskTimingForAllTasks(subjNums[0],taskModel='canonical')['taskRegressors']>.5
np.mean(np.sum(tasktiming_bycond,axis=0))
Mean number of time points per condition:
Out[59]:
114.0
In [60]:
#Plot mean rest FC matrix example (first task)
# plt.imshow(np.tanh(np.mean(np.arctanh(restFC_corr_matchtaskFC_bytask_bysubj[netorder,:,0,:][:,netorder,:]),axis=2)),origin='lower')
# plt.colorbar()
# plt.xlabel('Regions',fontsize=18)
# plt.ylabel('Regions',fontsize=18)
#Visualize FC matrix
ax = sns.heatmap(np.tanh(np.mean(np.arctanh(restFC_corr_matchtaskFC_bytask_bysubj[netorder,:,0,:][:,netorder,:]),axis=2)),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Pearson correlation (r)'})
ax.figure.suptitle('Corr rest FC, time matched to first task cond')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[60]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [61]:
#Run actflow predictions
print("==Activity flow mapping results, correlation-based resting-state FC, MATCHED TO TASK FC, 24 conditions==")
actflowOutput_restFCCorr_matchTaskFC = actflow.actflowcomp.actflowtest(activations_bycond, restFC_corr_matchtaskFC_bytask_bysubj, full_report=True)
==Activity flow mapping results, correlation-based resting-state FC, MATCHED TO TASK FC, 24 conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.51, t-value vs. 0: 98.82, p-value vs. 0: 1.8831750981609734e-155
 
Mean % variance explained (R^2 score, coeff. of determination) = -931.63
 
Mean MAE (mean absolute error) = 347.28
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.51, t-value vs. 0: 69.22, p-value vs. 0: 4.517020040535369e-129
 
Mean % variance explained (R^2 score, coeff. of determination) = -2371.80
 
Mean MAE (mean absolute error) = 347.28
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.45, t-value vs. 0: 87.42, p-value vs. 0: 2.5386638106714127e-146
By task condition:
Condition 1: r=0.44, t-value vs. 0: 40.72, p-value vs. 0: 3.3960357982997203e-91
Condition 2: r=0.42, t-value vs. 0: 30.12, p-value vs. 0: 3.802812376370346e-71
Condition 3: r=0.54, t-value vs. 0: 51.08, p-value vs. 0: 4.405479906713164e-107
Condition 4: r=0.56, t-value vs. 0: 51.15, p-value vs. 0: 3.5401529332990345e-107
Condition 5: r=0.36, t-value vs. 0: 26.77, p-value vs. 0: 8.973091672723986e-64
Condition 6: r=0.35, t-value vs. 0: 23.82, p-value vs. 0: 8.471693519034299e-57
Condition 7: r=0.53, t-value vs. 0: 43.44, p-value vs. 0: 1.1596733203291948e-95
Condition 8: r=0.34, t-value vs. 0: 25.72, p-value vs. 0: 2.3804844661954718e-61
Condition 9: r=0.31, t-value vs. 0: 24.01, p-value vs. 0: 2.8954719574934846e-57
Condition 10: r=0.32, t-value vs. 0: 23.68, p-value vs. 0: 1.826239261437329e-56
Condition 11: r=0.29, t-value vs. 0: 23.75, p-value vs. 0: 1.208786792078213e-56
Condition 12: r=0.32, t-value vs. 0: 25.05, p-value vs. 0: 9.032045987976195e-60
Condition 13: r=0.55, t-value vs. 0: 56.48, p-value vs. 0: 2.773154678288803e-114
Condition 14: r=0.58, t-value vs. 0: 55.97, p-value vs. 0: 1.2509742604636874e-113
Condition 15: r=0.54, t-value vs. 0: 50.78, p-value vs. 0: 1.1580232367111406e-106
Condition 16: r=0.58, t-value vs. 0: 55.14, p-value vs. 0: 1.4994325630907276e-112
Condition 17: r=0.44, t-value vs. 0: 39.18, p-value vs. 0: 1.5024332577893545e-88
Condition 18: r=0.40, t-value vs. 0: 37.53, p-value vs. 0: 1.2876407710399194e-85
Condition 19: r=0.46, t-value vs. 0: 47.00, p-value vs. 0: 3.587684448117567e-101
Condition 20: r=0.49, t-value vs. 0: 45.07, p-value vs. 0: 3.0994564453485526e-98
Condition 21: r=0.50, t-value vs. 0: 46.62, p-value vs. 0: 1.3070846077557655e-100
Condition 22: r=0.39, t-value vs. 0: 31.79, p-value vs. 0: 1.261933306793479e-74
Condition 23: r=0.42, t-value vs. 0: 39.27, p-value vs. 0: 1.0631824657826146e-88
Condition 24: r=0.45, t-value vs. 0: 43.88, p-value vs. 0: 2.2850107547266913e-96
In [62]:
print("===Compare Task corrFC actflow predictions to Rest corrFC (matching timing) actflow prediction===")
model_compare_TaskCorrFCVsRestCorrFCMatchTimingActflow = actflow.model_compare(target_actvect=actflowOutput_restFCCorr_matchTaskFC['actVect_actual_group'], model1_actvect=actflowOutput_restFCCorr_matchTaskFC['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCCorr['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Task corrFC actflow predictions to Rest corrFC (matching timing) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.51
Model2 mean Pearson r=0.66
R-value difference = -0.15
Model1 vs. Model2 T-value: -41.83, p-value: 4.75133395319819e-93
 
Model1 mean % predicted variance explained R^2=-931.63
Model2 mean % predicted variance explained R^2=-950.31
R^2 difference = 18.68
 
Model1 mean MAE = 347.28
Model2 mean MAE = 355.08
Model1 vs. Model2 mean MAE difference = -7.80
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.51
Model2 mean Pearson r=0.64
R-value difference = -0.12
Model1 vs. Model2 T-value: -41.71, p-value: 7.704353980600651e-93
 
Model1 mean % predicted variance explained R^2=-2371.80
Model2 mean % predicted variance explained R^2=-2179.55
R^2 difference = -192.24
 
Model1 mean MAE = 347.28
Model2 mean MAE = 355.08
Model1 vs. Model2 mean MAE difference = -7.80
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.45
Model2 mean Pearson r=0.67
R-value difference = -0.23
Model1 vs. Model2 T-value: -76.25, p-value: 3.385818709986526e-136
By task condition:
Condition 1: Model 1 r=0.44, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -26.90, p-value vs. 0: 4.440979099137389e-64
Condition 2: Model 1 r=0.42, Model 2 r=0.58, Model 1 vs. 2 R-value difference =-0.16, t-value Model1 vs. Model2: -21.34, p-value vs. 0: 1.3851405578730324e-50
Condition 3: Model 1 r=0.54, Model 2 r=0.83, Model 1 vs. 2 R-value difference =-0.28, t-value Model1 vs. Model2: -43.72, p-value vs. 0: 4.1192499491345826e-96
Condition 4: Model 1 r=0.56, Model 2 r=0.83, Model 1 vs. 2 R-value difference =-0.27, t-value Model1 vs. Model2: -41.62, p-value vs. 0: 1.0539998739522739e-92
Condition 5: Model 1 r=0.36, Model 2 r=0.56, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -22.42, p-value vs. 0: 2.4355807114709464e-53
Condition 6: Model 1 r=0.35, Model 2 r=0.51, Model 1 vs. 2 R-value difference =-0.17, t-value Model1 vs. Model2: -15.97, p-value vs. 0: 5.2201254960598855e-36
Condition 7: Model 1 r=0.53, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.10, t-value Model1 vs. Model2: -13.29, p-value vs. 0: 2.634812604044933e-28
Condition 8: Model 1 r=0.34, Model 2 r=0.51, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -16.15, p-value vs. 0: 1.5944621013236907e-36
Condition 9: Model 1 r=0.31, Model 2 r=0.49, Model 1 vs. 2 R-value difference =-0.17, t-value Model1 vs. Model2: -15.32, p-value vs. 0: 3.7672509455807806e-34
Condition 10: Model 1 r=0.32, Model 2 r=0.50, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -16.05, p-value vs. 0: 3.1581937653748046e-36
Condition 11: Model 1 r=0.29, Model 2 r=0.47, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -16.05, p-value vs. 0: 3.119530099134908e-36
Condition 12: Model 1 r=0.32, Model 2 r=0.50, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -18.34, p-value vs. 0: 1.3537424829399696e-42
Condition 13: Model 1 r=0.55, Model 2 r=0.85, Model 1 vs. 2 R-value difference =-0.31, t-value Model1 vs. Model2: -47.96, p-value vs. 0: 1.3410365519026514e-102
Condition 14: Model 1 r=0.58, Model 2 r=0.84, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -39.48, p-value vs. 0: 4.569235547640881e-89
Condition 15: Model 1 r=0.54, Model 2 r=0.85, Model 1 vs. 2 R-value difference =-0.31, t-value Model1 vs. Model2: -52.90, p-value vs. 0: 1.4046283115739553e-109
Condition 16: Model 1 r=0.58, Model 2 r=0.84, Model 1 vs. 2 R-value difference =-0.26, t-value Model1 vs. Model2: -43.37, p-value vs. 0: 1.5097993274288916e-95
Condition 17: Model 1 r=0.44, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -21.74, p-value vs. 0: 1.3260574156776336e-51
Condition 18: Model 1 r=0.40, Model 2 r=0.57, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -17.62, p-value vs. 0: 1.3026912912783449e-40
Condition 19: Model 1 r=0.46, Model 2 r=0.65, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -20.60, p-value vs. 0: 1.1757834426456693e-48
Condition 20: Model 1 r=0.49, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.15, t-value Model1 vs. Model2: -16.87, p-value vs. 0: 1.563851256600152e-38
Condition 21: Model 1 r=0.50, Model 2 r=0.71, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -21.22, p-value vs. 0: 2.894574287590295e-50
Condition 22: Model 1 r=0.39, Model 2 r=0.62, Model 1 vs. 2 R-value difference =-0.23, t-value Model1 vs. Model2: -23.32, p-value vs. 0: 1.4097812869783617e-55
Condition 23: Model 1 r=0.42, Model 2 r=0.62, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -21.93, p-value vs. 0: 4.236219211047726e-52
Condition 24: Model 1 r=0.45, Model 2 r=0.69, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -26.09, p-value vs. 0: 3.2464084482024463e-62

Visualizing predictions, rest and task correlation FC

In [63]:
#Visualize predicted and actual activations
data1=stats.zscore(np.mean(actflowOutput_restFCCorr_matchTaskFC['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
data2=stats.zscore(np.mean(activations_bycond,axis=2)[netorder,:],axis=None)
data3=stats.zscore(np.mean(actflowOutput_taskFCCorr['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
datamin=min(np.array([min(data1.flatten()),min(data2.flatten()),min(data3.flatten())]))
datamax=max(np.array([max(data1.flatten()),max(data2.flatten()),max(data3.flatten())]))
fig, axn = plt.subplots(1, 3, sharex=True, sharey=True, figsize=[14,5])
cbar_ax = fig.add_axes([.90, .3, .015, .5])
ax1 = sns.heatmap(data1,ax=axn[0],center=0,cmap='seismic',cbar=False,yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax1.title.set_text('Pred: Rest corr FC')
ax2 = sns.heatmap(data2,ax=axn[1],center=0,cmap='seismic',cbar=False,yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax2.title.set_text('Actual activations')
ax3 = sns.heatmap(data3,ax=axn[2],center=0,cmap='seismic',cbar=True,cbar_ax=cbar_ax,cbar_kws={'label': 'Activation (z-score)'},yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax3.title.set_text('Pred: Task corr FC')
ax1.set_xticklabels(ax1.get_xmajorticklabels(), fontsize = 12)
ax2.set_xticklabels(ax2.get_xmajorticklabels(), fontsize = 12)
ax3.set_xticklabels(ax3.get_xmajorticklabels(), fontsize = 12)
fig.tight_layout(rect=[0, 0, .9, 1])
# fig=ax.get_figure()
fig.savefig(figuredir+'restTaskActflowPred_CorrFC_discovery176.pdf')
/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py:2369: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "
In [64]:
ttestinputdata=actflowOutput_taskFCCorr['model_compare_output']['corr_conditionwise_compthenavg_bynode']
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
In [65]:
#Save condition-wise accuracy for each node to surface map
#taskcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
ttestinputdata=np.arctanh(actflowOutput_taskFCCorr['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.arctanh(actflowOutput_taskFCCorr['model_compare_output']['corr_conditionwise_compthenavg_bynode']),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat_task=datamat.copy()
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'taskActflowPred_condwise_CorrFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)

#restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
ttestinputdata=np.arctanh(actflowOutput_restFCCorr_matchTaskFC['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.arctanh(actflowOutput_restFCCorr_matchTaskFC['model_compare_output']['corr_conditionwise_compthenavg_bynode']),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat_rest=datamat.copy()
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'restMatchTaskActflowPred_condwise_CorrFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 100.0
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_CorrFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_CorrFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_CorrFC_discovery176.dscalar.nii
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 100.0
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_CorrFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_CorrFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_CorrFC_discovery176.dscalar.nii
In [66]:
#Plotting images in-line, Task and rest FC-based predictions 

from wbplot import pscalar
import matplotlib.image as mpimg

#RestFC predicted
inputdata=datamat_rest
print('Min value: ', np.min(inputdata))
print('Max value: ', np.max(inputdata))
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
#Set to all reds if no negative values
if min(inputdata) >= 0:
    colormap='Reds'
else:
    colormap='seismic'
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap=colormap,
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('RestFC actflow prediction acc')
plt.imshow(img)


#TaskFC predicted
inputdata=datamat_task
print('Min value: ', np.min(inputdata))
print('Max value: ', np.max(inputdata))
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
#Set to all reds if no negative values
if min(inputdata) >= 0:
    colormap='Reds'
else:
    colormap='seismic'
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap=colormap,
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('TaskFC acflow prediction acc')
plt.imshow(img)
Min value:  0.11055169073475297
Max value:  0.8036428440867709
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Min value:  0.2165885350764139
Max value:  0.876066609639216
Out[66]:
<matplotlib.image.AxesImage at 0x7faf86a9a588>
In [67]:
#Compare task-state and resting-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset=np.arctanh(actflowOutput_taskFCCorr['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
restdataset=np.arctanh(actflowOutput_restFCCorr_matchTaskFC['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset,restdataset)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat_taskVrest=datamat.copy()
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'taskVSrestMatchTaskActflowPred_condwise_CorrFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 94.16666666666667
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_CorrFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_CorrFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_CorrFC_discovery176.dscalar.nii
In [68]:
#Minimum value for task vs. rest map
datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
datamat=np.multiply(datamat,maxt_sigvector)
np.min(datamat[datamat>0])
Out[68]:
0.08014621753303948
In [ ]:
#Plotting images in-line, Task vs. rest FC-based predictions 

from wbplot import pscalar
import matplotlib.image as mpimg

#RestFC predicted
inputdata=datamat_taskVrest
print('Min value: ', np.min(inputdata))
print('Max value: ', np.max(inputdata))
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
#Set to all reds if no negative values
if min(inputdata) >= 0:
    colormap='Reds'
else:
    colormap='seismic'
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap=colormap,
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('RestFC actflow prediction acc')
plt.imshow(img)
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Min value:  0.0
Max value:  0.3857511272723973
Out[ ]:
<matplotlib.image.AxesImage at 0x7faf94427a90>

Test with FIR-only task FC

In [ ]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_FIRonly_data'

taskFCFIRonly_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFCFIRonly_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")  
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow\n")
        
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:07:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==")
actflowOutput_taskFCMRegFIRonly_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskFCFIRonly_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.80, t-value vs. 0: 140.90, p-value vs. 0: 4.5389416998745856e-182
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.58
 
Mean MAE (mean absolute error) = 7.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.79, t-value vs. 0: 157.21, p-value vs. 0: 2.473746530947629e-190
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.08
 
Mean MAE (mean absolute error) = 7.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.80, t-value vs. 0: 152.41, p-value vs. 0: 5.4465718757102326e-188
By task condition:
Condition 1: r=0.81, t-value vs. 0: 73.22, p-value vs. 0: 3.3032555908253077e-133
Condition 2: r=0.80, t-value vs. 0: 81.66, p-value vs. 0: 2.896883499960055e-141
Condition 3: r=0.88, t-value vs. 0: 77.58, p-value vs. 0: 1.8122523924977322e-137
Condition 4: r=0.88, t-value vs. 0: 78.01, p-value vs. 0: 7.064050785505081e-138
Condition 5: r=0.73, t-value vs. 0: 74.04, p-value vs. 0: 5.068079285596926e-134
Condition 6: r=0.68, t-value vs. 0: 62.71, p-value vs. 0: 7.390982495320794e-122
Condition 7: r=0.71, t-value vs. 0: 60.30, p-value vs. 0: 5.123854230950464e-119
Condition 8: r=0.60, t-value vs. 0: 46.80, p-value vs. 0: 7.052832146938589e-101
Condition 9: r=0.58, t-value vs. 0: 51.73, p-value vs. 0: 5.5511173649602015e-108
Condition 10: r=0.58, t-value vs. 0: 45.98, p-value vs. 0: 1.2164548218811596e-99
Condition 11: r=0.56, t-value vs. 0: 45.56, p-value vs. 0: 5.4287787010725465e-99
Condition 12: r=0.59, t-value vs. 0: 47.85, p-value vs. 0: 1.8918187072995967e-102
Condition 13: r=0.91, t-value vs. 0: 83.60, p-value vs. 0: 5.343081711850515e-143
Condition 14: r=0.89, t-value vs. 0: 83.01, p-value vs. 0: 1.7654556526195787e-142
Condition 15: r=0.91, t-value vs. 0: 93.41, p-value vs. 0: 2.992131320904387e-151
Condition 16: r=0.90, t-value vs. 0: 81.95, p-value vs. 0: 1.5896824520254702e-141
Condition 17: r=0.84, t-value vs. 0: 84.12, p-value vs. 0: 1.8363137583180966e-143
Condition 18: r=0.78, t-value vs. 0: 70.93, p-value vs. 0: 7.19692026093036e-131
Condition 19: r=0.84, t-value vs. 0: 87.06, p-value vs. 0: 5.208385680663801e-146
Condition 20: r=0.82, t-value vs. 0: 77.17, p-value vs. 0: 4.410143036674235e-137
Condition 21: r=0.85, t-value vs. 0: 87.60, p-value vs. 0: 1.8001618011133137e-146
Condition 22: r=0.79, t-value vs. 0: 75.77, p-value vs. 0: 1.0008690040345632e-135
Condition 23: r=0.82, t-value vs. 0: 86.00, p-value vs. 0: 4.194146056665873e-145
Condition 24: r=0.83, t-value vs. 0: 84.67, p-value vs. 0: 6.056740678409403e-144

Task-state multreg FC with parcel-level non-circular approach

In [ ]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_parcelnoncirc_data'

taskFC_mreg_parcelnoncirc_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj +'('+str(scount)+')')
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_mreg_parcelnoncirc_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")  
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow\n")
    
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("inputfilename = '/projects/f_mc1689_1/TaskFCActflow/docs/scripts/final_analyses/ActflowToolbox/network_definitions/parcels_to_remove_indices_cortexonly_data.h5'\n")
        file_python.write("h5f = h5py.File(inputfilename,'r')\n")
        file_python.write("parcels_to_remove={} \n")
        file_python.write("for parcelInt in range(360):\n")
        file_python.write("    outname1 = 'parcels_to_remove_indices'+'/'+str(parcelInt)\n")
        file_python.write("    parcels_to_remove[parcelInt] = h5f[outname1][:].copy()\n")
        file_python.write("h5f.close()\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        #file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming], n_components=0.9, svd_solver='full')\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming],parcelstoexclude_bytarget=parcels_to_remove)\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:07:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskFC_mreg_parcelnoncirc_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-state FC, PARCEL NON-CIRCULAR, 24 task conditions==")
actflowOutput_taskFCMReg_parcelnoncirc_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_mreg_parcelnoncirc_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, PARCEL NON-CIRCULAR, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.74, t-value vs. 0: 139.23, p-value vs. 0: 3.551547548142301e-181
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.47
 
Mean MAE (mean absolute error) = 7.78
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.73, t-value vs. 0: 142.61, p-value vs. 0: 5.5877548821562256e-183
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.09
 
Mean MAE (mean absolute error) = 7.78
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.75, t-value vs. 0: 162.92, p-value vs. 0: 5.056419957271384e-193
By task condition:
Condition 1: r=0.76, t-value vs. 0: 70.02, p-value vs. 0: 6.335144262843791e-130
Condition 2: r=0.76, t-value vs. 0: 74.60, p-value vs. 0: 1.39513529437692e-134
Condition 3: r=0.87, t-value vs. 0: 77.14, p-value vs. 0: 4.718725256983103e-137
Condition 4: r=0.88, t-value vs. 0: 80.82, p-value vs. 0: 1.719270290759063e-140
Condition 5: r=0.65, t-value vs. 0: 64.15, p-value vs. 0: 1.647897780179108e-123
Condition 6: r=0.62, t-value vs. 0: 59.98, p-value vs. 0: 1.2731620271483242e-118
Condition 7: r=0.67, t-value vs. 0: 56.21, p-value vs. 0: 6.235263146633831e-114
Condition 8: r=0.57, t-value vs. 0: 43.71, p-value vs. 0: 4.24526906907325e-96
Condition 9: r=0.54, t-value vs. 0: 46.26, p-value vs. 0: 4.613535364638656e-100
Condition 10: r=0.55, t-value vs. 0: 43.09, p-value vs. 0: 4.16764079560483e-95
Condition 11: r=0.53, t-value vs. 0: 42.47, p-value vs. 0: 4.334346420206959e-94
Condition 12: r=0.56, t-value vs. 0: 45.63, p-value vs. 0: 4.31406043740951e-99
Condition 13: r=0.90, t-value vs. 0: 84.79, p-value vs. 0: 4.7920000415849795e-144
Condition 14: r=0.90, t-value vs. 0: 100.43, p-value vs. 0: 1.1798666207744762e-156
Condition 15: r=0.90, t-value vs. 0: 90.24, p-value vs. 0: 1.1151547950829046e-148
Condition 16: r=0.89, t-value vs. 0: 81.52, p-value vs. 0: 3.874735452965494e-141
Condition 17: r=0.67, t-value vs. 0: 62.77, p-value vs. 0: 6.285159904976458e-122
Condition 18: r=0.71, t-value vs. 0: 60.11, p-value vs. 0: 8.858129609125342e-119
Condition 19: r=0.74, t-value vs. 0: 80.16, p-value vs. 0: 6.8786796186050475e-140
Condition 20: r=0.78, t-value vs. 0: 81.37, p-value vs. 0: 5.400574939765379e-141
Condition 21: r=0.78, t-value vs. 0: 76.73, p-value vs. 0: 1.1641293502083885e-136
Condition 22: r=0.78, t-value vs. 0: 68.15, p-value vs. 0: 6.246328032564199e-128
Condition 23: r=0.70, t-value vs. 0: 66.68, p-value vs. 0: 2.4274891325081967e-126
Condition 24: r=0.73, t-value vs. 0: 52.75, p-value vs. 0: 2.2520680155387522e-109

Resting-state multreg FC with parcel-level non-circular approach, time-matched to task FC

In [ ]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFC_parcelnoncirc_data'

restFC_mreg_parcelnoncirc_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj +'('+str(scount)+')')
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFC_mreg_parcelnoncirc_bycond_bysubj[:,:,condnum,scount] = h5f['restFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")  
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("inputfilename = '/projects/f_mc1689_1/TaskFCActflow/docs/scripts/final_analyses/ActflowToolbox/network_definitions/parcels_to_remove_indices_cortexonly_data.h5'\n")
        file_python.write("h5f = h5py.File(inputfilename,'r')\n")
        file_python.write("parcels_to_remove={} \n")
        file_python.write("for parcelInt in range(360):\n")
        file_python.write("    outname1 = 'parcels_to_remove_indices'+'/'+str(parcelInt)\n")
        file_python.write("    parcels_to_remove[parcelInt] = h5f[outname1][:].copy()\n")
        file_python.write("h5f.close()\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        #file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming], n_components=0.9, svd_solver='full')\n")
        file_python.write("    restFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(restdata[:,tasktiming],parcelstoexclude_bytarget=parcels_to_remove)\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:07:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_mreg_parcelnoncirc_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [ ]:
#Run actflow predictions, 24-condition multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC time-matched, PARCEL NON-CIRCULAR, 24 task conditions==")
actflowOutput_restFCMReg_parcelnoncirc_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_parcelnoncirc_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC time-matched, PARCEL NON-CIRCULAR, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.39, t-value vs. 0: 72.97, p-value vs. 0: 5.906358845302521e-133
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.52
 
Mean MAE (mean absolute error) = 12.79
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.43, t-value vs. 0: 78.24, p-value vs. 0: 4.235457462421895e-138
 
Mean % variance explained (R^2 score, coeff. of determination) = -2.19
 
Mean MAE (mean absolute error) = 12.79
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.39, t-value vs. 0: 85.04, p-value vs. 0: 2.850969645564453e-144
By task condition:
Condition 1: r=0.36, t-value vs. 0: 33.04, p-value vs. 0: 4.010913489802938e-77
Condition 2: r=0.34, t-value vs. 0: 29.88, p-value vs. 0: 1.2132354914781767e-70
Condition 3: r=0.48, t-value vs. 0: 45.90, p-value vs. 0: 1.6313130924182756e-99
Condition 4: r=0.48, t-value vs. 0: 44.94, p-value vs. 0: 4.870045398519804e-98
Condition 5: r=0.27, t-value vs. 0: 33.63, p-value vs. 0: 2.656549064574768e-78
Condition 6: r=0.22, t-value vs. 0: 30.01, p-value vs. 0: 6.655569868551265e-71
Condition 7: r=0.49, t-value vs. 0: 43.03, p-value vs. 0: 5.239317222920349e-95
Condition 8: r=0.32, t-value vs. 0: 28.01, p-value vs. 0: 1.4679170137220287e-66
Condition 9: r=0.31, t-value vs. 0: 31.27, p-value vs. 0: 1.5355709453537087e-73
Condition 10: r=0.32, t-value vs. 0: 28.16, p-value vs. 0: 6.775700694669266e-67
Condition 11: r=0.29, t-value vs. 0: 28.52, p-value vs. 0: 1.0619108385710929e-67
Condition 12: r=0.32, t-value vs. 0: 29.19, p-value vs. 0: 3.6735429807151035e-69
Condition 13: r=0.51, t-value vs. 0: 53.52, p-value vs. 0: 2.068929405585843e-110
Condition 14: r=0.53, t-value vs. 0: 51.31, p-value vs. 0: 2.155406162803703e-107
Condition 15: r=0.45, t-value vs. 0: 42.43, p-value vs. 0: 5.072014984656015e-94
Condition 16: r=0.47, t-value vs. 0: 51.46, p-value vs. 0: 1.3361006869247457e-107
Condition 17: r=0.39, t-value vs. 0: 36.42, p-value vs. 0: 1.3473664258707091e-83
Condition 18: r=0.33, t-value vs. 0: 31.42, p-value vs. 0: 7.545630622577771e-74
Condition 19: r=0.43, t-value vs. 0: 40.26, p-value vs. 0: 2.0488085332981756e-90
Condition 20: r=0.45, t-value vs. 0: 40.91, p-value vs. 0: 1.6680277008577508e-91
Condition 21: r=0.44, t-value vs. 0: 43.95, p-value vs. 0: 1.7602499828152563e-96
Condition 22: r=0.36, t-value vs. 0: 32.09, p-value vs. 0: 3.130754750986137e-75
Condition 23: r=0.40, t-value vs. 0: 36.06, p-value vs. 0: 6.3560521474188815e-83
Condition 24: r=0.42, t-value vs. 0: 38.84, p-value vs. 0: 5.930195307622001e-88
In [ ]:
print("===Compare Task multregFC actflow predictions to Rest multregFC (matching timing), PARCEL NON-CIRCULAR, actflow prediction===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTimingActflow_parcelnoncircular = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_parcelnoncirc_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_parcelnoncirc_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_parcelnoncirc_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True, avgthencomp_fixedeffects=True)
===Compare Task multregFC actflow predictions to Rest multregFC (matching timing), PARCEL NON-CIRCULAR, actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.39
Model2 mean Pearson r=0.74
R-value difference = -0.35
Model1 vs. Model2 T-value: -98.54, p-value: 3.077986284091213e-155
 
Model1 mean % predicted variance explained R^2=-0.52
Model2 mean % predicted variance explained R^2=0.47
R^2 difference = -0.98
 
Model1 mean MAE = 12.79
Model2 mean MAE = 7.78
Model1 vs. Model2 mean MAE difference = 5.00
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.43
Model2 mean Pearson r=0.73
R-value difference = -0.31
Model1 vs. Model2 T-value: -107.75, p-value: 6.392091841270425e-162
 
Model1 mean % predicted variance explained R^2=-2.19
Model2 mean % predicted variance explained R^2=-0.09
R^2 difference = -2.11
 
Model1 mean MAE = 12.79
Model2 mean MAE = 7.78
Model1 vs. Model2 mean MAE difference = 5.00
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Model1 Mean Pearson r=0.84
Model2 Mean Pearson r=0.97
R-value difference = -0.12
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=4.89
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.39
Model2 mean Pearson r=0.75
R-value difference = -0.36
Model1 vs. Model2 T-value: -122.02, p-value: 3.0031246308268265e-171
By task condition:
Condition 1: Model 1 r=0.36, Model 2 r=0.76, Model 1 vs. 2 R-value difference =-0.40, t-value Model1 vs. Model2: -45.33, p-value vs. 0: 1.2234593164747476e-98
Condition 2: Model 1 r=0.34, Model 2 r=0.76, Model 1 vs. 2 R-value difference =-0.42, t-value Model1 vs. Model2: -51.65, p-value vs. 0: 7.164224710993966e-108
Condition 3: Model 1 r=0.48, Model 2 r=0.87, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -48.45, p-value vs. 0: 2.559115382756021e-103
Condition 4: Model 1 r=0.48, Model 2 r=0.88, Model 1 vs. 2 R-value difference =-0.40, t-value Model1 vs. Model2: -53.82, p-value vs. 0: 8.103408049927836e-111
Condition 5: Model 1 r=0.27, Model 2 r=0.65, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -40.47, p-value vs. 0: 9.048029073198767e-91
Condition 6: Model 1 r=0.22, Model 2 r=0.62, Model 1 vs. 2 R-value difference =-0.40, t-value Model1 vs. Model2: -36.93, p-value vs. 0: 1.5898489131734441e-84
Condition 7: Model 1 r=0.49, Model 2 r=0.67, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -22.20, p-value vs. 0: 8.928902506063956e-53
Condition 8: Model 1 r=0.32, Model 2 r=0.57, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -22.51, p-value vs. 0: 1.474002435283425e-53
Condition 9: Model 1 r=0.31, Model 2 r=0.54, Model 1 vs. 2 R-value difference =-0.23, t-value Model1 vs. Model2: -22.28, p-value vs. 0: 5.5823016150224005e-53
Condition 10: Model 1 r=0.32, Model 2 r=0.55, Model 1 vs. 2 R-value difference =-0.23, t-value Model1 vs. Model2: -20.36, p-value vs. 0: 5.1820559642890064e-48
Condition 11: Model 1 r=0.29, Model 2 r=0.53, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -21.76, p-value vs. 0: 1.17802816721538e-51
Condition 12: Model 1 r=0.32, Model 2 r=0.56, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -23.47, p-value vs. 0: 5.911643193819852e-56
Condition 13: Model 1 r=0.51, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -53.68, p-value vs. 0: 1.2596341872770011e-110
Condition 14: Model 1 r=0.53, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.37, t-value Model1 vs. Model2: -60.49, p-value vs. 0: 3.086250485339665e-119
Condition 15: Model 1 r=0.45, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.45, t-value Model1 vs. Model2: -57.72, p-value vs. 0: 7.635550463775288e-116
Condition 16: Model 1 r=0.47, Model 2 r=0.89, Model 1 vs. 2 R-value difference =-0.42, t-value Model1 vs. Model2: -52.80, p-value vs. 0: 1.9200496570403916e-109
Condition 17: Model 1 r=0.39, Model 2 r=0.67, Model 1 vs. 2 R-value difference =-0.28, t-value Model1 vs. Model2: -27.44, p-value vs. 0: 2.6497871264491734e-65
Condition 18: Model 1 r=0.33, Model 2 r=0.71, Model 1 vs. 2 R-value difference =-0.38, t-value Model1 vs. Model2: -32.61, p-value vs. 0: 2.8580066149046226e-76
Condition 19: Model 1 r=0.43, Model 2 r=0.74, Model 1 vs. 2 R-value difference =-0.31, t-value Model1 vs. Model2: -31.75, p-value vs. 0: 1.5442220565449475e-74
Condition 20: Model 1 r=0.45, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.33, t-value Model1 vs. Model2: -37.05, p-value vs. 0: 9.582951173340575e-85
Condition 21: Model 1 r=0.44, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.33, t-value Model1 vs. Model2: -35.92, p-value vs. 0: 1.1173978516922988e-82
Condition 22: Model 1 r=0.36, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.42, t-value Model1 vs. Model2: -41.88, p-value vs. 0: 4.051369369274092e-93
Condition 23: Model 1 r=0.40, Model 2 r=0.70, Model 1 vs. 2 R-value difference =-0.30, t-value Model1 vs. Model2: -29.21, p-value vs. 0: 3.397161967661696e-69
Condition 24: Model 1 r=0.42, Model 2 r=0.73, Model 1 vs. 2 R-value difference =-0.31, t-value Model1 vs. Model2: -26.80, p-value vs. 0: 7.760120727124755e-64
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean Model1 r=0.85
Mean Model2 r=0.99
Mean Model1 vs. Model2 R-value difference=-0.14
By task condition:
Condition 1: Model1 r=0.85, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.14
Condition 2: Model1 r=0.84, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.14
Condition 3: Model1 r=0.91, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.08
Condition 4: Model1 r=0.91, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.09
Condition 5: Model1 r=0.89, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
Condition 6: Model1 r=0.89, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.10
Condition 7: Model1 r=0.85, Model2 r=0.96, Model 1 vs. Model2 R-value difference=-0.11
Condition 8: Model1 r=0.73, Model2 r=0.94, Model 1 vs. Model2 R-value difference=-0.21
Condition 9: Model1 r=0.73, Model2 r=0.93, Model 1 vs. Model2 R-value difference=-0.20
Condition 10: Model1 r=0.68, Model2 r=0.91, Model 1 vs. Model2 R-value difference=-0.23
Condition 11: Model1 r=0.60, Model2 r=0.92, Model 1 vs. Model2 R-value difference=-0.32
Condition 12: Model1 r=0.76, Model2 r=0.94, Model 1 vs. Model2 R-value difference=-0.19
Condition 13: Model1 r=0.93, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.07
Condition 14: Model1 r=0.93, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.07
Condition 15: Model1 r=0.90, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.10
Condition 16: Model1 r=0.89, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.11
Condition 17: Model1 r=0.84, Model2 r=0.98, Model 1 vs. Model2 R-value difference=-0.14
Condition 18: Model1 r=0.75, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.23
Condition 19: Model1 r=0.86, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.13
Condition 20: Model1 r=0.87, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.12
Condition 21: Model1 r=0.86, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.13
Condition 22: Model1 r=0.83, Model2 r=0.98, Model 1 vs. Model2 R-value difference=-0.15
Condition 23: Model1 r=0.84, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.14
Condition 24: Model1 r=0.86, Model2 r=0.98, Model 1 vs. Model2 R-value difference=-0.13

Task-state multreg FC with comparison to resting-state FC actflow predictions

In [23]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_data'

taskFC_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj +'('+str(scount)+')')
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")  
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow\n")
    
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        #file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming], n_components=0.9, svd_solver='full')\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:10:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [26]:
# plt.imshow(np.mean(taskFC_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskFC_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[26]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [27]:
#Run actflow predictions, 24-condition task multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==")
actflowOutput_taskFCMReg_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.76, t-value vs. 0: 146.04, p-value vs. 0: 8.954760287923477e-185
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.51
 
Mean MAE (mean absolute error) = 7.47
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.75, t-value vs. 0: 147.67, p-value vs. 0: 1.3078833136358264e-185
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.01
 
Mean MAE (mean absolute error) = 7.47
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.77, t-value vs. 0: 169.37, p-value vs. 0: 5.877040193700178e-196
By task condition:
Condition 1: r=0.77, t-value vs. 0: 72.95, p-value vs. 0: 6.212304357191994e-133
Condition 2: r=0.77, t-value vs. 0: 75.22, p-value vs. 0: 3.399631657942241e-135
Condition 3: r=0.88, t-value vs. 0: 80.62, p-value vs. 0: 2.593721284491915e-140
Condition 4: r=0.89, t-value vs. 0: 84.85, p-value vs. 0: 4.2350472928261526e-144
Condition 5: r=0.68, t-value vs. 0: 68.71, p-value vs. 0: 1.5557551359270675e-128
Condition 6: r=0.65, t-value vs. 0: 60.32, p-value vs. 0: 4.953562632320203e-119
Condition 7: r=0.69, t-value vs. 0: 60.87, p-value vs. 0: 1.0815439142904374e-119
Condition 8: r=0.60, t-value vs. 0: 47.52, p-value vs. 0: 5.946781341513935e-102
Condition 9: r=0.57, t-value vs. 0: 50.43, p-value vs. 0: 3.5949889961817933e-106
Condition 10: r=0.58, t-value vs. 0: 47.68, p-value vs. 0: 3.4803808600783063e-102
Condition 11: r=0.56, t-value vs. 0: 46.42, p-value vs. 0: 2.635620843844515e-100
Condition 12: r=0.59, t-value vs. 0: 49.76, p-value vs. 0: 3.271818578832649e-105
Condition 13: r=0.91, t-value vs. 0: 87.26, p-value vs. 0: 3.5113502850990168e-146
Condition 14: r=0.91, t-value vs. 0: 103.63, p-value vs. 0: 5.288586478276634e-159
Condition 15: r=0.91, t-value vs. 0: 93.73, p-value vs. 0: 1.6644096115582727e-151
Condition 16: r=0.90, t-value vs. 0: 86.30, p-value vs. 0: 2.3136046315662494e-145
Condition 17: r=0.69, t-value vs. 0: 68.68, p-value vs. 0: 1.6783796534339832e-128
Condition 18: r=0.73, t-value vs. 0: 64.64, p-value vs. 0: 4.500866124565361e-124
Condition 19: r=0.76, t-value vs. 0: 85.93, p-value vs. 0: 4.808091941794375e-145
Condition 20: r=0.80, t-value vs. 0: 86.73, p-value vs. 0: 9.900418944210282e-146
Condition 21: r=0.79, t-value vs. 0: 82.22, p-value vs. 0: 9.077002113274862e-142
Condition 22: r=0.79, t-value vs. 0: 73.15, p-value vs. 0: 3.934449897219711e-133
Condition 23: r=0.73, t-value vs. 0: 71.46, p-value vs. 0: 2.0402540640978048e-131
Condition 24: r=0.75, t-value vs. 0: 57.61, p-value vs. 0: 1.0383483492286476e-115
In [28]:
np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_nodewise_compthenavg_bycond'])
Out[28]:
0.48018888893341594
In [29]:
#plt.plot(np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_conditionwise_compthenavg_bynode'],axis=1))
#plt.imshow(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_conditionwise_compthenavg_bynode'],aspect='auto')
ax = sns.heatmap(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_conditionwise_compthenavg_bynode'],center=0,cmap='seismic',cbar=True)
meanR2=np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_conditionwise_compthenavg_bynode'],axis=1)
print('Number of nodes above 0.25:', np.sum(meanR2>0.25))
print('Number of nodes below -1:', np.sum(meanR2<-1))
print('Max value:', np.max(meanR2))
print('Min value:', np.min(meanR2))
meanPearsonr=np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'],axis=1)
print('meanPearsonr for nodes with R2 under -1:', np.mean(meanPearsonr[meanR2<-1]))
print('Number of above-0 r-values, for nodes with R2 under -1:', np.sum(meanPearsonr[meanR2<-1]>0))
Number of nodes above 0.25: 186
Number of nodes below -1: 38
Max value: 0.728789308394122
Min value: -2.78849346592408
meanPearsonr for nodes with R2 under -1: 0.43727637855809354
Number of above-0 r-values, for nodes with R2 under -1: 38
In [30]:
np.round(np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['R2_nodewise_compthenavg_bycond'],axis=1),2)
Out[30]:
array([ 0.52,  0.51,  0.73,  0.76,  0.05, -0.16,  0.43,  0.29,  0.25,
        0.26,  0.23,  0.27,  0.8 ,  0.81,  0.8 ,  0.78,  0.42,  0.48,
        0.53,  0.6 ,  0.6 ,  0.59,  0.47,  0.51])
In [31]:
np.round(np.mean(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_nodewise_compthenavg_bycond'],axis=1),2)
Out[31]:
array([0.76, 0.76, 0.87, 0.88, 0.67, 0.64, 0.68, 0.59, 0.56, 0.57, 0.55,
       0.58, 0.9 , 0.9 , 0.9 , 0.89, 0.68, 0.72, 0.75, 0.79, 0.78, 0.78,
       0.72, 0.73])

Match multreg resting-state FC data to task-state FC data (amount of data, task timing regression, timing, etc)

In [25]:
## Calculating multreg REST FC, matched timing with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFCMatchedTiming_data'

restFCMatchedTiming_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFCMatchedTiming_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['restFCRegressionMatchTiming'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    restFCMatchedTiming_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(restdata[:,tasktiming])\n")
        file_python.write("    #Save multiple-regression FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegressionMatchTiming/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:20:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [32]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFCMatchedTiming_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC, time matched to cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[32]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [33]:
#Analysis of task vs. rest matrix
#NOTE: MULTREG FC MATRICES ARE NOT SYMMETRIC (CAN'T JUST USE UPPER TRIANGLE OF MATRIX)

restFCDataMultreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]
diffMat=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]-restFCDataMultreg
#Remove diagonal
blankmat=np.ones((nParcels,nParcels))
upperTmat=np.triu(blankmat,k=1)
lowerTmat=np.tril(blankmat,k=-1)
offdiagmat=(upperTmat+lowerTmat)>0
diffMat_flat=diffMat[offdiagmat]
print('Minimum value: ' + str(min(diffMat_flat)))
print('Maximum value: ' + str(max(diffMat_flat)))
print('Mean value: ' + str(np.mean(diffMat_flat)))
print('Stdev value: ' + str(np.std(diffMat_flat)))

#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
diffMat_allsubj=np.mean(taskFC_mreg_bycond_bysubj,axis=2)-np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=2)
diffMat_allsubj_offdiag=np.transpose([diffMat_allsubj[:,:,subjN][offdiagmat] for subjN in np.arange(nSubjs)])

nConnections_mreg=int(nParcels*(nParcels-1))
print('Percent connections POSITIVE:', str(100*np.sum(diffMat[offdiagmat]>0)/nConnections_mreg))
print('Percent connections NEGATIVE:', str(100*np.sum(diffMat[offdiagmat]<0)/nConnections_mreg))

#FDR
tstats = [stats.ttest_1samp(diffMat_allsubj_offdiag[connnum,:],0.0) for connnum in np.arange(nConnections_mreg)]
tvals=np.array([tstats[connnum][0] for connnum in np.arange(nConnections_mreg)])
pvals=[tstats[connnum][1] for connnum in np.arange(nConnections_mreg)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
fdr_sigvector=pvals_FDRcorrected<0.05
#Bonferroni
# bonferroni_sigvector=pvals<(0.05/nConnections)

#MaxT
maxt_output=actflow.tools.max_t(diffMat_allsubj_offdiag)
maxTThreshold = maxt_output[1]
maxt_sigvector=np.array(maxt_output[2])<0.05
tvals=maxt_output[0]
#maxt_sigvector=np.abs(tvals)>maxTThreshold

totalconn=np.sum(maxt_sigvector)
poscon=np.sum(maxt_sigvector*(tvals>0))
negconn=np.sum(maxt_sigvector*(tvals<0))
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', totalconn)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', poscon)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', negconn)

print('')
percenttotal=100*np.sum(maxt_sigvector)/nConnections_mreg
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nConnections_mreg
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nConnections_mreg
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', percenttotal)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

print('')
percenttotal=100*np.sum(fdr_sigvector)/nConnections_mreg
percentpos=100*np.sum(fdr_sigvector*(tvals>0))/nConnections_mreg
percentneg=100*np.sum(fdr_sigvector*(tvals<0))/nConnections_mreg
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) TOTAL:', percenttotal)
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)
Minimum value: -0.045031264424832594
Maximum value: 0.012759983045665307
Mean value: -0.0001498655451833239
Stdev value: 0.0023529188842574987
Percent connections POSITIVE: 51.34246363354998
Percent connections NEGATIVE: 48.65753636645002
# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 3233
# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 339
# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 2894

Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 2.5015475085112966
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 0.26230269266480966
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 2.239244815846487

Percent significant connections (FDR corrected for multiple comparisons, p<0.05) TOTAL: 11.067007118539152
Percent significant connections (FDR corrected for multiple comparisons, p<0.05) POSITIVE: 4.296657381615599
Percent significant connections (FDR corrected for multiple comparisons, p<0.05) NEGATIVE: 6.770349736923553
In [34]:
taskFCmeanMat_mreg=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)
restFCmeanMat_mreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)
upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat_mreg)[0],k=1)
resttask_mattcorr=np.corrcoef(taskFCmeanMat_mreg[upperTIndices_corrmat], restFCmeanMat_mreg[upperTIndices_corrmat])
print('Mean task to rest FC similarity, mreg: ', str(resttask_mattcorr))
Mean task to rest FC similarity, mreg:  [[1.         0.94269935]
 [0.94269935 1.        ]]
In [35]:
#Visualize FC matrix - mean task FC vs. rest FC
restFCDataMultreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]
diffMat=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]-restFCDataMultreg
FCMin=np.min(np.min(diffMat))
FCMax=np.max(np.max(diffMat))
ax = sns.heatmap(diffMat,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Task - rest multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meantaskFCVsRestFC_multreg_discovery176.pdf')
In [36]:
#Visualize FC matrix - mean rest FC
restFCDataMultreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]
FCMin=np.min(np.min(restFCDataMultreg))
FCMax=np.max(np.max(restFCDataMultreg))
ax = sns.heatmap(restFCDataMultreg,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Rest multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meanRestFC_tasktimematched_multreg_discovery176.pdf')
In [37]:
#Visualize FC matrix - mean task FC
FCMin=np.min(np.min(restFCDataMultreg))
FCMax=np.max(np.max(restFCDataMultreg))
mean_taskFC=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]
ax = sns.heatmap(mean_taskFC,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Mean task multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meanTaskFC_multreg_discovery176.pdf')
In [38]:
#Run actflow predictions, 24-condition rest multiple-regression FC, timing matched to task
print("==Activity flow mapping results, multiple-regression-based resting-state FC, TASK TIMING MATCHED, 24 task conditions==")
actflowOutput_restFCMRegMatchedTiming_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFCMatchedTiming_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, TASK TIMING MATCHED, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.46, t-value vs. 0: 81.20, p-value vs. 0: 7.582463385984874e-141
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.29
 
Mean MAE (mean absolute error) = 11.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.48, t-value vs. 0: 88.64, p-value vs. 0: 2.3949863511701083e-147
 
Mean % variance explained (R^2 score, coeff. of determination) = -1.71
 
Mean MAE (mean absolute error) = 11.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.46, t-value vs. 0: 98.74, p-value vs. 0: 2.1794438844591313e-155
By task condition:
Condition 1: r=0.42, t-value vs. 0: 38.96, p-value vs. 0: 3.6345455026234296e-88
Condition 2: r=0.39, t-value vs. 0: 34.90, p-value vs. 0: 9.38207461751452e-81
Condition 3: r=0.53, t-value vs. 0: 52.70, p-value vs. 0: 2.6472451492312733e-109
Condition 4: r=0.54, t-value vs. 0: 52.34, p-value vs. 0: 8.116528476051004e-109
Condition 5: r=0.32, t-value vs. 0: 39.49, p-value vs. 0: 4.3323507861748326e-89
Condition 6: r=0.28, t-value vs. 0: 36.41, p-value vs. 0: 1.4269394352839885e-83
Condition 7: r=0.54, t-value vs. 0: 50.29, p-value vs. 0: 5.801262919988021e-106
Condition 8: r=0.38, t-value vs. 0: 34.24, p-value vs. 0: 1.738374904840273e-79
Condition 9: r=0.37, t-value vs. 0: 38.01, p-value vs. 0: 1.7419691949730015e-86
Condition 10: r=0.37, t-value vs. 0: 34.38, p-value vs. 0: 9.503053057556186e-80
Condition 11: r=0.35, t-value vs. 0: 35.01, p-value vs. 0: 5.798773422120449e-81
Condition 12: r=0.37, t-value vs. 0: 36.15, p-value vs. 0: 4.2115609237273596e-83
Condition 13: r=0.57, t-value vs. 0: 62.57, p-value vs. 0: 1.074458798943357e-121
Condition 14: r=0.58, t-value vs. 0: 59.91, p-value vs. 0: 1.5329489265366815e-118
Condition 15: r=0.52, t-value vs. 0: 52.17, p-value vs. 0: 1.3973991788757388e-108
Condition 16: r=0.54, t-value vs. 0: 62.35, p-value vs. 0: 1.9293117285615545e-121
Condition 17: r=0.47, t-value vs. 0: 46.96, p-value vs. 0: 4.115201298425799e-101
Condition 18: r=0.41, t-value vs. 0: 41.10, p-value vs. 0: 7.957252107059074e-92
Condition 19: r=0.51, t-value vs. 0: 51.72, p-value vs. 0: 5.744628829975645e-108
Condition 20: r=0.52, t-value vs. 0: 50.88, p-value vs. 0: 8.549061179107095e-107
Condition 21: r=0.51, t-value vs. 0: 55.89, p-value vs. 0: 1.5979386390661192e-113
Condition 22: r=0.43, t-value vs. 0: 41.34, p-value vs. 0: 3.091893796068493e-92
Condition 23: r=0.48, t-value vs. 0: 47.15, p-value vs. 0: 2.1132461523723496e-101
Condition 24: r=0.49, t-value vs. 0: 49.28, p-value vs. 0: 1.583982579797085e-104
In [39]:
np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['R2_nodewise_compthenavg_bycond'])
Out[39]:
-0.2315675381664065
In [40]:
actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output'].keys()
Out[40]:
dict_keys(['fullcomp_compthenavg_output', 'corr_fullcomp_compthenavg', 'R2_fullcomp_compthenavg', 'maeAcc_fullcomp_compthenavg', 'tval_ActflowPredAcc_fullcomp', 'pval_ActflowPredAcc_fullcomp', 'conditionwise_compthenavg_output', 'corr_conditionwise_compthenavg_bynode', 'R2_conditionwise_compthenavg_bynode', 'mae_conditionwise_compthenavg_bynode', 'nodewise_compthenavg_output', 'corr_nodewise_compthenavg_bycond', 'R2_nodewise_compthenavg_bycond', 'mae_nodewise_compthenavg_bycond'])
In [41]:
np.round(np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['R2_nodewise_compthenavg_bycond'],axis=1),2)
Out[41]:
array([-0.17, -0.36, -0.05, -0.05, -2.32, -3.65,  0.21,  0.  , -0.01,
       -0.01, -0.03, -0.  ,  0.11,  0.13, -0.13, -0.07,  0.08,  0.02,
        0.16,  0.16,  0.15,  0.04,  0.11,  0.12])
In [42]:
np.round(np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['corr_nodewise_compthenavg_bycond'],axis=1),2)
Out[42]:
array([0.41, 0.38, 0.52, 0.53, 0.31, 0.27, 0.53, 0.37, 0.36, 0.37, 0.34,
       0.37, 0.56, 0.57, 0.51, 0.53, 0.46, 0.4 , 0.51, 0.51, 0.51, 0.42,
       0.47, 0.48])
In [43]:
print("===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTimingActflow = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True, avgthencomp_fixedeffects=True)
===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.46
Model2 mean Pearson r=0.76
R-value difference = -0.31
Model1 vs. Model2 T-value: -93.97, p-value: 1.081422683882069e-151
 
Model1 mean % predicted variance explained R^2=-0.29
Model2 mean % predicted variance explained R^2=0.51
R^2 difference = -0.80
 
Model1 mean MAE = 11.94
Model2 mean MAE = 7.47
Model1 vs. Model2 mean MAE difference = 4.46
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=0.75
R-value difference = -0.27
Model1 vs. Model2 T-value: -106.27, p-value: 7.016589435699863e-161
 
Model1 mean % predicted variance explained R^2=-1.71
Model2 mean % predicted variance explained R^2=0.01
R^2 difference = -1.72
 
Model1 mean MAE = 11.94
Model2 mean MAE = 7.47
Model1 vs. Model2 mean MAE difference = 4.46
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Model1 Mean Pearson r=0.88
Model2 Mean Pearson r=0.97
R-value difference = -0.09
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=4.38
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.46
Model2 mean Pearson r=0.77
R-value difference = -0.32
Model1 vs. Model2 T-value: -119.98, p-value: 5.547588707183724e-170
By task condition:
Condition 1: Model 1 r=0.42, Model 2 r=0.77, Model 1 vs. 2 R-value difference =-0.36, t-value Model1 vs. Model2: -44.39, p-value vs. 0: 3.578293328874243e-97
Condition 2: Model 1 r=0.39, Model 2 r=0.77, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -51.01, p-value vs. 0: 5.542031661754578e-107
Condition 3: Model 1 r=0.53, Model 2 r=0.88, Model 1 vs. 2 R-value difference =-0.35, t-value Model1 vs. Model2: -47.88, p-value vs. 0: 1.7513868999684393e-102
Condition 4: Model 1 r=0.54, Model 2 r=0.89, Model 1 vs. 2 R-value difference =-0.35, t-value Model1 vs. Model2: -53.80, p-value vs. 0: 8.827212463723221e-111
Condition 5: Model 1 r=0.32, Model 2 r=0.68, Model 1 vs. 2 R-value difference =-0.36, t-value Model1 vs. Model2: -41.66, p-value vs. 0: 9.121352614935714e-93
Condition 6: Model 1 r=0.28, Model 2 r=0.65, Model 1 vs. 2 R-value difference =-0.38, t-value Model1 vs. Model2: -37.15, p-value vs. 0: 6.130632148402384e-85
Condition 7: Model 1 r=0.54, Model 2 r=0.69, Model 1 vs. 2 R-value difference =-0.15, t-value Model1 vs. Model2: -21.58, p-value vs. 0: 3.4630089065335926e-51
Condition 8: Model 1 r=0.38, Model 2 r=0.60, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -22.33, p-value vs. 0: 4.1228817898540005e-53
Condition 9: Model 1 r=0.37, Model 2 r=0.57, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -22.46, p-value vs. 0: 1.956721894252041e-53
Condition 10: Model 1 r=0.37, Model 2 r=0.58, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -20.20, p-value vs. 0: 1.3000926023585591e-47
Condition 11: Model 1 r=0.35, Model 2 r=0.56, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -21.73, p-value vs. 0: 1.38634675751199e-51
Condition 12: Model 1 r=0.37, Model 2 r=0.59, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -23.65, p-value vs. 0: 2.179791635154206e-56
Condition 13: Model 1 r=0.57, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.34, t-value Model1 vs. Model2: -52.67, p-value vs. 0: 2.876090498539478e-109
Condition 14: Model 1 r=0.58, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.33, t-value Model1 vs. Model2: -61.05, p-value vs. 0: 6.522134502209333e-120
Condition 15: Model 1 r=0.52, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -58.47, p-value vs. 0: 8.883987257401398e-117
Condition 16: Model 1 r=0.54, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.37, t-value Model1 vs. Model2: -53.32, p-value vs. 0: 3.797115166806904e-110
Condition 17: Model 1 r=0.47, Model 2 r=0.69, Model 1 vs. 2 R-value difference =-0.23, t-value Model1 vs. Model2: -25.50, p-value vs. 0: 7.850900474277026e-61
Condition 18: Model 1 r=0.41, Model 2 r=0.73, Model 1 vs. 2 R-value difference =-0.32, t-value Model1 vs. Model2: -30.97, p-value vs. 0: 6.191997640207495e-73
Condition 19: Model 1 r=0.51, Model 2 r=0.76, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -29.32, p-value vs. 0: 1.994599229133286e-69
Condition 20: Model 1 r=0.52, Model 2 r=0.80, Model 1 vs. 2 R-value difference =-0.27, t-value Model1 vs. Model2: -35.60, p-value vs. 0: 4.5394730254553194e-82
Condition 21: Model 1 r=0.51, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.28, t-value Model1 vs. Model2: -34.67, p-value vs. 0: 2.6640268310842542e-80
Condition 22: Model 1 r=0.43, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.36, t-value Model1 vs. Model2: -41.35, p-value vs. 0: 3.0046185540873743e-92
Condition 23: Model 1 r=0.48, Model 2 r=0.73, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -27.83, p-value vs. 0: 3.674898508880899e-66
Condition 24: Model 1 r=0.49, Model 2 r=0.75, Model 1 vs. 2 R-value difference =-0.26, t-value Model1 vs. Model2: -25.68, p-value vs. 0: 2.944401407137691e-61
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean Model1 r=0.89
Mean Model2 r=0.99
Mean Model1 vs. Model2 R-value difference=-0.10
By task condition:
Condition 1: Model1 r=0.90, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
Condition 2: Model1 r=0.87, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.12
Condition 3: Model1 r=0.93, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.07
Condition 4: Model1 r=0.93, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.07
Condition 5: Model1 r=0.93, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.06
Condition 6: Model1 r=0.91, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.08
Condition 7: Model1 r=0.88, Model2 r=0.97, Model 1 vs. Model2 R-value difference=-0.09
Condition 8: Model1 r=0.78, Model2 r=0.94, Model 1 vs. Model2 R-value difference=-0.16
Condition 9: Model1 r=0.78, Model2 r=0.94, Model 1 vs. Model2 R-value difference=-0.16
Condition 10: Model1 r=0.74, Model2 r=0.92, Model 1 vs. Model2 R-value difference=-0.18
Condition 11: Model1 r=0.70, Model2 r=0.93, Model 1 vs. Model2 R-value difference=-0.23
Condition 12: Model1 r=0.81, Model2 r=0.95, Model 1 vs. Model2 R-value difference=-0.14
Condition 13: Model1 r=0.95, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.05
Condition 14: Model1 r=0.95, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.05
Condition 15: Model1 r=0.94, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.06
Condition 16: Model1 r=0.92, Model2 r=1.00, Model 1 vs. Model2 R-value difference=-0.07
Condition 17: Model1 r=0.89, Model2 r=0.98, Model 1 vs. Model2 R-value difference=-0.09
Condition 18: Model1 r=0.82, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.17
Condition 19: Model1 r=0.90, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
Condition 20: Model1 r=0.90, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
Condition 21: Model1 r=0.90, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
Condition 22: Model1 r=0.87, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.11
Condition 23: Model1 r=0.89, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.10
Condition 24: Model1 r=0.90, Model2 r=0.99, Model 1 vs. Model2 R-value difference=-0.09
In [44]:
print("===Compare Task multregFC to Task corrFC actflow prediction===")
model_compare_TaskMultRegFCVsTaskCorrFCMatchTimingActflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCCorr['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Task multregFC to Task corrFC actflow prediction===
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-44-21ad52138853> in <module>
      1 print("===Compare Task multregFC to Task corrFC actflow prediction===")
----> 2 model_compare_TaskMultRegFCVsTaskCorrFCMatchTimingActflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCCorr['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)

NameError: name 'actflowOutput_taskFCCorr' is not defined
In [ ]:
print("===Compare rest multregFC ALL DATA actflow predictions to Rest multregFC (matching timing) actflow prediction===")
model_compare_RestALLDATAMultRegFCVsRestMultRegFCMatchTimingActflow = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCMReg_bytask['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)

Visualizing prediction, rest and task multreg FC

In [ ]:
#Visualize predicted and actual activations
data1=stats.zscore(np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
data2=stats.zscore(np.mean(activations_bycond,axis=2)[netorder,:],axis=None)
data3=stats.zscore(np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'],axis=2)[netorder,:],axis=None)
datamin=min(np.array([min(data1.flatten()),min(data2.flatten()),min(data3.flatten())]))
datamax=max(np.array([max(data1.flatten()),max(data2.flatten()),max(data3.flatten())]))
fig, axn = plt.subplots(1, 3, sharex=True, sharey=True, figsize=[14,5])
cbar_ax = fig.add_axes([.90, .3, .015, .5])
ax1 = sns.heatmap(data1,ax=axn[0],center=0,cmap='seismic',cbar=False,yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax1.title.set_text('Pred: Rest multreg FC')
ax2 = sns.heatmap(data2,ax=axn[1],center=0,cmap='seismic',cbar=False,yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax2.title.set_text('Actual activations')
ax3 = sns.heatmap(data3,ax=axn[2],center=0,cmap='seismic',cbar=True,cbar_ax=cbar_ax,cbar_kws={'label': 'Activation (z-score)'},yticklabels=100,xticklabels=taskConditions,vmin=datamin,vmax=datamax)
ax3.title.set_text('Pred: Task multreg FC')
ax1.set_xticklabels(ax1.get_xmajorticklabels(), fontsize = 12)
ax2.set_xticklabels(ax2.get_xmajorticklabels(), fontsize = 12)
ax3.set_xticklabels(ax3.get_xmajorticklabels(), fontsize = 12)
fig.tight_layout(rect=[0, 0, .9, 1])
# fig=ax.get_figure()
fig.savefig(figuredir+'restTaskActflowPred_MultregFC_discovery176.pdf')
/projects/f_mc1689_1/AnalysisTools/anaconda3/lib/python3.7/site-packages/matplotlib/figure.py:2369: UserWarning: This figure includes Axes that are not compatible with tight_layout, so results might be incorrect.
  warnings.warn("This figure includes Axes that are not compatible "
In [ ]:
# from importlib import reload
# actflow.tools = reload(actflow.tools)

#Save condition-wise accuracy for each node to surface map
#taskmultregFC
#Compute p-value threshold, using FDR correction for multiple comparisons
ttestinputdata=np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode']),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'taskActflowPred_condwise_MultregFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)

#Max and min values
print('Maximum of map: ', np.max(datamat[datamat>0]))
print('Minimum of map: ', np.min(datamat[datamat>0]))

#restmultregFC
#Compute p-value threshold, using FDR correction for multiple comparisons
ttestinputdata=np.arctanh(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.arctanh(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode']),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'restMatchTaskActflowPred_condwise_MultregFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)

#Max and min values
print('Maximum of map: ', np.max(datamat[datamat>0]))
print('Minimum of map: ', np.min(datamat[datamat>0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 100.0
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_MultregFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_MultregFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskActflowPred_condwise_MultregFC_discovery176.dscalar.nii
Maximum of map:  0.9060707599707769
Minimum of map:  0.3180311085521397
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 97.77777777777777
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_MultregFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_MultregFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/restMatchTaskActflowPred_condwise_MultregFC_discovery176.dscalar.nii
Maximum of map:  0.8466878015099921
Minimum of map:  0.10454057438205779
In [ ]:
#Compare task-state and resting-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset=np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
restdataset=np.arctanh(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset,restdataset)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
#Visualize data
datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
#Apply threshold
datamat=np.multiply(datamat,maxt_sigvector)
datamat=datamat[:,np.newaxis]
outfilename=figuredir+'taskVSrestMatchTaskActflowPred_condwise_MultregFC_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=False)

#Max and min values
print('Maximum of map: ', np.max(datamat[datamat!=0]))
print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 100.0
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_MultregFC_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_MultregFC_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskVSrestMatchTaskActflowPred_condwise_MultregFC_discovery176.dscalar.nii
Maximum of map:  0.6253418153931153
Minimum of map:  0.16809816330777846
In [ ]:
#Assess relationship between task activation amplitudes and task-FC boost in actflow prediction accuracy
#Must average across conditions, because we need a separate estimate per region (condition-wise accuracy estimates)
#Spatial map correlations

print('Assessing relationship between task activation amplitudes and task-FC boost in actflow prediction accuracy')

taskdataset=np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
restdataset=np.arctanh(actflowOutput_restFCMRegMatchedTiming_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
task_vs_rest_predacc=np.subtract(taskdataset,restdataset)

actamplitudes_condavg=np.mean(activations_bycond,axis=1)

corrs_actamplitudes_taskVSrestpredacc=np.zeros(nSubjs)
scount = 0
for subj in subjNums:

    corrs_actamplitudes_taskVSrestpredacc[scount]=np.corrcoef(actamplitudes_condavg[:,scount],task_vs_rest_predacc[:,scount])[0,1]
    scount=scount+1
    
print('Mean correlation between activity amplitude & taskFC prediction accuracy boost:', np.mean(corrs_actamplitudes_taskVSrestpredacc))
tstats = stats.ttest_1samp(corrs_actamplitudes_taskVSrestpredacc,0.0)
print('t-stat:',tstats[0])
print('p-value:',tstats[1])

plt.scatter(actamplitudes_condavg,task_vs_rest_predacc)
Assessing relationship between task activation amplitudes and task-FC boost in actflow prediction accuracy
Mean correlation between activity amplitude & taskFC prediction accuracy boost: -0.01470131765029264
t-stat: -2.588188926012769
p-value: 0.010458995589780033
Out[ ]:
<matplotlib.collections.PathCollection at 0x7faca82accf8>
In [ ]:
#Visualize specific task activations

#WM:2back:faces
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,21,:],axis=1)
data2=np.mean(activations_bycond[:,21,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,21,:],axis=1)

outfilename=figuredir+'restFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data1=data1[:,np.newaxis]
actflow.tools.map_to_surface(data1,outfilename,fliphemispheres=False)

outfilename=figuredir+'actual_WM2backFacescond21_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data2=data2[:,np.newaxis]
actflow.tools.map_to_surface(data2,outfilename,fliphemispheres=False)

outfilename=figuredir+'taskFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data3=data3[:,np.newaxis]
actflow.tools.map_to_surface(data3,outfilename,fliphemispheres=False)


#language:story
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,4,:],axis=1)
data2=np.mean(activations_bycond[:,4,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,4,:],axis=1)

outfilename=figuredir+'restFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data1=data1[:,np.newaxis]
actflow.tools.map_to_surface(data1,outfilename,fliphemispheres=False)

outfilename=figuredir+'actual_LanguageStorycond4_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data2=data2[:,np.newaxis]
actflow.tools.map_to_surface(data2,outfilename,fliphemispheres=False)

outfilename=figuredir+'taskFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data3=data3[:,np.newaxis]
actflow.tools.map_to_surface(data3,outfilename,fliphemispheres=False)


#motor:lh
condnum=9
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
data2=np.mean(activations_bycond[:,condnum,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)

outfilename=figuredir+'restFC_ActflowPred_MultregFC_MotorLHcond9_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data1=data1[:,np.newaxis]
actflow.tools.map_to_surface(data1,outfilename,fliphemispheres=False)

outfilename=figuredir+'actual_MotorLHcond9_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data2=data2[:,np.newaxis]
actflow.tools.map_to_surface(data2,outfilename,fliphemispheres=False)

outfilename=figuredir+'taskFC_ActflowPred_MultregFC_MotorLHcond9_discovery176'
#sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
data3=data3[:,np.newaxis]
actflow.tools.map_to_surface(data3,outfilename,fliphemispheres=False)
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_WM2backFacescond21_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_WM2backFacescond21_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_WM2backFacescond21_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_WM2backFacescond21_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_LanguageStorycond4_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_LanguageStorycond4_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_LanguageStorycond4_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_LanguageStorycond4_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/restFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_MotorLHcond9_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_MotorLHcond9_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/actual_MotorLHcond9_discovery176.dscalar.nii
Command:
wb_command -cifti-convert -from-text /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.csv /projectsn/f_mc1689_1/TaskFCActflow/TaskFCActflow_release/ActflowToolbox/dependencies/ColeAnticevicNetPartition/CortexSubcortex_ColeAnticevic_NetPartition_wSubcorGSR_parcels_LR.dlabel.nii /projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.dscalar.nii -reset-scalars
CIFTI dscalar is output as:/projects/f_mc1689_1/TaskFCActflow//docs/figures/taskFC_ActflowPred_MultregFC_MotorLHcond9_discovery176.dscalar.nii
In [ ]:
#Calculate R^2 similarities
from sklearn.metrics import r2_score

print('==WM:2back:faces, average-then-compare==')
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,21,:],axis=1)
data2=np.mean(activations_bycond[:,21,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,21,:],axis=1)
r2_data1vs2=r2_score(data2,data1)
print('restFC pred-to-actual R2=',r2_data1vs2)
r2_data3vs2=r2_score(data2,data3)
print('taskFC pred-to-actual R2=',r2_data3vs2)

print('==language:story, average-then-compare==')
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,4,:],axis=1)
data2=np.mean(activations_bycond[:,4,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,4,:],axis=1)
r2_data1vs2=r2_score(data2,data1)
print('restFC pred-to-actual R2=',r2_data1vs2)
r2_data3vs2=r2_score(data2,data3)
print('taskFC pred-to-actual R2=',r2_data3vs2)

print('==motor:lh, average-then-compare==')
data1=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,10,:],axis=1)
data2=np.mean(activations_bycond[:,9,:],axis=1)
data3=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,10,:],axis=1)
r2_data1vs2=r2_score(data2,data1)
print('restFC pred-to-actual R2=',r2_data1vs2)
r2_data3vs2=r2_score(data2,data3)
print('taskFC pred-to-actual R2=',r2_data3vs2)
==WM:2back:faces, average-then-compare==
restFC pred-to-actual R2= 0.5774068310097404
taskFC pred-to-actual R2= 0.9135813259411614
==language:story, average-then-compare==
restFC pred-to-actual R2= 0.8444741279707699
taskFC pred-to-actual R2= 0.9750305651827432
==motor:lh, average-then-compare==
restFC pred-to-actual R2= 0.33809614205498995
taskFC pred-to-actual R2= 0.47232920324216676
In [ ]:
#Calculate R^2 similarities, each subject separately
from sklearn.metrics import r2_score

print('==WM:2back:faces, compare-then-average==')
r2_data1vs2=np.zeros(nSubjs)
r2_data3vs2=np.zeros(nSubjs)
scount=0
for subj in subjNums:
    data1=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,21,scount]
    data2=activations_bycond[:,21,scount]
    data3=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,21,scount]
    r2_data1vs2[scount]=r2_score(data2,data1)
    r2_data3vs2[scount]=r2_score(data2,data3)
    scount += 1
print('restFC pred-to-actual R2=',np.mean(r2_data1vs2))
print('taskFC pred-to-actual R2=',np.mean(r2_data3vs2))

print('==language:story, compare-then-average==')
condnum=4
r2_data1vs2=np.zeros(nSubjs)
r2_data3vs2=np.zeros(nSubjs)
scount=0
for subj in subjNums:
    data1=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,scount]
    data2=activations_bycond[:,condnum,scount]
    data3=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,scount]
    r2_data1vs2[scount]=r2_score(data2,data1)
    r2_data3vs2[scount]=r2_score(data2,data3)
    scount += 1
print('restFC pred-to-actual R2=',np.mean(r2_data1vs2))
print('taskFC pred-to-actual R2=',np.mean(r2_data3vs2))

print('==motor:lh, compare-then-average==')
condnum=9
r2_data1vs2=np.zeros(nSubjs)
r2_data3vs2=np.zeros(nSubjs)
scount=0
for subj in subjNums:
    data1=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,scount]
    data2=activations_bycond[:,condnum,scount]
    data3=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,scount]
    r2_data1vs2[scount]=r2_score(data2,data1)
    r2_data3vs2[scount]=r2_score(data2,data3)
    scount += 1
print('restFC pred-to-actual R2=',np.mean(r2_data1vs2))
print('taskFC pred-to-actual R2=',np.mean(r2_data3vs2))
==WM:2back:faces, compare-then-average==
restFC pred-to-actual R2= 0.040453644437620444
taskFC pred-to-actual R2= 0.5850466614803417
==language:story, compare-then-average==
restFC pred-to-actual R2= -2.3227705302603057
taskFC pred-to-actual R2= 0.046442244221180484
==motor:lh, compare-then-average==
restFC pred-to-actual R2= -0.005768505157595084
taskFC pred-to-actual R2= 0.2569560023554915
In [ ]:
#Plotting images in-line, WM:2back:faces

from wbplot import pscalar
import matplotlib.image as mpimg

condlabel='WM:2back:faces'
condnum=21

#RestFC predicted
inputdata=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('RestFC predicted '+condlabel)
plt.imshow(img)

#Actual
inputdata=np.mean(activations_bycond[:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('Actual '+condlabel)
plt.imshow(img)

#TaskFC predicted
inputdata=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('TaskFC predicted '+condlabel)
plt.imshow(img)
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Out[ ]:
<matplotlib.image.AxesImage at 0x7faf27cfc2e8>
In [ ]:
#Plotting images in-line, language:story

from wbplot import pscalar
import matplotlib.image as mpimg

condlabel='language:story'
condnum=4

#RestFC predicted
inputdata=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('RestFC predicted '+condlabel)
plt.imshow(img)

#Actual
inputdata=np.mean(activations_bycond[:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('Actual '+condlabel)
plt.imshow(img)

#TaskFC predicted
inputdata=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('TaskFC predicted '+condlabel)
plt.imshow(img)
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Out[ ]:
<matplotlib.image.AxesImage at 0x7faca82b5be0>
In [ ]:
#Plotting images in-line, motor:lh

from wbplot import pscalar
import matplotlib.image as mpimg

condlabel='motor:lh'
condnum=9

#RestFC predicted
inputdata=np.mean(actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('RestFC predicted '+condlabel)
plt.imshow(img)

#Actual
inputdata=np.mean(activations_bycond[:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('Actual '+condlabel)
plt.imshow(img)

#TaskFC predicted
inputdata=np.mean(actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'][:,condnum,:],axis=1)
#flip hemispheres
inputdata_flipped=np.zeros(np.shape(inputdata))
inputdata_flipped[0:180]=inputdata[180:360]
inputdata_flipped[180:360]=inputdata[0:180]
file_out="out.png"
pscalar(
        file_out=file_out,
        pscalars=inputdata_flipped,
        cmap='coolwarm',
        transparent=True)
img = mpimg.imread(file_out)
plt.figure()
plt.axis('off')
plt.title('TaskFC predicted '+condlabel)
plt.imshow(img)
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
pixdim[1,2,3] should be non-zero; setting 0 dims to 1
Out[ ]:
<matplotlib.image.AxesImage at 0x7faf260c8828>

Focus on negative connections and actflow increases

Negative LESIONED: All (unthresholded) task vs. rest FC changes

In [ ]:
#Analysis of task vs. rest matrix
restFCDataMultreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]
diffMat=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)[netorder,:][:,netorder]-restFCDataMultreg
#Remove diagonal
blankmat=np.ones((nParcels,nParcels))
upperTmat=np.triu(blankmat,k=1)
lowerTmat=np.tril(blankmat,k=-1)
offdiagmat=(upperTmat+lowerTmat)>0
diffMat_flat=diffMat[offdiagmat]
print('Minimum value: ' + str(min(diffMat_flat)))
print('Maximum value: ' + str(max(diffMat_flat)))
print('Mean value: ' + str(np.mean(diffMat_flat)))
print('Stdev value: ' + str(np.std(diffMat_flat)))
Minimum value: -0.045031264424832594
Maximum value: 0.012759983045665307
Mean value: -0.0001498655451833239
Stdev value: 0.0023529188842574987
In [ ]:
#Analyze number an magnitude of FC decreases vs. increases

diffMat_taskvsrest=taskFC_mreg_bycond_bysubj-restFCMatchedTiming_mreg_bycond_bysubj

diffMat_taskvsrest_offdiag=np.zeros((nConnections_mreg,nConds,nSubjs))
for taskcond_num in np.arange(nConds):
    
    scount = 0
    for subj in subjNums:

        diffMat_thissubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,scount]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,scount]
        diffMat_thissubj_offdiag=diffMat_thissubj[offdiagmat]
        diffMat_taskvsrest_offdiag[:,taskcond_num,scount]=diffMat_thissubj_offdiag
        scount=scount+1
        
nTotalConnections=nConnections_mreg*np.shape(diffMat_taskvsrest)[2]*np.shape(diffMat_taskvsrest)[3]
nFCReductions=np.sum(np.sum(np.sum(diffMat_taskvsrest_offdiag<0)))
nFCIncreases=np.sum(np.sum(np.sum(diffMat_taskvsrest_offdiag>0)))
print('Percent rest-to-task FC reductions:', 100*nFCReductions/nTotalConnections)
print('Percent rest-to-task FC increases:', 100*nFCIncreases/nTotalConnections)
print('Average amplitude of rest-to-task FC reductions:', np.sum(np.sum(np.sum(diffMat_taskvsrest_offdiag*(diffMat_taskvsrest_offdiag<0))))/nFCReductions)
print('Average amplitude of rest-to-task FC increases:', np.sum(np.sum(np.sum(diffMat_taskvsrest_offdiag*(diffMat_taskvsrest_offdiag>0))))/nFCIncreases)
print('Min amplitude of rest-to-task FC reductions:', np.min(np.min(np.min(diffMat_taskvsrest_offdiag*(diffMat_taskvsrest_offdiag<0))))/nFCReductions)
print('Max amplitude of rest-to-task FC increases:', np.max(np.max(np.max(diffMat_taskvsrest_offdiag*(diffMat_taskvsrest_offdiag>0))))/nFCIncreases)
Percent rest-to-task FC reductions: 50.19767479518959
Percent rest-to-task FC increases: 49.80232520481041
Average amplitude of rest-to-task FC reductions: -0.04609992414249636
Average amplitude of rest-to-task FC increases: 0.046164961901167124
Min amplitude of rest-to-task FC reductions: -1.7964395165992767e-08
Max amplitude of rest-to-task FC increases: 1.7810292552148355e-08
In [ ]:
#LESION rest-to-task FC decreases only

taskFC_signegonlyLESIONED_mreg_bycond_bysubj_offdiag=np.zeros((nConnections_mreg,nConds,nSubjs))
taskFC_signegonlyLESIONED_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
for taskcond_num in np.arange(nConds):
    
    diffMat_allsubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,:]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,:]
    diffMat_allsubj_offdiag=np.transpose([diffMat_allsubj[:,:,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    diffMat_subjmean_offdiag=np.mean(diffMat_allsubj_offdiag,axis=1)
    #mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0) * (maxt_sigvector_bycond[:,taskcond_num]>0))
    mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0))
    
    taskFC_mreg_bysubj_offdiag=np.transpose([taskFC_mreg_bycond_bysubj[:,:,taskcond_num,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    
    scount = 0
    for subj in subjNums:
        
        diffMat_thissubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,scount]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,scount]
        diffMat_thissubj_offdiag=diffMat_thissubj[offdiagmat]
        mask_negonlyLESIONED_thissubj_offdiag=1-((diffMat_thissubj_offdiag<0))
        
        taskFC_signegonlyLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount]=taskFC_mreg_bysubj_offdiag[:,scount]*mask_negonlyLESIONED_thissubj_offdiag
        matrix_temp=np.zeros((nParcels,nParcels))
        matrix_temp[offdiagmat]=taskFC_signegonlyLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount].copy()
        taskFC_signegonlyLESIONED_mreg_bycond_bysubj[:,:,taskcond_num,scount]=matrix_temp.copy()
        scount=scount+1
In [ ]:
#Calculate task-state FC actflow with task-reduced FC values LESIONED

#Run actflow predictions, 24-condition task multiple-regression FC, task-reduced FC values only
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, task-reduced FC values LESIONED==")
actflowOutput_taskFCMReg_bycond_signegonlyLESIONED = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_signegonlyLESIONED_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, task-reduced FC values LESIONED==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.48, t-value vs. 0: 77.33, p-value vs. 0: 3.1283836681798124e-137
 
Mean % variance explained (R^2 score, coeff. of determination) = -7.96
 
Mean MAE (mean absolute error) = 22.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.54, t-value vs. 0: 74.64, p-value vs. 0: 1.2929939468065384e-134
 
Mean % variance explained (R^2 score, coeff. of determination) = -18.74
 
Mean MAE (mean absolute error) = 22.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.57, t-value vs. 0: 162.87, p-value vs. 0: 5.30744930344903e-193
By task condition:
Condition 1: r=0.52, t-value vs. 0: 43.92, p-value vs. 0: 1.994209526457592e-96
Condition 2: r=0.49, t-value vs. 0: 44.45, p-value vs. 0: 2.8529128391099616e-97
Condition 3: r=0.68, t-value vs. 0: 80.22, p-value vs. 0: 6.057224099645276e-140
Condition 4: r=0.66, t-value vs. 0: 76.51, p-value vs. 0: 1.9094333394392105e-136
Condition 5: r=0.29, t-value vs. 0: 32.12, p-value vs. 0: 2.7643328625015016e-75
Condition 6: r=0.28, t-value vs. 0: 27.72, p-value vs. 0: 6.512765054293657e-66
Condition 7: r=0.63, t-value vs. 0: 72.87, p-value vs. 0: 7.470563339454445e-133
Condition 8: r=0.49, t-value vs. 0: 42.65, p-value vs. 0: 2.1986967819602434e-94
Condition 9: r=0.43, t-value vs. 0: 39.14, p-value vs. 0: 1.7782402129312676e-88
Condition 10: r=0.44, t-value vs. 0: 40.96, p-value vs. 0: 1.352140407176542e-91
Condition 11: r=0.42, t-value vs. 0: 35.65, p-value vs. 0: 3.598265942431524e-82
Condition 12: r=0.45, t-value vs. 0: 36.14, p-value vs. 0: 4.475358115945962e-83
Condition 13: r=0.67, t-value vs. 0: 70.26, p-value vs. 0: 3.5585537420229857e-130
Condition 14: r=0.68, t-value vs. 0: 70.21, p-value vs. 0: 4.08796393443738e-130
Condition 15: r=0.62, t-value vs. 0: 65.67, p-value vs. 0: 3.1668624920067715e-125
Condition 16: r=0.65, t-value vs. 0: 52.61, p-value vs. 0: 3.521302340492236e-109
Condition 17: r=0.56, t-value vs. 0: 46.34, p-value vs. 0: 3.518906805776673e-100
Condition 18: r=0.58, t-value vs. 0: 50.69, p-value vs. 0: 1.5625396290515078e-106
Condition 19: r=0.71, t-value vs. 0: 59.93, p-value vs. 0: 1.4603676408760955e-118
Condition 20: r=0.70, t-value vs. 0: 76.65, p-value vs. 0: 1.3938589991542436e-136
Condition 21: r=0.66, t-value vs. 0: 71.87, p-value vs. 0: 7.770749906032107e-132
Condition 22: r=0.64, t-value vs. 0: 57.99, p-value vs. 0: 3.506993193804825e-116
Condition 23: r=0.67, t-value vs. 0: 63.79, p-value vs. 0: 4.177732205606099e-123
Condition 24: r=0.59, t-value vs. 0: 47.92, p-value vs. 0: 1.5244890796471248e-102
In [ ]:
#Also threshold resting-state FC to match increase-LESIONED task FC (same connections as above, but now applied to rest FC)
restFC_signegLESIONED_mreg_bycond_bysubj_offdiag=np.zeros((nConnections_mreg,nConds,nSubjs))
restFC_signegLESIONED_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
for taskcond_num in np.arange(nConds):
    
    diffMat_allsubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,:]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,:]
    diffMat_allsubj_offdiag=np.transpose([diffMat_allsubj[:,:,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    diffMat_subjmean_offdiag=np.mean(diffMat_allsubj_offdiag,axis=1)
    #mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0) * (maxt_sigvector_bycond[:,taskcond_num]>0))
    mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0))
    
    restFC_mreg_bysubj_offdiag=np.transpose([restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    
    scount = 0
    for subj in subjNums:
        
        diffMat_thissubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,scount]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,scount]
        diffMat_thissubj_offdiag=diffMat_thissubj[offdiagmat]
        mask_negonlyLESIONED_thissubj_offdiag=1-((diffMat_thissubj_offdiag<0))
        
        restFC_signegLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount]=restFC_mreg_bysubj_offdiag[:,scount]*mask_negonlyLESIONED_thissubj_offdiag
        matrix_temp=np.zeros((nParcels,nParcels))
        matrix_temp[offdiagmat]=restFC_signegLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount].copy()
        restFC_signegLESIONED_mreg_bycond_bysubj[:,:,taskcond_num,scount]=matrix_temp.copy()
        scount=scount+1
In [ ]:
#Calculate resting-state FC actflow using task-reduced FC values LESIONED

#Run actflow predictions, 24-condition task multiple-regression FC, task-reduced FC values only
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions, task-reduced FC values LESIONED==")
actflowOutput_restFCMReg_bycond_signegonlyLESIONED = actflow.actflowcomp.actflowtest(activations_bycond, restFC_signegLESIONED_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions, task-reduced FC values LESIONED==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = -0.13, t-value vs. 0: -11.12, p-value vs. 0: 4.630721960124088e-22
 
Mean % variance explained (R^2 score, coeff. of determination) = -14.24
 
Mean MAE (mean absolute error) = 28.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = -0.24, t-value vs. 0: -17.40, p-value vs. 0: 5.256401243336979e-40
 
Mean % variance explained (R^2 score, coeff. of determination) = -31.25
 
Mean MAE (mean absolute error) = 28.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.34, t-value vs. 0: 71.32, p-value vs. 0: 2.826280359871596e-131
By task condition:
Condition 1: r=0.29, t-value vs. 0: 28.51, p-value vs. 0: 1.1522721068464821e-67
Condition 2: r=0.23, t-value vs. 0: 18.74, p-value vs. 0: 1.0542077495693256e-43
Condition 3: r=0.42, t-value vs. 0: 32.31, p-value vs. 0: 1.1213545287498555e-75
Condition 4: r=0.37, t-value vs. 0: 24.79, p-value vs. 0: 3.855069985648986e-59
Condition 5: r=0.01, t-value vs. 0: 0.47, p-value vs. 0: 0.6403239448536198
Condition 6: r=-0.05, t-value vs. 0: -3.80, p-value vs. 0: 0.00019843535003538653
Condition 7: r=0.47, t-value vs. 0: 45.67, p-value vs. 0: 3.677453594323054e-99
Condition 8: r=0.24, t-value vs. 0: 20.33, p-value vs. 0: 5.993198714205483e-48
Condition 9: r=0.23, t-value vs. 0: 18.49, p-value vs. 0: 5.305244874805356e-43
Condition 10: r=0.23, t-value vs. 0: 17.46, p-value vs. 0: 3.433082702744151e-40
Condition 11: r=0.20, t-value vs. 0: 16.97, p-value vs. 0: 7.895123786085252e-39
Condition 12: r=0.24, t-value vs. 0: 20.98, p-value vs. 0: 1.2066054918377047e-49
Condition 13: r=0.45, t-value vs. 0: 37.96, p-value vs. 0: 2.183328682946932e-86
Condition 14: r=0.46, t-value vs. 0: 46.13, p-value vs. 0: 7.175585255396071e-100
Condition 15: r=0.48, t-value vs. 0: 49.48, p-value vs. 0: 8.139302963177162e-105
Condition 16: r=0.51, t-value vs. 0: 61.30, p-value vs. 0: 3.29243823484816e-120
Condition 17: r=0.40, t-value vs. 0: 39.00, p-value vs. 0: 3.1767839217521755e-88
Condition 18: r=0.35, t-value vs. 0: 27.82, p-value vs. 0: 3.757869765400263e-66
Condition 19: r=0.46, t-value vs. 0: 45.86, p-value vs. 0: 1.8625390992130385e-99
Condition 20: r=0.44, t-value vs. 0: 42.03, p-value vs. 0: 2.2144976395203918e-93
Condition 21: r=0.40, t-value vs. 0: 27.69, p-value vs. 0: 7.377948295980048e-66
Condition 22: r=0.34, t-value vs. 0: 23.82, p-value vs. 0: 8.438394877315622e-57
Condition 23: r=0.44, t-value vs. 0: 44.01, p-value vs. 0: 1.4296421699686649e-96
Condition 24: r=0.38, t-value vs. 0: 32.49, p-value vs. 0: 4.9003973063687476e-76
In [ ]:
print("===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, task-reduced FC values LESIONED===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTimingActflow_signegLESIONED = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond_signegonlyLESIONED['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCMReg_bycond_signegonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, task-reduced FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=-0.13
R-value difference = 0.61
Model1 vs. Model2 T-value: 48.21, p-value: 5.698631252050015e-103
 
Model1 mean % predicted variance explained R^2=-7.96
Model2 mean % predicted variance explained R^2=-14.24
R^2 difference = 6.28
 
Model1 mean MAE = 22.53
Model2 mean MAE = 28.53
Model1 vs. Model2 mean MAE difference = -6.00
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.54
Model2 mean Pearson r=-0.24
R-value difference = 0.78
Model1 vs. Model2 T-value: 41.69, p-value: 8.187976419093584e-93
 
Model1 mean % predicted variance explained R^2=-18.74
Model2 mean % predicted variance explained R^2=-31.25
R^2 difference = 12.50
 
Model1 mean MAE = 22.53
Model2 mean MAE = 28.53
Model1 vs. Model2 mean MAE difference = -6.00
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.57
Model2 mean Pearson r=0.34
R-value difference = 0.24
Model1 vs. Model2 T-value: 65.37, p-value: 6.851165720941572e-125
By task condition:
Condition 1: Model 1 r=0.52, Model 2 r=0.29, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 20.94, p-value vs. 0: 1.5165525561064967e-49
Condition 2: Model 1 r=0.49, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 19.73, p-value vs. 0: 2.327389717700336e-46
Condition 3: Model 1 r=0.68, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 27.57, p-value vs. 0: 1.3508894524983578e-65
Condition 4: Model 1 r=0.66, Model 2 r=0.37, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 28.40, p-value vs. 0: 1.9549744486082194e-67
Condition 5: Model 1 r=0.29, Model 2 r=0.01, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 17.42, p-value vs. 0: 4.556158733126533e-40
Condition 6: Model 1 r=0.28, Model 2 r=-0.05, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 16.92, p-value vs. 0: 1.12921226951246e-38
Condition 7: Model 1 r=0.63, Model 2 r=0.47, Model 1 vs. 2 R-value difference =0.16, t-value Model1 vs. Model2: 19.51, p-value vs. 0: 8.889219885062898e-46
Condition 8: Model 1 r=0.49, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 22.31, p-value vs. 0: 4.757942914892745e-53
Condition 9: Model 1 r=0.43, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.20, t-value Model1 vs. Model2: 17.38, p-value vs. 0: 5.92293782104987e-40
Condition 10: Model 1 r=0.44, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 17.47, p-value vs. 0: 3.3480995349081736e-40
Condition 11: Model 1 r=0.42, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 17.37, p-value vs. 0: 6.179735280734459e-40
Condition 12: Model 1 r=0.45, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.21, t-value Model1 vs. Model2: 16.90, p-value vs. 0: 1.271201601066473e-38
Condition 13: Model 1 r=0.67, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 20.85, p-value vs. 0: 2.6255771206209956e-49
Condition 14: Model 1 r=0.68, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 28.79, p-value vs. 0: 2.695530718546299e-68
Condition 15: Model 1 r=0.62, Model 2 r=0.48, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 14.20, p-value vs. 0: 6.1062829013084825e-31
Condition 16: Model 1 r=0.65, Model 2 r=0.51, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 13.38, p-value vs. 0: 1.3807627672602795e-28
Condition 17: Model 1 r=0.56, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.16, t-value Model1 vs. Model2: 15.73, p-value vs. 0: 2.515399403226083e-35
Condition 18: Model 1 r=0.58, Model 2 r=0.35, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 20.15, p-value vs. 0: 1.814849884044437e-47
Condition 19: Model 1 r=0.71, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 29.09, p-value vs. 0: 6.198343837641263e-69
Condition 20: Model 1 r=0.70, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 31.33, p-value vs. 0: 1.1337893212478949e-73
Condition 21: Model 1 r=0.66, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 22.67, p-value vs. 0: 5.973325792543747e-54
Condition 22: Model 1 r=0.64, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 23.58, p-value vs. 0: 3.2524559391872238e-56
Condition 23: Model 1 r=0.67, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 25.90, p-value vs. 0: 8.935083642016965e-62
Condition 24: Model 1 r=0.59, Model 2 r=0.38, Model 1 vs. 2 R-value difference =0.21, t-value Model1 vs. Model2: 18.28, p-value vs. 0: 1.971897920359802e-42
In [ ]:
print("===Compare regular Task multregFC actflow predictions to Task multregFC actflow predictions with task-reduced FC values LESIONED===")
model_compare_TaskMultRegFCVsTaskMultRegFCsignegLESIONED_Actflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_bycond_signegonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare regular Task multregFC actflow predictions to Task multregFC actflow predictions with task-reduced FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.76
Model2 mean Pearson r=0.48
R-value difference = 0.29
Model1 vs. Model2 T-value: 59.08, p-value: 1.5700727407899031e-117
 
Model1 mean % predicted variance explained R^2=0.51
Model2 mean % predicted variance explained R^2=-7.96
R^2 difference = 8.47
 
Model1 mean MAE = 7.47
Model2 mean MAE = 22.53
Model1 vs. Model2 mean MAE difference = -15.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.75
Model2 mean Pearson r=0.54
R-value difference = 0.21
Model1 vs. Model2 T-value: 52.09, p-value: 1.7635716859925387e-108
 
Model1 mean % predicted variance explained R^2=0.01
Model2 mean % predicted variance explained R^2=-18.74
R^2 difference = 18.76
 
Model1 mean MAE = 7.47
Model2 mean MAE = 22.53
Model1 vs. Model2 mean MAE difference = -15.06
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.77
Model2 mean Pearson r=0.57
R-value difference = 0.20
Model1 vs. Model2 T-value: 84.55, p-value: 7.74027102317158e-144
By task condition:
Condition 1: Model 1 r=0.77, Model 2 r=0.52, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 31.13, p-value vs. 0: 2.8739390017748243e-73
Condition 2: Model 1 r=0.77, Model 2 r=0.49, Model 1 vs. 2 R-value difference =0.28, t-value Model1 vs. Model2: 33.42, p-value vs. 0: 6.96017588662167e-78
Condition 3: Model 1 r=0.88, Model 2 r=0.68, Model 1 vs. 2 R-value difference =0.20, t-value Model1 vs. Model2: 32.78, p-value vs. 0: 1.2694630125308438e-76
Condition 4: Model 1 r=0.89, Model 2 r=0.66, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 37.76, p-value vs. 0: 4.826916051523798e-86
Condition 5: Model 1 r=0.68, Model 2 r=0.29, Model 1 vs. 2 R-value difference =0.39, t-value Model1 vs. Model2: 38.59, p-value vs. 0: 1.6259574459852856e-87
Condition 6: Model 1 r=0.65, Model 2 r=0.28, Model 1 vs. 2 R-value difference =0.38, t-value Model1 vs. Model2: 34.24, p-value vs. 0: 1.777865398966511e-79
Condition 7: Model 1 r=0.69, Model 2 r=0.63, Model 1 vs. 2 R-value difference =0.06, t-value Model1 vs. Model2: 8.40, p-value vs. 0: 1.5173544202989116e-14
Condition 8: Model 1 r=0.60, Model 2 r=0.49, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 17.75, p-value vs. 0: 5.674981368337897e-41
Condition 9: Model 1 r=0.57, Model 2 r=0.43, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 19.47, p-value vs. 0: 1.175095850202096e-45
Condition 10: Model 1 r=0.58, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 16.18, p-value vs. 0: 1.3321675031701784e-36
Condition 11: Model 1 r=0.56, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 17.58, p-value vs. 0: 1.6108315176126786e-40
Condition 12: Model 1 r=0.59, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 17.88, p-value vs. 0: 2.4286787964031385e-41
Condition 13: Model 1 r=0.91, Model 2 r=0.67, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 46.51, p-value vs. 0: 1.899108412581654e-100
Condition 14: Model 1 r=0.91, Model 2 r=0.68, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 53.41, p-value vs. 0: 2.8525847372865967e-110
Condition 15: Model 1 r=0.91, Model 2 r=0.62, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 48.01, p-value vs. 0: 1.1160140311475516e-102
Condition 16: Model 1 r=0.90, Model 2 r=0.65, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 38.36, p-value vs. 0: 4.232671716765663e-87
Condition 17: Model 1 r=0.69, Model 2 r=0.56, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 16.89, p-value vs. 0: 1.4001305856444273e-38
Condition 18: Model 1 r=0.73, Model 2 r=0.58, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 23.06, p-value vs. 0: 6.1223040514874745e-55
Condition 19: Model 1 r=0.76, Model 2 r=0.71, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 7.42, p-value vs. 0: 4.798878505068425e-12
Condition 20: Model 1 r=0.80, Model 2 r=0.70, Model 1 vs. 2 R-value difference =0.09, t-value Model1 vs. Model2: 19.16, p-value vs. 0: 7.668194124541196e-45
Condition 21: Model 1 r=0.79, Model 2 r=0.66, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 22.95, p-value vs. 0: 1.1758575478729929e-54
Condition 22: Model 1 r=0.79, Model 2 r=0.64, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 30.86, p-value vs. 0: 1.0537535454382964e-72
Condition 23: Model 1 r=0.73, Model 2 r=0.67, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 7.19, p-value vs. 0: 1.8584776981008408e-11
Condition 24: Model 1 r=0.75, Model 2 r=0.59, Model 1 vs. 2 R-value difference =0.16, t-value Model1 vs. Model2: 24.90, p-value vs. 0: 2.0729146329431636e-59
In [ ]:
print("===Compare regular Rest time-matched multregFC actflow predictions to Rest time-matched multregFC actflow predictions with task-reduced FC values LESIONED===")
model_compare_RestMultRegFCVsRestMultRegFCsignegLESIONED_Actflow = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCMReg_bycond_signegonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare regular Rest time-matched multregFC actflow predictions to Rest time-matched multregFC actflow predictions with task-reduced FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.46
Model2 mean Pearson r=-0.13
R-value difference = 0.59
Model1 vs. Model2 T-value: 44.88, p-value: 6.179237662923991e-98
 
Model1 mean % predicted variance explained R^2=-0.29
Model2 mean % predicted variance explained R^2=-14.24
R^2 difference = 13.95
 
Model1 mean MAE = 11.94
Model2 mean MAE = 28.53
Model1 vs. Model2 mean MAE difference = -16.59
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=-0.24
R-value difference = 0.72
Model1 vs. Model2 T-value: 43.55, p-value: 7.82396563267078e-96
 
Model1 mean % predicted variance explained R^2=-1.71
Model2 mean % predicted variance explained R^2=-31.25
R^2 difference = 29.54
 
Model1 mean MAE = 11.94
Model2 mean MAE = 28.53
Model1 vs. Model2 mean MAE difference = -16.59
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.46
Model2 mean Pearson r=0.34
R-value difference = 0.12
Model1 vs. Model2 T-value: 34.23, p-value: 1.8389644220038306e-79
By task condition:
Condition 1: Model 1 r=0.42, Model 2 r=0.29, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 13.86, p-value vs. 0: 5.672325484359043e-30
Condition 2: Model 1 r=0.39, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.16, t-value Model1 vs. Model2: 14.14, p-value vs. 0: 8.849792631969897e-31
Condition 3: Model 1 r=0.53, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 11.16, p-value vs. 0: 3.600617648180731e-22
Condition 4: Model 1 r=0.54, Model 2 r=0.37, Model 1 vs. 2 R-value difference =0.17, t-value Model1 vs. Model2: 14.09, p-value vs. 0: 1.234717001020976e-30
Condition 5: Model 1 r=0.32, Model 2 r=0.01, Model 1 vs. 2 R-value difference =0.31, t-value Model1 vs. Model2: 23.09, p-value vs. 0: 5.300095621448065e-55
Condition 6: Model 1 r=0.28, Model 2 r=-0.05, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 22.92, p-value vs. 0: 1.3545869070823665e-54
Condition 7: Model 1 r=0.54, Model 2 r=0.47, Model 1 vs. 2 R-value difference =0.07, t-value Model1 vs. Model2: 10.54, p-value vs. 0: 1.9761359284599057e-20
Condition 8: Model 1 r=0.38, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 13.27, p-value vs. 0: 2.9828625453428412e-28
Condition 9: Model 1 r=0.37, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 15.15, p-value vs. 0: 1.1135021034461194e-33
Condition 10: Model 1 r=0.37, Model 2 r=0.23, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 13.56, p-value vs. 0: 4.333346892529993e-29
Condition 11: Model 1 r=0.35, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 13.22, p-value vs. 0: 4.021265186216028e-28
Condition 12: Model 1 r=0.37, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 13.29, p-value vs. 0: 2.5526633042154266e-28
Condition 13: Model 1 r=0.57, Model 2 r=0.45, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 14.41, p-value vs. 0: 1.5045881096266091e-31
Condition 14: Model 1 r=0.58, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 16.13, p-value vs. 0: 1.894109347740076e-36
Condition 15: Model 1 r=0.52, Model 2 r=0.48, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 5.79, p-value vs. 0: 3.2674764301710155e-08
Condition 16: Model 1 r=0.54, Model 2 r=0.51, Model 1 vs. 2 R-value difference =0.03, t-value Model1 vs. Model2: 3.24, p-value vs. 0: 0.0014131309023476697
Condition 17: Model 1 r=0.47, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.07, t-value Model1 vs. Model2: 9.66, p-value vs. 0: 5.787638153928808e-18
Condition 18: Model 1 r=0.41, Model 2 r=0.35, Model 1 vs. 2 R-value difference =0.06, t-value Model1 vs. Model2: 7.14, p-value vs. 0: 2.4493932618416546e-11
Condition 19: Model 1 r=0.51, Model 2 r=0.46, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 8.14, p-value vs. 0: 7.069502580025937e-14
Condition 20: Model 1 r=0.52, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.08, t-value Model1 vs. Model2: 11.05, p-value vs. 0: 7.0807553583635e-22
Condition 21: Model 1 r=0.51, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 9.72, p-value vs. 0: 3.94630733525552e-18
Condition 22: Model 1 r=0.43, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.08, t-value Model1 vs. Model2: 7.38, p-value vs. 0: 6.245024730969188e-12
Condition 23: Model 1 r=0.48, Model 2 r=0.44, Model 1 vs. 2 R-value difference =0.04, t-value Model1 vs. Model2: 5.37, p-value vs. 0: 2.521990400377799e-07
Condition 24: Model 1 r=0.49, Model 2 r=0.38, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 11.71, p-value vs. 0: 9.16070810585463e-24
In [ ]:
#Calculate actflows manually
actflows_taskFC_negLESIONED=np.zeros((nParcels,nParcels,nConds,nSubjs))
scount = 0
for subj in subjNums:
    for cond in np.arange(24):
        
        for heldOutRegion in np.arange(nParcels):
            otherRegions=list(range(nParcels))
            otherRegions.remove(heldOutRegion)
            actflows_taskFC_negLESIONED[heldOutRegion,otherRegions,cond,scount]=activations_bycond[otherRegions,cond,scount]*taskFC_signegonlyLESIONED_mreg_bycond_bysubj[heldOutRegion,otherRegions,cond,scount]
        
    scount=scount+1
In [ ]:
#Quantify percentage of positive actflows (i.e., actflow increases given that rest is baseline) with FC decreases lesioned

actflows_taskFC_negLESIONED_flat=actflows_taskFC_negLESIONED.flatten()

num_pos_actflows=np.sum(actflows_taskFC_negLESIONED_flat>0)
num_actflows_total=np.shape(actflows_taskFC_negLESIONED_flat)[0]
print("Percentage of positive actflows, with all rest-to-task FC reductions lesioned:", 100*num_pos_actflows/num_actflows_total)

num_zero_actflows=np.sum(actflows_taskFC_negLESIONED_flat==0)
print("Percentage of 0-flow actflows, with all rest-to-task FC reductions lesioned:", 100*num_zero_actflows/num_actflows_total)

num_neg_actflows=np.sum(actflows_taskFC_negLESIONED_flat<0)
print("Percentage of negative actflows, with all rest-to-task FC reductions lesioned:", 100*num_neg_actflows/num_actflows_total)

num_pos_actflows=np.sum(actflows_taskFC_negLESIONED_flat>0)
num_actflows_totalnonzero=np.sum(actflows_taskFC_negLESIONED_flat!=0)
print("Percentage of positive actflows among non-zeros, with all rest-to-task FC reductions lesioned:", 100*num_pos_actflows/num_actflows_totalnonzero)

num_neg_actflows=np.sum(actflows_taskFC_negLESIONED_flat<0)
print("Percentage of negative actflows among non-zeros, with all rest-to-task FC reductions lesioned:", 100*num_neg_actflows/num_actflows_totalnonzero)
Percentage of positive actflows, with all rest-to-task FC reductions lesioned: 24.90765127402497
Percentage of 0-flow actflows, with all rest-to-task FC reductions lesioned: 50.336014587425176
Percentage of negative actflows, with all rest-to-task FC reductions lesioned: 24.75633413854985
Percentage of positive actflows among non-zeros, with all rest-to-task FC reductions lesioned: 50.152340910841204
Percentage of negative actflows among non-zeros, with all rest-to-task FC reductions lesioned: 49.847659089158796
In [ ]:
#Visualize actflow matrix
ax = sns.heatmap(np.mean(actflows_taskFC_negLESIONED[netorder,:,22,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Activity flow (act. * FC)'})
ax.figure.suptitle('Actflows (actflows_taskFC_incrthreshed), cond 23')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]

Positive LESIONED: All (unthresholded) task vs. rest FC changes

In [ ]:
#LESION rest-to-task FC increases only

taskFC_posonlyLESIONED_mreg_bycond_bysubj_offdiag=np.zeros((nConnections_mreg,nConds,nSubjs))
taskFC_posonlyLESIONED_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
for taskcond_num in np.arange(nConds):
    
    diffMat_allsubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,:]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,:]
    diffMat_allsubj_offdiag=np.transpose([diffMat_allsubj[:,:,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    diffMat_subjmean_offdiag=np.mean(diffMat_allsubj_offdiag,axis=1)
    #mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0) * (maxt_sigvector_bycond[:,taskcond_num]>0))
    mask_posonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag>0))
    
    taskFC_mreg_bysubj_offdiag=np.transpose([taskFC_mreg_bycond_bysubj[:,:,taskcond_num,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    
    scount = 0
    for subj in subjNums:
        
        diffMat_thissubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,scount]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,scount]
        diffMat_thissubj_offdiag=diffMat_thissubj[offdiagmat]
        mask_posonlyLESIONED_thissubj_offdiag=1-((diffMat_thissubj_offdiag>0))
        
        taskFC_posonlyLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount]=taskFC_mreg_bysubj_offdiag[:,scount]*mask_posonlyLESIONED_thissubj_offdiag
        matrix_temp=np.zeros((nParcels,nParcels))
        matrix_temp[offdiagmat]=taskFC_posonlyLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount].copy()
        taskFC_posonlyLESIONED_mreg_bycond_bysubj[:,:,taskcond_num,scount]=matrix_temp.copy()
        scount=scount+1
In [ ]:
#Calculate task-state FC actflow with task-increased FC values LESIONED

#Run actflow predictions, 24-condition task multiple-regression FC, task-reduced FC values only
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, task-increased FC values LESIONED==")
actflowOutput_taskFCMReg_bycond_posonlyLESIONED = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_posonlyLESIONED_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, task-increased FC values LESIONED==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = -0.20, t-value vs. 0: -19.06, p-value vs. 0: 1.4588303457772635e-44
 
Mean % variance explained (R^2 score, coeff. of determination) = -10.24
 
Mean MAE (mean absolute error) = 26.91
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = -0.30, t-value vs. 0: -22.14, p-value vs. 0: 1.2505578664478705e-52
 
Mean % variance explained (R^2 score, coeff. of determination) = -21.54
 
Mean MAE (mean absolute error) = 26.91
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.26, t-value vs. 0: 33.53, p-value vs. 0: 4.234327388119742e-78
By task condition:
Condition 1: r=0.42, t-value vs. 0: 29.33, p-value vs. 0: 1.8902114768006556e-69
Condition 2: r=0.40, t-value vs. 0: 22.06, p-value vs. 0: 1.9866712202752526e-52
Condition 3: r=0.22, t-value vs. 0: 7.81, p-value vs. 0: 5.007889154837708e-13
Condition 4: r=0.34, t-value vs. 0: 14.76, p-value vs. 0: 1.490087141478418e-32
Condition 5: r=0.08, t-value vs. 0: 5.82, p-value vs. 0: 2.804912167456447e-08
Condition 6: r=0.03, t-value vs. 0: 2.02, p-value vs. 0: 0.04472692842481385
Condition 7: r=0.21, t-value vs. 0: 7.32, p-value vs. 0: 8.589356205916409e-12
Condition 8: r=0.32, t-value vs. 0: 17.40, p-value vs. 0: 5.1980997140170166e-40
Condition 9: r=0.33, t-value vs. 0: 19.88, p-value vs. 0: 9.412135460238033e-47
Condition 10: r=0.34, t-value vs. 0: 19.00, p-value vs. 0: 2.1241862024025335e-44
Condition 11: r=0.30, t-value vs. 0: 16.97, p-value vs. 0: 7.977566896631692e-39
Condition 12: r=0.33, t-value vs. 0: 19.84, p-value vs. 0: 1.173221045863377e-46
Condition 13: r=0.40, t-value vs. 0: 18.20, p-value vs. 0: 3.130186757451285e-42
Condition 14: r=0.32, t-value vs. 0: 13.71, p-value vs. 0: 1.533086586298477e-29
Condition 15: r=0.17, t-value vs. 0: 6.43, p-value vs. 0: 1.1491225270091167e-09
Condition 16: r=0.09, t-value vs. 0: 3.43, p-value vs. 0: 0.0007506473378119802
Condition 17: r=0.21, t-value vs. 0: 7.36, p-value vs. 0: 6.933647419853647e-12
Condition 18: r=0.33, t-value vs. 0: 13.17, p-value vs. 0: 5.535198208325574e-28
Condition 19: r=-0.09, t-value vs. 0: -2.28, p-value vs. 0: 0.023862087949357187
Condition 20: r=0.26, t-value vs. 0: 8.22, p-value vs. 0: 4.5132375056637046e-14
Condition 21: r=0.37, t-value vs. 0: 12.25, p-value vs. 0: 2.5813235352482556e-25
Condition 22: r=0.38, t-value vs. 0: 14.55, p-value vs. 0: 5.876263215250661e-32
Condition 23: r=-0.01, t-value vs. 0: -0.42, p-value vs. 0: 0.6715409820873318
Condition 24: r=0.47, t-value vs. 0: 23.66, p-value vs. 0: 2.015509400084599e-56
In [ ]:
#Also threshold resting-state FC to match increase-LESIONED task FC (same connections as above, but now applied to rest FC)
restFC_posLESIONED_mreg_bycond_bysubj_offdiag=np.zeros((nConnections_mreg,nConds,nSubjs))
restFC_posLESIONED_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
for taskcond_num in np.arange(nConds):
    
    diffMat_allsubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,:]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,:]
    diffMat_allsubj_offdiag=np.transpose([diffMat_allsubj[:,:,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    diffMat_subjmean_offdiag=np.mean(diffMat_allsubj_offdiag,axis=1)
    #mask_negonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag<0) * (maxt_sigvector_bycond[:,taskcond_num]>0))
    mask_posonlyLESIONED_offdiag=1-((diffMat_subjmean_offdiag>0))
    
    restFC_mreg_bysubj_offdiag=np.transpose([restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,subjN][offdiagmat] for subjN in np.arange(nSubjs)])
    
    scount = 0
    for subj in subjNums:
        
        diffMat_thissubj=taskFC_mreg_bycond_bysubj[:,:,taskcond_num,scount]-restFCMatchedTiming_mreg_bycond_bysubj[:,:,taskcond_num,scount]
        diffMat_thissubj_offdiag=diffMat_thissubj[offdiagmat]
        mask_posonlyLESIONED_thissubj_offdiag=1-((diffMat_thissubj_offdiag>0))
        
        restFC_posLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount]=restFC_mreg_bysubj_offdiag[:,scount]*mask_posonlyLESIONED_thissubj_offdiag
        matrix_temp=np.zeros((nParcels,nParcels))
        matrix_temp[offdiagmat]=restFC_posLESIONED_mreg_bycond_bysubj_offdiag[:,taskcond_num,scount].copy()
        restFC_posLESIONED_mreg_bycond_bysubj[:,:,taskcond_num,scount]=matrix_temp.copy()
        scount=scount+1
In [ ]:
#Calculate resting-state FC actflow using task-increased FC values LESIONED

#Run actflow predictions, 24-condition task multiple-regression FC, task-reduced FC values only
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions, task-increased FC values LESIONED==")
actflowOutput_restFCMReg_bycond_posonlyLESIONED = actflow.actflowcomp.actflowtest(activations_bycond, restFC_posLESIONED_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions, task-increased FC values LESIONED==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.31, t-value vs. 0: 50.68, p-value vs. 0: 1.595035867179409e-106
 
Mean % variance explained (R^2 score, coeff. of determination) = -12.41
 
Mean MAE (mean absolute error) = 25.81
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.43, t-value vs. 0: 54.79, p-value vs. 0: 4.22323972486695e-112
 
Mean % variance explained (R^2 score, coeff. of determination) = -29.15
 
Mean MAE (mean absolute error) = 25.81
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.20, t-value vs. 0: 35.98, p-value vs. 0: 8.844339934919673e-83
By task condition:
Condition 1: r=0.19, t-value vs. 0: 15.33, p-value vs. 0: 3.5472515911616297e-34
Condition 2: r=0.20, t-value vs. 0: 18.29, p-value vs. 0: 1.7794108569642056e-42
Condition 3: r=0.17, t-value vs. 0: 9.22, p-value vs. 0: 9.589558811135199e-17
Condition 4: r=0.26, t-value vs. 0: 14.37, p-value vs. 0: 1.9201169916960392e-31
Condition 5: r=0.20, t-value vs. 0: 22.07, p-value vs. 0: 1.932799144903505e-52
Condition 6: r=0.21, t-value vs. 0: 20.53, p-value vs. 0: 1.7634526339001536e-48
Condition 7: r=0.24, t-value vs. 0: 13.01, p-value vs. 0: 1.6276054409822842e-27
Condition 8: r=0.22, t-value vs. 0: 20.52, p-value vs. 0: 1.936562770702632e-48
Condition 9: r=0.22, t-value vs. 0: 22.71, p-value vs. 0: 4.690267197129085e-54
Condition 10: r=0.24, t-value vs. 0: 21.92, p-value vs. 0: 4.528046734803731e-52
Condition 11: r=0.21, t-value vs. 0: 19.15, p-value vs. 0: 8.274406615275973e-45
Condition 12: r=0.21, t-value vs. 0: 19.74, p-value vs. 0: 2.252922846705496e-46
Condition 13: r=0.24, t-value vs. 0: 13.73, p-value vs. 0: 1.396043105324482e-29
Condition 14: r=0.20, t-value vs. 0: 12.86, p-value vs. 0: 4.39753472069888e-27
Condition 15: r=-0.06, t-value vs. 0: -3.19, p-value vs. 0: 0.0016982899860582953
Condition 16: r=-0.09, t-value vs. 0: -4.79, p-value vs. 0: 3.484217296883915e-06
Condition 17: r=0.19, t-value vs. 0: 13.14, p-value vs. 0: 7.052973804483011e-28
Condition 18: r=0.21, t-value vs. 0: 15.66, p-value vs. 0: 4.126319560233061e-35
Condition 19: r=0.22, t-value vs. 0: 13.60, p-value vs. 0: 3.3377762693204323e-29
Condition 20: r=0.28, t-value vs. 0: 19.12, p-value vs. 0: 9.936051345778441e-45
Condition 21: r=0.32, t-value vs. 0: 18.34, p-value vs. 0: 1.3399151416343443e-42
Condition 22: r=0.24, t-value vs. 0: 14.64, p-value vs. 0: 3.211719175605248e-32
Condition 23: r=0.18, t-value vs. 0: 10.60, p-value vs. 0: 1.384662456900355e-20
Condition 24: r=0.32, t-value vs. 0: 21.93, p-value vs. 0: 4.420562896714445e-52
In [ ]:
print("===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, task-increased FC values LESIONED===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTimingActflow_posLESIONED = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond_posonlyLESIONED['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCMReg_bycond_posonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, task-increased FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=-0.20
Model2 mean Pearson r=0.31
R-value difference = -0.51
Model1 vs. Model2 T-value: -32.81, p-value: 1.107379925214437e-76
 
Model1 mean % predicted variance explained R^2=-10.24
Model2 mean % predicted variance explained R^2=-12.41
R^2 difference = 2.17
 
Model1 mean MAE = 26.91
Model2 mean MAE = 25.81
Model1 vs. Model2 mean MAE difference = 1.10
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=-0.30
Model2 mean Pearson r=0.43
R-value difference = -0.72
Model1 vs. Model2 T-value: -35.25, p-value: 2.047423670348822e-81
 
Model1 mean % predicted variance explained R^2=-21.54
Model2 mean % predicted variance explained R^2=-29.15
R^2 difference = 7.62
 
Model1 mean MAE = 26.91
Model2 mean MAE = 25.81
Model1 vs. Model2 mean MAE difference = 1.10
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.26
Model2 mean Pearson r=0.20
R-value difference = 0.06
Model1 vs. Model2 T-value: 11.12, p-value: 4.613669764257945e-22
By task condition:
Condition 1: Model 1 r=0.42, Model 2 r=0.19, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 17.53, p-value vs. 0: 2.3040663303861003e-40
Condition 2: Model 1 r=0.40, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.20, t-value Model1 vs. Model2: 11.33, p-value vs. 0: 1.1397232992793815e-22
Condition 3: Model 1 r=0.22, Model 2 r=0.17, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 2.77, p-value vs. 0: 0.006207223137348779
Condition 4: Model 1 r=0.34, Model 2 r=0.26, Model 1 vs. 2 R-value difference =0.08, t-value Model1 vs. Model2: 5.41, p-value vs. 0: 2.004184635499811e-07
Condition 5: Model 1 r=0.08, Model 2 r=0.20, Model 1 vs. 2 R-value difference =-0.12, t-value Model1 vs. Model2: -6.02, p-value vs. 0: 9.924002682609346e-09
Condition 6: Model 1 r=0.03, Model 2 r=0.21, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -8.25, p-value vs. 0: 3.6768418943983585e-14
Condition 7: Model 1 r=0.21, Model 2 r=0.24, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -1.94, p-value vs. 0: 0.05389386198243686
Condition 8: Model 1 r=0.32, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.10, t-value Model1 vs. Model2: 5.81, p-value vs. 0: 2.8489261725328952e-08
Condition 9: Model 1 r=0.33, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 7.56, p-value vs. 0: 2.1370820456978413e-12
Condition 10: Model 1 r=0.34, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.10, t-value Model1 vs. Model2: 6.14, p-value vs. 0: 5.414305749925024e-09
Condition 11: Model 1 r=0.30, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.09, t-value Model1 vs. Model2: 5.52, p-value vs. 0: 1.182321494141724e-07
Condition 12: Model 1 r=0.33, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 8.18, p-value vs. 0: 5.626701833763826e-14
Condition 13: Model 1 r=0.40, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.17, t-value Model1 vs. Model2: 11.87, p-value vs. 0: 3.13114132057613e-24
Condition 14: Model 1 r=0.32, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 9.20, p-value vs. 0: 1.0447204891877784e-16
Condition 15: Model 1 r=0.17, Model 2 r=-0.06, Model 1 vs. 2 R-value difference =0.23, t-value Model1 vs. Model2: 14.13, p-value vs. 0: 9.648105731025647e-31
Condition 16: Model 1 r=0.09, Model 2 r=-0.09, Model 1 vs. 2 R-value difference =0.18, t-value Model1 vs. Model2: 10.42, p-value vs. 0: 4.415506928751033e-20
Condition 17: Model 1 r=0.21, Model 2 r=0.19, Model 1 vs. 2 R-value difference =0.01, t-value Model1 vs. Model2: 0.69, p-value vs. 0: 0.4901752869196325
Condition 18: Model 1 r=0.33, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 5.89, p-value vs. 0: 1.9061996166500857e-08
Condition 19: Model 1 r=-0.09, Model 2 r=0.22, Model 1 vs. 2 R-value difference =-0.31, t-value Model1 vs. Model2: -11.15, p-value vs. 0: 3.6561158166448435e-22
Condition 20: Model 1 r=0.26, Model 2 r=0.28, Model 1 vs. 2 R-value difference =-0.01, t-value Model1 vs. Model2: -0.64, p-value vs. 0: 0.5222610273923958
Condition 21: Model 1 r=0.37, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 2.56, p-value vs. 0: 0.01130726286300592
Condition 22: Model 1 r=0.38, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 7.97, p-value vs. 0: 1.9681907133867963e-13
Condition 23: Model 1 r=-0.01, Model 2 r=0.18, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -8.17, p-value vs. 0: 6.042458839560044e-14
Condition 24: Model 1 r=0.47, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 11.19, p-value vs. 0: 2.8630925818499943e-22
In [ ]:
print("===Compare regular Task multregFC actflow predictions to Task multregFC actflow predictions with task-increased FC values LESIONED===")
model_compare_TaskMultRegFCVsTaskMultRegFCposLESIONED_Actflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_bycond_posonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare regular Task multregFC actflow predictions to Task multregFC actflow predictions with task-increased FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.76
Model2 mean Pearson r=-0.20
R-value difference = 0.96
Model1 vs. Model2 T-value: 87.08, p-value: 5.019307265436631e-146
 
Model1 mean % predicted variance explained R^2=0.51
Model2 mean % predicted variance explained R^2=-10.24
R^2 difference = 10.76
 
Model1 mean MAE = 7.47
Model2 mean MAE = 26.91
Model1 vs. Model2 mean MAE difference = -19.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.75
Model2 mean Pearson r=-0.30
R-value difference = 1.05
Model1 vs. Model2 T-value: 70.90, p-value: 7.73573867675701e-131
 
Model1 mean % predicted variance explained R^2=0.01
Model2 mean % predicted variance explained R^2=-21.54
R^2 difference = 21.55
 
Model1 mean MAE = 7.47
Model2 mean MAE = 26.91
Model1 vs. Model2 mean MAE difference = -19.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.77
Model2 mean Pearson r=0.26
R-value difference = 0.51
Model1 vs. Model2 T-value: 84.72, p-value: 5.494743039186538e-144
By task condition:
Condition 1: Model 1 r=0.77, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.36, t-value Model1 vs. Model2: 30.68, p-value vs. 0: 2.5521431326055723e-72
Condition 2: Model 1 r=0.77, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.37, t-value Model1 vs. Model2: 29.31, p-value vs. 0: 2.0984048736340917e-69
Condition 3: Model 1 r=0.88, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.66, t-value Model1 vs. Model2: 36.68, p-value vs. 0: 4.376556435194274e-84
Condition 4: Model 1 r=0.89, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.55, t-value Model1 vs. Model2: 38.42, p-value vs. 0: 3.336602773461106e-87
Condition 5: Model 1 r=0.68, Model 2 r=0.08, Model 1 vs. 2 R-value difference =0.60, t-value Model1 vs. Model2: 40.82, p-value vs. 0: 2.3023237668323264e-91
Condition 6: Model 1 r=0.65, Model 2 r=0.03, Model 1 vs. 2 R-value difference =0.63, t-value Model1 vs. Model2: 38.31, p-value vs. 0: 5.034061320310455e-87
Condition 7: Model 1 r=0.69, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.48, t-value Model1 vs. Model2: 26.10, p-value vs. 0: 3.0966914307404085e-62
Condition 8: Model 1 r=0.60, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.28, t-value Model1 vs. Model2: 20.68, p-value vs. 0: 7.455373783161566e-49
Condition 9: Model 1 r=0.57, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 19.04, p-value vs. 0: 1.655562940864535e-44
Condition 10: Model 1 r=0.58, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 18.80, p-value vs. 0: 7.352458476170564e-44
Condition 11: Model 1 r=0.56, Model 2 r=0.30, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 17.89, p-value vs. 0: 2.2550944429676956e-41
Condition 12: Model 1 r=0.59, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 19.75, p-value vs. 0: 2.0661787082451488e-46
Condition 13: Model 1 r=0.91, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.51, t-value Model1 vs. Model2: 39.42, p-value vs. 0: 5.8291946800686296e-89
Condition 14: Model 1 r=0.91, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.59, t-value Model1 vs. Model2: 43.95, p-value vs. 0: 1.7735232069401277e-96
Condition 15: Model 1 r=0.91, Model 2 r=0.17, Model 1 vs. 2 R-value difference =0.74, t-value Model1 vs. Model2: 43.96, p-value vs. 0: 1.6936414069664648e-96
Condition 16: Model 1 r=0.90, Model 2 r=0.09, Model 1 vs. 2 R-value difference =0.82, t-value Model1 vs. Model2: 47.03, p-value vs. 0: 3.165943554839615e-101
Condition 17: Model 1 r=0.69, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.48, t-value Model1 vs. Model2: 22.41, p-value vs. 0: 2.571094471597841e-53
Condition 18: Model 1 r=0.73, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.40, t-value Model1 vs. Model2: 21.16, p-value vs. 0: 4.227686308851623e-50
Condition 19: Model 1 r=0.76, Model 2 r=-0.09, Model 1 vs. 2 R-value difference =0.85, t-value Model1 vs. Model2: 26.64, p-value vs. 0: 1.7699152591515007e-63
Condition 20: Model 1 r=0.80, Model 2 r=0.26, Model 1 vs. 2 R-value difference =0.53, t-value Model1 vs. Model2: 24.32, p-value vs. 0: 5.146975539018577e-58
Condition 21: Model 1 r=0.79, Model 2 r=0.37, Model 1 vs. 2 R-value difference =0.43, t-value Model1 vs. Model2: 22.98, p-value vs. 0: 9.842640381430076e-55
Condition 22: Model 1 r=0.79, Model 2 r=0.38, Model 1 vs. 2 R-value difference =0.41, t-value Model1 vs. Model2: 24.64, p-value vs. 0: 8.760581677829614e-59
Condition 23: Model 1 r=0.73, Model 2 r=-0.01, Model 1 vs. 2 R-value difference =0.74, t-value Model1 vs. Model2: 27.64, p-value vs. 0: 9.79593307444795e-66
Condition 24: Model 1 r=0.75, Model 2 r=0.47, Model 1 vs. 2 R-value difference =0.28, t-value Model1 vs. Model2: 20.22, p-value vs. 0: 1.1983583888878538e-47
In [ ]:
print("===Compare regular Rest time-matched multregFC actflow predictions to Rest time-matched multregFC actflow predictions with task-increased FC values LESIONED===")
model_compare_RestMultRegFCVsRestMultRegFCposLESIONED_Actflow = actflow.model_compare(target_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMRegMatchedTiming_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_restFCMReg_bycond_posonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare regular Rest time-matched multregFC actflow predictions to Rest time-matched multregFC actflow predictions with task-increased FC values LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.46
Model2 mean Pearson r=0.31
R-value difference = 0.14
Model1 vs. Model2 T-value: 24.68, p-value: 7.04099032331946e-59
 
Model1 mean % predicted variance explained R^2=-0.29
Model2 mean % predicted variance explained R^2=-12.41
R^2 difference = 12.12
 
Model1 mean MAE = 11.94
Model2 mean MAE = 25.81
Model1 vs. Model2 mean MAE difference = -13.87
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=0.43
R-value difference = 0.06
Model1 vs. Model2 T-value: 12.22, p-value: 3.0812163449060883e-25
 
Model1 mean % predicted variance explained R^2=-1.71
Model2 mean % predicted variance explained R^2=-29.15
R^2 difference = 27.44
 
Model1 mean MAE = 11.94
Model2 mean MAE = 25.81
Model1 vs. Model2 mean MAE difference = -13.87
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.46
Model2 mean Pearson r=0.20
R-value difference = 0.25
Model1 vs. Model2 T-value: 53.72, p-value: 1.1283573528904303e-110
By task condition:
Condition 1: Model 1 r=0.42, Model 2 r=0.19, Model 1 vs. 2 R-value difference =0.22, t-value Model1 vs. Model2: 21.65, p-value vs. 0: 2.3051946607931762e-51
Condition 2: Model 1 r=0.39, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.18, t-value Model1 vs. Model2: 17.31, p-value vs. 0: 9.244403705881717e-40
Condition 3: Model 1 r=0.53, Model 2 r=0.17, Model 1 vs. 2 R-value difference =0.36, t-value Model1 vs. Model2: 21.61, p-value vs. 0: 2.8430582739597403e-51
Condition 4: Model 1 r=0.54, Model 2 r=0.26, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 17.58, p-value vs. 0: 1.6157877524119e-40
Condition 5: Model 1 r=0.32, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 13.09, p-value vs. 0: 9.98151587168788e-28
Condition 6: Model 1 r=0.28, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.07, t-value Model1 vs. Model2: 6.74, p-value vs. 0: 2.2006446010946704e-10
Condition 7: Model 1 r=0.54, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 23.58, p-value vs. 0: 3.2273506528801006e-56
Condition 8: Model 1 r=0.38, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.15, t-value Model1 vs. Model2: 20.57, p-value vs. 0: 1.380355918068128e-48
Condition 9: Model 1 r=0.37, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 18.86, p-value vs. 0: 5.062360732396363e-44
Condition 10: Model 1 r=0.37, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 17.85, p-value vs. 0: 2.8528998867943445e-41
Condition 11: Model 1 r=0.35, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.14, t-value Model1 vs. Model2: 19.13, p-value vs. 0: 9.761973867259565e-45
Condition 12: Model 1 r=0.37, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.16, t-value Model1 vs. Model2: 19.98, p-value vs. 0: 5.088660438613875e-47
Condition 13: Model 1 r=0.57, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.33, t-value Model1 vs. Model2: 21.83, p-value vs. 0: 8.023125542867219e-52
Condition 14: Model 1 r=0.58, Model 2 r=0.20, Model 1 vs. 2 R-value difference =0.39, t-value Model1 vs. Model2: 29.06, p-value vs. 0: 7.288044979609017e-69
Condition 15: Model 1 r=0.52, Model 2 r=-0.06, Model 1 vs. 2 R-value difference =0.58, t-value Model1 vs. Model2: 32.48, p-value vs. 0: 5.1264392706394204e-76
Condition 16: Model 1 r=0.54, Model 2 r=-0.09, Model 1 vs. 2 R-value difference =0.63, t-value Model1 vs. Model2: 36.28, p-value vs. 0: 2.481996806106325e-83
Condition 17: Model 1 r=0.47, Model 2 r=0.19, Model 1 vs. 2 R-value difference =0.27, t-value Model1 vs. Model2: 22.54, p-value vs. 0: 1.2692589634809416e-53
Condition 18: Model 1 r=0.41, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.20, t-value Model1 vs. Model2: 17.20, p-value vs. 0: 1.7993319471135306e-39
Condition 19: Model 1 r=0.51, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.29, t-value Model1 vs. Model2: 21.81, p-value vs. 0: 8.943194325626982e-52
Condition 20: Model 1 r=0.52, Model 2 r=0.28, Model 1 vs. 2 R-value difference =0.24, t-value Model1 vs. Model2: 21.34, p-value vs. 0: 1.3769573349624037e-50
Condition 21: Model 1 r=0.51, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.19, t-value Model1 vs. Model2: 15.83, p-value vs. 0: 1.3520308832866792e-35
Condition 22: Model 1 r=0.43, Model 2 r=0.24, Model 1 vs. 2 R-value difference =0.19, t-value Model1 vs. Model2: 14.32, p-value vs. 0: 2.7732401959834597e-31
Condition 23: Model 1 r=0.48, Model 2 r=0.18, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 23.48, p-value vs. 0: 5.605738695675569e-56
Condition 24: Model 1 r=0.49, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.17, t-value Model1 vs. Model2: 17.78, p-value vs. 0: 4.504120003847652e-41
In [ ]:
print("===Compare regular Task multregFC negative LESIONED actflow predictions to Task multregFC actflow predictions with positive LESIONED===")
model_compare_TaskMultRegFCnegLESIONEDVsTaskMultRegFCposLESIONED_Actflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond_signegonlyLESIONED['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_bycond_posonlyLESIONED['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare regular Task multregFC negative LESIONED actflow predictions to Task multregFC actflow predictions with positive LESIONED===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=-0.20
R-value difference = 0.67
Model1 vs. Model2 T-value: 49.73, p-value: 3.545454902141102e-105
 
Model1 mean % predicted variance explained R^2=-7.96
Model2 mean % predicted variance explained R^2=-10.24
R^2 difference = 2.28
 
Model1 mean MAE = 22.53
Model2 mean MAE = 26.91
Model1 vs. Model2 mean MAE difference = -4.38
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.54
Model2 mean Pearson r=-0.30
R-value difference = 0.84
Model1 vs. Model2 T-value: 43.45, p-value: 1.1034933415445991e-95
 
Model1 mean % predicted variance explained R^2=-18.74
Model2 mean % predicted variance explained R^2=-21.54
R^2 difference = 2.79
 
Model1 mean MAE = 22.53
Model2 mean MAE = 26.91
Model1 vs. Model2 mean MAE difference = -4.38
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.57
Model2 mean Pearson r=0.26
R-value difference = 0.31
Model1 vs. Model2 T-value: 41.21, p-value: 5.0818431338716505e-92
By task condition:
Condition 1: Model 1 r=0.52, Model 2 r=0.42, Model 1 vs. 2 R-value difference =0.10, t-value Model1 vs. Model2: 8.72, p-value vs. 0: 2.0710104017515087e-15
Condition 2: Model 1 r=0.49, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.09, t-value Model1 vs. Model2: 5.38, p-value vs. 0: 2.3506241463219449e-07
Condition 3: Model 1 r=0.68, Model 2 r=0.22, Model 1 vs. 2 R-value difference =0.46, t-value Model1 vs. Model2: 17.43, p-value vs. 0: 4.360166200977561e-40
Condition 4: Model 1 r=0.66, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.32, t-value Model1 vs. Model2: 15.42, p-value vs. 0: 1.9354593998757207e-34
Condition 5: Model 1 r=0.29, Model 2 r=0.08, Model 1 vs. 2 R-value difference =0.21, t-value Model1 vs. Model2: 11.73, p-value vs. 0: 8.281165844192649e-24
Condition 6: Model 1 r=0.28, Model 2 r=0.03, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 12.02, p-value vs. 0: 1.1737822210850806e-24
Condition 7: Model 1 r=0.63, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.42, t-value Model1 vs. Model2: 15.67, p-value vs. 0: 3.892202559419501e-35
Condition 8: Model 1 r=0.49, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.17, t-value Model1 vs. Model2: 10.00, p-value vs. 0: 6.518725622405827e-19
Condition 9: Model 1 r=0.43, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.09, t-value Model1 vs. Model2: 6.14, p-value vs. 0: 5.320939461766762e-09
Condition 10: Model 1 r=0.44, Model 2 r=0.34, Model 1 vs. 2 R-value difference =0.10, t-value Model1 vs. Model2: 6.76, p-value vs. 0: 1.9674106224813313e-10
Condition 11: Model 1 r=0.42, Model 2 r=0.30, Model 1 vs. 2 R-value difference =0.13, t-value Model1 vs. Model2: 7.11, p-value vs. 0: 2.8341624109080275e-11
Condition 12: Model 1 r=0.45, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.11, t-value Model1 vs. Model2: 7.11, p-value vs. 0: 2.9010318989222223e-11
Condition 13: Model 1 r=0.67, Model 2 r=0.40, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 15.98, p-value vs. 0: 4.916984898178991e-36
Condition 14: Model 1 r=0.68, Model 2 r=0.32, Model 1 vs. 2 R-value difference =0.36, t-value Model1 vs. Model2: 17.97, p-value vs. 0: 1.364011299948193e-41
Condition 15: Model 1 r=0.62, Model 2 r=0.17, Model 1 vs. 2 R-value difference =0.45, t-value Model1 vs. Model2: 17.69, p-value vs. 0: 7.889583398092612e-41
Condition 16: Model 1 r=0.65, Model 2 r=0.09, Model 1 vs. 2 R-value difference =0.56, t-value Model1 vs. Model2: 20.84, p-value vs. 0: 2.7814556081350446e-49
Condition 17: Model 1 r=0.56, Model 2 r=0.21, Model 1 vs. 2 R-value difference =0.35, t-value Model1 vs. Model2: 11.72, p-value vs. 0: 8.622027173362522e-24
Condition 18: Model 1 r=0.58, Model 2 r=0.33, Model 1 vs. 2 R-value difference =0.25, t-value Model1 vs. Model2: 10.20, p-value vs. 0: 1.8011261244154288e-19
Condition 19: Model 1 r=0.71, Model 2 r=-0.09, Model 1 vs. 2 R-value difference =0.79, t-value Model1 vs. Model2: 19.08, p-value vs. 0: 1.335314769295342e-44
Condition 20: Model 1 r=0.70, Model 2 r=0.26, Model 1 vs. 2 R-value difference =0.44, t-value Model1 vs. Model2: 15.31, p-value vs. 0: 3.969132721813406e-34
Condition 21: Model 1 r=0.66, Model 2 r=0.37, Model 1 vs. 2 R-value difference =0.30, t-value Model1 vs. Model2: 11.79, p-value vs. 0: 5.301816786957286e-24
Condition 22: Model 1 r=0.64, Model 2 r=0.38, Model 1 vs. 2 R-value difference =0.26, t-value Model1 vs. Model2: 12.51, p-value vs. 0: 4.4612170769166045e-26
Condition 23: Model 1 r=0.67, Model 2 r=-0.01, Model 1 vs. 2 R-value difference =0.69, t-value Model1 vs. Model2: 19.14, p-value vs. 0: 8.75314658359568e-45
Condition 24: Model 1 r=0.59, Model 2 r=0.47, Model 1 vs. 2 R-value difference =0.12, t-value Model1 vs. Model2: 7.66, p-value vs. 0: 1.1998443512863658e-12
In [ ]:
#Calculate actflows manually
actflows_taskFC_posLESIONED=np.zeros((nParcels,nParcels,nConds,nSubjs))
scount = 0
for subj in subjNums:
    for cond in np.arange(24):
        
        for heldOutRegion in np.arange(nParcels):
            otherRegions=list(range(nParcels))
            otherRegions.remove(heldOutRegion)
            actflows_taskFC_posLESIONED[heldOutRegion,otherRegions,cond,scount]=activations_bycond[otherRegions,cond,scount]*taskFC_posonlyLESIONED_mreg_bycond_bysubj[heldOutRegion,otherRegions,cond,scount]
        
    scount=scount+1
In [ ]:
#Quantify percentage of positive actflows (i.e., actflow increases given that rest is baseline) with FC decreases lesioned

actflows_taskFC_posLESIONED_flat=actflows_taskFC_posLESIONED.flatten()

num_pos_actflows=np.sum(actflows_taskFC_posLESIONED_flat>0)
num_actflows_total=np.shape(actflows_taskFC_posLESIONED_flat)[0]
print("Percentage of positive actflows, with all rest-to-task FC increases lesioned:", 100*num_pos_actflows/num_actflows_total)

num_zero_actflows=np.sum(actflows_taskFC_posLESIONED_flat==0)
print("Percentage of 0-flow actflows, with all rest-to-task FC increases lesioned:", 100*num_zero_actflows/num_actflows_total)

num_neg_actflows=np.sum(actflows_taskFC_posLESIONED_flat<0)
print("Percentage of negative actflows, with all rest-to-task FC increases lesioned:", 100*num_neg_actflows/num_actflows_total)

num_pos_actflows=np.sum(actflows_taskFC_posLESIONED_flat>0)
num_actflows_totalnonzero=np.sum(actflows_taskFC_posLESIONED_flat!=0)
print("Percentage of positive actflows among non-zeros, with all rest-to-task FC increases lesioned:", 100*num_pos_actflows/num_actflows_totalnonzero)

num_neg_actflows=np.sum(actflows_taskFC_posLESIONED_flat<0)
print("Percentage of negative actflows among non-zeros, with all rest-to-task FC increases lesioned:", 100*num_neg_actflows/num_actflows_totalnonzero)
Percentage of positive actflows, with all rest-to-task FC increases lesioned: 25.0287042882529
Percentage of 0-flow actflows, with all rest-to-task FC increases lesioned: 49.9417631903526
Percentage of negative actflows, with all rest-to-task FC increases lesioned: 25.0295325213945
Percentage of positive actflows among non-zeros, with all rest-to-task FC increases lesioned: 49.999172730409235
Percentage of negative actflows among non-zeros, with all rest-to-task FC increases lesioned: 50.000827269590765
In [ ]:
#Visualize actflow matrix
ax = sns.heatmap(np.mean(actflows_taskFC_posLESIONED[netorder,:,22,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Activity flow (act. * FC)'})
ax.figure.suptitle('Actflows (actflows_taskFC_posLESIONED), cond 23')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]

Using task-general FC (multreg FC calculated across all tasks simultaneously)

In [ ]:
## Calculating multreg task-general FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregTaskGenFC_data'

taskgenFC_mreg_bysubj=np.zeros((nParcels,nParcels,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        taskgenFC_mreg_bysubj[:,:,scount] = h5f['taskgenFCRegression'][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
        
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("tasktiming_all=np.array(np.sum(tasktiming_bycond,axis=1)>0)\n")
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("taskgenFC_mreg_bysubj=actflow.connectivity_estimation.multregconn(taskdata[:,tasktiming_all])\n")
        file_python.write("#Save multiple-regression task FC data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'taskgenFCRegression'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=taskgenFC_mreg_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=taskgenFC_mreg_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:02:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskgenFC_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task-general FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [ ]:
taskFCmeanMat_mreg=np.mean(taskgenFC_mreg_bysubj,axis=2)
restFCmeanMat_mreg=np.mean(restFC_mreg_bysubj,axis=2)
upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat_mreg)[0],k=1)
resttask_mattcorr=np.corrcoef(taskFCmeanMat_mreg[upperTIndices_corrmat], restFCmeanMat_mreg[upperTIndices_corrmat])
print('Mean task-gen. to rest FC similarity, mreg: ', str(resttask_mattcorr))
Mean task-gen. to rest FC similarity, mreg:  [[1.         0.95087532]
 [0.95087532 1.        ]]
In [ ]:
#Analysis of task vs. rest matrix
restFCDataMultreg=np.mean(restFC_mreg_bysubj,axis=2)[netorder,:][:,netorder]
diffMat=np.mean(taskgenFC_mreg_bysubj,axis=2)[netorder,:][:,netorder]-restFCDataMultreg
print('Minimum value: ' + str(min(diffMat.flatten())))
print('Maximum value: ' + str(max(diffMat.flatten())))
print('Mean value: ' + str(np.mean(diffMat.flatten())))
print('Stdev value: ' + str(np.std(diffMat.flatten())))

#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
diffMat_allsubj=taskgenFC_mreg_bysubj-restFC_mreg_bysubj
upperTIndices_corrmat=np.triu_indices(np.shape(diffMat_allsubj)[0],k=1)
diffMat_allsubj_upperT=np.transpose([diffMat_allsubj[:,:,subjN][upperTIndices_corrmat] for subjN in np.arange(nSubjs)])

nConnections=int(nParcels*(nParcels-1)/2)
print('Percent connections POSITIVE:', str(100*np.sum(diffMat[upperTIndices_corrmat]>0)/nConnections))
print('Percent connections NEGATIVE:', str(100*np.sum(diffMat[upperTIndices_corrmat]<0)/nConnections))

#FDR
tstats = [stats.ttest_1samp(diffMat_allsubj_upperT[connnum,:],0.0) for connnum in np.arange(nConnections)]
tvals=np.array([tstats[connnum][0] for connnum in np.arange(nConnections)])
pvals=[tstats[connnum][1] for connnum in np.arange(nConnections)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
fdr_sigvector=pvals_FDRcorrected<0.05
#Bonferroni
# bonferroni_sigvector=pvals<(0.05/nConnections)

#MaxT
maxt_output=actflow.tools.max_t(diffMat_allsubj_upperT)
maxTThreshold = maxt_output[1]
maxt_sigvector=np.array(maxt_output[2])<0.05
tvals=maxt_output[0]
#maxt_sigvector=np.abs(tvals)>maxTThreshold

totalconn=np.sum(maxt_sigvector)
poscon=np.sum(maxt_sigvector*(tvals>0))
negconn=np.sum(maxt_sigvector*(tvals<0))
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', totalconn)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', poscon)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', negconn)

print('')
percenttotal=100*np.sum(maxt_sigvector)/nConnections
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nConnections
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nConnections
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', percenttotal)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

print('')
percenttotal=100*np.sum(fdr_sigvector)/nConnections
percentpos=100*np.sum(fdr_sigvector*(tvals>0))/nConnections
percentneg=100*np.sum(fdr_sigvector*(tvals<0))/nConnections
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) TOTAL:', percenttotal)
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant connections (FDR corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)
Minimum value: -0.19537868931596186
Maximum value: 0.07743451617961822
Mean value: -7.358140845126687e-06
Stdev value: 0.00562843309207656
Percent connections POSITIVE: 50.72423398328691
Percent connections NEGATIVE: 49.27576601671309
# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 497
# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 106
# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 391

Percent significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 0.7691117301145156
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 0.16403590219746209
Percent significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 0.6050758279170535

Percent significant connections (FDR corrected for multiple comparisons, p<0.05) TOTAL: 3.098112039616218
Percent significant connections (FDR corrected for multiple comparisons, p<0.05) POSITIVE: 1.2550294026617146
Percent significant connections (FDR corrected for multiple comparisons, p<0.05) NEGATIVE: 1.8430826369545033
In [ ]:
totalconn=np.sum(maxt_sigvector)
poscon=np.sum(maxt_sigvector*(tvals>0))
negconn=np.sum(maxt_sigvector*(tvals<0))
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL:', totalconn)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', poscon)
print('# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', negconn)
# significant connections (maxT corrected for multiple comparisons, p<0.05) TOTAL: 497
# significant connections (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 106
# significant connections (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 391
In [ ]:
#Visualize FC matrix - mean task FC vs. rest FC
FCMin=np.min(np.min(diffMat))
FCMax=np.max(np.max(diffMat))
ax = sns.heatmap(diffMat,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Task - rest multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meantaskGenFCVsRestFC_multreg_discovery176.pdf')
In [ ]:
#Visualize FC matrix - mean rest FC
FCMin=np.min(np.min(restFCDataMultreg))
FCMax=np.max(np.max(restFCDataMultreg))
ax = sns.heatmap(restFCDataMultreg,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Rest multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meanRestFC_multreg_discovery176.pdf')
In [ ]:
#Visualize FC matrix - mean task FC
meanTaskFCMultReg=np.mean(taskgenFC_mreg_bysubj,axis=2)[netorder,:][:,netorder]
FCMin=np.min(np.min(meanTaskFCMultReg))
FCMax=np.max(np.max(meanTaskFCMultReg))
ax = sns.heatmap(meanTaskFCMultReg,square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'},vmin=FCMin,vmax=FCMax)
#ax.invert_yaxis()
ax.figure.suptitle('Mean task multreg FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
fig=ax.get_figure()
fig.savefig(figuredir+'meantaskGenFC_multreg_discovery176.pdf')

Task-state multiple regression FC LOCALLY-NON-CIRCULAR with comparison to resting-state FC actflow predictions

In [ ]:
## Calculating NON-CIRCULAR multreg TASK FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_noncirc_data'

taskFC_noncirc_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_noncirc_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        # Create batchscripts directory if it doesn't exist
        if not os.path.exists('batchscripts/'): os.makedirs('batchscripts/')
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write(
            f"import sys; sys.path.insert(0, '{basedir}/docs/scripts/final_analyses/'); sys.path.insert(0, '{basedir}/docs/scripts/final_analyses/glmScripts/vertexwise_postproc/')\n"
            f"import loadTaskResiduals_byscript; import h5py; import ActflowToolbox as actflow; import taskGLMPipeline_VertexWise as tgp\n"
            f"taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('{str(subj)}',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=True, preprocdatadir_vertexwise='{preprocdatadir_vertexwise}', datadir='{datadir}')\n"    
            f"tasktiming_bycond=tgp.loadTaskTimingForAllTasks('{str(subj)}',taskModel='canonical')['taskRegressors']>.5\n"
            f"for condnum in range(24):\n"
            f"    tasktiming=tasktiming_bycond[:,condnum]\n"
            f"    output_data=actflow.connectivity_estimation.calcconn_parcelwise_noncircular_surface(taskdata[:,tasktiming],dilated_parcels=True,verbose=False,connmethod='pc_multregconn')\n"
            f"    #Save data to file\n"
            f"    h5f = h5py.File('{file_path}','a')\n"
            f"    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n"
            f"    try:\n"
            f"        h5f.create_dataset(outname1,data=output_data)\n"
            f"    except:\n"
            f"        del h5f[outname1]\n"
            f"        h5f.create_dataset(outname1,data=output_data)\n"
            f"    h5f.close()"
            )
        file_python.close()
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write(
            f'#!/bin/bash\n'
            f'#SBATCH --partition=nm3\n'
            f'#SBATCH --job-name={str(subj)}\n'
            f'#SBATCH --requeue\n'
            f'#SBATCH --time=00:20:00\n'
            f'#SBATCH --nodes=1\n'
            f'#SBATCH --ntasks=1\n'
            f'#SBATCH --cpus-per-task=20\n'
            f'#SBATCH --mem=64000\n'
            f'#SBATCH --export=ALL\n'
            f'time python batchscripts/pythonscript_{str(subj)}.py\n'
            )
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
ax = sns.heatmap(np.mean(taskFC_noncirc_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Multiple-regression FC (beta)'})
In [ ]:
np.corrcoef(taskFC_mreg_bycond_bysubj.flatten(),taskFC_noncirc_mreg_bycond_bysubj.flatten())
Out[ ]:
array([[1.      , 0.739939],
       [0.739939, 1.      ]])
In [ ]:
taskFCmeanMat_mreg=np.mean(np.mean(taskFC_noncirc_mreg_bycond_bysubj,axis=3),axis=2)
restFCmeanMat_mreg=np.mean(restFC_noncirc_mreg_bysubj,axis=2)
upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat_mreg)[0],k=1)
resttask_mattcorr=np.corrcoef(taskFCmeanMat_mreg[upperTIndices_corrmat], restFCmeanMat_mreg[upperTIndices_corrmat])
print('Mean noncirc task to rest FC similarity, mreg: ', str(resttask_mattcorr))
Mean noncirc task to rest FC similarity, mreg:  [[1.         0.71619801]
 [0.71619801 1.        ]]
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC
print("==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based task-state FC, 24 task conditions==")
actflowOutput_taskFCMReg_noncirc_bycond = actflow.actflowcomp.actflowtest(activations_noncirc_bycond, taskFC_noncirc_mreg_bycond_bysubj, separate_activations_bytarget=True, full_report=True)
==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based task-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.85, t-value vs. 0: 150.68, p-value vs. 0: 3.935808851437328e-187
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.67
 
Mean MAE (mean absolute error) = 5.74
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.85, t-value vs. 0: 185.63, p-value vs. 0: 6.902138020613777e-203
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.30
 
Mean MAE (mean absolute error) = 5.74
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.88, t-value vs. 0: 218.00, p-value vs. 0: 4.739951793650816e-215
By task condition:
Condition 1: r=0.79, t-value vs. 0: 75.62, p-value vs. 0: 1.4077924952614124e-135
Condition 2: r=0.84, t-value vs. 0: 85.04, p-value vs. 0: 2.8962930431074955e-144
Condition 3: r=0.96, t-value vs. 0: 130.01, p-value vs. 0: 5.151350478912314e-176
Condition 4: r=0.97, t-value vs. 0: 131.91, p-value vs. 0: 4.184642436140284e-177
Condition 5: r=0.70, t-value vs. 0: 70.50, p-value vs. 0: 2.0362856529371935e-130
Condition 6: r=0.64, t-value vs. 0: 60.17, p-value vs. 0: 7.517790463285536e-119
Condition 7: r=0.91, t-value vs. 0: 90.41, p-value vs. 0: 8.140573014484647e-149
Condition 8: r=0.74, t-value vs. 0: 58.01, p-value vs. 0: 3.333598037775318e-116
Condition 9: r=0.68, t-value vs. 0: 60.39, p-value vs. 0: 4.064923454535042e-119
Condition 10: r=0.69, t-value vs. 0: 61.47, p-value vs. 0: 2.0654669681211213e-120
Condition 11: r=0.70, t-value vs. 0: 62.96, p-value vs. 0: 3.816649278599708e-122
Condition 12: r=0.68, t-value vs. 0: 64.43, p-value vs. 0: 7.909389484184055e-124
Condition 13: r=0.95, t-value vs. 0: 104.92, p-value vs. 0: 6.285717065505832e-160
Condition 14: r=0.97, t-value vs. 0: 118.80, p-value vs. 0: 3.0731054114961825e-169
Condition 15: r=0.97, t-value vs. 0: 163.27, p-value vs. 0: 3.474726783190388e-193
Condition 16: r=0.96, t-value vs. 0: 135.92, p-value vs. 0: 2.313877158856432e-179
Condition 17: r=0.83, t-value vs. 0: 73.05, p-value vs. 0: 4.875991477880751e-133
Condition 18: r=0.86, t-value vs. 0: 74.82, p-value vs. 0: 8.440445831158034e-135
Condition 19: r=0.92, t-value vs. 0: 97.06, p-value vs. 0: 4.1675050244902146e-154
Condition 20: r=0.93, t-value vs. 0: 94.25, p-value vs. 0: 6.3952062354169805e-152
Condition 21: r=0.92, t-value vs. 0: 96.74, p-value vs. 0: 7.256767968636138e-154
Condition 22: r=0.92, t-value vs. 0: 88.64, p-value vs. 0: 2.3997388336719298e-147
Condition 23: r=0.87, t-value vs. 0: 68.58, p-value vs. 0: 2.145690191985656e-128
Condition 24: r=0.82, t-value vs. 0: 50.31, p-value vs. 0: 5.406570547120094e-106
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC
print("==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based task-state FC, 24 task conditions==")
actflowOutput_taskFCMReg_noncirc_bycond = actflow.actflowcomp.actflowtest(activations_noncirc_bycond, taskFC_noncirc_mreg_bycond_bysubj, separate_activations_bytarget=True, full_report=True)
==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based task-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.85, t-value vs. 0: 150.68, p-value vs. 0: 3.935808851437328e-187
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.67
 
Mean MAE (mean absolute error) = 5.74
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.85, t-value vs. 0: 185.63, p-value vs. 0: 6.902138020613777e-203
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.30
 
Mean MAE (mean absolute error) = 5.74
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.88, t-value vs. 0: 218.00, p-value vs. 0: 4.739951793650816e-215
By task condition:
Condition 1: r=0.79, t-value vs. 0: 75.62, p-value vs. 0: 1.4077924952614124e-135
Condition 2: r=0.84, t-value vs. 0: 85.04, p-value vs. 0: 2.8962930431074955e-144
Condition 3: r=0.96, t-value vs. 0: 130.01, p-value vs. 0: 5.151350478912314e-176
Condition 4: r=0.97, t-value vs. 0: 131.91, p-value vs. 0: 4.184642436140284e-177
Condition 5: r=0.70, t-value vs. 0: 70.50, p-value vs. 0: 2.0362856529371935e-130
Condition 6: r=0.64, t-value vs. 0: 60.17, p-value vs. 0: 7.517790463285536e-119
Condition 7: r=0.91, t-value vs. 0: 90.41, p-value vs. 0: 8.140573014484647e-149
Condition 8: r=0.74, t-value vs. 0: 58.01, p-value vs. 0: 3.333598037775318e-116
Condition 9: r=0.68, t-value vs. 0: 60.39, p-value vs. 0: 4.064923454535042e-119
Condition 10: r=0.69, t-value vs. 0: 61.47, p-value vs. 0: 2.0654669681211213e-120
Condition 11: r=0.70, t-value vs. 0: 62.96, p-value vs. 0: 3.816649278599708e-122
Condition 12: r=0.68, t-value vs. 0: 64.43, p-value vs. 0: 7.909389484184055e-124
Condition 13: r=0.95, t-value vs. 0: 104.92, p-value vs. 0: 6.285717065505832e-160
Condition 14: r=0.97, t-value vs. 0: 118.80, p-value vs. 0: 3.0731054114961825e-169
Condition 15: r=0.97, t-value vs. 0: 163.27, p-value vs. 0: 3.474726783190388e-193
Condition 16: r=0.96, t-value vs. 0: 135.92, p-value vs. 0: 2.313877158856432e-179
Condition 17: r=0.83, t-value vs. 0: 73.05, p-value vs. 0: 4.875991477880751e-133
Condition 18: r=0.86, t-value vs. 0: 74.82, p-value vs. 0: 8.440445831158034e-135
Condition 19: r=0.92, t-value vs. 0: 97.06, p-value vs. 0: 4.1675050244902146e-154
Condition 20: r=0.93, t-value vs. 0: 94.25, p-value vs. 0: 6.3952062354169805e-152
Condition 21: r=0.92, t-value vs. 0: 96.74, p-value vs. 0: 7.256767968636138e-154
Condition 22: r=0.92, t-value vs. 0: 88.64, p-value vs. 0: 2.3997388336719298e-147
Condition 23: r=0.87, t-value vs. 0: 68.58, p-value vs. 0: 2.145690191985656e-128
Condition 24: r=0.82, t-value vs. 0: 50.31, p-value vs. 0: 5.406570547120094e-106

Match multreg resting-state FC data to task-state FC data NON-CIRCULAR (amount of data, task timing regression, timing, etc)

In [ ]:
## Calculating multreg REST FC, NON-CIRCULAR, matched timing with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFCMatchedTiming_noncirc_data'

restFCMatchedTiming_noncirc_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFCMatchedTiming_noncirc_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['restFCRegressionMatchTiming'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/vertexwise_postproc/')\n")
        
        file_python.write("import loadRestResiduals_byscript; import h5py; import ActflowToolbox as actflow; import taskGLMPipeline_VertexWise as tgp; import numpy as np\n")
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=True, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    restFCMatchedTiming_noncirc_mreg_bycond_bysubj=actflow.connectivity_estimation.calcconn_parcelwise_noncircular(restdata[:,tasktiming],dilated_parcels=True,verbose=False,connmethod='pc_multregconn')\n")
        file_python.write("    #Save multiple-regression FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegressionMatchTiming/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_noncirc_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_noncirc_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:20:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
plt.imshow(np.mean(restFCMatchedTiming_noncirc_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),origin='lower')
plt.colorbar()
Out[ ]:
<matplotlib.colorbar.Colorbar at 0x7faf78033320>
In [ ]:
#Run actflow predictions, 24-condition rest multiple-regression FC
print("==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based resting-state FC, 24 task conditions==")
actflowOutput_restFCMReg_noncirc_bycond = actflow.actflowcomp.actflowtest(activations_noncirc_bycond, restFCMatchedTiming_noncirc_mreg_bycond_bysubj, separate_activations_bytarget=True, full_report=True)
==Activity flow mapping results, LOCALLY-NON-CIRCULAR, multiple-regression-based resting-state FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.42, t-value vs. 0: 76.35, p-value vs. 0: 2.7224887723700828e-136
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.37
 
Mean MAE (mean absolute error) = 12.32
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.45, t-value vs. 0: 82.11, p-value vs. 0: 1.1507624659883614e-141
 
Mean % variance explained (R^2 score, coeff. of determination) = -1.86
 
Mean MAE (mean absolute error) = 12.32
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.42, t-value vs. 0: 89.01, p-value vs. 0: 1.1731796223267399e-147
By task condition:
Condition 1: r=0.38, t-value vs. 0: 35.36, p-value vs. 0: 1.272566893211832e-81
Condition 2: r=0.35, t-value vs. 0: 31.13, p-value vs. 0: 2.8905012386692925e-73
Condition 3: r=0.50, t-value vs. 0: 47.89, p-value vs. 0: 1.6863177063319103e-102
Condition 4: r=0.50, t-value vs. 0: 47.35, p-value vs. 0: 1.0602183999275464e-101
Condition 5: r=0.28, t-value vs. 0: 35.35, p-value vs. 0: 1.3177984681320023e-81
Condition 6: r=0.25, t-value vs. 0: 32.81, p-value vs. 0: 1.1328794399344081e-76
Condition 7: r=0.51, t-value vs. 0: 45.85, p-value vs. 0: 1.918741891661153e-99
Condition 8: r=0.34, t-value vs. 0: 30.03, p-value vs. 0: 5.78830537574989e-71
Condition 9: r=0.33, t-value vs. 0: 33.66, p-value vs. 0: 2.4131267923316287e-78
Condition 10: r=0.34, t-value vs. 0: 30.19, p-value vs. 0: 2.7692115026963894e-71
Condition 11: r=0.32, t-value vs. 0: 31.05, p-value vs. 0: 4.2354392704789545e-73
Condition 12: r=0.34, t-value vs. 0: 31.68, p-value vs. 0: 2.1471724566531582e-74
Condition 13: r=0.53, t-value vs. 0: 56.02, p-value vs. 0: 1.0702124675334511e-113
Condition 14: r=0.55, t-value vs. 0: 54.49, p-value vs. 0: 1.0617978360206981e-111
Condition 15: r=0.48, t-value vs. 0: 45.90, p-value vs. 0: 1.6074474795035515e-99
Condition 16: r=0.49, t-value vs. 0: 53.94, p-value vs. 0: 5.685183088190426e-111
Condition 17: r=0.43, t-value vs. 0: 40.82, p-value vs. 0: 2.3281228610833658e-91
Condition 18: r=0.36, t-value vs. 0: 34.80, p-value vs. 0: 1.4518101389018436e-80
Condition 19: r=0.46, t-value vs. 0: 43.29, p-value vs. 0: 2.0285542097030728e-95
Condition 20: r=0.48, t-value vs. 0: 45.10, p-value vs. 0: 2.7936889125350976e-98
Condition 21: r=0.47, t-value vs. 0: 49.18, p-value vs. 0: 2.1705995115290612e-104
Condition 22: r=0.38, t-value vs. 0: 35.30, p-value vs. 0: 1.6255049872010506e-81
Condition 23: r=0.43, t-value vs. 0: 39.18, p-value vs. 0: 1.4851370464419702e-88
Condition 24: r=0.45, t-value vs. 0: 42.90, p-value vs. 0: 8.61008381222551e-95
In [ ]:
print("===Compare NON-CIRCULAR Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTiming_noncirc_Actflow = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_noncirc_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_noncirc_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_noncirc_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare NON-CIRCULAR Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.42
Model2 mean Pearson r=0.85
R-value difference = -0.42
Model1 vs. Model2 T-value: -120.37, p-value: 3.1626918778290836e-170
 
Model1 mean % predicted variance explained R^2=-0.37
Model2 mean % predicted variance explained R^2=0.67
R^2 difference = -1.04
 
Model1 mean MAE = 12.32
Model2 mean MAE = 5.74
Model1 vs. Model2 mean MAE difference = 6.58
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.45
Model2 mean Pearson r=0.85
R-value difference = -0.40
Model1 vs. Model2 T-value: -163.47, p-value: 2.796585046912401e-193
 
Model1 mean % predicted variance explained R^2=-1.86
Model2 mean % predicted variance explained R^2=0.30
R^2 difference = -2.16
 
Model1 mean MAE = 12.32
Model2 mean MAE = 5.74
Model1 vs. Model2 mean MAE difference = 6.58
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.42
Model2 mean Pearson r=0.88
R-value difference = -0.46
Model1 vs. Model2 T-value: -205.60, p-value: 1.2906434566816231e-210
By task condition:
Condition 1: Model 1 r=0.38, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.41, t-value Model1 vs. Model2: -51.33, p-value vs. 0: 2.0245582470021835e-107
Condition 2: Model 1 r=0.35, Model 2 r=0.84, Model 1 vs. 2 R-value difference =-0.49, t-value Model1 vs. Model2: -68.77, p-value vs. 0: 1.3480920610788183e-128
Condition 3: Model 1 r=0.50, Model 2 r=0.96, Model 1 vs. 2 R-value difference =-0.47, t-value Model1 vs. Model2: -96.89, p-value vs. 0: 5.61843930516034e-154
Condition 4: Model 1 r=0.50, Model 2 r=0.97, Model 1 vs. 2 R-value difference =-0.47, t-value Model1 vs. Model2: -106.56, p-value vs. 0: 4.345006805463182e-161
Condition 5: Model 1 r=0.28, Model 2 r=0.70, Model 1 vs. 2 R-value difference =-0.41, t-value Model1 vs. Model2: -46.13, p-value vs. 0: 7.190160808936721e-100
Condition 6: Model 1 r=0.25, Model 2 r=0.64, Model 1 vs. 2 R-value difference =-0.40, t-value Model1 vs. Model2: -37.59, p-value vs. 0: 1.0066053630181818e-85
Condition 7: Model 1 r=0.51, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.39, t-value Model1 vs. Model2: -62.79, p-value vs. 0: 5.877435600251362e-122
Condition 8: Model 1 r=0.34, Model 2 r=0.74, Model 1 vs. 2 R-value difference =-0.40, t-value Model1 vs. Model2: -35.96, p-value vs. 0: 9.666691728807378e-83
Condition 9: Model 1 r=0.33, Model 2 r=0.68, Model 1 vs. 2 R-value difference =-0.35, t-value Model1 vs. Model2: -35.38, p-value vs. 0: 1.1797545912105354e-81
Condition 10: Model 1 r=0.34, Model 2 r=0.69, Model 1 vs. 2 R-value difference =-0.35, t-value Model1 vs. Model2: -37.97, p-value vs. 0: 2.0730344960368075e-86
Condition 11: Model 1 r=0.32, Model 2 r=0.70, Model 1 vs. 2 R-value difference =-0.38, t-value Model1 vs. Model2: -41.13, p-value vs. 0: 7.134374624766148e-92
Condition 12: Model 1 r=0.34, Model 2 r=0.68, Model 1 vs. 2 R-value difference =-0.35, t-value Model1 vs. Model2: -38.58, p-value vs. 0: 1.7134738236869497e-87
Condition 13: Model 1 r=0.53, Model 2 r=0.95, Model 1 vs. 2 R-value difference =-0.42, t-value Model1 vs. Model2: -73.69, p-value vs. 0: 1.13014942621461e-133
Condition 14: Model 1 r=0.55, Model 2 r=0.97, Model 1 vs. 2 R-value difference =-0.42, t-value Model1 vs. Model2: -86.19, p-value vs. 0: 2.8873883986201024e-145
Condition 15: Model 1 r=0.48, Model 2 r=0.97, Model 1 vs. 2 R-value difference =-0.49, t-value Model1 vs. Model2: -113.02, p-value vs. 0: 1.7079221045252631e-165
Condition 16: Model 1 r=0.49, Model 2 r=0.96, Model 1 vs. 2 R-value difference =-0.46, t-value Model1 vs. Model2: -99.94, p-value vs. 0: 2.743310759291159e-156
Condition 17: Model 1 r=0.43, Model 2 r=0.83, Model 1 vs. 2 R-value difference =-0.41, t-value Model1 vs. Model2: -46.28, p-value vs. 0: 4.3618351270638947e-100
Condition 18: Model 1 r=0.36, Model 2 r=0.86, Model 1 vs. 2 R-value difference =-0.49, t-value Model1 vs. Model2: -49.43, p-value vs. 0: 9.736849517742778e-105
Condition 19: Model 1 r=0.46, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.45, t-value Model1 vs. Model2: -63.24, p-value vs. 0: 1.770550667174863e-122
Condition 20: Model 1 r=0.48, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.44, t-value Model1 vs. Model2: -68.69, p-value vs. 0: 1.631598159162885e-128
Condition 21: Model 1 r=0.47, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.45, t-value Model1 vs. Model2: -63.68, p-value vs. 0: 5.622747138768535e-123
Condition 22: Model 1 r=0.38, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.54, t-value Model1 vs. Model2: -70.51, p-value vs. 0: 1.9539028085059916e-130
Condition 23: Model 1 r=0.43, Model 2 r=0.87, Model 1 vs. 2 R-value difference =-0.44, t-value Model1 vs. Model2: -42.66, p-value vs. 0: 2.066816987648984e-94
Condition 24: Model 1 r=0.45, Model 2 r=0.82, Model 1 vs. 2 R-value difference =-0.37, t-value Model1 vs. Model2: -30.09, p-value vs. 0: 4.3991391764344e-71
In [ ]:
#Visualize predicted and actual activations
fig = plt.figure(figsize=[14,4])
ax1 = fig.add_subplot(131)
ax2 = fig.add_subplot(132)
ax3 = fig.add_subplot(133)
ax1.title.set_text('Pred: Rest multreg FC (matched time)')
ax1.imshow(np.mean(actflowOutput_restFCMReg_noncirc_bycond['actPredVector_bytask_bysubj'],axis=2)[netorder,:],aspect='auto',origin='lower')
ax2.title.set_text('Actual activations')
ax2.imshow(np.mean(activations_bycond,axis=2)[netorder,:],aspect='auto',origin='lower')
ax3.title.set_text('Pred: Task multreg FC')
ax3.imshow(np.mean(actflowOutput_taskFCMReg_noncirc_bycond['actPredVector_bytask_bysubj'],axis=2)[netorder,:],aspect='auto',origin='lower')
plt.show()
In [ ]:
#Compare task-state and resting-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset=np.arctanh(actflowOutput_taskFCMReg_noncirc_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
restdataset=np.arctanh(actflowOutput_restFCMReg_noncirc_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset,restdataset)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)

#Max and min values
print('Maximum of map: ', np.max(datamat[datamat!=0]))
print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 100.0
Maximum of map:  0.6253418153931153
Minimum of map:  0.16809816330777846

Multreg-based task-state FC - Task-state FC tests (double cross-validation)

In [49]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved, RUN 1 ONLY

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_run1_data'

taskFC_run1_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_run1_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    taskdata_condonly=taskdata[:,tasktiming]\n")
        file_python.write("    taskdata_condonly_run1only=taskdata_condonly[:,0:int(np.sum(tasktiming)/2)]\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata_condonly_run1only)\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:20:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
# plt.imshow(np.mean(taskFC_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskFC_run1_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC, half data only
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, just run 1==")
actflowOutput_taskFCMReg_run1only_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj, full_report=True, avgthencomp_fixedeffects=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, just run 1==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.72, t-value vs. 0: 112.38, p-value vs. 0: 4.505832001999541e-165
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.49
 
Mean MAE (mean absolute error) = 8.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.72, t-value vs. 0: 102.45, p-value vs. 0: 3.7988357281769215e-158
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.07
 
Mean MAE (mean absolute error) = 8.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Mean Pearson r=0.94
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=2.93
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.69, t-value vs. 0: 133.70, p-value vs. 0: 4.0123997558696656e-178
By task condition:
Condition 1: r=0.76, t-value vs. 0: 67.89, p-value vs. 0: 1.180471818145951e-127
Condition 2: r=0.77, t-value vs. 0: 67.88, p-value vs. 0: 1.208858511737846e-127
Condition 3: r=0.84, t-value vs. 0: 71.57, p-value vs. 0: 1.5742087834211462e-131
Condition 4: r=0.83, t-value vs. 0: 67.93, p-value vs. 0: 1.0597238479203442e-127
Condition 5: r=0.72, t-value vs. 0: 63.67, p-value vs. 0: 5.757208606271354e-123
Condition 6: r=0.71, t-value vs. 0: 58.97, p-value vs. 0: 2.122167751530994e-117
Condition 7: r=0.55, t-value vs. 0: 42.20, p-value vs. 0: 1.1893811872359725e-93
Condition 8: r=0.49, t-value vs. 0: 36.94, p-value vs. 0: 1.5188472096337996e-84
Condition 9: r=0.51, t-value vs. 0: 43.87, p-value vs. 0: 2.4029673303976156e-96
Condition 10: r=0.50, t-value vs. 0: 37.66, p-value vs. 0: 7.287388305973022e-86
Condition 11: r=0.47, t-value vs. 0: 41.51, p-value vs. 0: 1.6504475796024624e-92
Condition 12: r=0.51, t-value vs. 0: 42.24, p-value vs. 0: 1.0115066310690593e-93
Condition 13: r=0.84, t-value vs. 0: 66.09, p-value vs. 0: 1.099022574000902e-125
Condition 14: r=0.83, t-value vs. 0: 63.78, p-value vs. 0: 4.2886077569497126e-123
Condition 15: r=0.86, t-value vs. 0: 70.86, p-value vs. 0: 8.572081367344696e-131
Condition 16: r=0.87, t-value vs. 0: 79.40, p-value vs. 0: 3.488776822926217e-139
Condition 17: r=0.60, t-value vs. 0: 51.65, p-value vs. 0: 7.18624546888985e-108
Condition 18: r=0.54, t-value vs. 0: 41.13, p-value vs. 0: 7.115267754070758e-92
Condition 19: r=0.64, t-value vs. 0: 61.50, p-value vs. 0: 1.9306369385795633e-120
Condition 20: r=0.65, t-value vs. 0: 59.55, p-value vs. 0: 4.222665016806306e-118
Condition 21: r=0.65, t-value vs. 0: 57.97, p-value vs. 0: 3.636698817995383e-116
Condition 22: r=0.64, t-value vs. 0: 58.82, p-value vs. 0: 3.295475763939339e-117
Condition 23: r=0.65, t-value vs. 0: 58.85, p-value vs. 0: 3.032682359357081e-117
Condition 24: r=0.62, t-value vs. 0: 54.30, p-value vs. 0: 1.9124349759560644e-111
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean r=0.98
By task condition:
Condition 1: r=0.99
Condition 2: r=0.98
Condition 3: r=1.00
Condition 4: r=1.00
Condition 5: r=0.98
Condition 6: r=0.98
Condition 7: r=0.94
Condition 8: r=0.90
Condition 9: r=0.93
Condition 10: r=0.90
Condition 11: r=0.88
Condition 12: r=0.93
Condition 13: r=1.00
Condition 14: r=1.00
Condition 15: r=1.00
Condition 16: r=1.00
Condition 17: r=0.97
Condition 18: r=0.93
Condition 19: r=0.97
Condition 20: r=0.98
Condition 21: r=0.98
Condition 22: r=0.97
Condition 23: r=0.98
Condition 24: r=0.97
In [ ]:
#Run actflow predictions, 24-condition task multiple-regression FC, double cross-validation
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, DOUBLE CROSS-VALIDATION==")
actflowOutput_taskFCMReg_compRun2_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj, actVect_group_test=actvect_splitbyrun_group_run2s, full_report=True, avgthencomp_fixedeffects=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, DOUBLE CROSS-VALIDATION==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.40, t-value vs. 0: 57.31, p-value vs. 0: 2.501792930068209e-115
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.08
 
Mean MAE (mean absolute error) = 14.47
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.31, t-value vs. 0: 49.83, p-value vs. 0: 2.5471269606113633e-105
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.75
 
Mean MAE (mean absolute error) = 14.47
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Mean Pearson r=0.82
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=4.21
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.43, t-value vs. 0: 67.71, p-value vs. 0: 1.8336668922934905e-127
By task condition:
Condition 1: r=0.17, t-value vs. 0: 10.78, p-value vs. 0: 4.342820410032769e-21
Condition 2: r=0.12, t-value vs. 0: 7.22, p-value vs. 0: 1.5141621704545175e-11
Condition 3: r=0.62, t-value vs. 0: 42.39, p-value vs. 0: 5.732218852751475e-94
Condition 4: r=0.63, t-value vs. 0: 43.18, p-value vs. 0: 3.0251954226050614e-95
Condition 5: r=0.30, t-value vs. 0: 26.86, p-value vs. 0: 5.553891532698913e-64
Condition 6: r=0.29, t-value vs. 0: 27.00, p-value vs. 0: 2.6387136456469804e-64
Condition 7: r=0.43, t-value vs. 0: 33.33, p-value vs. 0: 1.0593013424251182e-77
Condition 8: r=0.19, t-value vs. 0: 12.20, p-value vs. 0: 3.6309029691565335e-25
Condition 9: r=0.13, t-value vs. 0: 8.00, p-value vs. 0: 1.6279551999515727e-13
Condition 10: r=0.12, t-value vs. 0: 8.55, p-value vs. 0: 5.815334282065672e-15
Condition 11: r=0.13, t-value vs. 0: 9.79, p-value vs. 0: 2.583744285789256e-18
Condition 12: r=0.14, t-value vs. 0: 9.26, p-value vs. 0: 7.268580397772852e-17
Condition 13: r=0.67, t-value vs. 0: 50.41, p-value vs. 0: 3.8377202630259556e-106
Condition 14: r=0.65, t-value vs. 0: 46.99, p-value vs. 0: 3.686215953636064e-101
Condition 15: r=0.65, t-value vs. 0: 51.71, p-value vs. 0: 5.995223533877907e-108
Condition 16: r=0.63, t-value vs. 0: 45.45, p-value vs. 0: 7.894208224983104e-99
Condition 17: r=0.45, t-value vs. 0: 32.59, p-value vs. 0: 3.0642896259462327e-76
Condition 18: r=0.39, t-value vs. 0: 26.85, p-value vs. 0: 6.004850344856814e-64
Condition 19: r=0.55, t-value vs. 0: 42.21, p-value vs. 0: 1.1560440283118185e-93
Condition 20: r=0.48, t-value vs. 0: 31.09, p-value vs. 0: 3.4946292929992066e-73
Condition 21: r=0.51, t-value vs. 0: 32.02, p-value vs. 0: 4.3562841890162845e-75
Condition 22: r=0.51, t-value vs. 0: 34.61, p-value vs. 0: 3.362281019745807e-80
Condition 23: r=0.54, t-value vs. 0: 41.95, p-value vs. 0: 3.0648390228335603e-93
Condition 24: r=0.56, t-value vs. 0: 46.70, p-value vs. 0: 1.0033495354760121e-100
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean r=0.92
By task condition:
Condition 1: r=0.72
Condition 2: r=0.60
Condition 3: r=0.97
Condition 4: r=0.97
Condition 5: r=0.90
Condition 6: r=0.83
Condition 7: r=0.93
Condition 8: r=0.74
Condition 9: r=0.76
Condition 10: r=0.64
Condition 11: r=0.81
Condition 12: r=0.76
Condition 13: r=0.99
Condition 14: r=0.99
Condition 15: r=0.95
Condition 16: r=0.98
Condition 17: r=0.94
Condition 18: r=0.84
Condition 19: r=0.97
Condition 20: r=0.94
Condition 21: r=0.92
Condition 22: r=0.95
Condition 23: r=0.94
Condition 24: r=0.97

Resting-state FC double-cross validation (match time to task)

In [51]:
## Calculating multreg REST FC, matched timing with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFCMatchedTiming_run1only_data'

restFCMatchedTiming_mreg_run1_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFCMatchedTiming_mreg_run1_bycond_bysubj[:,:,condnum,scount] = h5f['restFCRegressionMatchTiming'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    restdata_condonly=restdata[:,tasktiming]\n")
        file_python.write("    restdata_condonly_run1only=restdata_condonly[:,0:int(np.sum(tasktiming)/2)]\n")
        file_python.write("    restFCMatchedTiming_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(restdata_condonly_run1only)\n")
        file_python.write("    #Save multiple-regression FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegressionMatchTiming/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:20:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [ ]:
# plt.imshow(np.mean(taskFC_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFCMatchedTiming_mreg_run1_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[ ]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [ ]:
#Run actflow predictions, 24-condition rest multiple-regression FC, double cross-validation
print("==Activity flow mapping results, multiple-regression-based resting-state FC (matched time), 24 task conditions, DOUBLE CROSS-VALIDATION==")
actflowOutput_restFCMRegTimematched_compRun2_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, restFCMatchedTiming_mreg_run1_bycond_bysubj, actVect_group_test=actvect_splitbyrun_group_run2s, full_report=True, avgthencomp_fixedeffects=True)
==Activity flow mapping results, multiple-regression-based resting-state FC (matched time), 24 task conditions, DOUBLE CROSS-VALIDATION==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.27, t-value vs. 0: 47.74, p-value vs. 0: 2.770987895206258e-102
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.19
 
Mean MAE (mean absolute error) = 15.36
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.22, t-value vs. 0: 37.60, p-value vs. 0: 9.436842879534007e-86
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.89
 
Mean MAE (mean absolute error) = 15.36
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise correlations between predicted and actual activation patterns (calculated for each node separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N conditions: 24
Mean Pearson r=0.68
 
==Condition-wise Mean Absolute Error (MAE) between predicted and actual activation patterns (calculated for each node separateley):==
--Average-then-compare (calculating MAE accuracies after cross-subject averaging):
Each MAE based on N conditions: 24
Mean MAE=6.24
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.25, t-value vs. 0: 55.60, p-value vs. 0: 3.7697603544941394e-113
By task condition:
Condition 1: r=0.11, t-value vs. 0: 8.62, p-value vs. 0: 3.878107366108956e-15
Condition 2: r=0.07, t-value vs. 0: 5.01, p-value vs. 0: 1.3270312390524547e-06
Condition 3: r=0.37, t-value vs. 0: 31.68, p-value vs. 0: 2.1259741144705407e-74
Condition 4: r=0.39, t-value vs. 0: 31.59, p-value vs. 0: 3.3547405059901946e-74
Condition 5: r=0.22, t-value vs. 0: 25.08, p-value vs. 0: 7.639945010513375e-60
Condition 6: r=0.20, t-value vs. 0: 24.01, p-value vs. 0: 2.902696990267861e-57
Condition 7: r=0.35, t-value vs. 0: 28.20, p-value vs. 0: 5.576443125094201e-67
Condition 8: r=0.11, t-value vs. 0: 9.34, p-value vs. 0: 4.51076840235449e-17
Condition 9: r=0.09, t-value vs. 0: 6.85, p-value vs. 0: 1.229401081482412e-10
Condition 10: r=0.08, t-value vs. 0: 6.41, p-value vs. 0: 1.3299939014047802e-09
Condition 11: r=0.09, t-value vs. 0: 8.29, p-value vs. 0: 2.9297266227694867e-14
Condition 12: r=0.09, t-value vs. 0: 7.58, p-value vs. 0: 1.8974299481702792e-12
Condition 13: r=0.42, t-value vs. 0: 40.75, p-value vs. 0: 3.022480158526686e-91
Condition 14: r=0.44, t-value vs. 0: 40.73, p-value vs. 0: 3.3127374800634703e-91
Condition 15: r=0.37, t-value vs. 0: 35.68, p-value vs. 0: 3.1883298965063416e-82
Condition 16: r=0.37, t-value vs. 0: 35.15, p-value vs. 0: 3.181559645046282e-81
Condition 17: r=0.26, t-value vs. 0: 23.36, p-value vs. 0: 1.1120653634555163e-55
Condition 18: r=0.19, t-value vs. 0: 17.93, p-value vs. 0: 1.747864412647804e-41
Condition 19: r=0.34, t-value vs. 0: 30.92, p-value vs. 0: 7.948306231862956e-73
Condition 20: r=0.30, t-value vs. 0: 27.85, p-value vs. 0: 3.211242665110704e-66
Condition 21: r=0.29, t-value vs. 0: 29.60, p-value vs. 0: 4.971613402783162e-70
Condition 22: r=0.24, t-value vs. 0: 22.38, p-value vs. 0: 3.0750029446379615e-53
Condition 23: r=0.28, t-value vs. 0: 27.23, p-value vs. 0: 8.239053658181034e-65
Condition 24: r=0.31, t-value vs. 0: 28.87, p-value vs. 0: 1.822103400615612e-68
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Average-then-compare (calculating prediction accuracies after cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Mean r=0.78
By task condition:
Condition 1: r=0.53
Condition 2: r=0.42
Condition 3: r=0.86
Condition 4: r=0.86
Condition 5: r=0.84
Condition 6: r=0.74
Condition 7: r=0.85
Condition 8: r=0.57
Condition 9: r=0.59
Condition 10: r=0.44
Condition 11: r=0.58
Condition 12: r=0.58
Condition 13: r=0.91
Condition 14: r=0.91
Condition 15: r=0.86
Condition 16: r=0.86
Condition 17: r=0.82
Condition 18: r=0.69
Condition 19: r=0.84
Condition 20: r=0.82
Condition 21: r=0.80
Condition 22: r=0.83
Condition 23: r=0.82
Condition 24: r=0.82
In [ ]:
print("===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, DOUBLE CROSS-VALIDATION===")
model_compare_TaskMultRegFCVsRestMultRegFCMatchTimingActflow_doublecrossval = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run2s, model1_actvect=actflowOutput_restFCMRegTimematched_compRun2_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_compRun2_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare Task multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction, DOUBLE CROSS-VALIDATION===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.27
Model2 mean Pearson r=0.40
R-value difference = -0.13
Model1 vs. Model2 T-value: -37.59, p-value: 9.945865088085409e-86
 
Model1 mean % predicted variance explained R^2=-0.19
Model2 mean % predicted variance explained R^2=-0.08
R^2 difference = -0.11
 
Model1 mean MAE = 15.36
Model2 mean MAE = 14.47
Model1 vs. Model2 mean MAE difference = 0.90
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.22
Model2 mean Pearson r=0.31
R-value difference = -0.09
Model1 vs. Model2 T-value: -30.99, p-value: 5.672477589214232e-73
 
Model1 mean % predicted variance explained R^2=-0.89
Model2 mean % predicted variance explained R^2=-0.75
R^2 difference = -0.14
 
Model1 mean MAE = 15.36
Model2 mean MAE = 14.47
Model1 vs. Model2 mean MAE difference = 0.90
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.25
Model2 mean Pearson r=0.43
R-value difference = -0.18
Model1 vs. Model2 T-value: -50.00, p-value: 1.4744088269835504e-105
By task condition:
Condition 1: Model 1 r=0.11, Model 2 r=0.17, Model 1 vs. 2 R-value difference =-0.06, t-value Model1 vs. Model2: -6.81, p-value vs. 0: 1.5258684240958454e-10
Condition 2: Model 1 r=0.07, Model 2 r=0.12, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -6.54, p-value vs. 0: 6.539048636569323e-10
Condition 3: Model 1 r=0.37, Model 2 r=0.62, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -28.77, p-value vs. 0: 3.086905935989564e-68
Condition 4: Model 1 r=0.39, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -29.69, p-value vs. 0: 3.207997048703058e-70
Condition 5: Model 1 r=0.22, Model 2 r=0.30, Model 1 vs. 2 R-value difference =-0.08, t-value Model1 vs. Model2: -11.59, p-value vs. 0: 2.1110327766868073e-23
Condition 6: Model 1 r=0.20, Model 2 r=0.29, Model 1 vs. 2 R-value difference =-0.09, t-value Model1 vs. Model2: -14.33, p-value vs. 0: 2.5517396393504085e-31
Condition 7: Model 1 r=0.35, Model 2 r=0.43, Model 1 vs. 2 R-value difference =-0.08, t-value Model1 vs. Model2: -8.47, p-value vs. 0: 9.891153063648919e-15
Condition 8: Model 1 r=0.11, Model 2 r=0.19, Model 1 vs. 2 R-value difference =-0.08, t-value Model1 vs. Model2: -7.14, p-value vs. 0: 2.3776801833910366e-11
Condition 9: Model 1 r=0.09, Model 2 r=0.13, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -3.05, p-value vs. 0: 0.0026084812803570745
Condition 10: Model 1 r=0.08, Model 2 r=0.12, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -3.66, p-value vs. 0: 0.0003377946041469303
Condition 11: Model 1 r=0.09, Model 2 r=0.13, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -4.10, p-value vs. 0: 6.329815468275973e-05
Condition 12: Model 1 r=0.09, Model 2 r=0.14, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -4.69, p-value vs. 0: 5.568316999555576e-06
Condition 13: Model 1 r=0.42, Model 2 r=0.67, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -27.85, p-value vs. 0: 3.2010775651021603e-66
Condition 14: Model 1 r=0.44, Model 2 r=0.65, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -22.72, p-value vs. 0: 4.43335394332843e-54
Condition 15: Model 1 r=0.37, Model 2 r=0.65, Model 1 vs. 2 R-value difference =-0.28, t-value Model1 vs. Model2: -33.77, p-value vs. 0: 1.448792167005244e-78
Condition 16: Model 1 r=0.37, Model 2 r=0.63, Model 1 vs. 2 R-value difference =-0.26, t-value Model1 vs. Model2: -31.85, p-value vs. 0: 9.528773117730648e-75
Condition 17: Model 1 r=0.26, Model 2 r=0.45, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -18.44, p-value vs. 0: 7.23234897770135e-43
Condition 18: Model 1 r=0.19, Model 2 r=0.39, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -18.24, p-value vs. 0: 2.5152725427124973e-42
Condition 19: Model 1 r=0.34, Model 2 r=0.55, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -19.84, p-value vs. 0: 1.176889591102412e-46
Condition 20: Model 1 r=0.30, Model 2 r=0.48, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -15.13, p-value vs. 0: 1.2919061163485905e-33
Condition 21: Model 1 r=0.29, Model 2 r=0.51, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -16.04, p-value vs. 0: 3.4310852788477046e-36
Condition 22: Model 1 r=0.24, Model 2 r=0.51, Model 1 vs. 2 R-value difference =-0.27, t-value Model1 vs. Model2: -22.29, p-value vs. 0: 5.4311083710700135e-53
Condition 23: Model 1 r=0.28, Model 2 r=0.54, Model 1 vs. 2 R-value difference =-0.26, t-value Model1 vs. Model2: -24.17, p-value vs. 0: 1.1711800786844657e-57
Condition 24: Model 1 r=0.31, Model 2 r=0.56, Model 1 vs. 2 R-value difference =-0.25, t-value Model1 vs. Model2: -24.73, p-value vs. 0: 5.193450664061956e-59
In [ ]:
#Calculate percent of regions showing effect 

#Compute p-value threshold, using maxT correction for multiple comparisons
taskdataset=np.arctanh(actflowOutput_taskFCMReg_compRun2_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
restdataset=np.arctanh(actflowOutput_restFCMRegTimematched_compRun2_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset,restdataset)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 48.333333333333336

Group vs. individual task FC actflow predictions (inspired by Gratton2018)

Hypothesis: the task vs. rest FC boost to actflow will be much smaller using other subjects' FC relative to same-subject FC, due to large individualized task vs. rest FC effects

Focus on FC matrix similarity (replicating Gratton2018)

In [18]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved, RUN 2 ONLY

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_run2_data'

taskFC_run2_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_run2_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
        
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    taskdata_condonly=taskdata[:,tasktiming]\n")
        file_python.write("    taskdata_condonly_run2only=taskdata_condonly[:,int(np.sum(tasktiming)/2):int(np.sum(tasktiming)+1)]\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata_condonly_run2only)\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:5:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [19]:
tasktiming_bycond=tgp.loadTaskTimingForAllTasks(subjNums[0],taskModel='canonical')['taskRegressors']>.5
print('Number of time points by task condition (2 runs):')
for condnum in np.arange(24):
    timepoints=np.sum(tasktiming_bycond[:,condnum])
    print(condnum, ":", timepoints, "(", timepoints/.72, "seconds)")
Number of time points by task condition (2 runs):
0 : 124 ( 172.22222222222223 seconds)
1 : 150 ( 208.33333333333334 seconds)
2 : 152 ( 211.11111111111111 seconds)
3 : 152 ( 211.11111111111111 seconds)
4 : 283 ( 393.05555555555554 seconds)
5 : 307 ( 426.3888888888889 seconds)
6 : 66 ( 91.66666666666667 seconds)
7 : 64 ( 88.88888888888889 seconds)
8 : 64 ( 88.88888888888889 seconds)
9 : 64 ( 88.88888888888889 seconds)
10 : 64 ( 88.88888888888889 seconds)
11 : 64 ( 88.88888888888889 seconds)
12 : 132 ( 183.33333333333334 seconds)
13 : 132 ( 183.33333333333334 seconds)
14 : 155 ( 215.2777777777778 seconds)
15 : 155 ( 215.2777777777778 seconds)
16 : 76 ( 105.55555555555556 seconds)
17 : 76 ( 105.55555555555556 seconds)
18 : 76 ( 105.55555555555556 seconds)
19 : 76 ( 105.55555555555556 seconds)
20 : 76 ( 105.55555555555556 seconds)
21 : 76 ( 105.55555555555556 seconds)
22 : 76 ( 105.55555555555556 seconds)
23 : 76 ( 105.55555555555556 seconds)
In [20]:
tasktiming_bycond=tgp.loadTaskTimingForAllTasks(subjNums[0],taskModel='canonical')['taskRegressors']>.5
print('Number of time points by task condition (1 run):')
for condnum in np.arange(24):
    timepoints=np.sum(tasktiming_bycond[:,condnum])
    print(condnum, ":", .5*timepoints, "(", .5*timepoints/.72, "seconds)")
Number of time points by task condition (1 run):
0 : 62.0 ( 86.11111111111111 seconds)
1 : 75.0 ( 104.16666666666667 seconds)
2 : 76.0 ( 105.55555555555556 seconds)
3 : 76.0 ( 105.55555555555556 seconds)
4 : 141.5 ( 196.52777777777777 seconds)
5 : 153.5 ( 213.19444444444446 seconds)
6 : 33.0 ( 45.833333333333336 seconds)
7 : 32.0 ( 44.44444444444444 seconds)
8 : 32.0 ( 44.44444444444444 seconds)
9 : 32.0 ( 44.44444444444444 seconds)
10 : 32.0 ( 44.44444444444444 seconds)
11 : 32.0 ( 44.44444444444444 seconds)
12 : 66.0 ( 91.66666666666667 seconds)
13 : 66.0 ( 91.66666666666667 seconds)
14 : 77.5 ( 107.6388888888889 seconds)
15 : 77.5 ( 107.6388888888889 seconds)
16 : 38.0 ( 52.77777777777778 seconds)
17 : 38.0 ( 52.77777777777778 seconds)
18 : 38.0 ( 52.77777777777778 seconds)
19 : 38.0 ( 52.77777777777778 seconds)
20 : 38.0 ( 52.77777777777778 seconds)
21 : 38.0 ( 52.77777777777778 seconds)
22 : 38.0 ( 52.77777777777778 seconds)
23 : 38.0 ( 52.77777777777778 seconds)
In [21]:
# plt.imshow(np.mean(taskFC_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),origin='lower')
# plt.colorbar()
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskFC_run2_mreg_bycond_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17, run2')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[21]:
[Text(83.40000000000006, 0.5, 'Nodes'), Text(0.5, 15.0, 'Nodes')]
In [48]:
taskFCmeanMat_mreg=np.mean(np.mean(taskFC_mreg_bycond_bysubj,axis=3),axis=2)
restFCmeanMat_mreg=np.mean(np.mean(restFCMatchedTiming_mreg_bycond_bysubj,axis=3),axis=2)
upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat_mreg)[0],k=1)
resttask_mattcorr=np.corrcoef(taskFCmeanMat_mreg[upperTIndices_corrmat], restFCmeanMat_mreg[upperTIndices_corrmat])
print('Mean task to rest FC similarity, mreg, AVERAGE-THEN-COMPARE: ', str(resttask_mattcorr[0,1]))
Mean task to rest FC similarity, mreg, AVERAGE-THEN-COMPARE:  0.9426993529331876
In [45]:
resttask_mattcorr_bysubj=np.zeros((nSubjs))
for subjnum in np.arange(nSubjs):
    taskFCmeanMat_mreg=np.mean(taskFC_mreg_bycond_bysubj[:,:,:,subjnum],axis=2)
    restFCmeanMat_mreg=np.mean(restFCMatchedTiming_mreg_bycond_bysubj[:,:,:,subjnum],axis=2)
    upperTIndices_corrmat=np.triu_indices(np.shape(taskFCmeanMat_mreg)[0],k=1)
    resttask_mattcorr_bysubj[subjnum]=np.corrcoef(taskFCmeanMat_mreg[upperTIndices_corrmat], restFCmeanMat_mreg[upperTIndices_corrmat])[0,1]
print('Mean task to rest FC similarity, mreg, COMPARE-THEN-AVERAGE: ', str(np.mean(resttask_mattcorr_bysubj)))
Mean task to rest FC similarity, mreg, COMPARE-THEN-AVERAGE:  0.19683023503316993
In [52]:
#Group rest [rest from other subjects], mean similarity to each condition run 1
mattcorr_grouprest_simtotaskconds_bycond_bysubj=np.zeros((nConds,nSubjs))
upperTIndices_corrmat=np.triu_indices(np.shape(taskFC_mreg_bycond_bysubj[:,:,0,0])[0],k=1)
subjorder_plus1=np.arange(1,176)
subjorder_plus1=np.append(subjorder_plus1, 0)
for subjnum in np.arange(nSubjs):
    for condnum in np.arange(nConds):
        taskFCMat_mreg=taskFC_run1_mreg_bycond_bysubj[:,:,condnum,subjnum]
        restFCMat_mreg=restFCMatchedTiming_mreg_run1_bycond_bysubj[:,:,condnum,subjorder_plus1[subjnum]]
        mattcorr_grouprest_simtotaskconds_bycond_bysubj[condnum,subjnum]=np.corrcoef(taskFCMat_mreg[upperTIndices_corrmat], restFCMat_mreg[upperTIndices_corrmat])[0,1]
print('Group rest: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE: ', str(np.mean(np.mean(mattcorr_grouprest_simtotaskconds_bycond_bysubj))))
Group rest: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE:  0.021808339105796205
In [53]:
#Group task [same task from other subjects], mean similarity to each condition run 1
mattcorr_grouptask_simtotaskconds_bycond_bysubj=np.zeros((nConds,nSubjs))
upperTIndices_corrmat=np.triu_indices(np.shape(taskFC_mreg_bycond_bysubj[:,:,0,0])[0],k=1)
subjorder_plus1=np.arange(1,176)
subjorder_plus1=np.append(subjorder_plus1, 0)
for subjnum in np.arange(nSubjs):
    for condnum in np.arange(nConds):
        taskFCMat_mreg=taskFC_run1_mreg_bycond_bysubj[:,:,condnum,subjnum]
        task2FCMat_mreg=taskFC_run1_mreg_bycond_bysubj[:,:,condnum,subjorder_plus1[subjnum]]
        mattcorr_grouptask_simtotaskconds_bycond_bysubj[condnum,subjnum]=np.corrcoef(taskFCMat_mreg[upperTIndices_corrmat], task2FCMat_mreg[upperTIndices_corrmat])[0,1]
print('Group task: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE: ', str(np.mean(np.mean(mattcorr_grouptask_simtotaskconds_bycond_bysubj))))
Group task: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE:  0.022937598588497275
In [54]:
#Individual rest [rest from same subject], mean similarity to each condition run 1
mattcorr_indivrest_simtotaskconds_bycond_bysubj=np.zeros((nConds,nSubjs))
upperTIndices_corrmat=np.triu_indices(np.shape(taskFC_mreg_bycond_bysubj[:,:,0,0])[0],k=1)
for subjnum in np.arange(nSubjs):
    for condnum in np.arange(nConds):
        taskFCMat_mreg=taskFC_run1_mreg_bycond_bysubj[:,:,condnum,subjnum]
        restFCMat_mreg=restFCMatchedTiming_mreg_run1_bycond_bysubj[:,:,condnum,subjnum]
        mattcorr_indivrest_simtotaskconds_bycond_bysubj[condnum,subjnum]=np.corrcoef(taskFCMat_mreg[upperTIndices_corrmat], restFCMat_mreg[upperTIndices_corrmat])[0,1]
print('Individual rest: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE: ', str(np.mean(np.mean(mattcorr_indivrest_simtotaskconds_bycond_bysubj))))
Individual rest: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE:  0.027606468569516485
In [55]:
#Individual task [same task (diff. run) same subject], mean similarity to each condition run 1
mattcorr_indivtask_simtotaskconds_bycond_bysubj=np.zeros((nConds,nSubjs))
upperTIndices_corrmat=np.triu_indices(np.shape(taskFC_mreg_bycond_bysubj[:,:,0,0])[0],k=1)
for subjnum in np.arange(nSubjs):
    for condnum in np.arange(nConds):
        taskFCMat_mreg=taskFC_run1_mreg_bycond_bysubj[:,:,condnum,subjnum]
        task2FCMat_mreg=taskFC_run2_mreg_bycond_bysubj[:,:,condnum,subjnum]
        mattcorr_indivtask_simtotaskconds_bycond_bysubj[condnum,subjnum]=np.corrcoef(taskFCMat_mreg[upperTIndices_corrmat], task2FCMat_mreg[upperTIndices_corrmat])[0,1]
print('Individual task: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE: ', str(np.mean(np.mean(mattcorr_indivtask_simtotaskconds_bycond_bysubj))))
Individual task: Mean similarity across task conditions, mreg, COMPARE-THEN-AVERAGE:  0.5945011983187481

Focus on actflow group vs. individual FC effects

In [59]:
#GROUPREST
print('Group rest [rest from other subjects], predicting run 1 activations for all task conditions')
#Run actflow predictions, 24-condition REST multiple-regression FC FROM OTHER SUBJECTS, run 1 only
print("==Activity flow mapping, multiple-regression-based REST FC FROM OTHER SUBJECTS, 24 task conditions FROM RUN 1==")

#Shift subject order by 1 (so each subject uses a different subject's FC)
# subjorder_plus1=np.arange(1,176)
# subjorder_plus1=np.append(subjorder_plus1, 0)
#Randomly permute subject order, setting random seed so we always get the same order
subjorder_rand=np.arange(0,176)
np.random.seed(51)
subjorder_rand=np.random.permutation(subjorder_rand)
restFCMatchedTiming_mreg_run1_bycond_bysubj_subjordershift=restFCMatchedTiming_mreg_run1_bycond_bysubj[:,:,:,subjorder_rand].copy()
actflowOutput_restFCMReg_run1only_othersubj_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, restFCMatchedTiming_mreg_run1_bycond_bysubj_subjordershift, full_report=True)
Group rest [rest from other subjects], predicting run 1 activations for all task conditions
==Activity flow mapping, multiple-regression-based REST FC FROM OTHER SUBJECTS, 24 task conditions FROM RUN 1==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.43, t-value vs. 0: 71.10, p-value vs. 0: 4.868246623070917e-131
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.06
 
Mean MAE (mean absolute error) = 12.48
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.45, t-value vs. 0: 58.05, p-value vs. 0: 2.923521952651892e-116
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.63
 
Mean MAE (mean absolute error) = 12.48
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.33, t-value vs. 0: 83.12, p-value vs. 0: 1.410124208662454e-142
By task condition:
Condition 1: r=0.34, t-value vs. 0: 32.00, p-value vs. 0: 4.75170271412462e-75
Condition 2: r=0.33, t-value vs. 0: 28.93, p-value vs. 0: 1.35482129631191e-68
Condition 3: r=0.42, t-value vs. 0: 42.54, p-value vs. 0: 3.287901213127612e-94
Condition 4: r=0.44, t-value vs. 0: 45.33, p-value vs. 0: 1.2382408942748159e-98
Condition 5: r=0.30, t-value vs. 0: 36.06, p-value vs. 0: 6.2074219204195805e-83
Condition 6: r=0.28, t-value vs. 0: 33.38, p-value vs. 0: 8.329287441466422e-78
Condition 7: r=0.35, t-value vs. 0: 38.05, p-value vs. 0: 1.4956488617897329e-86
Condition 8: r=0.24, t-value vs. 0: 24.00, p-value vs. 0: 3.0390212065714226e-57
Condition 9: r=0.23, t-value vs. 0: 24.99, p-value vs. 0: 1.2705436866888295e-59
Condition 10: r=0.21, t-value vs. 0: 22.52, p-value vs. 0: 1.3674595384203763e-53
Condition 11: r=0.21, t-value vs. 0: 22.87, p-value vs. 0: 1.8386607499742848e-54
Condition 12: r=0.21, t-value vs. 0: 22.55, p-value vs. 0: 1.15788878621737e-53
Condition 13: r=0.42, t-value vs. 0: 42.69, p-value vs. 0: 1.8541008034752075e-94
Condition 14: r=0.45, t-value vs. 0: 43.39, p-value vs. 0: 1.4123495145914719e-95
Condition 15: r=0.40, t-value vs. 0: 43.06, p-value vs. 0: 4.687147349956529e-95
Condition 16: r=0.44, t-value vs. 0: 44.00, p-value vs. 0: 1.4718890927666849e-96
Condition 17: r=0.32, t-value vs. 0: 33.19, p-value vs. 0: 1.9977539333724458e-77
Condition 18: r=0.26, t-value vs. 0: 28.39, p-value vs. 0: 2.1200723213939286e-67
Condition 19: r=0.35, t-value vs. 0: 34.28, p-value vs. 0: 1.4442392993599274e-79
Condition 20: r=0.36, t-value vs. 0: 41.20, p-value vs. 0: 5.402304451476606e-92
Condition 21: r=0.33, t-value vs. 0: 33.85, p-value vs. 0: 1.0198226966423365e-78
Condition 22: r=0.27, t-value vs. 0: 28.25, p-value vs. 0: 4.255470355552584e-67
Condition 23: r=0.33, t-value vs. 0: 33.53, p-value vs. 0: 4.271440918573852e-78
Condition 24: r=0.33, t-value vs. 0: 33.49, p-value vs. 0: 5.18455448943718e-78
In [60]:
#GROUPOTHERTASK
#Group task [other task from other subjects], predicting run 1 activations for all task conditions
#Run actflow predictions, 24-condition task multiple-regression FC FROM OTHER SUBJECTS, run 1 only
print("==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER TASKS & OTHER SUBJECTS, 24 task conditions FROM RUN 1==")

# taskConditions = ['xEMOTION:fear','xEMOTION:neut','xGAMBLING:win','xGAMBLING:loss','xLANGUAGE:story','xLANGUAGE:math',
#                   'MOTOR:cue','MOTOR:lf','MOTOR:rf','MOTOR:lh','MOTOR:rh','MOTOR:t','REASONING:rel',
#                   'xREASONING:match','xSOCIAL:mental','xSOCIAL:rnd','xWM 0bk:body','xWM 0bk:faces','xWM 0bk:places',
#                   'xWM 0bk:tools','xWM 2bk:body','xWM 2bk:faces','xWM 2bk:places','xWM 2bk:tools']

#Randomly permute subject order, setting random seed so we always get the same order
subjorder_rand=np.arange(0,176)
np.random.seed(51)
subjorder_rand=np.random.permutation(subjorder_rand)
taskFC_run1_mreg_bycond_bysubj_subjordershift=taskFC_run1_mreg_bycond_bysubj[:,:,:,subjorder_rand].copy()
#Shifting task condition order to make sure FC data not from the same task
taskorder_shift=np.array((22, 23, 20, 21, 18, 19, 17, 16, 15, 14, 13, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)).astype(int)
actflowOutput_taskFCMReg_run1only_othertask_othersubj_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj_subjordershift[:,:,taskorder_shift,:], full_report=True)
==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER TASKS & OTHER SUBJECTS, 24 task conditions FROM RUN 1==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.44, t-value vs. 0: 78.37, p-value vs. 0: 3.2161548888690556e-138
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.12
 
Mean MAE (mean absolute error) = 12.32
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.44, t-value vs. 0: 55.68, p-value vs. 0: 2.941159193668095e-113
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.50
 
Mean MAE (mean absolute error) = 12.32
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.34, t-value vs. 0: 85.08, p-value vs. 0: 2.6232775301947785e-144
By task condition:
Condition 1: r=0.32, t-value vs. 0: 30.83, p-value vs. 0: 1.20361438399139e-72
Condition 2: r=0.24, t-value vs. 0: 24.86, p-value vs. 0: 2.5426802370785136e-59
Condition 3: r=0.47, t-value vs. 0: 46.69, p-value vs. 0: 1.0487376200904801e-100
Condition 4: r=0.45, t-value vs. 0: 46.26, p-value vs. 0: 4.5548205243338655e-100
Condition 5: r=0.22, t-value vs. 0: 27.19, p-value vs. 0: 9.777022860796988e-65
Condition 6: r=0.21, t-value vs. 0: 26.86, p-value vs. 0: 5.6066911948977614e-64
Condition 7: r=0.37, t-value vs. 0: 42.32, p-value vs. 0: 7.567838930473234e-94
Condition 8: r=0.22, t-value vs. 0: 23.14, p-value vs. 0: 3.916664071086861e-55
Condition 9: r=0.28, t-value vs. 0: 33.59, p-value vs. 0: 3.2830325854825095e-78
Condition 10: r=0.28, t-value vs. 0: 30.61, p-value vs. 0: 3.5263510515611454e-72
Condition 11: r=0.23, t-value vs. 0: 27.50, p-value vs. 0: 1.982866087051074e-65
Condition 12: r=0.24, t-value vs. 0: 27.65, p-value vs. 0: 9.263944813619046e-66
Condition 13: r=0.50, t-value vs. 0: 51.07, p-value vs. 0: 4.563909863794995e-107
Condition 14: r=0.55, t-value vs. 0: 59.56, p-value vs. 0: 4.030597779566531e-118
Condition 15: r=0.41, t-value vs. 0: 44.73, p-value vs. 0: 1.0591169549897887e-97
Condition 16: r=0.45, t-value vs. 0: 50.42, p-value vs. 0: 3.786482885774073e-106
Condition 17: r=0.36, t-value vs. 0: 41.97, p-value vs. 0: 2.8469176344419816e-93
Condition 18: r=0.30, t-value vs. 0: 32.49, p-value vs. 0: 4.878432766614269e-76
Condition 19: r=0.31, t-value vs. 0: 34.79, p-value vs. 0: 1.5202873014836222e-80
Condition 20: r=0.34, t-value vs. 0: 36.19, p-value vs. 0: 3.635028391461076e-83
Condition 21: r=0.36, t-value vs. 0: 40.84, p-value vs. 0: 2.184772268729258e-91
Condition 22: r=0.32, t-value vs. 0: 36.05, p-value vs. 0: 6.634382884745126e-83
Condition 23: r=0.33, t-value vs. 0: 37.91, p-value vs. 0: 2.5965715724685253e-86
Condition 24: r=0.36, t-value vs. 0: 39.27, p-value vs. 0: 1.0405489294744655e-88
In [61]:
#GROUPTASK
#Group task [same task from other subjects], predicting run 1 activations for all task conditions
#Run actflow predictions, 24-condition task multiple-regression FC FROM OTHER SUBJECTS, run 1 only
print("==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER SUBJECTS, 24 task conditions FROM RUN 1==")

#Randomly permute subject order, setting random seed so we always get the same order
subjorder_rand=np.arange(0,176)
np.random.seed(51)
subjorder_rand=np.random.permutation(subjorder_rand)
taskFC_run1_mreg_bycond_bysubj_subjordershift=taskFC_run1_mreg_bycond_bysubj[:,:,:,subjorder_rand].copy()
actflowOutput_taskFCMReg_run1only_othersubj_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj_subjordershift, full_report=True)
==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER SUBJECTS, 24 task conditions FROM RUN 1==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.51, t-value vs. 0: 73.13, p-value vs. 0: 4.063815668701142e-133
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.15
 
Mean MAE (mean absolute error) = 11.72
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.52, t-value vs. 0: 65.27, p-value vs. 0: 8.914438949006622e-125
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.51
 
Mean MAE (mean absolute error) = 11.72
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.44, t-value vs. 0: 69.58, p-value vs. 0: 1.8543541768191162e-129
By task condition:
Condition 1: r=0.40, t-value vs. 0: 34.18, p-value vs. 0: 2.278992137911041e-79
Condition 2: r=0.36, t-value vs. 0: 25.55, p-value vs. 0: 6.1281223648522665e-61
Condition 3: r=0.54, t-value vs. 0: 46.80, p-value vs. 0: 6.985457200892111e-101
Condition 4: r=0.54, t-value vs. 0: 44.09, p-value vs. 0: 1.0568420273360674e-96
Condition 5: r=0.34, t-value vs. 0: 29.10, p-value vs. 0: 5.937110204431346e-69
Condition 6: r=0.31, t-value vs. 0: 30.32, p-value vs. 0: 1.4363637458965525e-71
Condition 7: r=0.40, t-value vs. 0: 37.66, p-value vs. 0: 7.529203499962993e-86
Condition 8: r=0.28, t-value vs. 0: 26.34, p-value vs. 0: 8.838234643070338e-63
Condition 9: r=0.28, t-value vs. 0: 30.19, p-value vs. 0: 2.6447009432004554e-71
Condition 10: r=0.26, t-value vs. 0: 23.97, p-value vs. 0: 3.518479848118796e-57
Condition 11: r=0.26, t-value vs. 0: 24.87, p-value vs. 0: 2.495420704583351e-59
Condition 12: r=0.26, t-value vs. 0: 25.36, p-value vs. 0: 1.7138584159092362e-60
Condition 13: r=0.57, t-value vs. 0: 44.97, p-value vs. 0: 4.379082165332031e-98
Condition 14: r=0.58, t-value vs. 0: 47.31, p-value vs. 0: 1.2175889205684793e-101
Condition 15: r=0.59, t-value vs. 0: 48.84, p-value vs. 0: 6.863270506342571e-104
Condition 16: r=0.62, t-value vs. 0: 54.37, p-value vs. 0: 1.5239988001652846e-111
Condition 17: r=0.45, t-value vs. 0: 42.24, p-value vs. 0: 1.0191206506749227e-93
Condition 18: r=0.39, t-value vs. 0: 35.89, p-value vs. 0: 1.2723495155821315e-82
Condition 19: r=0.48, t-value vs. 0: 48.52, p-value vs. 0: 1.9806735597271575e-103
Condition 20: r=0.49, t-value vs. 0: 46.41, p-value vs. 0: 2.7414128411914133e-100
Condition 21: r=0.50, t-value vs. 0: 53.28, p-value vs. 0: 4.350281369689017e-110
Condition 22: r=0.43, t-value vs. 0: 43.43, p-value vs. 0: 1.2119837926483853e-95
Condition 23: r=0.50, t-value vs. 0: 55.68, p-value vs. 0: 3.0098940840720545e-113
Condition 24: r=0.48, t-value vs. 0: 49.53, p-value vs. 0: 6.974195998535188e-105
In [62]:
#INDIVREST
#Individual rest [rest from same subject], predicting run 1 activations for all task conditions
#Run actflow predictions, 24-condition REST multiple-regression FC, SAME SUBJECT
print("==Activity flow mapping, multiple-regression-based REST FC, 24 task conditions FROM RUN 1, SAME SUBJECT==")
actflowOutput_taskFCMReg_run1only_FCfromRest_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, restFCMatchedTiming_mreg_run1_bycond_bysubj, full_report=True)
==Activity flow mapping, multiple-regression-based REST FC, 24 task conditions FROM RUN 1, SAME SUBJECT==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.48, t-value vs. 0: 73.46, p-value vs. 0: 1.889190123342921e-133
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.13
 
Mean MAE (mean absolute error) = 12.04
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.49, t-value vs. 0: 61.87, p-value vs. 0: 7.0580800717369565e-121
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.50
 
Mean MAE (mean absolute error) = 12.04
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.38, t-value vs. 0: 83.67, p-value vs. 0: 4.600301596379119e-143
By task condition:
Condition 1: r=0.40, t-value vs. 0: 34.53, p-value vs. 0: 4.86122002907832e-80
Condition 2: r=0.38, t-value vs. 0: 31.70, p-value vs. 0: 1.9415962192184615e-74
Condition 3: r=0.48, t-value vs. 0: 48.16, p-value vs. 0: 6.800111224374566e-103
Condition 4: r=0.50, t-value vs. 0: 45.71, p-value vs. 0: 3.22740003192866e-99
Condition 5: r=0.39, t-value vs. 0: 36.15, p-value vs. 0: 4.257835577844314e-83
Condition 6: r=0.37, t-value vs. 0: 42.37, p-value vs. 0: 6.317697919687225e-94
Condition 7: r=0.41, t-value vs. 0: 37.40, p-value vs. 0: 2.21728865237197e-85
Condition 8: r=0.27, t-value vs. 0: 25.86, p-value vs. 0: 1.1295825806756572e-61
Condition 9: r=0.27, t-value vs. 0: 26.27, p-value vs. 0: 1.253622347234406e-62
Condition 10: r=0.25, t-value vs. 0: 26.12, p-value vs. 0: 2.863373349334915e-62
Condition 11: r=0.24, t-value vs. 0: 24.15, p-value vs. 0: 1.303315877548417e-57
Condition 12: r=0.26, t-value vs. 0: 24.96, p-value vs. 0: 1.4977465460020647e-59
Condition 13: r=0.46, t-value vs. 0: 41.12, p-value vs. 0: 7.365636693219735e-92
Condition 14: r=0.49, t-value vs. 0: 46.12, p-value vs. 0: 7.546214408186524e-100
Condition 15: r=0.47, t-value vs. 0: 45.04, p-value vs. 0: 3.4475884361365286e-98
Condition 16: r=0.50, t-value vs. 0: 51.09, p-value vs. 0: 4.2929900993981214e-107
Condition 17: r=0.36, t-value vs. 0: 35.86, p-value vs. 0: 1.4924335133535901e-82
Condition 18: r=0.30, t-value vs. 0: 30.18, p-value vs. 0: 2.8166048019280257e-71
Condition 19: r=0.40, t-value vs. 0: 38.40, p-value vs. 0: 3.559818413436542e-87
Condition 20: r=0.39, t-value vs. 0: 40.51, p-value vs. 0: 7.785357486251923e-91
Condition 21: r=0.36, t-value vs. 0: 35.85, p-value vs. 0: 1.5258184933872985e-82
Condition 22: r=0.30, t-value vs. 0: 29.95, p-value vs. 0: 8.726344566554383e-71
Condition 23: r=0.37, t-value vs. 0: 36.88, p-value vs. 0: 1.9589050350805658e-84
Condition 24: r=0.37, t-value vs. 0: 35.39, p-value vs. 0: 1.1326562369386966e-81
In [63]:
#INDIVOTHERTASK
#Indiv other task [other task from same subject], predicting run 1 activations for all task conditions

print("==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER TASKS & SAME SUBJECTS, 24 task conditions FROM RUN 1==")

#Shifting task condition order to make sure FC data not from the same task
taskorder_shift=np.array((22, 23, 20, 21, 18, 19, 17, 16, 15, 14, 13, 12, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11)).astype(int)
actflowOutput_taskFCMReg_run1only_othertask_samesubj_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj[:,:,taskorder_shift,:], full_report=True)
==Activity flow mapping, multiple-regression-based task-state FC FROM OTHER TASKS & SAME SUBJECTS, 24 task conditions FROM RUN 1==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.49, t-value vs. 0: 79.74, p-value vs. 0: 1.6672432878096776e-139
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.18
 
Mean MAE (mean absolute error) = 11.80
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.47, t-value vs. 0: 56.16, p-value vs. 0: 7.254085172259574e-114
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.39
 
Mean MAE (mean absolute error) = 11.80
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.41, t-value vs. 0: 87.94, p-value vs. 0: 9.240938897611081e-147
By task condition:
Condition 1: r=0.38, t-value vs. 0: 35.44, p-value vs. 0: 9.194690626040335e-82
Condition 2: r=0.29, t-value vs. 0: 28.79, p-value vs. 0: 2.7450419743824974e-68
Condition 3: r=0.56, t-value vs. 0: 53.70, p-value vs. 0: 1.176553735463141e-110
Condition 4: r=0.54, t-value vs. 0: 52.58, p-value vs. 0: 3.836898163298574e-109
Condition 5: r=0.26, t-value vs. 0: 32.82, p-value vs. 0: 1.0788196540037262e-76
Condition 6: r=0.26, t-value vs. 0: 28.30, p-value vs. 0: 3.3497819957242077e-67
Condition 7: r=0.44, t-value vs. 0: 43.92, p-value vs. 0: 1.9901824464200396e-96
Condition 8: r=0.27, t-value vs. 0: 24.62, p-value vs. 0: 9.949856571860257e-59
Condition 9: r=0.35, t-value vs. 0: 35.91, p-value vs. 0: 1.1729956437232309e-82
Condition 10: r=0.35, t-value vs. 0: 30.23, p-value vs. 0: 2.2842807148059997e-71
Condition 11: r=0.29, t-value vs. 0: 29.55, p-value vs. 0: 6.211361737796393e-70
Condition 12: r=0.30, t-value vs. 0: 28.44, p-value vs. 0: 1.6079474516957894e-67
Condition 13: r=0.59, t-value vs. 0: 47.62, p-value vs. 0: 4.258124372851686e-102
Condition 14: r=0.63, t-value vs. 0: 57.29, p-value vs. 0: 2.647238108272504e-115
Condition 15: r=0.48, t-value vs. 0: 51.90, p-value vs. 0: 3.216942093107981e-108
Condition 16: r=0.52, t-value vs. 0: 55.26, p-value vs. 0: 1.0298890201616647e-112
Condition 17: r=0.44, t-value vs. 0: 44.21, p-value vs. 0: 6.819415857028976e-97
Condition 18: r=0.38, t-value vs. 0: 37.49, p-value vs. 0: 1.5179697072709332e-85
Condition 19: r=0.35, t-value vs. 0: 38.51, p-value vs. 0: 2.2996935403276905e-87
Condition 20: r=0.38, t-value vs. 0: 38.62, p-value vs. 0: 1.47732238795828e-87
Condition 21: r=0.39, t-value vs. 0: 43.16, p-value vs. 0: 3.293082057144808e-95
Condition 22: r=0.38, t-value vs. 0: 41.40, p-value vs. 0: 2.5091874164744437e-92
Condition 23: r=0.39, t-value vs. 0: 41.55, p-value vs. 0: 1.4086022437221495e-92
Condition 24: r=0.41, t-value vs. 0: 39.91, p-value vs. 0: 8.32527900323254e-90
In [64]:
#INDIVTASK
#Individual task [same task (diff. run) same subject], predicting run 1 activations for all task conditions
#Run actflow predictions, 24-condition task multiple-regression FC, SAME SUBJECT, FC FROM RUN 2
print("==Activity flow mapping, multiple-regression-based task-state FC, 24 task conditions FROM RUN 1, SAME SUBJECT, FC FROM RUN 2==")

actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run2_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping, multiple-regression-based task-state FC, 24 task conditions FROM RUN 1, SAME SUBJECT, FC FROM RUN 2==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.67, t-value vs. 0: 108.80, p-value vs. 0: 1.2022522377386535e-162
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.40
 
Mean MAE (mean absolute error) = 9.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.67, t-value vs. 0: 97.74, p-value vs. 0: 1.2605934619453193e-154
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.10
 
Mean MAE (mean absolute error) = 9.75
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.62, t-value vs. 0: 117.89, p-value vs. 0: 1.1543656580118703e-168
By task condition:
Condition 1: r=0.67, t-value vs. 0: 51.67, p-value vs. 0: 6.820986267600911e-108
Condition 2: r=0.65, t-value vs. 0: 50.35, p-value vs. 0: 4.660043475712204e-106
Condition 3: r=0.73, t-value vs. 0: 68.60, p-value vs. 0: 2.038276308974566e-128
Condition 4: r=0.73, t-value vs. 0: 69.75, p-value vs. 0: 1.2198434804393787e-129
Condition 5: r=0.57, t-value vs. 0: 51.78, p-value vs. 0: 4.750960168441139e-108
Condition 6: r=0.58, t-value vs. 0: 56.17, p-value vs. 0: 7.034870035803986e-114
Condition 7: r=0.53, t-value vs. 0: 42.60, p-value vs. 0: 2.6014131346825853e-94
Condition 8: r=0.44, t-value vs. 0: 34.64, p-value vs. 0: 2.9678930135042872e-80
Condition 9: r=0.46, t-value vs. 0: 42.23, p-value vs. 0: 1.0438349422667955e-93
Condition 10: r=0.42, t-value vs. 0: 39.46, p-value vs. 0: 4.84187577851763e-89
Condition 11: r=0.41, t-value vs. 0: 37.57, p-value vs. 0: 1.0747755354964567e-85
Condition 12: r=0.42, t-value vs. 0: 38.94, p-value vs. 0: 3.992406740583185e-88
Condition 13: r=0.74, t-value vs. 0: 56.42, p-value vs. 0: 3.3220345189033595e-114
Condition 14: r=0.74, t-value vs. 0: 57.74, p-value vs. 0: 7.244735083629709e-116
Condition 15: r=0.74, t-value vs. 0: 67.53, p-value vs. 0: 2.8958057269423116e-127
Condition 16: r=0.82, t-value vs. 0: 72.22, p-value vs. 0: 3.384436295166027e-132
Condition 17: r=0.60, t-value vs. 0: 51.62, p-value vs. 0: 7.981627784330728e-108
Condition 18: r=0.54, t-value vs. 0: 41.13, p-value vs. 0: 6.96113599503169e-92
Condition 19: r=0.64, t-value vs. 0: 61.50, p-value vs. 0: 1.9306369379454058e-120
Condition 20: r=0.65, t-value vs. 0: 60.09, p-value vs. 0: 9.356990624448583e-119
Condition 21: r=0.65, t-value vs. 0: 57.74, p-value vs. 0: 7.143589486512043e-116
Condition 22: r=0.64, t-value vs. 0: 58.56, p-value vs. 0: 6.884950749986713e-117
Condition 23: r=0.65, t-value vs. 0: 58.28, p-value vs. 0: 1.5024546854394354e-116
Condition 24: r=0.63, t-value vs. 0: 53.83, p-value vs. 0: 7.907905679243698e-111
In [65]:
#INDIVTASKRUN
#Individual task run [same task/subject/run], predicting run 1 activations for all task conditions
#Run actflow predictions, 24-condition task multiple-regression FC, SAME SUBJECT, FC FROM SAME RUN
print("==Activity flow mapping, multiple-regression-based task-state FC, 24 task conditions FROM RUN 1, SAME SUBJECT, FC FROM SAME RUN==")

actflowOutput_taskFCMReg_run1only_FCfromrun1_bycond = actflow.actflowcomp.actflowtest(actvect_splitbyrun_group_run1s, taskFC_run1_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping, multiple-regression-based task-state FC, 24 task conditions FROM RUN 1, SAME SUBJECT, FC FROM SAME RUN==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.72, t-value vs. 0: 112.38, p-value vs. 0: 4.505832001999541e-165
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.49
 
Mean MAE (mean absolute error) = 8.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.72, t-value vs. 0: 102.45, p-value vs. 0: 3.7988357281769215e-158
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.07
 
Mean MAE (mean absolute error) = 8.94
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.69, t-value vs. 0: 133.70, p-value vs. 0: 4.0123997558696656e-178
By task condition:
Condition 1: r=0.76, t-value vs. 0: 67.89, p-value vs. 0: 1.180471818145951e-127
Condition 2: r=0.77, t-value vs. 0: 67.88, p-value vs. 0: 1.208858511737846e-127
Condition 3: r=0.84, t-value vs. 0: 71.57, p-value vs. 0: 1.5742087834211462e-131
Condition 4: r=0.83, t-value vs. 0: 67.93, p-value vs. 0: 1.0597238479203442e-127
Condition 5: r=0.72, t-value vs. 0: 63.67, p-value vs. 0: 5.757208606271354e-123
Condition 6: r=0.71, t-value vs. 0: 58.97, p-value vs. 0: 2.122167751530994e-117
Condition 7: r=0.55, t-value vs. 0: 42.20, p-value vs. 0: 1.1893811872359725e-93
Condition 8: r=0.49, t-value vs. 0: 36.94, p-value vs. 0: 1.5188472096337996e-84
Condition 9: r=0.51, t-value vs. 0: 43.87, p-value vs. 0: 2.4029673303976156e-96
Condition 10: r=0.50, t-value vs. 0: 37.66, p-value vs. 0: 7.287388305973022e-86
Condition 11: r=0.47, t-value vs. 0: 41.51, p-value vs. 0: 1.6504475796024624e-92
Condition 12: r=0.51, t-value vs. 0: 42.24, p-value vs. 0: 1.0115066310690593e-93
Condition 13: r=0.84, t-value vs. 0: 66.09, p-value vs. 0: 1.099022574000902e-125
Condition 14: r=0.83, t-value vs. 0: 63.78, p-value vs. 0: 4.2886077569497126e-123
Condition 15: r=0.86, t-value vs. 0: 70.86, p-value vs. 0: 8.572081367344696e-131
Condition 16: r=0.87, t-value vs. 0: 79.40, p-value vs. 0: 3.488776822926217e-139
Condition 17: r=0.60, t-value vs. 0: 51.65, p-value vs. 0: 7.18624546888985e-108
Condition 18: r=0.54, t-value vs. 0: 41.13, p-value vs. 0: 7.115267754070758e-92
Condition 19: r=0.64, t-value vs. 0: 61.50, p-value vs. 0: 1.9306369385795633e-120
Condition 20: r=0.65, t-value vs. 0: 59.55, p-value vs. 0: 4.222665016806306e-118
Condition 21: r=0.65, t-value vs. 0: 57.97, p-value vs. 0: 3.636698817995383e-116
Condition 22: r=0.64, t-value vs. 0: 58.82, p-value vs. 0: 3.295475763939339e-117
Condition 23: r=0.65, t-value vs. 0: 58.85, p-value vs. 0: 3.032682359357081e-117
Condition 24: r=0.62, t-value vs. 0: 54.30, p-value vs. 0: 1.9124349759560644e-111

Quantify relative contributions to prediction accuracy (inspired by Gratton2018)

Going through interesting combinations of actflow comparisons among analysis types: GROUPREST, GROUPOTHERTASK, GROUPTASK, INDIVREST, INDIVOTHERTASK, INDIVTASK, INDIVTASKRUN

Order: GROUPREST-GROUPOTHERTASK, GROUPREST-GROUPTASK, GROUPREST-INDIVREST, (no:GROUPREST-INDIVTASK), (no:GROUPREST-INDIVTASKRUN), GROUPOTHERTASK-GROUPTASK, (no:GROUPTASK-INDIVREST), GROUPTASK-INDIVTASK, (no:GROUPTASK-INDIVTASKRUN), INDIVREST-INDIVTASK, (no:INDIVREST-INDIVTASKRUN), INDIVOTHERTASK-INDIVTASK, INDIVTASK-INDIVTASKRUN

In [66]:
#GROUPREST-GROUPOTHERTASK
print("The effect of group other task vs. rest FC:")
print("===Compare GROUPREST vs GROUPOTHERTASK actflow prediction===")
model_compare_GROUPRESTvsGROUPOTHERTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_restFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_othertask_othersubj_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of group other task vs. rest FC:
===Compare GROUPREST vs GROUPOTHERTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.43
Model2 mean Pearson r=0.44
R-value difference = -0.01
Model1 vs. Model2 T-value: -3.60, p-value: 0.00041419722254628836
 
Model1 mean % predicted variance explained R^2=0.06
Model2 mean % predicted variance explained R^2=0.12
R^2 difference = -0.05
 
Model1 mean MAE = 12.48
Model2 mean MAE = 12.32
Model1 vs. Model2 mean MAE difference = 0.16
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [67]:
#GROUPREST-GROUPTASK
print("The effect of group task vs. rest FC:")
print("===Compare GROUPREST vs GROUPTASK actflow prediction===")
model_compare_GROUPRESTvsGROUPTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_restFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of group task vs. rest FC:
===Compare GROUPREST vs GROUPTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.43
Model2 mean Pearson r=0.51
R-value difference = -0.08
Model1 vs. Model2 T-value: -19.28, p-value: 3.827919460022508e-45
 
Model1 mean % predicted variance explained R^2=0.06
Model2 mean % predicted variance explained R^2=0.15
R^2 difference = -0.09
 
Model1 mean MAE = 12.48
Model2 mean MAE = 11.72
Model1 vs. Model2 mean MAE difference = 0.76
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [68]:
#GROUPREST-INDIVREST
print("The effect of individualized vs. group rest (intrinsic) FC:")
print("===Compare GROUPREST vs INDIVREST actflow prediction===")
model_compare_GROUPRESTvsINDIVREST = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_restFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_FCfromRest_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of individualized vs. group rest (intrinsic) FC:
===Compare GROUPREST vs INDIVREST actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.43
Model2 mean Pearson r=0.48
R-value difference = -0.05
Model1 vs. Model2 T-value: -13.48, p-value: 7.113329980522266e-29
 
Model1 mean % predicted variance explained R^2=0.06
Model2 mean % predicted variance explained R^2=0.13
R^2 difference = -0.07
 
Model1 mean MAE = 12.48
Model2 mean MAE = 12.04
Model1 vs. Model2 mean MAE difference = 0.44
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [69]:
#GROUPOTHERTASK-GROUPTASK
print("The effect of individualized task-general vs. task-specific FC:")
print("===Compare GROUPOTHERTASK vs GROUPTASK actflow prediction===")
model_compare_GROUPOTHERTASKvsGROUPTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_taskFCMReg_run1only_othertask_othersubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of individualized task-general vs. task-specific FC:
===Compare GROUPOTHERTASK vs GROUPTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.44
Model2 mean Pearson r=0.51
R-value difference = -0.07
Model1 vs. Model2 T-value: -17.42, p-value: 4.420977752056184e-40
 
Model1 mean % predicted variance explained R^2=0.12
Model2 mean % predicted variance explained R^2=0.15
R^2 difference = -0.04
 
Model1 mean MAE = 12.32
Model2 mean MAE = 11.72
Model1 vs. Model2 mean MAE difference = 0.61
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [70]:
#GROUPTASK-INDIVTASK
print("The effect of individualized vs. group task FC:")
print("===Compare GROUPTASK vs INDIVTASK actflow prediction===")
model_compare_GROUPTASKvsINDIVTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_taskFCMReg_run1only_othersubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of individualized vs. group task FC:
===Compare GROUPTASK vs INDIVTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.51
Model2 mean Pearson r=0.67
R-value difference = -0.16
Model1 vs. Model2 T-value: -38.67, p-value: 1.1843572399450907e-87
 
Model1 mean % predicted variance explained R^2=0.15
Model2 mean % predicted variance explained R^2=0.40
R^2 difference = -0.25
 
Model1 mean MAE = 11.72
Model2 mean MAE = 9.75
Model1 vs. Model2 mean MAE difference = 1.96
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [71]:
#INDIVREST-INDIVTASK
print("The effect of individualized task vs. rest FC:")
print("===Compare INDIVREST vs INDIVTASK actflow prediction===")
model_compare_INDIVRESTvsINDIVTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_taskFCMReg_run1only_FCfromRest_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of individualized task vs. rest FC:
===Compare INDIVREST vs INDIVTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.48
Model2 mean Pearson r=0.67
R-value difference = -0.19
Model1 vs. Model2 T-value: -64.12, p-value: 1.7604414654731994e-123
 
Model1 mean % predicted variance explained R^2=0.13
Model2 mean % predicted variance explained R^2=0.40
R^2 difference = -0.27
 
Model1 mean MAE = 12.04
Model2 mean MAE = 9.75
Model1 vs. Model2 mean MAE difference = 2.28
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [72]:
#INDIVOTHERTASK-INDIVTASK
print("The effect of individualized task-general vs. task-specific FC:")
print("===Compare INDIVOTHERTASK vs INDIVTASK actflow prediction===")
model_compare_INDIVOTHERTASKvsINDIVTASK = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_taskFCMReg_run1only_othertask_samesubj_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of individualized task-general vs. task-specific FC:
===Compare INDIVOTHERTASK vs INDIVTASK actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.49
Model2 mean Pearson r=0.67
R-value difference = -0.18
Model1 vs. Model2 T-value: -68.44, p-value: 3.02058064542215e-128
 
Model1 mean % predicted variance explained R^2=0.18
Model2 mean % predicted variance explained R^2=0.40
R^2 difference = -0.22
 
Model1 mean MAE = 11.80
Model2 mean MAE = 9.75
Model1 vs. Model2 mean MAE difference = 2.05
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
In [73]:
#INDIVTASK-INDIVTASKRUN
print("The effect of same-run task vs. individualized task FC:")
print("===Compare INDIVTASK vs INDIVTASKRUN actflow prediction===")
model_compare_INDIVTASKvsINDIVTASKRUN = actflow.model_compare(target_actvect=actvect_splitbyrun_group_run1s, model1_actvect=actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskFCMReg_run1only_FCfromrun1_bycond['actPredVector_bytask_bysubj'], full_report=False, print_report=True, print_by_condition=False, mean_absolute_error=True)
The effect of same-run task vs. individualized task FC:
===Compare INDIVTASK vs INDIVTASKRUN actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.67
Model2 mean Pearson r=0.72
R-value difference = -0.05
Model1 vs. Model2 T-value: -30.96, p-value: 6.739360019338731e-73
 
Model1 mean % predicted variance explained R^2=0.40
Model2 mean % predicted variance explained R^2=0.49
R^2 difference = -0.09
 
Model1 mean MAE = 9.75
Model2 mean MAE = 8.94
Model1 vs. Model2 mean MAE difference = 0.82
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.

Visualize

In [74]:
#Vizualize relative prediction accuracies
import pandas as pd
#GROUPREST, GROUPTASK, INDIVREST, INDIVTASK, INDIVTASKRUN
#colnames=np.array(('GroupRest','GroupTask','IndivRest','IndivTask','IndivTaskRun'))
colnames=np.array(('Pearson r','Source of FC'))
corr_array_byactflowtype=np.concatenate((actflowOutput_restFCMReg_run1only_othersubj_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_othersubj_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromRest_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromrun1_bycond['model_compare_output']['corr_fullcomp_compthenavg']))
analysistype_array=np.concatenate((np.repeat('GroupRest',nSubjs), np.repeat('GroupTask',nSubjs), np.repeat('IndivRest',nSubjs),
                                   np.repeat('IndivTask',nSubjs), np.repeat('IndivTaskRun',nSubjs)))
analysistype_series=pd.Series(analysistype_array, dtype="category")
#analysistype_array=np.concatenate((np.repeat(1,nSubjs), np.repeat(2,nSubjs), np.repeat(3,nSubjs),
#                                   np.repeat(4,nSubjs), np.repeat(5,nSubjs)))
subjnums_array=np.concatenate((np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs)))
df_actflowpreds = pd.DataFrame(np.vstack((corr_array_byactflowtype,analysistype_series)).T, index=subjnums_array, columns=colnames)
df_actflowpreds['Source of FC']=df_actflowpreds['Source of FC'].astype('category')
df_actflowpreds['Pearson r']=df_actflowpreds['Pearson r'].astype('float')
fig = plt.figure(figsize=[8,4])
ax = sns.boxplot(y='Pearson r',x='Source of FC',data=df_actflowpreds,color='lightgray')
fig.savefig(figuredir+'groupIndivCompTests_PearsonR_MultregFC_discovery176.pdf')
In [75]:
#Vizualize relative prediction accuracies - R^2 values
import pandas as pd
#GROUPREST, GROUPTASK, INDIVREST, INDIVTASK, INDIVTASKRUN
#colnames=np.array(('GroupRest','GroupTask','IndivRest','IndivTask','IndivTaskRun'))
colnames=np.array(('R^2','Source of FC'))
r2_array_byactflowtype=np.concatenate((actflowOutput_restFCMReg_run1only_othersubj_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_othersubj_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromRest_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromrun2_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskFCMReg_run1only_FCfromrun1_bycond['model_compare_output']['R2_fullcomp_compthenavg']))
analysistype_array=np.concatenate((np.repeat('GroupRest',nSubjs), np.repeat('GroupTask',nSubjs), np.repeat('IndivRest',nSubjs),
                                   np.repeat('IndivTask',nSubjs), np.repeat('IndivTaskRun',nSubjs)))
analysistype_series=pd.Series(analysistype_array, dtype="category")
subjnums_array=np.concatenate((np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs)))
df_actflowpreds_R2 = pd.DataFrame(np.vstack((r2_array_byactflowtype,analysistype_series)).T, index=subjnums_array, columns=colnames)
df_actflowpreds_R2['Source of FC']=df_actflowpreds_R2['Source of FC'].astype('category')
df_actflowpreds_R2['R^2']=df_actflowpreds_R2['R^2'].astype('float')
fig = plt.figure(figsize=[8,4])
ax = sns.boxplot(y='R^2',x='Source of FC',data=df_actflowpreds_R2,color='lightgray')
fig.savefig(figuredir+'groupIndivCompTests_R2_MultregFC_discovery176.pdf')
In [76]:
sns.set(style="whitegrid")
fig = plt.figure(figsize=[7,3])
ax = sns.barplot(y='R^2',x='Source of FC',data=df_actflowpreds_R2,color='lightgray',ci=95)
ax.set(ylabel='Normalized pred. acc. (R^2)')
fig.savefig(figuredir+'groupIndivCompTests_R2_barplots_MultregFC_discovery176.pdf')
In [77]:
sns.set(style="whitegrid")
fig = plt.figure(figsize=[7,3])
meanvals=df_actflowpreds_R2.groupby('Source of FC').mean()
#subtract intrinsic FC
baselines=np.array((0,meanvals['R^2'][0],meanvals['R^2'][0],meanvals['R^2'][2],meanvals['R^2'][3]))
meanvals=meanvals.sub(baselines,axis='rows')
meanvals_normalized=meanvals/meanvals.sum()
ax=meanvals_normalized.T.plot(kind='bar', stacked=True)
ax.set(ylabel='Normalized prediction accuracy (R^2)')
fig = ax.get_figure()
fig.savefig(figuredir+'groupIndivCompTests_R2_stackedbarplots_MultregFC_discovery176.pdf')
<Figure size 504x216 with 0 Axes>

Check possibility that original analyses were overfit (explaining why R^2 is better with half the data)

In [78]:
## Calculating multreg REST FC, matched timing with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFCMatchedTiming_halfncomp_data'

restFCMatchedTiming_halfncomp_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFCMatchedTiming_halfncomp_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['restFCRegressionMatchTiming'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        file_python.write("    restFCMatchedTiming_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(restdata[:,tasktiming], n_components=int(np.round(np.sum(tasktiming)/2)))\n")
        file_python.write("    #Save multiple-regression FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegressionMatchTiming/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFCMatchedTiming_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:20:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [79]:
#Run actflow predictions, 24-condition rest multiple-regression FC, timing matched to task
print("==Activity flow mapping results, multiple-regression-based resting-state FC, TASK TIMING MATCHED, 24 task conditions, halfncomp==")
actflowOutput_restFCMRegMatchedTiming_halfncomp_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFCMatchedTiming_halfncomp_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, TASK TIMING MATCHED, 24 task conditions, halfncomp==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.63, t-value vs. 0: 112.47, p-value vs. 0: 3.9088519001439045e-165
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.38
 
Mean MAE (mean absolute error) = 9.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.60, t-value vs. 0: 92.79, p-value vs. 0: 9.325667887322754e-151
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.18
 
Mean MAE (mean absolute error) = 9.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.54, t-value vs. 0: 107.35, p-value vs. 0: 1.2183565231136572e-161
By task condition:
Condition 1: r=0.51, t-value vs. 0: 46.37, p-value vs. 0: 3.1242568360323154e-100
Condition 2: r=0.49, t-value vs. 0: 39.72, p-value vs. 0: 1.7872877429976405e-89
Condition 3: r=0.65, t-value vs. 0: 62.51, p-value vs. 0: 1.2702179475581958e-121
Condition 4: r=0.65, t-value vs. 0: 62.18, p-value vs. 0: 3.026504376095479e-121
Condition 5: r=0.60, t-value vs. 0: 58.80, p-value vs. 0: 3.462297326249351e-117
Condition 6: r=0.59, t-value vs. 0: 60.47, p-value vs. 0: 3.273872088134748e-119
Condition 7: r=0.58, t-value vs. 0: 51.64, p-value vs. 0: 7.367503858210927e-108
Condition 8: r=0.41, t-value vs. 0: 35.21, p-value vs. 0: 2.4765140030243256e-81
Condition 9: r=0.40, t-value vs. 0: 38.42, p-value vs. 0: 3.245890172145852e-87
Condition 10: r=0.41, t-value vs. 0: 36.36, p-value vs. 0: 1.7492890671032716e-83
Condition 11: r=0.39, t-value vs. 0: 37.59, p-value vs. 0: 9.806307577731253e-86
Condition 12: r=0.40, t-value vs. 0: 37.18, p-value vs. 0: 5.445258960381026e-85
Condition 13: r=0.65, t-value vs. 0: 72.38, p-value vs. 0: 2.3529545141114596e-132
Condition 14: r=0.67, t-value vs. 0: 67.02, p-value vs. 0: 1.0340061244182274e-126
Condition 15: r=0.63, t-value vs. 0: 62.73, p-value vs. 0: 6.968171563652464e-122
Condition 16: r=0.65, t-value vs. 0: 68.27, p-value vs. 0: 4.586663103979907e-128
Condition 17: r=0.51, t-value vs. 0: 48.44, p-value vs. 0: 2.584356369246419e-103
Condition 18: r=0.46, t-value vs. 0: 46.93, p-value vs. 0: 4.4245420198016435e-101
Condition 19: r=0.56, t-value vs. 0: 52.56, p-value vs. 0: 4.0597872657793143e-109
Condition 20: r=0.57, t-value vs. 0: 54.02, p-value vs. 0: 4.497233401730062e-111
Condition 21: r=0.56, t-value vs. 0: 57.58, p-value vs. 0: 1.1423674634657438e-115
Condition 22: r=0.47, t-value vs. 0: 44.30, p-value vs. 0: 4.98834906726193e-97
Condition 23: r=0.52, t-value vs. 0: 50.34, p-value vs. 0: 4.942658129080455e-106
Condition 24: r=0.53, t-value vs. 0: 51.84, p-value vs. 0: 3.932499267848386e-108
In [80]:
## Calculating multreg FC with SLURM cluster command (in parallel), loading from data if saved

#Note: issue with subject 559457; SVD didn't converge; copying from original analysis (conservative test of hypothesis)

loaddata_ifavailable=True
datasuffix='_PCmultregTaskFC_halfncomp_data'

taskFC_halfncomp_mreg_bycond_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
#snums_touse=np.concatenate((np.arange(0,161), np.arange(162,176)))
for subj in subjNums:
    #print('Subject ' + subj +'('+str(scount)+')')
    file_path=resultsdir + subj + datasuffix + '.h5'
    #print(file_path)
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskFC_halfncomp_mreg_bycond_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")  
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    tasktiming=tasktiming_bycond[:,condnum]\n")
        #file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming], n_components=0.9, svd_solver='full')\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(taskdata[:,tasktiming], n_components=int(np.round(np.sum(tasktiming)/2)))\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=main\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:15:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [81]:
#Run actflow predictions, 24-condition rest multiple-regression FC, timing matched to task
print("==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, halfncomp==")
actflowOutput_taskfc_halfncomp_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskFC_halfncomp_mreg_bycond_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions, halfncomp==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.78, t-value vs. 0: 154.89, p-value vs. 0: 3.2851477398194505e-189
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.60
 
Mean MAE (mean absolute error) = 7.24
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.76, t-value vs. 0: 141.08, p-value vs. 0: 3.6020594034328457e-182
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.19
 
Mean MAE (mean absolute error) = 7.24
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.76, t-value vs. 0: 145.76, p-value vs. 0: 1.2526265582849551e-184
By task condition:
Condition 1: r=0.73, t-value vs. 0: 67.83, p-value vs. 0: 1.3748149454038396e-127
Condition 2: r=0.73, t-value vs. 0: 62.81, p-value vs. 0: 5.613185131281539e-122
Condition 3: r=0.88, t-value vs. 0: 81.58, p-value vs. 0: 3.4541523591757695e-141
Condition 4: r=0.89, t-value vs. 0: 84.14, p-value vs. 0: 1.7798209993458263e-143
Condition 5: r=0.78, t-value vs. 0: 75.04, p-value vs. 0: 5.1122163151540567e-135
Condition 6: r=0.78, t-value vs. 0: 69.44, p-value vs. 0: 2.6131917831059967e-129
Condition 7: r=0.66, t-value vs. 0: 52.65, p-value vs. 0: 3.062437924851138e-109
Condition 8: r=0.59, t-value vs. 0: 43.97, p-value vs. 0: 1.6342940099482513e-96
Condition 9: r=0.57, t-value vs. 0: 47.26, p-value vs. 0: 1.4301020711458815e-101
Condition 10: r=0.57, t-value vs. 0: 47.75, p-value vs. 0: 2.740072501528112e-102
Condition 11: r=0.55, t-value vs. 0: 43.72, p-value vs. 0: 4.180092026792929e-96
Condition 12: r=0.57, t-value vs. 0: 44.43, p-value vs. 0: 3.1073408767531446e-97
Condition 13: r=0.91, t-value vs. 0: 86.12, p-value vs. 0: 3.323326338589591e-145
Condition 14: r=0.89, t-value vs. 0: 81.77, p-value vs. 0: 2.307474700720852e-141
Condition 15: r=0.92, t-value vs. 0: 97.71, p-value vs. 0: 1.320068398375311e-154
Condition 16: r=0.92, t-value vs. 0: 94.92, p-value vs. 0: 1.907030543981619e-152
Condition 17: r=0.67, t-value vs. 0: 60.04, p-value vs. 0: 1.0518446066020229e-118
Condition 18: r=0.62, t-value vs. 0: 53.81, p-value vs. 0: 8.457011021252692e-111
Condition 19: r=0.71, t-value vs. 0: 63.40, p-value vs. 0: 1.1735761872357459e-122
Condition 20: r=0.74, t-value vs. 0: 73.43, p-value vs. 0: 2.0373344544767516e-133
Condition 21: r=0.75, t-value vs. 0: 71.97, p-value vs. 0: 6.108454235613697e-132
Condition 22: r=0.79, t-value vs. 0: 70.61, p-value vs. 0: 1.538675860634891e-130
Condition 23: r=0.70, t-value vs. 0: 65.87, p-value vs. 0: 1.918329161501076e-125
Condition 24: r=0.72, t-value vs. 0: 54.37, p-value vs. 0: 1.5225100334516538e-111

Multreg-based task-state FC - Task-general FC tests

In [82]:
## Calculating multreg task-general FC with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregTaskGenFC_data'

taskgenFC_mreg_bysubj=np.zeros((nParcels,nParcels,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        taskgenFC_mreg_bysubj[:,:,scount] = h5f['taskgenFCRegression'][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
 
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("tasktiming_all=np.array(np.sum(tasktiming_bycond,axis=1)>0)\n")
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("taskgenFC_mreg_bysubj=actflow.connectivity_estimation.multregconn(taskdata[:,tasktiming_all])\n")
        file_python.write("#Save multiple-regression task FC data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'taskgenFCRegression'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=taskgenFC_mreg_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=taskgenFC_mreg_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:05:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [83]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskgenFC_mreg_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task-general FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[83]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [84]:
#Run actflow predictions, 24-condition task-general multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general FC, 24 task conditions==")
actflowOutput_taskGeneralFCMReg_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-general FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.91, t-value vs. 0: 163.67, p-value vs. 0: 2.2745658702165087e-193
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.83
 
Mean MAE (mean absolute error) = 4.63
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.91, t-value vs. 0: 175.22, p-value vs. 0: 1.5927908326883923e-198
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.65
 
Mean MAE (mean absolute error) = 4.63
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.90, t-value vs. 0: 153.85, p-value vs. 0: 1.0651137917175806e-188
By task condition:
Condition 1: r=0.81, t-value vs. 0: 74.05, p-value vs. 0: 4.929943455521806e-134
Condition 2: r=0.78, t-value vs. 0: 68.38, p-value vs. 0: 3.472445848727976e-128
Condition 3: r=0.94, t-value vs. 0: 104.71, p-value vs. 0: 8.879199188781287e-160
Condition 4: r=0.94, t-value vs. 0: 102.40, p-value vs. 0: 4.154469025076904e-158
Condition 5: r=0.86, t-value vs. 0: 89.95, p-value vs. 0: 1.9347576652181606e-148
Condition 6: r=0.86, t-value vs. 0: 90.80, p-value vs. 0: 3.8692699611545716e-149
Condition 7: r=0.89, t-value vs. 0: 84.24, p-value vs. 0: 1.4460877090289357e-143
Condition 8: r=0.80, t-value vs. 0: 66.85, p-value vs. 0: 1.5728553282927267e-126
Condition 9: r=0.81, t-value vs. 0: 74.46, p-value vs. 0: 1.9234600386294935e-134
Condition 10: r=0.79, t-value vs. 0: 75.97, p-value vs. 0: 6.350978027040606e-136
Condition 11: r=0.80, t-value vs. 0: 77.04, p-value vs. 0: 5.885613972296142e-137
Condition 12: r=0.81, t-value vs. 0: 78.56, p-value vs. 0: 2.139244674417898e-138
Condition 13: r=0.95, t-value vs. 0: 97.61, p-value vs. 0: 1.5826595407093754e-154
Condition 14: r=0.94, t-value vs. 0: 96.14, p-value vs. 0: 2.1240788515188463e-153
Condition 15: r=0.96, t-value vs. 0: 121.11, p-value vs. 0: 1.1030418146194814e-170
Condition 16: r=0.95, t-value vs. 0: 116.44, p-value vs. 0: 9.911506074015138e-168
Condition 17: r=0.91, t-value vs. 0: 94.93, p-value vs. 0: 1.874888697377336e-152
Condition 18: r=0.90, t-value vs. 0: 90.21, p-value vs. 0: 1.1776013708916067e-148
Condition 19: r=0.93, t-value vs. 0: 107.07, p-value vs. 0: 1.898138520300951e-161
Condition 20: r=0.92, t-value vs. 0: 101.56, p-value vs. 0: 1.7163157846871812e-157
Condition 21: r=0.94, t-value vs. 0: 116.79, p-value vs. 0: 5.880391361974674e-168
Condition 22: r=0.92, t-value vs. 0: 98.96, p-value vs. 0: 1.47501079486501e-155
Condition 23: r=0.92, t-value vs. 0: 104.00, p-value vs. 0: 2.8566871579152146e-159
Condition 24: r=0.94, t-value vs. 0: 108.33, p-value vs. 0: 2.5458688941522585e-162
In [85]:
print("===Compare task-general multregFC actflow predictions to task-state multregFC actflow prediction===")
model_compare_TaskGenMultRegFCVsTaskSTateMultRegFC_Actflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskGeneralFCMReg_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare task-general multregFC actflow predictions to task-state multregFC actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.76
Model2 mean Pearson r=0.91
R-value difference = -0.15
Model1 vs. Model2 T-value: -94.55, p-value: 3.737948813842191e-152
 
Model1 mean % predicted variance explained R^2=0.51
Model2 mean % predicted variance explained R^2=0.83
R^2 difference = -0.32
 
Model1 mean MAE = 7.47
Model2 mean MAE = 4.63
Model1 vs. Model2 mean MAE difference = 2.84
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.75
Model2 mean Pearson r=0.91
R-value difference = -0.16
Model1 vs. Model2 T-value: -99.08, p-value: 1.2096515844542646e-155
 
Model1 mean % predicted variance explained R^2=0.01
Model2 mean % predicted variance explained R^2=0.65
R^2 difference = -0.64
 
Model1 mean MAE = 7.47
Model2 mean MAE = 4.63
Model1 vs. Model2 mean MAE difference = 2.84
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.77
Model2 mean Pearson r=0.90
R-value difference = -0.13
Model1 vs. Model2 T-value: -87.34, p-value: 2.9977309000154e-146
By task condition:
Condition 1: Model 1 r=0.77, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -9.87, p-value vs. 0: 1.5169369615015681e-18
Condition 2: Model 1 r=0.77, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.01, t-value Model1 vs. Model2: -2.42, p-value vs. 0: 0.016338964539881297
Condition 3: Model 1 r=0.88, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.06, t-value Model1 vs. Model2: -31.47, p-value vs. 0: 5.86635407584016e-74
Condition 4: Model 1 r=0.89, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -27.76, p-value vs. 0: 5.166760496773608e-66
Condition 5: Model 1 r=0.68, Model 2 r=0.86, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -39.88, p-value vs. 0: 9.403430467158001e-90
Condition 6: Model 1 r=0.65, Model 2 r=0.86, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -45.60, p-value vs. 0: 4.781164110447876e-99
Condition 7: Model 1 r=0.69, Model 2 r=0.89, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -54.42, p-value vs. 0: 1.314276227634942e-111
Condition 8: Model 1 r=0.60, Model 2 r=0.80, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -37.75, p-value vs. 0: 5.081310131472084e-86
Condition 9: Model 1 r=0.57, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.23, t-value Model1 vs. Model2: -38.32, p-value vs. 0: 4.878288024141472e-87
Condition 10: Model 1 r=0.58, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -36.54, p-value vs. 0: 8.210129334344092e-84
Condition 11: Model 1 r=0.56, Model 2 r=0.80, Model 1 vs. 2 R-value difference =-0.24, t-value Model1 vs. Model2: -40.62, p-value vs. 0: 5.099711632987497e-91
Condition 12: Model 1 r=0.59, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -41.89, p-value vs. 0: 3.811527879892841e-93
Condition 13: Model 1 r=0.91, Model 2 r=0.95, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -23.75, p-value vs. 0: 1.2580568271817828e-56
Condition 14: Model 1 r=0.91, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -18.55, p-value vs. 0: 3.5651444595671544e-43
Condition 15: Model 1 r=0.91, Model 2 r=0.96, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -29.68, p-value vs. 0: 3.276322545224274e-70
Condition 16: Model 1 r=0.90, Model 2 r=0.95, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -33.11, p-value vs. 0: 2.8908258001963814e-77
Condition 17: Model 1 r=0.69, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -48.20, p-value vs. 0: 5.8994583323382e-103
Condition 18: Model 1 r=0.73, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.17, t-value Model1 vs. Model2: -36.13, p-value vs. 0: 4.6986544175634954e-83
Condition 19: Model 1 r=0.76, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.17, t-value Model1 vs. Model2: -46.76, p-value vs. 0: 8.104891631936526e-101
Condition 20: Model 1 r=0.80, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.13, t-value Model1 vs. Model2: -36.14, p-value vs. 0: 4.385224816650458e-83
Condition 21: Model 1 r=0.79, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.15, t-value Model1 vs. Model2: -49.09, p-value vs. 0: 2.93888953837462e-104
Condition 22: Model 1 r=0.79, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.13, t-value Model1 vs. Model2: -37.78, p-value vs. 0: 4.550198812692969e-86
Condition 23: Model 1 r=0.73, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -51.63, p-value vs. 0: 7.58376382432563e-108
Condition 24: Model 1 r=0.75, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -47.02, p-value vs. 0: 3.3284459697239e-101
In [86]:
#Compare task-general and task-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset1=np.arctanh(actflowOutput_taskGeneralFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
taskdataset2=np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset1,taskdataset2)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata))
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)

tvals=np.array([tstats[nodenum][0] for nodenum in np.arange(nParcels)])
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nParcels
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nParcels
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

#Visualize data
# datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
# #Apply threshold
# datamat=np.multiply(datamat,maxt_sigvector)
# datamat=datamat[:,np.newaxis]
# outfilename='../figures/figure_parts/taskgeneralVStaskstateActflowPred_condwise_MultregFC_discovery176'
# #sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
# actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=True)

# #Max and min values
# print('Maximum of map: ', np.max(datamat[datamat!=0]))
# print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 98.33333333333333
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 98.33333333333333
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 0.0

Resting-state FC time-matched to task-general FC

In [87]:
## Calculating multreg REST FC, matched timing with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_PCmultregRestFC_matchedTaskGen_data'

restFC_mreg_matchedTaskGen_bysubj=np.zeros((nParcels,nParcels,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        restFC_mreg_matchedTaskGen_bysubj[:,:,scount] = h5f['restFCRegressionMatchTiming'][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("tasktiming_dur=3810\n")
        file_python.write("restFC_mreg_matchedTaskGen_bycond_bysubj=actflow.connectivity_estimation.pc_multregconn(restdata[:,0:tasktiming_dur])\n")
        file_python.write("#Save multiple-regression FC data to file\n")
        file_python.write("h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("outname1 = 'restFCRegressionMatchTiming'\n")
        file_python.write("try:\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_mreg_matchedTaskGen_bycond_bysubj)\n")
        file_python.write("except:\n")
        file_python.write("    del h5f[outname1]\n")
        file_python.write("    h5f.create_dataset(outname1,data=restFC_mreg_matchedTaskGen_bycond_bysubj)\n")
        file_python.write("h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:05:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [88]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_mreg_matchedTaskGen_bysubj[netorder,:,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest (task-general timing matched) FC')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[88]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [89]:
#Run actflow predictions, 24-condition multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state (task-general timing matched) FC, 24 task conditions==")
actflowOutput_restFCMReg_taskGeneralMatched_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_matchedTaskGen_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state (task-general timing matched) FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.88, t-value vs. 0: 162.89, p-value vs. 0: 5.194311585996199e-193
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.77
 
Mean MAE (mean absolute error) = 5.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.88, t-value vs. 0: 167.90, p-value vs. 0: 2.6678024910057022e-195
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.54
 
Mean MAE (mean absolute error) = 5.53
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.86, t-value vs. 0: 146.57, p-value vs. 0: 4.782781303540501e-185
By task condition:
Condition 1: r=0.77, t-value vs. 0: 70.46, p-value vs. 0: 2.1994981924209237e-130
Condition 2: r=0.74, t-value vs. 0: 63.15, p-value vs. 0: 2.2631637428306908e-122
Condition 3: r=0.89, t-value vs. 0: 108.95, p-value vs. 0: 9.525062434766253e-163
Condition 4: r=0.89, t-value vs. 0: 105.29, p-value vs. 0: 3.443231911194449e-160
Condition 5: r=0.81, t-value vs. 0: 83.91, p-value vs. 0: 2.8083795306105586e-143
Condition 6: r=0.80, t-value vs. 0: 89.88, p-value vs. 0: 2.2166172343505072e-148
Condition 7: r=0.87, t-value vs. 0: 87.97, p-value vs. 0: 8.750697498464992e-147
Condition 8: r=0.77, t-value vs. 0: 69.05, p-value vs. 0: 6.689632785856271e-129
Condition 9: r=0.77, t-value vs. 0: 71.19, p-value vs. 0: 3.857580894105445e-131
Condition 10: r=0.77, t-value vs. 0: 74.46, p-value vs. 0: 1.9144374140207965e-134
Condition 11: r=0.77, t-value vs. 0: 71.41, p-value vs. 0: 2.321533213768232e-131
Condition 12: r=0.77, t-value vs. 0: 74.43, p-value vs. 0: 2.0417289782719945e-134
Condition 13: r=0.90, t-value vs. 0: 105.55, p-value vs. 0: 2.2376197941555176e-160
Condition 14: r=0.90, t-value vs. 0: 97.62, p-value vs. 0: 1.5507522618820576e-154
Condition 15: r=0.91, t-value vs. 0: 123.96, p-value vs. 0: 1.9685241604568302e-172
Condition 16: r=0.90, t-value vs. 0: 119.22, p-value vs. 0: 1.6755052334965385e-169
Condition 17: r=0.88, t-value vs. 0: 97.88, p-value vs. 0: 9.772869478949104e-155
Condition 18: r=0.86, t-value vs. 0: 91.13, p-value vs. 0: 2.0618554958413717e-149
Condition 19: r=0.90, t-value vs. 0: 114.10, p-value vs. 0: 3.2784992847935226e-166
Condition 20: r=0.89, t-value vs. 0: 103.20, p-value vs. 0: 1.0823980270741791e-158
Condition 21: r=0.90, t-value vs. 0: 116.79, p-value vs. 0: 5.837004529511612e-168
Condition 22: r=0.87, t-value vs. 0: 96.13, p-value vs. 0: 2.171880631894413e-153
Condition 23: r=0.89, t-value vs. 0: 105.16, p-value vs. 0: 4.248051856068149e-160
Condition 24: r=0.90, t-value vs. 0: 114.22, p-value vs. 0: 2.752170081929545e-166
In [90]:
print("===Compare task-general multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===")
model_compare_TaskGenMultRegFCVsRestMultRegFCMatchTiming_Actflow = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_taskGeneralMatched_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_taskGeneralMatched_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskGeneralFCMReg_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare task-general multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.88
Model2 mean Pearson r=0.91
R-value difference = -0.03
Model1 vs. Model2 T-value: -44.57, p-value: 1.9031951409810695e-97
 
Model1 mean % predicted variance explained R^2=0.77
Model2 mean % predicted variance explained R^2=0.83
R^2 difference = -0.06
 
Model1 mean MAE = 5.53
Model2 mean MAE = 4.63
Model1 vs. Model2 mean MAE difference = 0.90
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.88
Model2 mean Pearson r=0.91
R-value difference = -0.03
Model1 vs. Model2 T-value: -45.16, p-value: 2.2756522748841582e-98
 
Model1 mean % predicted variance explained R^2=0.54
Model2 mean % predicted variance explained R^2=0.65
R^2 difference = -0.11
 
Model1 mean MAE = 5.53
Model2 mean MAE = 4.63
Model1 vs. Model2 mean MAE difference = 0.90
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.86
Model2 mean Pearson r=0.90
R-value difference = -0.04
Model1 vs. Model2 T-value: -49.52, p-value: 7.193274004015919e-105
By task condition:
Condition 1: Model 1 r=0.77, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -16.87, p-value vs. 0: 1.5945544308940198e-38
Condition 2: Model 1 r=0.74, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -14.59, p-value vs. 0: 4.4805214064287716e-32
Condition 3: Model 1 r=0.89, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -35.51, p-value vs. 0: 6.664189304926239e-82
Condition 4: Model 1 r=0.89, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -35.77, p-value vs. 0: 2.2015356299757546e-82
Condition 5: Model 1 r=0.81, Model 2 r=0.86, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -26.96, p-value vs. 0: 3.225122347128141e-64
Condition 6: Model 1 r=0.80, Model 2 r=0.86, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -25.22, p-value vs. 0: 3.502011040201564e-60
Condition 7: Model 1 r=0.87, Model 2 r=0.89, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -13.80, p-value vs. 0: 8.799574500493835e-30
Condition 8: Model 1 r=0.77, Model 2 r=0.80, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.67, p-value vs. 0: 8.664717093890181e-21
Condition 9: Model 1 r=0.77, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -12.40, p-value vs. 0: 9.745988700391882e-26
Condition 10: Model 1 r=0.77, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -7.15, p-value vs. 0: 2.2153283153601896e-11
Condition 11: Model 1 r=0.77, Model 2 r=0.80, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -11.51, p-value vs. 0: 3.387595677015934e-23
Condition 12: Model 1 r=0.77, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.91, p-value vs. 0: 1.7779479728734388e-21
Condition 13: Model 1 r=0.90, Model 2 r=0.95, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -36.22, p-value vs. 0: 3.1343322069612276e-83
Condition 14: Model 1 r=0.90, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -32.04, p-value vs. 0: 4.0555924155855094e-75
Condition 15: Model 1 r=0.91, Model 2 r=0.96, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -37.23, p-value vs. 0: 4.50921404234243e-85
Condition 16: Model 1 r=0.90, Model 2 r=0.95, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -38.10, p-value vs. 0: 1.1980127173544348e-86
Condition 17: Model 1 r=0.88, Model 2 r=0.91, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -17.92, p-value vs. 0: 1.9330566698375258e-41
Condition 18: Model 1 r=0.86, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -20.57, p-value vs. 0: 1.432190486145702e-48
Condition 19: Model 1 r=0.90, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -17.16, p-value vs. 0: 2.3904725987548697e-39
Condition 20: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -18.87, p-value vs. 0: 4.909149294881566e-44
Condition 21: Model 1 r=0.90, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -30.73, p-value vs. 0: 1.9416615041384866e-72
Condition 22: Model 1 r=0.87, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -27.34, p-value vs. 0: 4.494997347957396e-65
Condition 23: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -20.67, p-value vs. 0: 7.86977278750468e-49
Condition 24: Model 1 r=0.90, Model 2 r=0.94, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -27.61, p-value vs. 0: 1.115471406665221e-65
In [91]:
import gc
gc.collect()
Out[91]:
13634
In [92]:
#Compare task-general and resting-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset1=np.arctanh(actflowOutput_taskGeneralFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
taskdataset2=np.arctanh(actflowOutput_restFCMReg_taskGeneralMatched_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset1,taskdataset2)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata),nproc=numWorkers)
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)

tvals=np.array([tstats[nodenum][0] for nodenum in np.arange(nParcels)])
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nParcels
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nParcels
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

#Visualize data
# datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
# #Apply threshold
# datamat=np.multiply(datamat,maxt_sigvector)
# datamat=datamat[:,np.newaxis]
# outfilename='../figures/figure_parts/taskgeneralVStaskstateActflowPred_condwise_MultregFC_discovery176'
# #sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
# actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=True)

# #Max and min values
# print('Maximum of map: ', np.max(datamat[datamat!=0]))
# print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 77.77777777777777
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 77.77777777777777
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 0.0

Multreg task-general FC, current task held out

In [93]:
## Calculating multreg task-general FC, CURRENT TASK HELD OUT, with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregTaskGenFC_notcurrenttask_data'

taskgenFC_mreg_bycondheldout_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            taskgenFC_mreg_bycondheldout_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
 
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    conds_other=np.delete(np.arange(24),condnum)\n")
        file_python.write("    tasktiming=np.array(np.sum(tasktiming_bycond[:,conds_other],axis=1)>0)\n")
        file_python.write("    taskFC_mreg_bycond_bysubj=actflow.connectivity_estimation.multregconn(taskdata[:,tasktiming])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bycond_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:10:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [94]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskgenFC_mreg_bycondheldout_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg task FC, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[94]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [95]:
#Run actflow predictions, 24-condition task-general cond-held-out multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general cond-held-out FC, 24 task conditions==")
actflowOutput_taskGeneralFCMReg_condheldout_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bycondheldout_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based task-general cond-held-out FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.90, t-value vs. 0: 154.21, p-value vs. 0: 7.101024141122594e-189
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.80
 
Mean MAE (mean absolute error) = 5.04
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.89, t-value vs. 0: 165.27, p-value vs. 0: 4.182504099466639e-194
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.58
 
Mean MAE (mean absolute error) = 5.04
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.88, t-value vs. 0: 145.73, p-value vs. 0: 1.3052044361409723e-184
By task condition:
Condition 1: r=0.77, t-value vs. 0: 67.66, p-value vs. 0: 2.1005987391680727e-127
Condition 2: r=0.73, t-value vs. 0: 59.96, p-value vs. 0: 1.331566011363248e-118
Condition 3: r=0.92, t-value vs. 0: 99.49, p-value vs. 0: 5.867144348003435e-156
Condition 4: r=0.92, t-value vs. 0: 99.74, p-value vs. 0: 3.827296602239367e-156
Condition 5: r=0.81, t-value vs. 0: 82.27, p-value vs. 0: 8.277742215902606e-142
Condition 6: r=0.81, t-value vs. 0: 81.96, p-value vs. 0: 1.572041837980223e-141
Condition 7: r=0.88, t-value vs. 0: 83.86, p-value vs. 0: 3.1419698696270314e-143
Condition 8: r=0.78, t-value vs. 0: 65.12, p-value vs. 0: 1.3278028800738593e-124
Condition 9: r=0.79, t-value vs. 0: 71.40, p-value vs. 0: 2.3868797516757667e-131
Condition 10: r=0.77, t-value vs. 0: 72.49, p-value vs. 0: 1.8121912289690075e-132
Condition 11: r=0.78, t-value vs. 0: 74.71, p-value vs. 0: 1.0876840622739177e-134
Condition 12: r=0.78, t-value vs. 0: 75.49, p-value vs. 0: 1.867352440405625e-135
Condition 13: r=0.93, t-value vs. 0: 93.71, p-value vs. 0: 1.734855365986398e-151
Condition 14: r=0.92, t-value vs. 0: 92.94, p-value vs. 0: 7.111773579324265e-151
Condition 15: r=0.93, t-value vs. 0: 113.51, p-value vs. 0: 7.972620341283483e-166
Condition 16: r=0.92, t-value vs. 0: 104.98, p-value vs. 0: 5.737599566319063e-160
Condition 17: r=0.90, t-value vs. 0: 90.71, p-value vs. 0: 4.5527132387192315e-149
Condition 18: r=0.90, t-value vs. 0: 89.69, p-value vs. 0: 3.19022485864887e-148
Condition 19: r=0.92, t-value vs. 0: 105.47, p-value vs. 0: 2.5770026394834006e-160
Condition 20: r=0.92, t-value vs. 0: 99.78, p-value vs. 0: 3.5768254386348775e-156
Condition 21: r=0.93, t-value vs. 0: 113.37, p-value vs. 0: 9.940153155524062e-166
Condition 22: r=0.92, t-value vs. 0: 98.03, p-value vs. 0: 7.57246505420158e-155
Condition 23: r=0.92, t-value vs. 0: 102.61, p-value vs. 0: 2.929230748287578e-158
Condition 24: r=0.93, t-value vs. 0: 106.52, p-value vs. 0: 4.644467983244361e-161
In [96]:
print("===Compare task-general condheldout multregFC actflow predictions to task-state multregFC actflow prediction===")
model_compare_TaskGenMultRegFCVsTaskSTateMultRegFC_condheldout_Actflow = actflow.model_compare(target_actvect=actflowOutput_taskFCMReg_bycond['actVect_actual_group'], model1_actvect=actflowOutput_taskFCMReg_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskGeneralFCMReg_condheldout_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare task-general condheldout multregFC actflow predictions to task-state multregFC actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.76
Model2 mean Pearson r=0.90
R-value difference = -0.13
Model1 vs. Model2 T-value: -75.62, p-value: 1.3844578782838288e-135
 
Model1 mean % predicted variance explained R^2=0.51
Model2 mean % predicted variance explained R^2=0.80
R^2 difference = -0.28
 
Model1 mean MAE = 7.47
Model2 mean MAE = 5.04
Model1 vs. Model2 mean MAE difference = 2.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.75
Model2 mean Pearson r=0.89
R-value difference = -0.14
Model1 vs. Model2 T-value: -77.50, p-value: 2.1366202554854011e-137
 
Model1 mean % predicted variance explained R^2=0.01
Model2 mean % predicted variance explained R^2=0.58
R^2 difference = -0.57
 
Model1 mean MAE = 7.47
Model2 mean MAE = 5.04
Model1 vs. Model2 mean MAE difference = 2.43
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.77
Model2 mean Pearson r=0.88
R-value difference = -0.11
Model1 vs. Model2 T-value: -67.12, p-value: 8.024028202104053e-127
By task condition:
Condition 1: Model 1 r=0.77, Model 2 r=0.77, Model 1 vs. 2 R-value difference =0.00, t-value Model1 vs. Model2: 0.12, p-value vs. 0: 0.9078184368076587
Condition 2: Model 1 r=0.77, Model 2 r=0.73, Model 1 vs. 2 R-value difference =0.05, t-value Model1 vs. Model2: 9.74, p-value vs. 0: 3.58331925748914e-18
Condition 3: Model 1 r=0.88, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -16.72, p-value vs. 0: 4.023387423224061e-38
Condition 4: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -11.77, p-value vs. 0: 6.435667401262113e-24
Condition 5: Model 1 r=0.68, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.13, t-value Model1 vs. Model2: -25.59, p-value vs. 0: 4.7569933649362256e-61
Condition 6: Model 1 r=0.65, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.15, t-value Model1 vs. Model2: -27.83, p-value vs. 0: 3.5708068545464534e-66
Condition 7: Model 1 r=0.69, Model 2 r=0.88, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -50.24, p-value vs. 0: 6.727492278163245e-106
Condition 8: Model 1 r=0.60, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -31.10, p-value vs. 0: 3.424848074983364e-73
Condition 9: Model 1 r=0.57, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -32.08, p-value vs. 0: 3.369685561949762e-75
Condition 10: Model 1 r=0.58, Model 2 r=0.77, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -28.18, p-value vs. 0: 6.03959889056151e-67
Condition 11: Model 1 r=0.56, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.22, t-value Model1 vs. Model2: -33.39, p-value vs. 0: 8.180882195896301e-78
Condition 12: Model 1 r=0.59, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.20, t-value Model1 vs. Model2: -33.70, p-value vs. 0: 1.961774564974432e-78
Condition 13: Model 1 r=0.91, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -7.82, p-value vs. 0: 4.677159084068915e-13
Condition 14: Model 1 r=0.91, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.01, t-value Model1 vs. Model2: -5.44, p-value vs. 0: 1.7768687987201802e-07
Condition 15: Model 1 r=0.91, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -7.21, p-value vs. 0: 1.6249318674068938e-11
Condition 16: Model 1 r=0.90, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.01, t-value Model1 vs. Model2: -5.78, p-value vs. 0: 3.331986309212035e-08
Condition 17: Model 1 r=0.69, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.21, t-value Model1 vs. Model2: -43.21, p-value vs. 0: 2.7259410394034415e-95
Condition 18: Model 1 r=0.73, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.16, t-value Model1 vs. Model2: -34.54, p-value vs. 0: 4.731342972429474e-80
Condition 19: Model 1 r=0.76, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.17, t-value Model1 vs. Model2: -43.27, p-value vs. 0: 2.1670638017402574e-95
Condition 20: Model 1 r=0.80, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.12, t-value Model1 vs. Model2: -33.44, p-value vs. 0: 6.317369316092297e-78
Condition 21: Model 1 r=0.79, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.14, t-value Model1 vs. Model2: -41.32, p-value vs. 0: 3.4375129009883904e-92
Condition 22: Model 1 r=0.79, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.12, t-value Model1 vs. Model2: -35.35, p-value vs. 0: 1.3053531796059666e-81
Condition 23: Model 1 r=0.73, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.19, t-value Model1 vs. Model2: -47.90, p-value vs. 0: 1.6380603982087923e-102
Condition 24: Model 1 r=0.75, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.18, t-value Model1 vs. Model2: -41.33, p-value vs. 0: 3.268215771734392e-92
In [97]:
#Compare task-general and task-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset1=np.arctanh(actflowOutput_taskGeneralFCMReg_condheldout_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
taskdataset2=np.arctanh(actflowOutput_taskFCMReg_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset1,taskdataset2)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata),nproc=numWorkers)
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)

tvals=np.array([tstats[nodenum][0] for nodenum in np.arange(nParcels)])
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nParcels
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nParcels
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

#Visualize data
# datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
# #Apply threshold
# datamat=np.multiply(datamat,maxt_sigvector)
# datamat=datamat[:,np.newaxis]
# outfilename='../figures/figure_parts/taskgeneralVStaskstateActflowPred_condwise_MultregFC_discovery176'
# #sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
# actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=True)

# #Max and min values
# print('Maximum of map: ', np.max(datamat[datamat!=0]))
# print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 96.11111111111111
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 96.11111111111111
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 0.0

Resting-state FC time-matched to cond-held-out task-general FC

In [98]:
## Calculating multreg resting-state FC time-matched to task-general FC, CURRENT TASK HELD OUT, with SLURM cluster command (in parallel), loading from data if saved

loaddata_ifavailable=True
datasuffix='_multregRestFC_timeMatchedTaskGenFC_notcurrenttask_data'

restFC_mreg_timematchedtaskgen_bycondheldout_bysubj=np.zeros((nParcels,nParcels,nConds,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for condnum in range(24):
            dataid = 'condition' + str(condnum)
            restFC_mreg_timematchedtaskgen_bycondheldout_bysubj[:,:,condnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
    
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        #file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("for condnum in range(24):\n")
        file_python.write("    conds_other=np.delete(np.arange(24),condnum)\n")
        file_python.write("    tasktiming=np.array(np.sum(tasktiming_bycond[:,conds_other],axis=1)>0)\n")
        file_python.write("    restFC_mreg_timematchedtaskgen_bycondheldout_bysubj=actflow.connectivity_estimation.multregconn(restdata[:,tasktiming])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(condnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_timematchedtaskgen_bycondheldout_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_timematchedtaskgen_bycondheldout_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:10:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [99]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_mreg_timematchedtaskgen_bycondheldout_bysubj[netorder,:,16,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC time matched, cond 17')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[99]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [100]:
#Run actflow predictions, 24-condition resting-state FC, time matched to task-general cond-held-out multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC, time matched to task-general cond-held-out FC, 24 task conditions==")
actflowOutput_restFCMReg_timematchedtaskGeneral_condheldout_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_timematchedtaskgen_bycondheldout_bysubj, full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, time matched to task-general cond-held-out FC, 24 task conditions==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.87, t-value vs. 0: 157.99, p-value vs. 0: 1.0488473264000528e-190
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.75
 
Mean MAE (mean absolute error) = 5.81
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.87, t-value vs. 0: 163.07, p-value vs. 0: 4.2905826194481617e-193
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.48
 
Mean MAE (mean absolute error) = 5.81
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.84, t-value vs. 0: 142.02, p-value vs. 0: 1.1427650145836462e-182
By task condition:
Condition 1: r=0.75, t-value vs. 0: 69.06, p-value vs. 0: 6.654956510853234e-129
Condition 2: r=0.73, t-value vs. 0: 61.28, p-value vs. 0: 3.459817971646546e-120
Condition 3: r=0.88, t-value vs. 0: 103.63, p-value vs. 0: 5.297646298479514e-159
Condition 4: r=0.88, t-value vs. 0: 101.40, p-value vs. 0: 2.2440250257239042e-157
Condition 5: r=0.79, t-value vs. 0: 80.45, p-value vs. 0: 3.689204061712602e-140
Condition 6: r=0.78, t-value vs. 0: 86.38, p-value vs. 0: 1.9725181478013672e-145
Condition 7: r=0.86, t-value vs. 0: 85.18, p-value vs. 0: 2.1738823658189874e-144
Condition 8: r=0.75, t-value vs. 0: 66.31, p-value vs. 0: 6.273232399432638e-126
Condition 9: r=0.75, t-value vs. 0: 68.83, p-value vs. 0: 1.1706189133605088e-128
Condition 10: r=0.75, t-value vs. 0: 71.99, p-value vs. 0: 5.898599744111403e-132
Condition 11: r=0.75, t-value vs. 0: 68.02, p-value vs. 0: 8.615829894131056e-128
Condition 12: r=0.75, t-value vs. 0: 71.45, p-value vs. 0: 2.1003084819261166e-131
Condition 13: r=0.89, t-value vs. 0: 103.81, p-value vs. 0: 3.926554495849115e-159
Condition 14: r=0.89, t-value vs. 0: 96.44, p-value vs. 0: 1.2497566773588812e-153
Condition 15: r=0.90, t-value vs. 0: 118.56, p-value vs. 0: 4.380138591830496e-169
Condition 16: r=0.89, t-value vs. 0: 111.23, p-value vs. 0: 2.6572095025518625e-164
Condition 17: r=0.87, t-value vs. 0: 95.06, p-value vs. 0: 1.4945006763202345e-152
Condition 18: r=0.84, t-value vs. 0: 86.58, p-value vs. 0: 1.3452606471273309e-145
Condition 19: r=0.89, t-value vs. 0: 108.55, p-value vs. 0: 1.7784294511937551e-162
Condition 20: r=0.88, t-value vs. 0: 96.21, p-value vs. 0: 1.87093153987005e-153
Condition 21: r=0.89, t-value vs. 0: 111.60, p-value vs. 0: 1.499138184344471e-164
Condition 22: r=0.86, t-value vs. 0: 91.87, p-value vs. 0: 5.194603974170882e-150
Condition 23: r=0.88, t-value vs. 0: 100.57, p-value vs. 0: 9.159874759620729e-157
Condition 24: r=0.89, t-value vs. 0: 110.73, p-value vs. 0: 5.8276683597633154e-164
In [101]:
print("===Compare task-general (condheldout) multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===")
model_compare_TaskGenMultRegFCVsRestMultRegFCMatchTiming_condheldout_Actflow = actflow.model_compare(target_actvect=actflowOutput_restFCMReg_timematchedtaskGeneral_condheldout_bycond['actVect_actual_group'], model1_actvect=actflowOutput_restFCMReg_timematchedtaskGeneral_condheldout_bycond['actPredVector_bytask_bysubj'], model2_actvect=actflowOutput_taskGeneralFCMReg_condheldout_bycond['actPredVector_bytask_bysubj'], full_report=True, print_report=True, print_by_condition=True, mean_absolute_error=True)
===Compare task-general (condheldout) multregFC actflow predictions to Rest multregFC (matching timing) actflow prediction===
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Model1 mean Pearson r=0.87
Model2 mean Pearson r=0.90
R-value difference = -0.03
Model1 vs. Model2 T-value: -32.59, p-value: 3.066329274453115e-76
 
Model1 mean % predicted variance explained R^2=0.75
Model2 mean % predicted variance explained R^2=0.80
R^2 difference = -0.05
 
Model1 mean MAE = 5.81
Model2 mean MAE = 5.04
Model1 vs. Model2 mean MAE difference = 0.77
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Model1 mean Pearson r=0.87
Model2 mean Pearson r=0.89
R-value difference = -0.02
Model1 vs. Model2 T-value: -26.58, p-value: 2.4922422732761544e-63
 
Model1 mean % predicted variance explained R^2=0.48
Model2 mean % predicted variance explained R^2=0.58
R^2 difference = -0.10
 
Model1 mean MAE = 5.81
Model2 mean MAE = 5.04
Model1 vs. Model2 mean MAE difference = 0.77
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Model1 mean Pearson r=0.84
Model2 mean Pearson r=0.88
R-value difference = -0.04
Model1 vs. Model2 T-value: -38.35, p-value: 4.391473507730942e-87
By task condition:
Condition 1: Model 1 r=0.75, Model 2 r=0.77, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -7.31, p-value vs. 0: 9.204864902145285e-12
Condition 2: Model 1 r=0.73, Model 2 r=0.73, Model 1 vs. 2 R-value difference =-0.00, t-value Model1 vs. Model2: -0.24, p-value vs. 0: 0.8111889832566521
Condition 3: Model 1 r=0.88, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -24.60, p-value vs. 0: 1.0569012340111605e-58
Condition 4: Model 1 r=0.88, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -25.82, p-value vs. 0: 1.4129731027689075e-61
Condition 5: Model 1 r=0.79, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.77, p-value vs. 0: 4.624205759949204e-21
Condition 6: Model 1 r=0.78, Model 2 r=0.81, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.70, p-value vs. 0: 7.125625221205066e-21
Condition 7: Model 1 r=0.86, Model 2 r=0.88, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -15.55, p-value vs. 0: 8.398811891781044e-35
Condition 8: Model 1 r=0.75, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.01, p-value vs. 0: 6.244889671940466e-19
Condition 9: Model 1 r=0.75, Model 2 r=0.79, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -12.37, p-value vs. 0: 1.1833987003008544e-25
Condition 10: Model 1 r=0.75, Model 2 r=0.77, Model 1 vs. 2 R-value difference =-0.02, t-value Model1 vs. Model2: -5.67, p-value vs. 0: 5.663054333782167e-08
Condition 11: Model 1 r=0.75, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -11.40, p-value vs. 0: 7.356053094145953e-23
Condition 12: Model 1 r=0.75, Model 2 r=0.78, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -10.20, p-value vs. 0: 1.8003270981706426e-19
Condition 13: Model 1 r=0.89, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -23.47, p-value vs. 0: 5.905072561838314e-56
Condition 14: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -22.69, p-value vs. 0: 5.191093984766338e-54
Condition 15: Model 1 r=0.90, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -18.71, p-value vs. 0: 1.28139896487455e-43
Condition 16: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -17.28, p-value vs. 0: 1.1218987995845137e-39
Condition 17: Model 1 r=0.87, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -19.47, p-value vs. 0: 1.1478434162180423e-45
Condition 18: Model 1 r=0.84, Model 2 r=0.90, Model 1 vs. 2 R-value difference =-0.06, t-value Model1 vs. Model2: -23.71, p-value vs. 0: 1.5580713195357489e-56
Condition 19: Model 1 r=0.89, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.03, t-value Model1 vs. Model2: -19.15, p-value vs. 0: 8.530833093983338e-45
Condition 20: Model 1 r=0.88, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -21.52, p-value vs. 0: 4.7660132079963724e-51
Condition 21: Model 1 r=0.89, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -31.23, p-value vs. 0: 1.8387989855274422e-73
Condition 22: Model 1 r=0.86, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.06, t-value Model1 vs. Model2: -29.76, p-value vs. 0: 2.1830770785749386e-70
Condition 23: Model 1 r=0.88, Model 2 r=0.92, Model 1 vs. 2 R-value difference =-0.04, t-value Model1 vs. Model2: -21.77, p-value vs. 0: 1.0882577669105049e-51
Condition 24: Model 1 r=0.89, Model 2 r=0.93, Model 1 vs. 2 R-value difference =-0.05, t-value Model1 vs. Model2: -28.06, p-value vs. 0: 1.0944741596691381e-66
In [102]:
#Compare task-general and resting-state predictions

#taskcorrFC vs. restcorrFC
#Compute p-value threshold, using FDR correction for multiple comparisons
taskdataset1=np.arctanh(actflowOutput_taskGeneralFCMReg_condheldout_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
taskdataset2=np.arctanh(actflowOutput_restFCMReg_timematchedtaskGeneral_condheldout_bycond['model_compare_output']['corr_conditionwise_compthenavg_bynode'])
ttestinputdata=np.subtract(taskdataset1,taskdataset2)
tstats = [stats.ttest_1samp(ttestinputdata[nodenum],0.0) for nodenum in np.arange(nParcels)]
pvals=[tstats[nodenum][1] for nodenum in np.arange(nParcels)]
pvals_FDRcorrected=sm.stats.multipletests(pvals,method='fdr_bh')[1]
#Compute p-value threshold, using maxT (permutation test) correction for multiple comparisons
maxt_output=actflow.tools.max_t(np.array(ttestinputdata),nproc=numWorkers)
maxt_sigvector=maxt_output[2]<0.05
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05):', 100*np.sum(maxt_sigvector)/nParcels)

tvals=np.array([tstats[nodenum][0] for nodenum in np.arange(nParcels)])
percentpos=100*np.sum(maxt_sigvector*(tvals>0))/nParcels
percentneg=100*np.sum(maxt_sigvector*(tvals<0))/nParcels
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE:', percentpos)
print('Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE:', percentneg)

#Visualize data
# datamat=np.tanh(np.mean(np.subtract(taskdataset,restdataset),axis=1))
# #Apply threshold
# datamat=np.multiply(datamat,maxt_sigvector)
# datamat=datamat[:,np.newaxis]
# outfilename='../figures/figure_parts/taskgeneralVStaskstateActflowPred_condwise_MultregFC_discovery176'
# #sys.path.insert(0,'/projects/community/connectome_wb/1.3.2/kholodvl/workbench/bin_rh_linux64/')
# actflow.tools.map_to_surface(datamat,outfilename,fliphemispheres=True)

# #Max and min values
# print('Maximum of map: ', np.max(datamat[datamat!=0]))
# print('Minimum of map: ', np.min(datamat[datamat!=0]))
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05): 40.55555555555556
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) POSITIVE: 40.55555555555556
Percent significant nodes (maxT corrected for multiple comparisons, p<0.05) NEGATIVE: 0.0

Actflow prediction using other tasks individually

In [103]:
#ITERATE THROUGH ALTERNATIVE TASKS FOR FC: Run actflow predictions, 24-condition task multiple-regression FC
print("==ITERATE THROUGH ALTERNATIVE TASKS FOR FC: Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==")
#actflowOutput_taskFCMReg_bycond_test = actflow.actflowcomp.actflowtest(activations_bycond[:,0,:], taskFC_mreg_bycond_bysubj[:,:,1,:], print_by_condition=False, full_report=False)

#Calculate actflow predictions
predaccs_byFCcond_byactcond_bysubj=np.zeros((nConds, nConds, nSubjs))
scount = 0
for subj in subjNums:
    for FC_cond in np.arange(nConds):
        for act_cond in np.arange(nConds):
                
            actPredVector=actflow.actflowcalc(activations_bycond[:,act_cond,scount], taskFC_mreg_bycond_bysubj[:,:,FC_cond,scount])
            predaccs_byFCcond_byactcond_bysubj[FC_cond,act_cond,scount]=np.corrcoef(actPredVector,activations_bycond[:,act_cond,scount])[0,1]
            
    scount=scount+1

#Assess statistical signficance of actflow predictions
tstats_predaccs_byFCcond_byactcond=np.zeros((nConds, nConds))
pvals_predaccs_byFCcond_byactcond=np.ones((nConds, nConds))
for FC_cond in np.arange(nConds):
    for act_cond in np.arange(nConds):
            
            tstats = stats.ttest_1samp(np.arctanh(predaccs_byFCcond_byactcond_bysubj[FC_cond,act_cond,:]),0.0)
            tstats_predaccs_byFCcond_byactcond[FC_cond,act_cond]=tstats[0]
            pvals_predaccs_byFCcond_byactcond[FC_cond,act_cond]=tstats[1]


            
==ITERATE THROUGH ALTERNATIVE TASKS FOR FC: Activity flow mapping results, multiple-regression-based task-state FC, 24 task conditions==
In [104]:
ncomparisons=nConds*nConds
bonferroni_thresh=.05/ncomparisons
threshed_data=np.mean(predaccs_byFCcond_byactcond_bysubj,axis=2)*(pvals_predaccs_byFCcond_byactcond<bonferroni_thresh)
ax = sns.heatmap(threshed_data,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('sig (p<0.05) predaccs_byFCcond_byactcond')
ax.set(ylabel='FC source',xlabel='Predicted condition')
print('bonferroni_thresh (p<0.05 corrected): p<', bonferroni_thresh)
bonferroni_thresh (p<0.05 corrected): p< 8.680555555555556e-05
In [105]:
print('Task-state FC state generalization matrix analysis')
#upperTIndices_24by24=np.triu(np.ones((24,24)),k=1)>0
#Remove diagonal
blankmat=np.ones((nConds,nConds))
upperTmat=np.triu(blankmat,k=1)
lowerTmat=np.tril(blankmat,k=-1)
offdiagmat_conds=(upperTmat+lowerTmat)>0
mean_predact_crossconditions=np.tanh(np.mean(np.mean(np.mean(np.arctanh(predaccs_byFCcond_byactcond_bysubj),axis=2)[offdiagmat_conds])))
print('mean_predact_crossconditions: r=', mean_predact_crossconditions)

mean_predact_bysubj=np.mean(np.mean(np.arctanh(predaccs_byFCcond_byactcond_bysubj),axis=0),axis=1)
tstats = stats.ttest_1samp(mean_predact_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value vs. 0:', tstat)
print('p-value vs. 0:', pval)
Task-state FC state generalization matrix analysis
mean_predact_crossconditions: r= 0.48848133775920544
t-value vs. 0: 19.183063229746963
p-value vs. 0: 1.19786654210894e-15
In [106]:
#ITERATE THROUGH ALTERNATIVE TASK-EQUIVALENT TIME FROM REST FOR FC: Run actflow predictions, 24-condition rest multiple-regression FC
print("==ITERATE THROUGH TASK-EQUIVALENT TIME FROM REST FOR FC: Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions==")

#Calculate actflow predictions
predaccsRestFC_byFCcond_byactcond_bysubj=np.zeros((nConds, nConds, nSubjs))
scount = 0
for subj in subjNums:
    for FC_cond in np.arange(nConds):
        for act_cond in np.arange(nConds):
                
            actPredVector=actflow.actflowcalc(activations_bycond[:,act_cond,scount], restFCMatchedTiming_mreg_bycond_bysubj[:,:,FC_cond,scount])
            predaccsRestFC_byFCcond_byactcond_bysubj[FC_cond,act_cond,scount]=np.corrcoef(actPredVector,activations_bycond[:,act_cond,scount])[0,1]
            
    scount=scount+1

#Assess statistical signficance of actflow predictions
tstats_predaccsRestFC_byFCcond_byactcond=np.zeros((nConds, nConds))
pvals_predaccsRestFC_byFCcond_byactcond=np.ones((nConds, nConds))
for FC_cond in np.arange(nConds):
    for act_cond in np.arange(nConds):
            
            tstats = stats.ttest_1samp(np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj[FC_cond,act_cond,:]),0.0)
            tstats_predaccsRestFC_byFCcond_byactcond[FC_cond,act_cond]=tstats[0]
            pvals_predaccsRestFC_byFCcond_byactcond[FC_cond,act_cond]=tstats[1]


            
==ITERATE THROUGH TASK-EQUIVALENT TIME FROM REST FOR FC: Activity flow mapping results, multiple-regression-based resting-state FC, 24 task conditions==
In [107]:
print('Resting-state FC state generalization matrix analysis:')
#upperTIndices_24by24=np.triu(np.ones((24,24)),k=1)>0
#Remove diagonal
blankmat=np.ones((nConds,nConds))
upperTmat=np.triu(blankmat,k=1)
lowerTmat=np.tril(blankmat,k=-1)
offdiagmat_conds=(upperTmat+lowerTmat)>0
mean_predactrest_crossconditions=np.tanh(np.mean(np.mean(np.mean(np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj),axis=2)[offdiagmat_conds])))
print('mean_predactrest_crossconditions: r=', mean_predactrest_crossconditions)

mean_predact_bysubj=np.mean(np.mean(np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj),axis=0),axis=1)
tstats = stats.ttest_1samp(mean_predact_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value vs. 0:', tstat)
print('p-value vs. 0:', pval)

print('Task-state FC vs. Resting-state FC state generalization matrix analysis:')
mean_predacttaskVSrest_crossconditions=np.tanh(np.mean(np.mean(np.mean(np.arctanh(predaccs_byFCcond_byactcond_bysubj)-np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj),axis=2)[offdiagmat_conds])))
print('mean_predacttaskVSrest_crossconditions: r=', mean_predacttaskVSrest_crossconditions)

mean_predact_bysubj=np.mean(np.mean(np.arctanh(predaccs_byFCcond_byactcond_bysubj)-np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj),axis=0),axis=1)
tstats = stats.ttest_1samp(mean_predact_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value paired:', tstat)
print('p-value paired:', pval)
Resting-state FC state generalization matrix analysis:
mean_predactrest_crossconditions: r= 0.45401824399178853
t-value vs. 0: 30.07000789427279
p-value vs. 0: 5.737430018678397e-20
Task-state FC vs. Resting-state FC state generalization matrix analysis:
mean_predacttaskVSrest_crossconditions: r= 0.0442844811651388
t-value paired: 4.095619108075989
p-value paired: 0.00044354105308819247
In [108]:
#Assess statistical signficance of actflow predictions, TASK FC PREDICTIONS VS REST FC PREDICTIONS
tstats_predaccsTaskVSRestFC_byFCcond_byactcond=np.zeros((nConds, nConds))
pvals_predaccsTaskVSRestFC_byFCcond_byactcond=np.ones((nConds, nConds))
for FC_cond in np.arange(nConds):
    for act_cond in np.arange(nConds):
            #Paired t-tests
            predacc_diffs=np.arctanh(predaccs_byFCcond_byactcond_bysubj[FC_cond,act_cond,:])-np.arctanh(predaccsRestFC_byFCcond_byactcond_bysubj[FC_cond,act_cond,:])
            tstats = stats.ttest_1samp(predacc_diffs,0.0)
            tstats_predaccsTaskVSRestFC_byFCcond_byactcond[FC_cond,act_cond]=tstats[0]
            pvals_predaccsTaskVSRestFC_byFCcond_byactcond[FC_cond,act_cond]=tstats[1]
In [109]:
#Visualize matrices
predaccs_byFCcond_byactcond_mean=np.mean(predaccs_byFCcond_byactcond_bysubj,axis=2)
predaccsRestFC_byFCcond_byactcond_mean=np.mean(predaccsRestFC_byFCcond_byactcond_bysubj,axis=2)
datamin=np.min(np.array(np.min(np.min(predaccs_byFCcond_byactcond_mean)), np.min(np.min(predaccsRestFC_byFCcond_byactcond_mean))))
datamax=np.max(np.array(np.max(np.max(predaccs_byFCcond_byactcond_mean)), np.max(np.max(predaccsRestFC_byFCcond_byactcond_mean))))

ax = sns.heatmap(predaccs_byFCcond_byactcond_mean,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True, vmin=datamin,vmax=datamax)
ax.figure.suptitle('mean predaccs_byFCcond_byactcond')
ax.set(ylabel='FC source',xlabel='Predicted condition')
fig=ax.get_figure()
fig.savefig(figuredir+'predaccs_byFCcond_byactcond_discovery176.pdf')

#Resting-state FC predictions
ncomparisons=nConds*nConds
bonferroni_thresh=.05/ncomparisons
threshed_data=predaccsRestFC_byFCcond_byactcond_mean*(pvals_predaccsRestFC_byFCcond_byactcond<bonferroni_thresh)
plt.figure()
ax = sns.heatmap(threshed_data,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True, vmin=datamin,vmax=datamax)
ax.figure.suptitle('sig (p<0.05) predaccsRestFC_byFCcond_byactcond_bysubj')
ax.set(ylabel='FC source ',xlabel='Predicted condition')
print('bonferroni_thresh (p<0.05 corrected): p<', bonferroni_thresh)
fig=ax.get_figure()
fig.savefig(figuredir+'predaccsRestFC_byFCcond_byactcond_discovery176.pdf')
bonferroni_thresh (p<0.05 corrected): p< 8.680555555555556e-05
In [110]:
#Task FC - rest FC predictions no thresh
#ncomparisons=nConds*nConds
#bonferroni_thresh=.05/ncomparisons
#threshed_data=np.mean(predaccs_byFCcond_byactcond_bysubj-predaccsRestFC_byFCcond_byactcond_bysubj,axis=2)*(pvals_predaccsTaskVSRestFC_byFCcond_byactcond<bonferroni_thresh)
ax = sns.heatmap(np.mean(predaccs_byFCcond_byactcond_bysubj-predaccsRestFC_byFCcond_byactcond_bysubj,axis=2),center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('predaccsTask - predaccsRestFC')
ax.set(ylabel='FC source',xlabel='Predicted condition')
#print('bonferroni_thresh (p<0.05 corrected): p<', bonferroni_thresh)
fig=ax.get_figure()
fig.savefig(figuredir+'predaccs_byFCcond_byactcond_unthreshed_discovery176.pdf')
In [111]:
#Task FC - rest FC predictions
ncomparisons=nConds*nConds
bonferroni_thresh=.05/ncomparisons
threshed_data=np.mean(predaccs_byFCcond_byactcond_bysubj-predaccsRestFC_byFCcond_byactcond_bysubj,axis=2)*(pvals_predaccsTaskVSRestFC_byFCcond_byactcond<bonferroni_thresh)
ax = sns.heatmap(threshed_data,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('sig (p<0.05) predaccsTask - predaccsRestFC')
ax.set(ylabel='FC source',xlabel='Predicted condition')
print('bonferroni_thresh (p<0.05 corrected): p<', bonferroni_thresh)
fig=ax.get_figure()
fig.savefig(figuredir+'predaccs_byFCcond_byactcond_threshbonp05_discovery176.pdf')
bonferroni_thresh (p<0.05 corrected): p< 8.680555555555556e-05
In [112]:
actsim_bycond_bysubj=np.zeros((nConds, nConds, nSubjs))
scount = 0
for subj in subjNums:
    actsim_bycond_bysubj[:,:,scount]=np.corrcoef(np.transpose(activations_bycond[:,:,scount]))
    scount=scount+1

actsim_bycond_mean=np.mean(actsim_bycond_bysubj,axis=2)
np.fill_diagonal(actsim_bycond_mean,0)
ax = sns.heatmap(actsim_bycond_mean,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('Task activation similarity')
fig=ax.get_figure()
fig.savefig(figuredir+'actsim_bycond_discovery176.pdf')
In [113]:
taskFCsim_bycond_bysubj=np.zeros((nConds, nConds, nSubjs))
scount = 0
for subj in subjNums:
    taskFCsim_bycond_bysubj[:,:,scount]=np.corrcoef(np.transpose(taskFC_mreg_bycond_bysubj[:,:,:,scount].reshape(nParcels*nParcels,nConds)))
    scount=scount+1

taskFCsim_bycond_mean=np.mean(taskFCsim_bycond_bysubj,axis=2)
np.fill_diagonal(taskFCsim_bycond_mean,0)
ax = sns.heatmap(taskFCsim_bycond_mean,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('Task FC similarity')
fig=ax.get_figure()
fig.savefig(figuredir+'taskFCsim_bycond_discovery176.pdf')
In [114]:
taskFCsim_bycond_avgfirst=np.corrcoef(np.transpose(np.mean(taskFC_mreg_bycond_bysubj,axis=3).reshape(nParcels*nParcels,nConds)))

np.fill_diagonal(taskFCsim_bycond_avgfirst,0)
ax = sns.heatmap(taskFCsim_bycond_avgfirst,center=0,cmap='seismic',cbar=True,yticklabels=taskConditions,xticklabels=taskConditions, square=True)
ax.figure.suptitle('Task FC similarity, avg. first')
#fig=ax.get_figure()
#fig.savefig('../figures/figure_parts/taskFCsim_bycond_discovery176.pdf')
Out[114]:
Text(0.5, 0.98, 'Task FC similarity, avg. first')
In [115]:
#Compare taskFC generalization matrix to each of the others to infer likely cause of taskFC generalization

#Set up for removing diagonal
blankmat=np.ones((nConds,nConds))
upperTmat=np.triu(blankmat,k=1)
lowerTmat=np.tril(blankmat,k=-1)
offdiagmat_conds=(upperTmat+lowerTmat)>0


taskFCaccTOrestFCacc_bysubj=np.zeros((nSubjs))
scount = 0
for subj in subjNums:
    taskFCaccTOrestFCacc_bysubj[scount]=np.corrcoef(predaccs_byFCcond_byactcond_bysubj[:,:,scount][offdiagmat_conds],predaccsRestFC_byFCcond_byactcond_bysubj[:,:,scount][offdiagmat_conds])[0,1]
    scount=scount+1
print('taskFCaccTOrestFCacc_bysubj: r=',np.mean(taskFCaccTOrestFCacc_bysubj))

taskFCaccTOactsim_bysubj=np.zeros((nSubjs))
scount = 0
for subj in subjNums:
    taskFCaccTOactsim_bysubj[scount]=np.corrcoef(predaccs_byFCcond_byactcond_bysubj[:,:,scount][offdiagmat_conds],actsim_bycond_bysubj[:,:,scount][offdiagmat_conds])[0,1]
    scount=scount+1
print('taskFCaccTOactsim_bysubj: r=',np.mean(taskFCaccTOactsim_bysubj))
tstats = stats.ttest_1samp(taskFCaccTOactsim_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value vs. 0:', tstat)
print('p-value vs. 0:', pval)

taskFCaccTOtaskFCsim_bysubj=np.zeros((nSubjs))
scount = 0
for subj in subjNums:
    taskFCaccTOtaskFCsim_bysubj[scount]=np.corrcoef(predaccs_byFCcond_byactcond_bysubj[:,:,scount][offdiagmat_conds],taskFCsim_bycond_bysubj[:,:,scount][offdiagmat_conds])[0,1]
    scount=scount+1
print('taskFCaccTOtaskFCsim_bysubj: r=',np.mean(taskFCaccTOtaskFCsim_bysubj))
tstats = stats.ttest_1samp(taskFCaccTOtaskFCsim_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value vs. 0:', tstat)
print('p-value vs. 0:', pval)
taskFCaccTOrestFCacc_bysubj: r= 0.4991470600929351
taskFCaccTOactsim_bysubj: r= 0.607863648433976
t-value vs. 0: 95.48638529214321
p-value vs. 0: 6.878579886592626e-153
taskFCaccTOtaskFCsim_bysubj: r= 0.21144780699282492
t-value vs. 0: 38.639751801303944
p-value vs. 0: 1.3383304057929757e-87
In [116]:
print('Comparing actflow prediction for similar vs dissimilar task conditions (using activation similarity patterns):')
actsim_bycond_mean=np.mean(actsim_bycond_bysubj,axis=2)
np.fill_diagonal(actsim_bycond_mean,0)
pos_actsim_bycond_mean=actsim_bycond_mean>0
neg_actsim_bycond_mean=actsim_bycond_mean<0
predaccstaskVSrestFC_bycond_bysubj=predaccs_byFCcond_byactcond_bysubj-predaccsRestFC_byFCcond_byactcond_bysubj
predaccstaskVSrestFC_bycond_mean=np.mean(predaccstaskVSrestFC_bycond_bysubj,axis=2)
print('pos_actsim_bycond_mean: r=',np.tanh(np.mean(np.arctanh(predaccstaskVSrestFC_bycond_mean[pos_actsim_bycond_mean]))))
print('neg_actsim_bycond_mean: r=',np.tanh(np.mean(np.arctanh(predaccstaskVSrestFC_bycond_mean[neg_actsim_bycond_mean]))))
print('pos_actsim_bycond_mean - neg_actsim_bycond_mean: r-diff=',np.tanh(np.mean(np.arctanh(predaccstaskVSrestFC_bycond_mean[pos_actsim_bycond_mean])))-np.tanh(np.mean(np.arctanh(predaccstaskVSrestFC_bycond_mean[neg_actsim_bycond_mean]))))

pos_actsim_bysubj=np.zeros((nSubjs))
neg_actsim_bysubj=np.zeros((nSubjs))
scount = 0
for subj in subjNums:
    pos_actsim_bysubj[scount]=np.mean(np.arctanh(predaccstaskVSrestFC_bycond_bysubj[:,:,scount][pos_actsim_bycond_mean]))
    neg_actsim_bysubj[scount]=np.mean(np.arctanh(predaccstaskVSrestFC_bycond_bysubj[:,:,scount][neg_actsim_bycond_mean]))
    scount=scount+1

tstats = stats.ttest_1samp(pos_actsim_bysubj-neg_actsim_bysubj,0.0)
tstat=tstats[0]
pval=tstats[1]
print('t-value paired:', tstat)
print('p-value paired:', pval)
Comparing actflow prediction for similar vs dissimilar task conditions (using activation similarity patterns):
pos_actsim_bycond_mean: r= 0.04061270698722598
neg_actsim_bycond_mean: r= -0.04589371539465954
pos_actsim_bycond_mean - neg_actsim_bycond_mean: r-diff= 0.08650642238188552
t-value paired: 31.862339890244556
p-value paired: 9.177379368228011e-75

Number of time points analysis

Plan: predict 8 WM task conditions only, using (rest or task-general) data of different lengths: 5min, 10min, 20min, 50min

In [117]:
#REST: 5, 10, 20, and 30 minutes
## Calculating multreg resting-state FC time-matched to task-general FC, CURRENT TASK HELD OUT, with SLURM cluster command (in parallel), loading from data if saved

durations_insecs=np.array((300, 600, 1200, 1800))
durations_inTRs=np.round(durations_insecs/.72)
nDurations=4

loaddata_ifavailable=True
datasuffix='_multregRestFC_5_10_20_30mins_data'

restFC_mreg_bydurations_bysubj=np.zeros((nParcels,nParcels,nDurations,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for durnum in np.arange(nDurations):
            dataid = 'condition' + str(durnum)
            restFC_mreg_bydurations_bysubj[:,:,durnum,scount] = h5f['restFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadRestResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
        file_python.write("print('Subject " + str(subj) + "')\n")
        
        file_python.write("restdata = loadRestResiduals_byscript.loadRestResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        #file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("durations_insecs=np.array((300, 600, 1200, 1800))\n")
        file_python.write("durations_inTRs=np.round(durations_insecs/.72).astype(int)\n")
        file_python.write("for durnum in np.arange(4):\n")
        file_python.write("    print(str(durnum))\n")
        file_python.write("    restFC_mreg_bysubj=actflow.connectivity_estimation.multregconn(restdata[:,0:durations_inTRs[durnum]])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'restFCRegression/' + 'condition' + str(durnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=restFC_mreg_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=nm3\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:05:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [118]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(restFC_mreg_bydurations_bysubj[netorder,:,3,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg rest FC 30 mins')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[118]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [119]:
#Run actflow predictions, 24-condition resting-state FC, 5 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 5 min multiple-regression FC==")
actflowOutput_restFCMReg_5mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bydurations_bysubj[:,:,0,:], full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 5 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.36, t-value vs. 0: 74.85, p-value vs. 0: 7.931469751532154e-135
 
Mean % variance explained (R^2 score, coeff. of determination) = -2.71
 
Mean MAE (mean absolute error) = 21.28
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.46, t-value vs. 0: 85.94, p-value vs. 0: 4.7702656810429475e-145
 
Mean % variance explained (R^2 score, coeff. of determination) = -7.42
 
Mean MAE (mean absolute error) = 21.28
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.34, t-value vs. 0: 75.18, p-value vs. 0: 3.797830540993658e-135
By task condition:
Condition 1: r=0.28, t-value vs. 0: 30.48, p-value vs. 0: 6.535866809242082e-72
Condition 2: r=0.27, t-value vs. 0: 27.60, p-value vs. 0: 1.1771091000889513e-65
Condition 3: r=0.41, t-value vs. 0: 44.77, p-value vs. 0: 9.156416676113807e-98
Condition 4: r=0.40, t-value vs. 0: 43.47, p-value vs. 0: 1.0218051138113727e-95
Condition 5: r=0.30, t-value vs. 0: 32.28, p-value vs. 0: 1.31988915017452e-75
Condition 6: r=0.29, t-value vs. 0: 32.54, p-value vs. 0: 3.9373499335445346e-76
Condition 7: r=0.37, t-value vs. 0: 39.14, p-value vs. 0: 1.7482710848747243e-88
Condition 8: r=0.25, t-value vs. 0: 29.65, p-value vs. 0: 3.8751471294514e-70
Condition 9: r=0.25, t-value vs. 0: 28.61, p-value vs. 0: 6.682105243718298e-68
Condition 10: r=0.25, t-value vs. 0: 33.74, p-value vs. 0: 1.6512122451369405e-78
Condition 11: r=0.25, t-value vs. 0: 29.00, p-value vs. 0: 9.85527610332059e-69
Condition 12: r=0.27, t-value vs. 0: 33.39, p-value vs. 0: 7.893144132140037e-78
Condition 13: r=0.41, t-value vs. 0: 45.89, p-value vs. 0: 1.6720983872795994e-99
Condition 14: r=0.41, t-value vs. 0: 44.92, p-value vs. 0: 5.303178900292741e-98
Condition 15: r=0.39, t-value vs. 0: 45.52, p-value vs. 0: 6.149918934425392e-99
Condition 16: r=0.38, t-value vs. 0: 43.92, p-value vs. 0: 2.0064817425767497e-96
Condition 17: r=0.37, t-value vs. 0: 40.89, p-value vs. 0: 1.7920907048549355e-91
Condition 18: r=0.31, t-value vs. 0: 31.81, p-value vs. 0: 1.1595825829331282e-74
Condition 19: r=0.39, t-value vs. 0: 39.52, p-value vs. 0: 3.852864494266858e-89
Condition 20: r=0.39, t-value vs. 0: 44.38, p-value vs. 0: 3.7424565625601656e-97
Condition 21: r=0.40, t-value vs. 0: 45.15, p-value vs. 0: 2.3663878751516257e-98
Condition 22: r=0.33, t-value vs. 0: 34.85, p-value vs. 0: 1.1845874195746194e-80
Condition 23: r=0.35, t-value vs. 0: 39.77, p-value vs. 0: 1.427666544211222e-89
Condition 24: r=0.38, t-value vs. 0: 44.73, p-value vs. 0: 1.0381324043767492e-97
In [120]:
#Run actflow predictions, 24-condition resting-state FC, 10 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 10 min multiple-regression FC==")
actflowOutput_restFCMReg_10mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bydurations_bysubj[:,:,1,:], full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 10 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.71, t-value vs. 0: 115.80, p-value vs. 0: 2.5569678238001405e-167
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.39
 
Mean MAE (mean absolute error) = 8.91
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.76, t-value vs. 0: 124.43, p-value vs. 0: 1.0227109058553223e-172
 
Mean % variance explained (R^2 score, coeff. of determination) = -0.32
 
Mean MAE (mean absolute error) = 8.91
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.67, t-value vs. 0: 105.78, p-value vs. 0: 1.544724453056401e-160
By task condition:
Condition 1: r=0.57, t-value vs. 0: 49.76, p-value vs. 0: 3.2928183587556366e-105
Condition 2: r=0.54, t-value vs. 0: 45.11, p-value vs. 0: 2.7376674108442955e-98
Condition 3: r=0.73, t-value vs. 0: 74.90, p-value vs. 0: 7.139468443205578e-135
Condition 4: r=0.73, t-value vs. 0: 77.35, p-value vs. 0: 2.9680311694833467e-137
Condition 5: r=0.61, t-value vs. 0: 59.53, p-value vs. 0: 4.468516807061745e-118
Condition 6: r=0.61, t-value vs. 0: 62.48, p-value vs. 0: 1.362156240535415e-121
Condition 7: r=0.70, t-value vs. 0: 63.34, p-value vs. 0: 1.3606447407254602e-122
Condition 8: r=0.56, t-value vs. 0: 48.19, p-value vs. 0: 5.985866871352595e-103
Condition 9: r=0.55, t-value vs. 0: 49.12, p-value vs. 0: 2.6857781386105127e-104
Condition 10: r=0.55, t-value vs. 0: 51.25, p-value vs. 0: 2.6104505612967704e-107
Condition 11: r=0.54, t-value vs. 0: 50.88, p-value vs. 0: 8.427188186269656e-107
Condition 12: r=0.57, t-value vs. 0: 56.86, p-value vs. 0: 9.16788723422958e-115
Condition 13: r=0.75, t-value vs. 0: 76.89, p-value vs. 0: 8.156327990500936e-137
Condition 14: r=0.74, t-value vs. 0: 75.93, p-value vs. 0: 7.033089457514934e-136
Condition 15: r=0.74, t-value vs. 0: 80.94, p-value vs. 0: 1.3151997586365074e-140
Condition 16: r=0.72, t-value vs. 0: 74.39, p-value vs. 0: 2.2489530548698184e-134
Condition 17: r=0.71, t-value vs. 0: 66.37, p-value vs. 0: 5.348022502052031e-126
Condition 18: r=0.64, t-value vs. 0: 60.29, p-value vs. 0: 5.363876577671447e-119
Condition 19: r=0.73, t-value vs. 0: 73.66, p-value vs. 0: 1.2175467948951398e-133
Condition 20: r=0.73, t-value vs. 0: 72.60, p-value vs. 0: 1.409314287524189e-132
Condition 21: r=0.74, t-value vs. 0: 79.93, p-value vs. 0: 1.118439692486234e-139
Condition 22: r=0.67, t-value vs. 0: 67.33, p-value vs. 0: 4.818012864418328e-127
Condition 23: r=0.71, t-value vs. 0: 67.43, p-value vs. 0: 3.6836957596815746e-127
Condition 24: r=0.73, t-value vs. 0: 78.44, p-value vs. 0: 2.745274928950422e-138
In [121]:
#Run actflow predictions, 24-condition resting-state FC, 20 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 20 min multiple-regression FC==")
actflowOutput_restFCMReg_20mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bydurations_bysubj[:,:,2,:], full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 20 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.83, t-value vs. 0: 142.39, p-value vs. 0: 7.265831352906432e-183
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.67
 
Mean MAE (mean absolute error) = 6.63
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.84, t-value vs. 0: 150.87, p-value vs. 0: 3.1815584072351045e-187
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.31
 
Mean MAE (mean absolute error) = 6.63
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.79, t-value vs. 0: 127.77, p-value vs. 0: 1.0374245266625574e-174
By task condition:
Condition 1: r=0.69, t-value vs. 0: 60.40, p-value vs. 0: 3.956628782447701e-119
Condition 2: r=0.67, t-value vs. 0: 54.02, p-value vs. 0: 4.408178497860057e-111
Condition 3: r=0.84, t-value vs. 0: 95.39, p-value vs. 0: 8.250795310454885e-153
Condition 4: r=0.84, t-value vs. 0: 94.13, p-value vs. 0: 8.068789804444743e-152
Condition 5: r=0.74, t-value vs. 0: 74.74, p-value vs. 0: 1.0146414575958419e-134
Condition 6: r=0.73, t-value vs. 0: 77.81, p-value vs. 0: 1.1019850105053447e-137
Condition 7: r=0.81, t-value vs. 0: 75.61, p-value vs. 0: 1.440122634579622e-135
Condition 8: r=0.70, t-value vs. 0: 59.01, p-value vs. 0: 1.935968230593107e-117
Condition 9: r=0.69, t-value vs. 0: 58.42, p-value vs. 0: 1.0276532548585432e-116
Condition 10: r=0.69, t-value vs. 0: 62.50, p-value vs. 0: 1.281899378248095e-121
Condition 11: r=0.69, t-value vs. 0: 64.89, p-value vs. 0: 2.3771862630734264e-124
Condition 12: r=0.70, t-value vs. 0: 67.82, p-value vs. 0: 1.4137259331308065e-127
Condition 13: r=0.85, t-value vs. 0: 94.42, p-value vs. 0: 4.71245639123498e-152
Condition 14: r=0.85, t-value vs. 0: 89.43, p-value vs. 0: 5.2180985448195014e-148
Condition 15: r=0.85, t-value vs. 0: 103.19, p-value vs. 0: 1.107492256414294e-158
Condition 16: r=0.84, t-value vs. 0: 97.35, p-value vs. 0: 2.5030812014085806e-154
Condition 17: r=0.82, t-value vs. 0: 84.32, p-value vs. 0: 1.2354174716384153e-143
Condition 18: r=0.78, t-value vs. 0: 76.31, p-value vs. 0: 2.998411046780824e-136
Condition 19: r=0.85, t-value vs. 0: 95.96, p-value vs. 0: 2.9653311807928546e-153
Condition 20: r=0.84, t-value vs. 0: 89.06, p-value vs. 0: 1.0659629501220014e-147
Condition 21: r=0.85, t-value vs. 0: 100.87, p-value vs. 0: 5.528091991223339e-157
Condition 22: r=0.80, t-value vs. 0: 83.27, p-value vs. 0: 1.0502029045040436e-142
Condition 23: r=0.83, t-value vs. 0: 90.82, p-value vs. 0: 3.7446333839184135e-149
Condition 24: r=0.84, t-value vs. 0: 97.19, p-value vs. 0: 3.2987538129190233e-154
In [122]:
#Run actflow predictions, 24-condition resting-state FC, 30 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 30 min multiple-regression FC==")
actflowOutput_restFCMReg_30mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, restFC_mreg_bydurations_bysubj[:,:,3,:], full_report=True)
==Activity flow mapping results, multiple-regression-based resting-state FC, 24-conditions, 30 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.86, t-value vs. 0: 154.46, p-value vs. 0: 5.311056686344529e-189
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.73
 
Mean MAE (mean absolute error) = 5.98
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.87, t-value vs. 0: 160.17, p-value vs. 0: 9.740611566499041e-192
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.45
 
Mean MAE (mean absolute error) = 5.98
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.83, t-value vs. 0: 138.74, p-value vs. 0: 6.588476206920902e-181
By task condition:
Condition 1: r=0.73, t-value vs. 0: 65.50, p-value vs. 0: 4.961476774995813e-125
Condition 2: r=0.71, t-value vs. 0: 57.90, p-value vs. 0: 4.554797292678841e-116
Condition 3: r=0.87, t-value vs. 0: 103.64, p-value vs. 0: 5.258086472408917e-159
Condition 4: r=0.87, t-value vs. 0: 101.05, p-value vs. 0: 4.0794006218903794e-157
Condition 5: r=0.78, t-value vs. 0: 80.28, p-value vs. 0: 5.282722726201787e-140
Condition 6: r=0.77, t-value vs. 0: 83.72, p-value vs. 0: 4.170368229754999e-143
Condition 7: r=0.85, t-value vs. 0: 81.55, p-value vs. 0: 3.66253939473722e-141
Condition 8: r=0.74, t-value vs. 0: 64.72, p-value vs. 0: 3.707134690528687e-124
Condition 9: r=0.74, t-value vs. 0: 66.86, p-value vs. 0: 1.5353614980293455e-126
Condition 10: r=0.74, t-value vs. 0: 69.77, p-value vs. 0: 1.1720444790111085e-129
Condition 11: r=0.74, t-value vs. 0: 69.93, p-value vs. 0: 7.876774794834655e-130
Condition 12: r=0.75, t-value vs. 0: 71.36, p-value vs. 0: 2.577598340367895e-131
Condition 13: r=0.88, t-value vs. 0: 100.39, p-value vs. 0: 1.2486913478083415e-156
Condition 14: r=0.88, t-value vs. 0: 93.13, p-value vs. 0: 4.992258776687425e-151
Condition 15: r=0.89, t-value vs. 0: 114.30, p-value vs. 0: 2.434639222979485e-166
Condition 16: r=0.87, t-value vs. 0: 108.91, p-value vs. 0: 1.0203377107749054e-162
Condition 17: r=0.86, t-value vs. 0: 92.91, p-value vs. 0: 7.486613158221408e-151
Condition 18: r=0.82, t-value vs. 0: 83.15, p-value vs. 0: 1.3290185254562153e-142
Condition 19: r=0.88, t-value vs. 0: 108.45, p-value vs. 0: 2.1096388847448473e-162
Condition 20: r=0.87, t-value vs. 0: 100.24, p-value vs. 0: 1.6327892836510274e-156
Condition 21: r=0.88, t-value vs. 0: 111.36, p-value vs. 0: 2.1835138045369e-164
Condition 22: r=0.84, t-value vs. 0: 92.71, p-value vs. 0: 1.0905683549595854e-150
Condition 23: r=0.86, t-value vs. 0: 99.38, p-value vs. 0: 7.178457411126566e-156
Condition 24: r=0.88, t-value vs. 0: 107.93, p-value vs. 0: 4.788294097918601e-162
In [123]:
#TASK-GENERAL: 5, 10, 20, and 30 minutes
## Calculating multreg resting-state FC time-matched to task-general FC, CURRENT TASK HELD OUT, with SLURM cluster command (in parallel), loading from data if saved

durations_insecs=np.array((300, 600, 1200, 1800))
durations_inTRs=np.round(durations_insecs/.72)
nDurations=4

loaddata_ifavailable=True
datasuffix='_multregTaskGenFC_5_10_20_30mins_data'

taskgenFC_mreg_bydurations_bysubj=np.zeros((nParcels,nParcels,nDurations,nSubjs))
job_sent_to_scheduler=False
import os
scount = 0
for subj in subjNums:
    #print('Subject ' + subj)
    file_path=resultsdir + subj + datasuffix + '.h5'
    if loaddata_ifavailable and os.path.exists(file_path):
        h5f = h5py.File(file_path,'r')
        for durnum in np.arange(nDurations):
            dataid = 'condition' + str(durnum)
            taskgenFC_mreg_bydurations_bysubj[:,:,durnum,scount] = h5f['taskFCRegression'][dataid][:]
        h5f.close()
    else:
        if job_sent_to_scheduler==False:
            job_sent_to_scheduler=True
            print('Sending job to SLURM scheduler')
        
        file_python = open("batchscripts/pythonscript_" + str(subj) + ".py", "w")
        file_python.write("import sys; sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/'); sys.path.insert(0, '" + basedir + "/docs/scripts/final_analyses/glmScripts/parcellated_postproc/')\n")
        file_python.write("import loadTaskResiduals_byscript; import taskGLMPipeline_withNuisanceReg_GlasserParcels as tgp; import h5py; import ActflowToolbox as actflow; import numpy as np\n")
 
        file_python.write("print('Subject " + str(subj) + "')\n")
        file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR_plus_canonical',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        #file_python.write("taskdata = loadTaskResiduals_byscript.loadTaskResiduals_byscript('" + str(subj) + "',model='24pXaCompCorXVolterra',taskModel='FIR',zscore=False,vertexVoxelWise=False, preprocdatadir_vertexwise='" + preprocdatadir_vertexwise + "', datadir='" + datadir + "')\n")
        file_python.write("tasktiming_bycond=tgp.loadTaskTimingForAllTasks('"+str(subj)+"',taskModel='canonical')['taskRegressors']>.5\n")
        #Restrict to the number of task time points
        #file_python.write("restdata = restdata[:,0:np.shape(tasktiming_bycond)[0]]\n")
        file_python.write("durations_insecs=np.array((300, 600, 1200, 1800))\n")
        file_python.write("durations_inTRs=np.round(durations_insecs/.72).astype(int)\n")
        file_python.write("for durnum in np.arange(4):\n")
        file_python.write("    print(str(durnum))\n")
        #file_python.write("    conds_other=np.arange(16)\n")
        #Including all tasks (even the one being predicted), since we want as much task data as possible
        file_python.write("    tasktiming=np.array(np.sum(tasktiming_bycond,axis=1)>0)\n")   
        #file_python.write("    tasktiming=np.array(np.sum(tasktiming_bycond[:,conds_other],axis=1)>0)\n")  
        file_python.write("    tasktiming_indices=np.array((np.where(tasktiming))).astype(int)\n")
        #Selecting task time points at random, to spread out influence of each task
        file_python.write("    tasktiming_indices=np.random.permutation(tasktiming_indices[0])\n")
        file_python.write("    tasktiming_mod=tasktiming.copy()\n")
        file_python.write("    tasktiming_mod[np.arange(np.shape(tasktiming_mod)[0])]=0\n")
        file_python.write("    tasktiming_mod[tasktiming_indices[np.arange(durations_inTRs[durnum])]]=1\n")
        file_python.write("    print(str(np.sum(tasktiming_mod)))\n")
        file_python.write("    taskFC_mreg_bysubj=actflow.connectivity_estimation.multregconn(taskdata[:,tasktiming_mod])\n")
        file_python.write("    #Save multiple-regression task FC data to file\n")
        file_python.write("    h5f = h5py.File('"+file_path+"','a')\n")
        file_python.write("    outname1 = 'taskFCRegression/' + 'condition' + str(durnum)\n")
        file_python.write("    try:\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bysubj)\n")
        file_python.write("    except:\n")
        file_python.write("        del h5f[outname1]\n")
        file_python.write("        h5f.create_dataset(outname1,data=taskFC_mreg_bysubj)\n")
        file_python.write("    h5f.close()")
        file_python.close()
                          
        
        file_slurm = open("batchscripts/slurmscript.sh", "w")
        file_slurm.write('#!/bin/bash\n')
        file_slurm.write('#SBATCH --partition=main\n')
        file_slurm.write('#SBATCH --job-name='+str(subj)+'\n')
        file_slurm.write('#SBATCH --requeue\n')
        file_slurm.write('#SBATCH --time=00:05:00\n')
        file_slurm.write('#SBATCH --nodes=1\n')
        file_slurm.write('#SBATCH --ntasks=1\n')
        file_slurm.write('#SBATCH --cpus-per-task=20\n')
        file_slurm.write('#SBATCH --mem=64000\n')
        file_slurm.write('#SBATCH --export=ALL\n')
        file_slurm.write("time python batchscripts/pythonscript_" + str(subj) + ".py\n")
        file_slurm.close()
        
        os.system("chmod 755 batchscripts/pythonscript_" + str(subj) + ".py")
        os.system("chmod 755 batchscripts/slurmscript.sh")
        os.system("sbatch batchscripts/slurmscript.sh")

    scount += 1
    
if job_sent_to_scheduler:
    print("***WAIT FOR SLURM JOBS TO FINISH, THEN RUN AGAIN TO LOAD DATA***")
In [124]:
#Visualize FC matrix
ax = sns.heatmap(np.mean(taskgenFC_mreg_bydurations_bysubj[netorder,:,3,:][:,netorder,:],axis=2),square=True,center=0,cmap='seismic',cbar=True,xticklabels=100,yticklabels=100, cbar_kws={'label': 'Regression coef. (beta)'})
ax.figure.suptitle('Multreg taskgen FC 30 mins')
ax.set(xlabel='Nodes',ylabel='Nodes')
Out[124]:
[Text(80.90000000000006, 0.5, 'Nodes'), Text(0.5, 12.5, 'Nodes')]
In [125]:
#Run actflow predictions, 24-condition task-general FC, 5 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 5 min multiple-regression FC==")
actflowOutput_taskgenFCMReg_5mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bydurations_bysubj[:,:,0,:], full_report=True)
==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 5 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.48, t-value vs. 0: 86.66, p-value vs. 0: 1.131147112771684e-145
 
Mean % variance explained (R^2 score, coeff. of determination) = -1.53
 
Mean MAE (mean absolute error) = 17.44
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.54, t-value vs. 0: 95.85, p-value vs. 0: 3.5916577858840024e-153
 
Mean % variance explained (R^2 score, coeff. of determination) = -4.73
 
Mean MAE (mean absolute error) = 17.44
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.47, t-value vs. 0: 95.42, p-value vs. 0: 7.818984442177331e-153
By task condition:
Condition 1: r=0.36, t-value vs. 0: 34.94, p-value vs. 0: 7.831189251498717e-81
Condition 2: r=0.31, t-value vs. 0: 32.80, p-value vs. 0: 1.1903502237232614e-76
Condition 3: r=0.57, t-value vs. 0: 46.89, p-value vs. 0: 5.130357278327955e-101
Condition 4: r=0.57, t-value vs. 0: 42.72, p-value vs. 0: 1.6692819897226582e-94
Condition 5: r=0.38, t-value vs. 0: 32.68, p-value vs. 0: 2.044657637503891e-76
Condition 6: r=0.37, t-value vs. 0: 32.32, p-value vs. 0: 1.0736444678404003e-75
Condition 7: r=0.47, t-value vs. 0: 41.22, p-value vs. 0: 5.061848500599122e-92
Condition 8: r=0.31, t-value vs. 0: 29.67, p-value vs. 0: 3.510184704445763e-70
Condition 9: r=0.32, t-value vs. 0: 33.49, p-value vs. 0: 5.128180799509423e-78
Condition 10: r=0.32, t-value vs. 0: 31.08, p-value vs. 0: 3.7434186096389454e-73
Condition 11: r=0.32, t-value vs. 0: 35.49, p-value vs. 0: 7.207403988727638e-82
Condition 12: r=0.32, t-value vs. 0: 30.35, p-value vs. 0: 1.219573528277323e-71
Condition 13: r=0.58, t-value vs. 0: 48.34, p-value vs. 0: 3.59906271804237e-103
Condition 14: r=0.57, t-value vs. 0: 46.54, p-value vs. 0: 1.7328442267471293e-100
Condition 15: r=0.56, t-value vs. 0: 46.28, p-value vs. 0: 4.23359574752164e-100
Condition 16: r=0.53, t-value vs. 0: 44.38, p-value vs. 0: 3.6735982503492317e-97
Condition 17: r=0.50, t-value vs. 0: 43.60, p-value vs. 0: 6.357038956192882e-96
Condition 18: r=0.46, t-value vs. 0: 42.57, p-value vs. 0: 2.95970027138332e-94
Condition 19: r=0.55, t-value vs. 0: 48.47, p-value vs. 0: 2.3510671459654924e-103
Condition 20: r=0.53, t-value vs. 0: 44.46, p-value vs. 0: 2.749137153978358e-97
Condition 21: r=0.59, t-value vs. 0: 54.43, p-value vs. 0: 1.2714716198650863e-111
Condition 22: r=0.52, t-value vs. 0: 44.38, p-value vs. 0: 3.697608983124512e-97
Condition 23: r=0.53, t-value vs. 0: 44.33, p-value vs. 0: 4.5131365834134957e-97
Condition 24: r=0.58, t-value vs. 0: 47.62, p-value vs. 0: 4.2356869031602154e-102
In [126]:
#Run actflow predictions, 24-condition task-general FC, 10 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 10 min multiple-regression FC==")
actflowOutput_taskgenFCMReg_10mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bydurations_bysubj[:,:,1,:], full_report=True)
==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 10 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.83, t-value vs. 0: 133.22, p-value vs. 0: 7.534690384357665e-178
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.65
 
Mean MAE (mean absolute error) = 6.61
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.84, t-value vs. 0: 147.84, p-value vs. 0: 1.0706296306197892e-185
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.23
 
Mean MAE (mean absolute error) = 6.61
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.81, t-value vs. 0: 125.92, p-value vs. 0: 1.2997433613189223e-173
By task condition:
Condition 1: r=0.69, t-value vs. 0: 58.91, p-value vs. 0: 2.5304125842171955e-117
Condition 2: r=0.65, t-value vs. 0: 53.44, p-value vs. 0: 2.6663417079315196e-110
Condition 3: r=0.87, t-value vs. 0: 83.71, p-value vs. 0: 4.211310987561444e-143
Condition 4: r=0.87, t-value vs. 0: 84.23, p-value vs. 0: 1.4605937642878594e-143
Condition 5: r=0.73, t-value vs. 0: 68.46, p-value vs. 0: 2.875594022598483e-128
Condition 6: r=0.73, t-value vs. 0: 71.60, p-value vs. 0: 1.4691763961045954e-131
Condition 7: r=0.79, t-value vs. 0: 66.89, p-value vs. 0: 1.4483277223065304e-126
Condition 8: r=0.66, t-value vs. 0: 57.33, p-value vs. 0: 2.329292458695409e-115
Condition 9: r=0.66, t-value vs. 0: 59.44, p-value vs. 0: 5.718210092129267e-118
Condition 10: r=0.64, t-value vs. 0: 57.32, p-value vs. 0: 2.439360633963064e-115
Condition 11: r=0.65, t-value vs. 0: 65.74, p-value vs. 0: 2.6442212301215477e-125
Condition 12: r=0.67, t-value vs. 0: 63.61, p-value vs. 0: 6.694612075862555e-123
Condition 13: r=0.88, t-value vs. 0: 84.50, p-value vs. 0: 8.443857006099459e-144
Condition 14: r=0.87, t-value vs. 0: 80.74, p-value vs. 0: 2.0328709374349609e-140
Condition 15: r=0.89, t-value vs. 0: 92.94, p-value vs. 0: 7.051365802128394e-151
Condition 16: r=0.88, t-value vs. 0: 86.06, p-value vs. 0: 3.717226029289413e-145
Condition 17: r=0.83, t-value vs. 0: 75.42, p-value vs. 0: 2.2003620822893864e-135
Condition 18: r=0.81, t-value vs. 0: 74.67, p-value vs. 0: 1.2050615408047858e-134
Condition 19: r=0.86, t-value vs. 0: 84.40, p-value vs. 0: 1.0385438031946835e-143
Condition 20: r=0.85, t-value vs. 0: 81.55, p-value vs. 0: 3.6841605238042394e-141
Condition 21: r=0.88, t-value vs. 0: 94.15, p-value vs. 0: 7.666859442500182e-152
Condition 22: r=0.84, t-value vs. 0: 79.43, p-value vs. 0: 3.2875305506971473e-139
Condition 23: r=0.85, t-value vs. 0: 85.57, p-value vs. 0: 9.879991718098916e-145
Condition 24: r=0.88, t-value vs. 0: 86.34, p-value vs. 0: 2.1576036686155515e-145
In [127]:
#Run actflow predictions, 24-condition task-general FC, 20 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 20 min multiple-regression FC==")
actflowOutput_taskgenFCMReg_20mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bydurations_bysubj[:,:,2,:], full_report=True)
==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 20 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.89, t-value vs. 0: 155.98, p-value vs. 0: 9.75346726426353e-190
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.79
 
Mean MAE (mean absolute error) = 5.10
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.89, t-value vs. 0: 167.53, p-value vs. 0: 3.9503052480891834e-195
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.57
 
Mean MAE (mean absolute error) = 5.10
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.88, t-value vs. 0: 145.38, p-value vs. 0: 1.983153313591189e-184
By task condition:
Condition 1: r=0.78, t-value vs. 0: 68.88, p-value vs. 0: 1.0227862559824782e-128
Condition 2: r=0.75, t-value vs. 0: 62.05, p-value vs. 0: 4.341180195627744e-121
Condition 3: r=0.93, t-value vs. 0: 97.26, p-value vs. 0: 2.9162119410294674e-154
Condition 4: r=0.93, t-value vs. 0: 96.68, p-value vs. 0: 8.108296999427758e-154
Condition 5: r=0.83, t-value vs. 0: 85.20, p-value vs. 0: 2.08613124448454e-144
Condition 6: r=0.82, t-value vs. 0: 85.64, p-value vs. 0: 8.705882359723607e-145
Condition 7: r=0.87, t-value vs. 0: 80.65, p-value vs. 0: 2.4515729680993724e-140
Condition 8: r=0.77, t-value vs. 0: 61.72, p-value vs. 0: 1.0591516223315984e-120
Condition 9: r=0.77, t-value vs. 0: 69.06, p-value vs. 0: 6.610906648167934e-129
Condition 10: r=0.75, t-value vs. 0: 68.73, p-value vs. 0: 1.4692424211369164e-128
Condition 11: r=0.76, t-value vs. 0: 69.56, p-value vs. 0: 1.9477265828243372e-129
Condition 12: r=0.77, t-value vs. 0: 69.91, p-value vs. 0: 8.295969797988503e-130
Condition 13: r=0.93, t-value vs. 0: 91.75, p-value vs. 0: 6.42775720976789e-150
Condition 14: r=0.93, t-value vs. 0: 91.72, p-value vs. 0: 6.890617870700084e-150
Condition 15: r=0.94, t-value vs. 0: 114.49, p-value vs. 0: 1.8275982546830202e-166
Condition 16: r=0.94, t-value vs. 0: 107.44, p-value vs. 0: 1.0572758947089255e-161
Condition 17: r=0.89, t-value vs. 0: 93.18, p-value vs. 0: 4.535288550560385e-151
Condition 18: r=0.88, t-value vs. 0: 85.63, p-value vs. 0: 8.871497907028385e-145
Condition 19: r=0.91, t-value vs. 0: 103.10, p-value vs. 0: 1.2741493463400713e-158
Condition 20: r=0.91, t-value vs. 0: 97.78, p-value vs. 0: 1.16007205275369e-154
Condition 21: r=0.93, t-value vs. 0: 112.30, p-value vs. 0: 5.088956819030177e-165
Condition 22: r=0.90, t-value vs. 0: 92.78, p-value vs. 0: 9.541281692683221e-151
Condition 23: r=0.91, t-value vs. 0: 97.29, p-value vs. 0: 2.7866772748658406e-154
Condition 24: r=0.92, t-value vs. 0: 105.18, p-value vs. 0: 4.0830274453967895e-160
In [128]:
#Run actflow predictions, 24-condition task-general FC, 30 min multiple-regression FC
print("==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 30 min multiple-regression FC==")
actflowOutput_taskgenFCMReg_30mins_bycond = actflow.actflowcomp.actflowtest(activations_bycond, taskgenFC_mreg_bydurations_bysubj[:,:,3,:], full_report=True)
==Activity flow mapping results, multiple-regression-based task-general FC, 24-conditions, 30 min multiple-regression FC==
===Comparing prediction accuracies between models (similarity between predicted and actual brain activation patterns)===
 
==Comparisons between predicted and actual activation patterns, across all conditions and nodes:==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each comparison based on 24 conditions across 360 nodes, p-values based on 176 subjects (cross-subject variance in comparisons)
 
Mean Pearson r = 0.91, t-value vs. 0: 162.25, p-value vs. 0: 1.0311219124198775e-192
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.83
 
Mean MAE (mean absolute error) = 4.69
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Condition-wise comparisons between predicted and actual activation patterns (calculated for each node separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N conditions: 24, p-values based on N subjects (cross-subject variance in correlations): 176
 
Mean Pearson r = 0.91, t-value vs. 0: 173.28, p-value vs. 0: 1.1052739244303553e-197
 
Mean % variance explained (R^2 score, coeff. of determination) = 0.64
 
Mean MAE (mean absolute error) = 4.69
 
Note: Pearson r and Pearson r^2 are scale-invariant, while R^2 and MAE are not. R^2 units: percentage of the to-be-predicted data's unscaled variance, ranging from negative infinity (because prediction errors can be arbitrarily large) to positive 1. See https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html for more info.
 
==Node-wise (spatial) correlations between predicted and actual activation patterns (calculated for each condition separetely):==
--Compare-then-average (calculating prediction accuracies before cross-subject averaging):
Each correlation based on N nodes: 360, p-values based on N subjects (cross-subject variance in correlations): 176
Cross-condition mean r=0.90, t-value vs. 0: 153.14, p-value vs. 0: 2.3730534766383105e-188
By task condition:
Condition 1: r=0.81, t-value vs. 0: 73.10, p-value vs. 0: 4.393190876204947e-133
Condition 2: r=0.78, t-value vs. 0: 67.64, p-value vs. 0: 2.2103664006823945e-127
Condition 3: r=0.94, t-value vs. 0: 103.34, p-value vs. 0: 8.531490128740453e-159
Condition 4: r=0.94, t-value vs. 0: 100.64, p-value vs. 0: 8.223349814565749e-157
Condition 5: r=0.86, t-value vs. 0: 89.63, p-value vs. 0: 3.540680415752806e-148
Condition 6: r=0.85, t-value vs. 0: 89.74, p-value vs. 0: 2.8779711874904908e-148
Condition 7: r=0.89, t-value vs. 0: 83.27, p-value vs. 0: 1.0440891630501604e-142
Condition 8: r=0.80, t-value vs. 0: 66.37, p-value vs. 0: 5.319067417666491e-126
Condition 9: r=0.80, t-value vs. 0: 72.52, p-value vs. 0: 1.7091965441384486e-132
Condition 10: r=0.79, t-value vs. 0: 75.41, p-value vs. 0: 2.2587646587017323e-135
Condition 11: r=0.79, t-value vs. 0: 76.49, p-value vs. 0: 1.98779089481787e-136
Condition 12: r=0.80, t-value vs. 0: 77.36, p-value vs. 0: 2.919299918014331e-137
Condition 13: r=0.95, t-value vs. 0: 96.21, p-value vs. 0: 1.8970935492594412e-153
Condition 14: r=0.94, t-value vs. 0: 95.59, p-value vs. 0: 5.661284188512653e-153
Condition 15: r=0.96, t-value vs. 0: 118.38, p-value vs. 0: 5.648828308875519e-169
Condition 16: r=0.95, t-value vs. 0: 116.48, p-value vs. 0: 9.245749884475273e-168
Condition 17: r=0.91, t-value vs. 0: 93.84, p-value vs. 0: 1.358403760526434e-151
Condition 18: r=0.90, t-value vs. 0: 88.88, p-value vs. 0: 1.4973881555467332e-147
Condition 19: r=0.93, t-value vs. 0: 106.74, p-value vs. 0: 3.251995779637936e-161
Condition 20: r=0.92, t-value vs. 0: 100.10, p-value vs. 0: 2.0519425366552315e-156
Condition 21: r=0.94, t-value vs. 0: 116.11, p-value vs. 0: 1.5964989102767034e-167
Condition 22: r=0.92, t-value vs. 0: 98.82, p-value vs. 0: 1.88821044997836e-155
Condition 23: r=0.92, t-value vs. 0: 103.36, p-value vs. 0: 8.280244297113096e-159
Condition 24: r=0.94, t-value vs. 0: 106.46, p-value vs. 0: 5.079314336582753e-161

Visualize results

In [129]:
#Vizualize relative prediction accuracies, as a factor of time, r-values
import pandas as pd
#5, 10, 20, 30 mins; rest vs. task FC
#colnames=np.array(('GroupRest','GroupTask','IndivRest','IndivTask','IndivTaskRun'))
colnames=np.array(('Pearsonr','FCtype','Time','Timevals'))
corr_array_byactflowtype=np.concatenate((actflowOutput_restFCMReg_5mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_10mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_20mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_30mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_5mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_10mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_20mins_bycond['model_compare_output']['corr_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_30mins_bycond['model_compare_output']['corr_fullcomp_compthenavg']))
analysistype_array=np.concatenate((np.repeat('Rest FC',4*nSubjs), np.repeat('Task FC',4*nSubjs)))
analysistype_series=pd.Series(analysistype_array, dtype="category")
amounttime_array=np.concatenate((np.repeat('5 min',nSubjs), np.repeat('10 min',nSubjs), np.repeat('20 min',nSubjs),
                                np.repeat('30 min',nSubjs), np.repeat('5 min',nSubjs), np.repeat('10 min',nSubjs),
                                np.repeat('20 min',nSubjs), np.repeat('30 min',nSubjs)))
amounttime_series=pd.Series(amounttime_array, dtype="category")
amounttimevals_array=np.concatenate((np.repeat(5,nSubjs), np.repeat(10,nSubjs), np.repeat(20,nSubjs),
                                np.repeat(30,nSubjs), np.repeat(5,nSubjs), np.repeat(10,nSubjs),
                                np.repeat(20,nSubjs), np.repeat(30,nSubjs)))
amounttimevals_series=pd.Series(amounttimevals_array, dtype="category")
subjnums_array=np.concatenate((np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),
                              np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs)))
df_actflowpreds = pd.DataFrame(np.vstack((corr_array_byactflowtype,analysistype_series,amounttime_series,amounttimevals_series)).T, index=subjnums_array, columns=colnames)
df_actflowpreds['FCtype']=df_actflowpreds['FCtype'].astype('category')
df_actflowpreds['Time']=df_actflowpreds['Time'].astype('category')
df_actflowpreds['Pearsonr']=df_actflowpreds['Pearsonr'].astype('float')
df_actflowpreds=df_actflowpreds.sort_values('Timevals')
df_actflowpreds_rvals=df_actflowpreds.copy()
fig = plt.figure(figsize=[8,4])
ax = sns.boxplot(y='Pearsonr',x='Timevals',data=df_actflowpreds,hue='FCtype')
#ax = sns.catplot(y='Pearson r',x='Timevals',data=df_actflowpreds,hue='FC type',kind="point",capsize=.2)
fig.savefig(figuredir+'amounttimeFC_PearsonR_MultregFC_discovery176.pdf')
In [130]:
#Vizualize relative prediction accuracies, as a factor of time, r-values
import pandas as pd
#5, 10, 20, 30 mins; rest vs. task FC
#colnames=np.array(('GroupRest','GroupTask','IndivRest','IndivTask','IndivTaskRun'))
colnames=np.array(('R^2','FC type','Time','Timevals'))
corr_array_byactflowtype=np.concatenate((actflowOutput_restFCMReg_5mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_10mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_20mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_30mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_5mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_10mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_20mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_30mins_bycond['model_compare_output']['R2_fullcomp_compthenavg']))
analysistype_array=np.concatenate((np.repeat('Rest FC',4*nSubjs), np.repeat('Task FC',4*nSubjs)))
analysistype_series=pd.Series(analysistype_array, dtype="category")
amounttime_array=np.concatenate((np.repeat('5 min',nSubjs), np.repeat('10 min',nSubjs), np.repeat('20 min',nSubjs),
                                np.repeat('30 min',nSubjs), np.repeat('5 min',nSubjs), np.repeat('10 min',nSubjs),
                                np.repeat('20 min',nSubjs), np.repeat('30 min',nSubjs)))
amounttime_series=pd.Series(amounttime_array, dtype="category")
amounttimevals_array=np.concatenate((np.repeat(5,nSubjs), np.repeat(10,nSubjs), np.repeat(20,nSubjs),
                                np.repeat(30,nSubjs), np.repeat(5,nSubjs), np.repeat(10,nSubjs),
                                np.repeat(20,nSubjs), np.repeat(30,nSubjs)))
amounttimevals_series=pd.Series(amounttimevals_array, dtype="category")
subjnums_array=np.concatenate((np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),
                              np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs)))
df_actflowpreds = pd.DataFrame(np.vstack((corr_array_byactflowtype,analysistype_series,amounttime_series,amounttimevals_series)).T, index=subjnums_array, columns=colnames)
df_actflowpreds['FC type']=df_actflowpreds['FC type'].astype('category')
df_actflowpreds['Time']=df_actflowpreds['Time'].astype('category')
df_actflowpreds['R^2']=df_actflowpreds['R^2'].astype('float')
df_actflowpreds=df_actflowpreds.sort_values('Timevals')
fig = plt.figure(figsize=[8,4])
ax = sns.boxplot(y='R^2',x='Timevals',data=df_actflowpreds,hue='FC type')
ax.set(ylim=(np.min(np.min(df_actflowpreds['R^2']))-.25, 1))
#ax = sns.catplot(y='Pearson r',x='Timevals',data=df_actflowpreds,hue='FC type',kind="point",capsize=.2)
fig.savefig(figuredir+'amounttimeFC_Rsq_MultregFC_discovery176.pdf')
In [131]:
#Vizualize relative prediction accuracies, as a factor of time, r-values
import pandas as pd
#5, 10, 20, 30 mins; rest vs. task FC
#colnames=np.array(('GroupRest','GroupTask','IndivRest','IndivTask','IndivTaskRun'))
colnames=np.array(('R^2','FC type','Time','Timevals'))
corr_array_byactflowtype=np.concatenate((actflowOutput_restFCMReg_10mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_20mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_restFCMReg_30mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_10mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_20mins_bycond['model_compare_output']['R2_fullcomp_compthenavg'],
                                  actflowOutput_taskgenFCMReg_30mins_bycond['model_compare_output']['R2_fullcomp_compthenavg']))
analysistype_array=np.concatenate((np.repeat('Rest FC',3*nSubjs), np.repeat('Task FC',3*nSubjs)))
analysistype_series=pd.Series(analysistype_array, dtype="category")
amounttime_array=np.concatenate((np.repeat('10 min',nSubjs), np.repeat('20 min',nSubjs),
                                np.repeat('30 min',nSubjs), np.repeat('10 min',nSubjs),
                                np.repeat('20 min',nSubjs), np.repeat('30 min',nSubjs)))
amounttime_series=pd.Series(amounttime_array, dtype="category")
amounttimevals_array=np.concatenate((np.repeat(10,nSubjs), np.repeat(20,nSubjs),
                                np.repeat(30,nSubjs), np.repeat(10,nSubjs),
                                np.repeat(20,nSubjs), np.repeat(30,nSubjs)))
amounttimevals_series=pd.Series(amounttimevals_array, dtype="category")
subjnums_array=np.concatenate((np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs),
                              np.arange(nSubjs),np.arange(nSubjs),np.arange(nSubjs)))
df_actflowpreds = pd.DataFrame(np.vstack((corr_array_byactflowtype,analysistype_series,amounttime_series,amounttimevals_series)).T, index=subjnums_array, columns=colnames)
df_actflowpreds['FC type']=df_actflowpreds['FC type'].astype('category')
df_actflowpreds['Time']=df_actflowpreds['Time'].astype('category')
df_actflowpreds['R^2']=df_actflowpreds['R^2'].astype('float')
df_actflowpreds=df_actflowpreds.sort_values('Timevals')
fig = plt.figure(figsize=[8,4])
ax = sns.boxplot(y='R^2',x='Timevals',data=df_actflowpreds,hue='FC type')
#ax = sns.catplot(y='Pearson r',x='Timevals',data=df_actflowpreds,hue='FC type',kind="point",capsize=.2)
fig.savefig(figuredir+'amounttimeFC_Rsq_no5min_MultregFC_discovery176.pdf')

Comparing conditions

In [132]:
#ANOVA
import statsmodels.api as sm
from statsmodels.formula.api import ols

df_actflowpreds_rvals_lm=ols('Pearsonr ~ C(Timevals) + C(FCtype) + C(Timevals)*C(FCtype)', data=df_actflowpreds_rvals).fit() 
print(sm.stats.anova_lm(df_actflowpreds_rvals_lm, typ=2))
                          sum_sq      df            F         PR(>F)
C(Timevals)            48.634868     3.0  9020.632485   0.000000e+00
C(FCtype)               2.603230     1.0  1448.514991  3.404206e-218
C(Timevals):C(FCtype)   0.248911     3.0    46.167196   1.890488e-28
Residual                2.516040  1400.0          NaN            NaN
In [133]:
#Task vs. rest T-tests
print('Task vs. rest FC actflow predictions')
for timev in np.array((5,10,20,30)):
    print('Time:', str(timev), 'min')
    restFC5Min=df_actflowpreds_rvals[(df_actflowpreds_rvals['Timevals']==timev) & (df_actflowpreds_rvals['FCtype']=='Rest FC')]
    taskFC5Min=df_actflowpreds_rvals[(df_actflowpreds_rvals['Timevals']==timev) & (df_actflowpreds_rvals['FCtype']=='Task FC')]

    ttest_vals=stats.ttest_1samp(taskFC5Min['Pearsonr']-restFC5Min['Pearsonr'],0.0)
    print(ttest_vals)
Task vs. rest FC actflow predictions
Time: 5 min
Ttest_1sampResult(statistic=23.743499711544768, pvalue=1.2878092306304326e-56)
Time: 10 min
Ttest_1sampResult(statistic=48.797111589553325, pvalue=7.887233721889665e-104)
Time: 20 min
Ttest_1sampResult(statistic=45.179913414207626, pvalue=2.0993105578507104e-98)
Time: 30 min
Ttest_1sampResult(statistic=46.19464224985543, pvalue=5.805713208576939e-100)