Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added utilities for accessing sidecars and tabular input #913

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion hed/tools/analysis/annotation_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import io
import re
from pandas import DataFrame
from hed.models.sidecar import Sidecar
from hed.models import Sidecar, TabularInput
from hed.errors.exceptions import HedFileError
from hed.models.df_util import replace_ref

Expand Down Expand Up @@ -173,6 +173,20 @@ def merge_hed_dict(sidecar_dict, hed_dict):
sidecar_dict[key]['Levels'] = value_dict['Levels']


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.

Returns:
TabularInput: Represents a tabular input object.
"""

return TabularInput(file=io.StringIO(tsv_str), sidecar=sidecar)


def strs_to_sidecar(sidecar_strings):
""" Return a Sidecar from a sidecar as string or as a list of sidecars as strings.

Expand Down
28 changes: 18 additions & 10 deletions tests/tools/analysis/test_annotation_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
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, strs_to_sidecar
hed_to_df, merge_hed_dict, strs_to_sidecar, str_to_tabular
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 All @@ -27,6 +27,8 @@ def setUpClass(cls):
'../../data/schema_tests/HED8.2.0.xml'))
cls.bids_root_path = bids_root_path
json_path = os.path.realpath(os.path.join(bids_root_path, 'task-FacePerception_events.json'))
cls.events_path = os.path.realpath(os.path.join(bids_root_path, 'sub-002', 'eeg',
'sub-002_task-FacePerception_run-1_events.tsv'))
cls.json_path = json_path
json_sm_path = os.path.realpath(os.path.join(curation_base_dir, 'task-FacePerceptionSmall_events.json'))
cls.sidecar1a = {"a": {"c": {"c1": "blech3", "c2": "blech3a"}, "d": "blech4", "e": "blech5"},
Expand Down Expand Up @@ -210,15 +212,6 @@ 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 Expand Up @@ -301,6 +294,21 @@ def test_merge_hed_dict_full(self):
merge_hed_dict(example_sidecar, spreadsheet_sidecar)
self.assertEqual(6, len(example_sidecar), 'merge_hed_dict merges with the correct length')

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_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)


def test_flatten_cat_col(self):
col1 = self.sidecar2c["a"]
col2 = self.sidecar2c["b"]
Expand Down
Loading