Visualization

Visualizations of simulations with pyglet.

class epipack.vis.App(width, height, simulation_status, *args, **kwargs)[source]

Bases: pyglet.window.BaseWindow

A pyglet Window class that makes zooming and panning convenient and tracks user input.

Adapted from Peter Varo's solution at https://stackoverflow.com/a/19453006/4177832

Parameters
  • width (float) -- Width of the app window

  • height (float) -- Height of the app window

  • simulation_status (SimulationStatus) -- An object that tracks the simulation. Here, it's used to pause or increase the simulation speed.

add_batch(batch, prefunc=None)[source]

Add a batch that needs to be drawn. Optionally, also pass a function that's triggered before this batch is drawn.

init_gl(width, height)[source]
on_draw()[source]

Clear and draw all batches

on_key_press(symbol, modifiers)[source]

Check for keyboard input. Current inputs:

  • backspace or CMD+0: reset view

  • up : increase simulation speed

  • down : decrease simulation speed

  • space : pause simulation

on_mouse_drag(x, y, dx, dy, buttons, modifiers)[source]

Pan.

on_mouse_scroll(x, y, dx, dy)[source]

Zoom.

on_resize(width, height)[source]

Rescale.

class epipack.vis.Curve(x, y, color, scale, batch)[source]

Bases: object

A class that draws an OpenGL curve to a pyglet Batch instance with easy methods to update data.

Parameters
  • x (list) -- x data

  • y (list) -- y data

  • color (list) -- List of 3 integers between 0 and 255 (RGB color list)

  • scale (Scale) -- An instance of a Scale that maps the data dimensions to an area in a pyglet Window

batch

The batch instance in which this curve is drawn

Type

pyglet.graphics.Batch

scale

An instance of a Scale that maps the data dimensions to an area in a pyglet Window

Type

Scale

vertex_list

Contains the vertex list in window coordinates. format strings are v2f and c3B.

Type

pyglet.graphics.VertexList

color

as described above

Type

list

xmin

lower bound of x-dimension

Type

float

xmax

upper bound of x-dimension

Type

float

ymin

lower bound of y-dimension

Type

float

ymax

upper bound of y-dimension

Type

float

append_list(x, y)[source]

Append a list of data points to this curve. Note that if the bounds change with this update, the connect Scale-instance will be updated automatically.

append_single_value(x, y)[source]

Append a single data point to this curve. Note that if the bounds change with this update, the connect Scale-instance will be updated automatically.

rescale()[source]

Rescale this curve's data according to the connected Scale-instance

set(x, y)[source]

Set the data of this curve.

update_bounds(xmin, xmax, ymin, ymax)[source]

Compute the bounds of this curves data and update the scale accordingly

class epipack.vis.Scale(bound_increase_factor=1.0)[source]

Bases: object

A scale that maps all its connected graphics objects to world (window) dimensions.

Parameters

bound_increase_factor (float, default = 1.0) -- By how much the respective bound should increase once it's reached.

bound_increase_factor

By how much the respective bound should increase once it's reached.

Type

float

x0

lower bound of data x-dimension

Type

float

x1

upper bound of data x-dimension

Type

float

y0

lower bound of data y-dimension

Type

float

y1

upper bound of data y-dimension

Type

float

left

lower bound of world x-dimension

Type

float

right

upper bound of world x-dimension

Type

float

bottom

lower bound of world y-dimension

Type

float

top

upper bound of world y-dimension

Type

float

scaling_objects

A list of objects that need to be rescaled once the data or world dimensions change. Each entry of this list is assumed to be an object that has a method called rescale().

Type

list

add_scaling_object(obj)[source]

Append an object that depends on this Scale instance.

check_bounds(xmin, xmax, ymin, ymax)[source]

Check whether the global data dimensions have changed considered updated data dimensions of a single instance. If this is the case, trigger rescaling of all connected instances.

domain(x0, x1, y0, y1)[source]

Define the data dimensions.

extent(left, right, top, bottom)[source]

Define the world (window) dimensions.

scale(x, y)[source]

Scale data.

scalex(x)[source]

Scale x-data

scaley(y)[source]

Scale y-data

class epipack.vis.SimulationStatus(N, sampling_dt)[source]

Bases: object

Saves information about the current simulation.

Parameters
  • N (int) -- Number of nodes

  • sampling_dt (float) -- The amount of simulation time that's supposed to pass during a single update

old_node_status

An array containing node statuses of the previous update

Type

numpy.ndarray

sampling_dt

The amount of simulation time that's supposed to pass during a single update

Type

float

simulation_ended

Whether or not the simulation is over

Type

bool

paused

Whether or not the simulation is paused

Type

bool

set_simulation_status(simulation_ended)[source]

Trigger the simulation to be over.

update(old_node_status)[source]

Update the nodes statuses.

epipack.vis.get_network_batch(stylized_network, yoffset, draw_links=True, draw_nodes=True, draw_nodes_as_rectangles=False, n_circle_segments=16)[source]

Create a batch for a network visualization.

Parameters
  • stylized_network (dict) -- The network properties which are returned from the interactive visualization.

  • draw_links (bool, default : True) -- Whether the links should be drawn

  • draw_nodes (bool, default : True) -- Whether the nodes should be drawn

  • n_circle_segments (bool, default = 16) -- Number of segments a circle will be constructed of.

