pysensors.reconstruction package

Module contents

class pysensors.reconstruction.SSPOR(basis=None, optimizer=None, n_sensors=None)[source]

Bases: BaseEstimator

Sparse Sensor Placement Optimization for Reconstruction: a model for selecting the best sensor locations for state reconstruction.

Given a basis in which to represent the state (e.g. PCA modes) along with measurement data, a SSPOR instance produces a list of sensor locations (a permutation of the numbers 0, 1, …, n_input_features - 1) ranked in descending order of importance. One can then select the top k sensors and take future measurements at that limited set of locations.

The overall time complexity of fitting a SSPOR object is O(n_basis_modes * n_input_features * n_input_features) plus the cost for fitting the basis. Different bases have different complexities. The space complexity is O(n_basis_modes * n_input_features).

Parameters
  • basis (basis object, optional (default pysensors.basis.Identity)) – Basis in which to represent the data. Default is the identity basis (i.e. raw features).

  • optimizer (optimizer object, optional (default pysensors.optimizers.QR)) – Optimization method used to rank sensor locations.

  • n_sensors (int, optional (default n_input_features)) – Number of sensors to select. Note that s = SSPOR(n_sensors=10); s.fit(x) is equivalent to s = SSPOR(); s.fit(x); s.set_number_of_sensors(10).

Attributes
  • n_basis_modes (int) – Number of basis modes considered during fitting.

  • basis_matrix_ (np.ndarray) – Internal representation of the basis.

  • ranked_sensors_ (np.ndarray) – Sensor locations ranked in descending order of importance.

Examples

>>> import numpy as np
>>> from pysensors import SSPOR
>>>
>>> x = np.linspace(0, 1, 501)
>>> monomials = np.vander(x, 15).T
>>>
>>> model = SSPOR(n_sensors=5)
>>> model.fit(monomials)
SSPOR(basis=Identity(n_basis_modes=15), n_sensors=5, optimizer=QR())
>>> print(model.selected_sensors)
[500 377   0 460 185]
>>> print(x[model.selected_sensors])
[1.    0.754 0.    0.92  0.37 ]
>>> model.set_n_sensors(7)
>>> print(x[model.selected_sensors])
[1.    0.754 0.    0.92  0.37  0.572 0.134]
>>> f = np.sin(3*x)
>>> f_pred = model.predict(f[model.selected_sensors])
>>> print(np.linalg.norm(f - f_pred))
0.022405698005838044
fit(x, quiet=False, prefit_basis=False, seed=None, **optimizer_kws)[source]

Fit the SSPOR model, determining which sensors are relevant.

Parameters
  • x (array-like, shape (n_samples, n_input_features)) – Training data.

  • quiet (boolean, optional (default False)) – Whether or not to suppress warnings during fitting.

  • prefit_basis (boolean, optional (default False)) – Whether or not the basis has already been fit to x. For example, you may have already fit and experimented with a SVD object to determine the optimal number of modes. This option allows you to avoid an unnecessary SVD.

  • seed (int, optional (default None)) – Seed for the random number generator used to shuffle sensors after the self.basis.n_basis_modes sensor. Most optimizers only rank the top self.basis.n_basis_modes sensors, leaving the rest virtually untouched. As a result the remaining samples are randomly permuted.

  • optimizer_kws (dict, optional) – Keyword arguments to be passed to the get_sensors method of the optimizer.

Returns

self

Return type

a fitted SSPOR instance

predict(x, **solve_kws)[source]

Predict values at all positions given measurements at sensor locations.

Parameters
  • x (array-like, shape (n_samples, n_sensors)) – Measurements from which to form prediction. The measurements should be taken at the sensor locations specified by self.get_selected_sensors().

  • solve_kws (dict, optional) – keyword arguments to be passed to the linear solver used to invert the basis matrix.

Returns

y – Predicted values at every location.

Return type

numpy array, shape (n_samples, n_features)

