swifco_rs.analysis module

Output functionality, like writing files and showing live visualizations.

swifco_rs.analysis.color_map(name, lut=None)

Extract the colors from a named color map as described in https://matplotlib.org/stable/tutorials/colors/colormaps.html

swifco_rs.analysis.image_viewer(text_size=14)

Live raster image viewer for visual debugging.

Parameters

text_size – Text size for the model tick

Returns

A callback set_frame(buffer) to be used with RGB grid observers. buffer is an RGB image as numpy array of 8-bit values of dimension (ROWS, COLS, 3), or of scalars of dimension (ROWS, COLS) for a pseudo-color image.

swifco_rs.analysis.next_color(colors=None)

Create a callback to obtain unique colors for plotting categorical values.

Parameters

colors – Color cycle to use

Returns

A callback to obtain the next color

swifco_rs.analysis.tif_writer(path, dtype, ref_map=None)

Writes raster data to tiff files.

Parameters
  • path – Target path to write to. String interpolation with variable step possible, e.g. f"out-{seed:03d}.tif"

  • dtype – GDAL data type, e.g. gdal.GDT_Int32

  • ref_map – Reference map for projection and geo-transform

Returns

A callback function set_data(data, no_data) to set the raster data. data is a 2d numpy array, no_data is an optional no-data value.

swifco_rs.analysis.time_series_viewer(columns=None, interval=1, window=None)

Live time series line plot viewer for visual debugging.

Parameters
  • columns – List of column names to plot, or None for plotting all columns

  • interval – Update interval of the viewer, in model ticks

  • window – Time interval to display

Returns

A callback add_row(header, row) for use with table observers. header is a list of column names, row is a list of row values.

swifco_rs.analysis.video_blender(add_frame, /, mode)

Returns two video writer callbacks which will be blended before being passed on to the given one.

The dimensions of the image buffers passed to the two callbacks must match. The first callback must always be called before the second one.

Supported blend modes are “darken” and “lighten”.

swifco_rs.analysis.video_scaler(add_frame, /, scale)

Returns a video writter callback wrapping the given one in a scaling operation.

swifco_rs.analysis.video_stacker(add_frame, /, count, orientation)

Returns a list of video writer callbacks which will be stacked before being passed on to the given one.

The dimensions of the image buffers passed to the callbacks must match. The callbacks must always be called in the order in which they were returned.

Supported orientations are “horizontal” and “vertical”.

swifco_rs.analysis.video_writer(path, scale=4, fourcc='avc1', frame_rate=10, text_size=2, text_thickness=3, text_color=(255, 255, 255))

Write model RGB output rasters to video files.

Parameters
  • path – Path to the video output file

  • scale – Scale factor for the video, i.e. the cell size in pixels

  • fourcc – Codec, compression etc in FOURCC format

  • frame_rate – Video frame rate in frames per second

  • text_size – Text size for the model time step

  • text_thickness – Text thickness for the model time step

  • text_color – Text color for the model time step

Returns

A callback add_frame(buffer) for use with RGB grid observers. buffer is an RGB image as numpy array of 8-bit values of dimension (ROWS, COLS, 3), or of scalars of dimension (ROWS, COLS) for a pseudo-color image.