mosaik.util — Utility classes and functions

This module contains some utility functions and classes.

mosaik.util.connect_many_to_one(world, src_set, dest, *attrs, async_requests=False)[source]

connect each entity in src_set to dest.

See the connect for more details.

mosaik.util.connect_randomly(world, src_set, dest_set, *attrs, evenly=True, max_connects=inf)[source]

Randomly connect the entities from src_set to the entities from dest_set and return a subset of dest_set containing all entities with a connection.

world is an instance of the World to which the entities belong.

src_set and dest_set are iterables containing Entity instances. src_set may be empty, dest_set must not be empty. Each entity of src_set will be connected to an entity of dest_set, but not every entity of dest_set will necessarily have a connection (e.g., if you connect a set of three entities to a set of four entities). A set of all entities from dest_set, to which at least one entity from src_set was connected, will be returned.

attrs is a list of attribute names of pairs as in connect.

If the flag evenly is set to True, entities connections will be distributed as evenly as possible. That means if you connect a set of three entities to a set of three entities, there will be three 1:1 connections; if you connect four entities to three entities, there will be one 2:1 and two 1:1 connections. If evenly is set to False, connections will be truly random. That means if you connect three entities to three entities, you may either have three 1:1 connections, one 2:1 and two 1:1 connections or just one 3:1 connection.

max_connects lets you set the maximum number of connections that an entity of dest_set may receive. This argument is only taken into account if evenly is set to False.

mosaik.util.plot_execution_time(world, folder='figures', hdf5path=None, dpi=600, format='png', show_plot=True, slice=None)[source]

Creates an image visualizing the execution time of the different simulators of a mosaik scenario.

Parameters:
  • world – mosaik world object

  • folder – folder to store the image (only if no hdf5path is provided)

  • hdf5path – Path to HDF5 file, which will be used as path for the created image

  • dpi – DPI for created images

  • format – format for created image

  • show_plot – whether to open a window to show the plot

  • slice – reduce the timeframe that you show in the plot. Usage as in Python list slicing, i.e., negative values are possible to start from the end of the list. Jumps are not possible. slice needs to be a two-element integer list, e.g. [0, 5].

Returns:

None but image file will be written to file system

mosaik.util.plot_dataflow_graph(world, folder='figures', hdf5path=None, dpi=600, format='png', show_plot=True)[source]

Creates an image visualizing the data flow graph of a mosaik scenario. Using the spring layout from Matplotlib (Fruchterman- Reingold force-directed algorithm) to position the nodes.

Parameters:
  • world (World) – mosaik world object

  • folder (str) – folder to store the image (only if no hdf5path is provided)

  • hdf5path (str | None) – Path to HDF5 file, which will be used as path for the created image

  • dpi (int) – DPI for created images

  • format (Literal['png', 'pdf', 'svg']) – format for created image

  • show_plot (bool) – whether open a window to show the plot

Returns:

None but image file will be written to file system

mosaik.util.plot_execution_graph(world, title='', folder='figures', hdf5path=None, dpi=600, format='png', show_plot=True, save_plot=True, slice=None)[source]

Creates an image visualizing the execution graph of a mosaik scenario.

Parameters:
  • world (World) – mosaik world object

  • title (str) – the title of the graph

  • folder – folder to store the image (only if no hdf5path is provided)

  • hdf5path – Path to HDF5 file, which will be used as path for the created image

  • dpi – DPI for created images

  • format – format for created image

  • show_plot – whether to open a window to show the plot

  • slice – reduce the timeframe that you show in the plot. Usage as in Python list slicing, i.e., negative values are possible to start from the end of the list. Jumps are not possible. slice needs to be a two-element integer list, e.g. [0, 5].

  • save_plot (bool) –

Returns:

None but image file will be written to file system

mosaik.util.plot_execution_time_per_simulator(world, folder='figures', hdf5path=None, dpi=600, format='png', show_plot=True, plot_per_simulator=False, slice=None)[source]

Creates images visualizing the execution time of each of the different simulators of a mosaik scenario.

Parameters:
  • world – mosaik world object

  • folder – folder to store the image (only if no hdf5path is provided)

  • hdf5path – Path to HDF5 file, which will be used as path for the created image

  • dpi – DPI for created images

  • format – format for created image

  • show_plot – whether to open a window to show the plot

  • plot_per_simulator (bool) – whether to create a separated plot per simulator. This is especially useful if the step sizes of the simulators are very different.

  • slice – reduce the timeframe that you show in the plot. Usage as in Python list slicing, i.e., negative values are possible to start from the end of the list. Jumps are not possible. slice needs to be a two-element integer list, e.g. [0, 5].

Returns:

None but image file will be written to file system