def test_validator(self): events = BidsEventFiles(Test.root_path) hed_schema = \ load_schema('https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml') validator = HedValidator(hed_schema) validation_issues = events.validate(validators=[validator], check_for_warnings=False) self.assertFalse(validation_issues, "BidsEventFiles should have no validation errors") validation_issues = events.validate(validators=[validator], check_for_warnings=True) self.assertTrue(validation_issues, "BidsEventFiles should have validation warnings") self.assertEqual( len(validation_issues), 6, "BidsEventFiles should have 2 validation warnings for missing columns" )
""" Examples of creating a HedValidator and validating various Hed Strings with it. Classes Demonstrated: HedValidator - Validates a given input string or file """ from hed.errors.error_reporter import get_printable_issue_string from hed.models.hed_string import HedString from hed.schema.hed_schema_io import load_schema from hed.validator.hed_validator import HedValidator if __name__ == '__main__': hed_xml_url = 'https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml' hed_schema = load_schema(hed_xml_url) hed_validator = HedValidator(hed_schema) hed_validator_no_semantic = HedValidator(run_semantic_validation=False) hed_string_1 = \ "Sensory-event,Visual-presentation,Experimental-stimulus,Green,Non-target," + \ "(Letter/D, (Center-of, Computer-screen))" string_obj_1 = HedString(hed_string_1) validation_issues = string_obj_1.validate(hed_validator) print( get_printable_issue_string( validation_issues, title= '[Example 1] hed_string_1 should have no issues with HEDv8.0.0')) string_1_long, issues = string_obj_1.convert_to_short(hed_schema) print(
local_hed_file = '../../data/schema_data/HED7.2.0.xml' # path HED v7.1.1 stored locally example_data_path = '../../data/spreadsheet_data' # path to example data valid_tsv_file = os.path.join(example_data_path, 'ValidTwoColumnHED7_1_1.tsv') valid_tsv_file_no_header = os.path.join( example_data_path, 'ValidTwoColumnHED7_1_1NoHeader.tsv') valid_tsv_file_separate_cols = os.path.join(example_data_path, 'LKTEventCodesHED2.tsv') unsupported_csv_format = os.path.join(example_data_path, 'UnsupportedFormatCSV.csv') multiple_sheet_xlsx_file = os.path.join(example_data_path, 'ExcelMultipleSheets.xlsx') hed_schema_cached = load_schema_version(xml_version_number='7.1.1') hed_validator_old = HedValidator(hed_schema=hed_schema_cached) hed_schema_local = load_schema(local_hed_file) hed_validator_local = HedValidator(hed_schema=hed_schema_local) hed_validator_local_warnings = HedValidator(hed_schema=hed_schema_local) # Example 1a: Valid TSV file with default version of HED print(valid_tsv_file) input_file = HedInput(valid_tsv_file, tag_columns=[2]) validation_issues = input_file.validate_file(hed_validator_old, check_for_warnings=True) the_title = '[Example 1a] ValidTwoColumnHED7_1_1 is probably okay with default version of HED' print(get_printable_issue_string(validation_issues, title=the_title)) # Example 1b: Valid TSV file with specified local version of HED print(valid_tsv_file) input_file = HedInput(valid_tsv_file, tag_columns=[2]) validation_issues = input_file.validate_file(hed_validator_local)
if issues: return issues for event_obj in self.events_dict.values(): contents = event_obj.contents if not contents: contents = EventsInput(file=event_obj.file_path, sidecars=event_obj.sidecars) if keep_events: event_obj.my_contents = contents issues += contents.validate_file(validators=validators, check_for_warnings=check_for_warnings) return issues if __name__ == '__main__': path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '../../../tests/data/bids/eeg_ds003654s_hed') bids = BidsEventFiles(path) for file_obj in bids.sidecar_dict.values(): print(file_obj) for file_obj in bids.events_dict.values(): print(file_obj) print("Now validating.....") hed_schema = \ load_schema('https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml') validator = HedValidator(hed_schema=hed_schema) validation_issues = bids.validate(validators=[validator], check_for_warnings=False) issue_str = get_printable_issue_string(validation_issues, skip_filename=False) print(f"Issues: {issue_str}")
hed_string_obj = HedString(input_string) return hed_string_obj.convert_to_short(hed_schema) def short_to_long_string(input_string, hed_schema): hed_string_obj = HedString(input_string) return hed_string_obj.convert_to_long(hed_schema) if __name__ == '__main__': # Convert a spreadsheet to data_path = '../../data/spreadsheet_data' base_name = 'ExcelOneSheet' single_sheet = os.path.join(data_path, base_name + '.xlsx') loaded_schema = load_schema(local_hed) prefixed_needed_tag_columns = {2: 'Property/Informational-property/Label/', 3: 'Property/Informational-property/Description/'} loaded_file = HedInput(single_sheet, tag_columns=[4], column_prefix_dictionary=prefixed_needed_tag_columns, worksheet_name='LKT 8HED3') filename = os.path.join(data_path, base_name + '_short_form.xlsx') long_to_short_file(loaded_file, filename, loaded_schema) filename = os.path.join(data_path, base_name + '_long_form.xlsx') loaded_file = HedInput(single_sheet, tag_columns=[4], column_prefix_dictionary=prefixed_needed_tag_columns, worksheet_name='LKT 8HED3') short_to_long_file(loaded_file, filename, loaded_schema) inputs = 'Attribute/Sensory/Visual/Color/CSS-color/White-color/White'
import os from hed.errors.error_reporter import get_printable_issue_string from hed.models.sidecar import Sidecar from hed.models.events_input import EventsInput from hed.validator.hed_validator import HedValidator from hed.schema.hed_schema_io import load_schema from hed.schema.hed_schema_group import HedSchemaGroup if __name__ == '__main__': hed_xml_url = 'https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml' hed_library_url1 = \ 'https://raw.githubusercontent.com/hed-standard/hed-schema-library/main/hedxml/HED_score_0.0.1.xml' hed_library_url2 = \ 'https://raw.githubusercontent.com/hed-standard/hed-schema-library/main/hedxml/HED_test_1.0.2.xml' hed_schema = load_schema(hed_xml_url) hed_schema_lib1 = load_schema(hed_library_url1) hed_schema_lib1.set_library_prefix("sc") hed_schema_lib2 = load_schema(hed_library_url2) hed_schema_lib2.set_library_prefix("test") events_file = os.path.join( '../../../datasets/eeg_ds003654s_hed_library/sub-003/eeg/sub-003_task-FacePerception_run-2_events.tsv' ) json_file = os.path.join( '../../../datasets/eeg_ds003654s_hed_library/task-FacePerception_events.json' ) schema_group = HedSchemaGroup( [hed_schema, hed_schema_lib1, hed_schema_lib2]) validator = HedValidator(hed_schema=schema_group)
""" Example of converting a hed schema from .mediawiki format to .xml format. """ from hed.util.file_util import write_strings_to_file from hed.schema.hed_schema_io import load_schema if __name__ == '__main__': # Convert mediawiki to XML and write to a temporary file hed_wiki_url = 'https://raw.githubusercontent.com/hed-standard/hed-specification/master' + \ '/hedwiki/HED8.0.0.mediawiki' print("Converting HED8.0.0.mediawiki to XML:") hed_schema = load_schema(hed_wiki_url) xml_file_string = hed_schema.get_as_xml_string() if xml_file_string: xml_location = write_strings_to_file(xml_file_string) print(f"XML temporary file: {xml_location}")