From bebbd2731944625315602cbcee8d0b4195cc3687 Mon Sep 17 00:00:00 2001 From: IanCa Date: Mon, 23 Oct 2023 19:40:26 -0500 Subject: [PATCH] Add some attribute validator tests. Remove some unused code --- docs/source/conf.py | 2 +- hed/models/df_util.py | 26 ----- hed/models/indexed_df.py | 39 ------- .../test_schema_attribute_validators.py | 110 +++++++++++++++++- 4 files changed, 108 insertions(+), 69 deletions(-) delete mode 100644 hed/models/indexed_df.py diff --git a/docs/source/conf.py b/docs/source/conf.py index 1e5bf07d..4bc9f309 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -89,7 +89,7 @@ # Toc options 'collapse_navigation': False, 'sticky_navigation': True, - 'navigation_depth': 4, + 'navigation_depth': 7, 'includehidden': True, 'titles_only': False } diff --git a/hed/models/df_util.py b/hed/models/df_util.py index 2449e8a7..0a9373d1 100644 --- a/hed/models/df_util.py +++ b/hed/models/df_util.py @@ -120,26 +120,6 @@ def expand_defs(df, hed_schema, def_dict, columns=None): df.loc[mask, column] = df.loc[mask, column].apply(partial(_expand_defs, hed_schema=hed_schema, def_dict=def_dict)) -def sort_strings(df, hed_schema, tag_form="short_tag", columns=None): - """ Expands any def tags found in the dataframe. - - Converts in place - - Parameters: - df (pd.Dataframe or pd.Series): The dataframe or series to modify - hed_schema (HedSchema or None): The schema to use to identify defs - columns (list or None): The columns to modify on the dataframe - """ - if isinstance(df, pd.Series): - df[:] = df.apply(partial(_sort, hed_schema=hed_schema, tag_form=tag_form)) - else: - if columns is None: - columns = df.columns - - for column in columns: - df.loc[column] = df.loc[column].apply(partial(_sort, hed_schema=hed_schema, tag_form=tag_form)) - - def _convert_to_form(hed_string, hed_schema, tag_form): return str(HedString(hed_string, hed_schema).get_as_form(tag_form)) @@ -152,12 +132,6 @@ def _expand_defs(hed_string, hed_schema, def_dict): return str(HedString(hed_string, hed_schema, def_dict).expand_defs()) -def _sort(hed_string, hed_schema, tag_form): - sorted_string = HedString(hed_string, hed_schema) - sorted_string.sort() - return sorted_string.get_as_form(tag_form) - - def process_def_expands(hed_strings, hed_schema, known_defs=None, ambiguous_defs=None): """ Gather def-expand tags in the strings/compare with known definitions to find any differences diff --git a/hed/models/indexed_df.py b/hed/models/indexed_df.py deleted file mode 100644 index c23c4c7f..00000000 --- a/hed/models/indexed_df.py +++ /dev/null @@ -1,39 +0,0 @@ -from functools import partial -import pandas as pd - -from hed.models.sidecar import Sidecar -from hed.models.tabular_input import TabularInput -from hed.models.hed_string import HedString -from hed.models.definition_dict import DefinitionDict -from hed.models import df_util - - -class IndexedDF: - def __init__(self, tabular_input, sidecar, hed_schema): - self._hed_strings = df_util.get_assembled(tabular_input, sidecar, hed_schema, expand_defs=True) - # self._df = df - # self._index = self._create_index(df) - # self._hed_strings = df_util.get_assembled() - # - # def create_index_from_hed_strings(self): - # - # - # - # @staticmethod - # def find_rows_for_strings(self, df, search_strings): - # cache = {} - # for string in search_strings: - # if string not in cache: - # print("Hi") - # parts = string.split('/') - # for i in range(1, len(parts) + 1): - # part = '/'.join(parts[:i]) - # if part not in cache: - # if i == 1: - # searchable_rows = df - # else: - # searchable_rows = df[cache['/'.join(parts[:i - 1])]] - # cache[part] = searchable_rows[searchable_rows.str.contains(part)].index.to_list() - # # cache[string] = cache[part] # Assign the cache result to the complete string - # - # return cache diff --git a/tests/schema/test_schema_attribute_validators.py b/tests/schema/test_schema_attribute_validators.py index e3753c03..1411e928 100644 --- a/tests/schema/test_schema_attribute_validators.py +++ b/tests/schema/test_schema_attribute_validators.py @@ -2,13 +2,13 @@ import copy from hed.schema import schema_attribute_validators -from hed import schema +from hed import load_schema_version class Test(unittest.TestCase): @classmethod def setUpClass(cls): - cls.hed_schema = schema.load_schema_version("8.1.0") + cls.hed_schema = load_schema_version("8.2.0") def test_util_placeholder(self): tag_entry = self.hed_schema.tags["Event"] @@ -39,4 +39,108 @@ def test_util_rooted(self): self.assertFalse(schema_attribute_validators.tag_exists_base_schema_check(self.hed_schema, tag_entry, attribute_name)) tag_entry = copy.deepcopy(tag_entry) tag_entry.attributes["rooted"] = "NotRealTag" - self.assertTrue(schema_attribute_validators.tag_exists_base_schema_check(self.hed_schema, tag_entry, attribute_name)) \ No newline at end of file + self.assertTrue(schema_attribute_validators.tag_exists_base_schema_check(self.hed_schema, tag_entry, attribute_name)) + + def test_unit_class_exists(self): + tag_entry = self.hed_schema.tags["Weight/#"] + attribute_name = "unitClass" + self.assertFalse(schema_attribute_validators.unit_class_exists(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes["unitClass"] = "fakeClass" + self.assertTrue(schema_attribute_validators.unit_class_exists(self.hed_schema, tag_entry, attribute_name)) + + def test_value_class_exists(self): + tag_entry = self.hed_schema.tags["Weight/#"] + attribute_name = "valueClass" + self.assertFalse(schema_attribute_validators.value_class_exists(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes["valueClass"] = "fakeClass" + self.assertTrue(schema_attribute_validators.value_class_exists(self.hed_schema, tag_entry, attribute_name)) + + def test_unit_exists(self): + tag_entry = self.hed_schema.unit_classes["accelerationUnits"] + attribute_name = "defaultUnits" + self.assertFalse(schema_attribute_validators.unit_exists(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes["defaultUnits"] = "bad_unit" + self.assertTrue(schema_attribute_validators.unit_exists(self.hed_schema, tag_entry, attribute_name)) + + def test_deprecatedFrom(self): + tag_entry = self.hed_schema.tags["Event/Measurement-event"] + attribute_name = "deprecatedFrom" + self.assertFalse(schema_attribute_validators.tag_is_deprecated_check(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes["deprecatedFrom"] = "200.3.0" + self.assertTrue(schema_attribute_validators.tag_is_deprecated_check(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes["deprecatedFrom"] = "invalid" + self.assertTrue(schema_attribute_validators.tag_is_deprecated_check(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes["deprecatedFrom"] = "1" + self.assertTrue(schema_attribute_validators.tag_is_deprecated_check(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes["deprecatedFrom"] = "8.0.0" + self.assertFalse(schema_attribute_validators.tag_is_deprecated_check(self.hed_schema, tag_entry, attribute_name)) + + def test_conversionFactor(self): + tag_entry = self.hed_schema.unit_classes["accelerationUnits"].units['m-per-s^2'] + attribute_name = "conversionFactor" + self.assertFalse(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes[attribute_name] = "-1.0" + self.assertTrue(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes[attribute_name] = "10^3" + self.assertFalse(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes[attribute_name] = None + self.assertTrue(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + def test_conversionFactor_modifier(self): + tag_entry = self.hed_schema.unit_classes["magneticFieldUnits"].units['tesla'] + attribute_name = "conversionFactor" + self.assertFalse(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes[attribute_name] = "-1.0" + self.assertTrue(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes[attribute_name] = "10^3" + self.assertFalse(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + tag_entry.attributes[attribute_name] = None + self.assertTrue(schema_attribute_validators.conversion_factor(self.hed_schema, tag_entry, attribute_name)) + + def test_allowed_characters_check(self): + tag_entry = self.hed_schema.value_classes["dateTimeClass"] + attribute_name = "allowedCharacter" + valid_attributes = {'letters', 'blank', 'digits', 'alphanumeric', ":", "$", "a"} + self.assertFalse(schema_attribute_validators.allowed_characters_check(self.hed_schema, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + for attribute in valid_attributes: + tag_entry.attributes[attribute_name] = attribute + self.assertFalse(schema_attribute_validators.allowed_characters_check(self.hed_schema, tag_entry, attribute_name)) + + invalid_attributes = {'lettersdd', 'notaword', ":a"} + for attribute in invalid_attributes: + tag_entry.attributes[attribute_name] = attribute + self.assertTrue(schema_attribute_validators.allowed_characters_check(self.hed_schema, tag_entry, attribute_name)) + + def test_in_library_check(self): + score = load_schema_version("score_") + tag_entry = score.tags["Modulator"] + attribute_name = "inLibrary" + self.assertFalse(schema_attribute_validators.in_library_check(score, tag_entry, attribute_name)) + + tag_entry = copy.deepcopy(tag_entry) + tag_entry.attributes[attribute_name] = "invalid" + self.assertTrue(schema_attribute_validators.in_library_check(score, tag_entry, attribute_name)) + + tag_entry.attributes[attribute_name] = "" + self.assertTrue(schema_attribute_validators.in_library_check(score, tag_entry, attribute_name)) \ No newline at end of file