def test_hed_group_summary_str(self): hed_url_path = 'https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml' hed_schema = load_schema(hed_url_path) sidecar = Sidecar(self.bids_path) x = sidecar._column_data['hed_def_conds'].def_dict._defs y = x['scrambled-face-cond'] test_summary = HedGroupSummary(y.contents, hed_schema, name=y.name, keep_all_values=True) self.assertTrue(str(test_summary).startswith('Scrambled-face-cond'), 'HedGroupSummary has a string method')
def test_hed_group_summary_constructor(self): hed_url_path = 'https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml' hed_schema = load_schema(hed_url_path) sidecar = Sidecar(self.bids_path) x = sidecar._column_data['hed_def_conds'].def_dict._defs y = x['scrambled-face-cond'] test_summary = HedGroupSummary(y.contents, hed_schema, name=y.name, keep_all_values=True) self.assertEqual(test_summary.name, 'Scrambled-face-cond', 'HedGroupSummary has the right name') self.assertEqual(len(test_summary.tag_dict), 5, 'HedGroupSummary has the right number of tags')
def __init__(self, file=None, sidecars=None, attribute_columns=None, extra_def_dicts=None, also_gather_defs=True, name=None): """Constructor for the EventsInput class. Parameters ---------- file: str or file like An xlsx/tsv file to open. sidecars : str or [str] or Sidecar or [Sidecar] A list of json files to pull column metadata from attribute_columns: str or int or [str] or [int] A list of column names or numbers to treat as attributes. Default: ["duration", "onset"] extra_def_dicts: [DefDict] or DefDict or None DefDict objects containing all the definitions this file should use other than the ones coming from the file itself and from the column def groups. These are added as the last entries, so names will override earlier ones. also_gather_defs: bool Default to true. If False, do NOT extract any definitions from column groups, assume they are already in the def_dict list. name: str The name to display for this file for error purposes. """ if attribute_columns is None: attribute_columns = ["duration", "onset"] if sidecars: sidecars = Sidecar.load_multiple_sidecars(sidecars) new_mapper = ColumnMapper(sidecars=sidecars, optional_tag_columns=[self.HED_COLUMN_NAME], attribute_columns=attribute_columns) self._also_gather_defs = also_gather_defs self._extra_def_dicts = extra_def_dicts def_mapper = self.create_def_mapper(new_mapper, extra_def_dicts) super().__init__(file, file_type=".tsv", worksheet_name=None, has_column_names=True, mapper=new_mapper, def_mapper=def_mapper, name=name) if not self._has_column_names: raise ValueError( "You are attempting to open a bids_old style file with no column headers provided.\n" "This is probably not intended.")
def add_sidecars(self, sidecars): """ Gathers column definitions from a list of files and adds them to the column mapper. Parameters ---------- sidecars : [str or Sidecar] A list of filenames or loaded files in any mix """ self._has_sidecars = True sidecars = Sidecar.load_multiple_sidecars(sidecars) for sidecar in sidecars: for column_data in sidecar: self._add_column_data(column_data)
else: tag_values[key] = value_list if tag_values: json_dict["Tags with values:"] = tag_values if as_json: return json.dumps(json_dict) else: return json.dumps(json_dict, indent=4, sort_keys=True) def __str__(self): return f"{self.name}: {self.get_description()} Tags: {str(list(self.tag_dict.keys()))}" if __name__ == '__main__': the_path = '../../../tests/data/bids/eeg_ds003654s_hed/task-FacePerception_events.json' sidecar = Sidecar(the_path) hed_schema = \ load_schema('https://raw.githubusercontent.com/hed-standard/hed-specification/master/hedxml/HED8.0.0.xml') def_dicts = [column_entry.def_dict for column_entry in sidecar] group_list = [] for def_dict in def_dicts: this_dict = def_dict._defs for def_name, def_entry in this_dict.items(): x = HedGroupSummary(def_entry.contents, hed_schema, name=def_entry.name, keep_all_values=True) group_list.append(x) print("String representation:") print(f"{str(x)}") print("Dumping Json:") print(f"{x.to_json(with_values=True, as_json=False)}") print("whew")
from tempfile import TemporaryFile from hed.errors.error_reporter import get_printable_issue_string from hed.models.hed_string import HedString from hed.models.sidecar import Sidecar 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) json_filename = "../../../datasets/eeg_ds003654s_hed/task-FacePerception_events.json" # Example 1 print("\nExample 1 demonstrating Sidecar validation....") sidecar = Sidecar(json_filename) validator = HedValidator(hed_schema) issues = sidecar.validate_entries(validator, check_for_warnings=True) if issues: print(get_printable_issue_string(issues), "JSON dictionary from eeg_ds003654s_hed has validation errors") else: print( "JSON dictionary from eeg_ds003654s_hed has no validation errors") # Example 2: Convert JSON to long and output it. print("\n\nExample 2 converting a Sidecar to long in place ...") tag_form = 'long_tag' validator = HedValidator(hed_schema) for hed_string_obj, position_info, issue_items in sidecar.hed_string_iter( validators=validator, expand_defs=False, allow_placeholder=True):
def set_contents(self): self.contents = Sidecar(self.file_path, name=os.path.abspath(self.file_path))
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) sidecar = Sidecar(json_file) input_file = EventsInput(events_file, sidecars=sidecar) issues = input_file.validate_file_sidecars(validator, check_for_warnings=False) issues += input_file.validate_file(validator, check_for_warnings=False) print( get_printable_issue_string( issues, "Validating a Bids event file with its JSON sidecar"))