vectorinterpolators

Contents

vectorinterpolators#

This modules provides interpolator objects to find interpolated values in lists. - x generally describes the index value lists - y descreibes the corresponding values

Example usage#

https://mybinder.org/badge_logo.svg
 # import this module
 import themachinethatgoesping.tools.vectorinterpolators as vip

 # create value lists
timestamps = [100, 150, 300, 400, 950, 1000]
values     = [10, 50, 30, 500, -30,-20]

# create interpolator object (here a linear interpolator)
linear = vip.LinearInterpolator(X=timestamps, Y=values)

# find interpolated y value for a given x value (e.g. a random timestamp)
y_value = lip(target_x=125)
print(y_value) #<< 30.0

# find extrapolated y value for value outside timestamps boundary
y_value = linear(target_x=50)
print(y_value) #<< -30.0
../../_images/vectorinterpolators_1.png

Types#

class t_extr_mode(*values)#

extrapolation mode type.

extrapolate = 0#
nearest = 2#
fail = 1#
nan = 1#

Class: AkimaInterpolator#

class AkimaInterpolator(*args, **kwargs)#

Interpolator class to perform a (modified) akima interpolation. Uses boost makima interpolator. Note: this interpolator acts as linear interpolator if less than 4 values are stored.

Template Args:

XYType:: type of the x and y values (must be floating point))

__init__(self, X: Sequence[float] = [], Y: Sequence[float] = [], extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode = t_extr_mode.extrapolate) None#

Construct a new Interpolator object from a vector of pairs usage: interpolated_y_value = interpolator.interpolate(x_value)

Parameters:
  • X – X vector; must be unique and sorted in ascending order. same size as Y!

  • Y – Y vector; must be unique and sorted in ascending order. same size as X!

  • extrapolation_mode – :option o_extr_mode <themachinethatgoesping.to ols.vectorinterpolators.t_extr_mode>` object that describes the extrapolation mode

__call__(self, target_x: float) float#
__call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) numpy.ndarray[dtype=float64, order='C']

Overloaded function.

  1. __call__(self, target_x: float) -> float

get the interpolated y value for given x target

Parameters:

target_x – find the corresponding y value for this x value

Returns:

corresponding y value

  1. __call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) -> numpy.ndarray[dtype=float64, order='C']

get interpolated y values for given x targets (vectorized call)

Parameters:
  • targets_x – vector of x values. For each of these values find the corresponding y value

  • mp_cores – Number of OpenMP threads to use for parallelization. Default is 1 (no parallelism). Set to higher values to enable parallel interpolation.

Returns:

std::vector<YType> corresponding y values

append(self, x: float, y: float) None#

append an x- and the corresponding y value to the interpolator data. Exception: raises domain error, strong exception guarantee

Parameters:
  • x – value, must be > than all existing x values

  • y – corresponding y value

copy(self) AkimaInterpolator#

return a copy using the c++ default copy constructor

extend(self, X: Sequence[float], Y: Sequence[float]) None#

append x and y value lists to the interpolator data (vectorized call) Exception: raises domain error, strong exception guarantee

Parameters:
  • X – list of x values. Must be sorted in ascending order. All x values must be larger than the largest x value in the interpolator data.

  • Y – list of corresponding Y values. Must be same size as X

from_binary = <nanobind.nb_func object>#
get_data_X(self) list[float]#

return the x component of the internal data vector

Returns:

std::vector<XType>

get_data_Y(self) list[float]#

return the y component of the internal data vector

Returns:

std::vector<YType>

get_extrapolation_mode(self) themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode#

Get the currently set extrapolation mode

Returns:

o_extr_mode

info_string(self, float_precision: int = 3, superscript_exponents: bool = True) str#

Return object information as string

print(self, float_precision: int = 3, superscript_exponents: bool = True) None#

Print object information

set_data_XY(self, X: Sequence[float], Y: Sequence[float]) None#

change the input data to these X and Y vectors

Parameters:
  • X: – x vector (must be same size: order)

  • ascending (must be sorted in) – order)

  • Y: (y vector: must be same size) – y vector:

set_extrapolation_mode(self, extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode) None#

Set the extrapolation mode

Parameters:

extrapolation_modet_extr_mode object (enumerator) that describes the extrapolation mode

to_binary(self, resize_buffer: bool = True) bytes#

convert object to bytearray

Class: LinearInterpolator#

class LinearInterpolator(*args, **kwargs)#

Find linear interpolated values within vector data

Template Args:

XType:: type of the x values (must be floating point) YType:: type of the y values (must be floating point)

__init__(self, X: Sequence[float] = [], Y: Sequence[float] = [], extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode = t_extr_mode.extrapolate) None#

Construct a new Interpolator object from a vector of pairs usage: interpolated_y_value = interpolator.interpolate(x_value)

Parameters:
  • X – X vector; must be unique and sorted in ascending order. same size as Y!

  • Y – Y vector; must be unique and sorted in ascending order. same size as X!

  • extrapolation_mode – :option o_extr_mode <themachinethatgoesping.to ols.vectorinterpolators.t_extr_mode>` object that describes the extrapolation mode

