# out = infer_on_edf(edf_file_path=''
# eeg_channel='C4-M1',
# left_eog_channel='E1-M2',
# chin_emg_channel='EMG1-EMG2',
# ecg_channel='ECG1-ECG2',
# spo2_channel='SPO2',
# abdomen_rr_channel='ABDO',
# thoracic_rr_channel='dummy',
# device='mps',
# models_dir='../'
# )
Inference
For you!
load_pftsleep_models
load_pftsleep_models (models_dir='', encoder_model_name='pft_sleep_encoder.ckpt', classifier_model_name='pft_sleep_classifier.ckpt', classifier_head_defaults={'c_in': 7, 'input_size': 512, 'hidden_size': 1024, 'predict_every_n_patches': 5, 'n_classes': 5, 'num_rnn_layers': 2, 'contrastive': False, 'rnn_dropout': 0.1, 'module': 'GRU', 'bidirectional': True, 'affine': True, 'pool': 'average', 'pre_norm': False, 'mlp_final_head': True, 'linear_dropout': 0.1, 'temperature': 2})
*Loads the pftsleep models from the models directory
Args: models_dir (str): The directory of the saved models encoder_model_name (str): The name of the encoder model classifier_model_name (str): The name of the classifier model classifier_head_defaults (dict): The defaults for the classifier head, DO NOT CHANGE! Returns: encoder (PatchTFTSimpleLightning): The encoder model ss_classifier (PatchTFTSleepStage): The classifier model*
Type | Default | Details | |
---|---|---|---|
models_dir | str | The directory of the saved models | |
encoder_model_name | str | pft_sleep_encoder.ckpt | the name of the encoder model |
classifier_model_name | str | pft_sleep_classifier.ckpt | the name of the classifier model |
classifier_head_defaults | dict | {‘c_in’: 7, ‘input_size’: 512, ‘hidden_size’: 1024, ‘predict_every_n_patches’: 5, ‘n_classes’: 5, ‘num_rnn_layers’: 2, ‘contrastive’: False, ‘rnn_dropout’: 0.1, ‘module’: ‘GRU’, ‘bidirectional’: True, ‘affine’: True, ‘pool’: ‘average’, ‘pre_norm’: False, ‘mlp_final_head’: True, ‘linear_dropout’: 0.1, ‘temperature’: 2} | the defaults for the classifier head, DO NOT CHANGE! |
download_pftsleep_models
download_pftsleep_models (write_dir='', token=None)
*Function to download pftsleep models from hugging face
Args: write_dir (str): The directory to write the models to token (str): Your hugging face token to use to download the models*
Type | Default | Details | |
---|---|---|---|
write_dir | str | The directory to write the models to | |
token | NoneType | None | Your hugging face token to use to download the models |
process_edf
process_edf (edf_file_path, channels, reference_channels_dict={}, frequency=125, sample_length=28800, frequency_filters_ordered=[[0.5, 40], [0.3, 30], [0.3, 30], [0.3, 30], [0.4, None], [0.5, None], [0.5, None]], median_filter_kernel_size=3, overwrite_edf_duration=False, verbose=True)
*Process the edf file to prepare it for inference. This function is used to prepare the edf file for inference by reading the channels, resampling them to the correct frequency, filtering them, and padding them to the correct length. Do not change the default parameters (frequency, sample_length, frequency_filters_ordered, median_filter_kernel_size) of this function!
Args: edf_file_path (str): The path to the edf file to perform inference on channels (list): The channels to read from the edf file reference_channels_dict (dict): The reference channels to subtract from the channels. The keys are the channels to subtract from, and the values are the reference channels. frequency (int): The frequency to resample the channels to. Do not change this! sample_length (int): The length of the sequence to pad the channels to, expected by the model. Do not change this! frequency_filters_ordered (list): The frequency filters to apply to the channels. Do not change this! median_filter_kernel_size (int): The kernel size for the median filter. Do not change this! overwrite_edf_duration (bool): Whether to overwrite the duration of the edf file to the sample length, if the edf file duration key is corrupted. verbose (bool): Whether to print the verbose output.
Returns: signals (torch.Tensor): The processed signals sequence_padding_mask (torch.Tensor): The sequence padding mask*
Type | Default | Details | |
---|---|---|---|
edf_file_path | The edf file path to perform inference on | ||
channels | the channels to read from the edf file | ||
reference_channels_dict | dict | {} | the reference channels to subtract from the channels. The keys are the channels to subtract from, and the values are the reference channels. |
frequency | int | 125 | the frequency to resample the channels to. Do not change this! |
sample_length | int | 28800 | the length of the sequence to pad the channels to, expected by the model. Do not change this! |
frequency_filters_ordered | list | [[0.5, 40], [0.3, 30], [0.3, 30], [0.3, 30], [0.4, None], [0.5, None], [0.5, None]] | the frequency filters to apply to the channels. Do not change this! |
median_filter_kernel_size | int | 3 | the kernel size for the median filter. Do not change this! |
overwrite_edf_duration | bool | False | whether to overwrite the duration of the edf file to the sample length, if the edf file duration key is corrupted. |
verbose | bool | True | whether to print the verbose output. |
view_edf_channels
view_edf_channels (edf_file_path, uppercase=True)
*View the channels of an edf file.
Args: edf_file_path (str): The path to the edf file to view the channels of uppercase (bool): Whether to return the channels in uppercase
Returns: channels (list): The channels in the edf file*
Type | Default | Details | |
---|---|---|---|
edf_file_path | The path to the edf file to view the channels of | ||
uppercase | bool | True | Whether to return the channels in uppercase |
EDFDataset
EDFDataset (edf_file_paths, eeg_channel, left_eog_channel, chin_emg_channel, ecg_channel, spo2_channel, abdomen_rr_channel, thoracic_rr_channel, eeg_reference_channel=None, left_eog_reference_channel=None, chin_emg_reference_channel=None, ecg_reference_channel=None, **kwargs)
*An abstract class representing a :class:Dataset
.
All datasets that represent a map from keys to data samples should subclass it. All subclasses should overwrite :meth:__getitem__
, supporting fetching a data sample for a given key. Subclasses could also optionally overwrite :meth:__len__
, which is expected to return the size of the dataset by many :class:~torch.utils.data.Sampler
implementations and the default options of :class:~torch.utils.data.DataLoader
. Subclasses could also optionally implement :meth:__getitems__
, for speedup batched samples loading. This method accepts list of indices of samples of batch and returns list of samples.
.. note:: :class:~torch.utils.data.DataLoader
by default constructs an index sampler that yields integral indices. To make it work with a map-style dataset with non-integral indices/keys, a custom sampler must be provided.*
Type | Default | Details | |
---|---|---|---|
edf_file_paths | The paths to the edf files to perform inference on | ||
eeg_channel | the EEG channel name in the EDF. The model was trained with C4-M1 and C3-M2 referenced EEG channels. However, | ||
left_eog_channel | the left EOG channel name in the EDF. The model was trained with M2 referenced left EOG channels. | ||
chin_emg_channel | the chin EMG channel name in the EDF. The model was trained with chin refenced (chin 2 or chin 3) EMG channels. | ||
ecg_channel | the ECG channel name in the EDF. The model was trained with augmented lead 2 ecg channels | ||
spo2_channel | the SpO2 channel name in the EDF. | ||
abdomen_rr_channel | the abdomen RR channel name in the EDF. | ||
thoracic_rr_channel | the thoracic RR channel name in the EDF. | ||
eeg_reference_channel | NoneType | None | the EEG reference channel name in the EDF. The model was trained with C4-M1 and C3-M2 referenced EEG channels. This will reference the channels, if they havent already been referenced. |
left_eog_reference_channel | NoneType | None | the left EOG reference channel name in the EDF. The model was trained with M2 referenced left EOG channels. This will reference the channels, if they havent already been referenced. |
chin_emg_reference_channel | NoneType | None | the chin EMG reference channel name in the EDF. The model was trained with chin refenced (chin 2 or chin 3) EMG channels. This will reference the channels, if they havent already been referenced. |
ecg_reference_channel | NoneType | None | the ECG reference channel name in the EDF. The model was trained with augmented lead 2 ecg channels. This will reference the channels, if they havent already been referenced. |
kwargs | VAR_KEYWORD |
write_pred_to_hypjson
write_pred_to_hypjson (predictions, hypjson_path)
Function to write the predictions to a hypjson file.
create_hypjson
create_hypjson (epochs)
map_stage
map_stage (stage)
infer_on_edf
infer_on_edf (edf_file_path, eeg_channel, left_eog_channel, chin_emg_channel, ecg_channel, spo2_channel, abdomen_rr_channel, thoracic_rr_channel, eeg_reference_channel=None, left_eog_reference_channel=None, chin_emg_reference_channel=None, ecg_reference_channel=None, models_dir='', encoder_model_name='pft_sleep_encoder.ckpt', classifier_model_name='pft_sleep_classifier.ckpt', device='cpu', **kwargs)
*Performs inference on a single edf file using the pftsleep models. If you specify a channel as None or ‘dummy’, the channel will be passed through as a zero vector. This allows you to use the model even if some channels are not present in the edf file.
Args: edf_file_path (str): The path to the edf file to perform inference on eeg_channel (str): The name of the EEG channel in the EDF left_eog_channel (str): The name of the left EOG channel in the EDF chin_emg_channel (str): The name of the chin EMG channel in the EDF ecg_channel (str): The name of the ECG channel in the EDF spo2_channel (str): The name of the SpO2 channel in the EDF abdomen_rr_channel (str): The name of the abdomen RR channel in the EDF thoracic_rr_channel (str): The name of the thoracic RR channel in the EDF eeg_reference_channel (str): The name of the EEG reference channel in the EDF left_eog_reference_channel (str): The name of the left EOG reference channel in the EDF chin_emg_reference_channel (str): The name of the chin EMG reference channel in the EDF ecg_reference_channel (str): The name of the ECG reference channel in the EDF models_dir (str): The directory of the saved models encoder_model_name (str): The name of the encoder model classifier_model_name (str): The name of the classifier model device (str): The device to run the model on **kwargs: Additional keyword arguments for process_edf function
Returns: out (torch.Tensor): The sleep stage logit outputs of the classifier for each sleep epoch in the edf file*
Type | Default | Details | |
---|---|---|---|
edf_file_path | The edf file path to perform inference on | ||
eeg_channel | the EEG channel name in the EDF. The model was trained with C4-M1 and C3-M2 referenced EEG channels. However, | ||
left_eog_channel | the left EOG channel name in the EDF. The model was trained with M2 referenced left EOG channels. | ||
chin_emg_channel | the chin EMG channel name in the EDF. The model was trained with chin refenced (chin 2 or chin 3) EMG channels. | ||
ecg_channel | the ECG channel name in the EDF. The model was trained with augmented lead 2 ecg channels | ||
spo2_channel | the SpO2 channel name in the EDF. | ||
abdomen_rr_channel | the abdomen RR channel name in the EDF. | ||
thoracic_rr_channel | the thoracic RR channel name in the EDF. | ||
eeg_reference_channel | NoneType | None | the EEG reference channel name in the EDF. The model was trained with C4-M1 and C3-M2 referenced EEG channels. This will reference the channels, if they havent already been referenced. |
left_eog_reference_channel | NoneType | None | the left EOG reference channel name in the EDF. The model was trained with M2 referenced left EOG channels. This will reference the channels, if they havent already been referenced. |
chin_emg_reference_channel | NoneType | None | the chin EMG reference channel name in the EDF. The model was trained with chin refenced (chin 2 or chin 3) EMG channels. This will reference the channels, if they havent already been referenced. |
ecg_reference_channel | NoneType | None | the ECG reference channel name in the EDF. The model was trained with augmented lead 2 ecg channels. This will reference the channels, if they havent already been referenced. |
models_dir | str | the directory of the saved models | |
encoder_model_name | str | pft_sleep_encoder.ckpt | the name of the encoder model |
classifier_model_name | str | pft_sleep_classifier.ckpt | the name of the classifier model |
device | str | cpu | the device to run the model on |
kwargs | VAR_KEYWORD |
infer_on_edf_dataset
infer_on_edf_dataset (edf_dataloader, device='cpu', models_dir='', encoder_model_name='pft_sleep_encoder.ckpt', classifier_model_name='pft_sleep_classifier.ckpt')
*Performs inference on an EDFDataset.
Args: edf_dataloader (DataLoader): The dataloader (from EDFDataset) to perform inference on device (str): The device to run the model on batch_size (int): The batch size to use for inference models_dir (str): The directory of the saved models encoder_model_name (str): The name of the encoder model classifier_model_name (str): The name of the classifier model
Returns: preds (list): The predicted sleep stage logits for each edf file*
Type | Default | Details | |
---|---|---|---|
edf_dataloader | the edf dataset to perform inference on | ||
device | str | cpu | the device to run the model on |
models_dir | str | the directory of the saved models | |
encoder_model_name | str | pft_sleep_encoder.ckpt | the name of the encoder model |
classifier_model_name | str | pft_sleep_classifier.ckpt | the name of the classifier model |