ltbio.biosignals#

Overview#

Subpackages#

Submodules#

Classes#

Biosignal

A Biosignal is a set of channels (Timeseries), each of which with samples measuring a biological variable.

OverlappingTimeseries

An OverlappingTimeseries is a Timeseries that violates the rule that to each time point of its domain it must

Unitless

Helper class that provides a standard way to create an ABC using

Functions#

cross_correlation(biosignal1, biosignal2)

-

plot_comparison(biosignals, show, save_to)

-

Contents#

class ltbio.biosignals.Biosignal(timeseries: Dict[str | BodyLocation, timeseries.Timeseries] | str | Tuple[datetime], source: BiosignalSource.__subclasses__() = None, patient: ltbio.clinical.Patient.Patient = None, acquisition_location: ltbio.clinical.BodyLocation.BodyLocation = None, name: str = None, **options)#

Bases: abc.ABC

digraph inheritance54c5771212 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "ABC" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Helper class that provides a standard way to create an ABC using"]; "Biosignal" [URL="modalities/Biosignal/index.html#ltbio.biosignals.modalities.Biosignal.Biosignal",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top",tooltip="A Biosignal is a set of channels (Timeseries), each of which with samples measuring a biological variable."]; "ABC" -> "Biosignal" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

A Biosignal is a set of channels (Timeseries), each of which with samples measuring a biological variable. It may be associated with a source, a patient, and a body location. It can also have a name. It has an initial and final datetime. Its length is its number of channels. It can be resampled, filtered, and concatenated to other Biosignals. Amplitude and spectrum plots can be displayed and saved.

property acquisition_location#

Returns the associated acquisition location, or None if none was provided.

property added_noise#

Returns a reference to the noisy component, if the Biosignal was created with added noise; else the property does not exist.

property channel_names: set[str | BodyLocation]#

Returns a tuple with the labels associated to every channel.

property domain: Tuple[datetimerange.DateTimeRange]#
property duration#
property events#

Tuple of associated Events, ordered by datetime.

property final_datetime: datetime.datetime#

Returns the final datetime of the channel that ends the latest.

property initial_datetime: datetime.datetime#

Returns the initial datetime of the channel that starts the earliest.

property name#

Returns the associated name, or ‘No Name’ if none was provided.

property patient_code#

Returns the code of the associated Patient, or ‘n.d.’ if none was provided.

property patient_conditions: Set[ltbio.clinical.conditions.MedicalCondition.MedicalCondition]#

Returns the set of medical conditions of the associated Patient, or None if no Patient was associated.

property preview#

Returns 5 seconds of the middle of the signal.

property sampling_frequency: float#

Returns the sampling frequency of every channel (if equal), or raises an error if they are not equal.

property source: ltbio.biosignals.sources.BiosignalSource.BiosignalSource#

Returns the BiosignalSource from where the data was read, or None if was not specified.

property subdomains: Tuple[datetimerange.DateTimeRange]#
property type: ClassVar#

Returns the biosignal modality class. E.g.: ECG, EMG, EDA, …

EXTENSION = '.biosignal'#
apply_operation(operation, **kwargs)#
associate(events: Event | Collection[Event] | Dict[str, Event])#

Associates an Event to all Timeseries. Events have names that serve as keys. If keys are given, i.e. if ‘events’ is a dict, then the Event names are overridden. @param events: One or multiple Event objects. @rtype: None

delete_events()#
disassociate(event_name: str)#

Disassociates an Event from all Timeseries. @param event_name: The name of the Event to be removed. @rtype: None

filter(filter_design) int#

Filters every channel with to the given filter_design.

@param filter_design: A Filter object specifying the designed filter to be applied. @return: 0 if the filtering is applied successfully. @rtype: int

classmethod fromNoise(noises: Noise | Dict[str | BodyLocation, Noise], time_intervals: DateTimeRange | tuple[DateTimeRange], name: str = None)#

Creates a type of Biosignal from a noise source.

