Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
2.3 kB
2
Indexable
Never
# Boldizer 1.0 with Multithreading support
# A python script to extract BOLD-related data at participant level
# 
# (C) 2024 Giovanni Federico, PhD
# research@giovannifederico.net
# www.giovannifederico.net

import asyncio
import os

# SETUP
FeatQueryBinWithPath = "/Users/neurogiovanni/fsl/bin/featquery"
FeatQueryOptions = "-p -s -b"
RootDir = "/Volumes/Samsung-SSD/lyon-exp-2-transactive-memory-1/fmri-second-level-z24-p001.gfeat/"
OutputDir = "/Volumes/Samsung-SSD/lyon-exp-2-transactive-memory-1/boldizer/"
if not os.path.exists(OutputDir):
    os.makedirs(OutputDir)
ROIsDirectory = "/Volumes/Samsung-SSD/ad-hoc-rois/"
Subjects = 27
TypeOfData = "pe"  # or 'cope' for contrasts of beta weights

string_subs_pe = " ".join([f"stats/{TypeOfData}{s}" for s in range(1, Subjects + 1)])

COPE_Condition = {
    "cope1.feat": "alone",
    "cope2.feat": "camera",
    "cope3.feat": "expert-deleg",
    "cope4.feat": "expert-help",
    "cope6.feat": "tutorial",
}

async def run_command(command):
    """Asynchronously run command"""
    process = await asyncio.create_subprocess_exec(
        *command,
        stdout=asyncio.subprocess.PIPE,
        stderr=asyncio.subprocess.PIPE)
    stdout, stderr = await process.communicate()
    if process.returncode == 0:
        print(f"Success: {' '.join(command)}")
    else:
        print(f"Error: {' '.join(command)}\n{stderr.decode()}")

async def main():
    commands = []
    for roi in os.listdir(ROIsDirectory):
        roi_path = os.path.join(ROIsDirectory, roi)
        if os.path.isfile(roi_path):
            for cope, condition in COPE_Condition.items():
                command = [
                    FeatQueryBinWithPath,
                    "1",
                    os.path.join(RootDir, cope),
                    str(Subjects),
                    string_subs_pe,
                    f"boldizer-{condition}-{roi[:-4]}",
                    FeatQueryOptions,
                    roi_path
                ]
                commands.append(command)

    # Limit to 4 concurrent subprocesses
    sem = asyncio.Semaphore(5)

    async def run_limited(command):
        async with sem:
            await run_command(command)

    await asyncio.gather(*(run_limited(command) for command in commands))

# Python 3.7+
if __name__ == "__main__":
    asyncio.run(main())
Leave a Comment