duat.plot

Plot OSIRIS-generated data.

class duat.plot.Diagnostic(data_path)[source]

A OSIRIS diagnostic.

data_path

str – Path to the directory were the data is stored.

data_name

str – A friendly name for the data.

units

str – The name of the unit the magnitude is measured in.

dt

float – The time step between snapshots of a consecutive number.

t_0

float – The time of the first snapshot.

time_units

str – The name of the unit of time.

file_list

list of str – List of h5 files, one per time snapshot.

snapshot_list

list of int – List of integers identifying the snapshots. Multiply by dt to get time.

keys

list of str – Names of the datasets in the Diagnostic, given in human order.

axes

list of dict – Info of each axis in the Diagnostic.

datasets_as_axis

dict – Info of datasets if treated as axes. WARNING: Only used with energy bins.

shape

tuple – A tuple with:

  • list: The number of grid dimensions.
  • int: The number of datasets (excluding axes definition).
  • int: The number of snapshots in time.

Note

The axes list is provided in the order of the numpy convention for arrays. This is the opposite of order used to label the axes in the hdf5 files. For example in a 2d array the first axes will be the labeled as AXIS2, and the second will be AXIS1. Unless the user makes use of other external tools to read the data, he/she can safely ignore this note.

__init__(data_path)[source]

Create a Diagnostic instance.

Parameters:data_path (str) – Path of the directory containing the diagnostic data.
Raises:ValueError – If there is no data in data_path.
__repr__()[source]

Return repr(self).

axes_2d_colormap(output_path=None, dataset_selector=None, axes_selector=None, time_selector=None, dpi=200, latex_label=True, cmap=None, norm=None, show=True, rasterized=True, contour_plot=False)[source]

Generate a colormap in two axes.

A single time snapshot must be selected with the time_selector parameter. For an animated version in time see the time_2d_animation() method.

Note

For simple manipulation like labels or title you can make use of the returned tuple or a matplotlib.pyplot.style.context. More advanced manipulation can be done extracting the data with the get_generator() method instead.

Parameters:
  • output_path (str) – The place where the plot is saved. If “” or None, the figure is not saved.
  • dataset_selector – See get_generator() method.
  • axes_selector – See get_generator() method.
  • time_selector – See get_generator() method.
  • dpi (int) – The resolution of the file in dots per inch.
  • latex_label (bool) – Whether for use LaTeX code for the plot.
  • cmap (str or matplotlib.colors.Colormap) – The Colormap to use in the plot.
  • norm (str or matplotlib.colors.Normalize) –

    How to scale the colormap. For advanced manipulation, use some Normalize subclass, e.g., colors.SymLogNorm(0.01). Automatic scales can be selected with the following strings:

    • ”lin”: Linear scale from minimum to maximum.
    • ”log”: Logarithmic scale from minimum to maximum up to vmax/vmin>1E9, otherwise increasing vmin.
  • show (bool) – Whether to show the plot. This is blocking if matplotlib is in non-interactive mode.
  • rasterized (bool) – Whether the map is rasterized. This does not apply to axes, title… Note non-rasterized images with large amount of data exported to PDF might challenging to handle.
  • contour_plot (bool) – Whether contour lines are plot instead of the density map.
Returns:

Objects representing the generated plot.

Return type:

(matplotlib.figure.Figure, matplotlib.axes.Axes)

get_axes(dataset_selector=None, axes_selector=None)[source]

Get a dictionary with the info of the axes obtained as result of a given reduction.

Parameters:
  • dataset_selector – See get_generator() method.
  • axes_selector – See get_generator() method.
Returns:

Ordered list of the axes left by the reduction.

Return type:

list of dict

get_generator(dataset_selector=None, axes_selector=None, time_selector=None)[source]

Get a generator providing data from the file.

Calling this method returns a generator which, when called, will provide data for increasing times (unless modified by time_selector parameter). The data might be reduced either by selecting a position in an axis (or a dataset) or by using a function along some axis (or datasets), e.g., a sum.

This data is provided as numpy arrays where the first axis refers to dataset coordinate (if present) and next to (non-reduced) axis in the order they are found in the files.