get_selected_sensors()[source]

Get the indices of the sensors chosen by the model.

Returns

sensors – Indices of the sensors chosen by the model (i.e. the sensor locations) ranked in descending order of importance.

Return type

numpy array, shape (n_sensors,)

property selected_sensors

Get the indices of the sensors chosen by the model.

Returns

sensors – Indices of the sensors chosen by the model (i.e. the sensor locations) ranked in descending order of importance.

Return type

numpy array, shape (n_sensors,)

get_all_sensors()[source]

Get a ranked list consisting of all the sensors. The sensors are given in descending order of importance.

Returns

sensors – Indices of sensors in descending order of importance.

Return type

numpy array, shape (n_features,)

property all_sensors

Get a ranked list consisting of all the sensors. The sensors are given in descending order of importance.

Returns

sensors – Indices of sensors in descending order of importance.

Return type

numpy array, shape (n_features,)

set_number_of_sensors(n_sensors)[source]

Set n_sensors, the number of sensors to be used for prediction.

Parameters

n_sensors (int) – The number of sensors. Must be a positive integer. Cannot exceed the number of available sensors (n_features).

set_n_sensors(n_sensors)[source]

A convenience function accomplishing the same thing as set_number_of_sensors. Set n_sensors, the number of sensors to be used for prediction.

Parameters

n_sensors (int) – The number of sensors. Must be a positive integer. Cannot exceed the number of available sensors (n_features).

update_n_basis_modes(n_basis_modes, x=None, quiet=False)[source]

Re-fit the SSPOR object using a different value of n_basis_modes.

This method allows one to relearn sensor locations for a different number of basis modes _without_ re-fitting the basis in many cases. Specifically, if n_basis_modes <= self.basis.n_basis_modes then the basis does not need to be refit. Otherwise this function does not save any computational resources.

Parameters
  • n_basis_modes (positive int, optional (default None)) – Number of basis modes to be used during fit. Must be less than or equal to n_samples.

  • x (numpy array, shape (n_examples, n_features), optional (default None)) – Only used if n_basis_modes exceeds the number of available basis modes for the already fit basis.

  • quiet (boolean, optional (default False)) – Whether or not to suppress warnings during refitting.

score(x, y=None, score_function=None, score_kws={}, solve_kws={})[source]

Compute the reconstruction error for a given set of measurements.

Parameters
  • x (numpy array, shape (n_examples, n_features)) – Measurements with which to compute the score. Note that x should consist of measurements at every location, not just the recommended sensor location, i.e. its shape should be (n_examples, n_features) rather than (n_examples, n_sensors).

  • y (None) – Dummy input to maintain compatibility with Scikit-learn.

  • score_function (callable, optional (default None)) – Function used to compute the score. Should have the call signature score_function(y_true, y_pred, **score_kws). Default is the negative of the root mean squared error (sklearn expects higher scores to correspond to better performance).

  • score_kws (dict, optional) – Keyword arguments to be passed to score_function. Ignored if score_function is None.

  • solve_kws (dict, optional) – Keyword arguments to be passed to the predict method.

Returns

score – The score.

Return type

float

reconstruction_error(x_test, sensor_range=None, score=None, **solve_kws)[source]

Compute the reconstruction error for different numbers of sensors.

Parameters
  • x_test (numpy array, shape (n_examples, n_features)) – Measurements to be reconstructed.

  • sensor_range (1D numpy array, optional (default None)) – Numbers of sensors at which to compute the reconstruction error. If None, will be set to [1, 2, … , min(n_sensors, basis.n_basis_modes)].

  • score (callable, optional (default None)) – Function used to compute the reconstruction error. Should have the signature score(x, x_pred). If None, the root mean squared error is used.

  • solve_kws (dict, optional) – Keyword arguments to be passed to the linear solver.

Returns

error – Reconstruction scores for each number of sensors in sensor_range.

Return type

numpy array, shape (len(sensor_range),)