Coverage for src/python/ensembl/vep/stats/profile.py: 94%

41 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"""Fact-level profiling collected by instrumenting an annotation run. 

15 

16Profiling is opt-in because counting replaces each fact's ``compute`` callback and tracing records 

17every overlap derivation. The process-global callback replacement is restored before returning and 

18is not thread-safe; computed values remain local to each ``FactContext``. 

19""" 

20 

21from __future__ import annotations 

22 

23import logging 

24from collections.abc import Callable, Iterator, Sequence 

25from contextlib import contextmanager 

26 

27from attrs import evolve 

28 

29from ensembl.vep.annotators.base import Annotator 

30from ensembl.vep.annotators.consequence.context.fact import FactContext 

31from ensembl.vep.annotators.consequence.facts.base import REGISTRY 

32from ensembl.vep.models.overlap import OverlapConsequence, TranscriptVariationAllele 

33from ensembl.vep.trace import annotation_tracing 

34from ensembl.vep.trace.annotation import AnnotationTrace 

35from ensembl.vep.trace.run import RunTrace 

36 

37__all__ = ["count_fact_computations", "trace_run"] 

38 

39logger = logging.getLogger(__name__) 

40 

41 

42@contextmanager 

43def count_fact_computations() -> Iterator[dict[str, int]]: 

44 """Count every fact ``compute`` call and its declared cost, process-wide, for the duration. 

45 

46 Yields: 

47 dict[str, int]: A live ``{"computed": ..., "cost": ...}`` counter, updated as facts are 

48 computed while the block is active. 

49 """ 

50 counters = {"computed": 0, "cost": 0} 

51 original_compute_functions = {name: registered_fact.compute for name, registered_fact in REGISTRY.items()} 

52 

53 def make_counting_compute( 

54 fact_name: str, original_compute: Callable[[FactContext], object] 

55 ) -> Callable[[FactContext], object]: 

56 def counting_compute(fact_context: FactContext) -> object: 

57 counters["computed"] += 1 

58 counters["cost"] += REGISTRY[fact_name].cost 

59 return original_compute(fact_context) 

60 

61 return counting_compute 

62 

63 try: 

64 for fact_name, registered_fact in list(REGISTRY.items()): 

65 REGISTRY[fact_name] = evolve( 

66 registered_fact, compute=make_counting_compute(fact_name, registered_fact.compute) 

67 ) 

68 yield counters 

69 finally: 

70 for fact_name, original_compute in original_compute_functions.items(): 

71 REGISTRY[fact_name] = evolve(REGISTRY[fact_name], compute=original_compute) 

72 

73 

74def trace_run( 

75 annotator: Annotator[OverlapConsequence], 

76 overlaps: Sequence[TranscriptVariationAllele], 

77 *, 

78 annotator_name: str = "", 

79 cost_table: str = "", 

80) -> RunTrace: 

81 """Annotate every overlap with tracing on and fold the derivations into one run-scoped view. 

82 

83 Args: 

84 annotator (Annotator[OverlapConsequence]): The annotator to run. A native one never 

85 traces, so its run folds to nothing. 

86 overlaps (Sequence[TranscriptVariationAllele]): The overlaps to annotate, in order. 

87 annotator_name (str): The annotator's registry name, for the report. 

88 cost_table (str): Which cost table the declared costs came from, for the report. 

89 

90 Returns: 

91 RunTrace: The fold. 

92 """ 

93 traces: list[AnnotationTrace] = [] 

94 with annotation_tracing(quiet=True): 

95 for overlap in overlaps: 

96 annotator.annotate_feature_overlap(overlap) 

97 trace = getattr(annotator, "last_trace", None) 

98 if trace is not None: 98 ↛ 95line 98 didn't jump to line 95 because the condition on line 98 was always true

99 traces.append(trace) 

100 if not traces and overlaps: 100 ↛ 101line 100 didn't jump to line 101 because the condition on line 100 was never true

101 logger.warning("%s recorded no traces; a native annotator never does", annotator_name or "annotator") 

102 return RunTrace.fold(traces, annotator_name=annotator_name, cost_table=cost_table)