Skip to content

Commit

Permalink
let Edf.append_signals insert after last ordinary signal
Browse files Browse the repository at this point in the history
  • Loading branch information
hofaflo committed Dec 18, 2023
1 parent a4d9189 commit 9a33c5a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed
- Exclude EDF+ annotation signals from `Edf.signals`, `Edf.num_signals`, and `Edf.drop_signals()` ([#25](https://github.com/the-siesta-group/edfio/pull/25)).
- Provide more concise `__repr__` for `Edf` and `EdfSignal` ([#26](https://github.com/the-siesta-group/edfio/pull/26)).
- `Edf.append_signals()` now inserts new signals after the last ordinary (i.e. non-annotation) signal ([#28](https://github.com/the-siesta-group/edfio/pull/28)).

### Fixed
- Avoid floating point errors sometimes preventing the creation of an Edf with signals that are actually compatible in duration ([#15](https://github.com/the-siesta-group/edfio/pull/15)).
Expand Down
18 changes: 15 additions & 3 deletions edfio/edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1181,10 +1181,12 @@ def drop_signals(self, drop: Iterable[int | str]) -> None:

def append_signals(self, new_signals: EdfSignal | Iterable[EdfSignal]) -> None:
"""
Append one or more signal(s) to the Edf recording, after all existing ones.
Append one or more signal(s) to the Edf recording.
Every signal must be compatible with the current `data_record_duration` and all
signal durations must match the overall recording duration.
signal durations must match the overall recording duration. For recordings
containing EDF+ annotation signals, the new signals are inserted after the last
ordinary (i.e. non-annotation) signal.
Parameters
----------
Expand All @@ -1193,7 +1195,17 @@ def append_signals(self, new_signals: EdfSignal | Iterable[EdfSignal]) -> None:
"""
if isinstance(new_signals, EdfSignal):
new_signals = [new_signals]
self._set_signals((*self._signals, *new_signals))
last_ordinary_index = 0
for i, signal in enumerate(self._signals):
if signal.label != "EDF Annotations":
last_ordinary_index = i
self._set_signals(
[
*self._signals[: last_ordinary_index + 1],
*new_signals,
*self._signals[last_ordinary_index + 1 :],
]
)

@property
def _annotation_signals(self) -> Iterable[EdfSignal]:
Expand Down
24 changes: 24 additions & 0 deletions tests/test_edf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1171,6 +1171,30 @@ def test_append_signals_multiple():
assert actual == expected


def test_append_signals_appends_after_last_ordinary_signal():
edf = Edf(
[
EdfSignal(np.arange(3), 1, label="S1"),
EdfSignal(np.arange(3), 1, label="S2"),
_create_annotations_signal(
(), data_record_duration=1, num_data_records=3, with_timestamps=True
),
EdfSignal(np.arange(3), 1, label="S3"),
_create_annotations_signal(
(), data_record_duration=1, num_data_records=3, with_timestamps=True
),
]
)
edf.append_signals(
[
EdfSignal(np.arange(3), 1, label="S4"),
EdfSignal(np.arange(3), 1, label="S5"),
]
)
expected = ["S1", "S2", "EDF Annotations", "S3", "S4", "S5", "EDF Annotations"]
assert [s.label for s in edf._signals] == expected


@pytest.mark.parametrize(
("length", "sampling_frequency"),
[(1001, 10), (999, 10), (1000, 10.001), (1, 0.011)],
Expand Down

0 comments on commit 9a33c5a

Please sign in to comment.