Test hed_strings validation.
()
| 1970 | |
| 1971 | |
| 1972 | def test_hed_annotations(): |
| 1973 | """Test hed_strings validation.""" |
| 1974 | pytest.importorskip("hed") |
| 1975 | # test initting with bad value |
| 1976 | validation_fail_msg = "A HED string failed to validate" |
| 1977 | with pytest.raises(ValueError, match=validation_fail_msg): |
| 1978 | _ = HEDAnnotations( |
| 1979 | onset=[1], |
| 1980 | duration=[0.1], |
| 1981 | description=["a"], |
| 1982 | hed_string=["foo"], |
| 1983 | ) |
| 1984 | # test initting with good values |
| 1985 | good_values = dict( |
| 1986 | square="Sensory-event, Experimental-stimulus, Visual-presentation, (Square, " |
| 1987 | "DarkBlue, (Center-of, Computer-screen))", # extra spaces intentional |
| 1988 | tone="Sensory-event, Experimental-stimulus, Auditory-presentation, (Tone, " |
| 1989 | "Frequency/550 Hz)", |
| 1990 | press="Agent-action, (Experiment-participant, (Press, Mouse-button))", |
| 1991 | word="Sensory-event, (Word, Label/Word-look), Auditory-presentation, " |
| 1992 | "Visual-presentation", |
| 1993 | ) |
| 1994 | # single string should broadcast to all annotations |
| 1995 | ann_single = HEDAnnotations( |
| 1996 | onset=[0, 1], |
| 1997 | duration=[0.1, 0.1], |
| 1998 | description=["x", "y"], |
| 1999 | hed_string=good_values["tone"], |
| 2000 | ) |
| 2001 | assert list(ann_single.hed_string) == [good_values["tone"], good_values["tone"]] |
| 2002 | # extras cannot override reserved field names |
| 2003 | with pytest.raises(ValueError, match="reserved"): |
| 2004 | _ = HEDAnnotations( |
| 2005 | onset=[1], |
| 2006 | duration=[0.1], |
| 2007 | description=["a"], |
| 2008 | hed_string=[good_values["tone"]], |
| 2009 | extras=[{"hed_string": "bad"}], |
| 2010 | ) |
| 2011 | ann = HEDAnnotations( |
| 2012 | onset=[3, 2, 1], |
| 2013 | duration=[0.1, 0.0, 0.3], |
| 2014 | description=["d", "c", "a"], |
| 2015 | hed_string=[good_values["square"], good_values["tone"], good_values["press"]], |
| 2016 | ) |
| 2017 | # make sure sorting by onset worked correctly |
| 2018 | assert ann.hed_string[0] == good_values["press"] |
| 2019 | assert ann.hed_string._objs[0].get_original_hed_string() == good_values["press"] |
| 2020 | # test appending |
| 2021 | foo = ann.copy() |
| 2022 | ons_dur_desc = dict(onset=1.5, duration=0.2, description="b") |
| 2023 | with pytest.raises(ValueError, match=validation_fail_msg): |
| 2024 | foo.append(**ons_dur_desc, hed_string="foo") |
| 2025 | foo.append(**ons_dur_desc, hed_string=good_values["word"]) |
| 2026 | # make sure sorting by onset also works for .append() |
| 2027 | assert list(foo.hed_string) == [ |
| 2028 | x.get_original_hed_string() for x in foo.hed_string._objs |
| 2029 | ] |