open_cp.predictors¶
predictors¶
Contains base classes and utility functions for classes which make predictions, and classes which encapsulate a given prediction.
-
class
open_cp.predictors.
ContinuousPrediction
(cell_width=50, cell_height=50, xoffset=0, yoffset=0, samples=50)¶ Bases:
object
A prediction which allows the “risk” to be calculated at any point in a continuous fashion. Allows monte-carlo sampling to produce a grid risk.
Parameters: - cell_width – Width of cells to use in producing a grid risk.
- cell_height – Height of cells to use in producing a grid risk.
- xoffset – The x coordinate of the start of the grid.
- yoffset – The y coordinate of the start of the grid.
- samples – The number of samples to use when computing the risk in a grid cell.
-
grid_risk
(gx, gy)¶ Return an estimate of the average risk in the grid cell
-
rebase
(cell_width, cell_height, xoffset, yoffset, samples=50)¶ Returns a new instance using the same risk but with a different grid size and offset
-
risk
(x, y)¶ Return the risk at (a) coordinate(s).
Parameters: - x – The x coordinate to evaluate the risk at. May be a scalar or a one-dimensional numpy array.
- y – The y coordinate to evaluate the risk at. Should match x in being a scalar or a one-dimensional numpy array.
Returns: A scalar or numpy array as appropriate.
-
to_kernel
()¶ Returns a callable object which when called at point gives the risk at (point[0], point[1]). point may be an array.
-
class
open_cp.predictors.
DataTrainer
¶ Bases:
object
Base class for most “trainers”: classes which take data and “train” themselves (fit a statistical model, etc.) to the data. Can also be used as a base for classes which can directly return a “prediction”.
-
data
¶ An instance of
TimedPoints
giving the data to be trained on.
-
-
class
open_cp.predictors.
GridPrediction
(xsize, ysize, xoffset=0, yoffset=0)¶ Bases:
object
A prediction based on a grid. The risk is always computed by finding the grid cell the coordinates contained, and then deferring to the abstract grid_risk method.
Parameters: - xsize – The width of the grid cells.
- ysize – The height of the grid cells.
- xoffset – How much to offset the input x coordinate by; default 0.
- yoffset – How much to offset the input y coordinate by; default 0.
-
grid_risk
(gridx, gridy)¶
-
risk
(x, y)¶ The risk at coordinate (x,y).
-
xsize
¶ The width of each cell
-
ysize
¶ The height of each cell
-
class
open_cp.predictors.
GridPredictionArray
(xsize, ysize, matrix, xoffset=0, yoffset=0)¶ Bases:
open_cp.predictors.GridPrediction
A
GridPrediction
backed by a numpy array (or other two-dimensional list-like object).Parameters: - xsize – The width of the grid cells.
- ysize – The height of the grid cells.
- matrix – A two dimensional numpy array (or other object with a shape attribute and allowing indexing as matrix[y][x]).
- xoffset – How much to offset the input x coordinate by; default 0.
- yoffset – How much to offset the input y coordinate by; default 0.
-
static
from_continuous_prediction
(prediction, width, height)¶ Construct an instance from an instance of
ContinuousPrediction
using the grid size and offset specified in that instance. This is more efficient as we sample each grid cell once and then store the result.Parameters: - prediction – An instance of ContinuousPrediction to sample from
- width – Width of the grid, in number of cells
- height – Height of the grid, in number of cells
-
static
from_continuous_prediction_region
(prediction, region, cell_width, cell_height=None)¶ Construct an instance from an instance of
ContinuousPrediction
using the region and passed cell sizes.Parameters: - prediction – An instance of ContinuousPrediction to sample from
- region – The
RectangularRegion
the grid - cell_width – Width of each cell in the resulting grid
- cell_height – Optional; height of each cell in the resulting grid; defaults to cell_width
-
grid_risk
(gx, gy)¶ Find the risk in a grid cell.
Parameters: - gx – x coordinate of the cell
- gy – y coordinate of the cell
Returns: The risk in the cell, or 0 if the cell is outside the range of the data we have.
-
intensity_matrix
¶ Get the matrix containing data which we use
-
mesh_data
()¶ Returns a pair (xcoords, ycoords) which when paired with
intensity_matrix()
is suitable for passing to matplotlib.pcolor or pcolormesh. That is, intensity_matrix[i][j] is the risk intensity in the rectangular cell with diagonally opposite vertices (xcoords[j], ycoords[i]), (xcoords[j+1], ycoords[i+1]).
-
percentile_matrix
()¶ Returns a matrix of the same shape as
intensity_matrix()
but with float values giving the percentile of risk, normalised to [0,1]. So the cell with the highest risk is assigned 1.0. Ties are rounded up, so if three cells share the highest risk, they are all assigned 1.0.
-
class
open_cp.predictors.
KernelRiskPredictor
(kernel, **kwargs)¶ Bases:
open_cp.predictors.ContinuousPrediction
Wraps a kernel object so as to make a :class ContinuousPrediction: instance
Parameters: - kernel – A callable object with signature kernel(points) where points may be an array of size 2, for a single point, or an array of shape (2,N) for N points to be computed at once.
- kwards – Any constructor arguments which
ContinuousPrediction
takes.
-
risk
(x, y)¶ The risk given by the kernel.
-
open_cp.predictors.
grid_prediction_from_kernel
(kernel, region, grid_size)¶ Utility function to convert a space kernel into a grid based prediction.
Parameters: - kernel – A kernel object taking an array of shape (2,N) of N lots of spatial coordinates, and returning an array of shape (N).
- region – An instance of :class RectangularRegion: giving the region to use.
- grid_size – The size of grid to use.
Returns: An instance of :class GridPredictionArray: