Skip to content

Commit

Permalink
Updated validator and EventManager to handle n/a onsets
Browse files Browse the repository at this point in the history
  • Loading branch information
VisLab committed Oct 11, 2024
1 parent a5b99e3 commit 9bf1089
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 18 deletions.
2 changes: 1 addition & 1 deletion hed/tools/remodeling/operations/factor_hed_tags_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion hed/tools/remodeling/operations/factor_hed_type_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion hed/tools/remodeling/operations/merge_consecutive_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 6 additions & 7 deletions hed/tools/remodeling/operations/summarize_hed_tags_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])

Expand Down Expand Up @@ -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.
"""

Expand All @@ -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:
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hed/tools/remodeling/operations/summarize_hed_type_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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']:
Expand Down
46 changes: 45 additions & 1 deletion tests/tools/analysis/test_event_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))"
}
}
}

Expand Down Expand Up @@ -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"],
Expand All @@ -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)
Expand All @@ -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'])
Expand Down

0 comments on commit 9bf1089

Please sign in to comment.