pyelq 1.1.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.
@@ -0,0 +1,327 @@
1
+ # SPDX-FileCopyrightText: 2024 Shell Global Solutions International B.V. All Rights Reserved.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # -*- coding: utf-8 -*-
6
+ """Error model module."""
7
+ from dataclasses import dataclass, field
8
+ from typing import TYPE_CHECKING, Union
9
+
10
+ import numpy as np
11
+ from openmcmc import parameter
12
+ from openmcmc.distribution.distribution import Gamma
13
+ from openmcmc.model import Model
14
+ from openmcmc.sampler.sampler import NormalGamma
15
+
16
+ from pyelq.component.component import Component
17
+ from pyelq.gas_species import GasSpecies
18
+ from pyelq.meteorology import MeteorologyGroup
19
+ from pyelq.sensor.sensor import Sensor, SensorGroup
20
+
21
+ if TYPE_CHECKING:
22
+ from pyelq.plotting.plot import Plot
23
+
24
+
25
+ @dataclass
26
+ class ErrorModel(Component):
27
+ """Measurement precision model component for the model.
28
+
29
+ Attributes:
30
+ n_sensor (int): number of sensors in the sensor object used for analysis.
31
+ precision_index (np.ndarray): index mapping precision parameters onto observations. Will be set up differently
32
+ for different model types.
33
+ precision_parameter (parameter.Parameter): parameter object which constructs the full measurement error
34
+ precision matrix from the components stored in state. Will be passed to the distribution for the observed
35
+ when the full model is constructed.
36
+ prior_precision_shape (Union[np.ndarray, float]): prior shape parameters for the precision model. Set up
37
+ differently per model type.
38
+ prior_precision_rate (Union[np.ndarray, float]): prior rate parameters for the precision model. Set up
39
+ differently per model type.
40
+ initial_precision (Union[np.ndarray, float]): initial value for the precision to be passed to the analysis
41
+ routine. Set up differently per model type.
42
+ precision (np.ndarray): array of sampled measurement error precision values, populated in self.from_mcmc() after
43
+ the MCMC run is completed.
44
+
45
+ """
46
+
47
+ n_sensor: int = field(init=False)
48
+ precision_index: np.ndarray = field(init=False)
49
+ precision_parameter: parameter.Parameter = field(init=False)
50
+ prior_precision_shape: Union[np.ndarray, float] = field(init=False)
51
+ prior_precision_rate: Union[np.ndarray, float] = field(init=False)
52
+ initial_precision: Union[np.ndarray, float] = field(init=False)
53
+ precision: np.ndarray = field(init=False)
54
+
55
+ def initialise(
56
+ self, sensor_object: SensorGroup, meteorology: MeteorologyGroup = None, gas_species: GasSpecies = None
57
+ ):
58
+ """Take data inputs and extract relevant properties.
59
+
60
+ Args:
61
+ sensor_object (SensorGroup): sensor data.
62
+ meteorology (MeteorologyGroup): meteorology data. Defaults to None.
63
+ gas_species (GasSpecies): gas species information. Defaults to None.
64
+
65
+ """
66
+ self.n_sensor = sensor_object.nof_sensors
67
+
68
+ def make_model(self, model: list = None) -> list:
69
+ """Take model list and append new elements from current model component.
70
+
71
+ Args:
72
+ model (list, optional): Current list of model elements. Defaults to None.
73
+
74
+ Returns:
75
+ list: model output list.
76
+
77
+ """
78
+ if model is None:
79
+ model = []
80
+ model.append(Gamma("tau", shape="a_tau", rate="b_tau"))
81
+ return model
82
+
83
+ def make_sampler(self, model: Model, sampler_list: list = None) -> list:
84
+ """Take sampler list and append new elements from current model component.
85
+
86
+ Args:
87
+ model (Model): Full model list of distributions.
88
+ sampler_list (list, optional): Current list of samplers. Defaults to None.
89
+
90
+ Returns:
91
+ list: sampler output list.
92
+
93
+ """
94
+ if sampler_list is None:
95
+ sampler_list = []
96
+ sampler_list.append(NormalGamma("tau", model))
97
+ return sampler_list
98
+
99
+ def make_state(self, state: dict = None) -> dict:
100
+ """Take state dictionary and append initial values from model component.
101
+
102
+ Args:
103
+ state (dict, optional): current state vector. Defaults to None.
104
+
105
+ Returns:
106
+ dict: current state vector with components added.
107
+
108
+ """
109
+ if state is None:
110
+ state = {}
111
+ state["a_tau"] = self.prior_precision_shape.flatten()
112
+ state["b_tau"] = self.prior_precision_rate.flatten()
113
+ state["precision_index"] = self.precision_index
114
+ state["tau"] = self.initial_precision.flatten()
115
+ return state
116
+
117
+ def from_mcmc(self, store: dict):
118
+ """Extract results of mcmc from mcmc.store and attach to components.
119
+
120
+ Args:
121
+ store (dict): mcmc result dictionary.
122
+
123
+ """
124
+ self.precision = store["tau"]
125
+
126
+
127
+ @dataclass
128
+ class BySensor(ErrorModel):
129
+ """Version of measurement precision where each sensor object has a different precision.
130
+
131
+ Attributes:
132
+ prior_precision_shape (Union[np.ndarray, float]): prior shape parameters for the precision model, can be
133
+ specified either as a float or as a (nof_sensors, ) np.ndarray: a float specification will result in
134
+ the same parameter value for each sensor. Defaults to 1e-3.
135
+ prior_precision_rate (Union[np.ndarray, float]): prior rate parameters for the precision model, can be
136
+ specified either as a float or as a (nof_sensors, ) np.ndarray: a float specification will result in
137
+ the same parameter value for each sensor. Defaults to 1e-3.
138
+ initial_precision (Union[np.ndarray, float]): initial value for the precision parameters, can be specified
139
+ either as a float or as a (nof_sensors, ) np.ndarray: a float specification will result in the same
140
+ parameter value for each sensor. Defaults to 1.
141
+ precision_index (np.ndarray): index mapping precision parameters onto observations. Parameters 1:n_sensor are
142
+ mapped as the measurement error precisions of the corresponding sensors.
143
+ precision_parameter (Parameter.MixtureParameterMatrix): parameter specification for this model, maps the
144
+ current value of the parameter in the state dict onto the concentration data precisions.
145
+
146
+ """
147
+
148
+ prior_precision_shape: Union[np.ndarray, float] = 1e-3
149
+ prior_precision_rate: Union[np.ndarray, float] = 1e-3
150
+ initial_precision: Union[np.ndarray, float] = 1.0
151
+
152
+ def initialise(
153
+ self, sensor_object: SensorGroup, meteorology: MeteorologyGroup = None, gas_species: GasSpecies = None
154
+ ):
155
+ """Set up the error model using sensor properties.
156
+
157
+ Args:
158
+ sensor_object (SensorGroup): sensor data.
159
+ meteorology (MeteorologyGroup): meteorology data. Defaults to None.
160
+ gas_species (GasSpecies): gas species information. Defaults to None.
161
+
162
+ """
163
+ super().initialise(sensor_object=sensor_object, meteorology=meteorology, gas_species=gas_species)
164
+ self.prior_precision_shape = self.prior_precision_shape * np.ones((self.n_sensor,))
165
+ self.prior_precision_rate = self.prior_precision_rate * np.ones((self.n_sensor,))
166
+ self.initial_precision = self.initial_precision * np.ones((self.n_sensor,))
167
+ self.precision_index = sensor_object.sensor_index
168
+ self.precision_parameter = parameter.MixtureParameterMatrix(param="tau", allocation="precision_index")
169
+
170
+ def plot_iterations(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
171
+ """Plots the error model values for every sensor with respect to the MCMC iterations.
172
+
173
+ Args:
174
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the error_model
175
+ burn_in_value (int): Burn in value to show in plot.
176
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
177
+
178
+ Returns:
179
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
180
+ key 'error_model_iterations'
181
+
182
+ """
183
+ plot.plot_trace_per_sensor(
184
+ object_to_plot=self, sensor_object=sensor_object, plot_type="line", burn_in=burn_in_value
185
+ )
186
+
187
+ return plot
188
+
189
+ def plot_distributions(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
190
+ """Plots the distribution of the error model values after the burn in for every sensor.
191
+
192
+ Args:
193
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the error_model
194
+ burn_in_value (int): Burn in value to show in plot.
195
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
196
+
197
+ Returns:
198
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
199
+ key 'error_model_distributions'
200
+
201
+ """
202
+ plot.plot_trace_per_sensor(
203
+ object_to_plot=self, sensor_object=sensor_object, plot_type="box", burn_in=burn_in_value
204
+ )
205
+
206
+ return plot
207
+
208
+
209
+ @dataclass
210
+ class ByRelease(ErrorModel):
211
+ """ByRelease error model, special case of the measurement precision model.
212
+
213
+ Version of the measurement precision model where each sensor object has a different precision, and there are
214
+ different precisions for periods inside and outside controlled release periods. For all parameters: the first
215
+ element corresponds to the case where the sources are OFF; the second element corresponds to the case where the
216
+ sources are ON.
217
+
218
+ Attributes:
219
+ prior_precision_shape (np.ndarray): prior shape parameters for the precision model, can be
220
+ specified either as a (2, 1) np.ndarray or as a (2, nof_sensors) np.ndarray: the former specification
221
+ will result in the same prior specification for the off/on precisions for each sensor. Defaults to
222
+ np.array([1e-3, 1e-3]).
223
+ prior_precision_rate (np.ndarray): prior rate parameters for the precision model, can be
224
+ specified either as a (2, 1) np.ndarray or as a (2, nof_sensors) np.ndarray: the former specification
225
+ will result in the same prior specification for the off/on precisions for each sensor. Defaults to
226
+ np.array([1e-3, 1e-3]).
227
+ initial_precision (np.ndarray): initial value for the precision parameters, can be
228
+ specified either as a (2, 1) np.ndarray or as a (2, nof_sensors) np.ndarray: the former specification
229
+ will result in the same prior specification for the off/on precisions for each sensor. Defaults to
230
+ np.array([1.0, 1.0]).
231
+ precision_index (np.ndarray): index mapping precision parameters onto observations. Parameters 1:n_sensor are
232
+ mapped onto each sensor for the periods where the sources are OFF; parameters (n_sensor + 1):(2 * n_sensor)
233
+ are mapped onto each sensor for the periods where the sources are ON.
234
+ precision_parameter (Parameter.MixtureParameterMatrix): parameter specification for this model, maps the
235
+ current value of the parameter in the state dict onto the concentration data precisions.
236
+
237
+ """
238
+
239
+ prior_precision_shape: np.ndarray = field(default_factory=lambda: np.array([1e-3, 1e-3], ndmin=2).T)
240
+ prior_precision_rate: np.ndarray = field(default_factory=lambda: np.array([1e-3, 1e-3], ndmin=2).T)
241
+ initial_precision: np.ndarray = field(default_factory=lambda: np.array([1.0, 1.0], ndmin=2).T)
242
+
243
+ def initialise(
244
+ self, sensor_object: SensorGroup, meteorology: MeteorologyGroup = None, gas_species: GasSpecies = None
245
+ ):
246
+ """Set up the error model using sensor properties.
247
+
248
+ Args:
249
+ sensor_object (SensorGroup): sensor data.
250
+ meteorology (MeteorologyGroup): meteorology data. Defaults to None.
251
+ gas_species (GasSpecies): gas species information. Defaults to None.
252
+
253
+ """
254
+ super().initialise(sensor_object=sensor_object, meteorology=meteorology, gas_species=gas_species)
255
+ self.prior_precision_shape = self.prior_precision_shape * np.ones((2, self.n_sensor))
256
+ self.prior_precision_rate = self.prior_precision_rate * np.ones((2, self.n_sensor))
257
+ self.initial_precision = self.initial_precision * np.ones((2, self.n_sensor))
258
+ self.precision_index = sensor_object.sensor_index + sensor_object.source_on * self.n_sensor
259
+ self.precision_parameter = parameter.MixtureParameterMatrix(param="tau", allocation="precision_index")
260
+
261
+ def plot_iterations(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
262
+ """Plot the estimated error model parameters against iterations of the MCMC chain.
263
+
264
+ Works by simply creating a separate plot for each of the two categories of precision parameter (when the
265
+ sources are on/off). Creates a BySensor() object for each of the off/on precision cases, and then makes a
266
+ call to its plot function.
267
+
268
+ Args:
269
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the error_model
270
+ burn_in_value (int): Burn in value to show in plot.
271
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
272
+
273
+ Returns:
274
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
275
+ key 'error_model_iterations'
276
+
277
+ """
278
+ figure_keys = ["error_model_off_iterations", "error_model_on_iterations"]
279
+ figure_titles = [
280
+ "Estimated error parameter values: sources off",
281
+ "Estimated error parameter values: sources on",
282
+ ]
283
+ precision_arrays = [
284
+ self.precision[: sensor_object.nof_sensors, :],
285
+ self.precision[sensor_object.nof_sensors :, :],
286
+ ]
287
+ for key, title, array in zip(figure_keys, figure_titles, precision_arrays):
288
+ error_model = BySensor()
289
+ error_model.precision = array
290
+ plot = error_model.plot_iterations(plot, sensor_object, burn_in_value)
291
+ plot.figure_dict[key] = plot.figure_dict.pop("error_model_iterations")
292
+ plot.figure_dict[key].update_layout(title=title)
293
+ return plot
294
+
295
+ def plot_distributions(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
296
+ """Plot the estimated distributions of error model parameters.
297
+
298
+ Works by simply creating a separate plot for each of the two categories of precision parameter (when the
299
+ sources are off/on). Creates a BySensor() object for each of the off/on precision cases, and then makes a
300
+ call to its plot function.
301
+
302
+ Args:
303
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the error_model
304
+ burn_in_value (int): Burn in value to show in plot.
305
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
306
+
307
+ Returns:
308
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
309
+ key 'error_model_distributions'
310
+
311
+ """
312
+ figure_keys = ["error_model_off_distributions", "error_model_on_distributions"]
313
+ figure_titles = [
314
+ "Estimated error parameter distribution: sources off",
315
+ "Estimated error parameter distribution: sources on",
316
+ ]
317
+ precision_arrays = [
318
+ self.precision[: sensor_object.nof_sensors, :],
319
+ self.precision[sensor_object.nof_sensors :, :],
320
+ ]
321
+ for key, title, array in zip(figure_keys, figure_titles, precision_arrays):
322
+ error_model = BySensor()
323
+ error_model.precision = array
324
+ plot = error_model.plot_distributions(plot, sensor_object, burn_in_value)
325
+ plot.figure_dict[key] = plot.figure_dict.pop("error_model_distributions")
326
+ plot.figure_dict[key].update_layout(title=title)
327
+ return plot
@@ -0,0 +1,183 @@
1
+ # SPDX-FileCopyrightText: 2024 Shell Global Solutions International B.V. All Rights Reserved.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # -*- coding: utf-8 -*-
6
+ """Offset module."""
7
+ from dataclasses import dataclass, field
8
+ from typing import TYPE_CHECKING, Union
9
+
10
+ import numpy as np
11
+ from openmcmc import parameter
12
+ from openmcmc.distribution.distribution import Gamma
13
+ from openmcmc.distribution.location_scale import Normal
14
+ from openmcmc.model import Model
15
+ from openmcmc.sampler.sampler import NormalGamma, NormalNormal
16
+ from scipy import sparse
17
+
18
+ from pyelq.component.component import Component
19
+ from pyelq.gas_species import GasSpecies
20
+ from pyelq.meteorology import Meteorology
21
+ from pyelq.sensor.sensor import Sensor, SensorGroup
22
+
23
+ if TYPE_CHECKING:
24
+ from pyelq.plotting.plot import Plot
25
+
26
+
27
+ @dataclass
28
+ class PerSensor(Component):
29
+ """Offset implementation which assumes an additive offset between sensors.
30
+
31
+ The offset is which is constant in space and time and accounts for calibration differences between sensors.
32
+ To maintain parameter identifiability, the offset for the first sensor (with index 0) is assumed to be 0, and other
33
+ sensor offsets are defined relative to this beam.
34
+
35
+ Attributes:
36
+ n_sensor (int): number of sensors in the sensor object used for analysis.
37
+ offset (np.ndarray): array of sampled offset values, populated in self.from_mcmc() after the MCMC run is
38
+ completed.
39
+ precision_scalar (np.ndarray): array of sampled offset precision values, populated in self.from_mcmc() after
40
+ the MCMC run is completed. Only populated if update_precision is True.
41
+ indicator_basis (sparse.csc_matrix): [nof_observations x (nof_sensors - 1)] sparse matrix which assigns the
42
+ offset parameters to the correct observations.
43
+ update_precision (bool): logical indicating whether the offset prior precision parameter should be updated as
44
+ part of the analysis.
45
+ mean_offset (float): prior mean parameter for the offsets, assumed to be the same for each beam. Default is 0.
46
+ prior_precision_shape (float): shape parameter for the prior gamma distribution for the scalar precision
47
+ parameter. Default is 1e-3.
48
+ prior_precision_rate (float): rate parameter for the prior gamma distribution for the scalar precision
49
+ parameter(s). Default is 1e-3.
50
+ initial_precision (float): initial value for the scalar precision parameter. Default is 1.0.
51
+
52
+ """
53
+
54
+ n_sensor: int = field(init=False)
55
+ offset: np.ndarray = field(init=False)
56
+ precision_scalar: np.ndarray = field(init=False)
57
+ indicator_basis: sparse.csc_matrix = field(init=False)
58
+ update_precision: bool = False
59
+ mean_offset: float = 0.0
60
+ prior_precision_shape: float = 1e-3
61
+ prior_precision_rate: float = 1e-3
62
+ initial_precision: float = 1.0
63
+
64
+ def initialise(self, sensor_object: SensorGroup, meteorology: Meteorology, gas_species: GasSpecies):
65
+ """Take data inputs and extract relevant properties.
66
+
67
+ Args:
68
+ sensor_object (SensorGroup): sensor data
69
+ meteorology (MeteorologyGroup): meteorology data wind data
70
+ gas_species (GasSpecies): gas species information
71
+
72
+ """
73
+ self.n_sensor = len(sensor_object)
74
+ self.indicator_basis = sparse.csc_matrix(
75
+ np.equal(sensor_object.sensor_index[:, np.newaxis], np.array(range(1, self.n_sensor)))
76
+ )
77
+
78
+ def make_model(self, model: list = None) -> list:
79
+ """Take model list and append new elements from current model component.
80
+
81
+ Args:
82
+ model (list, optional): Current list of model elements. Defaults to [].
83
+
84
+ Returns:
85
+ list: model output list.
86
+
87
+ """
88
+ if model is None:
89
+ model = []
90
+ off_precision_predictor = parameter.ScaledMatrix(matrix="P_d", scalar="lambda_d")
91
+ model.append(Normal("d", mean="mu_d", precision=off_precision_predictor))
92
+ if self.update_precision:
93
+ model.append(Gamma("lambda_d", shape="a_lam_d", rate="b_lam_d"))
94
+ return model
95
+
96
+ def make_sampler(self, model: Model, sampler_list: list = None) -> list:
97
+ """Take sampler list and append new elements from current model component.
98
+
99
+ Args:
100
+ model (Model): Full model list of distributions.
101
+ sampler_list (list, optional): Current list of samplers. Defaults to [].
102
+
103
+ Returns:
104
+ list: sampler output list.
105
+
106
+ """
107
+ if sampler_list is None:
108
+ sampler_list = []
109
+ sampler_list.append(NormalNormal("d", model))
110
+ if self.update_precision:
111
+ sampler_list.append(NormalGamma("lambda_d", model))
112
+ return sampler_list
113
+
114
+ def make_state(self, state: dict = None) -> dict:
115
+ """Take state dictionary and append initial values from model component.
116
+
117
+ Args:
118
+ state (dict, optional): current state vector. Defaults to {}.
119
+
120
+ Returns:
121
+ dict: current state vector with components added.
122
+
123
+ """
124
+ if state is None:
125
+ state = {}
126
+ state["mu_d"] = np.ones((self.n_sensor - 1, 1)) * self.mean_offset
127
+ state["d"] = np.zeros((self.n_sensor - 1, 1))
128
+ state["B_d"] = self.indicator_basis
129
+ state["P_d"] = sparse.eye(self.n_sensor - 1, format="csc")
130
+ state["lambda_d"] = self.initial_precision
131
+ if self.update_precision:
132
+ state["a_lam_d"] = self.prior_precision_shape
133
+ state["b_lam_d"] = self.prior_precision_rate
134
+ return state
135
+
136
+ def from_mcmc(self, store: dict):
137
+ """Extract results of mcmc from mcmc.store and attach to components.
138
+
139
+ Args:
140
+ store (dict): mcmc result dictionary.
141
+
142
+ """
143
+ self.offset = store["d"]
144
+ if self.update_precision:
145
+ self.precision_scalar = store["lambda_d"]
146
+
147
+ def plot_iterations(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
148
+ """Plots the offset values for every sensor with respect to the MCMC iterations.
149
+
150
+ Args:
151
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the offset_model
152
+ burn_in_value (int): Burn in value to show in plot.
153
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
154
+
155
+ Returns:
156
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
157
+ key 'offset_iterations'
158
+
159
+ """
160
+ plot.plot_trace_per_sensor(
161
+ object_to_plot=self, sensor_object=sensor_object, plot_type="line", burn_in=burn_in_value
162
+ )
163
+
164
+ return plot
165
+
166
+ def plot_distributions(self, plot: "Plot", sensor_object: Union[SensorGroup, Sensor], burn_in_value: int) -> "Plot":
167
+ """Plots the distribution of the offset values after the burn in for every sensor.
168
+
169
+ Args:
170
+ sensor_object (Union[SensorGroup, Sensor]): Sensor object associated with the offset_model
171
+ burn_in_value (int): Burn in value to use for plot.
172
+ plot (Plot): Plot object to which this figure will be added in the figure dictionary
173
+
174
+ Returns:
175
+ plot (Plot): Plot object to which this figure is added in the figure dictionary with
176
+ key 'offset_distributions'
177
+
178
+ """
179
+ plot.plot_trace_per_sensor(
180
+ object_to_plot=self, sensor_object=sensor_object, plot_type="box", burn_in=burn_in_value
181
+ )
182
+
183
+ return plot