Parameters:
  • dataset_selector (str, int or callable) – Instructions to reduce datasets. An int selects a dataset in human order, a str selects it by name. A function taking a list and returning a scalar can be used to reduce the data, e.g., sum, mean…
  • axes_selector (tuple) –

    Instructions to reduce axes data. It must be a tuple of the same length of the number axes or None to perform no reduction. Each element can be of the following types:

    • int: Select the item in the given position.
    • None: No reduction is performed in this axis.
    • ScaledFunction: A function applied in a range selected by simulation units.
    • callable (default): Reduce the data along this axes using the given function (e.g., mean, max, sum…).
  • time_selector (slice or ScaledSlice) – A slice or ScaledSlice instance selecting the points in time to take. A slice selects times from the list returned by get_time_list(). A ScaledSlice chooses a slice that best represents a choice in terms of time units.
Returns:

A generator which provides the data.

Return type:

generator

get_time_list(time_selector=None)[source]

Get the list of times obtained as a result of a given slice.

Parameters:time_selector – See get_generator() method.
Returns:The times resulting as a consequence of the slice.
Return type:list of float
time_1d_animation(output_path=None, dataset_selector=None, axes_selector=None, time_selector=None, dpi=200, fps=1, scale_mode='expand', latex_label=True, interval=200)[source]

Generate a plot of 1d data animated in time.

If an output path with a suitable extension is supplied, the method will export it. Available formats are mp4 and gif. The returned objects allow for minimal customization and representation. For example in Jupyter you might use IPython.display.HTML(animation.to_html5_video()), where animation is the returned FuncAnimation instance.

Note

Exporting a high resolution animated gif with many frames might eat your RAM.

Parameters:
  • output_path (str) – The place where the plot is saved. If “” or None, the plot is shown in matplotlib.
  • dataset_selector – See get_generator() method.
  • axes_selector – See get_generator() method.
  • time_selector – See get_generator() method.
  • interval (float) – Delay between frames in ms. If exporting to mp4, the fps is used instead to generate the file, although the returned objects do use this value.
  • dpi (int) – The resolution of the frames in dots per inch (only if exporting).
  • fps (int) – The frames per seconds (only if exporting to mp4).
  • scale_mode (str) –

    How the scale is changed through time. Available methods are:

    • ”expand”: The y limits increase when needed, but they don’t decrease.
    • ”adjust_always”: Always change the y limits to those of the data.
    • ”max”: Use the maximum range from the beginning.
  • latex_label (bool) – Whether for use LaTeX code for the plot.
Returns:

Objects representing the generated plot and its animation.

Return type:

(matplotlib.figure.Figure, matplotlib.axes.Axes, matplotlib.animation.FuncAnimation)

Raises:

FileNotFoundError – If tried to export to mp4 but ffmpeg is not found in the system.

time_1d_colormap(output_path=None, dataset_selector=None, axes_selector=None, time_selector=None, dpi=200, latex_label=True, cmap=None, norm=None, show=True, rasterized=True, contour_plot=False)[source]

Generate a colormap in an axis and the time.

This function plots a magnitude depending on ONE spatial coordinate (hence the name) and on time as a colormap in the cartesian product of such a magnitude and the time.

Note

For simple manipulation like labels or title you can make use of the returned tuple or a matplotlib.pyplot.style.context. More advanced manipulation can be done extracting the data with the get_generator() method instead.

Parameters:
  • output_path (str) – The place where the plot is saved. If “” or None, the figure is not saved.
  • dataset_selector – See get_generator() method.
  • axes_selector – See get_generator() method.
  • time_selector – See get_generator() method.
  • dpi (int) – The resolution of the file in dots per inch.
  • latex_label (bool) – Whether for use LaTeX code for the plot.
  • cmap (str or matplotlib.colors.Colormap) – The Colormap to use in the plot.
  • norm (str or matplotlib.colors.Normalize) –

    How to scale the colormap. For advanced manipulation, use some Normalize subclass, e.g., colors.SymLogNorm(0.01). Automatic scales can be selected with the following strings:

    • ”lin”: Linear scale from minimum to maximum.
    • ”log”: Logarithmic scale from minimum to maximum up to vmax/vmin>1E9, otherwise increasing vmin.
  • show (bool) – Whether to show the plot. This is blocking if matplotlib is in non-interactive mode.
  • rasterized (bool) – Whether the map is rasterized. This does not apply to axes, title… Note non-rasterized images with large amount of data exported to PDF might challenging to handle.
  • contour_plot (bool) – Whether contour lines are plot instead of the density map.
