2
2
from __future__ import annotations
3
3
4
4
import subprocess
5
- import sys
6
5
from pathlib import Path
7
6
from typing import Any
8
7
9
8
from rich import print
10
9
11
10
from .matlab import matlab
12
- from .parsers import bidspm_log , common_parser
11
+ from .parsers import bidspm_log
13
12
14
13
log = bidspm_log (name = "bidspm" )
15
14
16
- new_line = ", ...\n \t "
15
+ new_line = ", ...\n \t "
17
16
18
17
19
18
def base_cmd (bids_dir : Path , output_dir : Path ) -> str :
20
- cmd = " bidspm('init');"
21
- cmd += f" bidspm( '{ bids_dir } '{ new_line } '{ output_dir } '"
19
+ cmd = f" bidspm( '{ bids_dir } '{ new_line } '{ output_dir } '"
22
20
return cmd
23
21
24
22
@@ -28,7 +26,7 @@ def append_main_cmd(cmd: str, analysis_level: str, action: str) -> str:
28
26
29
27
30
28
def end_cmd (cmd : str ) -> str :
31
- cmd += "); exit;"
29
+ cmd += "); exit;"
32
30
return cmd
33
31
34
32
@@ -99,10 +97,6 @@ def default_model(
99
97
ignore : list [str ] | None = None ,
100
98
options : Path | None = None ,
101
99
) -> int | str :
102
- if space and len (space ) > 1 :
103
- log .error (f"Only one space allowed for statistical analysis. Got\n :{ space } " )
104
- return 1
105
-
106
100
if task is None :
107
101
task = "{''}"
108
102
@@ -141,10 +135,6 @@ def preprocess(
141
135
dry_run : bool = False ,
142
136
options : Path | None = None ,
143
137
) -> int | str :
144
- if action == "preprocess" and task and len (task ) > 1 :
145
- log .error (f"Only one task allowed for preprocessing. Got\n :{ task } " )
146
- return 1
147
-
148
138
cmd = generate_cmd (
149
139
bids_dir = bids_dir ,
150
140
output_dir = output_dir ,
@@ -229,8 +219,8 @@ def stats(
229
219
output_dir : Path ,
230
220
analysis_level : str ,
231
221
action : str ,
232
- preproc_dir : Path ,
233
- model_file : Path ,
222
+ preproc_dir : Path | None ,
223
+ model_file : Path | None ,
234
224
verbosity : int = 2 ,
235
225
participant_label : list [str ] | None = None ,
236
226
fwhm : Any = 6 ,
@@ -246,10 +236,6 @@ def stats(
246
236
keep_residuals : bool = False ,
247
237
options : Path | None = None ,
248
238
) -> int | str :
249
- if space and len (space ) > 1 :
250
- log .error (f"Only one space allowed for statistical analysis. Got\n :{ space } " )
251
- return 1
252
-
253
239
cmd = generate_cmd (
254
240
bids_dir = bids_dir ,
255
241
output_dir = output_dir ,
@@ -281,7 +267,7 @@ def stats(
281
267
cmd += f"{ new_line } 'keep_residuals', true"
282
268
cmd = end_cmd (cmd )
283
269
284
- log .info ("Running statistics ." )
270
+ log .info (f "Running { action } ." )
285
271
286
272
return cmd
287
273
@@ -310,63 +296,6 @@ def generate_cmd(
310
296
return cmd
311
297
312
298
313
- def cli (argv : Any = sys .argv ) -> None :
314
- parser = common_parser ()
315
-
316
- args , _ = parser .parse_known_args (argv [1 :])
317
-
318
- bids_dir = Path (args .bids_dir [0 ]).absolute ()
319
- output_dir = Path (args .output_dir [0 ]).absolute ()
320
- analysis_level = args .analysis_level [0 ]
321
- action = args .action [0 ]
322
- roi_atlas = args .roi_atlas [0 ]
323
- bids_filter_file = (
324
- Path (args .bids_filter_file [0 ]).absolute ()
325
- if args .bids_filter_file is not None
326
- else None
327
- )
328
- preproc_dir = (
329
- Path (args .preproc_dir [0 ]).absolute () if args .preproc_dir is not None else None
330
- )
331
- model_file = (
332
- Path (args .model_file [0 ]).absolute () if args .model_file is not None else None
333
- )
334
- options = Path (args .options ).absolute () if args .options is not None else None
335
-
336
- return_code = bidspm (
337
- bids_dir ,
338
- output_dir ,
339
- analysis_level ,
340
- action = action ,
341
- participant_label = args .participant_label ,
342
- verbosity = args .verbosity ,
343
- task = args .task ,
344
- space = args .space ,
345
- ignore = args .ignore ,
346
- fwhm = args .fwhm ,
347
- bids_filter_file = bids_filter_file ,
348
- dummy_scans = args .dummy_scans ,
349
- anat_only = args .anat_only ,
350
- skip_validation = args .skip_validation ,
351
- dry_run = args .dry_run ,
352
- preproc_dir = preproc_dir ,
353
- model_file = model_file ,
354
- roi_based = args .roi_based ,
355
- roi_atlas = roi_atlas ,
356
- roi_name = args .roi_name ,
357
- roi_dir = args .roi_dir ,
358
- concatenate = args .concatenate ,
359
- design_only = args .design_only ,
360
- keep_residuals = args .keep_residuals ,
361
- options = options ,
362
- )
363
-
364
- if return_code == 1 :
365
- sys .exit (1 )
366
- else :
367
- sys .exit (0 )
368
-
369
-
370
299
def bidspm (
371
300
bids_dir : Path ,
372
301
output_dir : Path ,
@@ -394,13 +323,6 @@ def bidspm(
394
323
keep_residuals : bool = False ,
395
324
options : Path | None = None ,
396
325
) -> int :
397
- if not bids_dir .is_dir ():
398
- log .error (f"The 'bids_dir' does not exist:\n \t { bids_dir } " )
399
- return 1
400
-
401
- if preproc_dir is not None and not preproc_dir .is_dir ():
402
- log .error (f"The 'preproc_dir' does not exist:\n \t { preproc_dir } " )
403
- return 1
404
326
405
327
if action == "default_model" :
406
328
cmd = default_model (
@@ -448,14 +370,6 @@ def bidspm(
448
370
)
449
371
450
372
elif action in {"stats" , "contrasts" , "results" }:
451
- if preproc_dir is None or not preproc_dir .exists ():
452
- log .error (f"'preproc_dir' must be specified for stats. Got:\n { preproc_dir } " )
453
- return 1
454
-
455
- if model_file is None or not model_file .exists ():
456
- log .error (f"'model_file' must be specified for stats. Got:\n { model_file } " )
457
- return 1
458
-
459
373
cmd = stats (
460
374
bids_dir = bids_dir ,
461
375
output_dir = output_dir ,
@@ -477,11 +391,13 @@ def bidspm(
477
391
keep_residuals = keep_residuals ,
478
392
options = options ,
479
393
)
480
- else :
481
- log .error (f"\n unknown action: { action } " )
482
- return 1
483
394
484
- return cmd if isinstance (cmd , int ) else run_command (cmd )
395
+ if isinstance (cmd , int ):
396
+ return cmd
397
+
398
+ cmd = f" bidspm('init'); try; { cmd } catch; exit; end;"
399
+
400
+ return run_command (cmd )
485
401
486
402
487
403
def run_command (cmd : str , platform : str | None = None ) -> int :
0 commit comments