pysensors.utils package

Module contents

pysensors.utils.constrained_binary_solve(w, psi, quiet=False, fit_intercept=True, normalize=True, precompute='auto')[source]
pysensors.utils.constrained_multiclass_solve(w, psi, alpha=1.0, quiet=False, **lasso_kws)[source]

Solve

\[\begin{split}\text{argmin}_s \|s\|_0 \\ \text{subject to} \|w - \psi s\|_2^2 \leq tol\end{split}\]
pysensors.utils.validate_input(x, sensors=None)[source]

Ensure that x is of compatible type and shape.

Parameters:

x (numpy ndarray, shape [n_features,] or [n_examples, n_features]) – Data to be validated.

classpysensors.utils.BaseConstraint(**kwargs)[source]

Bases: object

Attributes:
  • Keyword Arguments

  • ——————

  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

functional_constraints(idx, info, **kwargs)[source]

Function for evaluating the functional constraints.

Parameters:
  • func (function, a function which is to be evaluated)

  • idx (np.ndarray, ranked list of sensor locations (column indices))

  • info (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

Keyword Arguments:
  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

Returns:

  • g (function, Contains the function defined by the user for the functional)

  • constraint.

get_functionalConstraind_sensors_indices(g)[source]

Function for finding constrained sensor locations on the grid and their ranks

Parameters:
  • senID (np.darray, ranked list of sensor locations (column indices))

  • g (float, constraint evaluation function (negative if violating the constraint))

Returns:

  • idx_constrained (np.darray, shape [No. of constrained locations], array which)

  • contains the constrained – locations of the grid in terms of column indices of basis_matrix.

  • rank (np.darray, shape [No. of constrained locations], array which contains)

  • rank of the constrained sensor locations

get_constraint_indices(all_sensors, info)[source]

A function for computing indices which lie within the region constrained by the user :Attributes: * all_sensors (np.darray,) – A ranked list of all sensor indices computed from just QR optimizer

  • info (pandas.DataFrame/np.ndarray shape [n_features, n_samples],) – Dataframe or Matrix which represent the measurement data.

Returns:

  • idx_const (np.darray, shape [No. of constrained locations],) – array which contains the constrained locations of the grid in terms of column indices of basis_matrix.

  • rank (np.darray, shape [No. of constrained locations],) – array which contains rank of the constrained sensor locations

draw_constraint(plot=None, **kwargs)[source]

Function for drawing the constraint defined by the user

plot_constraint_on_data(plot_type, plot=None, **kwargs)[source]

Function for plotting the user-defined constraint on the data :Attributes: * data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images)

containing measurement data

  • plot_type (string,) – the type of plot used to display the data image : if the data is represented in the fprm of an image scatter: if the data can be represented with a scatter plot contour_map: if the data can be represented in the form of a contour map

  • plot (to plot on an exisiting subplot, pass plot = (fig, ax),) – otherwise leave plot = None

Return type:

A plot of the constraint on top of the measurement data plot.

plot_grid(all_sensors)[source]

Function to plot the grid with data points that signify sensor locations to choose from :Attributes: all_sensors (np.darray,) – A ranked list of all sensor indices computed from just QR optimizer

Return type:

A plot of the user defined grid showing all possible sensor locations

plot_selected_sensors(sensors, all_sensors, color_constrained='red', color_unconstrained='green')[source]

Function to plot the sensor locations choosen during the optimization procedure. This function plots near-optimal sensors which are unconstrained sensor locations choosen by QR in the user defined color_unconstrained/green and sensors that are choosen through constraining certain regions of the grid in the under defined color_constrained/red. :Attributes: * sensors (np.darray,) – A ranked list of all sensor indices computed from QR/GQR/CCQR optimizer

  • all_sensors (np.darray,) – A ranked list of all sensor indices computed from just QR optimizer

  • color_constrained (string,) – The color the sensors that were selected due to the applied constraints should be plotted in

  • color_unconstrained (string,) – The color the sensors that were a part of the near-optimal sensors choosen through unconstrained QR optimizer should be plotted in

Return type:

A plot of the user defined grid showing chosen sensor locations

