Coverage for rdgai/gui/views.py: 93.40%

106 statements  

« prev     ^ index     » next       coverage.py v7.17.3, created at 2026-11-24 23:53 +0000

1""" 

2The views of the GUI for developing rdgai: queue filters, pairs by category and agreement between the human labels 

3and suggestions. Callers hold ``store.lock`` and have refreshed the store. 

4 

5Suggestions are never shown on test units (unless the agreement view is explicitly asked to include them), so the 

6filters that look at suggestions skip test units too. 

7""" 

8from collections import Counter 

9 

10from ..annotation import AnnotationError, AnnotationStore, CERTAINTY_VALUES 

11from ..evaluate import metrics 

12 

13LABEL_SOURCES = ("human", "pipeline", "rule") 

14QUEUE_SOURCES = LABEL_SOURCES + ("suggestion",) 

15AGREEMENT_SCOPES = ("example", "sampled", "test", "all") 

16TRUE_VALUES = ("1", "true", "yes", "on") 

17 

18 

19def check_queue_filters(store:AnnotationStore, filters:dict): 

20 if filters.get("category"): 

21 store.get_relation_type(filters["category"]) 

22 if filters.get("cert") and filters["cert"] not in CERTAINTY_VALUES: 

23 raise AnnotationError(f"cert must be one of {', '.join(CERTAINTY_VALUES)}.") 

24 if filters.get("source") and filters["source"] not in QUEUE_SOURCES: 

25 raise AnnotationError(f"source must be one of {', '.join(QUEUE_SOURCES)}.") 

26 

27 

28def labelled_relations(pair) -> list: 

29 """ The label elements of a pair in either direction. """ 

30 return [relation for relation in (pair.relation_element(), pair.get_inverse().relation_element()) if relation is not None] 

31 

32 

33def disagrees(store:AnnotationStore, app, pair) -> bool: 

34 """ True if the pair is labelled and a suggestion for it (in the same direction) gives another category. """ 

35 category = store.current_category(pair) 

36 if category is None: 

37 return False 

38 return any(suggestion.category != category for suggestion in store.suggestions_for(app, pair)) 

39 

40 

41def unit_matches(store:AnnotationStore, app, filters:dict, roles:dict) -> bool: 

42 """ Whether a unit passes the category, certainty, source and disagreement filters. """ 

43 category, cert, source = filters.get("category"), filters.get("cert"), filters.get("source") 

44 disagreement = str(filters.get("disagreement") or "").lower() in TRUE_VALUES 

45 if not (category or cert or source or disagreement): 

46 return True 

47 if store.hides(app): 

48 return False 

49 shows_suggestions = roles.get(str(app)) != "test" 

50 if (source == "suggestion" or disagreement) and not shows_suggestions: 

51 return False 

52 pairs = store.reading_ordered_pairs(app) 

53 if category: 

54 name = store.get_relation_type(category).name 

55 if not any(name in pair.relation_type_names() or name in pair.get_inverse().relation_type_names() for pair in pairs): 

56 return False 

57 if cert and not any(relation.get("cert") == cert for pair in pairs for relation in labelled_relations(pair)): 

58 return False 

59 if source == "suggestion": 

60 if not store.suggestion_relations(app): 

61 return False 

62 elif source and not any(store.label_source(directed) == source for pair in pairs for directed in (pair, pair.get_inverse())): 

63 return False 

64 if disagreement and not any(disagrees(store, app, pair) for pair in pairs): 

65 return False 

66 return True 

67 

68 

69def filter_queue(store:AnnotationStore, filters:dict) -> list: 

70 check_queue_filters(store, filters) 

71 apps = store.filter_units(status=filters["status"], verse=filters["verse"], sample=filters["sample"]) 

72 roles = store.sample_roles() 

73 return [app for app in apps if unit_matches(store, app, filters, roles)] 

74 

75 

76def category_pairs(store:AnnotationStore, category:str, source:str="all") -> list: 

