Karana.KUtils#

Simulation utilities such as data logging and data plotting.

Submodules#

Classes#

H5Writer

Logs data to an HDF5 file. The data to be logged is configured

PacketTableConfig

Define the columns and associated update functions for a PacketTable.

PlotData

SinglePlotData

Package Contents#

class Karana.KUtils.H5Writer(filename: str)#

Bases: Karana.Core.Base

Logs data to an HDF5 file. The data to be logged is configured

via `PacketTableConfig`s.

static create(filename: str) H5Writer#

Create an H5Writer class.

Parameters:

filename – The name of the h5 file to create.

Returns:

A ks_ptr to the newly created instance of H5Writer.

activateTable(name: str) None#

Activate the table with the given name.

Parameters:

name – The name of the table to activate.

createTable(config: PacketTableConfig) None#

Create a packet table to log.

This creates the table and adds it to the active tables. Activate tables will have a data entry added whenever log is called. To deactivate a table use the deactivateTable method. The name of the tame will be the same as the name of the PacketTableConfig.

Parameters:

config – The config that specifies the data of the packet table.

deactivateTable(name: str) None#

Deactivate the table with the given name.

Parameters:

name – The name of the table to deactivate.

getActiveTableNames() list[str]#

Get a vector of the active table names.

Returns:

A vector of the active table names.

getTableNames() list[str]#

Get a vector of all the table names.

Returns:

A vector of the table names.

log() None#

Log data for all the active tables.

logTable(name: str) None#

Create a log entry for just the given table.

This will log the table regardless of whether it is active or not.

Parameters:

name – The name of the table to log.

class Karana.KUtils.PacketTableConfig(name: str)#

Bases: Karana.Core.LockingBase

Define the columns and associated update functions for a PacketTable.

The PacketTable is the table that stores data in the H5 log file.

static create(name: str) PacketTableConfig#

Create a packet table config.

Parameters:

name – Name of the table. A / will be interpreted as a new group. Groups will be added as needed.

Returns:

A ks_ptr to the newly created instance of PacketTableConfig.

addData(name: str, f: collections.abc.Callable[[], Annotated[numpy.typing.NDArray[numpy.float64], [m, 1]]], vector_size: SupportsInt, as_scalars: bool = False) None#
addData(name: str, f: collections.abc.Callable[[], Annotated[numpy.typing.NDArray[numpy.float64], [m, n]]], matrix_rows: SupportsInt, matrix_cols: SupportsInt, as_scalars: bool = False) None

Add data to the table via function. :param name: The name of the column in the table. :param f: The function that produces the data (dynamic matrix). This

matrix must always be the same size.

Parameters:
  • matrix_rows – The number of rows in the matrix that the function produces. This matrix must always be the same size.

  • matrix_cols – The number of columns in the matrix that the function produces. This matrix must always be the same size.

  • as_scalars – Rather than using one column for the matrix, use multiple. This logs each matrix component as a scalar. The column names will be appended with a _X_Y where “X” and “Y” are integers that correspond with the row and index of the element in the matrix.

addDataFloat(name: str, f: collections.abc.Callable[[], float]) None#

Add data to the table via function. :param name: The name of the column in the table. :param f: The function that produces the data.

addDataInt(name: str, f: collections.abc.Callable[[], int]) None#

Add data to the table via function. :param name: The name of the column in the table. :param f: The function that produces the data.

fillBuf() None#

Fill the buffer by calling all the functions.

class Karana.KUtils.PlotData(plot_data: collections.abc.Sequence[SinglePlotData], host: str, port: str, target: str)#

Bases: Karana.Core.Base

static create(plot_data: collections.abc.Sequence[SinglePlotData], host: str, port: str, target: str) PlotData#

Create an instance of PlotData.

Parameters:
  • plot_data – The data for the plots.

  • host – The host for the websocket.

  • port – The port for the websocket.

  • target – The target for the websocket.

Returns:

A pointer to the newly created PlotData.

addPlot(plot_data: SinglePlotData) None#

Add a plot.

Parameters:

plot_data – The data for the plot to add.

removePlot(index: SupportsInt) None#

Remove the plot at index.

Parameters:

index – The index of the plot to remove.

sendWebsocketMessage(msg: str) None#

Send a message over the websocket. This should be used with caution and only if you know exactly what type of messages are allowed to be sent. :param msg: The message to send.

shutdown() None#

Shutdown the websocket client.

update() None#

Get data from the SinglePlotData and send it via a websocket to the server.

property plot_fns: list[SinglePlotData]#

The data for the plots.

class Karana.KUtils.SinglePlotData(title: str, x_data_name: str, x_data: collections.abc.Callable[[], float], y_data: dict[str, collections.abc.Callable[[], float | Annotated[numpy.typing.NDArray[numpy.float64], [m, 1]]]])#
class Karana.KUtils.SinglePlotData(title: str, x_data_name: str, x_data: collections.abc.Callable[[], float], y_data: collections.abc.Mapping[str, collections.abc.Callable[[], float | Annotated[numpy.typing.NDArray[numpy.float64], [3, 1]] | Annotated[numpy.typing.NDArray[numpy.float64], [m, 1]]]])
property title: str#

The title of the plot

property x_data: collections.abc.Callable[[], float]#

The function that produces the x_data

property x_data_name: str#

The name of the x_data

property y_data_fns: list[collections.abc.Callable[[], float | Annotated[numpy.typing.NDArray[numpy.float64], [3, 1]] | Annotated[numpy.typing.NDArray[numpy.float64], [m, 1]]]]#

The y_data functions

property y_data_names: list[str]#

The y_data names