|
| 1 | +from abc import abstractmethod, ABC |
| 2 | +from typing import Tuple, Union |
| 3 | + |
| 4 | +import numpy as np |
| 5 | +import matplotlib.pyplot as plt |
| 6 | +import pandas as pd |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +import bootplot.backend.matplotlib |
| 10 | + |
| 11 | + |
| 12 | +class Backend(ABC): |
| 13 | + def __init__(self, |
| 14 | + f: callable, |
| 15 | + data: Union[np.ndarray, pd.DataFrame], |
| 16 | + m: int, |
| 17 | + output_size_px: Tuple[int, int]): |
| 18 | + self.output_size_px = output_size_px |
| 19 | + self.f = f |
| 20 | + self.data = data |
| 21 | + self.m = m |
| 22 | + |
| 23 | + @abstractmethod |
| 24 | + def create_figure(self): |
| 25 | + raise NotImplemented |
| 26 | + |
| 27 | + def plot(self): |
| 28 | + indices = np.random.randint(low=0, high=len(self.data), size=len(self.data)) |
| 29 | + if isinstance(self.data, pd.DataFrame): |
| 30 | + return self.f(self.data.iloc[indices], self.data, *self.plot_args) |
| 31 | + elif isinstance(self.data, np.ndarray): |
| 32 | + return self.f(self.data[indices], self.data, *self.plot_args) |
| 33 | + |
| 34 | + @abstractmethod |
| 35 | + def plot_to_array(self) -> np.ndarray: |
| 36 | + raise NotImplemented |
| 37 | + |
| 38 | + @abstractmethod |
| 39 | + def clear_figure(self): |
| 40 | + raise NotImplemented |
| 41 | + |
| 42 | + @abstractmethod |
| 43 | + def close_figure(self): |
| 44 | + raise NotImplemented |
| 45 | + |
| 46 | + @property |
| 47 | + @abstractmethod |
| 48 | + def plot_args(self): |
| 49 | + raise NotImplemented |
| 50 | + |
| 51 | + |
| 52 | +class Basic(Backend): |
| 53 | + def __init__(self, f: callable, data: Union[np.ndarray, pd.DataFrame], m: int, output_size_px: Tuple[int, int]): |
| 54 | + super().__init__(f, data, m, output_size_px) |
| 55 | + self.cached_image = None |
| 56 | + |
| 57 | + def plot(self): |
| 58 | + self.cached_image = super().plot() |
| 59 | + |
| 60 | + def create_figure(self): |
| 61 | + pass |
| 62 | + |
| 63 | + def plot_to_array(self) -> np.ndarray: |
| 64 | + return self.cached_image |
| 65 | + |
| 66 | + def clear_figure(self): |
| 67 | + pass |
| 68 | + |
| 69 | + def close_figure(self): |
| 70 | + pass |
| 71 | + |
| 72 | + @property |
| 73 | + def plot_args(self): |
| 74 | + return [] |
| 75 | + |
| 76 | + |
| 77 | +class Matplotlib(Backend): |
| 78 | + def __init__(self, |
| 79 | + f: callable, |
| 80 | + data: Union[np.ndarray, pd.DataFrame], |
| 81 | + m: int, |
| 82 | + output_size_px: Tuple[int, int] = (512, 512)): |
| 83 | + self.fig = None |
| 84 | + self.ax = None |
| 85 | + super().__init__(f, data, m, output_size_px) |
| 86 | + |
| 87 | + def create_figure(self): |
| 88 | + self.fig, self.ax = bootplot.backend.matplotlib.create_figure(self.output_size_px) |
| 89 | + |
| 90 | + def plot_to_array(self) -> np.ndarray: |
| 91 | + return bootplot.backend.matplotlib.plot_to_array(self.fig) |
| 92 | + |
| 93 | + def clear_figure(self): |
| 94 | + bootplot.backend.matplotlib.clear_figure(self.ax) |
| 95 | + |
| 96 | + def close_figure(self): |
| 97 | + bootplot.backend.matplotlib.close_figure(None) |
| 98 | + |
| 99 | + @property |
| 100 | + def plot_args(self): |
| 101 | + return [self.ax] |
| 102 | + |
| 103 | + |
| 104 | +class GGPlot2(Backend): |
| 105 | + def __init__(self, |
| 106 | + f: callable, |
| 107 | + data: Union[np.ndarray, pd.DataFrame], |
| 108 | + m: int, |
| 109 | + output_size_px: Tuple[int, int] = (512, 512)): |
| 110 | + super().__init__(f, data, m, output_size_px) |
| 111 | + |
| 112 | + def create_figure(self): |
| 113 | + raise NotImplemented |
| 114 | + |
| 115 | + def plot_to_array(self) -> np.ndarray: |
| 116 | + raise NotImplemented |
| 117 | + |
| 118 | + def clear_figure(self): |
| 119 | + raise NotImplemented |
| 120 | + |
| 121 | + def close_figure(self): |
| 122 | + raise NotImplemented |
| 123 | + |
| 124 | + @property |
| 125 | + def plot_args(self): |
| 126 | + raise NotImplemented |
| 127 | + |
| 128 | + |
| 129 | +class Plotly(Backend): |
| 130 | + def __init__(self, |
| 131 | + f: callable, |
| 132 | + data: Union[np.ndarray, pd.DataFrame], |
| 133 | + m: int, |
| 134 | + output_size_px: Tuple[int, int] = (512, 512)): |
| 135 | + super().__init__(f, data, m, output_size_px) |
| 136 | + |
| 137 | + def create_figure(self): |
| 138 | + raise NotImplemented |
| 139 | + |
| 140 | + def plot_to_array(self) -> np.ndarray: |
| 141 | + raise NotImplemented |
| 142 | + |
| 143 | + def clear_figure(self): |
| 144 | + raise NotImplemented |
| 145 | + |
| 146 | + def close_figure(self): |
| 147 | + raise NotImplemented |
| 148 | + |
| 149 | + @property |
| 150 | + def plot_args(self): |
| 151 | + raise NotImplemented |
| 152 | + |
| 153 | + |
| 154 | +def create_backend(backend_string, f, data, m, **kwargs): |
| 155 | + if backend_string == 'matplotlib': |
| 156 | + return Matplotlib(f=f, data=data, m=m, **kwargs) |
| 157 | + elif backend_string == 'basic': |
| 158 | + return Basic(f=f, data=data, m=m, **kwargs) |
| 159 | + else: |
| 160 | + raise NotImplemented |
0 commit comments