1212
1313from __future__ import annotations
1414
15+ import logging
16+ from contextlib import contextmanager
17+ from typing import Callable , Tuple , Dict , Any
18+
1519import numpy as np
1620import pandas as pd
17- from typing import Callable , Tuple , Dict , Any
1821
1922__all__ = ["cluster_bootstrap_ci" , "paired_bootstrap_ci" ]
2023
24+ @contextmanager
25+ def _suppress_logging ():
26+ """Временное отключение логирования во время бутстрэпа."""
27+
28+ previous_level = logging .root .manager .disable
29+ logging .disable (logging .CRITICAL )
30+ try :
31+ yield
32+ finally :
33+ logging .disable (previous_level )
34+
2135
2236def cluster_bootstrap_ci (
2337 df : pd .DataFrame ,
@@ -50,13 +64,15 @@ def cluster_bootstrap_ci(
5064 Оценка метрики и нижняя/верхняя границы CI.
5165 """
5266 rng = np .random .default_rng (rng_seed )
53- theta_hat = float (estimator (df ))
67+ with _suppress_logging ():
68+ theta_hat = float (estimator (df ))
5469 clusters = df [cluster_col ].unique ()
5570 B = []
5671 for _ in range (n_boot ):
5772 sampled = rng .choice (clusters , size = len (clusters ), replace = True )
5873 part = df [df [cluster_col ].isin (sampled )].copy ()
59- B .append (float (estimator (part )))
74+ with _suppress_logging ():
75+ B .append (float (estimator (part )))
6076 low = np .percentile (B , 100 * alpha / 2 )
6177 high = np .percentile (B , 100 * (1 - alpha / 2 ))
6278 return theta_hat , float (low ), float (high )
@@ -83,14 +99,16 @@ def paired_bootstrap_ci(
8399 """
84100 rng = np .random .default_rng (rng_seed )
85101 clusters = df [cluster_col ].unique ()
86- vA , vB , dlt = estimator_pair (df )
102+ with _suppress_logging ():
103+ vA , vB , dlt = estimator_pair (df )
87104 BA : list [float ] = []
88105 BB : list [float ] = []
89106 BD : list [float ] = []
90107 for _ in range (n_boot ):
91108 sampled = rng .choice (clusters , size = len (clusters ), replace = True )
92109 part = df [df [cluster_col ].isin (sampled )].copy ()
93- a , b , d = estimator_pair (part )
110+ with _suppress_logging ():
111+ a , b , d = estimator_pair (part )
94112 BA .append (a )
95113 BB .append (b )
96114 BD .append (d )
0 commit comments