Skip to content

Commit 8f399de

Browse files
bendhousearteffigies
authored andcommitted
that was ruff
1 parent 2320469 commit 8f399de

File tree

1 file changed

+47
-26
lines changed

1 file changed

+47
-26
lines changed

scripts/collect_test_data.py

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
from datalad import api
2-
from tempfile import TemporaryDirectory
3-
from pathlib import Path
4-
from os.path import join
1+
# /// script
2+
# requires-python = ">=3.11"
3+
# dependencies = [
4+
# "datalad",
5+
# ]
6+
# ///
7+
8+
import argparse
9+
import json
10+
import os
511
import shutil
6-
import subprocess
12+
import sys
13+
from pathlib import Path
14+
from tempfile import TemporaryDirectory
15+
from typing import Union
16+
717
import bids
818
import pandas as pd
9-
import sys
10-
import json
11-
import argparse
12-
import os
19+
from datalad import api
1320

1421
readme_template = """# PETPrep Test Data Collection
1522
1623
## Overview
1724
18-
This dataset contains a curated collection of PET imaging data from multiple OpenNeuro datasets, compiled for testing and development of the PETPrep software pipeline. The data has been selected to provide a diverse range of PET imaging scenarios for comprehensive software testing.
25+
This dataset contains a curated collection of PET imaging data from multiple
26+
OpenNeuro datasets,compiled for testing and development of the PETPrep software pipeline.
27+
The data has been selected to provide a diverse range of PET imaging scenarios for comprehensive
28+
software testing.
1929
2030
## Dataset Information
2131
@@ -72,7 +82,8 @@
7282
7383
---
7484
75-
*This is a test dataset compiled for software development purposes. Please refer to the original datasets for research use.*
85+
*This is a test dataset compiled for software development purposes. Please refer to the original
86+
datasets for research use.*
7687
"""
7788

7889

@@ -90,7 +101,8 @@ def create_dataset_description():
90101
'This test data collection was created for PETPrep development and testing purposes'
91102
],
92103
'EthicsApprovals': [
93-
'This is a test dataset compiled from publicly available BIDS datasets for software testing purposes'
104+
'This is a test dataset compiled from publicly available BIDS datasets for software',
105+
'testing purposes'
94106
],
95107
'ReferencesAndLinks': [
96108
'https://github.com/nipreps/petprep',
@@ -139,22 +151,27 @@ def create_readme_content(pet_datasets, readme_template):
139151

140152

141153
def download_test_data(
142-
working_directory=TemporaryDirectory(),
143-
output_directory=os.getcwd(),
154+
working_directory: Union(TemporaryDirectory, None)=None,
155+
output_directory: Union(Path, str)='',
144156
pet_datasets_json=None, # Default to None, not the dict
145157
):
146158
# Use default datasets if no JSON file provided
147159
if pet_datasets_json is None:
148160
datasets_to_use = pet_datasets # Use the default defined at module level
149161
else:
150162
# Load from JSON file
151-
with open(pet_datasets_json, 'r') as infile:
163+
with open(pet_datasets_json) as infile:
152164
datasets_to_use = json.load(infile)
153165

166+
if not working_directory:
167+
working_directory = TemporaryDirectory()
168+
169+
if not output_directory:
170+
output_directory = os.getcwd()
171+
154172
with working_directory as data_path:
155173
combined_participants_tsv = pd.DataFrame()
156174
combined_subjects = []
157-
combined_dataset_files = []
158175
for (
159176
dataset_id,
160177
meta,
@@ -188,13 +205,14 @@ def download_test_data(
188205
)
189206
# if a subset of subjects are specified collect only those subjects in the install
190207
if meta.get('subject_ids', []) != []:
191-
for id in meta['subject_ids']:
192-
combined_subjects.append(id)
208+
for _id in meta['subject_ids']:
209+
combined_subjects.append(_id)
193210
# Get the entire subject directory content including git-annex files
194-
subject_dir = dataset_path / f'sub-{id}'
211+
subject_dir = dataset_path / f'sub-{_id}'
195212
if subject_dir.exists():
196-
# First, get all content in the subject directory (this retrieves git-annex files)
197-
result = dataset.get(str(subject_dir))
213+
# First, get all content in the subject directory
214+
# (this retrieves git-annex files)
215+
dataset.get(str(subject_dir))
198216

199217
# Then collect all files after they've been retrieved
200218
all_files = []
@@ -245,29 +263,32 @@ def download_test_data(
245263
if __name__ == '__main__':
246264
parser = argparse.ArgumentParser(
247265
prog='PETPrepTestDataCollector',
248-
description='Collects PET datasets from OpenNeuro.org and combines them into a single BIDS dataset using datalad and pandas',
266+
description='Collects PET datasets from OpenNeuro.org and'
267+
'combines them into a single BIDS dataset using datalad and pandas',
249268
formatter_class=argparse.RawTextHelpFormatter,
250269
)
251270
parser.add_argument(
252271
'--working-directory',
253272
'-w',
254273
type=str,
255274
default=TemporaryDirectory(),
256-
help='Working directory for downloading and combining datasets, defaults to a temporary directory.',
275+
help='Working directory for downloading and combining datasets,'
276+
'defaults to a temporary directory.',
257277
)
258278
parser.add_argument(
259279
'--output-directory',
260280
'-o',
261281
type=str,
262282
default=os.getcwd(),
263-
help=f'Output directory of combined dataset, defaults where this script is called from, presently {os.getcwd()}',
283+
help='Output directory of combined dataset,'
284+
'defaults where this script is called from, presently {os.getcwd()}',
264285
)
265286
parser.add_argument(
266287
'--datasets-json',
267288
'-j',
268289
type=str,
269290
default=None,
270-
help="""Use a custom json of datasets along
291+
help="""Use a custom json of datasets along
271292
a subset of subjects can also be specified.
272293
The default is structured like the following:
273294
@@ -279,7 +300,7 @@ def download_test_data(
279300
},
280301
"ds004868": {
281302
"version": "1.0.4",
282-
"description": "[description]",
303+
"description": "[description]",
283304
"subject_ids": ["PSBB01"]
284305
},
285306
"ds004869": {

0 commit comments

Comments
 (0)