Spline (hwx.common.math)#
- class Spline(x, y=None, unitSystem=None, xunits='time', yunits='length')#
Bases:
list
A two element list of (x, y) values.
Spline has methods to convert to/from units, add/insert/delete rows, interpolate, get the derivative and read/write CSV files.
The length of x should be the same as y and at least 4. Also, x should be increasing i.e. x[i] < x[i+1].
# Name
Type
property
property
# Name
Description
addInterpolated
(self, row)Adds a linear interpolated value after the specified row.
appendInterpolated
(self)Appends a linear interpolated value.
Return a copy of self.
deleteRows
(self, *deleteRows)Removes the specified rows.
derivative
(self, order=1, npts=None, type=’akima’, scaled=True)Computes the derivative.
fromBaseUnits
(self)Makes the Spline values work like other Double values.
getFormattedStrings
(self, format=5)Returns the values suitable for display in the gui fields or table cells.
getXFromY
(self, y)Returns a computed x value from the specified y value.
insertInterpolated
(self, row)Adds a linear interpolated value before the specified row.
interpolate
(self, npts=None, type=’akima’, order=0, x=None)Recalculates x and y by interpolation.
isValid
(self)Determines if self can be interpolated and if the derivative can be computed.
prependInterpolated
(self)Adds a linear interpolated value in the beginning.
readCsv
(self, fname, validationMethod=None)Reads a csv file to populate the x/y values.
reflect
(self)Reflects the data across X and Y.
removeNegativeX
(self)Removes x, y pairs with negative x values.
scalex
(self, factor)Mulitplies the x values by a scale factor.
scaley
(self, factor)Mulitplies the y values by a scale factor.
setComponent
(self, row, col, value)Sets a single value.
setValues
(self, x, y=None)Sets the list of x and y values.
shiftx
(self, offset)Shifts the x values by an offset.
shifty
(self, offset)Shifts the y values by an offset.
toBaseUnits
(self)Makes the Spline values work like other Double values.
writeCsv
(self, fname=None)Writes x/y values to a csv file.
Example
import math import matplotlib.pyplot as plt from hwx.common.math import Spline knots = 10 knot_vector = [1.0 * i for i in list(range(knots))] p = [math.exp(-i) / 3 for i in knot_vector] s = Spline(knot_vector, p) fig, ax = plt.subplots() plt.scatter(s.x, s.y) plt.title('Spilne') fig.tight_layout() show(fig)
- exception SplineValueError#
Bases:
ValueError
Exceptions for Spline objects.
- property x#
The x values.
- property y#
The y values.
- setValues(x, y=None)#
Sets the list of x and y values.
- Parameters:
x (list) – The list of x values.
y (list) – The list of y values.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- copy(**kwds)#
Return a copy of self.
It is updated with kwds if specified to support derived classes.
- isValid()#
Determines if self can be interpolated and if the derivative can be computed.
- Returns:
True if it is valid, False otherwise.
- Return type:
bool
- fromBaseUnits()#
Makes the Spline values work like other Double values.
- toBaseUnits()#
Makes the Spline values work like other Double values.
- getFormattedStrings(format=5)#
Returns the values suitable for display in the gui fields or table cells.
- Parameters:
format (int | str) – The format of the values as strings.
- Returns:
The x, y values.
- Return type:
list[list[float], list[float]]
- setComponent(row, col, value)#
Sets a single value.
- Parameters:
row (int) – The row value to set.
col (int) – The column value to set.
value (float) – The value to set.
- Raises:
SplineValueError – If value is not numeric.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- deleteRows(*deleteRows)#
Removes the specified rows.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- prependInterpolated()#
Adds a linear interpolated value in the beginning.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- appendInterpolated()#
Appends a linear interpolated value.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- addInterpolated(row)#
Adds a linear interpolated value after the specified row.
- Parameters:
row (int) – The row where to add the value after.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- insertInterpolated(row)#
Adds a linear interpolated value before the specified row.
- Parameters:
row (int) – The row where to add the value before.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- writeCsv(fname=None)#
Writes x/y values to a csv file.
- Parameters:
fname (str) – The path to the csv file.
- readCsv(fname, validationMethod=None)#
Reads a csv file to populate the x/y values.
- Parameters:
fname (str) – The path to the csv file.
validationMethod (func) – A function used to validate the csv reading.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- scalex(factor)#
Mulitplies the x values by a scale factor.
- Parameters:
factor (float) – The factor to multiply the values with.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- scaley(factor)#
Mulitplies the y values by a scale factor.
- Parameters:
factor (float) – The factor to multiply the values with.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- shiftx(offset)#
Shifts the x values by an offset.
- Parameters:
offset (float) – The offset to shift the values with.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- shifty(offset)#
Shifts the y values by an offset.
- Parameters:
offset (float) – The offset to shift the values with.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- interpolate(npts=None, type='akima', order=0, x=None)#
Recalculates x and y by interpolation.
This is used to get equally spaced values of x, kind can be “akima” or any scipy.interpolate.inter1d.
If x is specified, returns the computed y value.
- Parameters:
npts (int) – Number of samples to generate, if x is None.
type (str) – The type of interpolation. Possible choices are “akima”, “linear”, “cubic”, “zero”, “natural”. If none of these is given then “splev” is used.
order (int) – The order of interpolation.
x (float) – The x value used to return the interpolated y.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- getXFromY(y)#
Returns a computed x value from the specified y value.
This uses linear interpolation since we can not assume the y values are monotonically increasing.
- Parameters:
y (float) – The y value.
- Returns:
The x value.
- Return type:
float
- derivative(order=1, npts=None, type='akima', scaled=True)#
Computes the derivative.
Scale the derivative so it is not too big but can still be viewed.
- Parameters:
order (int) – Order of the derivative.
npts (int) – Number of points to interpolate.
type (str) – Type of Interpolation.
scaled (bool | float) – The factor to scale y values.
- Returns:
A reference to the instance Spline object on which it was called.
- Return type:
- reflect()#
Reflects the data across X and Y.
- removeNegativeX()#
Removes x, y pairs with negative x values.