|
| 1 | +"""Generation of calibration study results""" |
| 2 | +from typing import List, Optional, Dict |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +DEFAULT_STUDY_BIAS = 0.0 |
| 6 | +DEFAULT_STUDY_SCALE = 0.05 |
| 7 | + |
| 8 | +class study: |
| 9 | + """Object for generating study values from a normal distribution around true the true channel roi""" |
| 10 | + def __init__(self, channel_name:str, true_roi:float, random_seed:int=None, bias:float=DEFAULT_STUDY_BIAS, stdev:float=DEFAULT_STUDY_SCALE) -> None: |
| 11 | + self.channel_name = channel_name |
| 12 | + self._true_roi = true_roi |
| 13 | + self.rng = self._create_random_factory(seed=random_seed) |
| 14 | + self._bias = bias |
| 15 | + self._stdev = stdev |
| 16 | + |
| 17 | + @property |
| 18 | + def roi(self) -> float: |
| 19 | + """Reports the true ROI of the channel set at initializaiton |
| 20 | +
|
| 21 | + Returns: |
| 22 | + true_roi (float): the true ROI value for the channel.""" |
| 23 | + return self._true_roi |
| 24 | + |
| 25 | + def _create_random_factory(self, seed: int) -> np.random.Generator: |
| 26 | + """Internal helper that serves as a central random number generator, |
| 27 | + and can be initialized with a seed to enable testing. |
| 28 | + |
| 29 | + Args: |
| 30 | + seed (int): Optional seed value for random number generation |
| 31 | + Returns: |
| 32 | + rng (np.random.Generator): random number generator""" |
| 33 | + rng = np.random.default_rng(seed=seed) |
| 34 | + return rng |
| 35 | + |
| 36 | + def update_bias(self, value:float) -> None: |
| 37 | + """Updates the distribution bias to the passed value |
| 38 | +
|
| 39 | + Args: |
| 40 | + value (float): value to set the distribution bias to |
| 41 | + Returns: |
| 42 | + None""" |
| 43 | + self._bias = value |
| 44 | + |
| 45 | + def update_stdev(self, value:float) -> None: |
| 46 | + """Updates the distribution stdev to the passed value |
| 47 | +
|
| 48 | + Args: |
| 49 | + value (float): value to set the distribution stdev to |
| 50 | + Returns: |
| 51 | + None""" |
| 52 | + self._stdev = value |
| 53 | + |
| 54 | + def update_roi(self, value:float) -> None: |
| 55 | + """Updates the roi assigned to the channel as the passed value |
| 56 | +
|
| 57 | + Args: |
| 58 | + value (float): value to set the channel roi to |
| 59 | + Returns: |
| 60 | + None""" |
| 61 | + self._true_roi = value |
| 62 | + |
| 63 | + def generate(self, count:int=1) -> 'np.array': |
| 64 | + """Provides a study 'result' |
| 65 | + |
| 66 | + Args: |
| 67 | + count (int): number of study results to return (default is 1) |
| 68 | + Retuns: |
| 69 | + study_results (iterable[float]): an array of study results """ |
| 70 | + return self.rng.normal(loc=self._true_roi + self._bias, scale=self._stdev, size=count) |
| 71 | + |
| 72 | + def generate_dynamic(self, bias:list[float], stdev:list[float]) -> list: |
| 73 | + """Provides study results with non-stationary distribution |
| 74 | +
|
| 75 | + Args: |
| 76 | + bias (list[float]): iterable of bias values used to update the distribution per results |
| 77 | + stdev (list[float]): iterable of stdev values used to update the distribution per results |
| 78 | + Returns: |
| 79 | + study_results (iterable[float]): an array of study results """ |
| 80 | + results = [] |
| 81 | + for b, z in zip(bias, stdev): |
| 82 | + self.update_bias(b) |
| 83 | + self.update_stdev(z) |
| 84 | + results.append(self.generate()[0]) |
| 85 | + return results |
| 86 | + |
| 87 | +class batch_study: |
| 88 | + """Object for generating study values across all channels""" |
| 89 | + def __init__(self, channel_rois:dict, channel_distributions:dict[str, dict]=dict(), random_seed:int=None, bias:float=DEFAULT_STUDY_BIAS, stdev:float=DEFAULT_STUDY_SCALE) -> None: |
| 90 | + self._study_hold = {k: study(channel_name=k, true_roi=v, random_seed=random_seed, bias=channel_distributions.get(k, {}).get("bias",bias), stdev=channel_distributions.get(k, {}).get("stdev",stdev)) for k, v in channel_rois.items()} |
| 91 | + |
| 92 | + def generate(self, count:int=1) -> dict[str, 'np.array']: |
| 93 | + """Produces study results for all of the registered channels |
| 94 | +
|
| 95 | + Args: |
| 96 | + count (int): number of study results to return (default is 1) |
| 97 | + Retuns: |
| 98 | + study_results (dict[iterable[float]]): an array of study results""" |
| 99 | + return {k: v.generate(count) for k, v in self._study_hold.items()} |
| 100 | + |
| 101 | + def generate_dynamic(self, universal_bias: Optional[List[float]] = None, universal_stdev: Optional[List[float]] = None, |
| 102 | + channel_bias: Optional[dict[str, list[float]]]=None, channel_stdev: Optional[dict[str, list[float]]]=None) -> dict[str, list[float]]: |
| 103 | + """Produces study results for all of the registered channels |
| 104 | +
|
| 105 | + Args: |
| 106 | + universal_bias (List[float]): iterable of bias values used to update the distribution per results |
| 107 | + universal_stdev (List[float]): iterable of stdev values used to update the distribution per results |
| 108 | + channel_bias (dict[str, list[float]]): lookup of iterable of bias values used to update the distribution per results |
| 109 | + channel_stdev (dict[str, list[float]]): iterable of stdev values used to update the distribution per results |
| 110 | + Returns: |
| 111 | + study_results (iterable[float]): an array of study results """ |
| 112 | + assert all(x is not None for x in [universal_bias, universal_stdev]) or all(x is not None for x in [channel_bias, channel_stdev]), "both Universal or both channel specs must be passed" |
| 113 | + results = {channel: [] for channel in self._study_hold.keys()} |
| 114 | + if all(x is not None for x in [universal_bias, universal_stdev]): |
| 115 | + for b, z in zip(universal_bias, universal_stdev): |
| 116 | + for channel, study in self._study_hold.items(): |
| 117 | + study.update_bias(b) |
| 118 | + study.update_stdev(z) |
| 119 | + results[channel].append(study.generate()[0]) |
| 120 | + return results |
| 121 | + if all(x is not None for x in [channel_bias, channel_stdev]): |
| 122 | + for channel, study in self._study_hold.items(): |
| 123 | + for b, z in zip(channel_bias[channel], channel_stdev[channel]): |
| 124 | + study.update_bias(b) |
| 125 | + study.update_stdev(z) |
| 126 | + results[channel].append(study.generate()[0]) |
| 127 | + return results |
| 128 | + |
0 commit comments