Skip to content

Commit

Permalink
Fix 'unlabeled' -> vak.common.constants.DEFAULT_BACKGROUND_LABEL in t…
Browse files Browse the repository at this point in the history
…ests/
  • Loading branch information
NickleDave committed May 11, 2024
1 parent 8485964 commit f249f83
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
11 changes: 6 additions & 5 deletions tests/test_common/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import pandas as pd
import pytest

import vak.common # for constants
import vak.common.files.spect
import vak.common.labels

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 3 additions & 2 deletions tests/test_prep/test_split/test_split.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
23 changes: 12 additions & 11 deletions tests/test_transforms/test_frame_labels/test_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -98,18 +99,18 @@ 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):
# next line, convert all labels to single characters
# 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])
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 13 additions & 12 deletions tests/test_transforms/test_frame_labels/test_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest

import vak
import vak.common # for constants


from .test_functional import (
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand All @@ -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])
Expand All @@ -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"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down

0 comments on commit f249f83

Please sign in to comment.