Parameters:
  • noises

    • If a Noise object is given, the Biosignal will have 1 channel for the specified time interval.

    • If a dictionary of Noise objects is given, the Biosignal will have multiple channels, with different

    generated samples, for the specified time interval, named after the dictionary keys.

  • time_interval – Interval [x, y[ where x will be the initial date and time of every channel, and y will be

the final date and time of every channel; on a union of intervals, in case a tuple is given.

Parameters:

name – The name to be associated to the Biosignal. Optional.

Returns:

Biosignal subclass

get_event(name: str) ltbio.biosignals.timeseries.Event.Event#
invert(channel_label: str = None)#
classmethod load(filepath: str)#
plot(show: bool = True, save_to: str = None)#

Plots the amplitude in time of every channel. @param show: True if plot is to be immediately displayed; False otherwise. @param save_to: A path to save the plot as an image file; If none is provided, it is not saved.

plot_spectrum(show: bool = True, save_to: str = None)#

Plots the Bode plot of every channel. @param show: True if plot is to be immediately displayed; False otherwise. @param save_to: A path to save the plot as an image file; If none is provided, it is not saved.

abstract plot_summary(show: bool = True, save_to: str = None)#

Plots a summary of relevant aspects of common analysis of the Biosignal.

resample(frequency: float)#

Resamples every channel to the new sampling frequency given, using Fourier method. @param frequency: New sampling frequency (in Hertz).

restructure_domain(time_intervals: tuple[datetimerange.DateTimeRange])#
save(save_to: str)#
set_channel_name(current: str | BodyLocation, new: str | BodyLocation)#
set_event_name(current: str, new: str)#
tag(tags: str | tuple[str])#

Mark all channels with a tag. Useful to mark machine learning targets. :param tags: The label or labels to tag the channels. :return: None

undo_filters()#

Restores the raw samples of every channel, eliminating the action of any applied filter.

classmethod withAdditiveNoise(original, noise, name: str = None)#

Creates a new Biosignal from ‘original’ with added ‘noise’.

Parameters:
  • original – (Biosignal) The original Biosignal to be contaminated with noise.

  • noise – (Noise | Timeseries | Biosignal) The noise to add to the original Biosignal.

  • name – (str) The name to associate to the resulting Biosignal.

When ‘noise’ is a Noise:
  • A trench of noise, with the duration of the channel, will be generated to be added to each channel.

  • ‘noise’ should be configured with the same sampling frequency has the channels.

When ‘noise’ is a Biosignal:
When it has the same set of channels as ‘original’, sampled at the same frequency:
  • Each noisy channel will be added to the corresponding channel of ‘original’, in a template-wise manner.

When it has a unique channel:
  • That noisy channel will be added to every channel of ‘original’, in a template-wise manner.

  • That noisy channel should have the same sampling frequency has every channel of ‘original’.

  • If ‘noise’ has multiple segments, they are concatenated to make a hyper-template.

  • Exception: in the case where both Timeseries having the same domain, the noisy samples will be added in a

    segment-wise manner.

When ‘noise’ is a Timeseries sampled at the same frequency of ‘original’:
  • Its samples will be added to every channel of ‘original’, in a template-wise manner.

  • If ‘noise’ has multiple segments, they are concatenated to make a hyper-template.

  • Exception: in the case where both Timeseries having the same domain, the noisy samples will be added in a

    segment-wise manner.

  • ‘noise’ should have been sampled at the same frequency as ‘original’.

What is “template-wise manner”?
  • If the template segment is longer than any original segment, the template segment will be trimmed accordingly.

  • If the template segment is shorter than any original segment, the template will repeated in time.

  • If the two segments are of equal length, they are added as they are.

Returns:

A Biosignal with the same properties as the ‘original’, but with noise added to the samples of every channel.

Return type:

Biosignal subclass

class ltbio.biosignals.OverlappingTimeseries(samples: ndarray | list | tuple, initial_datetime: datetime.datetime, sampling_frequency: float, units: ltbio.biosignals.timeseries.Unit.Unit = None, name: str = None)#

Bases: Timeseries

An OverlappingTimeseries is a Timeseries that violates the rule that to each time point of its domain it must correspond one and only one sample. This special kind of Timeseries allows overlapping Segments, although it looses all its interpretational meaning in the context of being successive data points in time. This kind is useful to extract features from modalities or to train machine learning models.

It inherits all properties of Timeseries and most of its behaviour. In order to have overlapping Segments, indexing an exact timepoint is no longer possible; Although it is legal to index slices. # FIXME

property domain: Tuple[datetimerange.DateTimeRange]#

The intervals of date and time in which the Timeseries is defined, i.e., samples were acquired.

property duration: datetime.timedelta#

Returns real time passed from start to end, without overlaps.

property subdomains: Tuple[datetimerange.DateTimeRange]#
append(initial_datetime: datetime.datetime, samples: ndarray | list | tuple)#

Appends a new sequence of samples in a separate Segment. :param initial_datetime: The date and time of the first sample in ‘samples’. :param samples: The sequence of samples to add as a separate Segment. :return: None

class ltbio.biosignals.Unitless#

Bases: Unit

digraph inheritancef9c72b1d61 { bgcolor=transparent; rankdir=LR; size="8.0, 12.0"; "ABC" [fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",tooltip="Helper class that provides a standard way to create an ABC using"]; "Unit" [URL="timeseries/Unit/index.html#ltbio.biosignals.timeseries.Unit.Unit",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top"]; "ABC" -> "Unit" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Unitless" [URL="timeseries/Unit/index.html#ltbio.biosignals.timeseries.Unit.Unitless",fillcolor=white,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.25,shape=box,style="setlinewidth(0.5),filled",target="_top"]; "Unit" -> "Unitless" [arrowsize=0.5,style="setlinewidth(0.5)"]; }

Helper class that provides a standard way to create an ABC using inheritance.

SHORT = 'n.d.'#
convert_to(unit)#

Subclasses should return a function that receives an array of samples in the ‘self’ unit and return a converted array in the unit specified.

ltbio.biosignals.cross_correlation(biosignal1: modalities.Biosignal.Biosignal, biosignal2: modalities.Biosignal.Biosignal)#
ltbio.biosignals.plot_comparison(biosignals: Collection[modalities.Biosignal.Biosignal], show: bool = True, save_to: str = None)#