From 39a58dd077f693fbf222aab5904dd0e9ec572a53 Mon Sep 17 00:00:00 2001 From: IanCa Date: Wed, 31 Jan 2024 12:52:17 -0600 Subject: [PATCH] Fix bug in column mapper dicts could be returned wrong --- hed/models/column_mapper.py | 9 +++++---- tests/models/test_column_mapper.py | 31 ++++++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/hed/models/column_mapper.py b/hed/models/column_mapper.py index e0948b9b..6e886fff 100644 --- a/hed/models/column_mapper.py +++ b/hed/models/column_mapper.py @@ -2,6 +2,7 @@ from hed.models.sidecar import Sidecar from hed.errors.error_reporter import ErrorHandler from hed.errors.error_types import ValidationErrors +from hed.models.definition_dict import DefinitionDict import copy from collections import Counter @@ -32,16 +33,16 @@ def __init__(self, sidecar=None, tag_columns=None, column_prefix_dictionary=None the sidecar. Notes: - - All column numbers are 0 based. + - All column numbers are 0 based. - The column_prefix_dictionary may be deprecated/renamed in the future. - - These are no longer prefixes, but rather converted to value columns: + - These are no longer prefixes, but rather converted to value columns: {"key": "Description", 1: "Label/"} will turn into value columns as {"key": "Description/#", 1: "Label/#"} It will be a validation issue if column 1 is called "key" in the above example. This means it no longer accepts anything but the value portion only in the columns. """ - + # Maps column number to column_entry. This is what's actually used by most code. self._final_column_map = {} self._no_mapping_info = True @@ -392,7 +393,7 @@ def get_def_dict(self, hed_schema, extra_def_dicts=None): if self._sidecar: return self._sidecar.get_def_dict(hed_schema=hed_schema, extra_def_dicts=extra_def_dicts) - return [] + return DefinitionDict(extra_def_dicts, hed_schema=hed_schema) def get_column_mapping_issues(self): """ Get all the issues with finalizing column mapping(duplicate columns, missing required, etc) diff --git a/tests/models/test_column_mapper.py b/tests/models/test_column_mapper.py index 54064c51..cf33315f 100644 --- a/tests/models/test_column_mapper.py +++ b/tests/models/test_column_mapper.py @@ -2,7 +2,7 @@ import os from hed.models import ColumnMapper, ColumnType, HedString -from hed.models.sidecar import Sidecar +from hed.models.sidecar import Sidecar, DefinitionDict from hed.errors import ValidationErrors from hed import load_schema @@ -24,6 +24,7 @@ def setUpClass(cls): cls.base_data_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '../data/') cls.basic_events_json = os.path.join(cls.base_data_dir, "sidecar_tests/both_types_events.json") + cls.bids_events_defs = os.path.join(cls.base_data_dir, "validator_tests/bids_events.json") cls.basic_event_name = "trial_type" cls.basic_event_type = ColumnType.Categorical cls.basic_hed_tags_column = "onset" @@ -31,19 +32,6 @@ def setUpClass(cls): cls.basic_event_row = ["1.2", "0.6", "go", "1.435", "images/red_square.jpg"] cls.basic_event_row_invalid = ["1.2", "0.6", "invalid_category_key", "1.435", "images/red_square.jpg"] - cls.add_column_name = "TestColumn" - cls.add_column_number = 0 - cls.hed_string = "Event/Label/#" - cls.test_column_map = ["TestColumn"] - cls.required_prefix = "TestRequiredPrefix/" - cls.complex_hed_tag_required_prefix = \ - "TestRequiredPrefix/ThisIsAHedTag, (TestRequiredPrefix/NewTag, TestRequiredPrefix/NewTag3)" - cls.complex_hed_tag_no_prefix = "ThisIsAHedTag,(NewTag,NewTag3)" - - cls.short_tag_key = 'Item/Language-item/Character/' - cls.short_tag_with_missing_prefix = "D" - cls.short_tag_partial_prefix = 'Language-item/Character/' - cls.short_tag_partial_prefix2 = 'Character/' def test_set_tag_columns(self): mapper = ColumnMapper() @@ -211,6 +199,21 @@ def test_tag_mapping_complex(self): self.assertEqual(mapper._final_column_map[1].hed_dict, "Label/#") self.assertEqual(mapper._final_column_map[2].column_type, ColumnType.HEDTags) + def test_get_def_dict(self): + mapper = ColumnMapper() + def_dict_empty = mapper.get_def_dict(self.hed_schema) + self.assertIsInstance(def_dict_empty, DefinitionDict) + def_dict_base = DefinitionDict("(Definition/TestDef, (Event))", self.hed_schema) + self.assertIsInstance(def_dict_base, DefinitionDict) + self.assertEqual(len(def_dict_base.defs), 1) + def_dict = mapper.get_def_dict(self.hed_schema, extra_def_dicts=def_dict_base) + self.assertIsInstance(def_dict, DefinitionDict) + self.assertEqual(len(def_dict.defs), 1) + + mapper._set_sidecar(Sidecar(self.bids_events_defs)) + def_dict_combined = mapper.get_def_dict(self.hed_schema, extra_def_dicts=def_dict_base) + self.assertIsInstance(def_dict_combined, DefinitionDict) + self.assertEqual(len(def_dict_combined.defs), 4) if __name__ == '__main__':