DEMutilities.postprocessing.representation. academic_representation

Representation set for reasonably clean ‘academic’ plots that are based on output data generated in hdf5 files. Some basic plotting commands are provided for your utility, using matplotlib.pyplot We try to make some ‘sane’ choices on default options (colors, plot types, markers, axes layout) that are at least somewhat better than the matplotlib defaults. In any case, you are free to use the full matplotlib api to modify each plot afterwards to fully suit your needs. For more information about matplotlib see: https://matplotlib.org

Example usage:

import DEMutilities.postprocessing.representation.academic_representation as ar
import DEMutilities.postprocessing.h5storage as h5s

rep = ar.AcademicRepresentationSet()

reader = h5s.HdfFrameReader("simulation.h5")

#Assuming single value entries with name 'output_param1/2' exist in section 'results':
single_value1 = reader('results/output_param1')
single_value2 = reader('results/output_param2')

rep.bars( single_value1, single_value2)
rep.show()
rep.close()

#Assuming an entry with name 'lines' exists in section 'results':
lines = reader('results/lines')
rep.plot(lines)
#Save the figure to a '.png' file:
rep.savefig('my_figure.png')
rep.close()

#Assuming an entry with name 'output_data' exists in section 'results':
data = reader('results/output_data')
#Make a histogram of 'data':
rep.histogram( data )
rep.show()
rep.close()

#Make a heatmap of a 2-dimensional array stored in the entry 'image'
data = reader('results/image')
rep.heatmap( data )#Note that the x- and y-axis of 'data' are automatically consulted.
rep.show()
rep.close()

In order to be able to use this module import it like this:

import DEMutilities.postprocessing.representation.academic_representation
#or assign it to a shorter name
import DEMutilities.postprocessing.representation.academic_representation as aca

AcademicRepresentationSet

class DEMutilities.postprocessing.representation.academic_representation.AcademicRepresentationSet(cycle_cmap=None, N_colors=7, cycle_markers=None)

Bases: DEMutilities.postprocessing.representation.representationbase.RepresentationSetBase

Representation set for plotting data stored in hdf5 or compatible files in various ways.

Parameters:
  • cycle_map (string referring to a matplotlib.cm colormap (e.g. ‘jet’) or an actual matplotlib.color.Colormap object.) – (optional) color map that should be used as the default ‘cycler’ for determining the color of subsequent graphic elements (e.g. lines)
  • cycle_markers – List of markers to cycle between when plotting subsequent graphic elements, as understood e.g. by the matplotlib.pyplot.plot() function, e.g. [‘o’, ‘s’, ‘^’, ‘v’, ‘h’, ‘d’, ‘p’]
AcademicRepresentationSet(name, parent, **kwargs)
bars(*args, **kwargs)

From a list of data set entries containing positive single values, prepares a bar chart that plots bars for each ‘single’ value entry.

Note

To make labeling easier, the bars() uses by default horizontal bars (i.e. we use matplotlib.pyplot.barh()).

Parameters:
  • *args

    Any number of compatible hdf5 file object that contain a single value which can be extracted.

  • **kwargs

    Key word arguments passed to matplotlib.pyplot.barh() and the additional argument xscale which sets the scale of the x-axis to either ‘linear’ (default) or ‘log’.

close(**kwargs)

Close the current matplotlib.pyplot.figure object. Calling this between subsequent plots will make sure that new data is called in a new figure canvas.

errorbar(dsx, dsy, dsxerr=None, dsyerr=None, fontsize=16, **kwargs)

Make a errorbar plot of the data stored in dsy as a function of the data stored in dsx.

