From 7dc00592a0464ff5f81efefa85b6d3439a8bfc24 Mon Sep 17 00:00:00 2001 From: Alexandre Pron Date: Fri, 13 Dec 2024 17:17:01 +0100 Subject: [PATCH] add narps open exporter to save group level results into BIDS specific format --- narps_open/utils/export/__init__.py | 0 narps_open/utils/export/__main__.py | 82 +++++++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 83 insertions(+) create mode 100644 narps_open/utils/export/__init__.py create mode 100644 narps_open/utils/export/__main__.py diff --git a/narps_open/utils/export/__init__.py b/narps_open/utils/export/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/narps_open/utils/export/__main__.py b/narps_open/utils/export/__main__.py new file mode 100644 index 00000000..e5c287b7 --- /dev/null +++ b/narps_open/utils/export/__main__.py @@ -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 + # 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() diff --git a/setup.py b/setup.py index e3c65bb0..641db9fe 100644 --- a/setup.py +++ b/setup.py @@ -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',