diff --git a/changelog.d/rdann-sampto-exclusive.rst b/changelog.d/rdann-sampto-exclusive.rst new file mode 100644 index 00000000..a4943d3f --- /dev/null +++ b/changelog.d/rdann-sampto-exclusive.rst @@ -0,0 +1,7 @@ +**rdann and Annotation.apply_range stop before sampto, as rdrecord does** + ``rdann`` and ``Annotation.apply_range`` used to include an annotation at + sample ``sampto``, while ``rdrecord`` and ``rdsamp`` return the samples + before it. They now return the annotations before ``sampto``, so that the + annotations read with a signal's range are within that signal, and can index + it when read with ``shift_samps=True``. To include an annotation at a given + sample, pass one more than that sample as ``sampto``. diff --git a/src/wfdb/io/annotation.py b/src/wfdb/io/annotation.py index be02fe73..3a8928f5 100644 --- a/src/wfdb/io/annotation.py +++ b/src/wfdb/io/annotation.py @@ -245,16 +245,17 @@ def apply_range(self, sampfrom=0, sampto=None): Parameters ---------- sampfrom : int, optional - The minimum sample number for annotations to be returned. + The first sample number for annotations to be kept. sampto : int, optional - The maximum sample number for annotations to be returned. If None, - there is no upper limit. + The sample number at which to stop: annotations before this + sample are kept, as in `rdrecord`. If None, there is no upper + limit. """ n_annotations = len(self.sample) kept = self.sample >= sampfrom if sampto is not None: - kept &= self.sample <= sampto + kept &= self.sample < sampto kept_inds = np.flatnonzero(kept) for field in [ @@ -1953,10 +1954,12 @@ def rdann( The annotatator extension of the annotation file. ie. for file '100.atr', extension='atr'. sampfrom : int, optional - The minimum sample number for annotations to be returned. + The first sample number for annotations to be returned. sampto : int, optional - The maximum sample number for annotations to be returned. It must - be greater than `sampfrom`. Leave it out to read to the end. + The sample number at which to stop: annotations before this sample + are returned, as `rdrecord` and `rdsamp` return the samples before + it. It must be greater than `sampfrom`. Leave it out to read to the + end. shift_samps : bool, optional Specifies whether to return the sample indices relative to `sampfrom` (True), or sample 0 (False). @@ -2097,9 +2100,10 @@ def check_read_inputs(sampfrom, sampto, return_label_elements): Parameters ---------- sampfrom : int - The minimum sample number for annotations to be returned. + The first sample number for annotations to be returned. sampto : int - The maximum sample number for annotations to be returned. + The sample number at which to stop: only annotations before this + sample are returned. return_label_elements : list, str The label elements that are to be returned from reading the annotation file. A list with at least one of the following options: 'symbol', @@ -2203,7 +2207,8 @@ def proc_ann_bytes(filebytes, sampto): filebytes : ndarray The input filestream converted to an Nx2 array of unsigned bytes. sampto : int - The maximum sample number for annotations to be returned. + The sample number at which to stop: only annotations before this + sample are returned. Returns ------- @@ -2280,7 +2285,7 @@ def proc_ann_bytes(filebytes, sampto): subtype, chan, num, aux_note, update ) - if sampto and sampto < sample_total: + if sampto and sample_total >= sampto: sample, label_store, subtype, chan, num, aux_note = rm_last( sample, label_store, subtype, chan, num, aux_note ) diff --git a/tests/test_annotation.py b/tests/test_annotation.py index 534d9adc..8b9457d4 100644 --- a/tests/test_annotation.py +++ b/tests/test_annotation.py @@ -523,6 +523,59 @@ def test_rdann_sampfrom_without_sampto(): np.testing.assert_array_equal(shifted.sample, full.sample[start:] - 600000) +@pytest.mark.parametrize("sampfrom", [0, 1000]) +def test_rdann_sampto_is_not_included(sampfrom): + """As in rdrecord, rdann returns the annotations before sampto.""" + full = wfdb.rdann("sample-data/100", "atr") + sampto = int(full.sample[20]) + kept = (full.sample >= sampfrom) & (full.sample < sampto) + + ann = wfdb.rdann("sample-data/100", "atr", sampfrom=sampfrom, sampto=sampto) + + np.testing.assert_array_equal(ann.sample, full.sample[kept]) + assert ann.symbol == list(np.array(full.symbol)[kept]) + # One more sample includes the annotation at the old sampto + ann = wfdb.rdann("sample-data/100", "atr", sampfrom=sampfrom, sampto=sampto + 1) + assert ann.sample[-1] == sampto + + +def test_rdann_and_rdsamp_read_the_same_range(tmp_path): + """Annotations read with a signal's range index that signal (#470).""" + wfdb.wrsamp( + "test", + fs=250, + units=["mV"], + sig_name=["test"], + p_signal=np.linspace(0, 1, 250)[:, None], + fmt=["16"], + write_dir=str(tmp_path), + ) + wfdb.Annotation( + record_name="test", + extension="atr", + sample=np.array([0, 99, 100, 249]), + symbol=["N", "N", "N", "N"], + ).wrann(write_dir=str(tmp_path)) + record_name = str(tmp_path / "test") + + for sampfrom in [0, 50]: + signal, _ = wfdb.rdsamp(record_name, sampfrom=sampfrom, sampto=100) + ann = wfdb.rdann( + record_name, "atr", sampfrom=sampfrom, sampto=100, shift_samps=True + ) + assert ann.sample[-1] == 99 - sampfrom + signal[ann.sample] + + +def test_apply_range_sampto_is_not_included(): + ann = wfdb.Annotation( + "test", "atr", sample=np.array([10, 20, 30]), symbol=["N", "V", "N"] + ) + ann.apply_range(sampfrom=10, sampto=30) + np.testing.assert_array_equal(ann.sample, [10, 20]) + assert ann.symbol == ["N", "V"] + + def test_rdann_sampfrom_empty_file(tmp_path): """rdann with only sampfrom reads an annotation file with no annotations.""" # A file that holds only the end-of-file marker