class eelbrain.Factor(x, name=None, random=False, repeat=1, tile=1, labels=None, default=None)

Container for categorial data.

x : iterator

Sequence of Factor values (see also the labels kwarg).

name : str

Name of the Factor.

random : bool

Treat Factor as random factor (for ANOVA; default is False).

repeat : int | array of int

repeat each element in x, either a constant or a different number for each element.

tile : int

Repeat x as a whole tile many times.

labels : dict

An optional dictionary mapping values as they occur in x to the Factor’s cell labels.

default : str

Label to assign values not in label (by default this is str(value)).


The most obvious way to initialize a Factor is a list of strings:

>>> Factor(['in', 'in', 'in', 'out', 'out', 'out'])
Factor(['in', 'in', 'in', 'out', 'out', 'out'])

The same can be achieved with a list of integers plus a labels dict:

>>> Factor([1, 1, 1, 0, 0, 0], labels={1: 'in', 0: 'out'})
Factor(['in', 'in', 'in', 'out', 'out', 'out'])

Or more parsimoniously:

>>> Factor([1, 0], labels={1: 'in', 0: 'out'}, repeat=3)
Factor(['in', 'in', 'in', 'out', 'out', 'out'])

Since the Factor initialization simply iterates over the x argument, a Factor with one-character codes can also be initialized with a single string:

>>> Factor('iiiooo')
Factor(['i', 'i', 'i', 'o', 'o', 'o'])
.name : None | str

The Factor’s name.

.cells : tuple of str

Ordered names of all cells. Order is determined by the order of the labels argument. if labels is not specified, the order is initially alphabetical.

.random : bool

Whether the factor represents a random or fixed effect (for ANOVA).


aggregate(self, x[, name]) Summarize the Factor by collapsing within cells in x.
as_labels(self) Convert the Factor to a list of str
as_var(self, labels[, default, name]) Convert into a Var
copy(self[, name, repeat, tile]) A deep copy
count(self, value[, start]) Cumulative count of the occurrences of value
endswith(self, substr) An index that is true for all cases whose name ends with substr
enumerate_cells(self[, name]) Enumerate the occurrence of each cell value throughout the data
floodfill(self, regions[, empty]) Fill in empty regions in a Factor from the nearest non-empty value
get_index_to_match(self, other) Generate index to conform to another Factor’s order
index(self, cell) Array with int indices equal to cell
index_opt(self, cell) Find an optimized index for a given cell.
isany(self, *values) Find the index of entries matching one of the *values
isin(self, values) Find the index of entries matching one of the values
isnot(self, *values) Find the index of entries not in values
isnotin(self, values) Find the index of entries not in values
label_length(self[, name]) Create Var with the length of each label string
repeat(self, repeats[, name]) Repeat each element repeats times
sort_cells(self, order) Reorder the cells of the Factor (in-place)
sort_index(self[, descending, order]) Create an index that could be used to sort this data_object.
startswith(self, substr) An index that is true for all cases whose name starts with substr
table_categories(self) A table containing information about categories
tile(self, repeats[, name]) Construct a Factor by repeating self repeats times
update_labels(self, labels) Change one or more labels in place