Parameters:
  • dsx (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted on the x-axis
  • dsy (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted on the y-axis
  • dsxerr (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted as horizontal errorbar
  • dsyerr (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted as vertical errorbar
  • **kwargs – Key word arguments passed to :func:`matplotlib.pyplot.errorbar’
  • fontsize (int) – Size of font for axis and legend labels. Default=16.
errorbar_series(ds, dsyerr=None, fontsize=16, swap_axes=True, mask=slice(None, None, None), **kwargs)

Ordered series of a two-dimensional data set. The values will determine the ‘y-axis’, one of the axes determines the ‘x-axis’ while the other axis determines the labels for each curve. By default, we plot the major axis of ds as the x-axis and the minor axis as labels, but this behavior can be switched by setting swap_axes to False.

Parameters:
  • ds (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object containing a 2-dimensional array. The axis of the data will be searched in the same data set under ds’s attributes axis_0 and axis_1. The latter should have been automatically set when the argument axes in the function add_data() from h5storage’s DataSection has been specified to add the data in the data set.
  • swap_axes (bool) – If True, the major axes in ds will be the x-axis and the minor axis the labels. If False, the minor axes in ds will be the x-axis and the major axis the labels.
  • dsyerr (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object containing a 2-dimensional array, data entry that will be plotted as vertical errorbar. The axis of the data will be searched in the same data set under ds’s attributes axis_0 and axis_1. The latter should have been automatically set when the argument axes in the function add_data() from h5storage’s DataSection has been specified to add the data in the data set.
  • mask (slice) – (optional) slice that will be used to only use a sub-selection of labels.
  • **kwargs – Key word arguments passed to :func:`matplotlib.pyplot.errorbar’
heatmap(ds, dsx=None, dsy=None, smooth=0.0, swap_axes=False, drawfunc='pcolormesh', show_colorbar=True, show_colorbar_title=True, show_xlabel=True, show_ylabel=True, show_xaxis=True, show_yaxis=True, set_xticks=None, set_yticks=None, set_xticklabels=None, set_yticklabels=None, invert_yaxis=False, nan_color='black', **kwargs)

Make a heat-map of a matrix of z values as a function of a one-dimensional x and y array.

Parameters:
  • ds (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object containing a 2-dimensional array
  • dsx (h5py.Group or any object with a similar interface.) – (optional) A group from an hdf5 file with a 1-dimensional array for the x-axis. If not given, the entry axis_0 from ds will be searched.
  • dsy (h5py.Group or any object with a similar interface.) – (optional) A group from an hdf5 file with a 1-dimensional array for the y-axis. If not given, the entry axis_1 from ds will be searched.
  • swap_axes (bool) – (optional) If True, the major axis in data will be the y-axis in the heatmap. If False, the major axis in data will be the x-axis in the heatmap.
  • **kwargs – Key word arguments passed to matplotlib.pyplot.pcolormesh(). Additionally, a ‘smooth’ key word argument can be specified that specifies the normalized degree of Gaussian smoothing that should be applied to the matrix in ds.
histogram(ds, xscale='linear', yscale='linear', ylabel='frequency (-)', **kwargs)

Simple histogram of a y array stored in a data set entry

Note

The default histogram is a cumulative one, which is in most cases strongly recommended as the result gives richer information and is not so strongly ‘manipulated’ by the choice of the histogram’s bins.

Parameters:
  • ds (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object
  • **kwargs – Key word arguments passed to matplotlib.pyplot.hist()
ordered_series(ds, swap_axes=True, mask=slice(None, None, None), xscale='linear', yscale='linear', **kwargs)

Ordered series of a two-dimensional data set. The values will determine the ‘y-axis’, one of the axes determines the ‘x-axis’ while the other axis determines the labels for each curve. By default, we plot the major axis of ds as the x-axis and the minor axis as labels, but this behavior can be switched by setting swap_axes to False.

Parameters:
  • ds (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object containing a 2-dimensional array. The axis of the data will be searched in the same data set under ds’s attributes axis_0 and axis_1. The latter should have been automatically set when the argument axes in the function add_data() from h5storage’s DataSection has been specified to add the data in the data set.
  • swap_axes (bool) – If True, the major axes in ds will be the x-axis and the minor axis the labels. If False, the minor axes in ds will be the x-axis and the major axis the labels.
  • mask (slice) – (optional) slice that will be used to only use a sub-selection of labels.
  • **kwargs – Key word arguments passed to matplotlib.pyplot.plot()
  • xscale (str) – Scale of the x-axis. Set to either ‘linear’ or ‘log’
  • yscale (str) – Scale of the y-axis. Set to either ‘linear’ or ‘log’
pairs(*args, **kwargs)

Pairs plot showing a given set of arrays againts each other. This plot is similar to the default plotting of a data set in R, and allows for a quick visual inspection of any ordered or unordered parameter study. For each sub-plot, the method scatter() is called.

Parameters:
  • *args – List of groups from an hdf5 file (or any object with a similar interface)
  • **kwargs – Any key word argument passed to scatter().
pie(ds, labels=[''], unit='', startangle=0.0, counterclock=False, legend=True, **kwargs)
plot(dsy, dsx=None, xscale='linear', yscale='linear', ylim=None, ymin=None, ymax=None, legend_fontsize=16, **kwargs)

Simple one-dimensional plot

Parameters:
  • dsy (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object that will be plotted as the dependent variable (y-axis).
  • dsx – (optional) group from an hdf5 file object that will be plotted as the independent variable (x-axis). If not given, we will first search for the attribute ‘axis_0’ in dsy. If the latter cannot be found. The x-axis will contain the array index of each plotted points.
  • **kwargs

    Key word arguments passed to matplotlib.pyplot.plot()

  • xscale (str) – Scale of the x-axis. Set to either ‘linear’ or ‘log’
  • yscale (str) – Scale of the y-axis. Set to either ‘linear’ or ‘log’

Default key word arguments:

  • ms (marker size): 8
  • ls (line style): -
  • mfc (marker face color): 'None'
  • mew (marker edge width): 1
  • color: last color of class’ color cycler
  • marker: Last marker of class’ marker cycler
  • legend: True
  • label: None (If not user-specified, a label is searched in the dsy data entry)
sankey(ds, patchlabel='', orientations=None, labels='', trunklength=1.0, pathlengths=0.25, prior=None, connect=(0, 0), rotation=0, facecolor='b', **kwargs)
savefig(figname, dpi=300, bbox_inches='tight', **kwargs)

Save figure using pyplot savefig() backend, with some sane defaults to create high-quality figures.

Parameters:
  • figname (str) – name of the figure
  • dpi (int) – density of pixels per inch. Recommended: 300+
  • bbox_inches – specification of figure bounding box. Recommended: ‘tight’
scatter(dsx, dsy, s=20, c=None, xscale='linear', yscale='linear', fontsize=16, **kwargs)

Make a scatter plot of the data stored in dsy as a function of the data stored in dsx.

Parameters:
  • dsx (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted on the x-axis
  • dsy (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object with a data entry that will be plotted on the y-axis
  • s – Sizes of the scattered points. Can be either 1) a group from an hdf5 file object with a data entry, 2) a single integer specifying the size of the scattered symbols or 3) a list of integers specifying the size of the scattered point of the same size as the data in dsx and dsy.
  • c – Colors of the scattered points. Can be either 1) a group form an hdf5 file object with a data entry with colors (e.g. ‘red’, ‘r’ or rgb: (1,0,0)), 2) a single matplotlib color specifier, or 3) a list of colors of the same size as the data in dsx and dsy.
  • **kwargs – Key word arguments passed to matplotlib.pyplot.scatter()
  • xscale (str) – Scale of the x-axis. Set to either ‘linear’ or ‘log’
  • yscale (str) – Scale of the y-axis. Set to either ‘linear’ or ‘log’
  • fontsize (int) – Size of font for axis and legend labels. Default=16.
tabulate(*args, **kwargs)

From a list of data set entries containing 1D data, make a nice data table that lists the results in an organized manner

Parameters:
  • *args

    Any number of compatible hdf5 file object that contain ‘y’ data which can be extracted.

  • **kwargs

    Key word arguments.

Key word arguments:
  • colwidth (int): Padding width of each column in characters
  • indexing (bool): If True, the first column is an enumeration
  • fmt (str): string format to be used for floats and ints (default = ‘%1.2g’)
  • crop_common (bool): If True, string-variables will have their ‘common base’ cropped
  • mark_row (int): Row with given index will be colored (i.e. as a reference)
  • line_symbol (str): Symbol to be used to make the table’s frame (default=’-‘)
  • do_print (bool): If True (default), the result will already be printed to the screen
  • omit_equal_columns (bool): If True, columns with all identical values will be omitted
  • sort_with: Possible hdf5 data object (which might be an ‘arg’ as well) along which the data will be sorted
  • invert: If ‘True’, the sorting of the table will be inverted.
  • select: Optionally, give a slice to selected entries in the table (e.g. slice(None,None,2) for every other value)
value(ds)

Simply print a single value data set entry specified in the argument ‘ds’

Parameters:ds (h5py.Group or any object with a similar interface.) – A group from an hdf5 file object
DEMutilities.postprocessing.representation.academic_representation.draw_line(N, symbol)
DEMutilities.postprocessing.representation.academic_representation.format_number(val)
DEMutilities.postprocessing.representation.academic_representation.has_set_data(ax)
DEMutilities.postprocessing.representation.academic_representation.make_axlab(varname, unitname)
DEMutilities.postprocessing.representation.academic_representation.make_leglab(varname, value, unitname)
DEMutilities.postprocessing.representation.academic_representation.remove_axis(ax, delete_frame=True)
DEMutilities.postprocessing.representation.academic_representation.sensible_axis_limits(datalims, overshoot=0.05, scale='linear')
DEMutilities.postprocessing.representation.academic_representation.smooth_image(image, sigma=0)
DEMutilities.postprocessing.representation.academic_representation.strfmt(val, floatfmt)