cytoflow.operations.xform_stat#

Transforms a statistic. xform_stat has one class:

TransformStatisticOp – apply a function to a statistic, making a new statistic.

class cytoflow.operations.xform_stat.TransformStatisticOp[source]#

Bases: HasStrictTraits

Apply a function to a feature of a statistic, creating a new statistic.

If you set by, then calling apply will group the input statistic by unique combinations of the conditions in by, then call function on the column specified by feature in each group. The function should take a pandas.Series and it can return a float, a value that can be cast to a float, or pandas.Series whose dtype is a floating-point.

If function returns a float, then the resulting statistic will have one column with the name set to feature and levels that are the same as the conditions in by.

If function returns a pandas.Series, then the names of the rows will become the names of the columns in the new statistic and the levels will be the same as the conditions in by.

Note

If function returns a pandas.Series, it must have an index with only one level – no hierarchical indexing, please!

Note

If function returns a pandas.Series, it must return a series with the same index each time!

Finally, if by is left empty, then function must be a transformation. function must take a pandas.Series as an argument and return a pandas.Series with exactly the same index. The new statistic will contain that pandas.Series as its only column, with the column name set to feature.

name#

The operation name. Becomes the name of the new statistic.

Type:

Str

statistic#

The statistic to apply function to.

Type:

Str

feature#

The feature to apply function to.

Type:

Str

function#

The function used to transform the statistic. function must take a pandas.Series as its only parameter and return a float, a value that can be cast to float, or a pandas.Series whose dtype is float.

Type:

Callable

by#

A list of metadata attributes to aggregate the input statistic before applying the function. For example, if the statistic has two indices Time and Dox, setting by = ["Time", "Dox"] will apply function separately to each subset of the data with a unique combination of Time and Dox.

Type:

List(Str)

ignore_incomplete_groups#

Sometimes, a statistic doesn’t have a row for every possible group of labels. If this flag is true, groups that don’t have all possible labels of the non-grouped levels won’t have function called – this can make writing function easier, at the cost of losing some data.

Type:

Bool (default = False)

Examples

Make a little data set.

>>> import cytoflow as flow
>>> import pandas as pd
>>> import numpy as np
>>> import_op = flow.ImportOp()
>>> import_op.tubes = [flow.Tube(file = "Plate01/RFP_Well_A3.fcs",
...                              conditions = {'Dox' : 10.0}),
...                    flow.Tube(file = "Plate01/CFP_Well_A4.fcs",
...                              conditions = {'Dox' : 1.0})]
>>> import_op.conditions = {'Dox' : 'float'}
>>> ex = import_op.apply()

Create and parameterize the operation.

>>> ch_op = flow.ChannelStatisticOp(name = 'MeanByDox',
...                                 channel = 'Y2-A',
...                                 function = flow.geom_mean,
...                                 by = ['Dox'])
>>> ex2 = ch_op.apply(ex)

View the new statistic

>>> print(ex2.statistics.keys())
dict_keys(['MeanByDox'])
>>> print(ex2.statistics['MeanByDox'])
            Y2-A
Dox
1.0    19.805601
10.0  446.981927

Transform the statistic

>>> xform_op = flow.TransformStatisticOp(name = 'LogMean',
...                                      statistic = 'MeanByDox',
...                                      feature = 'Y2-A',
...                                      function = np.log)
>>> ex_3 = xform_op.apply(ex2)
>>> ex_3.statistics['LogMean']
    Y2-A
Dox
1.0    2.985965
10.0    6.102518
apply(experiment)[source]#

Applies function to a statistic.

Parameters:

experiment (Experiment) – The Experiment to apply the operation to

Returns:

The same as the old experiment, but with a new statistic that results from applying function to the statistic specified in statistic.

Return type:

Experiment