Skip to content

Commit

Permalink
Merge pull request #347 from EIT-ALIVE/217_detect_sample_frequency_dr…
Browse files Browse the repository at this point in the history
…aeger

Detect sample frequency for Draeger data
  • Loading branch information
psomhorst authored Dec 18, 2024
2 parents 6ad680f + 64a68d0 commit fef62be
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
6 changes: 1 addition & 5 deletions eitprocessing/datahandling/loading/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def load_eit_data(
Defaults to the same value as label.
description: long description of sequence for human interpretation.
sample_frequency: sample frequency at which the data was recorded.
Default for Draeger: 20
No default for Draeger. Will be autodetected. Warns if autodetected differs from provided.
Default for Timpel: 50
Default for Sentec: 50.2
first_frame: index of first frame to load.
Expand Down Expand Up @@ -68,10 +68,6 @@ def load_eit_data(
Vendor.SENTEC: sentec.load_from_single_path,
}[vendor]

if vendor == Vendor.DRAEGER and not sample_frequency:
msg = """Provide a sample frequency when loading Draeger data."""
raise NotImplementedError(msg) # automatic sample frequency detection is to be implemented per #217

first_frame = _check_first_frame(first_frame)

paths = EITData.ensure_path_list(path)
Expand Down
14 changes: 11 additions & 3 deletions eitprocessing/datahandling/loading/draeger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

def load_from_single_path(
path: Path,
sample_frequency: float,
sample_frequency: float | None = None,
first_frame: int = 0,
max_frames: int | None = None,
) -> dict[str, DataCollection]:
Expand Down Expand Up @@ -88,9 +88,17 @@ def load_from_single_path(
previous_marker,
)

estimated_sample_frequency = round((len(time) - 1) / (time[-1] - time[0]), 4)

if not sample_frequency:
msg = "No sample frequency provided. "
raise ValueError(msg)
sample_frequency = estimated_sample_frequency

if sample_frequency != estimated_sample_frequency:
msg = (
f"Provided sample frequency ({sample_frequency}) does not match "
f"the estimated sample frequency ({estimated_sample_frequency})."
)
warnings.warn(msg, RuntimeWarning)

# time wraps around the number of seconds in a day
time = np.unwrap(time, period=24 * 60 * 60)
Expand Down
13 changes: 9 additions & 4 deletions tests/test_loading.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ def test_loading_draeger(
# assert draeger_both != draeger_inverted


def test_sample_frequency_draeger():
with_sf = load_eit_data(draeger_file1, vendor="draeger", sample_frequency=20)
without_sf = load_eit_data(draeger_file1, vendor="draeger")
assert with_sf.eit_data["raw"].sample_frequency == without_sf.eit_data["raw"].sample_frequency

with pytest.warns(RuntimeWarning):
_ = load_eit_data(draeger_file1, vendor="draeger", sample_frequency=50)


def test_loading_timpel(
draeger1: Sequence,
timpel1: Sequence,
Expand Down Expand Up @@ -69,10 +78,6 @@ def test_loading_illegal():
with pytest.raises(OSError):
_ = load_eit_data(timpel_file, vendor="draeger", sample_frequency=20)

# no sample frequency provided
with pytest.raises(NotImplementedError):
_ = load_eit_data(timpel_file, vendor="draeger", sample_frequency=None)


def test_load_partial(
draeger2: Sequence,
Expand Down

0 comments on commit fef62be

Please sign in to comment.