__call__(self, target_x: float) float#
__call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) numpy.ndarray[dtype=float64, order='C']

Overloaded function.

  1. __call__(self, target_x: float) -> float

get the interpolated y value for given x target

Parameters:

target_x – find the corresponding y value for this x value

Returns:

corresponding y value

  1. __call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) -> numpy.ndarray[dtype=float64, order='C']

get interpolated y values for given x targets (vectorized call)

Parameters:
  • targets_x – vector of x values. For each of these values find the corresponding y value

  • mp_cores – Number of OpenMP threads to use for parallelization. Default is 1 (no parallelism). Set to higher values to enable parallel interpolation.

Returns:

std::vector<YType> corresponding y values

append(self, x: float, y: float) None#
copy(self) LinearInterpolator#

return a copy using the c++ default copy constructor

extend(self, X: Sequence[float], Y: Sequence[float]) None#
from_binary = <nanobind.nb_func object>#
get_data_X(self) list[float]#

return the x component of the internal data vector

Returns:

std::vector<XType>

get_data_Y(self) list[float]#

return the y component of the internal data vector

Returns:

std::vector<YType>

get_extrapolation_mode(self) themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode#

Get the currently set extrapolation mode

Returns:

o_extr_mode

info_string(self, float_precision: int = 3, superscript_exponents: bool = True) str#

Return object information as string

print(self, float_precision: int = 3, superscript_exponents: bool = True) None#

Print object information

set_data_XY(self, X: Sequence[float], Y: Sequence[float]) None#

change the input data to these X and Y vectors

Parameters:
  • X: – x vector (must be same size: order)

  • ascending (must be sorted in) – order)

  • Y: (y vector: must be same size) – y vector:

set_extrapolation_mode(self, extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode) None#

Set the extrapolation mode

Parameters:

extrapolation_modet_extr_mode object (enumerator) that describes the extrapolation mode

to_binary(self, resize_buffer: bool = True) bytes#

convert object to bytearray

Class: NearestInterpolator#

class NearestInterpolator(*args, **kwargs)#

Interpolator class to find nearest neighbors within vector data

Template Args:

XType:: type of the x values (must be floating point) YType:: type of the y values (must be floating point)

__init__(self, X: Sequence[float] = [], Y: Sequence[float] = [], extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode = t_extr_mode.extrapolate) None#

Construct a new Interpolator object from a vector of pairs usage: interpolated_y_value = interpolator.interpolate(x_value)

