Epochs class#
- class pyneon.Epochs(source: Stream | Events, times_df: DataFrame)#
Class to create and manage epochs in the data streams.
- Parameters:
- sourceStream or Events
Data to create epochs from.
- times_dfpandas.DataFrame, shape (n_epochs, 4), optional
DataFrame containing epoch information with the following columns:
t_ref: Reference time of the epoch, in nanoseconds.t_before: Time before the reference time to start the epoch, in nanoseconds.t_after: Time after the reference time to end the epoch, in nanoseconds.description: Description or label associated with the epoch.Must not have empty values.
- Attributes:
- epochspandas.DataFrame
DataFrame containing epoch information with the following columns:
t_ref(int64): Reference time of the epoch, in nanoseconds.t_before(int64): Time before the reference time to start the epoch, in nanoseconds.t_after(int64): Time after the reference time to end the epoch, in nanoseconds.description(str): Description or label associated with the epoch.data(object): DataFrame containing the data for each epoch.- datapandas.DataFrame
Annotated data with epoch information. In addition to the original data columns, the following columns are added:
epoch index(Int32): ID of the epoch the data belongs to.epoch time(Int64): Time relative to the epoch reference time, in nanoseconds.epoch description(str): Description or label associated with the epoch.If epochs overlap, data annotations are always overwritten by the latest epoch.
Methods
baseline_correction([baseline, method, inplace])Perform baseline correction on epochs.
plot([column_name, cmap_name, ax, show])Plot data from a specified column from epochs on a matplotlib axis.
to_numpy([column_names])Converts epochs into a 3D array with dimensions (n_epochs, n_channels, n_times).
Notes
An epoch spans the temporal range of
t_ref - t_beforetot_ref + t_afteras shown below:t_ref[0] t_ref[1] <--t_before[0]--|--t_after[0]--> <-t_before[1]-|-t_after[1]-> ├--------------------------------------------------------------------------------┤- baseline_correction(baseline: tuple[Number | None, Number | None] = (None, 0), method: str = 'mean', inplace: bool = True) DataFrame | None#
Perform baseline correction on epochs.
- Parameters:
- baseline(t_min, t_max), iterable of float | None
Start and end of the baseline window in seconds, relative to the event trigger (t_ref = 0).
Nonemeans “from the first / up to the last sample”. Default: (None, 0.0) -> the pre-trigger part of each epoch.- method“mean” or “linear”, optional
“mean” - Subtract the scalar mean of the baseline window.
“linear” - Fit a first-order (\(y = at + b\)) model within the baseline window and remove the fitted trend from the entire epoch (a very small, fast version of MNE’s regression detrending).
Defaults to “mean”.
- inplacebool
If True, overwrite epochs data. Otherwise return a new, corrected pandas.DataFrame and leave the object unchanged. Defaults to True.
- Returns:
- pandas.DataFrame
The baseline-corrected data (same shape & dtypes as original data).
- plot(column_name: str | None = None, cmap_name: str = 'cool', ax: Axes | None = None, show: bool = True) tuple[Figure, Axes]#
Plot data from a specified column from epochs on a matplotlib axis.
- Parameters:
- column_namestr
Name of the column to plot for
pyneon.Epochsconstructed from apyneon.Stream. Ifpyneon.Epochswas constructed from apyneon.Events, this parameter is ignored. Defaults to None.- cmap_namestr
Colormap to use for different epochs. Defaults to ‘cool’.
- axmatplotlib.axes.Axes or None
Axis to plot the data on. If
None, a new figure is created. Defaults toNone.- showbool
Show the figure if
True. Defaults to True.
- Returns:
- figmatplotlib.figure.Figure
Figure object containing the plot.
- axmatplotlib.axes.Axes
Axis object containing the plot.
- to_numpy(column_names: str | list[str] = 'all') tuple[ndarray, dict]#
Converts epochs into a 3D array with dimensions (n_epochs, n_channels, n_times). Acts similarly as
mne.Epochs.get_data(). Requires the epoch to be created from a uniformly-sampledpyneon.Stream.- Parameters:
- column_namesstr or list of str, optional
Column names to include in the NumPy array. If ‘all’, all columns are included. Only columns that can be converted to int or float can be included. Default is ‘all’.
- Returns:
- numpy_epochsnumpy.ndarray
NumPy array of shape (n_epochs, n_channels, n_times).
- infodict
A dictionary containing:
“column_ids”: List of provided column names.
“t_rel”: The common time grid, in nanoseconds.
“nan_flag”: Boolean indicating whether NaN values were found in the data.
Notes
The time grid (
t_rel) is in nanoseconds.If NaN values are present after interpolation, they are noted in
nan_flag.
- pyneon.events_to_times_df(events: Events, t_before: Number, t_after: Number, t_unit: Literal['s', 'ms', 'us', 'ns'] = 's', event_name: str | list[str] = 'all') DataFrame#
Construct a
times_dfDataFrame suitable for creating epochs from event data. For “simple”events(blinks, fixations, saccades), all events are used. For more complexevents(e.g., from “events.csv”, or concatenated events), the user can specify which events to include by anamecolumn.- Parameters:
- eventsEvents
Events instance containing the event times.
- t_beforenumbers.Number
Time before the event start time to start the epoch. Units specified by
t_unit.- t_afternumbers.Number
Time after the event start time to end the epoch. Units specified by
t_unit.- t_unitstr, optional
Unit of time for
t_beforeandt_after. Can be “s”, “ms”, “us”, or “ns”. Default is “s”.- event_namestr or list of str, optional
Only used if
eventsincludes more than one event type. If “all”, all events are used. Otherwise, thenamecolumn is used to filter events whose names are in the list. Default to “all”.
- Returns:
- pandas.DataFrame
DataFrame with columns:
t_ref,t_before,t_after,description(all in ns).
- pyneon.construct_times_df(t_ref: ndarray, t_before: ndarray | Number, t_after: ndarray | Number, description: ndarray | str, t_ref_unit: Literal['s', 'ms', 'us', 'ns'] = 'ns', t_other_unit: Literal['s', 'ms', 'us', 'ns'] = 's', global_t_ref: int = 0) DataFrame#
Handles the construction of the
times_dfDataFrame for creating epochs. It populates single values for t_before, t_after, and description to match the length of t_ref. and converts all times to UTC timestamps in nanoseconds.- Parameters:
- t_refnumpy.ndarray, optional
Array of reference times for the epochs. Units specified by
t_ref_unit.- t_beforenumpy.ndarray or Number, optional
Time before the reference time to start the epoch. Could be an array of equal length as
t_refor a single number (to be repeated for all epochs). Units specified byt_other_unit.- t_afternumpy.ndarray or Number, optional
Time after the reference time to end the epoch. Could be an array of equal length as
t_refor a single number (to be repeated for all epochs). Units specified byt_other_unit.- descriptionnumpy.ndarray or str, optional
Description or label associated with the epochs. Could be an array of equal length as
t_refor a single string (to be repeated for all epochs).- t_ref_unitstr, optional
Unit of time for
t_ref. Can be “s”, “ms”, “us”, or “ns”. Default is “ns”.- t_other_unitstr, optional
Unit of time for
t_beforeandt_after. Can be “s”, “ms”, “us”, or “ns”. Default is “s”.- global_t_refint, optional
Global reference time (in nanoseconds) to be added to t_ref. Unit is nanosecond. Defaults to 0. This is useful when the reference times are relative to a global start time (for instance
pyneon.Stream.first_ts).
- Returns:
- pandas.DataFrame
DataFrame with columns:
t_ref,t_before,t_after,description(all in ns).