Skip to content

Commit 7973607

Browse files
committed
ddt __str__()
1 parent 6c829d7 commit 7973607

File tree

6 files changed

+111
-10
lines changed

6 files changed

+111
-10
lines changed

docs/source/api/ddt.ipynb

+62-9
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,14 @@
191191
"cell_type": "markdown",
192192
"metadata": {},
193193
"source": [
194-
"## Partial Estimation"
194+
"## Finite Difference Estimators"
195+
]
196+
},
197+
{
198+
"cell_type": "markdown",
199+
"metadata": {},
200+
"source": [
201+
"### Partial Estimation"
195202
]
196203
},
197204
{
@@ -248,7 +255,7 @@
248255
"cell_type": "markdown",
249256
"metadata": {},
250257
"source": [
251-
"## Complete Estimation"
258+
"### Complete Estimation"
252259
]
253260
},
254261
{
@@ -294,7 +301,7 @@
294301
"cell_type": "markdown",
295302
"metadata": {},
296303
"source": [
297-
"## Convenience Classes"
304+
"### Convenience Classes"
298305
]
299306
},
300307
{
@@ -312,6 +319,15 @@
312319
"outputs": [],
313320
"source": [
314321
"differ = opinf.ddt.UniformFiniteDifferencer(t, scheme=\"fwd1\")\n",
322+
"print(differ)"
323+
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": null,
328+
"metadata": {},
329+
"outputs": [],
330+
"source": [
315331
"differ.verify(plot=True)"
316332
]
317333
},
@@ -329,8 +345,46 @@
329345
"cell_type": "markdown",
330346
"metadata": {},
331347
"source": [
332-
"## Custom Estimators\n",
333-
"\n",
348+
"## Interpolatory Estimators"
349+
]
350+
},
351+
{
352+
"cell_type": "markdown",
353+
"metadata": {},
354+
"source": [
355+
"The {class}`InterpolationDerivativeEstimator` interpolates the state data using classes from {mod}`scipy.interpolate` and evaluates the derivative of the interpolant."
356+
]
357+
},
358+
{
359+
"cell_type": "code",
360+
"execution_count": null,
361+
"metadata": {},
362+
"outputs": [],
363+
"source": [
364+
"estimator = opinf.ddt.InterpolationDerivativeEstimator(t, \"cubic\")\n",
365+
"print(estimator)"
366+
]
367+
},
368+
{
369+
"cell_type": "code",
370+
"execution_count": null,
371+
"metadata": {},
372+
"outputs": [],
373+
"source": [
374+
"estimator.verify(plot=True)"
375+
]
376+
},
377+
{
378+
"cell_type": "markdown",
379+
"metadata": {},
380+
"source": [
381+
"## Custom Estimators"
382+
]
383+
},
384+
{
385+
"cell_type": "markdown",
386+
"metadata": {},
387+
"source": [
334388
"New time derivative estimators can be defined by inheriting from {class}`DerivativeEstimatorTemplate`.\n",
335389
"Once implemented, the [`verify()`](opinf.ddt.DerivativeEstimatorTemplate.verify) method may be used to compare the results of [`estimate()`](opinf.ddt.DerivativeEstimatorTemplate.estimate) with true derivatives for a limited number of test cases."
336390
]
@@ -387,7 +441,8 @@
387441
"cell_type": "markdown",
388442
"metadata": {},
389443
"source": [
390-
"The following class wraps {class}`scipy.interpolate.CubicSpline` and uses its `derivative()` method to compute the derivative."
444+
"The following class wraps {class}`scipy.interpolate.CubicSpline` and uses its `derivative()` method to compute the derivative.\n",
445+
"This is a simplified version of {class}`DerivativeEstimatorTemplate`."
391446
]
392447
},
393448
{
@@ -407,7 +462,7 @@
407462
"\n",
408463
" # Required methods --------------------------------------------------------\n",
409464
" def estimate(self, states, inputs=None):\n",
410-
" \"\"\"Estimate the first time derivatives o’f the states.\"\"\"\n",
465+
" \"\"\"Estimate the first time derivatives of the states.\"\"\"\n",
411466
" spline = spinterpolate.CubicSpline(\n",
412467
" x=self.time_domain,\n",
413468
" y=states,\n",
@@ -425,9 +480,7 @@
425480
"metadata": {},
426481
"outputs": [],
427482
"source": [
428-
"t = np.linspace(0, 1, 200)\n",
429483
"spline_estimator = CubicSplineEstimator(t)\n",
430-
"\n",
431484
"print(spline_estimator)"
432485
]
433486
},

