diff --git a/hed/tools/remodeling/operations/factor_hed_tags_op.py b/hed/tools/remodeling/operations/factor_hed_tags_op.py index 1e60894c..7222c2a4 100644 --- a/hed/tools/remodeling/operations/factor_hed_tags_op.py +++ b/hed/tools/remodeling/operations/factor_hed_tags_op.py @@ -119,7 +119,7 @@ def do_op(self, dispatcher, df, name, sidecar=None): raise ValueError("QueryNameAlreadyColumn", f"Query [{query_name}]: is already a column name of the data frame") df_list = [input_data.dataframe] - tag_man = HedTagManager(input_data, dispatcher.hed_schema, remove_types=self.remove_types) + tag_man = HedTagManager(EventManager(input_data, dispatcher.hed_schema), remove_types=self.remove_types) hed_objs = tag_man.get_hed_objs(include_context=self.expand_context, replace_defs=self.replace_defs) df_factors = query_service.search_hed_objs(hed_objs, self.query_handlers, query_names=self.query_names) if len(df_factors.columns) > 0: diff --git a/hed/tools/remodeling/operations/factor_hed_type_op.py b/hed/tools/remodeling/operations/factor_hed_type_op.py index 67b068d1..8a3246c5 100644 --- a/hed/tools/remodeling/operations/factor_hed_type_op.py +++ b/hed/tools/remodeling/operations/factor_hed_type_op.py @@ -73,7 +73,7 @@ def do_op(self, dispatcher, df, name, sidecar=None): input_data = TabularInput(df.copy().fillna('n/a'), sidecar=sidecar, name=name) df_list = [input_data.dataframe] - var_manager = HedTypeManager(input_data, dispatcher.hed_schema) + var_manager = HedTypeManager(EventManager(input_data, dispatcher.hed_schema)) var_manager.add_type(self.type_tag.casefold()) df_factors = var_manager.get_factor_vectors( diff --git a/hed/tools/remodeling/operations/merge_consecutive_op.py b/hed/tools/remodeling/operations/merge_consecutive_op.py index 8041864e..8d193db2 100644 --- a/hed/tools/remodeling/operations/merge_consecutive_op.py +++ b/hed/tools/remodeling/operations/merge_consecutive_op.py @@ -115,7 +115,7 @@ def do_op(self, dispatcher, df, name, sidecar=None): df_new = df.copy() code_mask = df_new[self.column_name] == self.event_code - if sum(code_mask.astype(int)) == 0: + if not code_mask.any(): return df_new match_columns.append(self.column_name) match_df = df_new.loc[:, match_columns] diff --git a/hed/tools/remodeling/operations/summarize_column_names_op.py b/hed/tools/remodeling/operations/summarize_column_names_op.py index 8dbdb15b..03fed614 100644 --- a/hed/tools/remodeling/operations/summarize_column_names_op.py +++ b/hed/tools/remodeling/operations/summarize_column_names_op.py @@ -94,7 +94,7 @@ def __init__(self, sum_op): """ Constructor for column name summary manager. Parameters: - sum_op (BaseOp): Operation associated with this summary. + sum_op (SummarizeColumnNamesOp): Operation associated with this summary. """ super().__init__(sum_op) diff --git a/hed/tools/remodeling/operations/summarize_column_values_op.py b/hed/tools/remodeling/operations/summarize_column_values_op.py index 612e4b5e..5fa5ddff 100644 --- a/hed/tools/remodeling/operations/summarize_column_values_op.py +++ b/hed/tools/remodeling/operations/summarize_column_values_op.py @@ -133,7 +133,7 @@ def __init__(self, sum_op): """ Constructor for column value summary manager. Parameters: - sum_op (BaseOp): Operation associated with this summary. + sum_op (SummarizeColumnValuesOp): Operation associated with this summary. """ super().__init__(sum_op) diff --git a/hed/tools/remodeling/operations/summarize_definitions_op.py b/hed/tools/remodeling/operations/summarize_definitions_op.py index f953d593..65503fce 100644 --- a/hed/tools/remodeling/operations/summarize_definitions_op.py +++ b/hed/tools/remodeling/operations/summarize_definitions_op.py @@ -94,7 +94,7 @@ def __init__(self, sum_op, hed_schema, known_defs=None): """ Constructor for the summary of definitions. Parameters: - sum_op (BaseOp): Summary operation class for gathering definitions. + sum_op (SummarizeDefinitionsOp): Summary operation class for gathering definitions. hed_schema (HedSchema or HedSchemaGroup): Schema used for the dataset. known_defs (str or list or DefinitionDict): Definitions already known to be used. diff --git a/hed/tools/remodeling/operations/summarize_hed_tags_op.py b/hed/tools/remodeling/operations/summarize_hed_tags_op.py index fe6241f8..5e426bae 100644 --- a/hed/tools/remodeling/operations/summarize_hed_tags_op.py +++ b/hed/tools/remodeling/operations/summarize_hed_tags_op.py @@ -187,7 +187,7 @@ def __init__(self, parameters): } if self.word_cloud["use_mask"] and not self.word_cloud["mask_path"]: self.word_cloud["mask_path"] = os.path.realpath( - os.path.join(os.path.dirname(__file__), '../../../resources/word_cloud_brain_mask.png')) + os.path.join(os.path.dirname(__file__), '../../../resources/word_cloud_brain_mask.png')) if self.word_cloud["font_path"]: self.word_cloud["font_path"] = os.path.realpath(self.word_cloud["font_path"]) @@ -224,11 +224,12 @@ def validate_input_data(parameters): class HedTagSummary(BaseSummary): """ Manager of the HED tag summaries. """ + def __init__(self, sum_op): """ Constructor for HED tag summary manager. Parameters: - sum_op (BaseOp): Operation associated with this summary. + sum_op (SummarizeHedTagsOp): Operation associated with this summary. """ @@ -249,7 +250,7 @@ def update_summary(self, new_info): new_info['name'], total_events=len(new_info['df'])) input_data = TabularInput( new_info['df'], sidecar=new_info['sidecar'], name=new_info['name']) - tag_man = HedTagManager(input_data, new_info['schema'], remove_types=self.sum_op.remove_types) + tag_man = HedTagManager(EventManager(input_data, new_info['schema']), remove_types=self.sum_op.remove_types) hed_objs = tag_man.get_hed_objs(include_context=self.sum_op.include_context, replace_defs=self.sum_op.replace_defs) for hed in hed_objs: @@ -392,8 +393,7 @@ def _get_dataset_string(result, indent=BaseSummary.DISPLAY_INDENT): """ sum_list = [f"Dataset: Total events={result.get('Total events', 0)} " f"Total files={len(result.get('Files', []))}"] - sum_list = sum_list + \ - HedTagSummary._get_tag_list(result, indent=indent) + sum_list = sum_list + HedTagSummary._get_tag_list(result, indent=indent) return "\n".join(sum_list) @staticmethod @@ -409,8 +409,7 @@ def _get_individual_string(result, indent=BaseSummary.DISPLAY_INDENT): """ sum_list = [f"Total events={result.get('Total events', 0)}"] - sum_list = sum_list + \ - HedTagSummary._get_tag_list(result, indent=indent) + sum_list = sum_list + HedTagSummary._get_tag_list(result, indent=indent) return "\n".join(sum_list) @staticmethod diff --git a/hed/tools/remodeling/operations/summarize_hed_type_op.py b/hed/tools/remodeling/operations/summarize_hed_type_op.py index daa26440..36a9667f 100644 --- a/hed/tools/remodeling/operations/summarize_hed_type_op.py +++ b/hed/tools/remodeling/operations/summarize_hed_type_op.py @@ -108,7 +108,7 @@ def __init__(self, sum_op): """ Constructor for HED type summary manager. Parameters: - sum_op (BaseOp): Operation associated with this summary. + sum_op (SummarizeHedTypeOp): Operation associated with this summary. """ super().__init__(sum_op) diff --git a/hed/tools/remodeling/operations/summarize_hed_validation_op.py b/hed/tools/remodeling/operations/summarize_hed_validation_op.py index 3f720740..6549c4c6 100644 --- a/hed/tools/remodeling/operations/summarize_hed_validation_op.py +++ b/hed/tools/remodeling/operations/summarize_hed_validation_op.py @@ -106,7 +106,7 @@ def __init__(self, sum_op): """ Constructor for validation issue manager. Parameters: - sum_op (BaseOp): Operation associated with this summary. + sum_op (SummarizeHedValidationOp): Operation associated with this summary. """ super().__init__(sum_op) @@ -153,8 +153,7 @@ def update_summary(self, new_info): sidecar = new_info.get('sidecar', None) if sidecar and not isinstance(sidecar, Sidecar): - sidecar = Sidecar( - files=new_info['sidecar'], name=os.path.basename(sidecar)) + sidecar = Sidecar(files=new_info['sidecar'], name=os.path.basename(sidecar)) results = self._get_sidecar_results( sidecar, new_info, self.sum_op.check_for_warnings) if not results['sidecar_had_issues']: diff --git a/tests/tools/analysis/test_event_manager.py b/tests/tools/analysis/test_event_manager.py index 09474d6d..8a4024d5 100644 --- a/tests/tools/analysis/test_event_manager.py +++ b/tests/tools/analysis/test_event_manager.py @@ -33,6 +33,11 @@ def setUpClass(cls): "whatever": "Black", "whatelse": "Purple" } + }, + "defs": { + "HED": { + "defs1": "(Definition/Con1, (Condition-variable/Cond-one)), (Definition/Con2, (Condition-variable/Cond-one))" + } } } @@ -61,7 +66,7 @@ def test_constructor(self): self.assertEqual(event.end_index, len(manager1.input_data.dataframe)) def test_no_onset_constructor(self): - # + # No onsets --- has an event manager tsv = { "event_code": ["show", "respond", "show", "respond", "whatever", "show", "whatelse", "respond"], "HED": ["Age/100", "n/a", "n/a", "n/a", "Green", "n/a", "Female", "n/a"], @@ -79,6 +84,43 @@ def test_no_onset_constructor(self): self.assertIsNone(eman2.onsets) self.assertEqual(str(eman2.hed_strings[0]), "Age/100,Sensory-event,Visual-presentation") self.assertFalse(str(eman2.hed_strings[2])) + self.assertIsNone(eman2.base) + self.assertIsNone(eman2.context) + + def test_bad_onset_constructor(self): + tsv = { + "onset": [0.0, 1.2, 1.2, 3.0, 5, 3.5, 4, 6], + "duration": [0.5, "n/a", "n/a", "n/a", "n/a", "n/a", "n/a", "n/a"], + "event_code": ["show", "respond", "show", "respond", "whatever", "show", "whatelse", "respond"], + "HED": ["Age/100", "n/a", "n/a", "n/a", "Green", "n/a", "Female", "n/a"], + } + + tab = TabularInput(pd.DataFrame(tsv), sidecar=self.sidecar2) + with self.assertRaises(HedFileError): + EventManager(tab, self.schema) + + tsv = { + "onset": [0.0, 1.2, 1.2, 3.0, "n/a", 3.5, "n/a", 6], + "duration": [0.5, "n/a", "n/a", "n/a", "n/a", "n/a", "n/a", "n/a"], + "event_code": ["show", "respond", "show", "respond", "whatever", "show", "whatelse", "respond"], + "HED": ["Age/100", "n/a", "n/a", "n/a", "Green", "n/a", "Female", "n/a"], + } + tab = TabularInput(pd.DataFrame(tsv), sidecar=self.sidecar2) + with self.assertRaises(HedFileError): + EventManager(tab, self.schema) + + def test_unfold_no_onset(self): + tsv = { + "event_code": ["show", "respond", "show", "respond", "whatever", "show", "whatelse", "respond"], + "HED": ["Age/100,Condition-variable/Temp", "Def/Con1", "Def/Con2", "n/a", "Green", "n/a", "Female", "n/a"], + } + tab = TabularInput(pd.DataFrame(tsv), sidecar=self.sidecar2) + defs = self.sidecar2.get_def_dict(self.schema) + manager1 = EventManager(tab, self.schema) + hed1, base1, context1 = manager1.unfold_context() + hed2, base2, context2 = manager1.unfold_context(remove_types=["Condition-variable"]) + self.assertEqual(hed1[1], "Def/Con1,Press") + self.assertEqual(hed2[1], "Press") def test_unfold_context_no_remove(self): manager1 = EventManager(self.input_data, self.schema) @@ -87,6 +129,8 @@ def test_unfold_context_no_remove(self): self.assertIsInstance(hed[index], str) self.assertIsInstance(base[index], str) + + def test_unfold_context_remove(self): manager1 = EventManager(self.input_data, self.schema) hed, base, context = manager1.unfold_context(remove_types=['Condition-variable', 'Task'])