Returns:

Objects representing the generated plot.

Return type:

(matplotlib.figure.Figure, matplotlib.axes.Axes)

time_2d_animation(output_path=None, dataset_selector=None, axes_selector=None, time_selector=None, dpi=200, fps=1, cmap=None, norm=None, rasterized=True, z_min=None, z_max=None, latex_label=True, interval=200)[source]

Generate a plot of 2d data as a color map which animated in time.

If an output path with a suitable extension is supplied, the method will export it. Available formats are mp4 and gif. The returned objects allow for minimal customization and representation. For example in Jupyter you might use IPython.display.HTML(animation.to_html5_video()), where animation is the returned FuncAnimation instance.

Note

Exporting a high resolution animated gif with many frames might eat your RAM.

Parameters:
  • output_path (str) – The place where the plot is saved. If “” or None, the plot is shown in matplotlib.
  • dataset_selector – See get_generator() method.
  • axes_selector – See get_generator() method.
  • time_selector – See get_generator() method.
  • interval (float) – Delay between frames in ms. If exporting to mp4, the fps is used instead to generate the file, although the returned objects do use this value.
  • dpi (int) – The resolution of the frames in dots per inch (only if exporting).
  • fps (int) – The frames per seconds (only if exporting to mp4).
  • latex_label (bool) – Whether for use LaTeX code for the plot.
  • cmap (str or matplotlib.colors.Colormap) – The Colormap to use in the plot.
  • norm (str or matplotlib.colors.Normalize) –

    How to scale the colormap. For advanced manipulation, use some Normalize subclass, e.g., colors.SymLogNorm(0.01). Automatic scales can be selected with the following strings:

    • ”lin”: Linear scale from minimum to maximum.
    • ”log”: Logarithmic scale from minimum to maximum up to vmax/vmin>1E9, otherwise increasing vmin.
  • rasterized (bool) – Whether the map is rasterized. This does not apply to axes, title… Note non-rasterized images with large amount of data exported to PDF might challenging to handle.
Returns:

Objects representing the generated plot and its animation.

Return type:

(matplotlib.figure.Figure, matplotlib.axes.Axes, matplotlib.animation.FuncAnimation)

Raises:

FileNotFoundError – If tried to export to mp4 but ffmpeg is not found in the system.

class duat.plot.ScaledFunction(f, start, stop)[source]

A function that applies to a range defined in simulation units (instead of list position).

This object can be used to describe a component of an axes_selector parameter.

__init__(f, start, stop)[source]

Create a ScaledFunction instance.

Parameters:
  • f (Callable) – The function to be applied to the range, e.g., a sum.
  • start (float) – Where the application range should start. Actual start will be before if needed to include the given point
  • stop (float) – Where the slice should stop. Actual stop will be after if needed to include the given point.
__repr__()[source]

Return repr(self).

class duat.plot.ScaledSlice(start, stop, step=None)[source]

A slice described in simulation units (instead of list position).

This object can be used to describe a time_selector parameter.

__init__(start, stop, step=None)[source]

Create a ScaledSlice instance.

Parameters:
  • start (float) – Where the slice should start. Actual start will be before if needed.
  • stop (float) – Where the slice should stop. The point is in general excluded, as usual in Python.
  • step (float) – The desired step of the slice. Actual step will be the biggest multiple of the mesh step which is less than this one.
__repr__()[source]

Return repr(self).

duat.plot.get_diagnostic_list(run_dir='.')[source]

Create a list with the diagnostic found in the given run directory.

Parameters:run_dir (str) – The run directory.
Returns:List of the diagnostic found.
Return type:list of Diagnostic