Skip to content

Commit 6a0ff47

Browse files
committed
Resolved issue hed-standard#1183 and worked on the scripts
1 parent 46699cb commit 6a0ff47

4 files changed

Lines changed: 359 additions & 74 deletions

File tree

hed/validator/hed_validator.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ def check_tag_formatting(self, original_tag) -> list[dict]:
177177

178178
return validation_issues
179179

180-
def validate_units(self, original_tag, validate_text=None, report_as=None, error_code=None, index_offset=0) -> list[dict]:
180+
def validate_units(
181+
self, original_tag, validate_text=None, report_as=None, error_code=None, index_offset=0, allow_placeholders=True
182+
) -> list[dict]:
181183
"""Validate units and value classes
182184
183185
Parameters:
@@ -187,18 +189,19 @@ def validate_units(self, original_tag, validate_text=None, report_as=None, error
187189
Mostly for definitions that expand.
188190
error_code (str): The code to override the error as. Again mostly for def/def-expand tags.
189191
index_offset (int): Offset into the extension validate_text starts at
192+
allow_placeholders (bool): Whether placeholders are allowed (affects value class validation for "#")
190193
191194
Returns:
192195
list[dict]: Issues found from units
193196
"""
194197
if validate_text is None:
195198
validate_text = original_tag.extension
196199
issues = []
197-
if validate_text == "#":
200+
if validate_text == "#" and allow_placeholders:
198201
return []
199202
if original_tag.is_unit_class_tag():
200203
issues += self._unit_validator.check_tag_unit_class_units_are_valid(
201-
original_tag, validate_text, report_as=report_as, error_code=error_code
204+
original_tag, validate_text, report_as=report_as, error_code=error_code, allow_placeholders=allow_placeholders
202205
)
203206
elif original_tag.is_value_class_tag():
204207
issues += self._unit_validator.check_tag_value_class_valid(original_tag, validate_text, report_as=report_as)
@@ -240,8 +243,13 @@ def _validate_individual_tags_in_hed_string(self, hed_string_obj, allow_placehol
240243
hed_tag, self, allow_placeholders=allow_placeholders
241244
)
242245
elif (hed_tag.short_base_tag == DefTagNames.DEFINITION_KEY) and hed_tag.extension.endswith("/#"):
243-
validation_issues += self.validate_units(hed_tag, hed_tag.extension[:-2])
246+
validation_issues += self.validate_units(
247+
hed_tag, hed_tag.extension[:-2], allow_placeholders=allow_placeholders
248+
)
249+
elif allow_placeholders and hed_tag.is_unit_class_tag() and hed_tag.extension.startswith("# "):
250+
# If placeholder is followed by units (e.g., "# m-per-s^2"), validate the units
251+
validation_issues += self.validate_units(hed_tag, allow_placeholders=allow_placeholders)
244252
elif not (allow_placeholders and "#" in hed_tag.extension):
245-
validation_issues += self.validate_units(hed_tag)
253+
validation_issues += self.validate_units(hed_tag, allow_placeholders=allow_placeholders)
246254

247255
return validation_issues

hed/validator/util/class_util.py

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,48 @@ def _get_default_value_class_validators(self):
4848

4949
return validator_dict
5050

51-
def check_tag_unit_class_units_are_valid(self, original_tag, validate_text, report_as=None, error_code=None) -> list[dict]:
51+
def check_tag_unit_class_units_are_valid(
52+
self, original_tag, validate_text, report_as=None, error_code=None, allow_placeholders=True
53+
) -> list[dict]:
5254
"""Report incorrect unit class or units.
5355
5456
Parameters:
5557
original_tag (HedTag): The original tag that is used to report the error.
5658
validate_text (str): The text to validate.
5759
report_as (HedTag): Report errors as coming from this tag, rather than original_tag.
5860
error_code (str): Override error codes.
61+
allow_placeholders (bool): Whether placeholders are allowed (affects value class validation for "#")
5962
6063
Returns:
6164
list: Validation issues. Each issue is a dictionary.
6265
"""
66+
if not original_tag.is_unit_class_tag():
67+
return []
68+
6369
validation_issues = []
64-
if original_tag.is_unit_class_tag():
65-
66-
# Check the units first
67-
stripped_value, units = original_tag.get_stripped_unit_value(validate_text)
68-
if not stripped_value:
69-
validation_issues += self._report_bad_units(original_tag, report_as)
70-
return validation_issues
71-
72-
# Check the value classes
73-
validation_issues += self._check_value_class(original_tag, stripped_value, report_as)
74-
if validation_issues:
75-
return validation_issues
76-
77-
# We don't want to give this overall error twice
78-
if error_code and validation_issues and not any(error_code == issue["code"] for issue in validation_issues):
79-
new_issue = validation_issues[0].copy()
80-
new_issue["code"] = error_code
81-
validation_issues += [new_issue]
70+
# Check the units first
71+
stripped_value, units = original_tag.get_stripped_unit_value(validate_text)
72+
if not stripped_value:
73+
# stripped_value is None only when invalid units are present
74+
validation_issues += self._report_bad_units(original_tag, report_as)
75+
return validation_issues
76+
77+
# If value is a placeholder (#) and placeholders are allowed, it's valid
78+
# Invalid units would have been caught above (stripped_value would be None)
79+
if stripped_value == "#" and allow_placeholders:
80+
return validation_issues
81+
82+
# Check the value classes
83+
# If placeholders are NOT allowed, "#" will fail value class validation (e.g., not a valid number)
84+
validation_issues += self._check_value_class(original_tag, stripped_value, report_as)
85+
if validation_issues:
86+
return validation_issues
87+
88+
# We don't want to give this overall error twice
89+
if error_code and validation_issues and not any(error_code == issue["code"] for issue in validation_issues):
90+
new_issue = validation_issues[0].copy()
91+
new_issue["code"] = error_code
92+
validation_issues += [new_issue]
8293

8394
return validation_issues
8495

0 commit comments

Comments
 (0)