Parameters:
  • X – X vector; must be unique and sorted in ascending order. same size as Y!

  • Y – Y vector; must be unique and sorted in ascending order. same size as X!

  • extrapolation_mode – :option o_extr_mode <themachinethatgoesping.to ols.vectorinterpolators.t_extr_mode>` object that describes the extrapolation mode

__call__(self, target_x: float) float#
__call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) numpy.ndarray[dtype=float64, order='C']

Overloaded function.

  1. __call__(self, target_x: float) -> float

get the interpolated y value for given x target

Parameters:

target_x – find the corresponding y value for this x value

Returns:

corresponding y value

  1. __call__(self, targets_x: numpy.ndarray[dtype=float64, shape=(*), order='C'], mp_cores: int = 1) -> numpy.ndarray[dtype=float64, order='C']

get interpolated y values for given x targets (vectorized call)

Parameters:
  • targets_x – vector of x values. For each of these values find the corresponding y value

  • mp_cores – Number of OpenMP threads to use for parallelization. Default is 1 (no parallelism). Set to higher values to enable parallel interpolation.

Returns:

std::vector<YType> corresponding y values

append(self, x: float, y: float) None#
copy(self) NearestInterpolator#

return a copy using the c++ default copy constructor

extend(self, X: Sequence[float], Y: Sequence[float]) None#
from_binary = <nanobind.nb_func object>#
get_data_X(self) list[float]#

return the x component of the internal data vector

Returns:

std::vector<XType>

get_data_Y(self) list[float]#

return the y component of the internal data vector

Returns:

std::vector<YType>

get_extrapolation_mode(self) themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode#

Get the currently set extrapolation mode

Returns:

o_extr_mode

info_string(self, float_precision: int = 3, superscript_exponents: bool = True) str#

Return object information as string

print(self, float_precision: int = 3, superscript_exponents: bool = True) None#

Print object information

set_data_XY(self, X: Sequence[float], Y: Sequence[float]) None#

change the input data to these X and Y vectors

Parameters:
  • X: – x vector (must be same size: order)

  • ascending (must be sorted in) – order)

  • Y: (y vector: must be same size) – y vector:

set_extrapolation_mode(self, extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode) None#

Set the extrapolation mode

Parameters:

extrapolation_modet_extr_mode object (enumerator) that describes the extrapolation mode

to_binary(self, resize_buffer: bool = True) bytes#

convert object to bytearray

Class: SlerpInterpolator#

Example usage#

# import this module
import themachinethatgoesping.tools.vectorinterpolators as vip

# create value lists
timestamps = [100, 150, 1000] #x list, must be sorted, no duplicates
yaw       = [10, 50, -20]    #yaw values (degrees)
pitch     = [10, 50, -20]    #yaw values (degrees)
roll      = [10, 50, -20]    #yaw values (degrees)

# create interpolator object (here a linear interpolator)
slerp = vip.SlerpInterpolator(X=timestamps, Yaw=yaw, Pitch = pitch, Roll = roll)

# find interpolated y value for a given x value (e.g. a random timestamp)
ypr_value = slerp(target_x=125)
print(ypr_value) #<< [23.65708869335217, 32.74209962001251, 23.657088693352158]

# find extrapolated y value for value outside timestamps boundary
ypr_value = slerp(target_x=50)
print(ypr_value) #<< [355.97195450327666, -40.17949250447276, -4.0280454967233394]

Class#

class SlerpInterpolator(*args, **kwargs)#

Class that implements a slerp interpolation for vectors. Data is internally represented in quaternions using lib eigen. Interfaces to represent the data in yaw, pitch, roll angles are provided. the __call__ equivalent to get interpolated yaw pitch roll is the ypr function

Template Args:

XType:: type of the x values (must be floating point) YType:: floating point type of the y quaternion values (must be

floating point)

__init__(self, X: Sequence[float] = [], Yaw: Sequence[float] = [], Pitch: Sequence[float] = [], Roll: Sequence[float] = [], input_in_degrees: bool = True, extrapolation_mode: t_extr_mode = t_extr_mode.extrapolate) None#

Construct a new Slerp Interpolator object using vectors of x, yaw, pitch and roll

Parameters:
  • X – vector; must be unique and sorted in ascending order

  • yaw – vector with yaw data (rotation around z axis). Must be same size as X!

  • pitch – vector with pitch data (rotation around y axis). Must be same size as X!

  • roll – vector with roll data (rotation around x axis). Must be same size as X!

  • input_in_degrees – if true (default) yaw,pitch and roll are in °, otherwise [rad]

  • extrapolation_modet_extr_mode object that describes the extrapolation mode

__call__(self, target_x: float, output_in_degrees: bool = True) list[float]#
__call__(self, targets_x: Sequence[float], output_in_degrees: bool = True) list[list[float]]

Overloaded function.

  1. __call__(self, target_x: float, output_in_degrees: bool = True) -> list[float]

get the interpolated yaw, pitch and roll values for given x target

Parameters:
  • target_x – find the corresponding y value for this x value

  • output_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

Returns:

corresponding y value

  1. __call__(self, targets_x: collections.abc.Sequence[float], output_in_degrees: bool = True) -> list[list[float]]

get the interpolated yaw, pitch and roll values for given x target (vectorized call)

Parameters:
  • targets_x – vector of x values. For each of these values find the corrsponding yaw, pitch and roll value

  • output_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

Returns:

corresponding y value

append(self, x: float, yaw: float, pitch: float, roll: float, input_in_degrees: bool = True) None#
append(self, x: float, ypr: Sequence[float], input_in_degrees: bool = True) None

Overloaded function.

  1. append(self, x: float, yaw: float, pitch: float, roll: float, input_in_degrees: bool = True) -> None

append an x, yaw, pitch, roll data point

Parameters:
  • X – must be larger than all internal data points

  • yaw – rotation around z axis

  • pitch – rotation around y axis

  • roll – rotation around x axis

  • input_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

  1. append(self, x: float, ypr: collections.abc.Sequence[float], input_in_degrees: bool = True) -> None

append an x, yaw, pitch, roll data point

Parameters:
  • X – must be larger than all internal data points

  • ypr – array with one yaw, pitch and roll data point

  • input_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

copy(self) SlerpInterpolator#

return a copy using the c++ default copy constructor

extend(self, X: Sequence[float], Yaw: Sequence[float], Pitch: Sequence[float], Roll: Sequence[float], input_in_degrees: bool = True) None#
extend(self, X: Sequence[float], YPR: Sequence[Sequence[float]], input_in_degrees: bool = True) None

Overloaded function.

  1. extend(self, X: collections.abc.Sequence[float], Yaw: collections.abc.Sequence[float], Pitch: collections.abc.Sequence[float], Roll: collections.abc.Sequence[float], input_in_degrees: bool = True) -> None

append data with lists of x, yaw, pitch, roll data (vectorized call)

Parameters:
  • X – vector; must be unique and sorted in ascending order

  • yaw – vector with yaw data (rotation around z axis). Must be same size as X!

  • pitch – vector with pitch data (rotation around y axis). Must be same size as X!

  • roll – vector with roll data (rotation around x axis). Must be same size as X!

  • input_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

  1. extend(self, X: collections.abc.Sequence[float], YPR: collections.abc.Sequence[collections.abc.Sequence[float]], input_in_degrees: bool = True) -> None

append data with list of x, yaw, pitch, roll data (vectorized call)

Parameters:
  • X – vector; must be unique and sorted in ascending order

  • ypr – vector with yaw, pitch and roll data points. Must be same size as X!

  • input_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

from_binary = <nanobind.nb_func object>#
get_data_X(self) list[float]#

return the x component of the internal data vector

Returns:

std::vector<XType>

get_data_YPR(self, output_in_degrees: bool = True) list[list[float]]#

return the internal yrp data vector

Parameters:

output_in_degrees – convert yaw, pitch and roll to degrees (default = True)

Returns:

std::vector<std::array<3, YType>> YPR

get_extrapolation_mode(self) themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode#

Get the currently set extrapolation mode

Returns:

o_extr_mode

info_string(self, float_precision: int = 3, superscript_exponents: bool = True) str#

Return object information as string

print(self, float_precision: int = 3, superscript_exponents: bool = True) None#

Print object information

set_data_XYPR(self, X: Sequence[float], Yaw: Sequence[float], Pitch: Sequence[float], Roll: Sequence[float], input_in_degrees: bool = True) None#
set_data_XYPR(self, X: Sequence[float], YPR: Sequence[Sequence[float]], input_in_degrees: bool = True) None

change the input data to these X, yaw, pitch, roll vectors (will be converted to quaternion)

Parameters:
  • X – vector; must be unique and sorted in ascending order

  • yaw – vector with yaw data (rotation around z axis). Must be same size as X!

  • pitch – vector with pitch data (rotation around y axis). Must be same size as X!

  • roll – vector with roll data (rotation around x axis). Must be same size as X!

  • input_in_degrees – if true, yaw pitch and roll input values are in ° otherwise rad

set_extrapolation_mode(self, extrapolation_mode: themachinethatgoesping.tools_nanopy.vectorinterpolators.o_extr_mode) None#

Set the extrapolation mode

Parameters:

extrapolation_modet_extr_mode object (enumerator) that describes the extrapolation mode

to_binary(self, resize_buffer: bool = True) bytes#

convert object to bytearray