Skip to content

Commit 43ed272

Browse files
authored
Merge pull request #317 from python-adaptive/to_numpy
add to_numpy methods
2 parents 2e76abc + 447c6f5 commit 43ed272

File tree

5 files changed

+26
-0
lines changed

5 files changed

+26
-0
lines changed

adaptive/learner/average_learner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def __init__(self, function, atol=None, rtol=None, min_npoints=2):
5656
def n_requested(self):
5757
return self.npoints + len(self.pending_points)
5858

59+
def to_numpy(self):
60+
"""Data as NumPy array of size (npoints, 2) with seeds and values."""
61+
return np.array(sorted(self.data.items()))
62+
5963
def ask(self, n, tell_pending=True):
6064
points = list(range(self.n_requested, self.n_requested + n))
6165

adaptive/learner/integrator_learner.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,10 @@ def plot(self):
546546
xs, ys = zip(*[(x, y) for ival in ivals for x, y in sorted(ival.data.items())])
547547
return hv.Path((xs, ys))
548548

549+
def to_numpy(self):
550+
"""Data as NumPy array of size (npoints, 2)."""
551+
return np.array(sorted(self.data.items()))
552+
549553
def _get_data(self):
550554
# Change the defaultdict of SortedSets to a normal dict of sets.
551555
x_mapping = {k: set(v) for k, v in self.x_mapping.items()}

adaptive/learner/learner1D.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ def vdim(self):
269269
return 1
270270
return self._vdim
271271

272+
def to_numpy(self):
273+
"""Data as NumPy array of size ``(npoints, 2)`` if ``learner.function`` returns a scalar
274+
and ``(npoints, 1+vdim)`` if ``learner.function`` returns a vector of length ``vdim``."""
275+
return np.array([(x, *np.atleast_1d(y)) for x, y in sorted(self.data.items())])
276+
272277
@property
273278
def npoints(self):
274279
"""Number of evaluated points."""

adaptive/learner/learner2D.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,13 @@ def xy_scale(self):
377377
else:
378378
return np.array([xy_scale[0], xy_scale[1] / self.aspect_ratio])
379379

380+
def to_numpy(self):
381+
"""Data as NumPy array of size ``(npoints, 3)`` if ``learner.function`` returns a scalar
382+
and ``(npoints, 2+vdim)`` if ``learner.function`` returns a vector of length ``vdim``."""
383+
return np.array(
384+
[(x, y, *np.atleast_1d(z)) for (x, y), z in sorted(self.data.items())]
385+
)
386+
380387
def _scale(self, points):
381388
points = np.asarray(points, dtype=float)
382389
return (points - self.xy_mean) / self.xy_scale

adaptive/learner/learnerND.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,12 @@ def vdim(self):
379379
self._vdim = 1
380380
return self._vdim if self._vdim is not None else 1
381381

382+
def to_numpy(self):
383+
"""Data as NumPy array of size ``(npoints, dim+vdim)``, where ``dim`` is the
384+
size of the input dimension and ``vdim`` is the length of the return value
385+
of ``learner.function``."""
386+
return np.array([(*p, *np.atleast_1d(v)) for p, v in sorted(self.data.items())])
387+
382388
@property
383389
def bounds_are_done(self):
384390
return all(p in self.data for p in self._bounds_points)

0 commit comments

Comments
 (0)