From 9c1a5e083d83a1d8afb6f04e8b141ff583a3afe9 Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Mon, 20 Mar 2023 16:09:26 -0400 Subject: [PATCH 1/3] DEV: Fix nox session 'dev' to work on Windows --- noxfile.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/noxfile.py b/noxfile.py index 0c79632..1dedd60 100644 --- a/noxfile.py +++ b/noxfile.py @@ -1,6 +1,7 @@ import os import pathlib import shutil +import sys import nox @@ -43,7 +44,10 @@ def dev(session: nox.Session) -> None: # e.g. .venv session.run("virtualenv", os.fsdecode(VENV_DIR), silent=True) - python = os.fsdecode(VENV_DIR.joinpath("bin/python")) + if sys.platform.startswith("linux") or sys.platform == "darwin": + python = os.fsdecode(VENV_DIR.joinpath("bin/python")) + elif sys.platform.startswith("win"): + python = os.fsdecode(VENV_DIR.joinpath("Scripts/python.exe")) # Use the venv's interpreter to install the project along with # all it's dev dependencies, this ensures it's installed in the right way From b4d1ca4a5c28829d96e386804f2d86d3793eaeb5 Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Mon, 20 Mar 2023 16:15:54 -0400 Subject: [PATCH 2/3] BUG: Fix how Segment converts on/offset sample to int The onset/offset sample attributes of Segment have a converter that was written to convert numpy int64 to Python int. But it turns out that we don't always default to int64, e.g., on Windows it's int32. The converter failed then since it first checked for a dtype attribute, then checked *only* for int64. This logical error caused us to return None for int32. Instead we check whether there's a dtype *and* it's an instance of np.integer, which catches any numpy int type. --- src/crowsetta/segment.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/crowsetta/segment.py b/src/crowsetta/segment.py index 1daf024..e0e70c6 100644 --- a/src/crowsetta/segment.py +++ b/src/crowsetta/segment.py @@ -5,13 +5,13 @@ from attr.validators import instance_of -def int64_to_int(val): - """Converter that converts ``numpy.int64`` to ``int``, - returns ``int`` as is, and errors for other values. +def convert_int(val): + """Converter that converts ``numpy.integer`` to ``int``, + returns native Python ``int`` as is, and + raises an error for any other type. """ - if hasattr(val, "dtype"): - if val.dtype == np.int64: - return int(val) + if hasattr(val, "dtype") and isinstance(val, np.integer): + return int(val) elif isinstance(val, int): return val else: @@ -81,12 +81,12 @@ class Segment(object): offset_s = attr.ib(validator=attr.validators.optional(instance_of(float)), default=None) onset_sample = attr.ib( validator=attr.validators.optional(instance_of(int)), - converter=attr.converters.optional(int64_to_int), + converter=attr.converters.optional(convert_int), default=None, ) offset_sample = attr.ib( validator=attr.validators.optional(instance_of(int)), - converter=attr.converters.optional(int64_to_int), + converter=attr.converters.optional(convert_int), default=None, ) asdict = attr.asdict From b76482bea23e86f4a2331a1e95a56cb7725bb8e9 Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Mon, 20 Mar 2023 16:22:01 -0400 Subject: [PATCH 3/3] CLN: Rename 'Hertz' -> 'sample' in Sequence unit tests --- tests/test_sequence.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_sequence.py b/tests/test_sequence.py index 607ddfa..e846139 100644 --- a/tests/test_sequence.py +++ b/tests/test_sequence.py @@ -53,7 +53,7 @@ def test_from_keyword_bad_labels_type_raises(): Sequence.from_keyword(labels=labels, onset_samples=onset_samples, offset_samples=offset_samples) -def test_from_keyword__onset_offset_in_seconds(): +def test_from_keyword_onset_offset_in_seconds(): labels = "abcde" onsets_s = np.asarray([0.0, 0.2, 0.4, 0.6, 0.8]) offsets_s = np.asarray([0.1, 0.3, 0.5, 0.7, 0.9]) @@ -62,7 +62,7 @@ def test_from_keyword__onset_offset_in_seconds(): assert type(seq.segments) == tuple -def test_from_keyword_onset_offset_in_Hertz(): +def test_from_keyword_onset_offset_in_samples(): labels = "abcde" onset_samples = np.asarray([0, 2, 4, 6, 8]) offset_samples = np.asarray([1, 3, 5, 7, 9]) @@ -82,7 +82,7 @@ def test_from_dict_onset_offset_in_seconds(): assert type(seq.segments) == tuple -def test_from_dict_onset_offset_in_Hertz(): +def test_from_dict_onset_offset_in_samples(): seq_dict = { "labels": "abcde", "onset_samples": np.asarray([0, 2, 4, 6, 8]), @@ -108,17 +108,17 @@ def test_missing_onset_seconds_raises(): Sequence.from_keyword(labels="abcde", offsets_s=np.asarray([0.0, 0.2, 0.4, 0.6, 0.8])) -def test_missing_offset_Hertz_raises(): +def test_missing_offset_samples_raises(): with pytest.raises(ValueError): Sequence.from_keyword(labels="abcde", onset_samples=np.asarray([0, 2, 4, 6, 8])) -def test_missing_onset_Hertz_raises(): +def test_missing_onset_samples_raises(): with pytest.raises(ValueError): Sequence.from_keyword(labels="abcde", offset_samples=np.asarray([0, 2, 4, 6, 8])) -def test_as_dict_onset_offset_in_Hertz(): +def test_as_dict_onset_offset_in_samples(): labels = "abcde" onset_samples = np.asarray([0, 2, 4, 6, 8]) offset_samples = np.asarray([1, 3, 5, 7, 9])