sensors_dataframe(sensors)[source]

Function to form a dataframe of the sensor index along with it’s coordinate (X,Y,Z) positions :Attributes: sensors (np.darray,) – A ranked list of all sensor indices choosen from QR/CCQR/GQR optimizer

Return type:

A dataframe of the sensor locations choosen

annotate_sensors(sensors, all_sensors, color_constrained='red', color_unconstrained='green')[source]

Function to annotate the sensor location on the grid while also plotting the sensor location

Attributes:
  • sensors (np.darray,) – A ranked list of all sensor indices choosen from QR/CCQR/GQR optimizer

  • all_sensors (np.darray,) – A ranked list of all sensor indices computed from just QR optimizer

  • color_constrained (string,) – The color the sensors that were selected due to the applied constraints should be plotted in

  • color_unconstrained (string,) – The color the sensors that were a part of the near-optimal sensors choosen through unconstrained QR optimizer should be plotted in

Return type:

Annotation of sensor rank near the choosen sensor locations

classpysensors.utils.Circle(center_x, center_y, radius, loc='in', **kwargs)[source]

Bases: BaseConstraint

Attributes:
  • Keyword Arguments

  • ——————

  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

draw(ax, **kwargs)[source]

Function to plot a circle based on user-defined coordinates :Attributes: ax (axis on which the constraint circle should be plotted)

constraint_function(coords)[source]

Function to compute whether a certain point on the grid lies inside/outside the defined constrained region :Attributes: * x (float,) – x coordinate of point on the grid being evaluated to check whether

it lies inside or outside the constrained region

  • y (float,) – y coordinate of point on the grid being evaluated to check whether it lies inside or outside the constrained region

classpysensors.utils.Parabola(h, k, a, loc, **kwargs)[source]

Bases: BaseConstraint

Attributes:
  • Keyword Arguments

  • ——————

  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

draw(ax, **kwargs)[source]

Function to plot a parabola based on user-defined coordinates :Attributes: ax (axis on which the constraint parabola should be plotted)

constraint_function(coords)[source]

Function to compute whether a certain point on the grid lies inside/outside the defined constrained region :Attributes: * x (float,) – x coordinate of point on the grid being evaluated to check whether it lies

inside or outside the constrained region

  • y (float,) – y coordinate of point on the grid being evaluated to check whether it lies inside or outside the constrained region

classpysensors.utils.Polygon(xy_coords, loc='in', **kwargs)[source]

Bases: BaseConstraint

Attributes:
  • Keyword Arguments

  • ——————

  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

draw(ax, **kwargs)[source]

Function to plot a polygon based on user-defined coordinates :Attributes: ax (axis on which the constraint polygon should be plotted)

constraint_function(coords)[source]

Function to compute whether a certain point on the grid lies inside/outside the defined constrained region

Attributes:

coords (list or tuple) – [x, y] coordinates of point on the grid being evaluated to check whether it lies inside or outside the constrained region

Returns:

True if point satisfies the constraint (inside for "in", outside for "out"), False otherwise

Return type:

bool

classpysensors.utils.Ellipse(center_x, center_y, width, height, angle=0.0, loc='in', **kwargs)[source]

Bases: BaseConstraint

Attributes:
  • Keyword Arguments

  • ——————

  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • data (pandas.DataFrame/np.darray [n_samples, n_features],) – dataframe (used for scatter and contour plots) or matrix (used for images) containing measurement data

draw(ax, **kwargs)[source]

Function to plot an ellipse based on user-defined coordinates :Attributes: ax (axis on which the constraint ellipse should be plotted)

constraint_function(coords)[source]

Function to compute whether a certain point on the grid lies inside/outside the defined constrained region :Attributes: * x (float,) – x coordinate of point on the grid being evaluated to check whether it lies

inside or outside the constrained region

  • y (float,) – y coordinate of point on the grid being evaluated to check whether it lies inside or outside the constrained region

pysensors.utils.get_coordinates_from_indices(idx, info, **kwargs)[source]

Function for obtaining the coordinates on a grid from column indices

