66from typing import TYPE_CHECKING
77from unittest .mock import MagicMock , patch
88
9+ import nibabel as nib
910import pytest
1011
1112from rbc .metadata import (
@@ -146,14 +147,15 @@ def test_load_with_override(self, tmp_path: Path) -> None:
146147 assert meta .tr == pytest .approx (1.5 )
147148
148149 def test_load_no_slice_timing (self , tmp_path : Path ) -> None :
149- """load() sets slice_timing to None when absent from sidecar."""
150+ """load() sets slice_timing to None when absent from sidecar and header ."""
150151 bold = tmp_path / "bold.nii.gz"
151152 bold .touch ()
152153
153154 mock_hdr = MagicMock ()
154155 mock_hdr .__getitem__ = lambda _ , key : (
155156 [0 , 1 , 1 , 1 , 2.0 ] if key == "pixdim" else None
156157 )
158+ mock_hdr .get_slice_times .side_effect = nib .spatialimages .HeaderDataError ()
157159 mock_img = MagicMock ()
158160 mock_img .header = mock_hdr
159161
@@ -168,6 +170,32 @@ def test_load_no_slice_timing(self, tmp_path: Path) -> None:
168170
169171 assert meta .slice_timing is None
170172
173+ def test_load_slice_timing_from_header (self , tmp_path : Path ) -> None :
174+ """load() falls back to NIfTI header when sidecar lacks SliceTiming."""
175+ bold = tmp_path / "bold.nii.gz"
176+ bold .touch ()
177+
178+ header_times = [0.0 , 0.5 , 1.0 , 1.5 ]
179+
180+ mock_hdr = MagicMock ()
181+ mock_hdr .__getitem__ = lambda _ , key : (
182+ [0 , 1 , 1 , 1 , 2.0 ] if key == "pixdim" else None
183+ )
184+ mock_hdr .get_slice_times .return_value = header_times
185+ mock_img = MagicMock ()
186+ mock_img .header = mock_hdr
187+
188+ with (
189+ patch (
190+ "rbc.metadata.load_bids_metadata" ,
191+ return_value = {"RepetitionTime" : 2.0 },
192+ ),
193+ patch ("rbc.metadata.nib.nifti1.load" , return_value = mock_img ),
194+ ):
195+ meta = FunctionalMetadata .load (bold )
196+
197+ assert meta .slice_timing == header_times
198+
171199 def test_load_missing_tr_raises (self , tmp_path : Path ) -> None :
172200 """load() raises when TR cannot be determined."""
173201 bold = tmp_path / "bold.nii.gz"
0 commit comments