diff --git a/hed/tools/analysis/annotation_util.py b/hed/tools/analysis/annotation_util.py index 078a2968..2165f783 100644 --- a/hed/tools/analysis/annotation_util.py +++ b/hed/tools/analysis/annotation_util.py @@ -1,7 +1,9 @@ """ Utilities to facilitate annotation of events in BIDS. """ +import io import re from pandas import DataFrame +from hed.models.sidecar import Sidecar from hed.errors.exceptions import HedFileError from hed.models.df_util import replace_ref @@ -171,6 +173,27 @@ def merge_hed_dict(sidecar_dict, hed_dict): sidecar_dict[key]['Levels'] = value_dict['Levels'] +def strs_to_sidecar(sidecar_strings): + """ Return a Sidecar from a sidecar as string or as a list of sidecars as strings. + + Parameters: + sidecar_strings (string or list): String or strings representing sidecars. + + Returns: + Sidecar: the merged sidecar from the list. + """ + + if not isinstance(sidecar_strings, list): + sidecar_strings = [sidecar_strings] + if sidecar_strings: + file_list = [] + for s_string in sidecar_strings: + file_list.append(io.StringIO(s_string)) + return Sidecar(files=file_list, name="Merged_Sidecar") + else: + return None + + def _flatten_cat_col(col_key, col_dict): """ Flatten a sidecar entry corresponding to a categorical column. diff --git a/tests/models/test_sidecar.py b/tests/models/test_sidecar.py index 9fbb72d4..c8e8e5da 100644 --- a/tests/models/test_sidecar.py +++ b/tests/models/test_sidecar.py @@ -5,7 +5,6 @@ from hed.errors import HedFileError, ValidationErrors from hed.models import ColumnMetadata, HedString, Sidecar -from hed.validator import HedValidator from hed import schema from hed.models import DefinitionDict from hed.errors import ErrorHandler diff --git a/tests/schema/test_schema_compare.py b/tests/schema/test_schema_compare.py index f3ceb273..3409466f 100644 --- a/tests/schema/test_schema_compare.py +++ b/tests/schema/test_schema_compare.py @@ -1,16 +1,17 @@ import unittest -import json import copy from hed.schema import HedKey, HedSectionKey from hed.schema.schema_compare import compare_schemas -from hed.schema.schema_compare import gather_schema_changes, find_matching_tags, pretty_print_change_dict, compare_differences +from hed.schema.schema_compare import (gather_schema_changes, find_matching_tags, pretty_print_change_dict, + compare_differences) from hed import load_schema_version, load_schema -from . import util_create_schemas +from tests.schema import util_create_schemas import os + class TestSchemaComparison(unittest.TestCase): @classmethod def setUpClass(cls): diff --git a/tests/tools/analysis/test_annotation_util.py b/tests/tools/analysis/test_annotation_util.py index 1455d209..88a94ddf 100644 --- a/tests/tools/analysis/test_annotation_util.py +++ b/tests/tools/analysis/test_annotation_util.py @@ -6,7 +6,8 @@ from hed import schema as hedschema from hed.errors import HedFileError from hed.models.sidecar import Sidecar -from hed.tools.analysis.annotation_util import check_df_columns, df_to_hed, extract_tags, hed_to_df, merge_hed_dict +from hed.tools.analysis.annotation_util import check_df_columns, df_to_hed, extract_tags,\ + hed_to_df, merge_hed_dict, strs_to_sidecar 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 @@ -209,6 +210,15 @@ def test_generate_sidecar_entry_non_letters(self): self.assertEqual(entry2['HED'], '(Label/my_-123_10, Label/#)', "generate_sidecar_entry HED entry has correct label when no column values and special chars.") + def test_strs_to_sidecar(self): + with open(self.json_path, 'r') as fp: + sidecar_dict = json.load(fp) + self.assertIsInstance(sidecar_dict, dict) + sidecar_str = json.dumps(sidecar_dict) + self.assertIsInstance(sidecar_str, str) + sidecar_obj = strs_to_sidecar(sidecar_str) + self.assertIsInstance(sidecar_obj, Sidecar) + def test_hed_to_df(self): df1a = hed_to_df(self.sidecar1a, col_names=None) self.assertIsInstance(df1a, DataFrame)