pyparrm.parrm.PARRM#
- class pyparrm.parrm.PARRM(data: ndarray, sampling_freq: int | float, artefact_freq: int | float, verbose: bool = True)[source]#
Class for removing stimulation artefacts from data using PARRM.
The Period-based Artefact Reconstruction and Removal Method (PARRM) is described in Dastin-van Rijn et al. (2021) [1]. PARRM assumes that the artefacts are semi-regular, periodic, and linearly combined with the signal of interest.
- The methods should be called in the following order:
- Parameters:
- data
numpy.ndarray
, shape of (channels, times) Time-series from which stimulation artefacts should be identified and removed.
- sampling_freq
int
|float
Sampling frequency of
data
, in Hz.- artefact_freq
int
|float
Frequency of the stimulation artefact in
data
, in Hz.- verbose
bool
(defaultTrue
) Whether or not to print information about the status of the processing.
- data
References
- Attributes:
data
Return a copy of the data.
filter
Return a copy of the PARRM filter.
filtered_data
Return a copy of the most recently filtered data.
period
Return a copy of the estimated stimulation period.
settings
Return the settings used to generate the PARRM filter.
Methods
find_period
([search_samples, ...])Find the period of the artefacts.
explore_filter_params
([time_range, ...])Create an interactive plot to explore filter parameters.
create_filter
([filter_half_width, ...])Create the PARRM filter for removing the stimulation artefacts.
filter_data
([data])Apply the PARRM filter to the data and return it.
- find_period(search_samples: ndarray | None = None, assumed_periods: int | float | tuple[int | float] | None = None, outlier_boundary: int | float = 3.0, random_seed: int | None = None, n_jobs: int = 1) None [source]#
Find the period of the artefacts.
- Parameters:
- search_samples
numpy.ndarray
|None
(defaultNone
), shape of (times) Samples of
data
to use when finding the artefact period. IfNone
, all samples are used.- assumed_periods
int
|float
|tuple
[int
orfloat
] |None
(defaultNone
) Guess(es) of the artefact period. If
None
, the period is assumed to besampling_freq
/artefact_freq
.- outlier_boundary
int
|float
(default 3.0) Boundary (in standard deviation) to consider outlier values in
data
.- random_seed: int | None (default None)
Seed to use when generating indices of samples to search for the period. Only used if the number of available samples is less than the number of requested samples.
- n_jobs
int
(default 1) Number of jobs to run in parallel when optimising the period estimates. Must lie in the range [1, number of CPUs] (unless it is -1, in which case all available CPUs are used).
- search_samples
- explore_filter_params(time_range: list[int] | None = None, time_res: int | float = 0.01, freq_range: list[int] | None = None, freq_res: int | float = 5.0, n_jobs: int = 1) None [source]#
Create an interactive plot to explore filter parameters.
Can only be called after the artefact period has been estimated with
find_period()
.- Parameters:
- time_range
list
ofint
orfloat
|None
(defaultNone
) Range of the times to plot and filter in a list of length two, containing the first and last timepoints, respectively, in seconds. If
None
, all timepoints are used.- time_res
int
|float
(default 0.01) Time resolution, in seconds, to use when plotting the time-series data.
- freq_range
list
ofint
orfloat
|None
(defaultNone
) Range of the frequencies to plot in a list of length two, containing the first and last frequencies, respectively, in Hz. If
None
, all frequencies are used.- freq_res
int
|float
(default 5.0) Frequency resolution, in Hz, to use when computing the power spectra of the data.
- n_jobs
int
(default 1) Number of jobs to run in parallel when computing the power spectra. Must lie in the range [1, number of CPUs] (unless it is -1, in which case all available CPUs are used).
- time_range
Notes
It is recommended that you call this method from a script; behaviour in notebooks is not guaranteed!
- create_filter(filter_half_width: int | None = None, omit_n_samples: int = 0, filter_direction: str = 'both', period_half_width: int | float | None = None) None [source]#
Create the PARRM filter for removing the stimulation artefacts.
Can only be called after the artefact period has been estimated with
find_period()
.- Parameters:
- filter_half_width
int
|None
(defaultNone
) Half-width of the filter to create, in samples. If
None
, a filter half-width will be generated based onomit_n_samples
.- omit_n_samples
int
(default 0) Number of samples to omit from the centre of
filter_half_width
.- filter_direction
str
(default “both”) Direction from which samples should be taken to create the filter, relative to the centre of the filter window. Can be: “both” for backward and forward samples; “past” for backward samples only; and “future” for forward samples only.
- period_half_width
int
|float
|None
(defaultNone
) Half-width of the window in samples of period space for which points at a similar location in the waveform will be averaged. If
None
,period
/ 50 is used.
- filter_half_width
- filter_data(data: ndarray | None = None) ndarray [source]#
Apply the PARRM filter to the data and return it.
Can only be called after the filter has been created with
create_filter()
.- Parameters:
- data
numpy.ndarray
, shape of (channels, times) |None
(defaultNone
) The data to filter. If None,
data
is used.
- data
- Returns:
- filtered_data
numpy.ndarray
, shape of (channels, times) The filtered, artefact-free data.
- filtered_data