Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some attribute validator tests. Remove some unused code #783

Merged
merged 1 commit into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
# Toc options
'collapse_navigation': False,
'sticky_navigation': True,
'navigation_depth': 4,
'navigation_depth': 7,
'includehidden': True,
'titles_only': False
}
Expand Down
26 changes: 0 additions & 26 deletions hed/models/df_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand All @@ -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

Expand Down
39 changes: 0 additions & 39 deletions hed/models/indexed_df.py

This file was deleted.

110 changes: 107 additions & 3 deletions tests/schema/test_schema_attribute_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down Expand Up @@ -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))
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))