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)
Exemple #2
0
    def test_validate_column_group(self):
        validator = HedValidator(hed_schema=None)
        validation_issues = self.json_def_sidecar.validate_entries(
            validator, check_for_warnings=True)
        self.assertEqual(len(validation_issues), 0)

        validation_issues = self.default_sidecar.validate_entries(
            validator, check_for_warnings=True)
        self.assertEqual(len(validation_issues), 0)

        validation_issues = self.errors_sidecar.validate_entries(
            validator, check_for_warnings=True)
        self.assertEqual(len(validation_issues), 15)

        validation_issues = self.json_without_definitions_sidecar.validate_entries(
            validator, check_for_warnings=True)
        self.assertEqual(len(validation_issues), 1)

        hed_string = HedString(
            "(Definition/JsonFileDef/#, (Item/JsonDef1/#,Item/JsonDef1))")
        extra_def_dict = DefDict()
        hed_string.validate(extra_def_dict)

        validation_issues = self.json_without_definitions_sidecar.validate_entries(
            validator, check_for_warnings=True, extra_def_dicts=extra_def_dict)
        self.assertEqual(len(validation_issues), 0)
    def test__check_tag_starts_with(self):
        target_tag_name = "definition/"

        test_tags = ["Definition/TempTestDef", "Informational/Definition/TempTestDef",
                     "Attribute/Informational/Definition/TempTestDef"]

        for tag in test_tags:
            result = DefDict._check_tag_starts_with(tag, target_tag_name)
            self.assertTrue(result)
    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_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_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_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 setUpClass(cls):
        cls.base_data_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../data/')
        hed_xml_file = os.path.join(cls.base_data_dir, "hed_pairs/HED8.0.0t.xml")
        cls.hed_schema = schema.load_schema(hed_xml_file)
        cls.base_dict = DefDict()
        cls.def_contents_string = "(Item/TestDef1,Item/TestDef2)"
        cls.basic_definition_string = f"(Definition/TestDef,{cls.def_contents_string})"
        cls.basic_definition_string_no_paren = f"Definition/TestDef,{cls.def_contents_string}"
        cls.label_def_string = "Def/TestDef"
        cls.expanded_def_string = f"(Def-expand/TestDef,{cls.def_contents_string})"
        cls.basic_hed_string = "Item/BasicTestTag1,Item/BasicTestTag2"
        cls.basic_hed_string_with_def = f"{cls.basic_hed_string},{cls.label_def_string}"
        cls.basic_hed_string_with_def_first = f"{cls.label_def_string},{cls.basic_hed_string}"
        cls.basic_hed_string_with_def_first_paren = f"({cls.label_def_string},{cls.basic_hed_string})"
        cls.placeholder_label_def_string = "def/TestDefPlaceholder/2471"
        cls.placeholder_def_contents = "(Item/TestDef1/#,Item/TestDef2)"
        cls.placeholder_def_string = f"(Definition/TestDefPlaceholder/#,{cls.placeholder_def_contents})"
        cls.placeholder_expanded_def_string = "(Def-expand/TestDefPlaceholder/2471,(Item/TestDef1/2471,Item/TestDef2))"

        cls.placeholder_hed_string_with_def = f"{cls.basic_hed_string},{cls.placeholder_label_def_string}"
        cls.placeholder_hed_string_with_def_first = f"{cls.placeholder_label_def_string},{cls.basic_hed_string}"
        cls.placeholder_hed_string_with_def_first_paren = f"({cls.placeholder_label_def_string},{cls.basic_hed_string})"
    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])