fitgrid.fitgrid module

Developer classes for populating Time x Channel grids with fitted OLS and LMER models, not meant for general use. Instead, construct an Epochs object and pass it to fitgrid.lm and fitgrid.lmer.

class fitgrid.fitgrid.FitGrid(_grid, epoch_index, time)[source]

Bases: object

Hold rERP fit objects.

FitGrid should not be created directly, the right way to build it is to start with an Epochs object and pass it to a function like fitgrid.lm.

Parameters
  • _grid (Pandas DataFrame) – Pandas DataFrame of fit objects

  • epochs_index (pandas Index) – index containing epoch ids

  • time (str) – time column name

Returns

grid – FitGrid object

Return type

FitGrid

Notes

Slicing FitGrids is a little different than slicing Pandas DataFrames. For instance, we require that the keys in a list used to slice a FitGrid on time or channels be unique. The following Pandas quirk is inherited by FitGrids: slicing using a list where some keys are present but some are missing from a grid succeeds silently and creates columns with the missing keys as names. For example, if you have a grid with columns

‘channel1’, ‘channel2’

and you slice:

grid[:, [‘channel1’, ‘blah’]]

this returns a new grid with columns

‘channel1’, ‘blah’

where column ‘blah’ consists of NaN’s. Since version 21.0 of Pandas this throws a warning and should in future be replaced with a KeyError.

add_epoch_index(temp)[source]

We assume that temp is in long form, the columns are channels, and the first index level is time.

expand_series_or_df(temp)[source]

Expand a DataFrame that has Series or DataFrames for values.

save(filename)[source]

Save FitGrid object to file (reload with fitgrid.load_grid).

Parameters

filename (str) – file name to use

class fitgrid.fitgrid.LMERFitGrid(_grid, epoch_index, time)[source]

Bases: fitgrid.fitgrid.FitGrid

class fitgrid.fitgrid.LMFitGrid(_grid, epoch_index, time)[source]

Bases: fitgrid.fitgrid.FitGrid

influential_epochs(top=None)[source]

Return dataframe with top influential epochs ranked by Cook’s-D.

Parameters

top (int, optional, default None) – how many top epochs to return, all epochs by default

Returns

top_epochs – dataframe with epoch_id as index and aggregated Cook’s-D as values

Return type

pandas DataFrame

Notes

Cook’s distance is aggregated by simple averaging across time and channels.

plot_adj_rsquared()[source]

Plot adjusted \(R^2\) as a heatmap with marginal bar and line.

Returns

  • fig (matplotlib.figure.Figure) – figure containing plots

  • gs (matplotlib.gridspec.GridSpec) – grid specification that determines locations and sizes of subplots

  • bar, heatmap, colorbar, line (matplotlib.axes._subplots.AxesSubplot) – subplot objects

plot_betas(legend_on_bottom=False)[source]

Plot betas of the model, one plot per channel, overplotting betas.

Parameters

legend_on_bottom (bool, defaults to False) – set to True to plot single legend below all channel plots

Returns

  • fig (matplotlib.figure.Figure) – figure containing plots

  • axes (numpy.ndarray of matplotlib.axes.Axes) – axes objects