Skip to content

Commit

Permalink
Merge pull request #940 from VisLab/develop
Browse files Browse the repository at this point in the history
Minor update to support hed web
  • Loading branch information
VisLab authored Jun 5, 2024
2 parents 193696a + 317c0fc commit b93bee8
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .codespellrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[codespell]
skip = .git,*.pdf,*.svg,deprecated,*.xml,*.mediawiki,*.omn,*.toml
ignore-words-list = covert,hed,assertIn,parms
2 changes: 1 addition & 1 deletion hed/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from hed.models.sidecar import Sidecar
from hed.models.definition_dict import DefinitionDict
from hed.models.query_handler import QueryHandler
from hed.models.query_service import get_query_handlers, search_strings
from hed.models.query_service import get_query_handlers, search_hed_objs

from hed.schema.hed_schema import HedSchema
from hed.schema.hed_schema_group import HedSchemaGroup
Expand Down
2 changes: 1 addition & 1 deletion hed/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from .definition_dict import DefinitionDict
from .definition_entry import DefinitionEntry
from .query_handler import QueryHandler
from .query_service import get_query_handlers, search_strings
from .query_service import get_query_handlers, search_hed_objs
from .hed_group import HedGroup
from .spreadsheet_input import SpreadsheetInput
from .hed_string import HedString
Expand Down
8 changes: 4 additions & 4 deletions hed/models/query_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ def get_query_handlers(queries, query_names=None):
return expression_parsers, query_names, issues


def search_strings(hed_strings, queries, query_names):
def search_hed_objs(hed_objs, queries, query_names):
""" Return a DataFrame of factors based on results of queries.
Parameters:
hed_strings (list): A list of HedString objects (empty entries or None entries are 0's
hed_objs (list): A list of HedString objects (empty entries or None entries are 0's
queries (list): A list of query strings or QueryHandler objects.
query_names (list): A list of column names for results of queries.
Expand All @@ -54,9 +54,9 @@ def search_strings(hed_strings, queries, query_names):
:raises ValueError:
- If query names are invalid or duplicated.
"""
df_factors = pd.DataFrame(0, index=range(len(hed_strings)), columns=query_names)
df_factors = pd.DataFrame(0, index=range(len(hed_objs)), columns=query_names)
for parse_ind, parser in enumerate(queries):
for index, next_item in enumerate(hed_strings):
for index, next_item in enumerate(hed_objs):
if next_item:
match = parser.search(next_item)
if match:
Expand Down
23 changes: 22 additions & 1 deletion hed/tools/analysis/annotation_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pandas as pd
from pandas import DataFrame, Series
from hed.models.sidecar import Sidecar
from hed.models.hed_string import HedString
from hed.models.tabular_input import TabularInput

from hed.errors.exceptions import HedFileError
Expand Down Expand Up @@ -220,6 +221,27 @@ def str_to_tabular(tsv_str, sidecar=None):
return TabularInput(file=io.StringIO(tsv_str), sidecar=sidecar)


def strs_to_hed_objs(hed_strings, hed_schema):
""" Returns a list of HedString objects from a list of strings.
Parameters:
hed_strings (string or list): String or strings representing HED annotations.
hed_schema (HedSchema or HedSchemaGroup): Schema version for the strings.
Returns:
list or None: list of HedString objects or None.
"""

if not hed_strings:
return None
if not isinstance(hed_strings, list):
hed_strings = [hed_strings]
if hed_strings:
return [HedString(hed, hed_schema=hed_schema) for hed in hed_strings]
else:
return None


def strs_to_sidecar(sidecar_strings):
""" Return a Sidecar from a sidecar as string or as a list of sidecars as strings.
Expand All @@ -242,7 +264,6 @@ def strs_to_sidecar(sidecar_strings):
else:
return None


def to_factor(data, column=None):
"""Convert data to an integer factor list.
Expand Down
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 @@ -122,7 +122,7 @@ def do_op(self, dispatcher, df, name, sidecar=None):
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_strings(hed_objs, self.query_handlers, query_names=self.query_names)
df_factors = query_service.search_hed_objs(hed_objs, self.query_handlers, query_names=self.query_names)
if len(df_factors.columns) > 0:
df_list.append(df_factors)
df_new = pd.concat(df_list, axis=1)
Expand Down
10 changes: 10 additions & 0 deletions tests/tools/analysis/test_annotation_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,16 @@ def test_series_to_factor(self):
self.assertEqual(len(series2), len(factor2))
self.assertEqual(sum(factor2), 1)

def test_strs_to_hed_objs(self):
self.assertIsNone(annotation_util.strs_to_hed_objs('', self.hed_schema))
self.assertIsNone(annotation_util.strs_to_hed_objs(None, self.hed_schema))
self.assertIsNone(annotation_util.strs_to_hed_objs([], self.hed_schema))
hed_objs1 = annotation_util.strs_to_hed_objs(['Sensory-event', 'Red'], self.hed_schema)
self.assertIsInstance(hed_objs1, list)
self.assertEqual(len(hed_objs1), 2)
self.assertEqual('Red', str(hed_objs1[1]))
hed_objs2 = annotation_util.strs_to_hed_objs(['Sensory-event', 'Blech'], self.hed_schema)

def test_strs_to_sidecar(self):
with open(self.json_path, 'r') as fp:
sidecar_dict = json.load(fp)
Expand Down
22 changes: 11 additions & 11 deletions tests/tools/analysis/test_temporal_event.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ def setUpClass(cls):
def test_constructor_no_group(self):
test1 = HedString("(Onset, def/blech)", hed_schema=self.schema)
groups = test1.find_top_level_tags(["onset"], include_groups=1)
te = TemporalEvent(groups[0], 3, 4.5)
self.assertEqual(te.start_index, 3)
self.assertEqual(te.start_time, 4.5)
self.assertEqual(te.anchor, 'Def/blech')
self.assertFalse(te.internal_group)
temp_event = TemporalEvent(groups[0], 3, 4.5)
self.assertEqual(temp_event.start_index, 3)
self.assertEqual(temp_event.start_time, 4.5)
self.assertEqual(temp_event.anchor, 'Def/blech')
self.assertFalse(temp_event.internal_group)

def test_constructor_group(self):
test1 = HedString("(Onset, (Label/Apple, Blue), Def/Blech/54.3)", hed_schema=self.schema)
groups = test1.find_top_level_tags(["onset"], include_groups=1)
te = TemporalEvent(groups[0], 3, 4.5)
self.assertEqual(te.start_index, 3)
self.assertEqual(te.start_time, 4.5)
self.assertTrue(te.internal_group)
self.assertEqual(te.anchor, 'Def/Blech/54.3')
self.assertIsInstance(te.internal_group, HedGroup)
temp_event = TemporalEvent(groups[0], 3, 4.5)
self.assertEqual(temp_event.start_index, 3)
self.assertEqual(temp_event.start_time, 4.5)
self.assertTrue(temp_event.internal_group)
self.assertEqual(temp_event.anchor, 'Def/Blech/54.3')
self.assertIsInstance(temp_event.internal_group, HedGroup)

def test_constructor_on_files(self):
manager1 = EventManager(self.input_data, self.schema)
Expand Down

0 comments on commit b93bee8

Please sign in to comment.