Coverage for src/python/ensembl/vep/explain/compare.py: 98%

64 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-18 14:41 +0000

1# See the NOTICE file distributed with this work for additional information 

2# regarding copyright ownership. 

3# 

4# Licensed under the Apache License, Version 2.0 (the "License"); 

5# you may not use this file except in compliance with the License. 

6# You may obtain a copy of the License at 

7# http://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14"""Comparison of traced derivations and assigned terms. 

15 

16Comparing two traces identifies the first differing fact value, rule verdict, or subsumption path, 

17rather than reporting only different emitted terms. ``diff_terms`` compares a trace with terms 

18from an annotator or an already-annotated input when the other side cannot provide a trace. 

19""" 

20 

21from __future__ import annotations 

22 

23from collections.abc import Mapping, Sequence 

24 

25from ensembl.vep.trace.annotation import AnnotationTrace 

26from ensembl.vep.trace.events import RuleEvent 

27 

28__all__ = ["diff_terms", "diff_traces"] 

29 

30 

31def diff_traces(trace_a: AnnotationTrace, trace_b: AnnotationTrace) -> str: 

32 """Compare two derivations of one overlap. 

33 

34 Args: 

35 trace_a (AnnotationTrace): The first annotator's derivation. 

36 trace_b (AnnotationTrace): The second annotator's derivation. 

37 

38 Returns: 

39 str: Emitted-term result, shared fact differences, and rule-verdict differences. 

40 """ 

41 name_a = trace_a.annotator_name or "A" 

42 name_b = trace_b.annotator_name or "B" 

43 lines = [f"comparing {name_a} vs {name_b}"] 

44 

45 if trace_a.emitted_terms == trace_b.emitted_terms: 

46 lines.append(f"emitted terms AGREE: {list(trace_a.emitted_terms)}") 

47 else: 

48 lines.append( 

49 "emitted terms DIVERGE:\n" 

50 f" {name_a}: {list(trace_a.emitted_terms)}\n" 

51 f" {name_b}: {list(trace_b.emitted_terms)}" 

52 ) 

53 

54 lines.extend(_fact_value_divergences(trace_a, trace_b, name_a, name_b)) 

55 lines.extend(_rule_divergences(trace_a, trace_b, name_a, name_b)) 

56 return "\n".join(lines) 

57 

58 

59def _fact_value_divergences( 

60 trace_a: AnnotationTrace, trace_b: AnnotationTrace, name_a: str, name_b: str 

61) -> list[str]: 

62 """Return shared facts whose values differ. 

63 

64 Different strategies may legitimately touch different facts, so facts present on only one 

65 side are not value divergences. 

66 """ 

67 values_a = {event.name: event.value for event in trace_a.fact_events} 

68 values_b = {event.name: event.value for event in trace_b.fact_events} 

69 disagreements = [] 

70 for name in sorted(set(values_a) & set(values_b)): 

71 if values_a[name] != values_b[name]: 

72 disagreements.append(f" {name}: {name_a}={values_a[name]!r} {name_b}={values_b[name]!r}") 

73 if not disagreements: 

74 return [] 

75 return ["\nFACT VALUE DIVERGENCE:", *disagreements] 

76 

77 

78def _rule_divergences( 

79 trace_a: AnnotationTrace, trace_b: AnnotationTrace, name_a: str, name_b: str 

80) -> list[str]: 

81 """Return the first differing verdict and rules recorded on only one side.""" 

82 events_a = {event.term: event for event in trace_a.rule_events} 

83 events_b = {event.term: event for event in trace_b.rule_events} 

84 shared_terms = sorted(set(events_a) & set(events_b), key=lambda term: events_a[term].rank) 

85 

86 lines = [] 

87 first_divergent_term = next( 

88 (term for term in shared_terms if events_a[term].fired != events_b[term].fired), None 

89 ) 

90 if first_divergent_term is None: 

91 lines.append("no rule-verdict divergence among the rules both traces recorded") 

92 else: 

93 event_a, event_b = events_a[first_divergent_term], events_b[first_divergent_term] 

94 lines.append( 

95 f"\nFIRST RULE-VERDICT DIVERGENCE: [{event_a.rank}] {first_divergent_term}\n" 

96 f" {name_a}: {'FIRED' if event_a.fired else 'FALSE'} -- {event_a.reason}\n" 

97 f" {name_b}: {'FIRED' if event_b.fired else 'FALSE'} -- {event_b.reason}" 

98 ) 

99 

100 only_in_a = sorted(set(events_a) - set(events_b)) 

101 only_in_b = sorted(set(events_b) - set(events_a)) 

102 if only_in_a: 

103 lines.append(f"rules traced only by {name_a}: {only_in_a}") 

104 if only_in_b: 104 ↛ 105line 104 didn't jump to line 105 because the condition on line 104 was never true

105 lines.append(f"rules traced only by {name_b}: {only_in_b}") 

106 return lines 

107 

108 

109def diff_terms(trace: AnnotationTrace, expected_terms: Sequence[str], source: str) -> str: 

110 """Compare a derivation with terms assigned by another source. 

111 

112 For a missing expected term, the rule ledger distinguishes a false rule from a fired rule that 

113 was later subsumed. Terms absent from the catalogue are reported as unmodelled rather than as 

114 rule failures. 

115 

116 Args: 

117 trace (AnnotationTrace): Derivation to compare. 

118 expected_terms (Sequence[str]): Terms assigned by the other source. 

119 source (str): Source name used in the report. 

120 

121 Returns: 

122 str: Comparison with an explanation for each differing term. 

123 """ 

124 ours = tuple(trace.emitted_terms) 

125 theirs = tuple(expected_terms) 

126 if set(ours) == set(theirs): 

127 return f"terms AGREE with {source}: {list(ours)}" 

128 

129 events = {event.term: event for event in trace.rule_events} 

130 pruned_by = {event.dropped_term: event.subsumed_by for event in trace.prune_events} 

131 

132 lines = [f"terms DIVERGE from {source}:"] 

133 for term in sorted(set(theirs) - set(ours)): 

134 lines.append(f" only in {source}: {term} -- {_why_absent(term, events, pruned_by)}") 

135 for term in sorted(set(ours) - set(theirs)): 

136 event = events.get(term) 

137 reason = event.reason if event is not None else "no rule event recorded" 

138 lines.append(f" only ours: {term} -- fired on {reason}") 

139 return "\n".join(lines) 

140 

141 

142def _why_absent(term: str, events: Mapping[str, RuleEvent], pruned_by: Mapping[str, str]) -> str: 

143 """Return why a term the other side emitted is missing from ours.""" 

144 if term in pruned_by: 

145 return f"our rule fired, then was subsumed by {pruned_by[term]}" 

146 event = events.get(term) 

147 if event is None: 

148 return "not modelled by this catalogue (unexplainable, not a rule failure)" 

149 return f"our rule was FALSE -- {event.reason}"