Returns

network_objects -- A dictionary containing all the necessary objects to draw and update the network. - lines : a list of pyglet-line objects (one entry per link)

  • disks : a list of pyglet-circle objects (one entry per node)

  • circles : a list of pyglet-circle objects (one entry per node)

  • nodes_to_lines : a dictionary mapping a node to a list of pairs. Each pair's first entry is the focal node's neighbor and the second entry is the index of the line-object that connects the two

  • batch : the pyglet Batch instance that contains all of the objects

Return type

dict

epipack.vis.visualize(model, network, sampling_dt, ignore_plot_compartments=[], quarantine_compartments=[], config=None)[source]

Start a visualization of a stochastic simulation.

Parameters
  • model (epipack.stochastic_epi_models.StochasticEpiModel) -- An initialized StochasticEpiModel.

  • network (dict) --

    A stylized network in the netwulf-format (see https://netwulf.readthedocs.io/en/latest/python_api/post_back.html) where instead of 'x' and 'y', node positions are saved in 'x_canvas' and 'y_canvas'. Example:

    stylized_network = {
        "xlim": [0, 833],
        "ylim": [0, 833],
        "linkAlpha": 0.5,
        "nodeStrokeWidth": 0.75,
        "links": [
            {"source": 0, "target": 1, "width": 3.0 }
        ],
        "nodes": [
            {"id": 0,
             "x_canvas": 436.0933431058901,
             "y_canvas": 431.72418500564186,
             "radius": 20
             },
            {"id": 1,
             "x_canvas": 404.62184898400426,
             "y_canvas": 394.8158724310507,
             "radius": 20
             }
        ]
    }
    

  • sampling_dt (float) -- The amount of simulation time that's supposed to pass with a single update.

  • ignore_plot_compartments (list, default = []) -- List of compartment objects that are supposed to be ignored when plotted.

  • quarantine_compartments (list, default = []) -- List of compartment objects that are supposed to be resemble quarantine (i.e. temporarily losing all connections)

  • config (dict, default = None) --

    A dictionary containing all possible configuration options. Entries in this dictionary will overwrite the default config which is

    _default_config = {
                'plot_sampled_curve': True,
                'draw_links':True,
                'draw_nodes':True,
                'n_circle_segments':16,
                'plot_height':120,
                'bgcolor':'#253237',
                'curve_stroke_width':4.0,
                'node_stroke_width':1.0,
                'link_color': '#4b5a62',
                'node_stroke_color':'#000000',
                'node_color':'#264653',
                'bound_increase_factor':1.0,
                'update_dt':0.04,
                'show_curves':True,
                'draw_nodes_as_rectangles':False,
                'show_legend': True,
                'legend_font_color':'#fafaef',
                'legend_font_size':10,
                'padding':10,
                'compartment_colors':_colors
            }
    

epipack.vis.visualize_reaction_diffusion(model, network, sampling_dt, node_compartments, value_extent=[0.0, 1.0], integrator='euler', n_integrator_midpoints=0, config=None)[source]

Start a visualization of a reaction-diffusion simulation.

Parameters
  • model (epipack.deterministic_epi_models.DeterministicEpiModel) -- An initialized DeterministicEpiModel.

  • network (dict) --

    A stylized network in the netwulf-format (see https://netwulf.readthedocs.io/en/latest/python_api/post_back.html) where instead of 'x' and 'y', node positions are saved in 'x_canvas' and 'y_canvas'. Example:

    stylized_network = {
        "xlim": [0, 833],
        "ylim": [0, 833],
        "linkAlpha": 0.5,
        "nodeStrokeWidth": 0.75,
        "links": [
            {"source": 0, "target": 1, "width": 3.0 }
        ],
        "nodes": [
            {"id": 0,
             "x_canvas": 436.0933431058901,
             "y_canvas": 431.72418500564186,
             "radius": 20
             },
            {"id": 1,
             "x_canvas": 404.62184898400426,
             "y_canvas": 394.8158724310507,
             "radius": 20
             }
        ]
    }
    

  • sampling_dt (float) -- The amount of simulation time that's supposed to pass with a single update.

  • quarantine_compartments (list) -- List of compartment objects that are supposed to be resemble quarantine (i.e. temporarily losing all connections)

  • node_compartments (list) -- The compartments for which to display the concentrations. Each entry m in this list is expected to be a compartment associated with node m. this list should therefore be as long as the number of nodes.

  • config (dict, default = None) --

    A dictionary containing all possible configuration options. Entries in this dictionary will overwrite the default config which is

    _default_config = {
                'plot_sampled_curve': True,
                'draw_links':True,
                'draw_nodes':True,
                'n_circle_segments':16,
                'plot_height':120,
                'bgcolor':'#253237',
                'curve_stroke_width':4.0,
                'node_stroke_width':1.0,
                'link_color': '#4b5a62',
                'node_stroke_color':'#000000',
                'node_color':'#264653',
                'bound_increase_factor':1.0,
                'update_dt':0.04,
                'show_curves':True,
                'draw_nodes_as_rectangles':False,
                'show_legend': True,
                'legend_font_color':'#fafaef',
                'legend_font_size':10,
                'padding':10,
                'compartment_colors':_colors
            }