diff --git a/audformat/core/column.py b/audformat/core/column.py index 4ec5807e..57ab3fd4 100644 --- a/audformat/core/column.py +++ b/audformat/core/column.py @@ -254,20 +254,30 @@ def get( "for its labels." ) + # Check that at least one key is available for map + # if labels are stored as dictionary + keys = [] + for key, value in labels.items(): + if isinstance(value, dict): + keys += list(value.keys()) + keys = sorted(list(set(keys))) + if len(keys) > 0 and map not in keys: + raise ValueError( + f"Cannot map " + f"'{self._id}' " + f"to " + f"'{map}'. " + f"Expected one of " + f"{list(keys)}." + ) + mapping = {} for key, value in labels.items(): if isinstance(value, dict): if map in value: value = value[map] else: - raise ValueError( - f"Cannot map " - f"'{self._id}' " - f"to " - f"'{map}'. " - f"Expected one of " - f"{list(value)}." - ) + value = np.NaN mapping[key] = value result = result.map(mapping) diff --git a/tests/conftest.py b/tests/conftest.py index 8259f165..d65a2723 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -8,6 +8,11 @@ pytest.DB = audformat.testing.create_db() +# Adjust scheme dictionary to contain one missing label +labels = pytest.DB.schemes["label_map_str"].labels +labels['label3'].pop('prop2') +pytest.DB.schemes["label_map_str"].replace_labels(labels) + pytest.DB_ROOT = 'db' pytest.FILE_DUR = pd.to_timedelta('1s') diff --git a/tests/test_column.py b/tests/test_column.py index 6e44f33d..2a3ee70f 100644 --- a/tests/test_column.py +++ b/tests/test_column.py @@ -177,7 +177,7 @@ def test_get_as_segmented(): pytest.DB['files']['string'], 'map', None, marks=pytest.mark.xfail(raises=ValueError), ), - pytest.param( # no labels + pytest.param( # no labels in dict pytest.DB['files']['label_map_str'], 'bad', None, marks=pytest.mark.xfail(raises=ValueError), ), @@ -189,7 +189,10 @@ def test_map(column, map, expected_dtype): mapping = {} for key, value in pytest.DB.schemes[column.scheme_id].labels.items(): if isinstance(value, dict): - value = value[map] + if map in value: + value = value[map] + else: + value = np.NaN mapping[key] = value expected = expected.map(mapping).astype(expected_dtype) expected.name = map