Internals

Progress bars

class pigreads.progress.BaseProgressIterator(iterable)

Bases: Iterator[T]

Base class for progress iterators.

Parameters:

iterable (Iterable[TypeVar(T)]) – An iterable object.

class pigreads.progress.JSONProgressIterator(iterable, file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)

Bases: BaseProgressIterator[T]

JSON progress iterator.

This progress indicator prints the current index, total number of items, time elapsed, time remaining, and time per iteration in JSON format.

Parameters:
  • iterable (Iterable[TypeVar(T)]) – An iterable object.

  • file (TextIO) – File object to write progress to. (default: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)

Variables:
  • total – Total number of items.

  • file – File object to write progress to.

  • index – The current index.

  • start – Start time.

total: int | None
pigreads.progress.PROGRESS_ITERS: dict[str, Callable[[Any], Any]]

Mapping of progress bar types to progress indicators.

Options:

  • “bar”: a bar with detailed progress information

  • “json”: JSON progress information

  • “plain”: plain text progress information

  • “none”: no progress information

class pigreads.progress.PlainProgressIterator(iterable, file=<_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)

Bases: BaseProgressIterator[T]

Plain progress iterator.

This progress indicator simply prints the current index and total number of items.

Parameters:
  • iterable (Iterable[TypeVar(T)]) – An iterable object.

  • file (TextIO) – File object to write progress to. (default: <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>)

Variables:
  • total – Total number of items.

  • file – File object to write progress to.

  • index – The current index.

total: int | None
class pigreads.progress.T

abstract type variable for generic types

alias of TypeVar(‘T’)

Low-level implementation

class pigreads.core.Models(double_precision=False, context=None)

Bases: object

Core implementation of the Models class.

See:

pigreads.models.Models for the main interface to the models.

Parameters:
  • double_precision (bool) – If True, use double precision for calculations. (default: False)

  • context (Context | None) – OpenCL context to use for computations. (default: None)

property Nv: int

Maximum number of state variables in the models.

__len__()

Number of models.

Return type:

int

Returns:

Number of models.

add(key, code, Nv, params)

Select and enable a model with given parameters.

Parameters:
  • key (str) – The key of the model to be added.

  • code (str) – OpenCL code for the model.

  • Nv (int) – Number of variables.

  • params (ndarray[Any, Any]) – Parameter values.

Return type:

None

adjust_work_size(Nz, Ny, Nx)

Slightly increase the global work size to be a multiple of the local work size.

Parameters:
  • Nz (int) – Number of points in the z-dimension.

  • Ny (int) – Number of points in the y-dimension.

  • Nx (int) – Number of points in the x-dimension.

Return type:

tuple[int, int, int]

Returns:

Adjusted global work size.

property block_size: tuple[int, int, int]

Local work size for running OpenCL kernels.

compile(code)

Compile the given OpenCL code.

Parameters:

code (str) – OpenCL code to compile.

Return type:

None

property context: Context

The OpenCL context used for computations.

property double_precision: bool

Whether double precision is used.

get_block_size()

Get the local work size for running OpenCL kernels.

Return type:

tuple[int, int, int]

Returns:

Local work size.

get_key(imodel)

Get the key of the model with the given index.

Parameters:

imodel (int) – Index of the model.

Return type:

str

Returns:

Key of the model with the given index.

get_number_definitions()

Get the number of model definitions.

Return type:

int

Returns:

Number of model definitions

get_parameter(imodel, iparam)

Get the parameter with the given indices.

Parameters:
  • imodel (int) – Index of the model.

  • iparam (int) – Index of the parameter.

Return type:

float

Returns:

Parameter value.

prepare_context()

Make sure there is a context and a queue.

Return type:

None

property queue: CommandQueue

The OpenCL command queue used for computations.

property real: type

The floating-point type used, i.e., np.float32 or np.float64.

run(inhom, weights, states, stim_signal, stim_shape, Nt, dt)

Run a Pigreads simulation.

Parameters:
  • inhom (ndarray[Any, Any]) – A 3D array with integer values, encoding which model to use at each point. Its value is zero for points outside the medium and one or more for points inside. Values larger than zero are used to select one of multiple models: 1 for models[0], 2 for models[1], etc.

  • weights (ndarray[Any, Any]) – The weights for the diffusion term, see weights().

  • states (ndarray[Any, Any]) – The initial states of the simulation, a 4D array of shape (Nz, Ny, Nx, Nv).

  • stim_signal (ndarray[Any, Any]) – A 3D array with the stimulus signal at each time point for all variables, with shape (Nt, Ns, Nv).

  • stim_shape (ndarray[Any, Any]) – A 4D array specifying the shape of the stimulus, with shape (Ns, Nz, Ny, Nx).

  • Nt (Any) – The number of time steps to run the simulation for.

  • dt (Any) – The time step size.

Return type:

None

set_block_size(block_size)

Set the local work size for running OpenCL kernels.

Parameters:

block_size (tuple[int, int, int]) – Local work size.

Return type:

None

set_parameter(imodel, iparam, value)

Set the parameter with the given indices.

Parameters:
  • imodel (int) – Index of the model.

  • iparam (int) – Index of the parameter.

  • value (float) – New parameter value.

Return type:

None

weights(dz, dy, dx, mask, diffusivity)

Calculate the weights for the diffusion term in the reaction-diffusion equation.

Parameters:
  • dz (Any) – The distance between points in the z-dimension.

  • dy (Any) – The distance between points in the y-dimension.

  • dx (Any) – The distance between points in the x-dimension.

  • mask (ndarray[Any, Any]) – 3D boolean array encoding which points are inside the medium.

  • diffusivity (ndarray[Any, Any]) – The diffusivity matrix.

Return type:

ndarray[Any, Any]

Returns:

Weight matrix for the diffusion term, A 5D array of shape (1, Nz, Ny, Nx, 19).