Skip to content

Commit a1dcefd

Browse files
committed
Add LearnerND.to_dataframe
1 parent 9cc0e8c commit a1dcefd

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

adaptive/learner/learnerND.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,15 @@
1919
simplex_volume_in_embedding,
2020
)
2121
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
2331

2432

2533
def to_list(inp):
@@ -388,6 +396,26 @@ def to_numpy(self):
388396
of ``learner.function``."""
389397
return np.array([(*p, *np.atleast_1d(v)) for p, v in sorted(self.data.items())])
390398

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+
391419
@property
392420
def bounds_are_done(self):
393421
return all(p in self.data for p in self._bounds_points)

0 commit comments

Comments
 (0)