77 """ The directed pairs labelled with a category, in document order (not in hidden test units). """ 

78 relation_type = store.get_relation_type(category) 

79 if source not in ("all",) + LABEL_SOURCES: 

80 raise AnnotationError(f"source must be one of all, {', '.join(LABEL_SOURCES)}.") 

81 pairs = [pair for pair in store.directed_pairs() if relation_type in pair.types and not store.hides(pair.app)] 

82 if source != "all": 

83 pairs = [pair for pair in pairs if store.label_source(pair) == source] 

84 # a category that is its own inverse labels both directions of a pair: list it once, in display direction 

85 chosen = {} 

86 for pair in pairs: 

87 key = (id(pair.app), frozenset((pair.active.n, pair.passive.n))) 

88 if key not in chosen: 

89 chosen[key] = pair 

90 elif display_direction(store, pair): 

91 chosen[key] = pair 

92 return list(chosen.values()) 

93 

94 

95def display_direction(store:AnnotationStore, pair) -> bool: 

96 """ Whether the directed pair is in the order the GUI shows it (reading order: the earlier reading first). """ 

97 return any((ordered.active.n, ordered.passive.n) == (pair.active.n, pair.passive.n) for ordered in store.reading_ordered_pairs(pair.app)) 

98 

99 

100def default_scope(store:AnnotationStore) -> str: 

101 return "example" if store.load_sample() is not None else "all" 

102 

103 

104def agreement(store:AnnotationStore, scope:str|None=None, source:str|None=None, display_justification=lambda text: text) -> dict: 

105 """ 

106 Compares the human labels with the suggestions of one source, pair by pair in display direction. 

107 By default only the sample's example units are compared; test units only when the scope asks for them. 

108 """ 

109 scope = scope or default_scope(store) 

110 if scope not in AGREEMENT_SCOPES: 

111 raise AnnotationError(f"scope must be one of {', '.join(AGREEMENT_SCOPES)}.") 

112 apps = store.filter_units(sample=scope) 

113 categories = [category.name for category in store.categories()] 

114 

115 counts = Counter() 

116 compared_pairs = [] # (app, pair, human category, {source: suggestion}) 

117 for app in apps: 

118 if store.hides(app): 

119 continue 

120 for pair in store.reading_ordered_pairs(app): 

121 if store.coverage_for(pair) is not None: 

122 continue 

123 by_source = {} 

124 for suggestion in store.suggestions_for(app, pair): 

125 by_source.setdefault(suggestion.source, suggestion) 

126 counts.update(by_source.keys()) 

127 human = store.current_category(pair) if store.label_source(pair) == "human" else None 

128 if human and by_source: 

129 compared_pairs.append((app, pair, human, by_source)) 

130 

131 sources = [dict(source=name, count=count) for name, count in sorted(counts.items(), key=lambda item: (-item[1], item[0]))] 

132 if source is None: 

133 source = sources[0]["source"] if sources else None 

134 

135 gold, predicted, disagreements = {}, {}, [] 

136 for app, pair, human, by_source in compared_pairs: 

137 suggestion = by_source.get(source) 

138 if suggestion is None: 

139 continue 

140 key = (str(app), pair.active.n, pair.passive.n) 

141 gold[key] = human 

142 predicted[key] = suggestion.category 

143 if suggestion.category != human: 

144 disagreements.append(dict( 

145 unit_id=str(app), first=pair.active.n, second=pair.passive.n, 

146 first_text=str(pair.active), second_text=str(pair.passive), 

147 human=human, suggested=suggestion.category, source=suggestion.source, 

148 justification=display_justification(suggestion.justification or ""), 

149 )) 

150 scores = metrics(gold, predicted, categories) 

151 return dict( 

152 scope=scope, 

153 sources=sources, 

154 source=source, 

155 compared=len(gold), 

156 agreed=scores.correct, 

157 metrics=scores.model_dump(), 

158 disagreements=disagreements, 

159 )