forked from dynamicslab/pysindy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconftest.py
513 lines (411 loc) · 12.4 KB
/
conftest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
"""
Shared pytest fixtures for unit tests.
"""
from pathlib import Path
import numpy as np
import pytest
from scipy.integrate import solve_ivp
from pysindy.differentiation import FiniteDifference
from pysindy.differentiation import SpectralDerivative
from pysindy.feature_library import CustomLibrary
from pysindy.feature_library import FourierLibrary
from pysindy.feature_library import GeneralizedLibrary
from pysindy.feature_library import PDELibrary
from pysindy.feature_library import PolynomialLibrary
from pysindy.utils.odes import logistic_map
from pysindy.utils.odes import logistic_map_control
from pysindy.utils.odes import logistic_map_multicontrol
from pysindy.utils.odes import lorenz
from pysindy.utils.odes import lorenz_control
def pytest_addoption(parser):
parser.addoption(
"--external-notebook",
action="append",
default=[],
help=(
"name of notebook to test. Only valid if running"
" test_notebooks.test_external"
),
)
def pytest_generate_tests(metafunc):
if "external_notebook" in metafunc.fixturenames:
metafunc.parametrize(
"external_notebook",
[
Path(f.lstrip('"').rstrip('"'))
for f in metafunc.config.getoption("external_notebook")
],
)
@pytest.fixture(scope="session")
def data_1d():
t = np.linspace(0, 1, 10)
x = 2 * t.reshape(-1, 1)
return x, t
@pytest.fixture(scope="session")
def data_1d_bad_shape():
t = np.linspace(0, 5, 10)
x = 2 * t
return x, t
@pytest.fixture(scope="session")
def data_lorenz():
t = np.linspace(0, 1, 12)
x0 = [8, 27, -7]
x = solve_ivp(lorenz, (t[0], t[-1]), x0, t_eval=t).y.T
return x, t
@pytest.fixture
def data_multiple_trajectories():
n_points = [100, 200, 500]
initial_conditions = [
[8, 27, -7],
[-10.9595724, 21.7346758, 24.5722540],
[-3.95406365, -9.21825124, 12.07459147],
]
x_list = []
t_list = []
for n, x0 in zip(n_points, initial_conditions):
t = np.linspace(0, 5, n)
t_list.append(t)
x_list.append(solve_ivp(lorenz, (t[0], t[-1]), x0, t_eval=t).y.T)
return x_list, t_list
@pytest.fixture(scope="session")
def diffuse_multiple_trajectories():
def diffuse(t, u, dx, nx):
u = np.reshape(u, nx)
du = SpectralDerivative(d=2, axis=0)._differentiate(u, dx)
return np.reshape(u * du, nx)
# Required for accurate solve_ivp results
integrator_keywords = {}
integrator_keywords["rtol"] = 1e-8
integrator_keywords["method"] = "LSODA"
integrator_keywords["atol"] = 1e-8
N = 25
h0 = 1.0
L = 5
T = 1
t = np.linspace(0, T, N)
x = np.arange(0, N) * L / N
ep = 0.5 * h0
y0 = np.reshape(
h0 + ep * np.cos(4 * np.pi / L * x) + ep * np.cos(2 * np.pi / L * x), N
)
dx = x[1] - x[0]
sol1 = solve_ivp(
diffuse, (t[0], t[-1]), y0=y0, t_eval=t, args=(dx, N), **integrator_keywords
)
u = [np.reshape(sol1.y, (N, N, 1))]
return t, x, u
@pytest.fixture(scope="session")
def data_discrete_time():
n_steps = 100
mu = 3.6
x = np.zeros((n_steps))
x[0] = 0.5
for i in range(1, n_steps):
x[i] = logistic_map(x[i - 1], mu)
return x
@pytest.fixture(scope="session")
def data_discrete_time_multiple_trajectories():
n_steps = 100
mus = [1, 2.3, 3.6]
x = [np.zeros((n_steps)) for mu in mus]
for i, mu in enumerate(mus):
x[i][0] = 0.5
for k in range(1, n_steps):
x[i][k] = logistic_map(x[i][k - 1], mu)
return x
@pytest.fixture(scope="session")
def data_1d_random_pde():
n = 100
t = np.linspace(0, 10, n)
dt = t[1] - t[0]
x = np.linspace(0, 10, n)
u = np.random.randn(n, n, 1)
u_dot = FiniteDifference(axis=1)._differentiate(u, t=dt)
return t, x, u, u_dot
@pytest.fixture(scope="session")
def data_2d_random_pde():
n = 4
t = np.linspace(0, 10, n)
dt = t[1] - t[0]
x = np.linspace(0, 10, n)
y = np.linspace(0, 10, n)
X, Y = np.meshgrid(x, y)
spatial_grid = np.asarray([X, Y]).T
u = np.random.randn(n, n, n, 2)
u_dot = FiniteDifference(axis=2)._differentiate(u, t=dt)
return spatial_grid, u, u_dot
@pytest.fixture(scope="session")
def data_3d_random_pde():
n = 4
t = np.linspace(0, 10, n)
dt = t[1] - t[0]
x = np.linspace(0, 10, n)
y = np.linspace(0, 10, n)
z = np.linspace(0, 10, n)
(
X,
Y,
Z,
) = np.meshgrid(x, y, z, indexing="ij")
spatial_grid = np.asarray([X, Y, Z])
spatial_grid = np.transpose(spatial_grid, axes=[1, 2, 3, 0])
u = np.random.randn(n, n, n, n, 2)
u_dot = FiniteDifference(axis=3)._differentiate(u, t=dt)
return spatial_grid, u, u_dot
@pytest.fixture(scope="session")
def data_5d_random_pde():
n = 4
t = np.linspace(0, n, n)
dt = t[1] - t[0]
v = np.linspace(0, 10, n)
w = np.linspace(0, 10, n)
x = np.linspace(0, 10, n)
y = np.linspace(0, 10, n)
z = np.linspace(0, 10, n)
V, W, X, Y, Z = np.meshgrid(v, w, x, y, z, indexing="ij")
spatial_grid = np.asarray([V, W, X, Y, Z])
spatial_grid = np.transpose(spatial_grid, axes=[1, 2, 3, 4, 5, 0])
u = np.random.randn(n, n, n, n, n, n, 2)
u_dot = FiniteDifference(axis=5)._differentiate(u, t=dt)
return spatial_grid, u, u_dot
@pytest.fixture(scope="session")
def data_2d_resolved_pde():
n = 8
nt = 1000
t = np.linspace(0, 10, nt)
dt = t[1] - t[0]
x = np.linspace(0, 10, n)
y = np.linspace(0, 10, n)
X, Y = np.meshgrid(x, y)
spatial_grid = np.asarray([X, Y]).T
u = np.random.randn(n, n, nt, 2)
u_dot = FiniteDifference(axis=-2)._differentiate(u, t=dt)
return spatial_grid, u, u_dot
@pytest.fixture(scope="session")
def data_derivative_1d():
x = 2 * np.linspace(1, 100, 100)
x_dot = 2 * np.ones(100)
return x, x_dot
@pytest.fixture(scope="session")
def data_derivative_quasiperiodic_1d():
t = np.arange(1000) * 2 * np.pi / 1000
x = 2 * np.sin(t)
x_dot = 2 * np.cos(t)
return t, x, x_dot
@pytest.fixture(scope="session")
def data_derivative_2d():
x = np.zeros((100, 2))
x[:, 0] = 2 * np.linspace(1, 100, 100)
x[:, 1] = -10 * np.linspace(1, 100, 100)
x_dot = np.ones((100, 2))
x_dot[:, 0] *= 2
x_dot[:, 1] *= -10
return x, x_dot
@pytest.fixture(scope="session")
def data_derivative_quasiperiodic_2d():
t = np.arange(1000) * 2 * np.pi / 1000
x = np.zeros((1000, 2))
x[:, 0] = 2 * np.sin(t)
x[:, 1] = 2 * np.cos(2 * t)
x_dot = np.zeros((1000, 2))
x_dot[:, 0] = 2 * np.cos(t)
x_dot[:, 1] = -4 * np.sin(2 * t)
return t, x, x_dot
@pytest.fixture(scope="session")
def data_2dspatial():
u = np.zeros((100, 50, 2))
x = np.linspace(1, 100, 100)
y = np.linspace(1, 50, 50)
X, Y = np.meshgrid(x, y, indexing="ij")
u[:, :, 0] = np.cos(X) * np.sin(Y)
u[:, :, 1] = -np.sin(X) * np.cos(Y) ** 2
return x, y, u
@pytest.fixture
def custom_library():
library_functions = [
lambda x: x,
lambda x: x**2,
lambda x: 0 * x,
lambda x, y: x * y,
]
function_names = [
lambda s: str(s),
lambda s: str(s) + "^2",
lambda s: "0",
lambda s, t: str(s) + " " + str(t),
]
return CustomLibrary(
library_functions=library_functions, function_names=function_names
)
@pytest.fixture
def custom_library_bias():
library_functions = [
lambda x: x,
lambda x: x**2,
lambda x: 0 * x,
lambda x, y: x * y,
]
function_names = [
lambda s: str(s),
lambda s: str(s) + "^2",
lambda s: "0",
lambda s, t: str(s) + " " + str(t),
]
return CustomLibrary(
library_functions=library_functions,
function_names=function_names,
include_bias=True,
)
@pytest.fixture
def quadratic_library():
library_functions = [
lambda x: x,
lambda x, y: x * y,
lambda x: x**2,
]
function_names = [
lambda x: str(x),
lambda x, y: "{} * {}".format(x, y),
lambda x: "{}^2".format(x),
]
return CustomLibrary(
library_functions=library_functions, function_names=function_names
)
@pytest.fixture
def generalized_library():
tensor_array = [[1, 1]]
return GeneralizedLibrary(
[PolynomialLibrary(), FourierLibrary()],
tensor_array=tensor_array,
)
@pytest.fixture
def sindypi_library(data_lorenz):
library_functions = [
lambda x: x,
lambda x: x**2,
lambda x, y: x * y,
]
function_names = [
lambda s: str(s),
lambda s: str(s) + "^2",
lambda s, t: str(s) + " " + str(t),
]
_, t = data_lorenz
return PDELibrary(
library_functions=library_functions,
function_names=function_names,
temporal_grid=t,
implicit_terms=True,
derivative_order=1,
)
@pytest.fixture
def ode_library():
library_functions = [
lambda x: x,
lambda x: x**2,
lambda x, y: x * y,
]
function_names = [
lambda s: str(s),
lambda s: str(s) + "^2",
lambda s, t: str(s) + " " + str(t),
]
return PDELibrary(
library_functions=library_functions,
function_names=function_names,
)
@pytest.fixture
def pde_library(data_lorenz):
_, spatial_grid = data_lorenz
library_functions = [
lambda x: x,
lambda x: x**2,
lambda x, y: x * y,
]
function_names = [
lambda s: str(s),
lambda s: str(s) + "^2",
lambda s, t: str(s) + " " + str(t),
]
return PDELibrary(
library_functions=library_functions,
function_names=function_names,
spatial_grid=spatial_grid,
derivative_order=4,
)
@pytest.fixture(scope="session")
def data_linear_oscillator_corrupted():
t = np.linspace(0, 1, 100)
x = 3 * np.exp(-2 * t)
y = 0.5 * np.exp(t)
np.random.seed(1)
corrupt_idxs = np.random.choice(np.arange(1, t.size - 1), t.size // 20)
x[corrupt_idxs] = 0
X = np.stack((x, y), axis=-1)
X_dot = FiniteDifference(order=2)(X, t)
# build an array of the indices of samples that should be trimmed
trimmed_idxs = np.concatenate((corrupt_idxs - 1, corrupt_idxs, corrupt_idxs + 1))
trimming_array = np.ones(X.shape[0])
trimming_array[trimmed_idxs] = 0.0
return X, X_dot, trimming_array
@pytest.fixture(scope="session")
def data_linear_combination():
t = np.linspace(0, 5, 100)
x = np.stack((np.exp(t), np.sin(t), np.cos(t)), axis=-1)
y = np.stack((x[:, 0] + x[:, 1], x[:, 1] + x[:, 2]), axis=-1)
return x, y
# Datasets with control inputs
@pytest.fixture(scope="session")
def data_lorenz_c_1d():
def u_fun(t):
if len(np.shape(t)) == 0:
return np.column_stack([np.sin(2 * t), 0])
else:
return np.column_stack([np.sin(2 * t), np.zeros(len(t))])
t = np.linspace(0, 1, 100)
x0 = [8, 27, -7]
x = solve_ivp(lorenz_control, (t[0], t[-1]), x0, t_eval=t, args=(u_fun,)).y.T
u = u_fun(t)
return x, t, u, u_fun
@pytest.fixture(scope="session")
def data_lorenz_c_2d():
def u_fun(t):
return np.column_stack([np.sin(2 * t), t**2])
t = np.linspace(0, 1, 100)
x0 = [8, 27, -7]
x = solve_ivp(lorenz_control, (t[0], t[-1]), x0, t_eval=t, args=(u_fun,)).y.T
u = u_fun(t)
return x, t, u, u_fun
@pytest.fixture(scope="session")
def data_discrete_time_c():
n_steps = 100
mu = 3.6
u = 0.01 * np.random.randn(n_steps)
x = np.zeros((n_steps))
x[0] = 0.5
for i in range(1, n_steps):
x[i] = logistic_map_control(x[i - 1], mu, u[i - 1])
return x, u
@pytest.fixture(scope="session")
def data_discrete_time_c_multivariable():
n_steps = 100
mu = 3.6
u1 = 0.1 * np.random.randn(n_steps)
u2 = 0.1 * np.random.randn(n_steps)
u = np.column_stack((u1, u2))
x = np.zeros((n_steps))
x[0] = 0.5
for i in range(1, n_steps):
x[i] = logistic_map_multicontrol(x[i - 1], mu, u[i - 1])
return x, u
@pytest.fixture(scope="session")
def data_discrete_time_multiple_trajectories_c():
n_steps = 100
mus = [1, 2.3, 3.6]
u = [0.001 * np.random.randn(n_steps) for mu in mus]
x = [np.zeros((n_steps)) for mu in mus]
for i, mu in enumerate(mus):
x[i][0] = 0.5
for k in range(1, n_steps):
x[i][k] = logistic_map_control(x[i][k - 1], mu, u[i][k - 1])
return x, u