|
15 | 15 | from adaptive.learner.learner1D import Learner1D, _get_intervals |
16 | 16 | from adaptive.notebook_integration import ensure_holoviews |
17 | 17 | from adaptive.types import Real |
| 18 | +from adaptive.utils import default_parameters |
| 19 | + |
| 20 | +try: |
| 21 | + import pandas |
| 22 | + |
| 23 | + with_pandas = True |
| 24 | + |
| 25 | +except ModuleNotFoundError: |
| 26 | + with_pandas = False |
18 | 27 |
|
19 | 28 | Point = Tuple[int, Real] |
20 | 29 | Points = List[Point] |
@@ -127,6 +136,45 @@ def min_samples_per_point(self) -> int: |
127 | 136 | return 0 |
128 | 137 | return min(self._number_samples.values()) |
129 | 138 |
|
| 139 | + def to_numpy(self, mean: bool = True) -> np.ndarray: |
| 140 | + if mean: |
| 141 | + return super().to_numpy() |
| 142 | + else: |
| 143 | + return np.array( |
| 144 | + [ |
| 145 | + (seed, x, *np.atleast_1d(y)) |
| 146 | + for x, seed_y in self._data_samples.items() |
| 147 | + for seed, y in seed_y.items() |
| 148 | + ] |
| 149 | + ) |
| 150 | + |
| 151 | + def to_dataframe( |
| 152 | + self, |
| 153 | + mean: bool = True, |
| 154 | + with_default_function_args: bool = True, |
| 155 | + function_prefix: str = "function.", |
| 156 | + seed_name: str = "seed", |
| 157 | + x_name: str = "x", |
| 158 | + y_name: str = "y", |
| 159 | + ) -> pandas.DataFrame: |
| 160 | + if not with_pandas: |
| 161 | + raise ImportError("pandas is not installed.") |
| 162 | + if mean: |
| 163 | + data = sorted(self.data.items()) |
| 164 | + columns = [x_name, y_name] |
| 165 | + else: |
| 166 | + data = [ |
| 167 | + (seed, x, y) |
| 168 | + for x, seed_y in sorted(self._data_samples.items()) |
| 169 | + for seed, y in sorted(seed_y.items()) |
| 170 | + ] |
| 171 | + columns = [seed_name, x_name, y_name] |
| 172 | + df = pandas.DataFrame(data, columns=columns) |
| 173 | + if with_default_function_args: |
| 174 | + defaults = default_parameters(self.function, function_prefix) |
| 175 | + df = df.assign(**defaults) |
| 176 | + return df |
| 177 | + |
130 | 178 | def ask(self, n: int, tell_pending: bool = True) -> tuple[Points, list[float]]: |
131 | 179 | """Return 'n' points that are expected to maximally reduce the loss.""" |
132 | 180 | # If some point is undersampled, resample it |
|
0 commit comments