Parameters:
  • idx (int, sensor ID)

  • info (pandas.DataFrame/np.ndarray shape [n_features, n_samples], Dataframe or)

  • data. (Matrix which represent the measurement)

Keyword Arguments:
  • X_axis (string,) – Name of the column in dataframe to be plotted on the X axis.

  • Y-axis (string,) – Name of the column in dataframe to be plotted on the Y axis.

  • Field (string,) – Name of the column in dataframe to be plotted as a contour map.

  • Returns – (x,y) : tuple, The coordinates on the grid of each sensor.

pysensors.utils.get_indices_from_coordinates(coordinates, shape)[source]

Function for obtaining the indices of columns/sensors from coordinates on a grid when data is in the form of a matrix

Parameters:
  • coordinates (tuple of array_like , (x,y) pair coordinates of sensor locations on)

  • grid (the)

  • shape (tuple of ints, Shape of the matrix fed as data to the algorithm)

Returns:

  • np.ravel_multi_index(coordinates,shape,order=’F’) (np.ndarray, The indices of the)

  • sensors.

pysensors.utils.exact_n(dlens, piv, j, **kwargs)[source]

Function for mapping constrained sensor locations with the QR procedure.

Parameters:
  • lin_idx (np.ndarray, shape [No. of constrained locations]) – Array which contains the constrained locations of the grid in terms of column indices of basis_matrix.

  • dlens (np.ndarray, shape [n_features - j]) – Array which contains the norm of columns of basis matrix.

  • piv (np.ndarray, shape [n_features]) – Ranked list of sensor locations.

  • n_const_sensors (int,) – Number of sensors to be placed in the constrained area.

  • j (int,) – current sensor to be placed in the QR/GQR algorithm.

Returns:

dlens

Return type:

np.darray, shape [Variable based on j] with constraints mapped into it.

pysensors.utils.max_n(dlens, piv, j, **kwargs)[source]

Function for mapping constrained sensor locations with the QR procedure (Optimally).

Parameters:
  • lin_idx (np.ndarray, shape [No. of constrained locations]) – Array which contains the constrained locations of the grid in terms of column indices of basis_matrix.

  • dlens (np.ndarray, shape [Variable based on j]) – Array which contains the norm of columns of basis matrix.

  • piv (np.ndarray, shape [n_features]) – Ranked list of sensor locations.

  • j (int,) – Iterative variable in the QR algorithm.

  • const_sensors (int,) – Number of sensors to be placed in the constrained area.

  • all_sensors (np.ndarray, shape [n_features]) – Ranked list of sensor locations.

  • n_sensors (integer,) – Total number of sensors

Returns:

dlens

Return type:

np.darray, shape [Variable based on j] with constraints mapped into it.

pysensors.utils.predetermined(dlens, piv, j, **kwargs)[source]

Function for mapping constrained sensor locations with the QR procedure.

Parameters:
  • lin_idx (np.ndarray, shape [No. of constrained locations], array which contains) – the constrained locationsof the grid in terms of column indices of basis_matrix.

  • dlens (np.ndarray, shape [Variable based on j], array which contains the norm of)

  • matrix. (columns of basis)

  • piv (np.ndarray, shape [n_features], ranked list of sensor locations.)

  • n_const_sensors (int, number of sensors to be placed in the constrained area.)

  • j (int, iterative variable in the QR algorithm.)

Returns:

dlens

Return type:

np.darray, shape [Variable based on j] with constraints mapped into it.

pysensors.utils.determinant(top_sensors, n_features, basis_matrix)[source]

Function for calculating |C.T phi.T C phi|.

Parameters:
  • top_sensors (np.darray,) – Column indices of choosen sensor locations

  • n_features (int,) – No. of features of dataset

  • basis_matrix (np.darray,) – The basis matrix calculated by model.basis_matrix_

Returns:

optimality – The dterminant value obtained.

Return type:

Float,

pysensors.utils.relative_reconstruction_error(data, prediction)[source]

Function for calculating relative error between actual data and the reconstruction

Parameters
data: np.darray,

The actual data from the dataset evaluated

predictionnp.darray,

The predicted values from model.predict(X[:,top_sensors])

error_valFloat,

The relative error calculated.