def test_def_mapping_single_line(self): schema_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/hed_pairs/HED8.0.0.mediawiki') hed_schema = schema.load_schema(schema_path) validator = HedValidator(hed_schema=hed_schema) def_mapper = DefinitionMapper() string_with_def = \ '(Definition/TestDefPlaceholder/#,(Item/TestDef1/#,Item/TestDef2)), def/TestDefPlaceholder/2471' test_string = HedString(string_with_def) issues = test_string.validate([validator, def_mapper], check_for_definitions=True) self.assertEqual(len(issues), 0)
def test_expand_def_tags_placeholder_invalid(self): def_dict = DefDict() def_string = HedString(self.placeholder_def_string) def_string.convert_to_canonical_forms(None) def_dict.check_for_definitions(def_string) def_mapper = DefinitionMapper(def_dict) placeholder_label_def_string_no_placeholder = "def/TestDefPlaceholder" test_string = HedString(placeholder_label_def_string_no_placeholder) test_string.convert_to_canonical_forms(None) def_issues = def_mapper.expand_def_tags(test_string) self.assertEqual(str(test_string), placeholder_label_def_string_no_placeholder) self.assertTrue(def_issues) def_dict = DefDict() def_string = HedString(self.basic_definition_string) def_string.convert_to_canonical_forms(None) def_dict.check_for_definitions(def_string) def_mapper = DefinitionMapper(def_dict) label_def_string_has_invalid_placeholder = "def/TestDef/54687" test_string = HedString(label_def_string_has_invalid_placeholder) test_string.convert_to_canonical_forms(None) def_issues = def_mapper.expand_def_tags(test_string) self.assertEqual(str(test_string), label_def_string_has_invalid_placeholder) self.assertTrue(def_issues)
def test_basic_onset_errors_with_def_mapper(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) validators = [def_mapper, onset_mapper] test_strings = [ f"({self.placeholder_label_def_string},Onset)", f"({self.placeholder_label_def_string},Offset)", f"({self.placeholder_label_def_string},Offset)", f"({self.placeholder_label_def_string}, Onset, (Event), (Event))", f"({self.placeholder_label_def_string}, Onset, (Event))", "(Onset)", f"({self.placeholder_label_def_string}, def/TestDefPlaceholder/2, Onset, (Event))", "(def/TestDefInvalid, Onset)", "(def/TestDefPlaceholder, Onset)", f"({self.placeholder_label_def_string}, Offset, (Event))" ] # count of how many onset names are in the mapper after the line is run expected_context = [1, 0, 0, 0, 1, 1, 1, 1, 1, 1] # count of issues the line generates test_issues = [ [], [], self.format_error(OnsetErrors.OFFSET_BEFORE_ONSET, tag=0), self.format_error(OnsetErrors.ONSET_WRONG_NUMBER_GROUPS, tag=0, tag_list=[ self.placeholder_label_def_string, 'Onset', '(Event)', '(Event)' ]), [], self.format_error(OnsetErrors.ONSET_NO_DEF_TAG_FOUND, tag=0), self.format_error(OnsetErrors.ONSET_TOO_MANY_DEFS, tag=0, tag_list=['def/TestDefPlaceholder/2']), self.format_error(ValidationErrors.HED_DEF_UNMATCHED, tag=0), self.format_error(ValidationErrors.HED_DEF_VALUE_MISSING, tag=0), self.format_error(OnsetErrors.ONSET_WRONG_NUMBER_GROUPS, tag=0, tag_list=[ self.placeholder_label_def_string, 'Offset', '(Event)' ]), ] self._test_issues_base(test_strings, test_issues, expected_context, validators, expand_defs=False)
def test_onset_multiple_or_misplaced_errors(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_string = HedString(self.definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) hed_validator = HedValidator(hed_schema=self.hed_schema) validators = [hed_validator, def_mapper, onset_mapper] test_strings = [ f"{self.placeholder_label_def_string},Onset", f"({self.placeholder_label_def_string},Onset, Onset)", f"({self.placeholder_label_def_string},Onset, Offset)", ] # count of issues the line generates onset_list = [ 'Property/Data-property/Data-marker/Temporal-marker/Onset' ] offset_list = [ 'Property/Data-property/Data-marker/Temporal-marker/Offset' ] test_issues = [ self.format_error(ValidationErrors.HED_TOP_LEVEL_TAG, tag=1), self.format_error(ValidationErrors.HED_TAG_REPEATED, tag=2) + self.format_error(ValidationErrors.HED_MULTIPLE_TOP_TAGS, tag=1, multiple_tags=onset_list), self.format_error(ValidationErrors.HED_MULTIPLE_TOP_TAGS, tag=1, multiple_tags=offset_list), ] self._test_issues_no_context(test_strings, test_issues, validators) test_issues = [ self.format_error(ValidationErrors.HED_TOP_LEVEL_TAG, tag=1), self.format_error(ValidationErrors.HED_TAG_REPEATED, tag=2) + self.format_error(ValidationErrors.HED_MULTIPLE_TOP_TAGS, tag=1, multiple_tags=onset_list), self.format_error(ValidationErrors.HED_MULTIPLE_TOP_TAGS, tag=1, multiple_tags=offset_list), ] # Repeat with just hed validator self._test_issues_no_context(test_strings, test_issues, hed_validator)
def test_basic_onset_errors_expanded(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_string = HedString(self.definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) test_strings = [ f"({self.placeholder_expanded_def_string},Onset)", f"({self.placeholder_expanded_def_string},Offset)", f"({self.placeholder_expanded_def_string},Offset)", f"({self.placeholder_expanded_def_string}, Onset, (Event), (Event))", f"({self.placeholder_expanded_def_string}, Onset, (Event))", "(Onset)", f"({self.placeholder_expanded_def_string}, def/InvalidDef, Onset, (Event))", "(def/TestDefInvalid, Onset)", "(def/TestDefPlaceholder, Onset)", "(def/TestDefNormal/InvalidPlaceholder, Onset)" ] # count of how many onset names are in the mapper after the line is run expected_context = [1, 0, 0, 0, 1, 1, 1, 1, 1, 1] # count of issues the line generates test_issues = [[], [], self.format_error(OnsetErrors.OFFSET_BEFORE_ONSET, tag=0), self.format_error( OnsetErrors.ONSET_WRONG_NUMBER_GROUPS, tag=0, tag_list=[ self.placeholder_expanded_def_string, 'Onset', '(Event)', '(Event)' ]), [], self.format_error(OnsetErrors.ONSET_NO_DEF_TAG_FOUND, tag=0), self.format_error(OnsetErrors.ONSET_TOO_MANY_DEFS, tag=0, tag_list=['def/InvalidDef']), self.format_error(OnsetErrors.ONSET_DEF_UNMATCHED, tag=0), self.format_error(OnsetErrors.ONSET_PLACEHOLDER_WRONG, tag=0, has_placeholder=True), self.format_error(OnsetErrors.ONSET_PLACEHOLDER_WRONG, tag=0, has_placeholder=False)] self._test_issues_base(test_strings, test_issues, expected_context, [onset_mapper])
def test_onset_multiple_or_misplaced_errors_no_validator(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_string = HedString(self.definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) validators = [def_mapper, onset_mapper] test_strings = [ f"{self.placeholder_label_def_string},Onset", f"({self.placeholder_label_def_string},Onset, Onset)", f"({self.placeholder_label_def_string},Onset, Offset)", f"({self.placeholder_label_def_string},Onset, Event)", ] # count of issues the line generates test_issues = [ [], self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=4, def_tag="Def-expand/TestDefPlaceholder/2471"), self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=4, def_tag="Def-expand/TestDefPlaceholder/2471"), self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=4, def_tag="Def-expand/TestDefPlaceholder/2471"), ] self._test_issues_no_context(test_strings, test_issues, validators) # Verify it also works without def mapping test_issues = [ [], self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=2, def_tag=self.placeholder_label_def_string), self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=2, def_tag=self.placeholder_label_def_string), self.format_error(OnsetErrors.ONSET_TAG_OUTSIDE_OF_GROUP, tag=2, def_tag=self.placeholder_label_def_string), ] self._test_issues_no_context(test_strings, test_issues, [validators[1]])
def test_onset_with_defs_in_them(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) test_strings = [ f"({self.placeholder_label_def_string},Onset, ({self.label_def_string}))", ] # count of how many onset names are in the mapper after the line is run expected_context = [ 1, ] # count of issues the line generates test_issues = [[]] self._test_issues_base(test_strings, test_issues, expected_context, [onset_mapper])
def test_expand_def_tags_with_validator(self): def_dict = DefDict() def_string = HedString(self.basic_definition_string) def_string.convert_to_canonical_forms(None) def_dict.check_for_definitions(def_string) def_mapper = DefinitionMapper(def_dict) validator = HedValidator(self.hed_schema) validators = [validator, def_mapper] test_string = HedString(self.basic_definition_string) def_issues = test_string.validate(validators, expand_defs=True) self.assertEqual(test_string.get_as_short(), "") test_string = HedString(self.basic_definition_string_no_paren) def_issues = test_string.validate(validators, expand_defs=True) self.assertTrue(def_issues) self.assertEqual(test_string.get_as_short(), self.basic_definition_string_no_paren) test_string = HedString(self.basic_hed_string + "," + self.basic_definition_string) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), self.basic_hed_string) test_string = HedString(self.basic_definition_string + "," + self.basic_hed_string) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), self.basic_hed_string) test_string = HedString(self.basic_hed_string_with_def) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), self.basic_hed_string + "," + self.expanded_def_string) test_string = HedString(self.basic_hed_string_with_def_first) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), self.expanded_def_string + "," + self.basic_hed_string) test_string = HedString(self.basic_hed_string_with_def_first_paren) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), "(" + self.expanded_def_string + "," + self.basic_hed_string + ")")
def test_test_interleaving_onset_offset(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_string = HedString(self.definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) test_strings = [ f"({self.placeholder_label_def_string},Onset)", f"({self.placeholder_label_def_string2},Onset)", "Event", f"({self.placeholder_label_def_string},Offset)", f"({self.placeholder_label_def_string},Offset)", f"({self.label_def_string}, Onset)", f"({self.placeholder_label_def_string2},Offset)", ] # count of how many onset names are in the mapper after the line is run expected_context = [ 1, 2, 2, 1, 1, 2, 1, ] # count of issues the line generates test_issues = [ [], [], [], [], self.format_error(OnsetErrors.OFFSET_BEFORE_ONSET, tag=0), [], [], ] self._test_issues_base(test_strings, test_issues, expected_context, [onset_mapper])
def test_expand_def_tags_no_expand(self): def_dict = DefDict() def_string = HedString(self.basic_definition_string) def_string.convert_to_canonical_forms(None) def_dict.check_for_definitions(def_string) def_mapper = DefinitionMapper(def_dict) test_string = HedString(self.basic_definition_string) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), "") test_string = HedString(self.basic_definition_string_no_paren) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_definition_string_no_paren) test_string = HedString(self.basic_hed_string + "," + self.basic_definition_string) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_hed_string) test_string = HedString(self.basic_definition_string + "," + self.basic_hed_string) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_hed_string) test_string = HedString(self.basic_hed_string_with_def) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_hed_string_with_def) test_string = HedString(self.basic_hed_string_with_def_first) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_hed_string_with_def_first) test_string = HedString(self.basic_hed_string_with_def_first_paren) def_issues = test_string.validate(def_mapper, expand_defs=False) self.assertFalse(def_issues) self.assertEqual(str(test_string), self.basic_hed_string_with_def_first_paren)
def test_changing_tag_then_def_mapping(self): def_dict = DefDict() def_string = HedString(self.basic_definition_string) def_string.convert_to_canonical_forms(None) def_dict.check_for_definitions(def_string) def_mapper = DefinitionMapper(def_dict) validator = HedValidator(self.hed_schema) validators = [validator, def_mapper] test_string = HedString(self.label_def_string) tag = test_string.get_direct_children()[0] tag.tag = "Organizational-property/" + str(tag) def_issues = test_string.validate(validators, expand_defs=True) self.assertFalse(def_issues) self.assertEqual(test_string.get_as_short(), f"{self.expanded_def_string}") test_string = HedString(self.label_def_string) tag = test_string.get_direct_children()[0] tag.tag = "Organizational-property22/" + str(tag) def_issues = test_string.validate(validators, expand_defs=True) self.assertTrue(def_issues)
def test_onset_two_in_one_line(self): def_dict = DefDict() def_string = HedString(self.placeholder_definition_string) def_string.validate(def_dict) def_string = HedString(self.definition_string) def_string.validate(def_dict) def_mapper = DefinitionMapper(def_dict) onset_mapper = OnsetMapper(def_mapper) test_strings = [ f"({self.placeholder_label_def_string},Onset), ({self.placeholder_label_def_string2},Onset)", f"({self.placeholder_label_def_string2},Offset)", f"({self.placeholder_label_def_string},Offset)", f"({self.placeholder_label_def_string},Onset)", f"({self.placeholder_label_def_string},Offset), ({self.placeholder_label_def_string2},Onset)", f"({self.placeholder_label_def_string},Onset), ({self.placeholder_label_def_string},Onset)", ] # count of how many onset names are in the mapper after the line is run expected_context = [2, 1, 0, 1, 1, 2] # count of issues the line generates test_issues = [[], [], [], [], [], []] self._test_issues_base(test_strings, test_issues, expected_context, [onset_mapper])