cytoflow.views.base_views¶
Base classes for views.
I found, as I wrote a bunch of views, that I was also writing a bunch of shared boiler-plate code. This led to more bugs and a harder-to-maintain codebase. So, I extracted the copied code in a short hierarchy of reusable base classes:
BaseView
– implements a view with row, column and hue facets.
After setting up the facet grid, it calls the derived class’s
_grid_plot
to actually do the plotting. BaseView.plot
also
has parameters to set the plot style, legend, axis labels, etc.
BaseDataView
– implements a view that plots an Experiment
’s
data (as opposed to a statistic.) Includes functionality for subsetting
the data before plotting, and determining axis limits and scales.
Base1DView
– implements a 1-dimensional data view. See
HistogramView
for an example.
Base2DView
– implements a 2-dimensional data view. See
ScatterplotView
for an example.
BaseNDView
– implements an N-dimensional data view. See
RadvizView
for an example.
BaseStatisticsView
– implements a view that plots a statistic from
an Experiment
(as opposed to the underlying data.) These views
have a “primary” BaseStatisticsView.variable
, and can be subset
as well.
Base1DStatisticsView
– implements a view that plots one dimension
of a statistic. See BarChartView
for an example.
Base2DStatisticsView
– implements a view that plots two dimensions
of a statistic. See Stats2DView
for an example.
- class cytoflow.views.base_views.BaseView[source]¶
Bases:
traits.has_traits.HasStrictTraits
The base class for facetted plotting.
- xfacet¶
Set to one of the
Experiment.conditions
in theExperiment
, and a new column of subplots will be added for every unique value of that condition.- Type
String
- yfacet¶
Set to one of the
Experiment.conditions
in theExperiment
, and a new row of subplots will be added for every unique value of that condition.- Type
String
- huefacet¶
Set to one of the
Experiment.conditions
in the in theExperiment
, and a new color will be added to the plot for every unique value of that condition.- Type
String
- plot(experiment, data, **kwargs)[source]¶
Base function for facetted plotting
- Parameters
experiment (Experiment) – The
Experiment
to plot using this view.title (str) – Set the plot title
xlabel (str) – Set the X axis label
ylabel (str) – Set the Y axis label
huelabel (str) – Set the label for the hue facet (in the legend)
legend (bool) – Plot a legend for the color or hue facet? Defaults to
True
.sharex (bool) – If there are multiple subplots, should they share X axes? Defaults to
True
.sharey (bool) – If there are multiple subplots, should they share Y axes? Defaults to
True
.row_order (list) – Override the row facet value order with the given list. If a value is not given in the ordering, it is not plotted. Defaults to a “natural ordering” of all the values.
col_order (list) – Override the column facet value order with the given list. If a value is not given in the ordering, it is not plotted. Defaults to a “natural ordering” of all the values.
hue_order (list) – Override the hue facet value order with the given list. If a value is not given in the ordering, it is not plotted. Defaults to a “natural ordering” of all the values.
height (float) – The height of each row in inches. Default = 3.0
aspect (float) – The aspect ratio of each subplot. Default = 1.5
col_wrap (int) – If
xfacet
is set andyfacet
is not set, you can “wrap” the subplots around so that they form a multi-row grid by setting this to the number of columns you want.sns_style ({“darkgrid”, “whitegrid”, “dark”, “white”, “ticks”}) – Which
seaborn
style to apply to the plot? Default iswhitegrid
.sns_context ({“paper”, “notebook”, “talk”, “poster”}) – Which
seaborn
context to use? Controls the scaling of plot elements such as tick labels and the legend. Default istalk
.palette (palette name, list, or dict) – Colors to use for the different levels of the hue variable. Should be something that can be interpreted by
seaborn.color_palette
, or a dictionary mapping hue levels to matplotlib colors.despine (Bool) – Remove the top and right axes from the plot? Default is
True
.
- Other Parameters
cmap (matplotlib colormap) – If plotting a huefacet with many values, use this color map instead of the default.
norm (matplotlib.colors.Normalize) – If plotting a huefacet with many values, use this object for color scale normalization.
- class cytoflow.views.base_views.BaseDataView[source]¶
Bases:
cytoflow.views.base_views.BaseView
The base class for data views (as opposed to statistics views).
- subset¶
An expression that specifies the subset of the statistic to plot. Passed unmodified to
pandas.DataFrame.query
.- Type
- plot(experiment, **kwargs)[source]¶
Plot some data from an experiment. This function takes care of checking for facet name validity and subsetting, then passes the underlying dataframe to
BaseView.plot
- Parameters
min_quantile (float (>0.0 and <1.0, default = 0.001)) – Clip data that is less than this quantile.
max_quantile (float (>0.0 and <1.0, default = 1.00)) – Clip data that is greater than this quantile.
- Other Parameters
lim (Dict(Str : (float, float))) – Set the range of each channel’s axis. If unspecified, assume that the limits are the minimum and maximum of the clipped data. Required.
scale (Dict(Str : IScale)) – Scale the data on each axis. Required.
- class cytoflow.views.base_views.Base1DView[source]¶
Bases:
cytoflow.views.base_views.BaseDataView
A data view that plots data from a single channel.
- channel¶
The channel to view
- Type
Str
- scale¶
The scale applied to the data before plotting it.
- Type
{‘linear’, ‘log’, ‘logicle’}
- class cytoflow.views.base_views.Base2DView[source]¶
Bases:
cytoflow.views.base_views.BaseDataView
A data view that plots data from two channels.
- xchannel¶
The channel to view on the X axis
- Type
Str
- ychannel¶
The channel to view on the Y axis
- Type
Str
- xscale¶
The scales applied to the
xchannel
data before plotting it.- Type
{‘linear’, ‘log’, ‘logicle’} (default = ‘linear’)
- class cytoflow.views.base_views.BaseNDView[source]¶
Bases:
cytoflow.views.base_views.BaseDataView
A data view that plots data from one or more channels.
- channels¶
The channels to view
- Type
List(Str)
- scale¶
Re-scale the data in the specified channels before plotting. If a channel isn’t specified, assume that the scale is linear.
- Type
Dict(Str : {“linear”, “logicle”, “log”})
- class cytoflow.views.base_views.BaseStatisticsView[source]¶
Bases:
cytoflow.views.base_views.BaseView
The base class for statistics views (as opposed to data views).
- variable¶
The condition that varies when plotting this statistic: used for the x axis of line plots, the bar groups in bar plots, etc.
- Type
- subset¶
An expression that specifies the subset of the statistic to plot. Passed unmodified to
pandas.DataFrame.query
.- Type
- enum_plots(experiment, data)[source]¶
Enumerate the named plots we can make from this set of statistics.
- Returns
An iterator across the possible plot names. The iterator ALSO has an instance attribute called
by
, which holds a list of the facets that are not yet set (and thus need to be specified in the plot name.)- Return type
iterator
- plot(experiment, data, plot_name=None, **kwargs)[source]¶
Plot some data from a statistic.
This function takes care of checking for facet name validity and subsetting, then passes the dataframe to
BaseView.plot
- Parameters
plot_name (str) – If this
IView
can make multiple plots,plot_name
is the name of the plot to make. Must be one of the values retrieved fromenum_plots
.
- class cytoflow.views.base_views.Base1DStatisticsView[source]¶
Bases:
cytoflow.views.base_views.BaseStatisticsView
The base class for 1-dimensional statistic views – ie, the
variable
attribute is on the x axis, and the statistic value is on the y axis.- statistic¶
The name of the statistic to plot. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- error_statistic¶
The name of the statistic used to plot error bars. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- scale¶
The scale applied to the data before plotting it.
- Type
{‘linear’, ‘log’, ‘logicle’}
- enum_plots(experiment)[source]¶
Enumerate the named plots we can make from this set of statistics.
- Returns
An iterator across the possible plot names. The iterator ALSO has an instance attribute called
by
, which holds a list of the facets that are not yet set (and thus need to be specified in the plot name.)- Return type
iterator
- class cytoflow.views.base_views.Base2DStatisticsView[source]¶
Bases:
cytoflow.views.base_views.BaseStatisticsView
The base class for 2-dimensional statistic views – ie, the
variable
attribute varies independently, and the corresponding values from the x and y statistics are plotted on the x and y axes.- xstatistic¶
The name of the statistic to plot on the X axis. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- ystatistic¶
The name of the statistic to plot on the Y axis. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- x_error_statistic¶
The name of the statistic used to plot error bars on the X axis. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- y_error_statistic¶
The name of the statistic used to plot error bars on the Y axis. Must be a key in the
Experiment.statistics
attribute of theExperiment
being plotted.
- xscale¶
The scale applied to
xstatistic
before plotting it.- Type
{‘linear’, ‘log’, ‘logicle’}
- yscale¶
The scale applied to
ystatistic
before plotting it.- Type
{‘linear’, ‘log’, ‘logicle’}
- enum_plots(experiment)[source]¶
Enumerate the named plots we can make from this set of statistics.
- Returns
An iterator across the possible plot names. The iterator ALSO has an instance attribute called
by
, which holds a list of the facets that are not yet set (and thus need to be specified in the plot name.)- Return type
iterator