Skip to content

Commit

Permalink
add narps open exporter to save group level results into BIDS specifi…
Browse files Browse the repository at this point in the history
…c format
  • Loading branch information
alexpron committed Dec 13, 2024
1 parent cd16a82 commit 7dc0059
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
Empty file.
82 changes: 82 additions & 0 deletions narps_open/utils/export/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/python
# coding: utf-8

""" A command line tool for the narps_open.utils.export module """

import os
from os.path import join, exists, basename
import shutil
from argparse import ArgumentParser

from narps_open.utils.configuration import Configuration
from narps_open.pipelines import get_implemented_pipelines
from narps_open.runner import PipelineRunner


# Derived Class Cleaner or only add method
class PipelineExporter(PipelineRunner):
def get_export_filename(i):
"""Get NARPS compliant filename based on the position in the fixed list files
"""
hypothesis = str(i // 2 + 1) # 2 files per hypothesis starting at 1
suffix = "" if i%2 == 0 else "un" #even thresholded odd unthresholded
export_filename = f"hypo_{hypothesis}_{suffix}thresh.nii.gz"
return export_filename

def get_export_filename(i):
"""Get NARPS compliant filename based on the position in the fixed list files
"""
hypothesis = str(i // 2 + 1) # 2 files per hypothesis starting at 1
suffix = "" if i % 2 == 0 else "un" # even thresholded odd unthresholded
export_filename = f"hypo_{hypothesis}_{suffix}thresh.nii.gz"
return export_filename


def main():
""" Entry-point for the command line tool narps_open_export """

# Parse arguments
parser = ArgumentParser(description = 'Export group level NARPS reproduced results into BIDS format')
parser.add_argument('-t', '--team', type = str, required = True,
help = 'the team ID', choices = get_implemented_pipelines())
parser.add_argument('-n', '--nsubjects', type = int, required = True,
help='the number of subjects to be selected')
arguments = parser.parse_args()

# Initialize pipeline
runner = PipelineRunner(arguments.team)
runner.pipeline.directories.dataset_dir = Configuration()['directories']['dataset']
runner.pipeline.directories.results_dir = Configuration()['directories']['reproduced_results']
runner.pipeline.directories.set_output_dir_with_team_id(arguments.team)
runner.pipeline.directories.set_working_dir_with_team_id(arguments.team)
# should I use export files with team_id
runner.nb_subjects = arguments.nsubjects


# Retrieve the paths to the reproduced files
reproduced_files = runner.pipeline.get_hypotheses_outputs()
# Not statisfactory !
# TODO: a group of minimun XXXXX subjects can be exported

Check failure on line 61 in narps_open/utils/export/__main__.py

View workflow job for this annotation

GitHub Actions / Check for spelling errors

minimun ==> minimum
# TODO have a hash based on the exact subjects list
# TODO: full dataset should be the default case

export_dir = join(runner.pipeline.directories.results_dir, "group-level", runner.pipeline.team_id, f"nbsub-{str(len(runner.nb_subjects))}")
if not exists(export_dir):
os.makedirs(export_dir)
for i, file in enumerate(reproduced_files):
export_file = join(export_dir, get_export_filename(i))
# TODO use datalad instead (installation will be messier)
shutil.copy2(file, export_file)









if __name__ == '__main__':
main()
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
entry_points = {
'console_scripts': [
'narps_open_runner = narps_open.runner:main',
'narps_open_exporter = narps_open.utils.export.__main__:main',
'narps_open_tester = narps_open.tester:main',
'narps_open_status = narps_open.utils.status:main',
'narps_open_correlations = narps_open.utils.correlation.__main__:main',
Expand Down

0 comments on commit 7dc0059

Please sign in to comment.