Core¶
EcgBatch¶
-
class
EcgBatch(index, preloaded=None, unique_labels=None)[source]¶ Bases:
cardio.dataset.dataset.batch.BatchBatch class for ECG signals storing.
Contains ECG signals and additional metadata along with various processing methods.
Parameters: - index (DatasetIndex) – Unique identifiers of ECGs in the batch.
- preloaded (tuple, optional) – Data to put in the batch if given. Defaults to
None. - unique_labels (1-D ndarray, optional) – Array with unique labels in a dataset.
-
index¶ DatasetIndex – Unique identifiers of ECGs in the batch.
-
signal¶ 1-D ndarray – Array of 2-D ndarrays with ECG signals in channels first format.
-
annotation¶ 1-D ndarray – Array of dicts with different types of annotations.
-
meta¶ 1-D ndarray – Array of dicts with metadata about signals.
-
target¶ 1-D ndarray – Array with signals’ labels.
-
unique_labels¶ 1-D ndarray – Array with unique labels in a dataset.
-
label_binarizer¶ LabelBinarizer – Object for label one-hot encoding.
Note
Some batch methods take
indexas their first argument afterself. You should not specify it in your code, it will be passed automatically byinbatch_paralleldecorator. For example,resample_signalsmethod withindexandfsarguments should be called asbatch.resample_signals(fs).
Methods¶
Input/output methods¶
EcgBatch.load(src=None, fmt=None, components=None, ann_ext=None, *args, **kwargs)[source]¶Load given batch components from source.
Most of the
EcgBatchactions work under the assumption that bothsignalandmetacomponents are loaded. In case this assumption is not fulfilled, normal operation of the actions is not guaranteed.This method supports loading of signals from wfdb, DICOM, EDF, wav and blosc formats.
Parameters: Returns: batch (EcgBatch) – Batch with loaded components. Changes batch data inplace.
EcgBatch.dump(dst=None, fmt=None, components=None, *args, **kwargs)¶Save data to another array or a file.
Parameters:
- dst – a destination (e.g. an array or a file name)
- fmt (str) – a destination format, one of None, ‘blosc’, ‘csv’, ‘hdf5’, ‘feather’
- components (None or str or tuple of str) – components to load
- *args – other parameters are passed to format-specific writers
- *kwargs – other parameters are passed to format-specific writers
EcgBatch.show_ecg(index=None, start=0, end=None, annot=None, subplot_size=(10, 4))[source]¶Plot an ECG signal.
Optionally highlight QRS complexes along with P and T waves. Each channel is displayed on a separate subplot.
Parameters:
- index (element of
self.indices, optional) – Index of a signal to plot. If undefined, the first ECG in the batch is used.- start (int, optional) – The start point of the displayed part of the signal (in seconds).
- end (int, optional) – The end point of the displayed part of the signal (in seconds).
- annot (str, optional) – If not
None, specifies attribute that stores annotation obtained fromcardio.models.HMModel.- subplot_size (tuple) – Width and height of each subplot in inches.
Raises:
ValueError– If the chosen signal is not two-dimensional.
Batch processing¶
EcgBatch.deepcopy()¶Return a deep copy of the batch.
Constructs a new
Batchinstance and then recursively copies all the objects found in the original batch, except thepipeline, which remains unchanged.
Returns: Batch
- classmethod
EcgBatch.merge(batches, batch_size=None)[source]¶Concatenate a list of
EcgBatchinstances and split the result into two batches of sizesbatch_sizeandsum(lens of batches) - batch_sizerespectively.
Parameters:
- batches (list) – List of
EcgBatchinstances.- batch_size (positive int, optional) – Length of the first resulting batch. If
None, equals the length of the concatenated batch.Returns:
- new_batch (EcgBatch) – Batch of no more than
batch_sizefirst items from the concatenation of input batches. Contains a deep copy of input batches’ data.- rest_batch (EcgBatch) – Batch of the remaining items. Contains a deep copy of input batches’ data.
Raises:
ValueError– Ifbatch_sizeis non-positive or non-integer.
Versatile components processing¶
EcgBatch.apply_transform(func, *args, src='signal', dst='signal', **kwargs)[source]¶Apply a function to each item in the batch.
Parameters:
- func (callable) – A function to apply. Must accept an item of
srcas its first argument ifsrcis notNone.- src (str, array-like or
None, optional) – The source to get the data from. Ifsrcisstr, it is treated as the batch attribute or component name. Defaults tosignalcomponent.- dst (str, writeable array-like or
None, optional) – The source to put the result in. Ifdstisstr, it is treated as the batch attribute or component name. Defaults tosignalcomponent.- args (misc) – Any additional positional arguments to
func.- kwargs (misc) – Any additional named arguments to
func.Returns: batch (EcgBatch) – Transformed batch. If
dstisstr, the corresponding attribute or component is changed inplace.
EcgBatch.apply_to_each_channel(index, func, *args, src='signal', dst='signal', **kwargs)[source]¶Apply a function to each slice of a signal over the axis 0 (typically the channel axis).
Parameters:
- func (callable) – A function to apply. Must accept a signal as its first argument.
- src (str, optional) – Batch attribute or component name to get the data from. Defaults to
signalcomponent.- dst (str, optional) – Batch attribute or component name to put the result in. Defaults to
signalcomponent.- args (misc) – Any additional positional arguments to
func.- kwargs (misc) – Any additional named arguments to
func.Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.standardize(index, axis=None, eps=1e-10, *, src='signal', dst='signal')[source]¶Standardize data along specified axes by removing the mean and scaling to unit variance.
Parameters:
- axis (
Noneor int or tuple of ints, optional) – Axis or axes along which standardization is performed. The default is to compute for the flattened array.- eps (float) – Small addition to avoid division by zero.
- src (str, optional) – Batch attribute or component name to get the data from.
- dst (str, optional) – Batch attribute or component name to put the result in.
Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
Labels processing¶
EcgBatch.drop_labels(drop_list)[source]¶Drop elements whose labels are in
drop_list.This method creates a new batch and updates only components and
unique_labelsattribute. The information stored in other attributes will be lost.
Parameters: drop_list (list) – Labels to be dropped from a batch. Returns: batch (EcgBatch) – Filtered batch. Creates a new EcgBatchinstance.Raises: SkipBatchException– If all batch data was dropped. If the batch is created by apipeline, its processing will be stopped and thepipelinewill create the next batch.
EcgBatch.keep_labels(keep_list)[source]¶Drop elements whose labels are not in
keep_list.This method creates a new batch and updates only components and
unique_labelsattribute. The information stored in other attributes will be lost.
Parameters: keep_list (list) – Labels to be kept in a batch. Returns: batch (EcgBatch) – Filtered batch. Creates a new EcgBatchinstance.Raises: SkipBatchException– If all batch data was dropped. If the batch is created by apipeline, its processing will be stopped and thepipelinewill create the next batch.
Channels processing¶
EcgBatch.drop_channels(names=None, indices=None)[source]¶Drop channels whose names are in
namesor whose indices are inindices.
Parameters: Returns: batch (EcgBatch) – Batch with dropped channels. Changes
self.signalandself.metainplace.Raises:
ValueError– If bothnamesandindicesare empty.ValueError– If all channels should be dropped.
EcgBatch.keep_channels(names=None, indices=None)[source]¶Drop channels whose names are not in
namesand whose indices are not inindices.
Parameters: Returns: batch (EcgBatch) – Batch with dropped channels. Changes
self.signalandself.metainplace.Raises:
ValueError– If bothnamesandindicesare empty.ValueError– If all channels should be dropped.
Signal processing¶
EcgBatch.convolve_signals(kernel, padding_mode='edge', axis=-1, **kwargs)[source]¶Convolve signals with given
kernel.
Parameters: Returns: batch (EcgBatch) – Convolved batch. Changes
self.signalinplace.Raises:
ValueError– Ifkernelis not one-dimensional or has non-numericdtype.
EcgBatch.band_pass_signals(index, low=None, high=None, axis=-1)[source]¶Reject frequencies outside a given range.
Parameters:
- low (positive float, optional) – High-pass filter cutoff frequency (in Hz).
- high (positive float, optional) – Low-pass filter cutoff frequency (in Hz).
- axis (int, optional) – Axis along which signals are sliced. Default value is -1.
Returns: batch (EcgBatch) – Filtered batch. Changes
self.signalinplace.
EcgBatch.drop_short_signals(min_length, axis=-1)[source]¶Drop short signals from a batch.
Parameters:
- min_length (positive int) – Minimal signal length.
- axis (int, optional) – Axis along which length is calculated. Default value is -1.
Returns: batch (EcgBatch) – Filtered batch. Creates a new
EcgBatchinstance.
EcgBatch.flip_signals(index, window_size=None, threshold=0)[source]¶Flip 2-D signals whose R-peaks are directed downwards.
Each element of
self.signalmust be a 2-D ndarray. Signals are flipped along axis 1 (signal axis). For each subarray ofwindow_sizelength skewness is calculated and compared withthresholdto decide whether this subarray should be flipped or not. Then the mode of the result is calculated to make the final decision.
Parameters:
- window_size (int, optional) – Signal is split into K subarrays of
window_sizelength. If it is not possible, data in the end of the signal is removed. Ifwindow_sizeis not given, the whole array is checked without splitting.- threshold (float, optional) – If skewness of a subarray is less than the
threshold, it “votes” for flipping the signal. Default value is 0.Returns: batch (EcgBatch) – Batch with flipped signals. Changes
self.signalinplace.Raises:
ValueError– If given signal is not two-dimensional.
EcgBatch.slice_signals(index, selection_object)[source]¶Perform indexing or slicing of signals in a batch. Allows basic
NumPyindexing and slicing along with advanced indexing.
Parameters: selection_object (slice or int or a tuple of slices and ints) – An object that is used to slice signals. Returns: batch (EcgBatch) – Batch with sliced signals. Changes self.signalinplace.
EcgBatch.split_signals(index, length, step, pad_value=0)[source]¶Split 2-D signals along axis 1 (signal axis) with given
lengthandstep.If signal length along axis 1 is less than
length, it is padded to the left withpad_value.Notice, that each resulting signal will be a 3-D ndarray of shape
[n_segments, n_channels, length]. If you would like to get a number of 2-D signals of shape[n_channels, length]as a result, you need to applyunstack_signalsmethod then.
Parameters: Returns: batch (EcgBatch) – Batch of split signals. Changes
self.signalinplace.Raises:
ValueError– If:
- given signal is not two-dimensional,
stepis not int or dict,lengthorstepfor a given signal is negative or non-integer.KeyError– Ifstepdict has no signal’s target key.
EcgBatch.random_split_signals(index, length, n_segments, pad_value=0)[source]¶Split 2-D signals along axis 1 (signal axis)
n_segmentstimes with random start position and givenlength.If signal length along axis 1 is less than
length, it is padded to the left withpad_value.Notice, that each resulting signal will be a 3-D ndarray of shape
[n_segments, n_channels, length]. If you would like to get a number of 2-D signals of shape[n_channels, length]as a result, you need to applyunstack_signalsmethod then.
Parameters: Returns: batch (EcgBatch) – Batch of split signals. Changes
self.signalinplace.Raises:
ValueError– If:
- given signal is not two-dimensional,
n_segmentsis not int or dict,lengthorn_segmentsfor a given signal is negative or non-integer.KeyError– Ifn_segmentsdict has no signal’s target key.
EcgBatch.unstack_signals()[source]¶Create a new batch in which each signal’s element along axis 0 is considered as a separate signal.
This method creates a new batch and updates only components and
unique_labelsattribute. Signal’s data from non-signalcomponents is duplicated using a deep copy for each of the resulting signals. The information stored in other attributes will be lost.
Returns: batch (same class as self) – Batch with split signals and duplicated other components. Examples
>>> batch.signal array([array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]])], dtype=object)>>> batch = batch.unstack_signals() >>> batch.signal array([array([0, 1, 2, 3]), array([4, 5, 6, 7]), array([ 8, 9, 10, 11])], dtype=object)
EcgBatch.resample_signals(index, fs)[source]¶Resample 2-D signals along axis 1 (signal axis) to given sampling rate.
Parameters: fs (positive float) – New sampling rate.
Returns: batch (EcgBatch) – Resampled batch. Changes
self.signalandself.metainplace.Raises:
ValueError– If given signal is not two-dimensional.ValueError– Iffsis negative or non-numeric.
EcgBatch.random_resample_signals(index, distr, **kwargs)[source]¶Resample 2-D signals along axis 1 (signal axis) to a new sampling rate, sampled from a given distribution.
If new sampling rate is negative, the signal is left unchanged.
Parameters: Returns: batch (EcgBatch) – Resampled batch. Changes
self.signalandself.metainplace.Raises:
ValueError– If given signal is not two-dimensional.ValueError– Ifdistris not a string or a callable.
Complex ECG processing¶
Fourier-based transformations¶
EcgBatch.fft(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a Discrete Fourier Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument tonumpy.fft.fft.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.ifft(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute an inverse Discrete Fourier Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument tonumpy.fft.ifft.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.rfft(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a real-input Discrete Fourier Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument tonumpy.fft.rfft.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.irfft(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a real-input inverse Discrete Fourier Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument tonumpy.fft.irfft.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.spectrogram(index, *args, src='signal', dst='signal', **kwargs)[source]¶Compute a spectrogram for each slice of a signal over the axis 0 (typically the channel axis).
This method is a wrapper around
scipy.signal.spectrogram, that accepts the same arguments, except thefswhich is substituted automatically from signal’s meta. The method returns only the spectrogram itself.
Parameters:
- src (str, optional) – Batch attribute or component name to get the data from.
- dst (str, optional) – Batch attribute or component name to put the result in.
- args (misc) – Any additional positional arguments to
scipy.signal.spectrogram.- kwargs (misc) – Any additional named arguments to
scipy.signal.spectrogram.Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
Wavelet-based transformations¶
EcgBatch.dwt(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a single level Discrete Wavelet Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.dwt.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.idwt(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a single level inverse Discrete Wavelet Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.idwt.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.wavedec(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a multilevel 1D Discrete Wavelet Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.wavedec.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.waverec(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a multilevel 1D Inverse Discrete Wavelet Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.waverec.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.pdwt(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a partial Discrete Wavelet Transform data decomposition for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.downcoef.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
EcgBatch.cwt(index, func, *args, src='signal', dst='signal', **kwargs)¶Compute a Continuous Wavelet Transform for each slice of a signal over the axis 0 (typically the channel axis).
This method simply wraps
apply_to_each_channelmethod by setting thefuncargument topywt.cwt.
Parameters: Returns: batch (EcgBatch) – Transformed batch. Changes
dstattribute or component.
Other methods¶
EcgBatch.calc_ecg_parameters(index, src=None)[source]¶Calculate ECG report parameters and write them to the
metacomponent.Calculates PQ, QT, QRS intervals along with their borders and the heart rate value based on the annotation and writes them to the
metacomponent.
Parameters: src (str) – Batch attribute or component name to get the annotation from. Returns: batch (EcgBatch) – Batch with report parameters stored in the metacomponent.Raises: ValueError– IfsrcisNoneor is not an attribute of a batch.
EcgDataset¶
-
class
EcgDataset(index=None, batch_class=<class 'cardio.core.ecg_batch.EcgBatch'>, preloaded=None, index_class=<class 'cardio.dataset.dataset.dsindex.FilesIndex'>, *args, **kwargs)[source]¶ Bases:
cardio.dataset.dataset.dataset.DatasetDataset that generates batches of
EcgBatchclass.Contains indices of ECGs and a specific
batch_classto create and process batches - small subsets of data.Parameters: - index (DatasetIndex or None, optional) – Unique identifiers of ECGs in a dataset. If
indexis not given, it is constructed by instantiatingindex_classwithargsandkwargs. - batch_class (type, optional) – Class of batches, generated by dataset. Must be inherited from
Batch. - preloaded (tuple, optional) – Data to put in created batches. Defaults to
None. - index_class (type, optional) – Class of built index if
indexis not given. Must be inherited fromDatasetIndex. - args (misc, optional) – Additional positional argments to
index_class.__init__. - kwargs (misc, optional) – Additional named argments to
index_class.__init__.
- index (DatasetIndex or None, optional) – Unique identifiers of ECGs in a dataset. If