Coverage for src/python/ensembl/vep/trace/events.py: 100%

36 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"""Trace events and recording state for consequence annotations. 

15 

16This module has no ``ensembl.vep`` imports because facts, contexts, and caches depend on its event 

17types. Higher-level reconstruction belongs in ``trace.reconstruct``. 

18""" 

19 

20from __future__ import annotations 

21 

22from collections.abc import Iterable 

23from enum import Enum 

24 

25from attrs import define, field, frozen 

26 

27__all__ = ["FactEvent", "FactOrigin", "PruneEvent", "RuleEvent", "TraceRecorder"] 

28 

29 

30class FactOrigin(Enum): 

31 """Identify how a fact value became known. 

32 

33 Attributes: 

34 COMPUTED (FactOrigin): Computed by the registered fact function. 

35 PREFILLED (FactOrigin): Supplied by a zone map without computation. 

36 CACHED (FactOrigin): Supplied by the shared-codon cache. 

37 FORCED_FALSE (FactOrigin): Proved false through ``requires`` propagation. 

38 """ 

39 

40 COMPUTED = "computed" 

41 PREFILLED = "prefilled by zone map" 

42 CACHED = "codon cache hit" 

43 FORCED_FALSE = "forced False by requires" 

44 

45 

46@frozen(kw_only=True, auto_attribs=True) 

47class FactEvent: 

48 """Record a fact value and how it became known. 

49 

50 Attributes: 

51 name (str): The fact's name. 

52 value (object): The fact's value. 

53 cost (int): Cost charged for a computed fact or avoided for a forced-false fact; zero for 

54 values supplied by a cache. 

55 origin (FactOrigin): How the value became known. 

56 reads (int): Number of ``get_fact`` requests for this fact. 

57 forced_by (tuple[str, ...]): False ``requires`` atoms that forced the value, when relevant. 

58 """ 

59 

60 name: str 

61 value: object 

62 cost: int 

63 origin: FactOrigin 

64 reads: int = field(default=0) 

65 forced_by: tuple[str, ...] = field(default=()) 

66 

67 

68@frozen(kw_only=True, auto_attribs=True) 

69class RuleEvent: 

70 """Record a rule verdict for one overlap. 

71 

72 Attributes: 

73 term (str): The SO term this rule decides. 

74 rank (int): The rule's severity rank. 

75 fired (bool): True if the rule's formula evaluated True, before subsumption pruning. 

76 settled_early (bool): Whether a guard or partial evaluation settled the rule. 

77 reason (str): Formula or partial-evaluation reason for the verdict. 

78 """ 

79 

80 term: str 

81 rank: int 

82 fired: bool 

83 settled_early: bool 

84 reason: str 

85 

86 

87@frozen(kw_only=True, auto_attribs=True) 

88class PruneEvent: 

89 """Record one subsumption decision. 

90 

91 Attributes: 

92 dropped_term (str): Term removed from the emitted terms. 

93 subsumed_by (str): Firing term that subsumed ``dropped_term``. 

94 """ 

95 

96 dropped_term: str 

97 subsumed_by: str 

98 

99 

100@define(kw_only=True, auto_attribs=True) 

101class TraceRecorder: 

102 """Collect live state that cannot be reconstructed after annotation. 

103 

104 Reconstruction derives values and rule outcomes from the completed fact context. The recorder 

105 retains only original read counts and cache origins, which replay cannot recover. 

106 

107 Attributes: 

108 read_counts (dict[str, int]): Number of ``get_fact`` requests by fact name. 

109 served (dict[str, FactOrigin]): Cache origin by fact name. 

110 """ 

111 

112 read_counts: dict[str, int] = field(factory=dict) 

113 served: dict[str, FactOrigin] = field(factory=dict) 

114 

115 def note_served(self, names: Iterable[str], origin: FactOrigin) -> None: 

116 """Record facts supplied by a cache without computation. 

117 

118 Args: 

119 names (Iterable[str]): Newly memoised fact names supplied by the cache. 

120 origin (FactOrigin): Cache origin for the facts. 

121 """ 

122 for name in names: 

123 self.served[name] = origin