Skip to content

Commit

Permalink
Merge pull request #911 from VisLab/develop
Browse files Browse the repository at this point in the history
Included a str2_to_sidecars utility
  • Loading branch information
VisLab authored Apr 22, 2024
2 parents c0e4532 + ebf650e commit f6a388f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
23 changes: 23 additions & 0 deletions hed/tools/analysis/annotation_util.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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.
Expand Down
1 change: 0 additions & 1 deletion tests/models/test_sidecar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions tests/schema/test_schema_compare.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down
12 changes: 11 additions & 1 deletion tests/tools/analysis/test_annotation_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f6a388f

Please sign in to comment.