|
24 | 24 |
|
25 | 25 | import random |
26 | 26 | from itertools import chain, zip_longest |
27 | | -from typing import Iterator |
| 27 | +from typing import Iterator, Sequence |
28 | 28 |
|
29 | 29 | DEFAULT_ROUND_DECIMALS = 2 |
30 | 30 | """Round decimals to use when comparing values to be sorted for iteration purposes.""" |
@@ -172,14 +172,14 @@ def random_iterator(**kwargs) -> Iterator[int]: |
172 | 172 |
|
173 | 173 |
|
174 | 174 | def _value_iterator( |
175 | | - values: list, ascending: bool, round_decimals: int = DEFAULT_ROUND_DECIMALS |
| 175 | + values: Sequence[float], ascending: bool, round_decimals: int = DEFAULT_ROUND_DECIMALS |
176 | 176 | ) -> Iterator[int]: |
177 | 177 | """ |
178 | 178 | Traverse the given values in ascending or descenting order. |
179 | 179 |
|
180 | 180 | Parameters |
181 | 181 | ---------- |
182 | | - values : :obj:`list` |
| 182 | + values : :obj:`Sequence` |
183 | 183 | List of values to traverse. |
184 | 184 | ascending : :obj:`bool` |
185 | 185 | If ``True``, traverse in ascending order; traverse in descending order |
@@ -211,70 +211,57 @@ def _value_iterator( |
211 | 211 | return (index[1] for index in indexed_vals) |
212 | 212 |
|
213 | 213 |
|
214 | | -def bvalue_iterator(*_, **kwargs) -> Iterator[int]: |
215 | | - """ |
216 | | - Traverse the volumes in a DWI dataset by increasing b-value. |
217 | | -
|
218 | | - Parameters |
219 | | - ---------- |
220 | | - bvals : :obj:`list` |
221 | | - List of b-values corresponding to all orientations of the dataset. |
222 | | - Please note that ``bvals`` is a keyword argument and MUST be provided |
223 | | - to generate the volume sequence. |
224 | | -
|
225 | | - Yields |
226 | | - ------ |
227 | | - :obj:`int` |
228 | | - The next index. |
| 214 | +def monotonic_value_iterator(*_, **kwargs) -> Iterator[int]: |
| 215 | + try: |
| 216 | + feature = next(k for k in (BVALS_KWARG, UPTAKE_KWARG) if kwargs.get(k) is not None) |
| 217 | + except StopIteration: |
| 218 | + raise TypeError(KWARG_ERROR_MSG.format(kwarg=f"{BVALS_KWARG} or {UPTAKE_KWARG}")) |
229 | 219 |
|
230 | | - Examples |
231 | | - -------- |
232 | | - >>> list(bvalue_iterator(bvals=[0.0, 0.0, 1000.0, 1000.0, 700.0, 700.0, 2000.0, 2000.0, 0.0])) |
233 | | - [0, 1, 8, 4, 5, 2, 3, 6, 7] |
234 | | -
|
235 | | - """ |
236 | | - bvals = kwargs.pop(BVALS_KWARG, None) |
237 | | - if bvals is None: |
238 | | - raise TypeError(KWARG_ERROR_MSG.format(kwarg=BVALS_KWARG)) |
| 220 | + ascending = feature == BVALS_KWARG |
| 221 | + values = kwargs[feature] |
239 | 222 | return _value_iterator( |
240 | | - bvals, ascending=True, round_decimals=kwargs.pop("round_decimals", DEFAULT_ROUND_DECIMALS) |
| 223 | + values, |
| 224 | + ascending=ascending, |
| 225 | + round_decimals=kwargs.get("round_decimals", DEFAULT_ROUND_DECIMALS), |
241 | 226 | ) |
242 | 227 |
|
243 | 228 |
|
244 | | -def uptake_iterator(*_, **kwargs) -> Iterator[int]: |
245 | | - """ |
246 | | - Traverse the volumes in a PET dataset by decreasing uptake value. |
| 229 | +monotonic_value_iterator.__doc__ = f""" |
| 230 | +Traverse the volumes by increasing b-value in a DWI dataset or by decreasing |
| 231 | +uptake value in a PET dataset. |
247 | 232 |
|
248 | | - This function assumes that each uptake value corresponds to a single volume, |
249 | | - and that this value summarizes the uptake of the volume in a meaningful way, |
250 | | - e.g. a mean value across the entire volume. |
| 233 | +This function requires ``bvals`` or ``uptake`` be a keyword argument to generate |
| 234 | +the volume sequence. The b-values are assumed to all orientations in a DWI |
| 235 | +dataset, and uptake uptake values correspond to all volumes in a PET dataset. |
251 | 236 |
|
252 | | - Parameters |
253 | | - ---------- |
254 | | - uptake : :obj:`list` |
255 | | - List of uptake values corresponding to all volumes of the dataset. |
256 | | - Please note that ``uptake`` is a keyword argument and MUST be provided |
257 | | - to generate the volume sequence. |
| 237 | +It is assumed that each uptake value corresponds to a single volume, and that |
| 238 | +this value summarizes the uptake of the volume in a meaningful way, e.g. a mean |
| 239 | +value across the entire volume. |
258 | 240 |
|
259 | | - Yields |
260 | | - ------ |
261 | | - :obj:`int` |
262 | | - The next index. |
| 241 | +Other Parameters |
| 242 | +---------------- |
| 243 | +{SIZE_KEYS_DOC} |
263 | 244 |
|
264 | | - Examples |
265 | | - -------- |
266 | | - >>> list(uptake_iterator(uptake=[-1.23, 1.06, 1.02, 1.38, -1.46, -1.12, -1.19, 1.24, 1.05])) |
267 | | - [3, 7, 1, 8, 2, 5, 6, 0, 4] |
| 245 | +Notes |
| 246 | +----- |
| 247 | +Only one of the above keyword arguments may be provided at a time. If ``size`` |
| 248 | +is given, all other size-related keyword arguments will be ignored. If ``size`` |
| 249 | +is not provided, the function will attempt to infer the number of volumes from |
| 250 | +the length or value of the provided keyword argument. If more than one such |
| 251 | +keyword is provided, a :exc:`ValueError` will be raised. |
268 | 252 |
|
269 | | - """ |
270 | | - uptake = kwargs.pop(UPTAKE_KWARG, None) |
271 | | - if uptake is None: |
272 | | - raise TypeError(KWARG_ERROR_MSG.format(kwarg=UPTAKE_KWARG)) |
273 | | - return _value_iterator( |
274 | | - uptake, |
275 | | - ascending=False, |
276 | | - round_decimals=kwargs.pop("round_decimals", DEFAULT_ROUND_DECIMALS), |
277 | | - ) |
| 253 | +Yields |
| 254 | +------ |
| 255 | +:obj:`int` |
| 256 | + The next index. |
| 257 | +
|
| 258 | +Examples |
| 259 | +-------- |
| 260 | +>>> list(monotonic_value_iterator(bvals=[0.0, 0.0, 1000.0, 1000.0, 700.0, 700.0, 2000.0, 2000.0, 0.0])) |
| 261 | +[0, 1, 8, 4, 5, 2, 3, 6, 7] |
| 262 | +>>> list(monotonic_value_iterator(uptake=[-1.23, 1.06, 1.02, 1.38, -1.46, -1.12, -1.19, 1.24, 1.05])) |
| 263 | +[3, 7, 1, 8, 2, 5, 6, 0, 4] |
| 264 | +""" |
278 | 265 |
|
279 | 266 |
|
280 | 267 | def centralsym_iterator(**kwargs) -> Iterator[int]: |
|
0 commit comments