Skip to content

Commit aa73f3a

Browse files
committed
moving get_fmap_type to utils
1 parent e04eeb3 commit aa73f3a

File tree

2 files changed

+64
-60
lines changed

2 files changed

+64
-60
lines changed

CPAC/utils/datasource.py

Lines changed: 2 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -377,67 +377,9 @@ def create_fmap_datasource(fmap_dct, wf_name="fmap_datasource"):
377377

378378
def get_fmap_phasediff_metadata(data_config_scan_params):
379379
"""Return the scan parameters for a field map phasediff scan."""
380-
def get_fmap_type(metadata):
381-
"""Determine the type of field map from metadata.
382-
383-
reference: https://bids-specification.readthedocs.io/en/latest/modality-specific-files/magnetic-resonance-imaging-data.html#case-1-phase-difference-map-and-at-least-one-magnitude-image
384-
385-
Parameters
386-
----------
387-
metadata : dict or str
388-
Metadata dictionary or path to a JSON file containing metadata.
389-
390-
Returns
391-
-------
392-
str or None
393-
Returns the type of field map as a string:
394-
- "phasediff" for phase difference maps with two echo times
395-
- "phase" for single echo phase maps
396-
- "fieldmap" for field maps with units like Hz, rad/s, T, or Tesla
397-
- "epi" for EPI field maps with phase encoding direction
398-
"""
399-
400-
if not isinstance(metadata, dict):
401-
if isinstance(metadata, str) and ".json" in metadata:
402-
import json
403-
404-
try:
405-
with open(metadata, "r", encoding="utf-8") as f:
406-
metadata = json.load(f)
407-
except (FileNotFoundError, json.JSONDecodeError):
408-
return None
409-
else:
410-
return None
411-
412-
# Check for required BIDS fields only
413-
match (
414-
"EchoTime1" in metadata,
415-
"EchoTime2" in metadata,
416-
"EchoTime" in metadata,
417-
"Units" in metadata,
418-
"PhaseEncodingDirection" in metadata,
419-
):
420-
case (True, True, _, _, _):
421-
# Case 1: Phase-difference map (REQUIRED: EchoTime1 AND EchoTime2)
422-
return "phasediff"
423-
case (False, False, True, _, _):
424-
# Case 2: Single phase map (REQUIRED: EchoTime, but NOT EchoTime1/2)
425-
return "phase"
426-
case (_, _, _, True, _):
427-
# Case 3: Direct field mapping (REQUIRED: Units)
428-
units = metadata["Units"].lower()
429-
if units in ["hz", "rad/s", "t", "tesla", "hertz"]:
430-
return "fieldmap"
431-
case (_, _, _, _, True):
432-
# Case 4: EPI field maps (REQUIRED: PhaseEncodingDirection)
433-
pe_dir = metadata["PhaseEncodingDirection"]
434-
if pe_dir in ["i", "i-", "j", "j-", "k", "k-"]:
435-
return "epi"
436-
case _:
437-
return None
438-
439-
return None
440380

381+
from CPAC.utils.utils import get_fmap_type
382+
441383
if (
442384
not isinstance(data_config_scan_params, dict)
443385
and ".json" in data_config_scan_params

CPAC/utils/utils.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,3 +2657,65 @@ def flip_orientation_code(code):
26572657
"""Reverts an orientation code by flipping R↔L, A↔P, and I↔S."""
26582658
flip_dict = {"R": "L", "L": "R", "A": "P", "P": "A", "I": "S", "S": "I"}
26592659
return "".join(flip_dict[c] for c in code)
2660+
2661+
2662+
def get_fmap_type(metadata):
2663+
"""Determine the type of field map from metadata.
2664+
2665+
reference: https://bids-specification.readthedocs.io/en/latest/modality-specific-files/magnetic-resonance-imaging-data.html#case-1-phase-difference-map-and-at-least-one-magnitude-image
2666+
2667+
Parameters
2668+
----------
2669+
metadata : dict or str
2670+
Metadata dictionary or path to a JSON file containing metadata.
2671+
2672+
Returns
2673+
-------
2674+
str or None
2675+
Returns the type of field map as a string:
2676+
- "phasediff" for phase difference maps with two echo times
2677+
- "phase" for single echo phase maps
2678+
- "fieldmap" for field maps with units like Hz, rad/s, T, or Tesla
2679+
- "epi" for EPI field maps with phase encoding direction
2680+
"""
2681+
2682+
if not isinstance(metadata, dict):
2683+
if isinstance(metadata, str) and ".json" in metadata:
2684+
import json
2685+
2686+
try:
2687+
with open(metadata, "r", encoding="utf-8") as f:
2688+
metadata = json.load(f)
2689+
except (FileNotFoundError, json.JSONDecodeError):
2690+
return None
2691+
else:
2692+
return None
2693+
2694+
# Check for required BIDS fields only
2695+
match (
2696+
"EchoTime1" in metadata,
2697+
"EchoTime2" in metadata,
2698+
"EchoTime" in metadata,
2699+
"Units" in metadata,
2700+
"PhaseEncodingDirection" in metadata,
2701+
):
2702+
case (True, True, _, _, _):
2703+
# Case 1: Phase-difference map (REQUIRED: EchoTime1 AND EchoTime2)
2704+
return "phasediff"
2705+
case (False, False, True, _, _):
2706+
# Case 2: Single phase map (REQUIRED: EchoTime, but NOT EchoTime1/2)
2707+
return "phase"
2708+
case (_, _, _, True, _):
2709+
# Case 3: Direct field mapping (REQUIRED: Units)
2710+
units = metadata["Units"].lower()
2711+
if units in ["hz", "rad/s", "t", "tesla", "hertz"]:
2712+
return "fieldmap"
2713+
case (_, _, _, _, True):
2714+
# Case 4: EPI field maps (REQUIRED: PhaseEncodingDirection)
2715+
pe_dir = metadata["PhaseEncodingDirection"]
2716+
if pe_dir in ["i", "i-", "j", "j-", "k", "k-"]:
2717+
return "epi"
2718+
case _:
2719+
return None
2720+
2721+
return None

0 commit comments

Comments
 (0)