From f249f831c0f301d7b34a130979986d25334eda6a Mon Sep 17 00:00:00 2001 From: David Nicholson Date: Fri, 10 May 2024 23:22:09 -0400 Subject: [PATCH] Fix 'unlabeled' -> vak.common.constants.DEFAULT_BACKGROUND_LABEL in tests/ --- tests/test_common/test_labels.py | 11 ++++---- tests/test_prep/test_split/test_split.py | 5 ++-- .../test_frame_labels/test_functional.py | 23 +++++++++-------- .../test_frame_labels/test_transforms.py | 25 ++++++++++--------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/tests/test_common/test_labels.py b/tests/test_common/test_labels.py index e8c4df0dc..398d53684 100644 --- a/tests/test_common/test_labels.py +++ b/tests/test_common/test_labels.py @@ -4,6 +4,7 @@ import pandas as pd import pytest +import vak.common # for constants import vak.common.files.spect import vak.common.labels @@ -77,19 +78,19 @@ def test_from_df(config_type, model_name, audio_format, spect_format, annot_form INTS_LABELMAP = {str(val): val for val in range(1, 20)} INTS_LABELMAP_WITH_UNLABELED = copy.deepcopy(INTS_LABELMAP) -INTS_LABELMAP_WITH_UNLABELED['unlabeled'] = 0 +INTS_LABELMAP_WITH_UNLABELED[vak.common.constants.DEFAULT_BACKGROUND_LABEL] = 0 -DEFAULT_SKIP = ('unlabeled',) +DEFAULT_SKIP = (vak.common.constants.DEFAULT_BACKGROUND_LABEL,) @pytest.mark.parametrize( 'labelmap, skip', [ ({'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, None), - ({'unlabeled': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, None), - ({'unlabeled': 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, ('unlabeled',)), + ({vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, None), + ({vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, 'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}, (vak.common.constants.DEFAULT_BACKGROUND_LABEL,)), (INTS_LABELMAP, None), - (INTS_LABELMAP_WITH_UNLABELED, ('unlabeled',)) + (INTS_LABELMAP_WITH_UNLABELED, (vak.common.constants.DEFAULT_BACKGROUND_LABEL,)) ] ) def test_multi_char_labels_to_single_char(labelmap, skip): diff --git a/tests/test_prep/test_split/test_split.py b/tests/test_prep/test_split/test_split.py index 129af2e25..844ade2a8 100644 --- a/tests/test_prep/test_split/test_split.py +++ b/tests/test_prep/test_split/test_split.py @@ -5,6 +5,7 @@ import pandas as pd import pytest +import vak.common # for constants import vak.common.annotation import vak.prep.spectrogram_dataset.spect_helper import vak.prep.split.split @@ -276,7 +277,7 @@ def test_split_frame_classification_dataframe( labelmap_path = dataset_path / "labelmap.json" with labelmap_path.open("r") as f: labelmap = json.load(f) - labelset = set(key for key in labelmap.keys() if key != 'unlabeled') + labelset = set(key for key in labelmap.keys() if key != vak.common.constants.DEFAULT_BACKGROUND_LABEL) dataset_df_split = vak.prep.split.split.frame_classification_dataframe( dataset_df, dataset_path, labelset=labelset, train_dur=train_dur, val_dur=val_dur, test_dur=test_dur @@ -321,7 +322,7 @@ def test_split_unit_dataframe( labelmap_path = dataset_path / "labelmap.json" with labelmap_path.open("r") as f: labelmap = json.load(f) - labelset = set(key for key in labelmap.keys() if key != 'unlabeled') + labelset = set(key for key in labelmap.keys() if key != vak.common.constants.DEFAULT_BACKGROUND_LABEL) dataset_df_split = vak.prep.split.split.unit_dataframe( dataset_df, dataset_path, labelset=labelset, train_dur=train_dur, val_dur=val_dur, test_dur=test_dur diff --git a/tests/test_transforms/test_frame_labels/test_functional.py b/tests/test_transforms/test_frame_labels/test_functional.py index a279beb79..b5453240f 100644 --- a/tests/test_transforms/test_frame_labels/test_functional.py +++ b/tests/test_transforms/test_frame_labels/test_functional.py @@ -28,6 +28,7 @@ import numpy as np import pytest +import vak.common # for constants import vak.common.files.spect import vak.common.labels import vak.transforms.frame_labels @@ -87,7 +88,7 @@ def test_from_segments(annot, spect_path, labelset): annot.seq.onsets_s, annot.seq.offsets_s, timebins, - background_label=labelmap['unlabeled'], + background_label=labelmap[vak.common.constants.DEFAULT_BACKGROUND_LABEL], ) assert lbl_tb.shape == timebins.shape assert all( @@ -98,10 +99,10 @@ def test_from_segments(annot, spect_path, labelset): @pytest.mark.parametrize( "lbl_tb, labelmap, labels_expected_int", [ - (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {'unlabeled': 0, 'a': 1, 'b': 2}, [1, 2]), - (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {'unlabeled': 0, '1': 1, '2': 2}, [1, 2]), - (np.array([0, 0, 21, 21, 0, 0, 22, 22, 0, 0]), {'unlabeled': 0, '21': 21, '22': 22}, [21, 22]), - (np.array([0, 0, 11, 11, 0, 0, 12, 12, 0, 0]), {'unlabeled': 0, '11': 11, '12': 12}, [11, 12]), + (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, 'a': 1, 'b': 2}, [1, 2]), + (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '1': 1, '2': 2}, [1, 2]), + (np.array([0, 0, 21, 21, 0, 0, 22, 22, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '21': 21, '22': 22}, [21, 22]), + (np.array([0, 0, 11, 11, 0, 0, 12, 12, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '11': 11, '12': 12}, [11, 12]), ] ) def test_to_labels(lbl_tb, labelmap, labels_expected_int): @@ -109,7 +110,7 @@ def test_to_labels(lbl_tb, labelmap, labels_expected_int): # we can easily compare strings we get back with expected; # this is what core.eval does labelmap = vak.common.labels.multi_char_labels_to_single_char( - labelmap, skip=('unlabeled',) + labelmap, skip=(vak.common.constants.DEFAULT_BACKGROUND_LABEL,) ) labelmap_inv = {v: k for k, v in labelmap.items()} labels_expected = ''.join([labelmap_inv[lbl_int] for lbl_int in labels_expected_int]) @@ -150,7 +151,7 @@ def test_to_labels_real_data( # we can easily compare strings we get back with expected; # this is what core.eval does labelmap = vak.common.labels.multi_char_labels_to_single_char( - labelmap, skip=('unlabeled',) + labelmap, skip=(vak.common.constants.DEFAULT_BACKGROUND_LABEL,) ) TIMEBINS_KEY = "t" @@ -323,13 +324,13 @@ def test_remove_short_segments(lbl_tb, unlabeled, timebin_dur, min_segment_dur, 0, np.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0]), ), - # test MajorityVote works when there is no 'unlabeled' segment at start of vector + # test MajorityVote works when there is no vak.common.constants.DEFAULT_BACKGROUND_LABEL segment at start of vector ( np.asarray([1, 1, 2, 1, 0, 0, 0, 0]), 0, np.asarray([1, 1, 1, 1, 0, 0, 0, 0]) ), - # test MajorityVote works when there is no 'unlabeled' segment at end of vector + # test MajorityVote works when there is no vak.common.constants.DEFAULT_BACKGROUND_LABEL segment at end of vector ( np.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1]), 0, @@ -389,14 +390,14 @@ def test_majority_vote(lbl_tb_in, unlabeled, lbl_tb_expected): # majority vote converts second segment to label "a" np.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0]), ), - # test MajorityVote works when there is no 'unlabeled' segment at start of vector + # test MajorityVote works when there is no vak.common.constants.DEFAULT_BACKGROUND_LABEL segment at start of vector ( np.array([1, 1, 2, 1, 0, 0, 0, 0]), None, True, np.array([1, 1, 1, 1, 0, 0, 0, 0]), ), - # test MajorityVote works when there is no 'unlabeled' segment at end of vector + # test MajorityVote works when there is no vak.common.constants.DEFAULT_BACKGROUND_LABEL segment at end of vector ( np.array([0, 0, 0, 1, 1, 1, 0, 0, 0, 1, 2, 1]), None, diff --git a/tests/test_transforms/test_frame_labels/test_transforms.py b/tests/test_transforms/test_frame_labels/test_transforms.py index 40cc07dc2..399eee87e 100644 --- a/tests/test_transforms/test_frame_labels/test_transforms.py +++ b/tests/test_transforms/test_frame_labels/test_transforms.py @@ -2,6 +2,7 @@ import pytest import vak +import vak.common # for constants from .test_functional import ( @@ -36,7 +37,7 @@ def test_call(self, annot, spect_path, labelset): 'Annotation with label not in labelset, would not include in dataset' ) - from_segments_tfm = vak.transforms.frame_labels.FromSegments(background_label=labelmap['unlabeled']) + from_segments_tfm = vak.transforms.frame_labels.FromSegments(background_label=labelmap[vak.common.constants.DEFAULT_BACKGROUND_LABEL]) lbl_tb = from_segments_tfm( lbls_int, annot.seq.onsets_s, @@ -55,7 +56,7 @@ class TestToLabels: [tup[2] for tup in FROM_SEGMENTS_PARAMETRIZE_ARGVALS], ) def test_init(self, labelset): - # Note that we add an 'unlabeled' class because post-processing transforms *require* it + # Note that we add an vak.common.constants.DEFAULT_BACKGROUND_LABEL class because post-processing transforms *require* it # This is default, just making it explicit labelset = vak.common.converters.labelset_to_set(labelset) labelmap = vak.common.labels.to_map(labelset, map_background=True) @@ -68,17 +69,17 @@ def test_init(self, labelset): @pytest.mark.parametrize( "lbl_tb, labelmap, labels_expected_int", [ - (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {'unlabeled': 0, 'a': 1, 'b': 2}, [1, 2]), - (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {'unlabeled': 0, '1': 1, '2': 2}, [1, 2]), - (np.array([0, 0, 21, 21, 0, 0, 22, 22, 0, 0]), {'unlabeled': 0, '21': 21, '22': 22}, [21, 22]), - (np.array([0, 0, 11, 11, 0, 0, 12, 12, 0, 0]), {'unlabeled': 0, '11': 11, '12': 12}, [11, 12]), + (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, 'a': 1, 'b': 2}, [1, 2]), + (np.array([0, 0, 1, 1, 0, 0, 2, 2, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '1': 1, '2': 2}, [1, 2]), + (np.array([0, 0, 21, 21, 0, 0, 22, 22, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '21': 21, '22': 22}, [21, 22]), + (np.array([0, 0, 11, 11, 0, 0, 12, 12, 0, 0]), {vak.common.constants.DEFAULT_BACKGROUND_LABEL: 0, '11': 11, '12': 12}, [11, 12]), ] ) def test_call(self, lbl_tb, labelmap, labels_expected_int): - # Note that we add an 'unlabeled' class because post-processing transforms *require* it + # Note that we add an vak.common.constants.DEFAULT_BACKGROUND_LABEL class because post-processing transforms *require* it # This is default, just making it explicit labelmap = vak.common.labels.multi_char_labels_to_single_char( - labelmap, skip=('unlabeled',) + labelmap, skip=(vak.common.constants.DEFAULT_BACKGROUND_LABEL,) ) labelmap_inv = {v: k for k, v in labelmap.items()} labels_expected = ''.join([labelmap_inv[lbl_int] for lbl_int in labels_expected_int]) @@ -103,7 +104,7 @@ def test_call_real_data( # we can easily compare strings we get back with expected; # this is what core.eval does labelmap = vak.common.labels.multi_char_labels_to_single_char( - labelmap, skip=('unlabeled',) + labelmap, skip=(vak.common.constants.DEFAULT_BACKGROUND_LABEL,) ) TIMEBINS_KEY = "t" @@ -152,7 +153,7 @@ class TestToSegments: [tup[2] for tup in FROM_SEGMENTS_PARAMETRIZE_ARGVALS], ) def test_init(self, labelset): - # Note that we add an 'unlabeled' class because post-processing transforms *require* it + # Note that we add an vak.common.constants.DEFAULT_BACKGROUND_LABEL class because post-processing transforms *require* it # This is default, just making it explicit labelset = vak.common.converters.labelset_to_set(labelset) labelmap = vak.common.labels.to_map(labelset, map_background=True) @@ -218,7 +219,7 @@ class TestPostprocess: [argvals[3:5] + (TIMEBIN_DUR_FOR_PARAMETRIZE,) for argvals in POSTPROCESS_PARAMS_ARGVALS] ) def test_init(self, min_segment_dur, majority_vote, timebin_dur): - # Note that we add an 'unlabeled' class + # Note that we add an vak.common.constants.DEFAULT_BACKGROUND_LABEL class # because post-processing transforms *require* it # This is default, just making it explicit to_labels_tfm = vak.transforms.frame_labels.PostProcess( @@ -233,7 +234,7 @@ def test_init(self, min_segment_dur, majority_vote, timebin_dur): POSTPROCESS_PARAMS_ARGVALS ) def test_call(self, lbl_tb, timebin_dur, background_label, min_segment_dur, majority_vote, lbl_tb_expected): - # Note that we add an 'unlabeled' class because post-processing transforms *require* it + # Note that we add an vak.common.constants.DEFAULT_BACKGROUND_LABEL class because post-processing transforms *require* it # This is default, just making it explicit postprocess_tfm = vak.transforms.frame_labels.PostProcess( min_segment_dur=min_segment_dur,