###### ------------------- načítání souborů dat a labelů pro DP -----------------

### importy
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils.data import Dataset, DataLoader
import random
import json
import os
import sys
import wfdb
import matplotlib.pyplot as plt
from scipy import signal
from scipy.fft import rfft, rfftfreq, fft, fftfreq
from config import Config

### načtení souboru
def load_file(sample_path):

    "funkce pro načtení jednoho souboru s formátem .WFDB"

    ## signal data
    sig, fields = wfdb.rdsamp(sample_path)
    sig = sig.T ## přeskládání na řádkový vektor
    fs = fields["fs"] ## sampling frequency
    length = fields["sig_len"]
    global_labels = fields["comments"]
    global_labels = ''.join(global_labels) ## převedení listu na string

    ## annotation
    ann = wfdb.rdann(sample_path, 'atr')
    qrs_locations = ann.sample ## annotation qrs location in samples relative to the beginning of the record
    # annotation_symbols = np.array(ann.symbol) ## display the annotation symbols - +, N, A, V
    fib_locations = ann.aux_note  ## contains list of strings with auxiliary information - (AFIB, (N, (AFL
    # summary_of_sample = ann.contained_labels ## table of the set of annotation labels contained in the file


    ## get labels
    if global_labels == 'paroxysmal atrial fibrillation':
        label = 1
    elif global_labels == 'persistent atrial fibrillation':
        label = 1
    else:
        label = 0

    return sig, length, fs, label, global_labels, qrs_locations, fib_locations
