Coverage for src/python/ensembl/vep/runner/catalogue/costs.py: 88%

44 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"""Consequence fact cost measurement.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from typing import ClassVar 

20 

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

22from ensembl.vep.annotators.consequence.facts.costs import ( 

23 declared_vs_measured, 

24 normalised_cost_table, 

25 profile_self_nanoseconds, 

26 write_cost_table, 

27) 

28from ensembl.vep.config import Config 

29from ensembl.vep.config.report import resolve_report_format 

30from ensembl.vep.corpus.registry import CorpusOptionsError, build_corpus_from_options 

31from ensembl.vep.runner.base import ExitCode 

32from ensembl.vep.render.table import TableFormat, render_table 

33from ensembl.vep.runner.registry import register_runner 

34 

35__all__ = ["Runner"] 

36 

37logger = logging.getLogger(__name__) 

38 

39HEADERS = ("Fact", "Declared", "Measured ns", "Measured cost") 

40 

41 

42@register_runner("catalogue.costs") 

43class Runner: 

44 """Runner for profiling fact costs against declared tiers. 

45 

46 Profiling replaces every fact's ``compute`` with a timing shim, so this is a deliberate 

47 measurement run and never something an annotation path does. 

48 """ 

49 

50 name: ClassVar[str] = "catalogue.costs" 

51 sections: ClassVar[tuple[str, ...]] = ("logging", "catalogue", "catalogue_costs", "corpus") 

52 description: ClassVar[str] = "measure each fact's real cost against its declared tier" 

53 

54 def __init__(self, config: Config) -> None: 

55 """Hold the configuration.""" 

56 self.config = config 

57 

58 def run(self) -> ExitCode: 

59 """Profile, report, and optionally persist the measured table.""" 

60 try: 

61 with build_corpus_from_options(self.config.corpus) as corpus: 

62 logger.info("profiling over %s", corpus.description) 

63 contexts = FactContext.for_overlaps([overlap for _name, overlap in corpus.named_overlaps]) 

64 logger.info("%d contexts", len(contexts)) 

65 measured = profile_self_nanoseconds(contexts) 

66 except CorpusOptionsError as unusable: 

67 logger.error("%s", unusable) 

68 return ExitCode.USAGE 

69 

70 rows: list[tuple[object, ...]] = [] 

71 for name, declared, nanoseconds, cost in declared_vs_measured(measured): 

72 rows.append((name, declared, f"{nanoseconds:.1f}", cost)) 

73 try: 

74 table_format = resolve_report_format( 

75 self.config.catalogue_costs.output_format, graph=False, table_default="text", graph_formats=() 

76 ) 

77 except ValueError as unusable: 

78 logger.error("%s", unusable) 

79 return ExitCode.USAGE 

80 print(render_table(HEADERS, rows, TableFormat(table_format))) 

81 

82 if self.config.catalogue_costs.output_file is not None: 

83 destination = write_cost_table( 

84 normalised_cost_table(measured), self.config.catalogue_costs.output_file 

85 ) 

86 logger.info("wrote measured cost table to %s", destination) 

87 return ExitCode.OK