Coverage for src/python/ensembl/vep/runner/bench/sweep.py: 87%

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"""Benchmarks across values of one numeric annotation setting.""" 

15 

16from __future__ import annotations 

17 

18import logging 

19from typing import ClassVar 

20 

21from ensembl.vep.bench.baseline import BenchResults 

22from ensembl.vep.bench.sweep import DEFAULT_BATCH_STRATEGIES, Sweep, SweepPoint, sweep_strategy 

23from ensembl.vep.config import Config 

24from ensembl.vep.corpus.registry import CorpusOptionsError 

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

26from ensembl.vep.runner.base import ExitCode 

27from ensembl.vep.runner.bench import common 

28from ensembl.vep.runner.registry import register_runner 

29 

30__all__ = ["Runner"] 

31 

32logger = logging.getLogger(__name__) 

33 

34 

35@register_runner("bench.sweep") 

36class Runner: 

37 """Benchmark runner sweeping one setting across strategies. 

38 

39 Attributes: 

40 name (ClassVar[str]): The registry name. 

41 sections (ClassVar[tuple[str, ...]]): The configuration sections read. 

42 description (ClassVar[str]): One-line description for the subcommand list. 

43 """ 

44 

45 name: ClassVar[str] = "bench.sweep" 

46 sections: ClassVar[tuple[str, ...]] = ("logging", "corpus", "bench", "bench_sweep") 

47 description: ClassVar[str] = "sweep one numeric annotation setting and time each value" 

48 

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

50 """Hold the configuration.""" 

51 self.config = config 

52 

53 def run(self) -> ExitCode: 

54 """Sweep, report, and record.""" 

55 options = self.config.bench_sweep 

56 try: 

57 strategies = common.resolve_strategies(options.strategies, DEFAULT_BATCH_STRATEGIES) 

58 sweep = Sweep(knob=options.sweep_knob, values=tuple(options.sweep_values)) 

59 table_format = TableFormat(common.table_format(self.config)) 

60 except ValueError as problem: 

61 logger.error("%s", problem) 

62 return ExitCode.USAGE 

63 if not strategies: 63 ↛ 64line 63 didn't jump to line 64 because the condition on line 63 was never true

64 logger.error("no strategy to sweep is registered here") 

65 return ExitCode.USAGE 

66 

67 failed = False 

68 points: list[SweepPoint] = [] 

69 try: 

70 with common.corpora(self.config) as labelled: 

71 for label, overlaps in labelled: 

72 corpus_points: list[SweepPoint] = [] 

73 for strategy in strategies: 

74 measured, failures = sweep_strategy( 

75 label, strategy, overlaps, sweep, common.policy(self.config) 

76 ) 

77 for failure in failures: 77 ↛ 78line 77 didn't jump to line 78 because the loop on line 77 never started

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

79 failed = failed or bool(failures) 

80 corpus_points.extend(measured) 

81 print(f"\n{label}: {len(overlaps)} overlaps, sweeping {sweep.knob}") 

82 print( 

83 render_table( 

84 ("value", *(f"{name} ms/1k ctx" for name in strategies)), 

85 _rows(sweep, strategies, corpus_points), 

86 table_format, 

87 ) 

88 ) 

89 points.extend(corpus_points) 

90 except CorpusOptionsError as unusable: 

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

92 return ExitCode.USAGE 

93 

94 if failed: 94 ↛ 95line 94 didn't jump to line 95 because the condition on line 94 was never true

95 return ExitCode.FAILURE 

96 results = BenchResults( 

97 info=common.run_info(self.config, self.name), rows=tuple(point.to_json() for point in points) 

98 ) 

99 return common.record_results(self.config, results) 

100 

101 

102def _rows(sweep: Sweep, strategies: list[str], points: list[SweepPoint]) -> list[tuple[object, ...]]: 

103 """Return one row per value, one column per strategy.""" 

104 by_key = {(point.strategy, point.value): point.ms_per_1k for point in points} 

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

106 for value in sweep.values: 

107 cells: list[object] = [value] 

108 for strategy in strategies: 

109 measured = by_key.get((strategy, value)) 

110 cells.append(f"{measured:.3f}" if measured is not None else "-") 

111 rows.append(tuple(cells)) 

112 return rows