romda 3.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- romda/__init__.py +28 -0
- romda/bias_estimators/__init__.py +12 -0
- romda/bias_estimators/aux.py +526 -0
- romda/bias_estimators/bias.py +526 -0
- romda/bias_estimators/constantbias.py +105 -0
- romda/bias_estimators/esn.py +420 -0
- romda/data_assimilation.py +68 -0
- romda/estimators/__init__.py +71 -0
- romda/estimators/base.py +431 -0
- romda/estimators/deterministic.py +288 -0
- romda/estimators/ensembles.py +984 -0
- romda/estimators/inflation.py +49 -0
- romda/models/__init__.py +51 -0
- romda/models/data_driven/__init__.py +20 -0
- romda/models/data_driven/autoencoders/__init__.py +320 -0
- romda/models/data_driven/autoencoders/pod.py +492 -0
- romda/models/data_driven/autoencoders/pod_utils.py +576 -0
- romda/models/data_driven/esn.py +1212 -0
- romda/models/data_driven/esn_config.py +652 -0
- romda/models/data_driven/linear_model.py +212 -0
- romda/models/data_driven/pod_esn.py +581 -0
- romda/observations.py +778 -0
- romda/plotting/__init__.py +4 -0
- romda/plotting/annular.py +557 -0
- romda/plotting/cylinder.py +664 -0
- romda/plotting/plot_results.py +272 -0
- romda/plotting/pod.py +318 -0
- romda/plotting/utils_plotting.py +145 -0
- romda/utils.py +1085 -0
- romda-3.0.0.dist-info/METADATA +197 -0
- romda-3.0.0.dist-info/RECORD +34 -0
- romda-3.0.0.dist-info/WHEEL +5 -0
- romda-3.0.0.dist-info/licenses/LICENSE +21 -0
- romda-3.0.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
from copy import deepcopy
|
|
2
|
+
from typing import Optional
|
|
3
|
+
|
|
4
|
+
import matplotlib.pyplot as plt
|
|
5
|
+
import numpy as np
|
|
6
|
+
from numpy.linalg import inv
|
|
7
|
+
from romda.models import HistoryTracker, Integrator, Model
|
|
8
|
+
from romda.plotting import categorical_cmap
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _ensrkf_update(Af: np.ndarray, d: np.ndarray, Cdd: np.ndarray, M: np.ndarray) -> np.ndarray:
|
|
12
|
+
r"""Stateless ensemble square-root Kalman filter update, for the Bayesian bias update.
|
|
13
|
+
|
|
14
|
+
Implements the same analysis kernel as `romda.estimators.ensembles.EnSRKF`, but
|
|
15
|
+
takes an explicit measurement matrix `M` instead of reading one from a `Model`
|
|
16
|
+
(the bias state has no associated `Model`, so it cannot use `EnSRKF` directly).
|
|
17
|
+
Kept private and self-contained (no `romda`-internal imports) to avoid a
|
|
18
|
+
circular import between `romda.bias_estimators` and `romda.estimators`.
|
|
19
|
+
|
|
20
|
+
Parameters
|
|
21
|
+
----------
|
|
22
|
+
Af : ndarray, shape (N, m)
|
|
23
|
+
Forecast ensemble.
|
|
24
|
+
d : ndarray, shape (Nq,)
|
|
25
|
+
Observation vector.
|
|
26
|
+
Cdd : ndarray, shape (Nq, Nq)
|
|
27
|
+
Observation-noise covariance.
|
|
28
|
+
M : ndarray, shape (Nq, N)
|
|
29
|
+
Measurement operator.
|
|
30
|
+
|
|
31
|
+
Returns
|
|
32
|
+
-------
|
|
33
|
+
ndarray, shape (N, m)
|
|
34
|
+
Analysis ensemble.
|
|
35
|
+
"""
|
|
36
|
+
m = Af.shape[1]
|
|
37
|
+
d = np.atleast_1d(d)
|
|
38
|
+
Cdd = np.atleast_2d(Cdd)
|
|
39
|
+
|
|
40
|
+
psi_f_m = np.mean(Af, 1, keepdims=True)
|
|
41
|
+
Psi_f = Af - psi_f_m
|
|
42
|
+
|
|
43
|
+
y = M @ psi_f_m
|
|
44
|
+
S = M @ Psi_f
|
|
45
|
+
|
|
46
|
+
C = (m - 1) * Cdd + S @ S.T
|
|
47
|
+
Cinv = inv(C)
|
|
48
|
+
|
|
49
|
+
ma = psi_f_m + Psi_f @ S.T @ Cinv @ (d[:, None] - y)
|
|
50
|
+
|
|
51
|
+
T = np.eye(m) - S.T @ Cinv @ S
|
|
52
|
+
ev, evec = np.linalg.eigh(T)
|
|
53
|
+
sqrtT = evec @ np.diag(np.sqrt(np.maximum(ev, 0.0))) @ evec.T
|
|
54
|
+
|
|
55
|
+
Aa = ma + Psi_f @ sqrtT
|
|
56
|
+
return Aa if np.isreal(Aa).all() else Af
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class Bias:
|
|
60
|
+
r"""Base class for the model-bias estimators used in bias-aware data assimilation.
|
|
61
|
+
|
|
62
|
+
A bias estimator provides three things to the assimilation loop:
|
|
63
|
+
|
|
64
|
+
1. a **forecast** of the bias between analyses (`time_integrate`), driven by its
|
|
65
|
+
internal forecaster (an ESN, a constant map, a linear model, ...);
|
|
66
|
+
2. the **Jacobian** of the bias with respect to the observables
|
|
67
|
+
(`state_derivative`), $\mathbf{J} = \mathrm{d}\mathbf{b}/\mathrm{d}\mathbf{q}$,
|
|
68
|
+
required by the regularized bias-aware EnKF;
|
|
69
|
+
3. an **update rule** from the analysis innovation
|
|
70
|
+
(`update_state_from_innovation`), optionally Bayesian (an internal EnSRKF on
|
|
71
|
+
the bias state).
|
|
72
|
+
|
|
73
|
+
The estimator state has $N_\mathrm{dim}$ components: $[\mathbf{b}]$ if the
|
|
74
|
+
observations are unbiased, or $[\mathbf{b}; \mathbf{i}]$ (bias and innovations,
|
|
75
|
+
$N_\mathrm{dim} = 2 N_q$) if `biased_observations` is set. Child classes may add
|
|
76
|
+
hidden components (e.g., the ESN reservoir).
|
|
77
|
+
|
|
78
|
+
Parameters
|
|
79
|
+
----------
|
|
80
|
+
innovation : np.ndarray
|
|
81
|
+
Initial innovation/bias estimate, used to set the observable dimension $N_q$.
|
|
82
|
+
t : float
|
|
83
|
+
Initial time.
|
|
84
|
+
dt : float
|
|
85
|
+
Time step of the output history.
|
|
86
|
+
**kwargs
|
|
87
|
+
Class-attribute overrides (see Attributes) and forecaster options.
|
|
88
|
+
|
|
89
|
+
Attributes
|
|
90
|
+
----------
|
|
91
|
+
upsample : int
|
|
92
|
+
Upsampling factor of the internal forecaster time step relative to ``dt``.
|
|
93
|
+
L : int
|
|
94
|
+
Number of trajectories in the training dataset (data-driven estimators).
|
|
95
|
+
augment_data : bool or int
|
|
96
|
+
Whether (and how much) to augment the training data.
|
|
97
|
+
bayesian_update : bool
|
|
98
|
+
If True, the innovation update is a Bayesian (EnSRKF) update of the full
|
|
99
|
+
estimator state; otherwise the innovation is assigned directly.
|
|
100
|
+
biased_observations : bool
|
|
101
|
+
If True, the observations themselves are assumed biased and the estimator
|
|
102
|
+
tracks bias and innovations separately.
|
|
103
|
+
force_retrain : bool
|
|
104
|
+
If True, retrain the forecaster even if a cached configuration exists.
|
|
105
|
+
"""
|
|
106
|
+
|
|
107
|
+
upsample = 1
|
|
108
|
+
L = 1
|
|
109
|
+
augment_data = False
|
|
110
|
+
|
|
111
|
+
forecaster_type = None # This should be set in child classes to specify the expected type of the forecaster model, e.g., ESN_model for ESN_bias.
|
|
112
|
+
bayesian_update = False # Default to not perform bayesian update to state
|
|
113
|
+
biased_observations = False # Whether observations are biased or not
|
|
114
|
+
force_retrain = False
|
|
115
|
+
|
|
116
|
+
keys_to_print = ['bayesian_update', 'upsample', 'biased_observations', 'L', 'augment_data_length']
|
|
117
|
+
extra_keys_to_print = []
|
|
118
|
+
|
|
119
|
+
def __init__(self, innovation, t, dt, **kwargs):
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
# ===================== ASSIGN PROVIDED KWARGS ======================= ##
|
|
123
|
+
keys = list(kwargs.keys())
|
|
124
|
+
[setattr(self, key, kwargs.pop(key)) for key in keys if hasattr(self, key)]
|
|
125
|
+
|
|
126
|
+
self.keys_to_print += self.extra_keys_to_print
|
|
127
|
+
|
|
128
|
+
# ================== Setup dimensions ================= ##
|
|
129
|
+
|
|
130
|
+
self.precision_t = int(-np.log10(dt)) + 2
|
|
131
|
+
self.dt = dt
|
|
132
|
+
self.Nq = self._format_state(innovation).shape[1]
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
# ================== Initialize Forecaster & HISTORY ================= ##
|
|
136
|
+
|
|
137
|
+
self.init_forecaster(**kwargs)
|
|
138
|
+
|
|
139
|
+
bias_state = self.initialize_bias_state
|
|
140
|
+
|
|
141
|
+
assert bias_state.shape[-2] == self.N, f"Bias state shape {bias_state.shape} does not match expected (Nt, N = {self.N}, Nens)."
|
|
142
|
+
self.update_history(bias_state, t=t, reset=True)
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
@property
|
|
146
|
+
def name(self):
|
|
147
|
+
return self.__class__.__name__
|
|
148
|
+
|
|
149
|
+
@property
|
|
150
|
+
def N_ens(self):
|
|
151
|
+
if not hasattr(self, '_N_ens'):
|
|
152
|
+
return 1
|
|
153
|
+
return self._N_ens
|
|
154
|
+
|
|
155
|
+
@N_ens.setter
|
|
156
|
+
def N_ens(self, value):
|
|
157
|
+
if value <= 0:
|
|
158
|
+
raise ValueError("Number of ensemble members must be positive.")
|
|
159
|
+
if hasattr(self, 'history'):
|
|
160
|
+
Warning("Changing N_ens after initialization may lead to inconsistencies in the history.")
|
|
161
|
+
self._N_ens = value
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def augment_data_length(self):
|
|
165
|
+
augment_data = self.augment_data
|
|
166
|
+
if augment_data:
|
|
167
|
+
if isinstance(augment_data, int) and augment_data > 1:
|
|
168
|
+
return augment_data
|
|
169
|
+
return 2
|
|
170
|
+
return 1
|
|
171
|
+
|
|
172
|
+
@property
|
|
173
|
+
def initialize_bias_state(self):
|
|
174
|
+
"""
|
|
175
|
+
Only used at initialization. If the forecaster is a model, this should be handled by the child class.
|
|
176
|
+
The state has N_dim components: [bias] if the observations are unbiased,
|
|
177
|
+
or [bias; innovations] if the observations are biased (N_dim = 2 * Nq).
|
|
178
|
+
"""
|
|
179
|
+
return np.zeros((self.N_dim, self.N_ens))
|
|
180
|
+
|
|
181
|
+
@property
|
|
182
|
+
def forecaster(self):
|
|
183
|
+
assert hasattr(self, '_forecaster'), 'Forecaster not initialized yet.'
|
|
184
|
+
return self._forecaster # type: ignore #should be an instance of a forecaster model, e.g., ESN_model
|
|
185
|
+
|
|
186
|
+
@forecaster.setter
|
|
187
|
+
def forecaster(self, obj):
|
|
188
|
+
if self.forecaster_type is None:
|
|
189
|
+
raise ValueError('Child class must specify forecaster_type.')
|
|
190
|
+
assert isinstance(obj, self.forecaster_type), f'Forecaster must be an instance of {self.forecaster_type}, but got {type(obj)}.'
|
|
191
|
+
self._forecaster = obj
|
|
192
|
+
|
|
193
|
+
@property
|
|
194
|
+
def history(self) -> HistoryTracker:
|
|
195
|
+
return self._forecaster.history # type: ignore
|
|
196
|
+
|
|
197
|
+
@property
|
|
198
|
+
def integrator(self) -> Integrator:
|
|
199
|
+
"""
|
|
200
|
+
This is the integrator used by the model of the bias. E.g., DiscreteIntegrator if using ESN_model as forecaster.
|
|
201
|
+
"""
|
|
202
|
+
return self._forecaster.integrator # type: ignore
|
|
203
|
+
|
|
204
|
+
@property
|
|
205
|
+
def N_dim(self):
|
|
206
|
+
if not self.biased_observations:
|
|
207
|
+
return self.Nq
|
|
208
|
+
else:
|
|
209
|
+
return 2 * self.Nq
|
|
210
|
+
|
|
211
|
+
@property
|
|
212
|
+
def N(self):
|
|
213
|
+
return self.N_dim + self.N_hidden
|
|
214
|
+
|
|
215
|
+
@property
|
|
216
|
+
def N_hidden(self):
|
|
217
|
+
# Number of hidden units in the bias model, e.g., for ESN bias model. This is added to the state dimension N_dim to get the total state dimension N.
|
|
218
|
+
return 0
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@property
|
|
222
|
+
def bias_idx(self):
|
|
223
|
+
return np.arange(self.Nq)
|
|
224
|
+
|
|
225
|
+
@property
|
|
226
|
+
def observed_idx(self):
|
|
227
|
+
if self.biased_observations:
|
|
228
|
+
return self.bias_idx + self.Nq
|
|
229
|
+
else:
|
|
230
|
+
return self.bias_idx
|
|
231
|
+
|
|
232
|
+
def init_forecaster(self, **kwargs):
|
|
233
|
+
"""
|
|
234
|
+
.....
|
|
235
|
+
"""
|
|
236
|
+
raise NotImplementedError('Bias child classes must implement _init_forecaster() method.')
|
|
237
|
+
|
|
238
|
+
def state_derivative(self):
|
|
239
|
+
"""
|
|
240
|
+
Returns the derivative of the bias state, which is used for time integration.
|
|
241
|
+
This is computed by the forecaster model.
|
|
242
|
+
"""
|
|
243
|
+
raise NotImplementedError('Bias child classes must implement state_derivative property, typically computed by the forecaster model.')
|
|
244
|
+
|
|
245
|
+
def washout_phase(self, d_wash, t_wash, **kwargs) -> Optional[tuple[np.ndarray, np.ndarray]]:
|
|
246
|
+
"""
|
|
247
|
+
Optional method to initialize the bias model if needed, e.g., by running a washout phase with given data.
|
|
248
|
+
By default, does nothing, but can be implemented in child classes if needed.
|
|
249
|
+
"""
|
|
250
|
+
return None
|
|
251
|
+
|
|
252
|
+
@property
|
|
253
|
+
def washout_data(self):
|
|
254
|
+
"""
|
|
255
|
+
Returns the washout data used for initializing the bias model, which is typically obtained from the washout phase using the validation data. This property can be used to access the washout data for further processing or analysis.
|
|
256
|
+
|
|
257
|
+
Returns
|
|
258
|
+
-------
|
|
259
|
+
Tuple of (washout_data, washout_time) where:
|
|
260
|
+
- washout_data: np.ndarray - Washout data used for initializing the bias model.
|
|
261
|
+
- washout_time: np.ndarray - Time points corresponding to the washout data.
|
|
262
|
+
"""
|
|
263
|
+
return getattr(self, '_washout_data', (None, None))
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
@washout_data.setter
|
|
268
|
+
def washout_data(self, value):
|
|
269
|
+
assert isinstance(value, tuple) and len(value) == 2, "Washout data must be a tuple of (washout_data, washout_time)."
|
|
270
|
+
self._washout_data = value
|
|
271
|
+
|
|
272
|
+
def _format_state(self, b):
|
|
273
|
+
"""
|
|
274
|
+
Ensure b has shape (nt, nb, nens)
|
|
275
|
+
"""
|
|
276
|
+
if b.ndim == 3:
|
|
277
|
+
return b # already (nt, nb, nens)
|
|
278
|
+
elif b.ndim == 1:
|
|
279
|
+
b_repeated = np.repeat(b[:, np.newaxis], self.N_ens, axis=1) # (nb,) -> (nb, nens)
|
|
280
|
+
return b_repeated[np.newaxis, :, :] # (nb, nens) -> (1, nb, nens) Add extra dimension for time
|
|
281
|
+
|
|
282
|
+
elif b.ndim == 2 and (b.shape[-1] == self.N_ens or b.shape[0] == self.N_dim):
|
|
283
|
+
return b[np.newaxis, :, :] # (nb, nens) -> (1, nb, nens) # Add extra dimension for time
|
|
284
|
+
elif b.ndim == 2 and b.shape[-1] != self.N_ens:
|
|
285
|
+
return np.repeat(b[:, :, np.newaxis], self.N_ens, axis=2) # (nt, nb) -> (nt, nb, nens)
|
|
286
|
+
else:
|
|
287
|
+
raise AssertionError(f'b must have 1, 2 or 3 dimensions, got {b.ndim}=({b.shape})')
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
@property
|
|
291
|
+
def dt(self):
|
|
292
|
+
return self._dt
|
|
293
|
+
|
|
294
|
+
@dt.setter
|
|
295
|
+
def dt(self, value):
|
|
296
|
+
"""Setter for the time step."""
|
|
297
|
+
if value <= 0:
|
|
298
|
+
raise ValueError("Time step must be positive.")
|
|
299
|
+
self._dt = np.round(value, self.precision_t)
|
|
300
|
+
|
|
301
|
+
@property
|
|
302
|
+
def hist(self):
|
|
303
|
+
"""Returns only the valid (non-empty) portion of the history buffer."""
|
|
304
|
+
return self.history.hist
|
|
305
|
+
|
|
306
|
+
@property
|
|
307
|
+
def hist_t(self):
|
|
308
|
+
"""Returns only the valid portion of the time history."""
|
|
309
|
+
return self.history.hist_t
|
|
310
|
+
|
|
311
|
+
@property
|
|
312
|
+
def current_state(self):
|
|
313
|
+
"""Returns the current state (last entry in history)."""
|
|
314
|
+
return self.history.current_state.copy()
|
|
315
|
+
|
|
316
|
+
@property
|
|
317
|
+
def current_time(self):
|
|
318
|
+
"""Returns the current time (last entry in time history)."""
|
|
319
|
+
return self.history.current_time
|
|
320
|
+
|
|
321
|
+
|
|
322
|
+
@property
|
|
323
|
+
def config(self):
|
|
324
|
+
_config = dict()
|
|
325
|
+
for key in self.keys_to_print:
|
|
326
|
+
if hasattr(self, key):
|
|
327
|
+
_config[key] = getattr(self, key)
|
|
328
|
+
|
|
329
|
+
return _config
|
|
330
|
+
|
|
331
|
+
@property
|
|
332
|
+
def current_bias(self):
|
|
333
|
+
"""Returns the current (ensemble-mean) bias computed from the current state.
|
|
334
|
+
Shape: (Nq, 1) -- the bias is defined on the ensemble mean."""
|
|
335
|
+
return self.get_bias(state=self.current_state, mean=True)[0, :, :]
|
|
336
|
+
|
|
337
|
+
@property
|
|
338
|
+
def current_innovations(self):
|
|
339
|
+
"""Returns the current (ensemble-mean) innovations computed from the current state.
|
|
340
|
+
Shape: (Nq, 1)."""
|
|
341
|
+
return self.get_innovations(state=self.current_state, mean=True)[0, :, :]
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def get_bias(self, state, mean=False):
|
|
345
|
+
state = self._format_state(state)
|
|
346
|
+
if mean:
|
|
347
|
+
state = np.mean(state, axis=-1, keepdims=True)
|
|
348
|
+
|
|
349
|
+
return state[:, self.bias_idx, :]
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def get_innovations(self, state, mean=False):
|
|
353
|
+
state = self._format_state(state)
|
|
354
|
+
if mean:
|
|
355
|
+
state = np.mean(state, axis=-1, keepdims=True)
|
|
356
|
+
|
|
357
|
+
return state[:, self.observed_idx, :]
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
def get_bias_hist(self, mean=False):
|
|
361
|
+
return self.get_bias(state=self.hist, mean=mean)
|
|
362
|
+
|
|
363
|
+
def time_integrate(self, Nt):
|
|
364
|
+
return self.integrator.advance(Nt=Nt)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def update_history(self, state, t=None, reset=False, modify_saved_states=False, **kwargs):
|
|
368
|
+
state = self._format_state(state) # Ensure shape (nt, nstate, nens)
|
|
369
|
+
|
|
370
|
+
# Ensure time array matches nt
|
|
371
|
+
if t is None:
|
|
372
|
+
t = (np.arange(state.shape[0]) * self.dt).round(self.precision_t) + self.current_time
|
|
373
|
+
if isinstance(t, float):
|
|
374
|
+
t = np.array([t])
|
|
375
|
+
assert t.size == state.shape[0], f"Length of t ({t.size}) must match number of time steps in state ({state.shape[0]})."
|
|
376
|
+
|
|
377
|
+
if modify_saved_states:
|
|
378
|
+
assert state.shape[0] == 1, "When modify_saved_states is True, state must have only one time step (shape[0] == 1)."
|
|
379
|
+
|
|
380
|
+
self.history.update_history(state, t=t, reset=reset, modify_saved_states=modify_saved_states)
|
|
381
|
+
self.update_history_aux(state=state, reset=reset, modify_saved_states=modify_saved_states, **kwargs)
|
|
382
|
+
|
|
383
|
+
def update_history_aux(self, **kwargs):
|
|
384
|
+
"""Auxiliary method to update any additional history attributes in child classes if needed."""
|
|
385
|
+
pass
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
def update_state_from_innovation(self, input_innovation):
|
|
389
|
+
"""
|
|
390
|
+
Optional method to perform a Bayesian update to the state using the bias model. This can be implemented in child classes if needed, e.g., for ESN bias model.
|
|
391
|
+
By default, does nothing, but can be implemented in child classes if needed.
|
|
392
|
+
|
|
393
|
+
Parameters
|
|
394
|
+
----------
|
|
395
|
+
input_innovation : np.ndarray
|
|
396
|
+
Analysis innovation ensemble, shape ``(Nq, m)``, ``(Nq, 1)`` or ``(Nq,)``.
|
|
397
|
+
|
|
398
|
+
Returns
|
|
399
|
+
-------
|
|
400
|
+
np.ndarray
|
|
401
|
+
Updated estimator state, shape ``(N, N_ens)``.
|
|
402
|
+
"""
|
|
403
|
+
input_innovation = self._format_state(input_innovation) # Ensure shape (nt, nstate, nens)
|
|
404
|
+
assert input_innovation.shape[0] == 1, "Input innovation must have only one time step (shape[0] == 1) for state_from_innovation method."
|
|
405
|
+
|
|
406
|
+
forecast_state = self.current_state
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
if self.bayesian_update:
|
|
410
|
+
mean_innovation = np.mean(input_innovation[0], axis=-1) # Average innovation across ensemble (obs_dim, Nens) -> (obs_dim,)
|
|
411
|
+
# cov_innovation = (inn_uncertainty * np.max(np.abs(mean_innovation)))**2 * np.eye(mean_innovation.shape[0]) # Diagonal covariance of innovation (obs_dim, obs_dim)
|
|
412
|
+
# cov_innovation = np.cov(input_innovation[0], rowvar=True) # Full covariance of innovation (obs_dim, obs_dim)
|
|
413
|
+
# cov_innovation = np.atleast_2d(cov_innovation)
|
|
414
|
+
conv_inn = input_innovation[0] - mean_innovation[:, np.newaxis] # Centered innovations (obs_dim, Nens)
|
|
415
|
+
cov_innovation = np.atleast_2d(np.cov(conv_inn, rowvar=True)) # Full covariance of centered innovations (obs_dim, obs_dim)
|
|
416
|
+
|
|
417
|
+
updated_state = _ensrkf_update(Af=forecast_state, d=mean_innovation, Cdd=cov_innovation, M=self.DA_method)
|
|
418
|
+
else:
|
|
419
|
+
updated_state = forecast_state.copy()
|
|
420
|
+
mean_innovation = np.mean(input_innovation[0], axis=-1, keepdims=True) # Average innovation across ensemble (obs_dim, Nens) -> (obs_dim, 1)
|
|
421
|
+
if input_innovation.shape[-1] == self.N_ens:
|
|
422
|
+
# One innovation per bias-ensemble member: assign directly
|
|
423
|
+
updated_state[self.observed_idx, :] = input_innovation[0]
|
|
424
|
+
|
|
425
|
+
elif self.N_ens == 1 or input_innovation.shape[-1] == 1:
|
|
426
|
+
# Assign the same mean innovation to all ensemble members
|
|
427
|
+
updated_state[self.observed_idx, :] = np.repeat(mean_innovation, self.N_ens, axis=-1)
|
|
428
|
+
|
|
429
|
+
else: # ensemble sizes differ => resample from the innovation statistics
|
|
430
|
+
cov_innovation = np.cov(input_innovation[0], rowvar=True)
|
|
431
|
+
mean_innovation = mean_innovation.flatten()
|
|
432
|
+
cov_innovation = np.atleast_2d(cov_innovation)
|
|
433
|
+
resampled_innovation = np.random.multivariate_normal(mean_innovation, cov_innovation, size=self.N_ens).T # Resample innovations for each ensemble member (obs_dim, Nens)
|
|
434
|
+
updated_state[self.observed_idx, :] = resampled_innovation
|
|
435
|
+
|
|
436
|
+
return updated_state
|
|
437
|
+
|
|
438
|
+
|
|
439
|
+
@property
|
|
440
|
+
def DA_method(self):
|
|
441
|
+
"""Cached measurement operator `M` for the Bayesian innovation update (`_ensrkf_update`)."""
|
|
442
|
+
if not self.bayesian_update:
|
|
443
|
+
raise ValueError("Why is this being accessed when bayesian_update is False?")
|
|
444
|
+
elif not hasattr(self, '_DA_method'):
|
|
445
|
+
observation_operator = np.zeros((len(self.observed_idx), self.N))
|
|
446
|
+
observation_operator[:, self.observed_idx] = np.eye(len(self.observed_idx))
|
|
447
|
+
self._DA_method = observation_operator
|
|
448
|
+
print(f"Initialized Bayesian-update observation operator for bias model. M shape: {observation_operator.shape}")
|
|
449
|
+
|
|
450
|
+
return self._DA_method
|
|
451
|
+
|
|
452
|
+
def print_bias_parameters(self, indent=2):
|
|
453
|
+
print(f'\n{self.__class__.__name__}')
|
|
454
|
+
print(f'{"=" * len(self.__class__.__name__)}')
|
|
455
|
+
for key in sorted(set(self.keys_to_print)):
|
|
456
|
+
if hasattr(self, key):
|
|
457
|
+
val = getattr(self, key)
|
|
458
|
+
if type(val) is float:
|
|
459
|
+
print(f'{" " * indent}{key} = {val:.6}')
|
|
460
|
+
else:
|
|
461
|
+
print(f'{" " * indent}{key} = {val}')
|
|
462
|
+
if isinstance(self.forecaster, Model):
|
|
463
|
+
print(f'{" " * indent}====Forecaster model ====')
|
|
464
|
+
self.forecaster.print_parameters(show_header=False,
|
|
465
|
+
indent=indent*2)
|
|
466
|
+
|
|
467
|
+
|
|
468
|
+
def copy(self):
|
|
469
|
+
return deepcopy(self)
|
|
470
|
+
|
|
471
|
+
def close(self):
|
|
472
|
+
"""Close any resources used by the bias model, e.g., forecaster model."""
|
|
473
|
+
if hasattr(self, '_forecaster') and hasattr(self._forecaster, 'close'):
|
|
474
|
+
self._forecaster.close()
|
|
475
|
+
|
|
476
|
+
|
|
477
|
+
# ================= Visualization methods ================== #
|
|
478
|
+
def visualize_bias_and_innovations(self, state=None, t=None, plot_members=True, ylims=None):
|
|
479
|
+
if state is None:
|
|
480
|
+
state = self.hist
|
|
481
|
+
|
|
482
|
+
inn = self.get_innovations(state=state, mean=not plot_members)
|
|
483
|
+
b = self.get_bias(state=state, mean=not plot_members)
|
|
484
|
+
|
|
485
|
+
if t is None:
|
|
486
|
+
t = self.hist_t[-len(inn):]
|
|
487
|
+
|
|
488
|
+
lbls =( [f'inn_{ii}' for ii in range(inn.shape[1])],
|
|
489
|
+
[f'b_{ii}' for ii in range(b.shape[1])])
|
|
490
|
+
ttls = ['Innovations', 'Bias']
|
|
491
|
+
|
|
492
|
+
|
|
493
|
+
t_mid = len(t) - len(t)//8
|
|
494
|
+
lims = [[0, t_mid ], [t_mid, len(t)-1] ]
|
|
495
|
+
|
|
496
|
+
Nq = inn.shape[1]
|
|
497
|
+
nens = inn.shape[-1]
|
|
498
|
+
cols = categorical_cmap(nc=2, nsc=nens, continuous=False)
|
|
499
|
+
cols = [cols[ii * nens:(ii + 1) * nens] for ii in range(2)]
|
|
500
|
+
ci = 0
|
|
501
|
+
# Plot the time evolution of the observables
|
|
502
|
+
for y, lbl, ttl, cs in zip([inn, b], lbls, ttls, cols):
|
|
503
|
+
|
|
504
|
+
fig = plt.figure(figsize=(8, Nq+1), layout="constrained")
|
|
505
|
+
plt.suptitle(f'{ttl} time evolution')
|
|
506
|
+
|
|
507
|
+
axs = fig.subplots(Nq, 2, sharey='row', sharex='col')
|
|
508
|
+
if self.Nq == 1:
|
|
509
|
+
axs = [axs]
|
|
510
|
+
|
|
511
|
+
|
|
512
|
+
for ii, ax in enumerate(axs):
|
|
513
|
+
for jj, lim in enumerate(lims):
|
|
514
|
+
lines = ax[jj].plot(t[lim[0]:lim[1]], y[lim[0]:lim[1], ii])
|
|
515
|
+
for line, color in zip(lines, cs):
|
|
516
|
+
line.set_color(color)
|
|
517
|
+
if ii == self.Nq-1:
|
|
518
|
+
ax[jj].set(xlabel='$t$', xlim=[t[lim[0]], t[lim[1]]])
|
|
519
|
+
|
|
520
|
+
ax[0].set(ylabel=lbl[ii])
|
|
521
|
+
if ylims is not None:
|
|
522
|
+
ax[0].set(ylim=ylims)
|
|
523
|
+
|
|
524
|
+
ci += 1
|
|
525
|
+
|
|
526
|
+
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from types import SimpleNamespace
|
|
2
|
+
|
|
3
|
+
import numpy as np
|
|
4
|
+
from romda.models import ConstantIntegrator, HistoryTracker
|
|
5
|
+
|
|
6
|
+
from .bias import Bias
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class ConstantBias(Bias):
|
|
10
|
+
r"""
|
|
11
|
+
Constant (persistent) bias estimator.
|
|
12
|
+
|
|
13
|
+
The bias is held constant between analysis steps, i.e., the forecast model of the
|
|
14
|
+
bias is $\dot{\mathbf{b}} = \mathbf{0}$. At each analysis step, the bias state is
|
|
15
|
+
reset to the latest innovation (see `Bias.update_state_from_innovation`). This is
|
|
16
|
+
the classic persistent-bias assumption.
|
|
17
|
+
|
|
18
|
+
Parameters
|
|
19
|
+
----------
|
|
20
|
+
innovation : np.ndarray
|
|
21
|
+
Initial innovation/bias estimate, shape (Nq,), (Nq, N_ens) or (1, Nq, N_ens).
|
|
22
|
+
t : float
|
|
23
|
+
Initial time.
|
|
24
|
+
dt : float
|
|
25
|
+
Time step of the output history.
|
|
26
|
+
k : float or np.ndarray, optional
|
|
27
|
+
If provided, the initial bias state is set to the constant value(s) k instead of
|
|
28
|
+
the provided innovation.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
upsample = 1 # The forecast is constant, so no upsampling is needed
|
|
32
|
+
|
|
33
|
+
def __init__(self, innovation, t, dt, k=None, **kwargs):
|
|
34
|
+
|
|
35
|
+
if k is not None:
|
|
36
|
+
innovation = np.ones(np.shape(innovation)) * k # Constant initial bias
|
|
37
|
+
|
|
38
|
+
self._b0 = np.asarray(innovation, dtype=float)
|
|
39
|
+
|
|
40
|
+
super().__init__(innovation=innovation, t=t, dt=dt, **kwargs)
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def initialize_bias_state(self):
|
|
44
|
+
b0 = self._b0
|
|
45
|
+
if b0.ndim == 3:
|
|
46
|
+
b0 = b0[0]
|
|
47
|
+
elif b0.ndim == 1:
|
|
48
|
+
b0 = b0[:, np.newaxis]
|
|
49
|
+
|
|
50
|
+
if b0.shape[-1] != self.N_ens:
|
|
51
|
+
b0 = np.repeat(np.mean(b0, axis=-1, keepdims=True), self.N_ens, axis=1)
|
|
52
|
+
|
|
53
|
+
if self.biased_observations and b0.shape[0] == self.Nq:
|
|
54
|
+
# State is [bias; innovations]: initialize both blocks with the same values
|
|
55
|
+
b0 = np.concatenate([b0, b0], axis=0)
|
|
56
|
+
|
|
57
|
+
assert b0.shape[0] == self.N_dim, \
|
|
58
|
+
f'Initial bias state has {b0.shape[0]} components, expected N_dim={self.N_dim}.'
|
|
59
|
+
return b0
|
|
60
|
+
|
|
61
|
+
def state_derivative(self):
|
|
62
|
+
n_bias = self.Nq
|
|
63
|
+
return np.zeros([n_bias, n_bias])
|
|
64
|
+
|
|
65
|
+
def init_forecaster(self, **kwargs):
|
|
66
|
+
"""
|
|
67
|
+
Constant forecaster: no underlying model, the state is simply held in time.
|
|
68
|
+
"""
|
|
69
|
+
self._forecaster = SimpleNamespace()
|
|
70
|
+
|
|
71
|
+
initial_capacity = kwargs.pop('initial_capacity', 1000)
|
|
72
|
+
self._forecaster.history = HistoryTracker(initial_capacity=initial_capacity)
|
|
73
|
+
|
|
74
|
+
# INITIALISE INTEGRATOR STRATEGY ================== ##
|
|
75
|
+
self._forecaster.integrator = ConstantIntegrator(self)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class NoBias(ConstantBias):
|
|
79
|
+
"""
|
|
80
|
+
Placeholder bias estimator that always returns zero bias.
|
|
81
|
+
Useful to run the bias-aware machinery in its unbiased limit.
|
|
82
|
+
"""
|
|
83
|
+
|
|
84
|
+
def __init__(self, innovation, t, dt, **kwargs):
|
|
85
|
+
kwargs.pop('k', None)
|
|
86
|
+
super().__init__(innovation=innovation, t=t, dt=dt, k=0., **kwargs)
|
|
87
|
+
|
|
88
|
+
def update_state_from_innovation(self, input_innovation):
|
|
89
|
+
# NoBias never updates: the bias remains identically zero
|
|
90
|
+
return self.current_state
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
if __name__ == '__main__':
|
|
94
|
+
nb = ConstantBias(innovation=np.ones((2, 7)), t=0.0, dt=0.1)
|
|
95
|
+
|
|
96
|
+
state, t = nb.time_integrate(Nt=10)
|
|
97
|
+
nb.update_history(state, t)
|
|
98
|
+
|
|
99
|
+
reset_constant = np.array([np.random.rand(nb.N_dim)]).T * np.ones_like(state[-1])
|
|
100
|
+
nb.update_history(reset_constant, modify_saved_states=True)
|
|
101
|
+
|
|
102
|
+
state, t = nb.time_integrate(Nt=10)
|
|
103
|
+
nb.update_history(state, t)
|
|
104
|
+
|
|
105
|
+
print(nb.history.hist.shape, nb.get_bias_hist())
|