|
19 | 19 | simplex_volume_in_embedding,
|
20 | 20 | )
|
21 | 21 | from adaptive.notebook_integration import ensure_holoviews, ensure_plotly
|
22 |
| -from adaptive.utils import cache_latest, restore |
| 22 | +from adaptive.utils import assign_defaults, cache_latest, restore |
| 23 | + |
| 24 | +try: |
| 25 | + import pandas |
| 26 | + |
| 27 | + with_pandas = True |
| 28 | + |
| 29 | +except ModuleNotFoundError: |
| 30 | + with_pandas = False |
23 | 31 |
|
24 | 32 |
|
25 | 33 | def to_list(inp):
|
@@ -388,6 +396,26 @@ def to_numpy(self):
|
388 | 396 | of ``learner.function``."""
|
389 | 397 | return np.array([(*p, *np.atleast_1d(v)) for p, v in sorted(self.data.items())])
|
390 | 398 |
|
| 399 | + def to_dataframe( |
| 400 | + self, |
| 401 | + with_default_function_args: bool = True, |
| 402 | + function_prefix: str = "function.", |
| 403 | + point_names: tuple[str] = ("x", "y", "z"), |
| 404 | + value_name: str = "y", |
| 405 | + ) -> pandas.DataFrame: |
| 406 | + if not with_pandas: |
| 407 | + raise ImportError("pandas is not installed.") |
| 408 | + if len(point_names) != self.ndim: |
| 409 | + raise ValueError( |
| 410 | + f"point_names ({point_names}) should have the" |
| 411 | + f" same length as learner.ndims ({self.ndim})" |
| 412 | + ) |
| 413 | + data = sorted((*x, y) for x, y in self.data.items()) |
| 414 | + df = pandas.DataFrame(data, columns=[*point_names, value_name]) |
| 415 | + if with_default_function_args: |
| 416 | + assign_defaults(self.function, df, function_prefix) |
| 417 | + return df |
| 418 | + |
391 | 419 | @property
|
392 | 420 | def bounds_are_done(self):
|
393 | 421 | return all(p in self.data for p in self._bounds_points)
|
|
0 commit comments