neuromancer.psl.base module

Base classes for dynamic systems.

class neuromancer.psl.base.Backend(backend)[source]

Bases: object

backends = {'numpy': {'cast': <built-in function array>, 'cat': <function concatenate>, 'core': <module 'numpy' from '/Users/raab944/Library/CloudStorage/OneDrive-PNNL/Documents/projects/buildings/neuromancer-src/neuromancer/.venv/lib/python3.10/site-packages/numpy/__init__.py'>, 'grad': <function Backend.<lambda>>, 'odeint': functools.partial(<function odeint>, tfirst=True)}, 'torch': {'cast': <built-in method tensor of type object>, 'cat': <built-in method cat of type object>, 'core': <module 'torch' from '/Users/raab944/Library/CloudStorage/OneDrive-PNNL/Documents/projects/buildings/neuromancer-src/neuromancer/.venv/lib/python3.10/site-packages/torch/__init__.py'>, 'grad': <function grad>, 'odeint': <function odeint>}}
numpy_backend = {'cast': <built-in function array>, 'cat': <function concatenate>, 'core': <module 'numpy' from '/Users/raab944/Library/CloudStorage/OneDrive-PNNL/Documents/projects/buildings/neuromancer-src/neuromancer/.venv/lib/python3.10/site-packages/numpy/__init__.py'>, 'grad': <function Backend.<lambda>>, 'odeint': functools.partial(<function odeint>, tfirst=True)}
torch_backend = {'cast': <built-in method tensor of type object>, 'cat': <built-in method cat of type object>, 'core': <module 'torch' from '/Users/raab944/Library/CloudStorage/OneDrive-PNNL/Documents/projects/buildings/neuromancer-src/neuromancer/.venv/lib/python3.10/site-packages/torch/__init__.py'>, 'grad': <function grad>, 'odeint': <function odeint>}
class neuromancer.psl.base.EmulatorBase(exclude_norms=['Time'], backend='numpy', requires_grad=False, seed: int | Generator = 59, set_stats=True)[source]

Bases: ABC, Module

add_missing_parameters()[source]
change_backend(backend=<module 'torch' from '/Users/raab944/Library/CloudStorage/OneDrive-PNNL/Documents/projects/buildings/neuromancer-src/neuromancer/.venv/lib/python3.10/site-packages/torch/__init__.py'>)[source]
denormalize(data, normalizer=None, key=None)[source]
Parameters:
  • data

  • normalizer

  • key

Returns:

get_x0()[source]

Randomly sample an initial condition

Parameters:

box – Dictionary with keys ‘min’ and ‘max’ and values np.arrays with shape=(nx,)

normalize(data, normalizer=None, key=None)[source]
Parameters:
  • data – (Tensor, ndarray, or dict of tensor or ndarray)

  • normalizer

  • key

Returns:

property params
restore_random_state(rng_state=None)[source]

Load random state

save_random_state()[source]

Save random state for later use

set_params(parameters, requires_grad=False, cast=True)[source]
set_stats(x0=None, U=None, D=None, nsim=None, sim=None)[source]

Get a hyperbox defined by min and max values on each of nx axes. Used to sample initial conditions for simulations. Box is generated by simulating system with step size ts for nsim steps and then taking the min and max along each axis

Parameters:
  • system – (psl.ODE_NonAutonomous)

  • ts – (float) Timestep interval size

  • nsim – (int) Number of simulation steps to use in defining box

show(data=None, figname=None)[source]

By default will plot the data used to generate initial system statistics from the canonical 1000 step simulation.

Parameters:
  • data – (dict {str: Tensor or ndarray}) Will plot data from any system simulation given via this argument

  • figname – (str) Optional name for figure. By default uses class name and saves figure as .png.

abstract simulate()[source]
class neuromancer.psl.base.EquationWrapper(Time, U, equations, backend)[source]

Bases: object

The interface for odeint methods in torch and scipy does not handle exogenous inputs. This wrapper allows us to index control inputs by time point.

class neuromancer.psl.base.ODE_Autonomous(exclude_norms=['Time'], backend='numpy', requires_grad=False, seed: int | Generator = 59, set_stats=True)[source]

Bases: EmulatorBase

base class autonomous ODE

abstract equations()[source]
forward(x, t)[source]

Define the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

simulate(nsim=None, Time=None, ts=None, x0=None)[source]
Parameters:
  • nsim – (int) Number of steps for open loop response

  • ninit – (float) initial simulation time

  • ts – (float) step size, sampling time

  • Time – (Sequence of float) Optional timesteps to integrate over.

  • x0 – (float) state initial conditions

Returns:

The response matrices, i.e. X

class neuromancer.psl.base.ODE_NonAutonomous(exclude_norms=['Time'], backend='numpy', requires_grad=False, seed: int | Generator = 59, set_stats=True)[source]

Bases: EmulatorBase

base class non-autonomous ODE

add_missing_parameters()[source]
abstract equations()[source]
forward(x, u)[source]

For compatibility with the System class for open/closed loop simulations

Parameters:
  • x – 2d Matrix (1, nx) # for torch backend generalize to (batchsize, nx)

  • t – (1, 1)

  • u – (1, nu)

Returns:

x_next (1, nx)

get_R(nsim)[source]

For sampling a sequence of reference trajectories

Parameters:

nsim – (int) Length of sequence

Returns:

Matrix nsim X nx0

get_U(nsim, umin=None, umax=None, signal=None, **signal_kwargs)[source]

For sampling a sequence of control actions :param nsim: length of sequence :return: Matrix nsim X nU

get_simulation_args(nsim, Time, ts, x0, U)[source]
simulate(nsim=None, Time=None, ts=None, x0=None, U=None)[source]
Parameters:
  • nsim – (int) Number of steps for open loop response

  • Time – (Sequence of float) Optional timesteps to integrate over.

  • ts – (float) step size, sampling time

  • x0 – (float) state initial conditions

Returns:

Dictionary containing X, Y, U

neuromancer.psl.base.cast_backend(method)[source]

Decorator to cast numerics to appropriate backend.

Parameters:

method

Returns:

neuromancer.psl.base.download(url, dst)[source]

Function is used by FileEmulator and BuildingEnvelope classes to retrieve data we don’t want to host on github.

Parameters:
  • url – (str) Url to retrieve data from

  • dst – (str pathlike) Where to store the data on disk

neuromancer.psl.base.grad(tensor, requires_grad)[source]

Helper function to set gradient of tensors for pytorch backend :param tensor: :param requires_grad: :return: