opynsim#
Classes
|
Represents data as a table containing rows and columns, with metadata ( |
|
Integrates the forward dynamics of a |
|
Settings for a forward integrator (e.g. as used by |
A compiled model of a physics system. |
|
|
A high-level specification for a |
Represents a single state of a |
|
|
Represents a stage of state realization (computation). |
|
Represents a sequence of |
|
Represents an immutable, cheap-to-use, readable symbol. |
- class opynsim.DataFrame(*args, **kwargs)#
Bases:
objectRepresents data as a table containing rows and columns, with metadata (
attrs()).DataFrameonly supports storingfloat64data. It enforces no constraints on the number/order/labels of columns, or the values it contains. However, other APIs in OPynSim may enforce stronger constraints (e.g. “TheDataFramesupplied to this solver must contain a series namedtimewith strongly monotonically increasing values”). In general, those constraints are checked at runtime and produce a validation error. Callers are expected to handle filtering/cleaning/fixing theirDataFrames accordingly.DataFrame’s functionality is limited to being just good enough for common OPynSim-related tasks. However, it the Arrow PyCapsule Protocol, which lets you import/exportDataFrames to more-comprehensive libraries (e.g.DataFrame.from_arrow(),pandas.DataFrame.from_arrow,polars.from_arrow,pyarrow.table.__init__). If you want to do some fancy data manipulation, we recommend using one of those libraries.- __arrow_c_stream__(self, requested_schema: typing_extensions.CapsuleType | None = None) typing_extensions.CapsuleType#
Exports this
DataFrameas anArrowSchema(see: Apache Arrow PyCapsule Interface).This is a low-level interface that other dataframe libraries (e.g. Pandas and Polars) can use to natively read
DataFrames. For example,polars.DataFrame.__init__acceptsDataFrames because it implements this method, as doespandas.DataFrame.from_arrow.Note: This method also exports metadata (
attrs()), but third-party libraries handle metadata inconsistently. At time of writing, PyArrow encodes metadata into its table schema, but Pandas and Polars drop it. Therefore, we recommend that callers propagate metadata manually, or adjust theirDataFrameto no need the metadata (e.g. useModel.convert_data_frame_to_radians()so thatinDegreesisn’t needed).- Parameters:
requested_schema – An optional Arrow schema capsule (currently ignored).
- Returns:
A
PyCapsulecalled “arrow_array_stream” containing a CArrowArrayStreamstruct.
- __init__(self) None#
Constructs an empty
DataFrame.At the moment (WIP), the only way to construct a
DataFramethat contains data is via methods likeopynsim.read_sto().
- property attrs#
Returns the attributes (metadata) associated with
self.In some cases, attributes can affect the behavior of functions that read data from
DataFrames. Notably, functions likeopynsim.Model.states_from_data_frame()look for attributes like'inDegrees'to perform on-the-fly degrees-to-radians conversions on legacy data files.
- from_arrow = <nanobind.nb_func object>#
- property shape#
Returns the shape of the
DataFrame.
- to_pandas(self) object#
Returns a
pandas.DataFrameconstructed fromself.The
pandasmodule is lazilyimported when this method is called. It’s expected that the caller’s environment supplies a version ofpandasthat is compatible with the Arrow API (>= v2.2.0). This may require additionally supplyingpyarrow, whichpandasmay internally use to implement the API.See also:
__arrow_c_stream__()andfrom_arrow().
- to_polars(self) object#
Returns a
polars.DataFrameconstructed fromself.The
polarsmodule is lazilyimported when this method is called. It’s expected that the callers have installed a version ofpolarsthat is compatible with the Arrow API (>= v0.20.4).See also:
__arrow_c_stream__()andfrom_arrow().
- class opynsim.ForwardDynamicsSolver(*args, **kwargs)#
Bases:
objectIntegrates the forward dynamics of a
Model+ModelStatepair.The solver stores a
ModelStatethat it integrates forward in time to a caller-specified timepoint (seeintegrate_to()).- __init__(self, model: opynsim._core.Model, model_state: opynsim._core.ModelState, integrator_settings: opynsim._core.IntegratorSettings | None = None) None#
Constructs a
ForwardDynamicsSolverofmodelinmodel_state.- Parameters:
model – The
Modelthat is being integrated.model_state – The state of
modelthat the solver begins integration from.integrator_settings – The
IntegratorSettingsthat the solvers’s integrator uses for integration.
- integrate_to(self, time: float, realized_to: opynsim._core.ModelStateStage = ModelStateStage.REPORT) opynsim._core.ModelState#
Integrates the solvers’s internal
ModelStateforwards totimeand returns a copy of the internal state realized to at leastrealized_to.
- class opynsim.IntegratorSettings(*args, **kwargs)#
Bases:
objectSettings for a forward integrator (e.g. as used by
ForwardDynamicsSolver).Note: Modifying integrator settings can have a large effect on its performance and behavior. When tweaking the settings, it is recommended to re-validate outcomes.
- __init__(self) None#
- class opynsim.Model#
Bases:
objectA compiled model of a physics system.
A
Modelcan only be created from aModelSpecificationvia theModelSpecification.compile()function. Therefore, editing aModelrequires editing its associatedModelSpecificationand recompiling it to create a newModel.- __init__(*args, **kwargs)#
- column_to_state_variable_mappings(self, data_frame: opynsim._core.DataFrame) dict[str, opynsim._core.Symbol]#
Returns associative mappings between the names of columns in
data_frameand state variables in thisModel, where the correspondence can be found.This mapping uses a variety of heuristics, including (e.g.) accounting for legacy column headers supported by earlier files in SIMM and OpenSim. It is how
states_from_data_frame()maps dataframes intoModelStates, so this method can be useful for debugging why states aren’t being read correctly.
- convert_data_frame_to_radians(self, data_frame: opynsim._core.DataFrame) opynsim._core.DataFrame#
Returns a new
DataFramewith all rotational columns converted to radians (if applicable).If
data_frame.attrs["inDegrees"] != "yes", returns an identical clone ofdata_frame. Otherwise, creates a newDataFramewhererotational_columns_in()is used to scale all rotational columns from degrees to radians (other columns are left unmodified). The"inDegrees"key is cleared from the returnedDataFrame's attributes.
- property coordinates#
Returns a list of all the coordinates in the model.
- get_coordinate_speed(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol) float#
Returns the value of the corresponding state variable in
model_statefor the speed ofcoordinate.
- get_coordinate_value(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol) float#
Returns the value of the corresponding state variable in
model_statefor the value ofcoordinate.
- get_output_value(self, model_state: opynsim._core.ModelState, output: opynsim._core.Symbol) float | numpy.ndarray[dtype=float64, shape=(3)]#
Returns the value of
outputfor the givenmodel_state.
- initial_state(self, *, realized_to: opynsim._core.ModelStateStage = ModelStateStage.INSTANCE) opynsim._core.ModelState#
Returns a
ModelStatethat represents the initial (default) state of thisModel.The initial state of a
Modelis dictated by theModelSpecificationused to compile it. For example, if a translational coordinate in the specification had adefault_valueof1.0then that would be written into theModelStatereturned by this function.The returned
ModelStatewill be realized to at leastrealized_toas-if by callingModel.realize()on it.
- is_coordinate_locked(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol) bool#
Returns
Trueifcoordinateis locked inmodel_state.Locking a coordinate prevents it from changing its value/speed (e.g. by a solver).
- is_coordinate_rotational(self, coordinate: opynsim._core.Symbol) bool#
Returns
Trueifcoordinateis rotational (i.e. not translational, coupled, or undefined).
- property num_coordinates#
Returns the number of coordinates in the model.
A coordinate represents a single degree of freedom (DoF) in the model, such as a joint angle, translation, or rotational parameter that contributes to the configuration/pose of a model.
- property num_outputs#
Returns the number of outputs the model has.
- property outputs#
Returns a list of all outputs the model has.
- realize(self, model_state: opynsim._core.ModelState, model_state_stage: opynsim._core.ModelStateStage) None#
Realizes
model_stateto the desiredmodel_state_stage, which modifiesmodel_statein-place.“Realization” of the state involves taking a new set of values from the
ModelStateand performing computations that those new values enable. Realization is performed in-order oneModelStateStageat time. For example,ModelStateStage.POSITIONis realized beforeModelStateStage.VELOCITY,thenModelStateStage.DYNAMICS, and so on.
- rotational_columns_in(self, data_frame: opynsim._core.DataFrame) set[str]#
Returns the names of columns in
data_framethat can be mapped to rotational state variables in thisModelin the column-order ofdata_frame.This is how
states_from_data_frame()automatically converts degrees to radians whendata_frame.attrs["inDegrees"] == "yes", so it can be useful for debugging why states aren’t being read correctly.
- set_coordinate_locked(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol, locked: bool = True) None#
Sets the locked state of
coordinateinmodel_statetolocked.Locking a coordinate prevents it from changing its value/speed (e.g. by a solver).
- set_coordinate_speed(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol, speed: float) None#
Sets the value corresponding state variable in
model_statefor the speed ofcoordinatetospeed.Changing the speed of a coordinate changes
model_state’sModelStateStagetoModelStateStage.POSITION. Therefore, you may need to userealize()to re-realize the state to a later stage if you intend on using the state with a method that requires a later stage (e.g. rendering).
- set_coordinate_value(self, model_state: opynsim._core.ModelState, coordinate: opynsim._core.Symbol, value: float) None#
Sets corresponding state variable in
model_statefor the value ofcoordinatetovalue.Changing the value of a coordinate changes
model_state’sModelStateStagetoModelStateStage.POSITION. Therefore, you may need to userealize()to re-realize the state to a later stage if you intend on using the state with a method that requires a later stage (e.g. rendering).
- states_from_data_frame(self, data_frame: opynsim._core.DataFrame, *, realized_to: opynsim._core.ModelStateStage = ModelStateStage.INSTANCE) opynsim._core.ModelStates#
Returns
ModelStatesconstructed fromdata_frame.Columns in
data_framewill be mapped to state variables in thisModel(see:column_to_state_variable_mappings()). Each row indata_frameconstructs oneModelStatein the returnedModelStates, in row-order.If
data_frame.attrs["inDegrees"] == "yes"then rotational columns indata_framewill be automatically converted into radians internally (see:rotational_columns_in()). This is to supportDataFrames loaded from legacy data sources.Each of the returned
ModelStates will be realized to at leastrealized_toas-if by callingModel.realize()on each of them.
- class opynsim.ModelSpecification(*args, **kwargs)#
Bases:
objectA high-level specification for a
Model.A
ModelSpecificationis what Python code can manipulate, scale, and customize before passing it tocompile(), which returns a readonlyModel.Notes
OPynSim’s API design separates the specification of a model (
ModelSpecification) from its validated, assembled, and optimized physics engine representation (Model) to ensure that the compilation process (compile()) can freeze and optimize internal datastructures at a single point in the process.- __init__(self) None#
- compile(self) opynsim._core.Model#
Compiles this
ModelSpecificationinto aModel.The compilation process:
Validates this
ModelSpecification’s components (properties, subcomponents, and sockets), throwing an exception if the specification is invalid in some way.Assembles a physics system from the validated specification, throwing an exception if the physics system cannot be assembled (e.g. if the contains impossible-to-satisfy joints, or invalid muscle definitions).
- Raises:
Exception – If the compilation process failed in some way. It is assumed that the provided
ModelSpecificationis valid.
- class opynsim.ModelState#
Bases:
objectRepresents a single state of a
Model.A
ModelStatebundles together the state variables, cache variables, and other information necessary to describe a single state of aModel.Models can read/manipulateModelStates in order toModel.realize()the state to a later stage (e.g. as part of forward integration) or read outputs values. However,ModelStates may also be created, read, and manipulated by downstream Python code and other utilities in OPynSim.- __init__(*args, **kwargs)#
- property stage#
Returns the
ModelStateStageof the state.Notes
A state may be realized to a later stage with
Model.realize().
- property time#
Returns the time of the state.
- class opynsim.ModelStateStage(*values)#
Bases:
EnumRepresents a stage of state realization (computation).
When calling methods like
Model.realize(), aModelStateis realized in-order through eachModelStateStage, starting at the lowest stage and ending at the highest stage.Each time a
ModelStateadvances through aModelStateStage, more information is available in the state. For example, after aModelStateis realized toModelStateStage.POSITION, positional quantities such as the positions of bodies and offset frames are known, and any positional output on the associatedModelcan then extract that information from theModelState.For convenience, the
opynsimmodule defines aliases for eachModelStateStage:opynsim.STAGE_TOPOLOGY->opynsim.ModelStateStage.TOPOLOGYopynsim.STAGE_MODEL->opynsim.ModelStateStage.MODELopynsim.STAGE_INSTANCE->opynsim.ModelStateStage.INSTANCEopynsim.STAGE_TIME->opynsim.ModelStateStage.TIMEopynsim.STAGE_POSITION->opynsim.ModelStateStage.POSITIONopynsim.STAGE_VELOCITY->opynsim.ModelStateStage.VELOCITYopynsim.STAGE_DYNAMICS->opynsim.ModelStateStage.DYNAMICSopynsim.STAGE_ACCELERATION->opynsim.ModelStateStage.ACCELERATIONopynsim.STAGE_REPORT->opynsim.ModelStateStage.REPORT
- ACCELERATION = 7#
- DYNAMICS = 6#
- INSTANCE = 2#
- MODEL = 1#
- POSITION = 4#
- REPORT = 8#
- TIME = 3#
- TOPOLOGY = 0#
- VELOCITY = 5#
- class opynsim.ModelStates(*args, **kwargs)#
Bases:
objectRepresents a sequence of
ModelStates.ModelStatesis typically returned from functions that produce sequences ofModelStates, such asModel.states_from_data_frame(). The API ofModelStatesis list-like, meaning downstream code can iterate over eachModelState, uselenwith it, randomly access a state withmodel_states[idx]and so on.- __init__(self) None#
- to_list(self) list[opynsim._core.ModelState]#
- class opynsim.Symbol(*args, **kwargs)#
Bases:
objectRepresents an immutable, cheap-to-use, readable symbol.
Symbols are extensively used by the OPynSim API to accelerate associative lookups. They are the middle-ground between fast, but hard to read/introspect, integer handles and slow, simpler string handles.
From Python code’s point of view, symbols should be seen as string-like handles that OPynSim accepts/emits. You can safely store symbols independently of any larger data structure, and interchange them across your entire Python codebase, without having to worry about object lifetimes. OPynSim’s native code uses runtime-checked associative lookups, rather than pointers, to ensure that the Python API is memory-safe and can provide suitable feedback whenever a lookup fails.
- __init__(self, id: str) None#
Constructs a symbol from a Python string.
- opynsim.read_csv(source: str | os.PathLike) opynsim._core.DataFrame#
Returns a
DataFrameparsed from an.csvfile on the caller’s filesystem.The CSV file must have a header section, delimited by ‘endheader`. This usually necessitates adding an endheader entry just above the header row (TODO: this limitation was inherited from OpenSim and shouldn’t be a thing long-term).
- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_jpeg(source: str | os.PathLike) opynsim._core.graphics.Texture2D#
Returns a
graphics.Texture2Dparsed from a.jpegfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_jpg(source: str | os.PathLike) opynsim._core.graphics.Texture2D#
An alias for
read_jpeg()
- opynsim.read_mot(source: str | os.PathLike) opynsim._core.DataFrame#
Returns a
DataFrameparsed from an.motfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_obj(source: str | os.PathLike) opynsim._core.graphics.Mesh#
Returns a
graphics.Meshparsed from a.objfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_osim(source: str | os.PathLike) opynsim._core.ModelSpecification#
Returns a
ModelSpecificationparsed from an .osim file on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_png(source: str | os.PathLike) opynsim._core.graphics.Texture2D#
Returns a
graphics.Texture2Dparsed from a.pngfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_stl(source: str | os.PathLike) opynsim._core.graphics.Mesh#
Returns a
graphics.Meshparsed from a.stlfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_sto(source: str | os.PathLike) opynsim._core.DataFrame#
Returns a
DataFrameparsed from an.stofile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_trc(source: str | os.PathLike) opynsim._core.DataFrame#
Returns a
DataFrameparsed from an.trcfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
- opynsim.read_vtp(source: str | os.PathLike) opynsim._core.graphics.Mesh#
Returns a
graphics.Meshparsed from a.vtpfile on the caller’s filesystem.- Raises:
RuntimeError – If the file cannot be found, read, or is invalid.
Modules