|
27 | 27 | from contextlib import suppress
|
28 | 28 | from pathlib import Path
|
29 | 29 | from typing import Optional, Union, List
|
30 |
| -from bids.layout import BIDSLayout |
| 30 | +from bids.layout import BIDSLayout, BIDSFile |
31 | 31 | from bids.utils import listify
|
32 | 32 |
|
33 | 33 | from .. import fieldmaps as fm
|
@@ -317,12 +317,20 @@ def find_estimators(
|
317 | 317 | B0FieldIdentifier=f'"{b0_id}"', # Double quotes to match JSON, not Python repr
|
318 | 318 | regex_search=True,
|
319 | 319 | )
|
320 |
| - e = fm.FieldmapEstimation([ |
321 |
| - fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata()) |
322 |
| - for fmap in bare_ids + listed_ids |
323 |
| - ]) |
324 |
| - _log_debug_estimation(logger, e, layout.root) |
325 |
| - estimators.append(e) |
| 320 | + try: |
| 321 | + e = fm.FieldmapEstimation( |
| 322 | + [ |
| 323 | + fm.FieldmapFile(fmap.path, metadata=fmap.get_metadata()) |
| 324 | + for fmap in bare_ids + listed_ids |
| 325 | + ] |
| 326 | + ) |
| 327 | + except ValueError as err: |
| 328 | + _log_debug_estimator_fail( |
| 329 | + logger, b0_id, listed_ids, layout.root, str(err) |
| 330 | + ) |
| 331 | + else: |
| 332 | + _log_debug_estimation(logger, e, layout.root) |
| 333 | + estimators.append(e) |
326 | 334 |
|
327 | 335 | # Step 2. If no B0FieldIdentifiers were found, try several heuristics
|
328 | 336 | if not estimators:
|
@@ -484,3 +492,20 @@ def _log_debug_estimation(
|
484 | 492 | len(estimation.sources),
|
485 | 493 | "\n- ".join([str(s.path.relative_to(bids_root)) for s in estimation.sources]),
|
486 | 494 | )
|
| 495 | + |
| 496 | + |
| 497 | +def _log_debug_estimator_fail( |
| 498 | + logger: logging.Logger, |
| 499 | + b0_id: str, |
| 500 | + files: List[BIDSFile], |
| 501 | + bids_root: str, |
| 502 | + message: str |
| 503 | +) -> None: |
| 504 | + """A helper function to log failures to build an estimator when running with verbosity.""" |
| 505 | + logger.debug( |
| 506 | + "Failed to construct %s estimation from %d sources:\n- %s\nError: %s", |
| 507 | + b0_id, |
| 508 | + len(files), |
| 509 | + "\n- ".join([str(s.path.relative_to(bids_root)) for s in files]), |
| 510 | + message, |
| 511 | + ) |
0 commit comments