rapidtide 3.0.11__py3-none-any.whl → 3.1.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rapidtide/Colortables.py +492 -27
- rapidtide/OrthoImageItem.py +1049 -46
- rapidtide/RapidtideDataset.py +1533 -86
- rapidtide/_version.py +3 -3
- rapidtide/calccoherence.py +196 -29
- rapidtide/calcnullsimfunc.py +188 -40
- rapidtide/calcsimfunc.py +242 -42
- rapidtide/correlate.py +1203 -383
- rapidtide/data/examples/src/testLD +56 -0
- rapidtide/data/examples/src/testalign +1 -1
- rapidtide/data/examples/src/testdelayvar +0 -1
- rapidtide/data/examples/src/testfmri +53 -3
- rapidtide/data/examples/src/testglmfilt +5 -5
- rapidtide/data/examples/src/testhappy +29 -7
- rapidtide/data/examples/src/testppgproc +17 -0
- rapidtide/data/examples/src/testrolloff +11 -0
- rapidtide/data/models/model_cnn_pytorch/best_model.pth +0 -0
- rapidtide/data/models/model_cnn_pytorch/loss.png +0 -0
- rapidtide/data/models/model_cnn_pytorch/loss.txt +1 -0
- rapidtide/data/models/model_cnn_pytorch/model.pth +0 -0
- rapidtide/data/models/model_cnn_pytorch/model_meta.json +68 -0
- rapidtide/decorators.py +91 -0
- rapidtide/dlfilter.py +2226 -110
- rapidtide/dlfiltertorch.py +4842 -0
- rapidtide/externaltools.py +327 -12
- rapidtide/fMRIData_class.py +79 -40
- rapidtide/filter.py +1899 -810
- rapidtide/fit.py +2011 -581
- rapidtide/genericmultiproc.py +93 -18
- rapidtide/happy_supportfuncs.py +2047 -172
- rapidtide/helper_classes.py +584 -43
- rapidtide/io.py +2370 -372
- rapidtide/linfitfiltpass.py +346 -99
- rapidtide/makelaggedtcs.py +210 -24
- rapidtide/maskutil.py +448 -62
- rapidtide/miscmath.py +827 -121
- rapidtide/multiproc.py +210 -22
- rapidtide/patchmatch.py +242 -42
- rapidtide/peakeval.py +31 -31
- rapidtide/ppgproc.py +2203 -0
- rapidtide/qualitycheck.py +352 -39
- rapidtide/refinedelay.py +431 -57
- rapidtide/refineregressor.py +494 -189
- rapidtide/resample.py +671 -185
- rapidtide/scripts/applyppgproc.py +28 -0
- rapidtide/scripts/showxcorr_legacy.py +7 -7
- rapidtide/scripts/stupidramtricks.py +15 -17
- rapidtide/simFuncClasses.py +1052 -77
- rapidtide/simfuncfit.py +269 -69
- rapidtide/stats.py +540 -238
- rapidtide/tests/happycomp +9 -0
- rapidtide/tests/test_cleanregressor.py +1 -2
- rapidtide/tests/test_dlfiltertorch.py +627 -0
- rapidtide/tests/test_findmaxlag.py +24 -8
- rapidtide/tests/test_fullrunhappy_v1.py +0 -2
- rapidtide/tests/test_fullrunhappy_v2.py +0 -2
- rapidtide/tests/test_fullrunhappy_v3.py +11 -4
- rapidtide/tests/test_fullrunhappy_v4.py +10 -2
- rapidtide/tests/test_fullrunrapidtide_v7.py +1 -1
- rapidtide/tests/test_getparsers.py +11 -3
- rapidtide/tests/test_refinedelay.py +0 -1
- rapidtide/tests/test_simroundtrip.py +16 -8
- rapidtide/tests/test_stcorrelate.py +3 -1
- rapidtide/tests/utils.py +9 -8
- rapidtide/tidepoolTemplate.py +142 -38
- rapidtide/tidepoolTemplate_alt.py +165 -44
- rapidtide/tidepoolTemplate_big.py +189 -52
- rapidtide/util.py +1217 -118
- rapidtide/voxelData.py +684 -37
- rapidtide/wiener.py +136 -23
- rapidtide/wiener2.py +113 -7
- rapidtide/workflows/adjustoffset.py +105 -3
- rapidtide/workflows/aligntcs.py +85 -2
- rapidtide/workflows/applydlfilter.py +87 -10
- rapidtide/workflows/applyppgproc.py +540 -0
- rapidtide/workflows/atlasaverage.py +210 -47
- rapidtide/workflows/atlastool.py +100 -3
- rapidtide/workflows/calcSimFuncMap.py +288 -69
- rapidtide/workflows/calctexticc.py +201 -9
- rapidtide/workflows/ccorrica.py +101 -6
- rapidtide/workflows/cleanregressor.py +165 -31
- rapidtide/workflows/delayvar.py +171 -23
- rapidtide/workflows/diffrois.py +81 -3
- rapidtide/workflows/endtidalproc.py +144 -4
- rapidtide/workflows/fdica.py +195 -15
- rapidtide/workflows/filtnifti.py +70 -3
- rapidtide/workflows/filttc.py +74 -3
- rapidtide/workflows/fitSimFuncMap.py +202 -51
- rapidtide/workflows/fixtr.py +73 -3
- rapidtide/workflows/gmscalc.py +113 -3
- rapidtide/workflows/happy.py +801 -199
- rapidtide/workflows/happy2std.py +144 -12
- rapidtide/workflows/happy_parser.py +163 -23
- rapidtide/workflows/histnifti.py +118 -2
- rapidtide/workflows/histtc.py +84 -3
- rapidtide/workflows/linfitfilt.py +117 -4
- rapidtide/workflows/localflow.py +328 -28
- rapidtide/workflows/mergequality.py +79 -3
- rapidtide/workflows/niftidecomp.py +322 -18
- rapidtide/workflows/niftistats.py +174 -4
- rapidtide/workflows/pairproc.py +98 -4
- rapidtide/workflows/pairwisemergenifti.py +85 -2
- rapidtide/workflows/parser_funcs.py +1421 -40
- rapidtide/workflows/physiofreq.py +137 -11
- rapidtide/workflows/pixelcomp.py +207 -5
- rapidtide/workflows/plethquality.py +103 -21
- rapidtide/workflows/polyfitim.py +151 -11
- rapidtide/workflows/proj2flow.py +75 -2
- rapidtide/workflows/rankimage.py +111 -4
- rapidtide/workflows/rapidtide.py +368 -76
- rapidtide/workflows/rapidtide2std.py +98 -2
- rapidtide/workflows/rapidtide_parser.py +109 -9
- rapidtide/workflows/refineDelayMap.py +144 -33
- rapidtide/workflows/refineRegressor.py +675 -96
- rapidtide/workflows/regressfrommaps.py +161 -37
- rapidtide/workflows/resamplenifti.py +85 -3
- rapidtide/workflows/resampletc.py +91 -3
- rapidtide/workflows/retrolagtcs.py +99 -9
- rapidtide/workflows/retroregress.py +176 -26
- rapidtide/workflows/roisummarize.py +174 -5
- rapidtide/workflows/runqualitycheck.py +71 -3
- rapidtide/workflows/showarbcorr.py +149 -6
- rapidtide/workflows/showhist.py +86 -2
- rapidtide/workflows/showstxcorr.py +160 -3
- rapidtide/workflows/showtc.py +159 -3
- rapidtide/workflows/showxcorrx.py +190 -10
- rapidtide/workflows/showxy.py +185 -15
- rapidtide/workflows/simdata.py +264 -38
- rapidtide/workflows/spatialfit.py +77 -2
- rapidtide/workflows/spatialmi.py +250 -27
- rapidtide/workflows/spectrogram.py +305 -32
- rapidtide/workflows/synthASL.py +154 -3
- rapidtide/workflows/tcfrom2col.py +76 -2
- rapidtide/workflows/tcfrom3col.py +74 -2
- rapidtide/workflows/tidepool.py +2971 -130
- rapidtide/workflows/utils.py +19 -14
- rapidtide/workflows/utils_doc.py +293 -0
- rapidtide/workflows/variabilityizer.py +116 -3
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/METADATA +10 -8
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/RECORD +144 -128
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/entry_points.txt +1 -0
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/WHEEL +0 -0
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/licenses/LICENSE +0 -0
- {rapidtide-3.0.11.dist-info → rapidtide-3.1.1.dist-info}/top_level.txt +0 -0
|
@@ -23,6 +23,7 @@ import argparse
|
|
|
23
23
|
import os.path as op
|
|
24
24
|
import sys
|
|
25
25
|
from argparse import Namespace
|
|
26
|
+
from typing import Any, Callable, Dict, List, Optional, Tuple, Union
|
|
26
27
|
|
|
27
28
|
import rapidtide.filter as tide_filt
|
|
28
29
|
import rapidtide.io as tide_io
|
|
@@ -30,12 +31,90 @@ import rapidtide.util as tide_util
|
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
class IndicateSpecifiedAction(argparse.Action):
|
|
33
|
-
def __call__(
|
|
34
|
+
def __call__(
|
|
35
|
+
self,
|
|
36
|
+
parser: argparse.ArgumentParser,
|
|
37
|
+
namespace: argparse.Namespace,
|
|
38
|
+
values: Any,
|
|
39
|
+
option_string: Optional[str] = None,
|
|
40
|
+
) -> None:
|
|
41
|
+
"""
|
|
42
|
+
Store the parsed values in the namespace and mark as non-default.
|
|
43
|
+
|
|
44
|
+
This method is called when an argument parser encounters a command-line
|
|
45
|
+
argument that matches this action. It stores the parsed values in the
|
|
46
|
+
namespace and sets a corresponding flag indicating the value was explicitly
|
|
47
|
+
provided (not default).
|
|
48
|
+
|
|
49
|
+
Parameters
|
|
50
|
+
----------
|
|
51
|
+
parser : argparse.ArgumentParser
|
|
52
|
+
The ArgumentParser object which contains this action.
|
|
53
|
+
namespace : argparse.Namespace
|
|
54
|
+
The namespace object to which the parsed values will be stored.
|
|
55
|
+
values : Any
|
|
56
|
+
The parsed values for the argument.
|
|
57
|
+
option_string : str, optional
|
|
58
|
+
The command-line option string that triggered this action.
|
|
59
|
+
|
|
60
|
+
Returns
|
|
61
|
+
-------
|
|
62
|
+
None
|
|
63
|
+
This method does not return a value.
|
|
64
|
+
|
|
65
|
+
Notes
|
|
66
|
+
-----
|
|
67
|
+
This action stores the values using `setattr(namespace, self.dest, values)`
|
|
68
|
+
and additionally sets `setattr(namespace, self.dest + "_nondefault", True)`
|
|
69
|
+
to indicate that the value was explicitly provided rather than using a default.
|
|
70
|
+
|
|
71
|
+
Examples
|
|
72
|
+
--------
|
|
73
|
+
>>> import argparse
|
|
74
|
+
>>> parser = argparse.ArgumentParser()
|
|
75
|
+
>>> parser.add_argument('--verbose', action='store_true')
|
|
76
|
+
>>> args = parser.parse_args(['--verbose'])
|
|
77
|
+
>>> args.verbose
|
|
78
|
+
True
|
|
79
|
+
"""
|
|
34
80
|
setattr(namespace, self.dest, values)
|
|
35
81
|
setattr(namespace, self.dest + "_nondefault", True)
|
|
36
82
|
|
|
37
83
|
|
|
38
|
-
def detailedversion():
|
|
84
|
+
def detailedversion() -> None:
|
|
85
|
+
"""
|
|
86
|
+
Print detailed version information including release version, git metadata, and Python version.
|
|
87
|
+
|
|
88
|
+
This function retrieves version information from the tide_util.version() function and
|
|
89
|
+
prints comprehensive details about the current build, including release version, git
|
|
90
|
+
commit hash, git commit date, whether the working directory is dirty, and the Python
|
|
91
|
+
version being used. The function then exits the program with sys.exit().
|
|
92
|
+
|
|
93
|
+
Returns
|
|
94
|
+
-------
|
|
95
|
+
None
|
|
96
|
+
This function does not return any value and exits the program after printing
|
|
97
|
+
the version information.
|
|
98
|
+
|
|
99
|
+
Notes
|
|
100
|
+
-----
|
|
101
|
+
The function relies on the tide_util.version() function to retrieve git and release
|
|
102
|
+
information. The output includes:
|
|
103
|
+
- release_version: The official release version string
|
|
104
|
+
- git_sha: The git commit hash
|
|
105
|
+
- git_date: The git commit date
|
|
106
|
+
- git_isdirty: Boolean indicating if the working directory has uncommitted changes
|
|
107
|
+
- python_version: The Python version information
|
|
108
|
+
|
|
109
|
+
Examples
|
|
110
|
+
--------
|
|
111
|
+
>>> detailedversion()
|
|
112
|
+
release version: 1.2.3
|
|
113
|
+
git_sha: a1b2c3d4e5f6
|
|
114
|
+
git_date: 2023-10-15 14:30:45
|
|
115
|
+
git_isdirty: False
|
|
116
|
+
python_version: (3, 9, 7, 'final', 0)
|
|
117
|
+
"""
|
|
39
118
|
(
|
|
40
119
|
release_version,
|
|
41
120
|
git_sha,
|
|
@@ -51,30 +130,141 @@ def detailedversion():
|
|
|
51
130
|
sys.exit()
|
|
52
131
|
|
|
53
132
|
|
|
54
|
-
def setifnotset(thedict, thekey, theval):
|
|
133
|
+
def setifnotset(thedict: Dict[str, Any], thekey: str, theval: Any) -> None:
|
|
134
|
+
"""
|
|
135
|
+
Set a value in dictionary if key with "_nondefault" suffix is not present.
|
|
136
|
+
|
|
137
|
+
This function checks if a key with the suffix "_nondefault" exists in the
|
|
138
|
+
dictionary. If not found, it prints a message and sets the specified key
|
|
139
|
+
to the given value.
|
|
140
|
+
|
|
141
|
+
Parameters
|
|
142
|
+
----------
|
|
143
|
+
thedict : dict of str to Any
|
|
144
|
+
The dictionary to modify
|
|
145
|
+
thekey : str
|
|
146
|
+
The key to set in the dictionary
|
|
147
|
+
theval : Any
|
|
148
|
+
The value to set for thekey
|
|
149
|
+
|
|
150
|
+
Returns
|
|
151
|
+
-------
|
|
152
|
+
None
|
|
153
|
+
This function modifies the dictionary in-place and returns None
|
|
154
|
+
|
|
155
|
+
Notes
|
|
156
|
+
-----
|
|
157
|
+
The function uses the convention that keys with "_nondefault" suffix indicate
|
|
158
|
+
that a default value has already been overridden. This is useful for tracking
|
|
159
|
+
which settings have been explicitly set versus those that remain at default values.
|
|
160
|
+
|
|
161
|
+
Examples
|
|
162
|
+
--------
|
|
163
|
+
>>> config = {}
|
|
164
|
+
>>> setifnotset(config, 'debug', True)
|
|
165
|
+
overriding debug
|
|
166
|
+
>>> print(config)
|
|
167
|
+
{'debug': True}
|
|
168
|
+
|
|
169
|
+
>>> config = {'debug_nondefault': True}
|
|
170
|
+
>>> setifnotset(config, 'debug', False)
|
|
171
|
+
>>> print(config)
|
|
172
|
+
{'debug_nondefault': True}
|
|
173
|
+
"""
|
|
55
174
|
if (thekey + "_nondefault") not in thedict.keys():
|
|
56
175
|
print("overriding " + thekey)
|
|
57
176
|
thedict[thekey] = theval
|
|
58
177
|
|
|
59
178
|
|
|
60
|
-
def is_valid_file(parser, arg):
|
|
179
|
+
def is_valid_file(parser: argparse.ArgumentParser, arg: Optional[str]) -> Optional[str]:
|
|
61
180
|
"""
|
|
62
|
-
Check if argument is existing file.
|
|
181
|
+
Check if argument is an existing file.
|
|
182
|
+
|
|
183
|
+
This function validates that the provided argument corresponds to an existing file.
|
|
184
|
+
If the argument is None, the function returns None without further validation.
|
|
185
|
+
If the argument is not None, it parses the file specification and checks if the file exists.
|
|
186
|
+
|
|
187
|
+
Parameters
|
|
188
|
+
----------
|
|
189
|
+
parser : argparse.ArgumentParser
|
|
190
|
+
The argument parser object used to raise errors when file validation fails.
|
|
191
|
+
arg : str, optional
|
|
192
|
+
The file path to validate. If None, no validation is performed.
|
|
193
|
+
|
|
194
|
+
Returns
|
|
195
|
+
-------
|
|
196
|
+
str, optional
|
|
197
|
+
Returns the original argument if it is a valid file path, or None if the argument is None.
|
|
198
|
+
|
|
199
|
+
Raises
|
|
200
|
+
------
|
|
201
|
+
SystemExit
|
|
202
|
+
If the file specified in arg does not exist, the parser will raise a SystemExit error.
|
|
203
|
+
|
|
204
|
+
Notes
|
|
205
|
+
-----
|
|
206
|
+
This function uses `tide_io.parsefilespec()` to parse the file specification and
|
|
207
|
+
`os.path.isfile()` to check file existence.
|
|
208
|
+
|
|
209
|
+
Examples
|
|
210
|
+
--------
|
|
211
|
+
>>> import argparse
|
|
212
|
+
>>> parser = argparse.ArgumentParser()
|
|
213
|
+
>>> is_valid_file(parser, "existing_file.txt")
|
|
214
|
+
'existing_file.txt'
|
|
215
|
+
|
|
216
|
+
>>> is_valid_file(parser, "nonexistent_file.txt")
|
|
217
|
+
# Raises SystemExit with error message
|
|
63
218
|
"""
|
|
64
219
|
if arg is not None:
|
|
65
220
|
thefilename, colspec = tide_io.parsefilespec(arg)
|
|
66
221
|
else:
|
|
67
222
|
thefilename = None
|
|
68
223
|
|
|
69
|
-
if not op.isfile(thefilename) and thefilename is not None:
|
|
224
|
+
if not op.isfile(thefilename) and (thefilename is not None):
|
|
70
225
|
parser.error("The file {0} does not exist!".format(thefilename))
|
|
71
226
|
|
|
72
227
|
return arg
|
|
73
228
|
|
|
74
229
|
|
|
75
|
-
def invert_float(parser, arg):
|
|
230
|
+
def invert_float(parser: argparse.ArgumentParser, arg: Union[str, float]) -> Union[str, float]:
|
|
76
231
|
"""
|
|
77
232
|
Check if argument is float or auto.
|
|
233
|
+
|
|
234
|
+
This function validates if the input argument is a float value or the string "auto".
|
|
235
|
+
If the argument is a valid float, it computes the multiplicative inverse (1/x).
|
|
236
|
+
If the argument is "auto", it returns "auto" unchanged.
|
|
237
|
+
|
|
238
|
+
Parameters
|
|
239
|
+
----------
|
|
240
|
+
parser : argparse.ArgumentParser
|
|
241
|
+
The argument parser object used for validation.
|
|
242
|
+
arg : str or float
|
|
243
|
+
The argument to be checked and inverted. Can be a string representation
|
|
244
|
+
of a float or the string "auto".
|
|
245
|
+
|
|
246
|
+
Returns
|
|
247
|
+
-------
|
|
248
|
+
str or float
|
|
249
|
+
If arg is "auto", returns "auto". Otherwise, returns the multiplicative
|
|
250
|
+
inverse of the float value.
|
|
251
|
+
|
|
252
|
+
Notes
|
|
253
|
+
-----
|
|
254
|
+
This function relies on the `is_float` helper function for validation.
|
|
255
|
+
The multiplicative inverse is computed as 1.0 / arg, so the input must be
|
|
256
|
+
non-zero to avoid division by zero errors.
|
|
257
|
+
|
|
258
|
+
Examples
|
|
259
|
+
--------
|
|
260
|
+
>>> invert_float(parser, 2.0)
|
|
261
|
+
0.5
|
|
262
|
+
|
|
263
|
+
>>> invert_float(parser, "auto")
|
|
264
|
+
"auto"
|
|
265
|
+
|
|
266
|
+
>>> invert_float(parser, "0.5")
|
|
267
|
+
2.0
|
|
78
268
|
"""
|
|
79
269
|
arg = is_float(parser, arg)
|
|
80
270
|
|
|
@@ -83,9 +273,59 @@ def invert_float(parser, arg):
|
|
|
83
273
|
return arg
|
|
84
274
|
|
|
85
275
|
|
|
86
|
-
def is_float(
|
|
276
|
+
def is_float(
|
|
277
|
+
parser: argparse.ArgumentParser,
|
|
278
|
+
arg: Union[str, float],
|
|
279
|
+
minval: Optional[float] = None,
|
|
280
|
+
maxval: Optional[float] = None,
|
|
281
|
+
) -> Union[str, float]:
|
|
87
282
|
"""
|
|
88
283
|
Check if argument is float or auto.
|
|
284
|
+
|
|
285
|
+
This function validates that the input argument can be converted to a float
|
|
286
|
+
or is the string "auto". It also checks that the value falls within specified
|
|
287
|
+
bounds if minval or maxval are provided.
|
|
288
|
+
|
|
289
|
+
Parameters
|
|
290
|
+
----------
|
|
291
|
+
parser : argparse.ArgumentParser
|
|
292
|
+
The argument parser object used for error reporting.
|
|
293
|
+
arg : str or float
|
|
294
|
+
The argument to validate. Can be a string representation of a float
|
|
295
|
+
or the string "auto".
|
|
296
|
+
minval : float, optional
|
|
297
|
+
The minimum allowed value. If specified, the argument must be greater than
|
|
298
|
+
or equal to this value.
|
|
299
|
+
maxval : float, optional
|
|
300
|
+
The maximum allowed value. If specified, the argument must be less than
|
|
301
|
+
or equal to this value.
|
|
302
|
+
|
|
303
|
+
Returns
|
|
304
|
+
-------
|
|
305
|
+
str or float
|
|
306
|
+
Returns the validated argument as a float if it's not "auto", otherwise
|
|
307
|
+
returns the string "auto".
|
|
308
|
+
|
|
309
|
+
Raises
|
|
310
|
+
------
|
|
311
|
+
SystemExit
|
|
312
|
+
If the argument cannot be converted to a float or violates the min/max
|
|
313
|
+
constraints, the parser will call sys.exit() with an error message.
|
|
314
|
+
|
|
315
|
+
Examples
|
|
316
|
+
--------
|
|
317
|
+
>>> parser = argparse.ArgumentParser()
|
|
318
|
+
>>> is_float(parser, "3.14")
|
|
319
|
+
3.14
|
|
320
|
+
|
|
321
|
+
>>> is_float(parser, "auto")
|
|
322
|
+
'auto'
|
|
323
|
+
|
|
324
|
+
>>> is_float(parser, "5", minval=0, maxval=10)
|
|
325
|
+
5.0
|
|
326
|
+
|
|
327
|
+
>>> is_float(parser, "-1", minval=0)
|
|
328
|
+
SystemExit: Value -1.0 is smaller than 0
|
|
89
329
|
"""
|
|
90
330
|
if arg != "auto":
|
|
91
331
|
try:
|
|
@@ -100,9 +340,52 @@ def is_float(parser, arg, minval=None, maxval=None):
|
|
|
100
340
|
return arg
|
|
101
341
|
|
|
102
342
|
|
|
103
|
-
def is_valid_file_or_float(
|
|
343
|
+
def is_valid_file_or_float(
|
|
344
|
+
parser: argparse.ArgumentParser, arg: Optional[str]
|
|
345
|
+
) -> Union[str, float]:
|
|
104
346
|
"""
|
|
105
|
-
Check if argument is existing file.
|
|
347
|
+
Check if argument is an existing file or a valid float value.
|
|
348
|
+
|
|
349
|
+
This function validates whether the input argument is either:
|
|
350
|
+
1. An existing file path, or
|
|
351
|
+
2. A valid float number
|
|
352
|
+
|
|
353
|
+
If the argument is not a valid file but can be converted to a float,
|
|
354
|
+
the function returns the float value. Otherwise, it raises an error
|
|
355
|
+
through the provided parser.
|
|
356
|
+
|
|
357
|
+
Parameters
|
|
358
|
+
----------
|
|
359
|
+
parser : argparse.ArgumentParser
|
|
360
|
+
The argument parser object used to raise errors when validation fails.
|
|
361
|
+
arg : str, optional
|
|
362
|
+
The argument to validate. Can be a file path or a string representation
|
|
363
|
+
of a float number. If None, the function returns None.
|
|
364
|
+
|
|
365
|
+
Returns
|
|
366
|
+
-------
|
|
367
|
+
str or float
|
|
368
|
+
If argument is a valid file path, returns the original string.
|
|
369
|
+
If argument is a valid float, returns the float value.
|
|
370
|
+
If argument is None, returns None.
|
|
371
|
+
|
|
372
|
+
Raises
|
|
373
|
+
------
|
|
374
|
+
SystemExit
|
|
375
|
+
Raised by the parser when the argument is neither a valid file nor
|
|
376
|
+
a valid float number.
|
|
377
|
+
|
|
378
|
+
Examples
|
|
379
|
+
--------
|
|
380
|
+
>>> parser = argparse.ArgumentParser()
|
|
381
|
+
>>> is_valid_file_or_float(parser, "data.txt")
|
|
382
|
+
"data.txt"
|
|
383
|
+
|
|
384
|
+
>>> is_valid_file_or_float(parser, "3.14")
|
|
385
|
+
3.14
|
|
386
|
+
|
|
387
|
+
>>> is_valid_file_or_float(parser, "invalid_file.txt")
|
|
388
|
+
SystemExit: Value invalid_file.txt is not a float or a valid filename
|
|
106
389
|
"""
|
|
107
390
|
if arg is not None:
|
|
108
391
|
thefilename, colspec = tide_io.parsefilespec(arg)
|
|
@@ -119,9 +402,54 @@ def is_valid_file_or_float(parser, arg):
|
|
|
119
402
|
return arg
|
|
120
403
|
|
|
121
404
|
|
|
122
|
-
def is_int(
|
|
405
|
+
def is_int(
|
|
406
|
+
parser: argparse.ArgumentParser,
|
|
407
|
+
arg: Union[str, int],
|
|
408
|
+
minval: Optional[int] = None,
|
|
409
|
+
maxval: Optional[int] = None,
|
|
410
|
+
) -> Union[str, int]:
|
|
123
411
|
"""
|
|
124
412
|
Check if argument is int or auto.
|
|
413
|
+
|
|
414
|
+
This function validates that the input argument is either an integer or the string "auto".
|
|
415
|
+
If the argument is not "auto", it attempts to convert it to an integer. The function also
|
|
416
|
+
checks that the integer value falls within the specified bounds.
|
|
417
|
+
|
|
418
|
+
Parameters
|
|
419
|
+
----------
|
|
420
|
+
parser : argparse.ArgumentParser
|
|
421
|
+
The argument parser object used to raise errors when validation fails.
|
|
422
|
+
arg : str or int
|
|
423
|
+
The argument to validate. Can be an integer or the string "auto".
|
|
424
|
+
minval : int, optional
|
|
425
|
+
The minimum allowed value for the integer. If None, no minimum check is performed.
|
|
426
|
+
maxval : int, optional
|
|
427
|
+
The maximum allowed value for the integer. If None, no maximum check is performed.
|
|
428
|
+
|
|
429
|
+
Returns
|
|
430
|
+
-------
|
|
431
|
+
str or int
|
|
432
|
+
Returns the original argument if it is "auto", otherwise returns the converted integer.
|
|
433
|
+
|
|
434
|
+
Raises
|
|
435
|
+
------
|
|
436
|
+
SystemExit
|
|
437
|
+
Raised by the parser.error() method when validation fails.
|
|
438
|
+
|
|
439
|
+
Examples
|
|
440
|
+
--------
|
|
441
|
+
>>> parser = argparse.ArgumentParser()
|
|
442
|
+
>>> is_int(parser, "42")
|
|
443
|
+
42
|
|
444
|
+
|
|
445
|
+
>>> is_int(parser, "auto")
|
|
446
|
+
'auto'
|
|
447
|
+
|
|
448
|
+
>>> is_int(parser, "42", minval=0, maxval=100)
|
|
449
|
+
42
|
|
450
|
+
|
|
451
|
+
>>> is_int(parser, "42", minval=50) # raises parser.error
|
|
452
|
+
SystemExit: Value 42 is smaller than 50
|
|
125
453
|
"""
|
|
126
454
|
if arg != "auto":
|
|
127
455
|
try:
|
|
@@ -136,9 +464,46 @@ def is_int(parser, arg, minval=None, maxval=None):
|
|
|
136
464
|
return arg
|
|
137
465
|
|
|
138
466
|
|
|
139
|
-
def is_range(parser, arg):
|
|
467
|
+
def is_range(parser: argparse.ArgumentParser, arg: Optional[List[Any]]) -> Optional[List[Any]]:
|
|
140
468
|
"""
|
|
141
469
|
Check if argument is min/max pair.
|
|
470
|
+
|
|
471
|
+
This function validates that the provided argument is a list containing exactly two elements
|
|
472
|
+
that can be converted to floats, with the first element being less than or equal to the second.
|
|
473
|
+
|
|
474
|
+
Parameters
|
|
475
|
+
----------
|
|
476
|
+
parser : argparse.ArgumentParser
|
|
477
|
+
The argument parser object used for error reporting
|
|
478
|
+
arg : list of any, optional
|
|
479
|
+
List containing two elements representing min and max values, or None
|
|
480
|
+
|
|
481
|
+
Returns
|
|
482
|
+
-------
|
|
483
|
+
list of any, optional
|
|
484
|
+
The original argument if validation passes, or None if arg is None
|
|
485
|
+
|
|
486
|
+
Raises
|
|
487
|
+
------
|
|
488
|
+
SystemExit
|
|
489
|
+
If argument is not None and does not contain exactly two elements, or if min > max
|
|
490
|
+
|
|
491
|
+
Notes
|
|
492
|
+
-----
|
|
493
|
+
This function is typically used as a type validator for argparse arguments.
|
|
494
|
+
|
|
495
|
+
Examples
|
|
496
|
+
--------
|
|
497
|
+
>>> import argparse
|
|
498
|
+
>>> parser = argparse.ArgumentParser()
|
|
499
|
+
>>> is_range(parser, ['1', '10'])
|
|
500
|
+
['1', '10']
|
|
501
|
+
|
|
502
|
+
>>> is_range(parser, ['10', '1'])
|
|
503
|
+
SystemExit: Argument min must be lower than max.
|
|
504
|
+
|
|
505
|
+
>>> is_range(parser, ['1', '2', '3'])
|
|
506
|
+
SystemExit: Argument must be min/max pair.
|
|
142
507
|
"""
|
|
143
508
|
if arg is not None and len(arg) != 2:
|
|
144
509
|
parser.error("Argument must be min/max pair.")
|
|
@@ -148,9 +513,37 @@ def is_range(parser, arg):
|
|
|
148
513
|
return arg
|
|
149
514
|
|
|
150
515
|
|
|
151
|
-
def is_valid_tag(parser, arg):
|
|
516
|
+
def is_valid_tag(parser: argparse.ArgumentParser, arg: Optional[str]) -> Tuple[str, str]:
|
|
152
517
|
"""
|
|
153
518
|
Check if argument is existing file.
|
|
519
|
+
|
|
520
|
+
Parameters
|
|
521
|
+
----------
|
|
522
|
+
parser : argparse.ArgumentParser
|
|
523
|
+
The argument parser object used for error reporting.
|
|
524
|
+
arg : str, optional
|
|
525
|
+
The argument string to validate, expected to be in the format "tagname,value1,value2,...".
|
|
526
|
+
If None, the function will return empty strings.
|
|
527
|
+
|
|
528
|
+
Returns
|
|
529
|
+
-------
|
|
530
|
+
tuple of (str, str)
|
|
531
|
+
A tuple containing (tagname, tagval) where tagname is the first part
|
|
532
|
+
before the first comma and tagval is the rest of the string joined by commas.
|
|
533
|
+
|
|
534
|
+
Notes
|
|
535
|
+
-----
|
|
536
|
+
If the argument contains fewer than 2 comma-separated parts, the parser will
|
|
537
|
+
raise an error with the message "No tag value specified."
|
|
538
|
+
|
|
539
|
+
Examples
|
|
540
|
+
--------
|
|
541
|
+
>>> parser = argparse.ArgumentParser()
|
|
542
|
+
>>> is_valid_tag(parser, "environment,production,us-east")
|
|
543
|
+
('environment', 'production,us-east')
|
|
544
|
+
|
|
545
|
+
>>> is_valid_tag(parser, "type,web,app")
|
|
546
|
+
('type', 'web,app')
|
|
154
547
|
"""
|
|
155
548
|
if arg is not None:
|
|
156
549
|
argparts = arg.split(",")
|
|
@@ -173,7 +566,43 @@ DEFAULT_PADVAL = 0
|
|
|
173
566
|
DEFAULT_WINDOWFUNC = "hamming"
|
|
174
567
|
|
|
175
568
|
|
|
176
|
-
def addreqinputniftifile(
|
|
569
|
+
def addreqinputniftifile(
|
|
570
|
+
parser: argparse.ArgumentParser, varname: str, addedtext: str = ""
|
|
571
|
+
) -> None:
|
|
572
|
+
"""
|
|
573
|
+
Add a required input NIFTI file argument to an ArgumentParser.
|
|
574
|
+
|
|
575
|
+
This function adds a command-line argument for specifying a required input
|
|
576
|
+
NIFTI file to the provided ArgumentParser. The argument includes validation
|
|
577
|
+
to ensure the file exists and is readable.
|
|
578
|
+
|
|
579
|
+
Parameters
|
|
580
|
+
----------
|
|
581
|
+
parser : argparse.ArgumentParser
|
|
582
|
+
The ArgumentParser object to which the argument will be added.
|
|
583
|
+
varname : str
|
|
584
|
+
The name of the command-line argument (e.g., '--input' or '-i').
|
|
585
|
+
addedtext : str, optional
|
|
586
|
+
Additional text to append to the help message. Default is empty string.
|
|
587
|
+
|
|
588
|
+
Returns
|
|
589
|
+
-------
|
|
590
|
+
None
|
|
591
|
+
This function modifies the parser in-place and returns nothing.
|
|
592
|
+
|
|
593
|
+
Notes
|
|
594
|
+
-----
|
|
595
|
+
The function uses a lambda function for type validation that calls
|
|
596
|
+
`is_valid_file(parser, x)` to ensure the specified file exists and is
|
|
597
|
+
readable before accepting it as a valid input.
|
|
598
|
+
|
|
599
|
+
Examples
|
|
600
|
+
--------
|
|
601
|
+
>>> import argparse
|
|
602
|
+
>>> parser = argparse.ArgumentParser()
|
|
603
|
+
>>> addreqinputniftifile(parser, '--input', 'Path to the input NIFTI file.')
|
|
604
|
+
>>> args = parser.parse_args(['--input', 'data.nii.gz'])
|
|
605
|
+
"""
|
|
177
606
|
parser.add_argument(
|
|
178
607
|
varname,
|
|
179
608
|
type=lambda x: is_valid_file(parser, x),
|
|
@@ -181,7 +610,46 @@ def addreqinputniftifile(parser, varname, addedtext=""):
|
|
|
181
610
|
)
|
|
182
611
|
|
|
183
612
|
|
|
184
|
-
def addreqoutputniftifile(
|
|
613
|
+
def addreqoutputniftifile(
|
|
614
|
+
parser: argparse.ArgumentParser, varname: str, addedtext: str = ""
|
|
615
|
+
) -> None:
|
|
616
|
+
"""
|
|
617
|
+
Add a required argument for specifying output NIFTI file name to an argument parser.
|
|
618
|
+
|
|
619
|
+
This function adds a command-line argument to the provided ArgumentParser instance
|
|
620
|
+
that specifies the output NIFTI file name. The argument is required and accepts
|
|
621
|
+
string input.
|
|
622
|
+
|
|
623
|
+
Parameters
|
|
624
|
+
----------
|
|
625
|
+
parser : argparse.ArgumentParser
|
|
626
|
+
The argument parser to which the output NIFTI file argument will be added.
|
|
627
|
+
varname : str
|
|
628
|
+
The name of the command-line argument (without dashes). This will be used
|
|
629
|
+
as the argument name in the parser.
|
|
630
|
+
addedtext : str, optional
|
|
631
|
+
Additional text to append to the help message. Default is empty string.
|
|
632
|
+
|
|
633
|
+
Returns
|
|
634
|
+
-------
|
|
635
|
+
None
|
|
636
|
+
This function modifies the parser in-place and does not return any value.
|
|
637
|
+
|
|
638
|
+
Notes
|
|
639
|
+
-----
|
|
640
|
+
The added argument will be a required positional argument that accepts a string
|
|
641
|
+
representing the output NIFTI file path. The help message will include the
|
|
642
|
+
provided addedtext to give additional context to users.
|
|
643
|
+
|
|
644
|
+
Examples
|
|
645
|
+
--------
|
|
646
|
+
>>> import argparse
|
|
647
|
+
>>> parser = argparse.ArgumentParser()
|
|
648
|
+
>>> addreqoutputniftifile(parser, "output_file", "Path to the output NIFTI file.")
|
|
649
|
+
>>> args = parser.parse_args(['--output_file', 'output.nii.gz'])
|
|
650
|
+
>>> print(args.output_file)
|
|
651
|
+
output.nii.gz
|
|
652
|
+
"""
|
|
185
653
|
parser.add_argument(
|
|
186
654
|
varname,
|
|
187
655
|
type=str,
|
|
@@ -189,7 +657,49 @@ def addreqoutputniftifile(parser, varname, addedtext=""):
|
|
|
189
657
|
)
|
|
190
658
|
|
|
191
659
|
|
|
192
|
-
def addreqinputtextfile(
|
|
660
|
+
def addreqinputtextfile(
|
|
661
|
+
parser: argparse.ArgumentParser, varname: str, onecol: bool = False
|
|
662
|
+
) -> None:
|
|
663
|
+
"""
|
|
664
|
+
Add a required text file argument to an argument parser for timeseries data.
|
|
665
|
+
|
|
666
|
+
This function adds a command-line argument to specify a text file containing
|
|
667
|
+
one or more timeseries columns. The argument supports column selection options
|
|
668
|
+
depending on whether single or multiple columns are expected.
|
|
669
|
+
|
|
670
|
+
Parameters
|
|
671
|
+
----------
|
|
672
|
+
parser : argparse.ArgumentParser
|
|
673
|
+
The argument parser to which the text file argument will be added.
|
|
674
|
+
varname : str
|
|
675
|
+
The name of the argument variable (used as the argument flag).
|
|
676
|
+
onecol : bool, optional
|
|
677
|
+
If True, the argument expects a single column selection.
|
|
678
|
+
If False, the argument accepts multiple column specifications.
|
|
679
|
+
Default is False.
|
|
680
|
+
|
|
681
|
+
Returns
|
|
682
|
+
-------
|
|
683
|
+
None
|
|
684
|
+
This function modifies the parser in-place and returns None.
|
|
685
|
+
|
|
686
|
+
Notes
|
|
687
|
+
-----
|
|
688
|
+
The text file can be in BIDS format, which allows column names to be used
|
|
689
|
+
in addition to integer column indices. Column selection syntax varies based
|
|
690
|
+
on the `onecol` parameter:
|
|
691
|
+
|
|
692
|
+
- When `onecol=True`: Use `[:COLUMN]` where COLUMN is an integer or column name
|
|
693
|
+
- When `onecol=False`: Use `[:COLSPEC]` where COLSPEC is an integer, a column
|
|
694
|
+
separated list of ranges, or a comma separated set of column names
|
|
695
|
+
|
|
696
|
+
Examples
|
|
697
|
+
--------
|
|
698
|
+
>>> import argparse
|
|
699
|
+
>>> parser = argparse.ArgumentParser()
|
|
700
|
+
>>> addreqinputtextfile(parser, 'timeseries_file', onecol=False)
|
|
701
|
+
>>> args = parser.parse_args()
|
|
702
|
+
"""
|
|
193
703
|
if onecol:
|
|
194
704
|
colspecline = (
|
|
195
705
|
"Use [:COLUMN] to select which column to use, where COLUMN is an "
|
|
@@ -208,7 +718,55 @@ def addreqinputtextfile(parser, varname, onecol=False):
|
|
|
208
718
|
)
|
|
209
719
|
|
|
210
720
|
|
|
211
|
-
def addreqinputtextfiles(
|
|
721
|
+
def addreqinputtextfiles(
|
|
722
|
+
parser: argparse.ArgumentParser,
|
|
723
|
+
varname: str,
|
|
724
|
+
numreq: str = "Two",
|
|
725
|
+
nargs: str = "*",
|
|
726
|
+
onecol: bool = False,
|
|
727
|
+
) -> None:
|
|
728
|
+
"""
|
|
729
|
+
Add required input text files argument to an ArgumentParser.
|
|
730
|
+
|
|
731
|
+
This function adds a command-line argument to specify one or more text files
|
|
732
|
+
containing timeseries data. The argument supports various column selection
|
|
733
|
+
options depending on the `onecol` parameter.
|
|
734
|
+
|
|
735
|
+
Parameters
|
|
736
|
+
----------
|
|
737
|
+
parser : argparse.ArgumentParser
|
|
738
|
+
The ArgumentParser object to which the argument will be added.
|
|
739
|
+
varname : str
|
|
740
|
+
The name of the command-line argument (without dashes).
|
|
741
|
+
numreq : str, optional
|
|
742
|
+
Number of required input files (default is "Two").
|
|
743
|
+
nargs : str, optional
|
|
744
|
+
Argument specification for the number of arguments (default is "*").
|
|
745
|
+
onecol : bool, optional
|
|
746
|
+
If True, single column selection is enabled; if False, multiple column
|
|
747
|
+
selection is enabled (default is False).
|
|
748
|
+
|
|
749
|
+
Returns
|
|
750
|
+
-------
|
|
751
|
+
None
|
|
752
|
+
This function modifies the parser in-place and returns None.
|
|
753
|
+
|
|
754
|
+
Notes
|
|
755
|
+
-----
|
|
756
|
+
The added argument accepts text files with timeseries data. When `onecol` is
|
|
757
|
+
True, column selection uses the format [:COLUMN] where COLUMN is an integer
|
|
758
|
+
or column name (if input file is BIDS). When `onecol` is False, column
|
|
759
|
+
selection uses the format [:COLSPEC] where COLSPEC can be an integer, a
|
|
760
|
+
comma-separated list of ranges, or a comma-separated set of column names
|
|
761
|
+
(if input file is BIDS). Default behavior is to use all columns.
|
|
762
|
+
|
|
763
|
+
Examples
|
|
764
|
+
--------
|
|
765
|
+
>>> import argparse
|
|
766
|
+
>>> parser = argparse.ArgumentParser()
|
|
767
|
+
>>> addreqinputtextfiles(parser, "input_files", numreq="Three", onecol=True)
|
|
768
|
+
>>> args = parser.parse_args()
|
|
769
|
+
"""
|
|
212
770
|
if onecol:
|
|
213
771
|
colspecline = (
|
|
214
772
|
"Use [:COLUMN] to select which column to use, where COLUMN is an "
|
|
@@ -228,7 +786,52 @@ def addreqinputtextfiles(parser, varname, numreq="Two", nargs="*", onecol=False)
|
|
|
228
786
|
)
|
|
229
787
|
|
|
230
788
|
|
|
231
|
-
def addreqoutputtextfile(
|
|
789
|
+
def addreqoutputtextfile(
|
|
790
|
+
parser: argparse.ArgumentParser, varname: str, rootname: bool = False
|
|
791
|
+
) -> None:
|
|
792
|
+
"""
|
|
793
|
+
Add a required argument for specifying output text file name to an argument parser.
|
|
794
|
+
|
|
795
|
+
This function adds a command-line argument to the provided ArgumentParser instance
|
|
796
|
+
that specifies the name of an output text file. The argument is required and
|
|
797
|
+
accepts string values.
|
|
798
|
+
|
|
799
|
+
Parameters
|
|
800
|
+
----------
|
|
801
|
+
parser : argparse.ArgumentParser
|
|
802
|
+
The ArgumentParser instance to which the output file argument will be added.
|
|
803
|
+
varname : str
|
|
804
|
+
The name of the command-line argument (e.g., '--output-file' or '-o').
|
|
805
|
+
rootname : bool, optional
|
|
806
|
+
If True, the help text will indicate this is a root name for output files
|
|
807
|
+
(default is False, which indicates a single output text file name).
|
|
808
|
+
|
|
809
|
+
Returns
|
|
810
|
+
-------
|
|
811
|
+
None
|
|
812
|
+
This function modifies the parser in-place and does not return any value.
|
|
813
|
+
|
|
814
|
+
Notes
|
|
815
|
+
-----
|
|
816
|
+
This function is typically used when setting up command-line interfaces for
|
|
817
|
+
tools that require a specified output text file name. The argument added is
|
|
818
|
+
required, meaning the parser will raise an error if this argument is not provided.
|
|
819
|
+
|
|
820
|
+
Examples
|
|
821
|
+
--------
|
|
822
|
+
>>> import argparse
|
|
823
|
+
>>> parser = argparse.ArgumentParser()
|
|
824
|
+
>>> addreqoutputtextfile(parser, '--output-file')
|
|
825
|
+
>>> args = parser.parse_args(['--output-file', 'results.txt'])
|
|
826
|
+
>>> print(args.output_file)
|
|
827
|
+
'results.txt'
|
|
828
|
+
|
|
829
|
+
>>> parser = argparse.ArgumentParser()
|
|
830
|
+
>>> addreqoutputtextfile(parser, '--root-name', rootname=True)
|
|
831
|
+
>>> args = parser.parse_args(['--root-name', 'data'])
|
|
832
|
+
>>> print(args.root_name)
|
|
833
|
+
'data'
|
|
834
|
+
"""
|
|
232
835
|
if rootname:
|
|
233
836
|
helpline = "Root name for the output files"
|
|
234
837
|
else:
|
|
@@ -241,9 +844,44 @@ def addreqoutputtextfile(parser, varname, rootname=False):
|
|
|
241
844
|
|
|
242
845
|
|
|
243
846
|
def addtagopts(
|
|
244
|
-
opt_group,
|
|
245
|
-
helptext="Additional key, value pairs to add to the options json file (useful for tracking analyses).",
|
|
246
|
-
):
|
|
847
|
+
opt_group: argparse._ArgumentGroup,
|
|
848
|
+
helptext: str = "Additional key, value pairs to add to the options json file (useful for tracking analyses).",
|
|
849
|
+
) -> None:
|
|
850
|
+
"""
|
|
851
|
+
Add infotag argument to an argument group for adding key-value pairs to options JSON.
|
|
852
|
+
|
|
853
|
+
This function adds an --infotag command line argument to the provided argument group.
|
|
854
|
+
The argument accepts key-value pairs that are stored as additional metadata in the
|
|
855
|
+
options JSON file, which is useful for tracking analysis runs and experiments.
|
|
856
|
+
|
|
857
|
+
Parameters
|
|
858
|
+
----------
|
|
859
|
+
opt_group : argparse._ArgumentGroup
|
|
860
|
+
The argument group to which the infotag argument will be added
|
|
861
|
+
helptext : str, optional
|
|
862
|
+
Help text to display for the infotag argument (default: "Additional key, value pairs to add to the options json file (useful for tracking analyses).")
|
|
863
|
+
|
|
864
|
+
Returns
|
|
865
|
+
-------
|
|
866
|
+
None
|
|
867
|
+
This function modifies the argument group in-place and returns None
|
|
868
|
+
|
|
869
|
+
Notes
|
|
870
|
+
-----
|
|
871
|
+
The --infotag argument uses action="append" with nargs=2, allowing multiple
|
|
872
|
+
key-value pairs to be specified on the command line. Each pair is stored as
|
|
873
|
+
a tuple (key, value) in the argument group's parsed arguments.
|
|
874
|
+
|
|
875
|
+
Examples
|
|
876
|
+
--------
|
|
877
|
+
>>> import argparse
|
|
878
|
+
>>> parser = argparse.ArgumentParser()
|
|
879
|
+
>>> group = parser.add_argument_group('Options')
|
|
880
|
+
>>> addtagopts(group)
|
|
881
|
+
>>> args = parser.parse_args(['--infotag', 'experiment', 'run1', '--infotag', 'version', '1.0'])
|
|
882
|
+
>>> args.infotag
|
|
883
|
+
[['experiment', 'run1'], ['version', '1.0']]
|
|
884
|
+
"""
|
|
247
885
|
opt_group.add_argument(
|
|
248
886
|
"--infotag",
|
|
249
887
|
action="append",
|
|
@@ -254,7 +892,47 @@ def addtagopts(
|
|
|
254
892
|
)
|
|
255
893
|
|
|
256
894
|
|
|
257
|
-
def postprocesstagopts(args):
|
|
895
|
+
def postprocesstagopts(args: Namespace) -> Namespace:
|
|
896
|
+
"""
|
|
897
|
+
Process infotag options and convert them to INFO_ prefixed environment variables.
|
|
898
|
+
|
|
899
|
+
This function takes a Namespace object containing command line arguments and
|
|
900
|
+
processes any infotag options by converting them into environment variable
|
|
901
|
+
assignments with INFO_ prefix. The original infotag argument is removed from
|
|
902
|
+
the returned namespace.
|
|
903
|
+
|
|
904
|
+
Parameters
|
|
905
|
+
----------
|
|
906
|
+
args : Namespace
|
|
907
|
+
Command line arguments namespace containing potentially 'infotag' attribute
|
|
908
|
+
which is a list of tuples where each tuple contains (tag_name, tag_value).
|
|
909
|
+
|
|
910
|
+
Returns
|
|
911
|
+
-------
|
|
912
|
+
Namespace
|
|
913
|
+
Updated namespace with infotag options converted to INFO_ prefixed
|
|
914
|
+
environment variables. If infotag is None, returns the original args.
|
|
915
|
+
|
|
916
|
+
Notes
|
|
917
|
+
-----
|
|
918
|
+
The function modifies the input namespace in-place by converting infotag
|
|
919
|
+
entries from tuples of (tag_name, tag_value) to environment variable
|
|
920
|
+
assignments with INFO_ prefix. For example, if infotag contains
|
|
921
|
+
[('version', '1.0')], the result will have INFO_version='1.0' as a new
|
|
922
|
+
attribute in the namespace.
|
|
923
|
+
|
|
924
|
+
Examples
|
|
925
|
+
--------
|
|
926
|
+
>>> from argparse import Namespace
|
|
927
|
+
>>> args = Namespace(infotag=[('version', '1.0'), ('build', '2023')])
|
|
928
|
+
>>> result = postprocesstagopts(args)
|
|
929
|
+
>>> print(result.INFO_version)
|
|
930
|
+
'1.0'
|
|
931
|
+
>>> print(result.INFO_build)
|
|
932
|
+
'2023'
|
|
933
|
+
>>> print(hasattr(result, 'infotag'))
|
|
934
|
+
False
|
|
935
|
+
"""
|
|
258
936
|
if args.infotag is not None:
|
|
259
937
|
argvars = vars(args)
|
|
260
938
|
for thetag in argvars["infotag"]:
|
|
@@ -265,7 +943,50 @@ def postprocesstagopts(args):
|
|
|
265
943
|
return args
|
|
266
944
|
|
|
267
945
|
|
|
268
|
-
def addnormalizationopts(
|
|
946
|
+
def addnormalizationopts(
|
|
947
|
+
parser: argparse.ArgumentParser,
|
|
948
|
+
normtarget: str = "timecourse",
|
|
949
|
+
defaultmethod: str = DEFAULT_NORMTYPE,
|
|
950
|
+
) -> None:
|
|
951
|
+
"""
|
|
952
|
+
Add normalization options to an argument parser.
|
|
953
|
+
|
|
954
|
+
This function adds a new argument group titled "Normalization options" to the
|
|
955
|
+
provided argument parser. It includes an argument for specifying the normalization
|
|
956
|
+
method to be applied to the specified target.
|
|
957
|
+
|
|
958
|
+
Parameters
|
|
959
|
+
----------
|
|
960
|
+
parser : argparse.ArgumentParser
|
|
961
|
+
The argument parser to which the normalization options will be added.
|
|
962
|
+
normtarget : str, optional
|
|
963
|
+
The target data type being normalized (default is "timecourse").
|
|
964
|
+
defaultmethod : str, optional
|
|
965
|
+
The default normalization method to use (default is DEFAULT_NORMTYPE).
|
|
966
|
+
|
|
967
|
+
Returns
|
|
968
|
+
-------
|
|
969
|
+
None
|
|
970
|
+
This function modifies the parser in-place and returns None.
|
|
971
|
+
|
|
972
|
+
Notes
|
|
973
|
+
-----
|
|
974
|
+
The available normalization methods are:
|
|
975
|
+
|
|
976
|
+
- "None" - demean only
|
|
977
|
+
- "percent" - divide by mean
|
|
978
|
+
- "variance" - divide by variance
|
|
979
|
+
- "stddev" or "z" - divide by standard deviation
|
|
980
|
+
- "p2p" - divide by range
|
|
981
|
+
- "mad" - divide by median absolute deviation
|
|
982
|
+
|
|
983
|
+
Examples
|
|
984
|
+
--------
|
|
985
|
+
>>> import argparse
|
|
986
|
+
>>> parser = argparse.ArgumentParser()
|
|
987
|
+
>>> addnormalizationopts(parser)
|
|
988
|
+
>>> args = parser.parse_args()
|
|
989
|
+
"""
|
|
269
990
|
norm_opts = parser.add_argument_group("Normalization options")
|
|
270
991
|
norm_opts.add_argument(
|
|
271
992
|
"--normmethod",
|
|
@@ -288,7 +1009,38 @@ def addnormalizationopts(parser, normtarget="timecourse", defaultmethod=DEFAULT_
|
|
|
288
1009
|
)
|
|
289
1010
|
|
|
290
1011
|
|
|
291
|
-
def addversionopts(parser):
|
|
1012
|
+
def addversionopts(parser: argparse.ArgumentParser) -> None:
|
|
1013
|
+
"""
|
|
1014
|
+
Add version-related command line arguments to the given argument parser.
|
|
1015
|
+
|
|
1016
|
+
This function adds two version options to the provided argument parser:
|
|
1017
|
+
'--version' for simplified version information and '--detailedversion'
|
|
1018
|
+
for detailed version information.
|
|
1019
|
+
|
|
1020
|
+
Parameters
|
|
1021
|
+
----------
|
|
1022
|
+
parser : argparse.ArgumentParser
|
|
1023
|
+
The argument parser to which version options will be added.
|
|
1024
|
+
|
|
1025
|
+
Returns
|
|
1026
|
+
-------
|
|
1027
|
+
None
|
|
1028
|
+
This function modifies the parser in-place and does not return anything.
|
|
1029
|
+
|
|
1030
|
+
Notes
|
|
1031
|
+
-----
|
|
1032
|
+
The version information is retrieved using `tide_util.version()` function.
|
|
1033
|
+
The '--version' option displays only the major version number, while
|
|
1034
|
+
'--detailedversion' shows the complete version information including
|
|
1035
|
+
minor and patch versions.
|
|
1036
|
+
|
|
1037
|
+
Examples
|
|
1038
|
+
--------
|
|
1039
|
+
>>> import argparse
|
|
1040
|
+
>>> parser = argparse.ArgumentParser()
|
|
1041
|
+
>>> addversionopts(parser)
|
|
1042
|
+
>>> args = parser.parse_args(['--version'])
|
|
1043
|
+
"""
|
|
292
1044
|
version_opts = parser.add_argument_group("Version options")
|
|
293
1045
|
version_opts.add_argument(
|
|
294
1046
|
"--version",
|
|
@@ -304,7 +1056,47 @@ def addversionopts(parser):
|
|
|
304
1056
|
)
|
|
305
1057
|
|
|
306
1058
|
|
|
307
|
-
def addsamplerateopts(parser, details=False):
|
|
1059
|
+
def addsamplerateopts(parser: argparse.ArgumentParser, details: bool = False) -> None:
|
|
1060
|
+
"""
|
|
1061
|
+
Add sample rate related arguments to an argument parser.
|
|
1062
|
+
|
|
1063
|
+
This function adds mutually exclusive arguments for specifying the sample rate
|
|
1064
|
+
of data files. The user can either specify the sample rate directly using
|
|
1065
|
+
'--samplerate' or define it indirectly using '--sampletime' (which sets the
|
|
1066
|
+
sample rate to 1.0/TSTEP).
|
|
1067
|
+
|
|
1068
|
+
Parameters
|
|
1069
|
+
----------
|
|
1070
|
+
parser : argparse.ArgumentParser
|
|
1071
|
+
The argument parser to which the sample rate arguments will be added.
|
|
1072
|
+
details : bool, optional
|
|
1073
|
+
If True, additional detailed help text may be included (default is False).
|
|
1074
|
+
|
|
1075
|
+
Returns
|
|
1076
|
+
-------
|
|
1077
|
+
None
|
|
1078
|
+
This function modifies the parser in-place and does not return any value.
|
|
1079
|
+
|
|
1080
|
+
Notes
|
|
1081
|
+
-----
|
|
1082
|
+
The arguments are mutually exclusive - only one of '--samplerate' or '--sampletime'
|
|
1083
|
+
can be specified. If neither is specified, the default sample rate is 1.0.
|
|
1084
|
+
|
|
1085
|
+
Examples
|
|
1086
|
+
--------
|
|
1087
|
+
>>> import argparse
|
|
1088
|
+
>>> parser = argparse.ArgumentParser()
|
|
1089
|
+
>>> addsamplerateopts(parser)
|
|
1090
|
+
>>> args = parser.parse_args(['--samplerate', '44100'])
|
|
1091
|
+
>>> args.samplerate
|
|
1092
|
+
44100.0
|
|
1093
|
+
|
|
1094
|
+
>>> parser = argparse.ArgumentParser()
|
|
1095
|
+
>>> addsamplerateopts(parser)
|
|
1096
|
+
>>> args = parser.parse_args(['--sampletime', '0.001'])
|
|
1097
|
+
>>> args.samplerate
|
|
1098
|
+
1000.0
|
|
1099
|
+
"""
|
|
308
1100
|
sampling = parser.add_mutually_exclusive_group()
|
|
309
1101
|
sampling.add_argument(
|
|
310
1102
|
"--samplerate",
|
|
@@ -333,8 +1125,51 @@ def addsamplerateopts(parser, details=False):
|
|
|
333
1125
|
|
|
334
1126
|
|
|
335
1127
|
def addfilteropts(
|
|
336
|
-
parser
|
|
337
|
-
|
|
1128
|
+
parser: argparse.ArgumentParser,
|
|
1129
|
+
filtertarget: str = "timecourses",
|
|
1130
|
+
defaultmethod: str = DEFAULT_FILTERBAND,
|
|
1131
|
+
details: bool = False,
|
|
1132
|
+
) -> None:
|
|
1133
|
+
"""
|
|
1134
|
+
Add filtering options to an argument parser for configuring signal filtering.
|
|
1135
|
+
|
|
1136
|
+
This function adds a group of arguments to the provided `argparse.ArgumentParser`
|
|
1137
|
+
that allow users to specify filtering parameters for timecourses or other signals.
|
|
1138
|
+
It supports predefined filter bands, custom pass/stop frequencies, and various
|
|
1139
|
+
filter types and settings.
|
|
1140
|
+
|
|
1141
|
+
Parameters
|
|
1142
|
+
----------
|
|
1143
|
+
parser : argparse.ArgumentParser
|
|
1144
|
+
The argument parser to which the filtering options will be added.
|
|
1145
|
+
filtertarget : str, optional
|
|
1146
|
+
The target of the filtering operation (e.g., "timecourses"). Default is "timecourses".
|
|
1147
|
+
defaultmethod : str, optional
|
|
1148
|
+
The default filter band to use. Default is `DEFAULT_FILTERBAND`.
|
|
1149
|
+
details : bool, optional
|
|
1150
|
+
If True, adds additional detailed filtering options such as filter type,
|
|
1151
|
+
Butterworth order, padding, and padding type. Default is False.
|
|
1152
|
+
|
|
1153
|
+
Returns
|
|
1154
|
+
-------
|
|
1155
|
+
None
|
|
1156
|
+
This function modifies the parser in place and does not return any value.
|
|
1157
|
+
|
|
1158
|
+
Notes
|
|
1159
|
+
-----
|
|
1160
|
+
- The `--filterband` argument allows selection of predefined frequency bands.
|
|
1161
|
+
- The `--filterfreqs` and `--filterstopfreqs` arguments allow custom frequency
|
|
1162
|
+
filtering, with `--filterstopfreqs` requiring `--filterfreqs` to be specified.
|
|
1163
|
+
- When `details=True`, additional options such as `--filtertype`, `--butterorder`,
|
|
1164
|
+
`--padseconds`, and `--padtype` are added.
|
|
1165
|
+
|
|
1166
|
+
Examples
|
|
1167
|
+
--------
|
|
1168
|
+
>>> import argparse
|
|
1169
|
+
>>> parser = argparse.ArgumentParser()
|
|
1170
|
+
>>> addfilteropts(parser, filtertarget="signal", defaultmethod="hrv_lf")
|
|
1171
|
+
>>> args = parser.parse_args()
|
|
1172
|
+
"""
|
|
338
1173
|
filt_opts = parser.add_argument_group("Filtering options")
|
|
339
1174
|
filt_opts.add_argument(
|
|
340
1175
|
"--filterband",
|
|
@@ -454,7 +1289,44 @@ def addfilteropts(
|
|
|
454
1289
|
)
|
|
455
1290
|
|
|
456
1291
|
|
|
457
|
-
def postprocesssamplerateopts(args, debug=False):
|
|
1292
|
+
def postprocesssamplerateopts(args: Namespace, debug: bool = False) -> Namespace:
|
|
1293
|
+
"""
|
|
1294
|
+
Process sample rate options for the application.
|
|
1295
|
+
|
|
1296
|
+
This function handles the sample rate configuration, setting it to a default value
|
|
1297
|
+
when "auto" is specified, or using the provided value otherwise.
|
|
1298
|
+
|
|
1299
|
+
Parameters
|
|
1300
|
+
----------
|
|
1301
|
+
args : Namespace
|
|
1302
|
+
Command line arguments namespace containing the samplerate option
|
|
1303
|
+
debug : bool, optional
|
|
1304
|
+
Debug flag for additional logging (default is False)
|
|
1305
|
+
|
|
1306
|
+
Returns
|
|
1307
|
+
-------
|
|
1308
|
+
Namespace
|
|
1309
|
+
Updated arguments namespace with processed sample rate value
|
|
1310
|
+
|
|
1311
|
+
Notes
|
|
1312
|
+
-----
|
|
1313
|
+
When samplerate is set to "auto", it will be converted to 1.0. This is typically
|
|
1314
|
+
used as a default fallback value when automatic sample rate detection is not
|
|
1315
|
+
implemented or desired.
|
|
1316
|
+
|
|
1317
|
+
Examples
|
|
1318
|
+
--------
|
|
1319
|
+
>>> import argparse
|
|
1320
|
+
>>> args = argparse.Namespace(samplerate="auto")
|
|
1321
|
+
>>> result = postprocesssamplerateopts(args)
|
|
1322
|
+
>>> print(result.samplerate)
|
|
1323
|
+
1.0
|
|
1324
|
+
|
|
1325
|
+
>>> args = argparse.Namespace(samplerate=44100)
|
|
1326
|
+
>>> result = postprocesssamplerateopts(args)
|
|
1327
|
+
>>> print(result.samplerate)
|
|
1328
|
+
44100
|
|
1329
|
+
"""
|
|
458
1330
|
# set the sample rate
|
|
459
1331
|
if args.samplerate == "auto":
|
|
460
1332
|
samplerate = 1.0
|
|
@@ -465,7 +1337,56 @@ def postprocesssamplerateopts(args, debug=False):
|
|
|
465
1337
|
return args
|
|
466
1338
|
|
|
467
1339
|
|
|
468
|
-
def postprocessfilteropts(args, debug=False):
|
|
1340
|
+
def postprocessfilteropts(args: Namespace, debug: bool = False) -> Tuple[Namespace, Any]:
|
|
1341
|
+
"""
|
|
1342
|
+
Post-process filter options and configure a non-causal filter for tidal analysis.
|
|
1343
|
+
|
|
1344
|
+
This function configures the filter parameters based on the input arguments,
|
|
1345
|
+
sets up an appropriate filter type (e.g., trapezoidal or arbitrary passband),
|
|
1346
|
+
and initializes a `NoncausalFilter` object for subsequent use in tidal data processing.
|
|
1347
|
+
|
|
1348
|
+
Parameters
|
|
1349
|
+
----------
|
|
1350
|
+
args : Namespace
|
|
1351
|
+
A namespace object containing filter configuration parameters such as
|
|
1352
|
+
`filtertype`, `filtorder`, `padseconds`, `prefilterpadtype`, `passvec`,
|
|
1353
|
+
`stopvec`, and `filterband`.
|
|
1354
|
+
debug : bool, optional
|
|
1355
|
+
If True, prints debug information about the filter configuration and
|
|
1356
|
+
internal state during processing. Default is False.
|
|
1357
|
+
|
|
1358
|
+
Returns
|
|
1359
|
+
-------
|
|
1360
|
+
Tuple[Namespace, Any]
|
|
1361
|
+
A tuple containing:
|
|
1362
|
+
- `args`: The updated namespace with additional attributes like `arbvec`,
|
|
1363
|
+
`lowerstop`, `lowerpass`, `upperpass`, and `upperstop`.
|
|
1364
|
+
- `theprefilter`: The configured `NoncausalFilter` object used for filtering.
|
|
1365
|
+
|
|
1366
|
+
Notes
|
|
1367
|
+
-----
|
|
1368
|
+
- If `stopvec` is provided without `passvec`, a `ValueError` is raised.
|
|
1369
|
+
- The function supports both predefined filter bands and arbitrary pass/stop
|
|
1370
|
+
frequency vectors.
|
|
1371
|
+
- The `arbvec` is constructed as `[lowerpass, upperpass, lowerstop, upperstop]`
|
|
1372
|
+
for internal use, but the filter expects frequencies in the order
|
|
1373
|
+
`[lowerstop, lowerpass, upperpass, upperstop]`.
|
|
1374
|
+
|
|
1375
|
+
Examples
|
|
1376
|
+
--------
|
|
1377
|
+
>>> import argparse
|
|
1378
|
+
>>> args = argparse.Namespace()
|
|
1379
|
+
>>> args.filtertype = "trapezoidal"
|
|
1380
|
+
>>> args.filtorder = 4
|
|
1381
|
+
>>> args.padseconds = 10
|
|
1382
|
+
>>> args.prefilterpadtype = "constant"
|
|
1383
|
+
>>> args.passvec = [0.1, 0.2]
|
|
1384
|
+
>>> args.stopvec = None
|
|
1385
|
+
>>> args.filterband = "M2"
|
|
1386
|
+
>>> args, filter_obj = postprocessfilteropts(args)
|
|
1387
|
+
>>> print(args.lowerpass)
|
|
1388
|
+
0.1
|
|
1389
|
+
"""
|
|
469
1390
|
# configure the filter
|
|
470
1391
|
# set the trapezoidal flag, if using
|
|
471
1392
|
try:
|
|
@@ -548,7 +1469,41 @@ def postprocessfilteropts(args, debug=False):
|
|
|
548
1469
|
return args, theprefilter
|
|
549
1470
|
|
|
550
1471
|
|
|
551
|
-
def addwindowopts(parser, windowtype=DEFAULT_WINDOWFUNC):
|
|
1472
|
+
def addwindowopts(parser: argparse.ArgumentParser, windowtype: str = DEFAULT_WINDOWFUNC) -> None:
|
|
1473
|
+
"""
|
|
1474
|
+
Add windowing options to an argument parser for correlation analysis.
|
|
1475
|
+
|
|
1476
|
+
This function adds command-line arguments related to windowing operations
|
|
1477
|
+
that can be applied prior to correlation calculations. These options include
|
|
1478
|
+
the choice of window function and zero padding strategy.
|
|
1479
|
+
|
|
1480
|
+
Parameters
|
|
1481
|
+
----------
|
|
1482
|
+
parser : argparse.ArgumentParser
|
|
1483
|
+
The argument parser to which windowing options will be added.
|
|
1484
|
+
windowtype : str, optional
|
|
1485
|
+
The default window function to use. Default is DEFAULT_WINDOWFUNC.
|
|
1486
|
+
|
|
1487
|
+
Returns
|
|
1488
|
+
-------
|
|
1489
|
+
None
|
|
1490
|
+
This function modifies the parser in-place and returns nothing.
|
|
1491
|
+
|
|
1492
|
+
Notes
|
|
1493
|
+
-----
|
|
1494
|
+
The windowing options are added to a dedicated argument group called "Windowing options".
|
|
1495
|
+
The zero padding parameter allows for control over edge artifacts in correlation calculations,
|
|
1496
|
+
with negative values triggering automatic padding selection.
|
|
1497
|
+
|
|
1498
|
+
Examples
|
|
1499
|
+
--------
|
|
1500
|
+
>>> import argparse
|
|
1501
|
+
>>> parser = argparse.ArgumentParser()
|
|
1502
|
+
>>> addwindowopts(parser)
|
|
1503
|
+
>>> args = parser.parse_args(['--windowfunc', 'hann'])
|
|
1504
|
+
>>> args.windowfunc
|
|
1505
|
+
'hann'
|
|
1506
|
+
"""
|
|
552
1507
|
wfunc = parser.add_argument_group("Windowing options")
|
|
553
1508
|
wfunc.add_argument(
|
|
554
1509
|
"--windowfunc",
|
|
@@ -578,7 +1533,43 @@ def addwindowopts(parser, windowtype=DEFAULT_WINDOWFUNC):
|
|
|
578
1533
|
)
|
|
579
1534
|
|
|
580
1535
|
|
|
581
|
-
def addplotopts(parser, multiline=True):
|
|
1536
|
+
def addplotopts(parser: argparse.ArgumentParser, multiline: bool = True) -> None:
|
|
1537
|
+
"""
|
|
1538
|
+
Add general plot appearance options to an argument parser.
|
|
1539
|
+
|
|
1540
|
+
This function adds a set of arguments to the provided `argparse.ArgumentParser`
|
|
1541
|
+
object that control the appearance of plots, including titles, axis labels,
|
|
1542
|
+
legends, colors, line widths, and output options.
|
|
1543
|
+
|
|
1544
|
+
Parameters
|
|
1545
|
+
----------
|
|
1546
|
+
parser : argparse.ArgumentParser
|
|
1547
|
+
The argument parser to which the plot options will be added.
|
|
1548
|
+
multiline : bool, optional
|
|
1549
|
+
If True, allows multiple legends and colors to be specified as comma-separated
|
|
1550
|
+
lists. If False, expects single legend and color values. Default is True.
|
|
1551
|
+
|
|
1552
|
+
Returns
|
|
1553
|
+
-------
|
|
1554
|
+
None
|
|
1555
|
+
This function modifies the parser in place and does not return any value.
|
|
1556
|
+
|
|
1557
|
+
Notes
|
|
1558
|
+
-----
|
|
1559
|
+
The arguments added by this function are grouped under "General plot appearance options".
|
|
1560
|
+
The `multiline` parameter affects the behavior of the `--legends`, `--colors`, and
|
|
1561
|
+
`--linewidth` arguments:
|
|
1562
|
+
|
|
1563
|
+
- When `multiline=True`, these arguments accept comma-separated lists.
|
|
1564
|
+
- When `multiline=False`, they expect single values.
|
|
1565
|
+
|
|
1566
|
+
Examples
|
|
1567
|
+
--------
|
|
1568
|
+
>>> import argparse
|
|
1569
|
+
>>> parser = argparse.ArgumentParser()
|
|
1570
|
+
>>> addplotopts(parser, multiline=True)
|
|
1571
|
+
>>> args = parser.parse_args()
|
|
1572
|
+
"""
|
|
582
1573
|
plotopts = parser.add_argument_group("General plot appearance options")
|
|
583
1574
|
plotopts.add_argument(
|
|
584
1575
|
"--title",
|
|
@@ -729,7 +1720,40 @@ def addplotopts(parser, multiline=True):
|
|
|
729
1720
|
)
|
|
730
1721
|
|
|
731
1722
|
|
|
732
|
-
def addpermutationopts(parser, numreps=10000):
|
|
1723
|
+
def addpermutationopts(parser: argparse.ArgumentParser, numreps: int = 10000) -> None:
|
|
1724
|
+
"""
|
|
1725
|
+
Add permutation testing options to an argument parser.
|
|
1726
|
+
|
|
1727
|
+
This function adds command-line arguments for configuring permutation-based
|
|
1728
|
+
significance testing, including the permutation method and number of null
|
|
1729
|
+
correlations to compute.
|
|
1730
|
+
|
|
1731
|
+
Parameters
|
|
1732
|
+
----------
|
|
1733
|
+
parser : argparse.ArgumentParser
|
|
1734
|
+
The argument parser to which the permutation options will be added.
|
|
1735
|
+
numreps : int, optional
|
|
1736
|
+
Number of null correlations to run for significance estimation.
|
|
1737
|
+
Default is 10000. Set to 0 to disable significance testing.
|
|
1738
|
+
|
|
1739
|
+
Returns
|
|
1740
|
+
-------
|
|
1741
|
+
None
|
|
1742
|
+
This function modifies the parser in-place and does not return any value.
|
|
1743
|
+
|
|
1744
|
+
Notes
|
|
1745
|
+
-----
|
|
1746
|
+
The function adds two main arguments to the parser:
|
|
1747
|
+
1. ``--permutationmethod``: Specifies the permutation method ('shuffle' or 'phaserandom')
|
|
1748
|
+
2. ``--numnull``: Sets the number of null correlations for significance testing
|
|
1749
|
+
|
|
1750
|
+
Examples
|
|
1751
|
+
--------
|
|
1752
|
+
>>> import argparse
|
|
1753
|
+
>>> parser = argparse.ArgumentParser()
|
|
1754
|
+
>>> addpermutationopts(parser, numreps=5000)
|
|
1755
|
+
>>> args = parser.parse_args()
|
|
1756
|
+
"""
|
|
733
1757
|
sigcalc_opts = parser.add_argument_group("Significance calculation options")
|
|
734
1758
|
permutationmethod = sigcalc_opts.add_mutually_exclusive_group()
|
|
735
1759
|
permutationmethod.add_argument(
|
|
@@ -759,7 +1783,53 @@ def addpermutationopts(parser, numreps=10000):
|
|
|
759
1783
|
)
|
|
760
1784
|
|
|
761
1785
|
|
|
762
|
-
def addsearchrangeopts(
|
|
1786
|
+
def addsearchrangeopts(
|
|
1787
|
+
parser: argparse.ArgumentParser,
|
|
1788
|
+
details: bool = False,
|
|
1789
|
+
defaultmin: float = -30.0,
|
|
1790
|
+
defaultmax: float = 30.0,
|
|
1791
|
+
) -> None:
|
|
1792
|
+
"""
|
|
1793
|
+
Add search range options to an argument parser for lag fitting.
|
|
1794
|
+
|
|
1795
|
+
This function adds command-line arguments for specifying the range of lags
|
|
1796
|
+
to consider during fitting operations. It provides options for setting
|
|
1797
|
+
the minimum and maximum lag values, as well as an optional fixed delay
|
|
1798
|
+
parameter.
|
|
1799
|
+
|
|
1800
|
+
Parameters
|
|
1801
|
+
----------
|
|
1802
|
+
parser : argparse.ArgumentParser
|
|
1803
|
+
The argument parser to which the search range options will be added.
|
|
1804
|
+
details : bool, optional
|
|
1805
|
+
If True, adds additional detailed options including --fixdelay.
|
|
1806
|
+
Default is False.
|
|
1807
|
+
defaultmin : float, optional
|
|
1808
|
+
The default minimum lag value in seconds. Default is -30.0.
|
|
1809
|
+
defaultmax : float, optional
|
|
1810
|
+
The default maximum lag value in seconds. Default is 30.0.
|
|
1811
|
+
|
|
1812
|
+
Returns
|
|
1813
|
+
-------
|
|
1814
|
+
None
|
|
1815
|
+
This function modifies the parser in-place and returns None.
|
|
1816
|
+
|
|
1817
|
+
Notes
|
|
1818
|
+
-----
|
|
1819
|
+
The function adds two main arguments:
|
|
1820
|
+
- --searchrange: Specifies the lag range from LAGMIN to LAGMAX
|
|
1821
|
+
- --fixdelay: When details=True, sets a fixed delay time for all voxels
|
|
1822
|
+
|
|
1823
|
+
Examples
|
|
1824
|
+
--------
|
|
1825
|
+
>>> import argparse
|
|
1826
|
+
>>> parser = argparse.ArgumentParser()
|
|
1827
|
+
>>> addsearchrangeopts(parser)
|
|
1828
|
+
>>> args = parser.parse_args(['--searchrange', '-10', '10'])
|
|
1829
|
+
|
|
1830
|
+
>>> addsearchrangeopts(parser, details=True)
|
|
1831
|
+
>>> args = parser.parse_args(['--searchrange', '-5', '5', '--fixdelay', '2.0'])
|
|
1832
|
+
"""
|
|
763
1833
|
parser.add_argument(
|
|
764
1834
|
"--searchrange",
|
|
765
1835
|
dest="lag_extrema",
|
|
@@ -785,7 +1855,60 @@ def addsearchrangeopts(parser, details=False, defaultmin=-30.0, defaultmax=30.0)
|
|
|
785
1855
|
)
|
|
786
1856
|
|
|
787
1857
|
|
|
788
|
-
def postprocesssearchrangeopts(args):
|
|
1858
|
+
def postprocesssearchrangeopts(args: Namespace) -> Namespace:
|
|
1859
|
+
"""
|
|
1860
|
+
Post-process search range options for delay estimation.
|
|
1861
|
+
|
|
1862
|
+
This function handles additional argument parsing for delay estimation parameters
|
|
1863
|
+
that cannot be handled by argparse alone. It processes initial delay values and
|
|
1864
|
+
sets appropriate lag extrema for search range optimization.
|
|
1865
|
+
|
|
1866
|
+
Parameters
|
|
1867
|
+
----------
|
|
1868
|
+
args : argparse.Namespace
|
|
1869
|
+
Namespace containing command line arguments. Expected attributes include:
|
|
1870
|
+
- initialdelayvalue : float, optional
|
|
1871
|
+
Initial delay value to fix for delay estimation
|
|
1872
|
+
- lag_extrema : tuple of float
|
|
1873
|
+
Tuple containing (lag_min, lag_max) for search range
|
|
1874
|
+
- lag_extrema_nondefault : tuple of float, optional
|
|
1875
|
+
Non-default lag extrema values to override defaults
|
|
1876
|
+
|
|
1877
|
+
Returns
|
|
1878
|
+
-------
|
|
1879
|
+
argparse.Namespace
|
|
1880
|
+
Updated args namespace with processed delay and search range parameters.
|
|
1881
|
+
Adds/updates the following attributes:
|
|
1882
|
+
- fixdelay : bool
|
|
1883
|
+
Flag indicating if delay was fixed
|
|
1884
|
+
- lagmin : float
|
|
1885
|
+
Minimum lag value for search range
|
|
1886
|
+
- lagmax : float
|
|
1887
|
+
Maximum lag value for search range
|
|
1888
|
+
- lagmin_nondefault : bool
|
|
1889
|
+
Flag indicating if lagmin was overridden from default
|
|
1890
|
+
- lagmax_nondefault : bool
|
|
1891
|
+
Flag indicating if lagmax was overridden from default
|
|
1892
|
+
|
|
1893
|
+
Notes
|
|
1894
|
+
-----
|
|
1895
|
+
If initialdelayvalue is provided, the function sets fixdelay=True and creates
|
|
1896
|
+
a search range centered around the initial delay value with ±10.0 range.
|
|
1897
|
+
|
|
1898
|
+
Examples
|
|
1899
|
+
--------
|
|
1900
|
+
>>> import argparse
|
|
1901
|
+
>>> args = argparse.Namespace()
|
|
1902
|
+
>>> args.initialdelayvalue = 5.0
|
|
1903
|
+
>>> args.lag_extrema = (-10.0, 10.0)
|
|
1904
|
+
>>> result = postprocesssearchrangeopts(args)
|
|
1905
|
+
>>> print(result.fixdelay)
|
|
1906
|
+
True
|
|
1907
|
+
>>> print(result.lagmin)
|
|
1908
|
+
-5.0
|
|
1909
|
+
>>> print(result.lagmax)
|
|
1910
|
+
15.0
|
|
1911
|
+
"""
|
|
789
1912
|
# Additional argument parsing not handled by argparse
|
|
790
1913
|
# first handle fixed delay
|
|
791
1914
|
try:
|
|
@@ -810,7 +1933,45 @@ def postprocesssearchrangeopts(args):
|
|
|
810
1933
|
return args
|
|
811
1934
|
|
|
812
1935
|
|
|
813
|
-
def addtimerangeopts(parser):
|
|
1936
|
+
def addtimerangeopts(parser: argparse.ArgumentParser) -> None:
|
|
1937
|
+
"""
|
|
1938
|
+
Add time range arguments to an argument parser for timepoint filtering.
|
|
1939
|
+
|
|
1940
|
+
This function adds a command-line argument '--timerange' that allows users
|
|
1941
|
+
to specify a range of timepoints to include in the analysis. The argument
|
|
1942
|
+
accepts two integer values representing the start and end timepoints.
|
|
1943
|
+
|
|
1944
|
+
Parameters
|
|
1945
|
+
----------
|
|
1946
|
+
parser : argparse.ArgumentParser
|
|
1947
|
+
The argument parser to which the time range arguments will be added.
|
|
1948
|
+
|
|
1949
|
+
Returns
|
|
1950
|
+
-------
|
|
1951
|
+
None
|
|
1952
|
+
This function modifies the parser in-place and does not return any value.
|
|
1953
|
+
|
|
1954
|
+
Notes
|
|
1955
|
+
-----
|
|
1956
|
+
- The time range is inclusive on both ends
|
|
1957
|
+
- If END is set to -1, analysis will continue to the last timepoint
|
|
1958
|
+
- Negative values for START will be automatically set to 0
|
|
1959
|
+
- Default behavior (no timerange specified) is to use all timepoints
|
|
1960
|
+
- Both START and END must be non-negative integers when not equal to -1
|
|
1961
|
+
|
|
1962
|
+
Examples
|
|
1963
|
+
--------
|
|
1964
|
+
>>> import argparse
|
|
1965
|
+
>>> parser = argparse.ArgumentParser()
|
|
1966
|
+
>>> addtimerangeopts(parser)
|
|
1967
|
+
>>> args = parser.parse_args(['--timerange', '10', '50'])
|
|
1968
|
+
>>> args.timerange
|
|
1969
|
+
(10, 50)
|
|
1970
|
+
|
|
1971
|
+
>>> args = parser.parse_args(['--timerange', '0', '-1'])
|
|
1972
|
+
>>> args.timerange
|
|
1973
|
+
(0, -1)
|
|
1974
|
+
"""
|
|
814
1975
|
parser.add_argument(
|
|
815
1976
|
"--timerange",
|
|
816
1977
|
dest="timerange",
|
|
@@ -828,7 +1989,53 @@ def addtimerangeopts(parser):
|
|
|
828
1989
|
)
|
|
829
1990
|
|
|
830
1991
|
|
|
831
|
-
def postprocesstimerangeopts(args):
|
|
1992
|
+
def postprocesstimerangeopts(args: Namespace) -> Namespace:
|
|
1993
|
+
"""
|
|
1994
|
+
Process timerange options and set start and end points for time range filtering.
|
|
1995
|
+
|
|
1996
|
+
This function takes a namespace object containing timerange information and
|
|
1997
|
+
converts it into startpoint and endpoint values that can be used for time
|
|
1998
|
+
range filtering operations. The timerange is expected to be a list or tuple
|
|
1999
|
+
with two elements: [start_time, end_time].
|
|
2000
|
+
|
|
2001
|
+
Parameters
|
|
2002
|
+
----------
|
|
2003
|
+
args : Namespace
|
|
2004
|
+
A namespace object containing a 'timerange' attribute. The timerange
|
|
2005
|
+
should be a list or tuple of two elements: [start_time, end_time].
|
|
2006
|
+
The end_time can be -1 to indicate no upper bound.
|
|
2007
|
+
|
|
2008
|
+
Returns
|
|
2009
|
+
-------
|
|
2010
|
+
Namespace
|
|
2011
|
+
The same namespace object with added 'startpoint' and 'endpoint' attributes
|
|
2012
|
+
set based on the timerange values.
|
|
2013
|
+
|
|
2014
|
+
Notes
|
|
2015
|
+
-----
|
|
2016
|
+
- If the end_time in timerange is -1, the endpoint is set to 10000000000
|
|
2017
|
+
- Both startpoint and endpoint are converted to integers
|
|
2018
|
+
- The function modifies the input namespace in-place and returns it
|
|
2019
|
+
|
|
2020
|
+
Examples
|
|
2021
|
+
--------
|
|
2022
|
+
>>> from types import Namespace
|
|
2023
|
+
>>> args = Namespace()
|
|
2024
|
+
>>> args.timerange = [100, 200]
|
|
2025
|
+
>>> result = postprocesstimerangeopts(args)
|
|
2026
|
+
>>> print(result.startpoint)
|
|
2027
|
+
100
|
|
2028
|
+
>>> print(result.endpoint)
|
|
2029
|
+
200
|
|
2030
|
+
|
|
2031
|
+
>>> args = Namespace()
|
|
2032
|
+
>>> args.timerange = [50, -1]
|
|
2033
|
+
>>> result = postprocesstimerangeopts(args)
|
|
2034
|
+
>>> print(result.startpoint)
|
|
2035
|
+
50
|
|
2036
|
+
>>> print(result.endpoint)
|
|
2037
|
+
10000000000
|
|
2038
|
+
"""
|
|
832
2039
|
args.startpoint = int(args.timerange[0])
|
|
833
2040
|
if args.timerange[1] == -1:
|
|
834
2041
|
args.endpoint = 10000000000
|
|
@@ -837,7 +2044,54 @@ def postprocesstimerangeopts(args):
|
|
|
837
2044
|
return args
|
|
838
2045
|
|
|
839
2046
|
|
|
840
|
-
def parserange(
|
|
2047
|
+
def parserange(
|
|
2048
|
+
timerange: Tuple[int, int], descriptor: str = "timerange", debug: bool = False
|
|
2049
|
+
) -> Tuple[int, int]:
|
|
2050
|
+
"""
|
|
2051
|
+
Parse and validate a time range tuple, ensuring valid start and end points.
|
|
2052
|
+
|
|
2053
|
+
This function processes a time range tuple by handling negative values and
|
|
2054
|
+
validating the relationship between start and end points. Negative start values
|
|
2055
|
+
are set to 0, and negative end values are set to 100000000. The function ensures
|
|
2056
|
+
that the start point is strictly less than the end point.
|
|
2057
|
+
|
|
2058
|
+
Parameters
|
|
2059
|
+
----------
|
|
2060
|
+
timerange : tuple of int
|
|
2061
|
+
A tuple containing (start, end) time points. Negative values are handled
|
|
2062
|
+
according to the function's logic.
|
|
2063
|
+
descriptor : str, optional
|
|
2064
|
+
A descriptive name used in error messages, default is "timerange".
|
|
2065
|
+
debug : bool, optional
|
|
2066
|
+
If True, prints debug information including startpoint, endpoint, and
|
|
2067
|
+
original timerange values, default is False.
|
|
2068
|
+
|
|
2069
|
+
Returns
|
|
2070
|
+
-------
|
|
2071
|
+
tuple of int
|
|
2072
|
+
A validated tuple containing (startpoint, endpoint) where both values
|
|
2073
|
+
are non-negative and startpoint < endpoint.
|
|
2074
|
+
|
|
2075
|
+
Raises
|
|
2076
|
+
------
|
|
2077
|
+
ValueError
|
|
2078
|
+
If the calculated startpoint is greater than or equal to the calculated
|
|
2079
|
+
endpoint.
|
|
2080
|
+
|
|
2081
|
+
Examples
|
|
2082
|
+
--------
|
|
2083
|
+
>>> parserange((10, 20))
|
|
2084
|
+
(10, 20)
|
|
2085
|
+
|
|
2086
|
+
>>> parserange((-5, 15))
|
|
2087
|
+
(0, 15)
|
|
2088
|
+
|
|
2089
|
+
>>> parserange((10, -5))
|
|
2090
|
+
(10, 100000000)
|
|
2091
|
+
|
|
2092
|
+
>>> parserange((20, 10))
|
|
2093
|
+
ValueError: timerange startpoint must be < endpoint
|
|
2094
|
+
"""
|
|
841
2095
|
if timerange[0] < 0:
|
|
842
2096
|
startpoint = 0
|
|
843
2097
|
else:
|
|
@@ -855,7 +2109,39 @@ def parserange(timerange, descriptor="timerange", debug=False):
|
|
|
855
2109
|
return startpoint, endpoint
|
|
856
2110
|
|
|
857
2111
|
|
|
858
|
-
def addsimilarityopts(parser):
|
|
2112
|
+
def addsimilarityopts(parser: argparse.ArgumentParser) -> None:
|
|
2113
|
+
"""
|
|
2114
|
+
Add mutual information similarity options to an argument parser.
|
|
2115
|
+
|
|
2116
|
+
This function adds a command-line argument for specifying the time constant
|
|
2117
|
+
of temporal smoothing to apply to the mutual information function. The
|
|
2118
|
+
smoothing helps reduce noise in the similarity calculations.
|
|
2119
|
+
|
|
2120
|
+
Parameters
|
|
2121
|
+
----------
|
|
2122
|
+
parser : argparse.ArgumentParser
|
|
2123
|
+
The argument parser to which the mutual information smoothing options will be added.
|
|
2124
|
+
|
|
2125
|
+
Returns
|
|
2126
|
+
-------
|
|
2127
|
+
None
|
|
2128
|
+
This function modifies the parser in-place and does not return any value.
|
|
2129
|
+
|
|
2130
|
+
Notes
|
|
2131
|
+
-----
|
|
2132
|
+
The smoothing time constant (TAU) controls the temporal smoothing of the
|
|
2133
|
+
mutual information function. When TAU <= 0.0, smoothing is disabled.
|
|
2134
|
+
Default value is 3.0 seconds.
|
|
2135
|
+
|
|
2136
|
+
Examples
|
|
2137
|
+
--------
|
|
2138
|
+
>>> import argparse
|
|
2139
|
+
>>> parser = argparse.ArgumentParser()
|
|
2140
|
+
>>> addsimilarityopts(parser)
|
|
2141
|
+
>>> args = parser.parse_args(['--mutualinfosmoothingtime', '2.5'])
|
|
2142
|
+
>>> args.smoothingtime
|
|
2143
|
+
2.5
|
|
2144
|
+
"""
|
|
859
2145
|
parser.add_argument(
|
|
860
2146
|
"--mutualinfosmoothingtime",
|
|
861
2147
|
dest="smoothingtime",
|
|
@@ -871,9 +2157,61 @@ def addsimilarityopts(parser):
|
|
|
871
2157
|
)
|
|
872
2158
|
|
|
873
2159
|
|
|
874
|
-
def setargs(
|
|
2160
|
+
def setargs(
|
|
2161
|
+
thegetparserfunc: Callable[[], argparse.ArgumentParser],
|
|
2162
|
+
inputargs: Optional[List[str]] = None,
|
|
2163
|
+
) -> Tuple[Namespace, List[str]]:
|
|
875
2164
|
"""
|
|
876
2165
|
Compile arguments for rapidtide workflow.
|
|
2166
|
+
|
|
2167
|
+
This function processes command line arguments or a provided argument list
|
|
2168
|
+
using an argument parser function. It handles both interactive command line
|
|
2169
|
+
argument parsing and programmatic argument parsing with error handling.
|
|
2170
|
+
|
|
2171
|
+
Parameters
|
|
2172
|
+
----------
|
|
2173
|
+
thegetparserfunc : callable
|
|
2174
|
+
A function that returns an argparse.ArgumentParser object. This function
|
|
2175
|
+
should not take any arguments and should return a configured parser.
|
|
2176
|
+
inputargs : list of str, optional
|
|
2177
|
+
List of arguments to parse. If None (default), arguments are parsed
|
|
2178
|
+
from sys.argv. Default is None.
|
|
2179
|
+
|
|
2180
|
+
Returns
|
|
2181
|
+
-------
|
|
2182
|
+
tuple
|
|
2183
|
+
A tuple containing:
|
|
2184
|
+
- args : argparse.Namespace
|
|
2185
|
+
Parsed arguments as a namespace object
|
|
2186
|
+
- argstowrite : list of str
|
|
2187
|
+
The argument list that was parsed (either sys.argv or inputargs)
|
|
2188
|
+
|
|
2189
|
+
Raises
|
|
2190
|
+
------
|
|
2191
|
+
SystemExit
|
|
2192
|
+
If the argument parser encounters an error or --help option is used.
|
|
2193
|
+
|
|
2194
|
+
Notes
|
|
2195
|
+
-----
|
|
2196
|
+
This function provides a unified interface for argument parsing in the
|
|
2197
|
+
rapidtide workflow, supporting both command line and programmatic usage.
|
|
2198
|
+
When --help is invoked, a helpful message is displayed before raising
|
|
2199
|
+
SystemExit.
|
|
2200
|
+
|
|
2201
|
+
Examples
|
|
2202
|
+
--------
|
|
2203
|
+
>>> from argparse import ArgumentParser
|
|
2204
|
+
>>> def get_parser():
|
|
2205
|
+
... parser = ArgumentParser()
|
|
2206
|
+
... parser.add_argument('--input', help='Input file')
|
|
2207
|
+
... return parser
|
|
2208
|
+
>>> args, arglist = setargs(get_parser)
|
|
2209
|
+
>>> args.input
|
|
2210
|
+
None
|
|
2211
|
+
|
|
2212
|
+
>>> args, arglist = setargs(get_parser, ['--input', 'test.txt'])
|
|
2213
|
+
>>> args.input
|
|
2214
|
+
'test.txt'
|
|
877
2215
|
"""
|
|
878
2216
|
if inputargs is None:
|
|
879
2217
|
# get arguments from the command line
|
|
@@ -898,9 +2236,52 @@ def setargs(thegetparserfunc, inputargs=None):
|
|
|
898
2236
|
return args, argstowrite
|
|
899
2237
|
|
|
900
2238
|
|
|
901
|
-
def generic_init(
|
|
2239
|
+
def generic_init(
|
|
2240
|
+
theparser: Callable[[], argparse.ArgumentParser],
|
|
2241
|
+
themain: Callable[[Namespace], None],
|
|
2242
|
+
inputargs: Optional[List[str]] = None,
|
|
2243
|
+
) -> None:
|
|
902
2244
|
"""
|
|
903
2245
|
Compile arguments either from the command line, or from an argument list.
|
|
2246
|
+
|
|
2247
|
+
This function serves as a generic initialization wrapper that handles
|
|
2248
|
+
argument parsing for command-line applications. It can parse arguments
|
|
2249
|
+
from either sys.argv or a provided list of arguments, and then executes
|
|
2250
|
+
the main function with the parsed arguments.
|
|
2251
|
+
|
|
2252
|
+
Parameters
|
|
2253
|
+
----------
|
|
2254
|
+
theparser : Callable[[], argparse.ArgumentParser]
|
|
2255
|
+
A callable that returns an argparse.ArgumentParser instance
|
|
2256
|
+
configured with the desired command-line arguments.
|
|
2257
|
+
themain : Callable[[Namespace], None]
|
|
2258
|
+
A callable that takes a parsed arguments Namespace object
|
|
2259
|
+
and performs the main application logic.
|
|
2260
|
+
inputargs : List[str], optional
|
|
2261
|
+
List of argument strings to parse. If None, arguments are parsed
|
|
2262
|
+
from sys.argv. Default is None.
|
|
2263
|
+
|
|
2264
|
+
Returns
|
|
2265
|
+
-------
|
|
2266
|
+
None
|
|
2267
|
+
This function does not return any value.
|
|
2268
|
+
|
|
2269
|
+
Notes
|
|
2270
|
+
-----
|
|
2271
|
+
The function automatically saves the raw command line as an attribute
|
|
2272
|
+
`commandline` on the parsed arguments object for later use.
|
|
2273
|
+
|
|
2274
|
+
Examples
|
|
2275
|
+
--------
|
|
2276
|
+
>>> def create_parser():
|
|
2277
|
+
... parser = argparse.ArgumentParser()
|
|
2278
|
+
... parser.add_argument('--verbose', action='store_true')
|
|
2279
|
+
... return parser
|
|
2280
|
+
...
|
|
2281
|
+
>>> def main_func(args):
|
|
2282
|
+
... print(f"Verbose mode: {args.verbose}")
|
|
2283
|
+
...
|
|
2284
|
+
>>> generic_init(create_parser, main_func)
|
|
904
2285
|
"""
|
|
905
2286
|
if inputargs is None:
|
|
906
2287
|
print("processing command line arguments")
|