From 8daebfa67febedd5a39d4fa9126ed58b24c7a8ca Mon Sep 17 00:00:00 2001 From: Kay Robbins <1189050+VisLab@users.noreply.github.com> Date: Wed, 24 Apr 2024 14:15:45 -0500 Subject: [PATCH 1/3] Added a get_sidecar to TabularInput --- hed/models/tabular_input.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/hed/models/tabular_input.py b/hed/models/tabular_input.py index 58346883..6ff9ce8a 100644 --- a/hed/models/tabular_input.py +++ b/hed/models/tabular_input.py @@ -81,3 +81,7 @@ def get_column_refs(self): if self._sidecar: return self._sidecar.get_column_refs() return [] + + def get_sidecar(self): + """Return the sidecar associated with this TabularInput.""" + return self._sidecar \ No newline at end of file From 94671fe2fd59657387c7d585f34f68da4c255f11 Mon Sep 17 00:00:00 2001 From: Kay Robbins <1189050+VisLab@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:14:34 -0500 Subject: [PATCH 2/3] Added a str_list to annotation utilities to handle None for MATLAB --- hed/tools/analysis/annotation_util.py | 14 ++++++++++++++ tests/tools/analysis/test_annotation_util.py | 19 +++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/hed/tools/analysis/annotation_util.py b/hed/tools/analysis/annotation_util.py index 085da3bb..1f7b9476 100644 --- a/hed/tools/analysis/annotation_util.py +++ b/hed/tools/analysis/annotation_util.py @@ -173,6 +173,20 @@ def merge_hed_dict(sidecar_dict, hed_dict): sidecar_dict[key]['Levels'] = value_dict['Levels'] +def str_list(obj_list): + """ Return a list with the objects converted to string except for None elements. + + Parameters: + obj_list (list): A list of objects that are None or have a str method. + + Returns: + list: A list with the objects converted to strings -- except None values are preserved. + """ + + # Using list comprehension to convert non-None items to strings + return [str(item) if item is not None else None for item in obj_list] + + def str_to_tabular(tsv_str, sidecar=None): """ Return a TabularInput a tsv string. diff --git a/tests/tools/analysis/test_annotation_util.py b/tests/tools/analysis/test_annotation_util.py index 3df6a381..8f4f5994 100644 --- a/tests/tools/analysis/test_annotation_util.py +++ b/tests/tools/analysis/test_annotation_util.py @@ -6,11 +6,11 @@ from hed import schema as hedschema from hed.errors import HedFileError from hed.models.sidecar import Sidecar +from hed.models.hed_string import HedString from hed.tools.analysis.annotation_util import check_df_columns, df_to_hed, extract_tags,\ - hed_to_df, merge_hed_dict, strs_to_sidecar, str_to_tabular + hed_to_df, merge_hed_dict, strs_to_sidecar, str_to_tabular, str_list from hed.tools.analysis.annotation_util import _flatten_cat_col, _flatten_val_col, _get_value_entry, _tag_list_to_str, \ _update_cat_dict, generate_sidecar_entry -# from hed.tools.analysis.annotation_util import _find_last_pos, _find_first_pos, trim_back, trim_front from hed.tools.analysis.tabular_summary import TabularSummary from hed.tools.util.io_util import get_file_list @@ -308,6 +308,21 @@ def test_strs_to_tabular(self): events_contents = file.read() tab_in = str_to_tabular(events_contents, sidecar=self.json_path) + def test_str_list(self): + # schema + # list1 = [HedString('Red, Sensory-event', schema)] + list1 = ['abc', '', None, 3.24] + str_list1 = str_list(list1) + self.assertEqual(len(str_list1), len(list1)) + self.assertIsNone(str_list1[2], None) + self.assertEqual(str_list1[3], '3.24') + self.assertFalse(str_list1[1]) + list2 = [HedString('Red, Sensory-event', self.hed_schema), None, HedString('', self.hed_schema)] + str_list2 = str_list(list2) + self.assertEqual(len(str_list2), len(list2)) + self.assertIsNone(str_list2[1], None) + self.assertEqual(str_list2[0], 'Red,Sensory-event') + self.assertEqual(str_list2[2], '') def test_flatten_cat_col(self): col1 = self.sidecar2c["a"] From 100fa961a9e53ad5629ffe965467ce820db8f1a0 Mon Sep 17 00:00:00 2001 From: Kay Robbins <1189050+VisLab@users.noreply.github.com> Date: Thu, 25 Apr 2024 09:32:31 -0500 Subject: [PATCH 3/3] Updated the strlist renamed to_strlist --- hed/tools/analysis/annotation_util.py | 30 ++++++++++---------- tests/tools/analysis/test_annotation_util.py | 12 ++++---- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/hed/tools/analysis/annotation_util.py b/hed/tools/analysis/annotation_util.py index 1f7b9476..3b9aa265 100644 --- a/hed/tools/analysis/annotation_util.py +++ b/hed/tools/analysis/annotation_util.py @@ -173,26 +173,12 @@ def merge_hed_dict(sidecar_dict, hed_dict): sidecar_dict[key]['Levels'] = value_dict['Levels'] -def str_list(obj_list): - """ Return a list with the objects converted to string except for None elements. - - Parameters: - obj_list (list): A list of objects that are None or have a str method. - - Returns: - list: A list with the objects converted to strings -- except None values are preserved. - """ - - # Using list comprehension to convert non-None items to strings - return [str(item) if item is not None else None for item in obj_list] - - def str_to_tabular(tsv_str, sidecar=None): """ Return a TabularInput a tsv string. Parameters: tsv_str (str): A string representing a tabular input. - sidecar (Sidecar): An optional Sidecar object. + sidecar (Sidecar, str, File or File-like): An optional Sidecar object. Returns: TabularInput: Represents a tabular input object. @@ -222,6 +208,20 @@ def strs_to_sidecar(sidecar_strings): return None +def to_strlist(obj_list): + """ Return a list with the objects converted to string except for None elements. + + Parameters: + obj_list (list): A list of objects that are None or have a str method. + + Returns: + list: A list with the objects converted to strings -- except None values are preserved. + """ + + # Using list comprehension to convert non-None items to strings + return [str(item) if item is not None else None for item in obj_list] + + def _flatten_cat_col(col_key, col_dict): """ Flatten a sidecar entry corresponding to a categorical column. diff --git a/tests/tools/analysis/test_annotation_util.py b/tests/tools/analysis/test_annotation_util.py index 8f4f5994..0d655c44 100644 --- a/tests/tools/analysis/test_annotation_util.py +++ b/tests/tools/analysis/test_annotation_util.py @@ -7,8 +7,9 @@ from hed.errors import HedFileError from hed.models.sidecar import Sidecar from hed.models.hed_string import HedString -from hed.tools.analysis.annotation_util import check_df_columns, df_to_hed, extract_tags,\ - hed_to_df, merge_hed_dict, strs_to_sidecar, str_to_tabular, str_list +from hed.models.tabular_input import TabularInput +from hed.tools.analysis.annotation_util import check_df_columns, df_to_hed, extract_tags, \ + hed_to_df, merge_hed_dict, strs_to_sidecar, str_to_tabular, to_strlist from hed.tools.analysis.annotation_util import _flatten_cat_col, _flatten_val_col, _get_value_entry, _tag_list_to_str, \ _update_cat_dict, generate_sidecar_entry from hed.tools.analysis.tabular_summary import TabularSummary @@ -307,18 +308,19 @@ def test_strs_to_tabular(self): with open(self.events_path, 'r') as file: events_contents = file.read() tab_in = str_to_tabular(events_contents, sidecar=self.json_path) + self.assertIsInstance(tab_in, TabularInput) - def test_str_list(self): + def test_convert_to_strlist(self): # schema # list1 = [HedString('Red, Sensory-event', schema)] list1 = ['abc', '', None, 3.24] - str_list1 = str_list(list1) + str_list1 = to_strlist(list1) self.assertEqual(len(str_list1), len(list1)) self.assertIsNone(str_list1[2], None) self.assertEqual(str_list1[3], '3.24') self.assertFalse(str_list1[1]) list2 = [HedString('Red, Sensory-event', self.hed_schema), None, HedString('', self.hed_schema)] - str_list2 = str_list(list2) + str_list2 = to_strlist(list2) self.assertEqual(len(str_list2), len(list2)) self.assertIsNone(str_list2[1], None) self.assertEqual(str_list2[0], 'Red,Sensory-event')