77class OnsetValidator :
88 """ Validates onset/offset pairs. """
99
10- def __init__ (self , def_dict , run_full_onset_checks = True ):
11- self ._defs = def_dict
10+ def __init__ (self ):
1211 self ._onsets = {}
13- self ._run_full_onset_checks = run_full_onset_checks
1412
15- def validate_onset_offset (self , hed_string_obj ):
16- """ Validate onset/offset
13+ def validate_temporal_relations (self , hed_string_obj ):
14+ """ Validate onset/offset/inset tag relations
1715
1816 Parameters:
1917 hed_string_obj (HedString): The hed string to check.
@@ -22,76 +20,46 @@ def validate_onset_offset(self, hed_string_obj):
2220 list: A list of issues found in validating onsets (i.e., out of order onsets, unknown def names).
2321 """
2422 onset_issues = []
25- for found_onset , found_group in self ._find_onset_tags (hed_string_obj ):
26- if not found_onset :
23+ used_def_names = set ()
24+ for temporal_tag , temporal_group in self ._find_temporal_tags (hed_string_obj ):
25+ if not temporal_tag :
2726 return []
2827
29- def_tags = found_group .find_def_tags ()
28+ def_tags = temporal_group .find_def_tags (include_groups = 0 )
3029 if not def_tags :
31- onset_issues += ErrorHandler .format_error (OnsetErrors .ONSET_NO_DEF_TAG_FOUND , found_onset )
3230 continue
3331
34- if len (def_tags ) > 1 :
35- onset_issues += ErrorHandler .format_error (OnsetErrors .ONSET_TOO_MANY_DEFS ,
36- tag = def_tags [0 ][0 ],
37- tag_list = [tag [0 ] for tag in def_tags [1 :]])
32+ def_tag = def_tags [0 ]
33+ def_name = def_tag .extension
34+ if def_name .lower () in used_def_names :
35+ onset_issues += ErrorHandler .format_error (OnsetErrors .ONSET_SAME_DEFS_ONE_ROW , tag = temporal_tag ,
36+ def_name = def_name )
3837 continue
3938
40- # Get all children but def group and onset/offset, then validate #/type of children.
41- def_tag , def_group , _ = def_tags [0 ]
42- if def_group is None :
43- def_group = def_tag
44- children = [child for child in found_group .children if
45- def_group is not child and found_onset is not child ]
46- max_children = 1
47- if found_onset .short_base_tag == DefTagNames .OFFSET_ORG_KEY :
48- max_children = 0
49- if len (children ) > max_children :
50- onset_issues += ErrorHandler .format_error (OnsetErrors .ONSET_WRONG_NUMBER_GROUPS ,
51- def_tag ,
52- found_group .children )
53- continue
54-
55- if children :
56- # Make this a loop if max_children can be > 1
57- child = children [0 ]
58- if not isinstance (child , HedGroup ):
59- onset_issues += ErrorHandler .format_error (OnsetErrors .ONSET_TAG_OUTSIDE_OF_GROUP ,
60- child ,
61- def_tag )
39+ used_def_names .add (def_tag .extension .lower ())
6240
6341 # At this point we have either an onset or offset tag and it's name
64- onset_issues += self ._handle_onset_or_offset (def_tag , found_onset )
42+ onset_issues += self ._handle_onset_or_offset (def_tag , temporal_tag )
6543
6644 return onset_issues
6745
68- def _find_onset_tags (self , hed_string_obj ):
46+ def _find_temporal_tags (self , hed_string_obj ):
6947 return hed_string_obj .find_top_level_tags (anchor_tags = DefTagNames .TEMPORAL_KEYS )
7048
7149 def _handle_onset_or_offset (self , def_tag , onset_offset_tag ):
7250 is_onset = onset_offset_tag .short_base_tag == DefTagNames .ONSET_ORG_KEY
7351 full_def_name = def_tag .extension
74- def_name , _ , placeholder = def_tag .extension .partition ('/' )
75-
76- def_entry = self ._defs .get (def_name )
77- if def_entry is None :
78- return ErrorHandler .format_error (OnsetErrors .ONSET_DEF_UNMATCHED , tag = def_tag )
79- if bool (def_entry .takes_value ) != bool (placeholder ):
80- return ErrorHandler .format_error (OnsetErrors .ONSET_PLACEHOLDER_WRONG , tag = def_tag ,
81- has_placeholder = bool (def_entry .takes_value ))
82-
83- if self ._run_full_onset_checks :
84- if is_onset :
85- # onset can never fail as it implies an offset
86- self ._onsets [full_def_name .lower ()] = full_def_name
87- else :
88- is_offset = onset_offset_tag .short_base_tag == DefTagNames .OFFSET_ORG_KEY
89- if full_def_name .lower () not in self ._onsets :
90- if is_offset :
91- return ErrorHandler .format_error (OnsetErrors .OFFSET_BEFORE_ONSET , tag = def_tag )
92- else :
93- return ErrorHandler .format_error (OnsetErrors .INSET_BEFORE_ONSET , tag = def_tag )
94- elif is_offset :
95- del self ._onsets [full_def_name .lower ()]
52+ if is_onset :
53+ # onset can never fail as it implies an offset
54+ self ._onsets [full_def_name .lower ()] = full_def_name
55+ else :
56+ is_offset = onset_offset_tag .short_base_tag == DefTagNames .OFFSET_ORG_KEY
57+ if full_def_name .lower () not in self ._onsets :
58+ if is_offset :
59+ return ErrorHandler .format_error (OnsetErrors .OFFSET_BEFORE_ONSET , tag = def_tag )
60+ else :
61+ return ErrorHandler .format_error (OnsetErrors .INSET_BEFORE_ONSET , tag = def_tag )
62+ elif is_offset :
63+ del self ._onsets [full_def_name .lower ()]
9664
9765 return []
0 commit comments