View: A colored 2d View on Data.¶
-
class
colorview2d.
View
(data=None, cfgfile=None, config=None, pipeline=None)[source]¶ A class to handle a 2d
numpy.ndarray
with (linearly scaled) _axes, apply a (extendable) range of filters (mods) to the data while keeping track of the modifications.Hosts a
matplotlib.pyplot.Figure
of the data. Customization of this figure is simplified with respect to the matplotlib library. Provides interactive colorbar controls.Undocumented methods: The class provides methods that are not documented because they are generated on-init.
add_<Modname>(arg1, ...)
andrm_<Modname>()
.There is one such method for each mod in
modlist
. This simplfies callsadd_mod(<Modname>, (arg1, ...))
.
set_<Parametername>(Parameter)
as shortcut toView.config[<Parametername>] = Parameter
.
Example: data = colroview2d.Data(np.random.random((100, 100))) fig = colorview2d.View(data) fig.add_Smooth(2, 2) fig.plot_pdf('Test.pdf')
-
modlist
¶ A (autogenerated) list of all mods that can be found in the mods/ subfolder.
-
data
¶ A
colorview2d.Data
. It encapsulates the 2d data.
-
config
¶ Holds information on the plot layout, ticks, fonts etc. Can be accessed via
myview.set_<Parametername>(<Parameter>)
and in a dict-like fashionmyview.config['Parametername'] = <Parameter>
. Also anconfig.update(dict)
function is available. The attribute is initialized with a fixed set of parameters read fromdefault.cv2d
config file in the package directory.Important: Does not fully implement a dictionary interface.
-
update
(dict)¶ update the configuration with a dictionary containing valid parameters. Note that the plot, if there is any, is updated when the config is changed via
update
-
update_raw
(dict)¶ update the configuration without updating any existing plot.
-
-
fig
¶ The
matplotlib.pyplot.figure
.
-
pipeline
¶ A dictionary with mod identifiers (strings) and their arguments (tuples).
-
plotting
¶ Boolean. Are we showing any plot at the moment?
-
show_plt_fig
()[source]¶ Show two interactive
matplotlib.pyplot.Figure
plots. The first displays the data with config and pipeline applied. The second provides two matplotlib slider widgets to control the limits of the colorbar interactively and a Reset button to apply the default (full-range) colorbar limits.
-
add_mod
(modname, modargs=(), pos=-1, do_apply=True)[source]¶ Add a mod to the pipeline by its title string and its arguments either to the end of the pipeline or at a specified postion.
Parameters: - modname (string) – The type of the mod.
- modargs (tuple) – A tuple containing the arguments of the mod.
- pos (int) – Where to add the mod in the pipeline. Default is last.
- do_apply (boolean) – Trigger modification of the data (True) or just add mod to the pipeline.
-
remove_mod
(modtype=None, pos=-1, do_apply=True)[source]¶ Removes the last mod from the pipeline, or the mod at position pos or the last mod in the pipeline with the type modtype.
Parameters: - modtype (string) – The identifier of the mod type.
- pos (int) – The position of the mod in the pipeline.
- do_apply (bool) – Is the pipeline applied after the element is removed?
-
replace_data
(newdata)[source]¶ Replace the data.
In contrast to calling
myview.data = newdata
, the method replace_data also replaces the copy of the raw data.Warning: Some modifications may not be applicable to the new data.
Parameters: newdata ( colorview2d.Data
) – the new data.
-
load_config
(cfgpath)[source]¶ Load the configuration and the pipeline from a config file specified in the YAML format.
Parameters: cfgpath (string) – The path to a cv2d configuration file.
-
save_config
(cfgpath)[source]¶ Save the configuration and the pipeline to a config file.
Parameters: cfgpath (string) – the path to the config file
-
draw_plot
()[source]¶ (Re-)draw the
matplotlib.pyplot.figure
.This method is intended to be used directly when you are only interested in the figure object itself which can be obtained by
myview.fig
attribute.It includes an axes object containing the (imshow generated) 2d color plot with labels, ticks and colorbar as specified in the config dictionary.
Data: A container for the array and the axes bounds.¶
-
class
colorview2d.
Data
(data, range_bounds=None)[source]¶ Data
hosts, well, the data and its axes.Data is stored in a 2d
numpy-ndarray
. For the axes, only the bounds are stored. We assume linear scaling of the axes. If no bounds are specified, we use(0, n)
as boundaries,n
being the number of rows and columns, respectively.-
xleft
¶ Right boundary value of the x-axis.
-
xright
¶ Left boundary value of the x-axis.
-
xmin
¶ Minimum value of the x-axis range.
-
xmax
¶ Maximum value of the x-axis range.
-
dx
¶ Spacing of x-axis values.
-
ytop
¶ Top boundary value of the y-axis.
-
ybottom
¶ Bottom boundary value of the y-axis.
-
ymin
¶ Minimum value of the y-axis range.
-
ymax
¶ Maximum value of the y-axis range.
-
dy
¶ Spacing of y-axis values.
-
zmin
¶ Minimum value of the 2d
numpy.ndarray
.
-
zmax
¶ Maximum value of the 2d
numpy.ndarray
.
-
xwidth
¶ Size of the array along the x-axis.
-
ywidth
¶ Size of the array along the y-axis.
-
zdata
¶ 2d
numpy.ndarray
.
-
y_range
¶ A linear y-range array.
-
x_range
¶ A linear x-range array.
-
xrange_bounds
¶ Boundary values on the x-axis as a tuple (left, right).
-
yrange_bounds
¶ Boundary values on the y-axis as a tuple (bottom, top).
-
deep_copy
()[source]¶ Deep copy the
colorview2d.Data
object and return the copy.Returns: A copy of the Colorview2d.Data
instance.
-
is_within_bounds
(coordinate)[source]¶ Check if the given coordinate (y, x) is within the ranges of the axes.
Returns: a boolean.
-
crop
(boundaries)[source]¶ Crop the data to a subset of the array specifiying the corners of the subset in units of the axes ranges.
Parameters: boundaries (tuple) – (bottom boundary, top boundary, left boundary, right boundary)
-
x_range_idx_by_val
(value)[source]¶ Return the nearest index of a value within the x axis range.
Parameters: value – A value in the range of the x axis Returns: The closest index on the x axis range.
-
y_range_idx_by_val
(value)[source]¶ Return the nearest index of a value within the y axis range.
Parameters: value – A value in the range of the y axis Returns: The closest index on the y axis range.
-
idx_by_val_coordinate
(coordinate)[source]¶ Return the nearest index pair for a coordinate pair (y, x) along the two axes.
Parameters: coordinate (tuple) – y-axis value, x-axis value (inverse order!) Returns: (y-axis index, x-axis index) – both integer
-
extract_ylinetrace
(xval, ystartval, ystopval)[source]¶ Extract a linetrace along a given y-axis range vor a specific value on the x axis.
Parameters: - xval (float) – Position of the linecut along the x-axis.
- ystartval (float) – First and ...
- ystopval (float) – last value of the range along the y-axis.
Returns: numpy array with two rows [0] linecutdata [1] y-axis range
-
extract_xlinetrace
(yval, xstartval, xstopval)[source]¶ Extract a linetrace along a given y-axis range vor a specific value on the x axis.
Parameters: - yval (float) – Position of the linecut along the y-axis.
- xstartval (float) – Start and ...
- xstopval (float) – stop value of the range along the x-axis.
Returns: numpy array with two rows [0] linecutdata [1] x-axis range
-
extract_ylinetrace_series
(x_first, x_last, x_interval, ystart, ystop)[source]¶ Extract linetraces along a given y-axis range for values on the x axis within a given range and separated by a given interval.
Parameters: - x_first (float) – value on the x-axis for the first line trace in the series.
- x_last (float) – value on the x-axis for the last line trace in the series.
- x_interval (float) – the (positive) interval between two linecuts on the x-axis.
- ystart (float) – Start and ...
- ystop (float) – stop value of the range along the y-axis.
Returns: a numpy array with n + 1 rows with the length equal to the y-dimensions of zdata. n is the number of linecuts, i.e., abs(x_last - x_first) / x_interval. The last row contains the y-axis range.
-
extract_xlinetrace_series
(y_first, y_last, y_interval, xstart, xstop)[source]¶ Extract linetraces along a given x-axis range for values on the y axis within a given range and separated by a given interval.
Parameters: - y_first (float) – value on the y-axis for the first line trace in the series.
- y_last (float) – value on the y-axis for the last line trace in the series.
- y_interval (float) – the (positive) interval between two linecuts on the y-axis.
- xstart (float) – Start and ...
- xstop (float) – stop value of the range along the x-axis.
Returns: a numpy array with n + 1 rows with the length equal to the x-dimensions of zdata. n is the number of linecuts, i.e., abs(y_last - y_first) / y_interval. The last row contains the x-axis range.
-
extract_arbitrary_linetrace
(coordinate_one, coordinate_two)[source]¶ Extract a linetrace between two arbitrary points.
Parameters: - coordinate_one (tuple) – coordinate in the coordinate system of the axis. The order is (yval, xval)!
- coordinate_two (tuple) – coordinates in the coordinate system of the x and y axes. The order is (yval, xval)!
Returns: Array with the linetrace. No axis range is supplied since it does not make sense along any arbitrary direction.
-
resize
(new_ywidth, new_xwidth, order=1)[source]¶ Interpolate the array to a new, larger size.
Uses scipy.misc.imresize. The ranges are interpolated accordingly.
Parameters: - new_ywidth (int) – new dimensions along the y-axis.
- new_xwidth (int) – new dimensions along the x-axis.
- order (int) – order of the interpolation. See
scipy.misc.imresize()
-
Configuration¶
The colorview2d.View
class has a config attribute.
The parameters stored in its dictionary are
read from colorview2d/default.cv2d
config file.
# Config file for colorview2d (YAML)
---
# Default colormap
# Print a list of available fonts with colorview2d.utils.colormaplist()
Colormap: jet
# Minimum and maximum for the colorbar
Cbmin: auto
Cbmax: auto
# Labels for the x- and y-axis
Xlabel: x-axis
Ylabel: y-axis
# Label for the colorbar
Cblabel: colorscale
# Format of ticklabels
# Use c-style printf format
# e.g. %.2f for two digits after the point
Xtickformat: auto
Ytickformat: auto
Cbtickformat: auto
# Fontstyle and size
# Print a list of available fonts with colorview2d.utils.fontlist()
Font: default
Fontsize: 12
# Ticksize for x- and y-axis
Xticklength: 4
Yticklength: 4
# Linewidth of lines that are drawn in the plot
Linewidth: 1
# Default Size of the plot and dpi
# Note that these are *only* applied when the plot is printed to pdf.
Width: 8
Height: 6
Dpi: 80