src/opinf/ddt/_base.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import scipy.linalg as la
1313
import matplotlib.pyplot as plt
1414

15-
from .. import errors
15+
from .. import errors, utils
1616

1717

1818
class DerivativeEstimatorTemplate(abc.ABC):
@@ -86,6 +86,17 @@ def time_domain(self):
8686
"""Time domain of the snapshot data, a (k,) ndarray."""
8787
return self.__t
8888

89+
def __str__(self):
90+
"""String representation: class name, time domain."""
91+
out = [self.__class__.__name__]
92+
t = self.time_domain
93+
out.append(f"time_domain: {t.size} entries in [{t.min()}, {t.max()}]")
94+
return "\n ".join(out)
95+
96+
def __repr__(self):
97+
"""Unique ID + string representation."""
98+
return utils.str2repr(self)
99+
89100
# Main routine ------------------------------------------------------------
90101
def _check_dimensions(self, states, inputs, check_against_time=True):
91102
"""Check dimensions and alignment of the state and inputs."""

src/opinf/ddt/_finite_difference.py

+13
Original file line numberDiff line numberDiff line change
@@ -862,6 +862,13 @@ def scheme(self):
862862
"""Finite difference engine."""
863863
return self.__scheme
864864

865+
def __str__(self):
866+
"""String representation: class name, time domain."""
867+
head = DerivativeEstimatorTemplate.__str__(self)
868+
tail = [f"time step: {self.dt:.2e}"]
869+
tail.append(f"finite difference scheme: {self.scheme.__name__}()")
870+
return f"{head}\n " + "\n ".join(tail)
871+
865872
# Main routine ------------------------------------------------------------
866873
def estimate(self, states, inputs=None):
867874
r"""Estimate the first time derivatives of the states using
@@ -918,6 +925,12 @@ def __init__(self, time_domain):
918925
errors.OpInfWarning,
919926
)
920927

928+
def __str__(self):
929+
"""String representation: class name, time domain."""
930+
head = DerivativeEstimatorTemplate.__str__(self)
931+
tail = "finite difference engine: np.gradient(edge_order=2)"
932+
return f"{head}\n {tail}"
933+
921934
# Main routine ------------------------------------------------------------
922935
def estimate(self, states, inputs=None):
923936
r"""Estimate the first time derivatives of the states using

src/opinf/ddt/_interpolation.py

+20
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,26 @@ def __init__(
100100
self.__options = types.MappingProxyType(options)
101101

102102
# Properties --------------------------------------------------------------
103+
def __str__(self):
104+
"""String representation: class name, time domain."""
105+
head = DerivativeEstimatorTemplate.__str__(self)
106+
options = ", ".join(
107+
[
108+
f"{key}={repr(value)}"
109+
for key, value in self.options.items()
110+
if key != "axis"
111+
]
112+
)
113+
tail = [
114+
f"InterpolatorClass: {self.InterpolatorClass.__name__}({options})"
115+
]
116+
if (t2 := self.new_time_domain) is not None:
117+
tail.append(
118+
f"new_time_domain: {t2.size} entries "
119+
f"in [{t2.min()}, {t2.max()}]"
120+
)
121+
return f"{head}\n " + "\n ".join(tail)
122+
103123
@property
104124
def InterpolatorClass(self):
105125
"""One-dimensional differentiable interpolator class."""

tests/ddt/test_finite_difference.py

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ def myscheme():
7979
differ = self.Diff(t, scheme=name)
8080
assert differ.scheme is func
8181

82+
repr(differ)
83+
8284
def test_estimate(self, r=3, k=100, m=2):
8385
"""Use verify() to validate estimate()
8486
for all registered difference schemes.
@@ -131,6 +133,7 @@ def test_init(self, k=100):
131133
t = t**2
132134
differ = self.Diff(t)
133135
assert differ.time_domain is t
136+
repr(differ)
134137

135138
# Too many dimensions.
136139
t3 = np.sort(np.random.random((3, 3, 3)))

tests/ddt/test_interpolation.py

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def test_init(self, k=100):
4141
assert "axis" in est.options
4242
assert est.options["axis"] == 1
4343
assert est.new_time_domain.shape == t.shape
44+
repr(est)
4445

4546
def test_estimate(self, r=5, m=3, k=20):
4647
"""Use verify() to test estimate()."""

0 commit comments

Comments
 (0)