def __data_generation(self, list_IDs_temp):
        'Generates data containing batch_size samples' # X : (n_samples, *dim, n_channels)
        # Initialization
        X = np.empty((self.batch_size, *self.dim, self.n_channels), dtype = float)
        y = np.empty((self.batch_size), dtype = int)

        # Generate data
        for i, ID in enumerate(list_IDs_temp):
            
            data = extend_ts(self.h5file[ID]['ecgdata'][:, 0], self.sequence_length)
            data = np.reshape(data, (1, len(data)))
        
            if self.augment:
            
                # dropout bursts
                data = zero_filter(data, threshold = 2, depth = 10)
            
                # random resampling
                data = random_resample(data)
            
            # Generate spectrogram
            data_spectrogram = spectrogram(data, nperseg = self.nperseg, noverlap = self.noverlap)[2]
            
            # Normalize
            data_transformed = norm_float(data_spectrogram, self.data_mean, self.data_std)
        
            X[i,] = np.expand_dims(data_transformed, axis = 3)
        
            # Assuming that the dataset names are unique (only 1 per label)
            y[i] = self.labels[ID]

        return X, keras.utils.to_categorical(y, num_classes=self.n_classes)
def __data_generation(self, list_IDs_temp):
    'Generates data containing batch_size samples'  # X : (n_samples, *dim, n_channels)
    # Initialization
    X = np.empty((self.batch_size, *self.dim, self.n_channels), dtype=float)
    y = np.empty((self.batch_size), dtype=int)

    # Generate data
    for i, ID in enumerate(list_IDs_temp):

        data = extend_ts(self.h5file[ID]['ecgdata'][:, 0],
                         self.sequence_length)
        data = np.reshape(data, (1, len(data)))

        if self.augment:

            # dropout bursts
            data = zero_filter(data, threshold=2, depth=10)

            # random resampling
            data = random_resample(data)

        # Generate spectrogram
        data_spectrogram = spectrogram(data,
                                       nperseg=self.nperseg,
                                       noverlap=self.noverlap)[2]

        # Normalize spectrogram
        #data_transformed = norm_float(data_spectrogram, self.data_mean, self.data_std)
        data_norm = (data_spectrogram -
                     np.mean(data_spectrogram)) / np.std(data_spectrogram)

        X[i, ] = np.expand_dims(data_norm, axis=3)

        # Assuming that the dataset names are unique (only 1 per label)
        y[i] = self.labels[ID]

    return X, keras.utils.to_categorical(y, num_classes=self.n_classes)
Exemple #3
0
# Parameters needed for the batch generator

# Maximum sequence length
max_length = sequence_length_max

# Output dimensions
sequence_length = max_length
spectrogram_nperseg = 64  # Spectrogram window
spectrogram_noverlap = 32  # Spectrogram overlap
n_classes = len(label_df.label.unique())
batch_size = 32

# calculate image dimensions
data = fetch_h5data(h5file, [0], sequence_length)
_, _, Sxx = spectrogram(data,
                        nperseg=spectrogram_nperseg,
                        noverlap=spectrogram_noverlap)
dim = Sxx[0].shape

print('Maximum sequence length:', max_length)

params = {
    'batch_size': batch_size,
    'dim': dim,
    'nperseg': spectrogram_nperseg,
    'noverlap': spectrogram_noverlap,
    'n_channels': 1,
    'sequence_length': sequence_length,
    'n_classes': n_classes,
    'shuffle': True
}
from scipy import signal
f1, PSD = signal.periodogram(ts_extended, fs, 'flattop', scaling='density')
fig, ax1 = plt.subplots(figsize=(15, 3))
ax1.plot(f1, PSD, 'b')
ax1.set(xlabel='Frequency [Hz]',
        xlim=[0, time[-1]],
        xticks=np.arange(0, time[-1] + 5, 10))
ax1.set(ylabel='PSD')
plt.title('Power spectral density (PSD)', fontsize=15)
fig.savefig('physionet_ECG_PSD.png', bbox_inches='tight', dpi=150)
plt.show()

from physionet_processing import spectrogram

# Convert ECG into spectrograms without and with log transform
Sx = spectrogram(np.expand_dims(ts_extended, axis=0), log_spectrogram=False)[2]
Sx_log = spectrogram(np.expand_dims(ts_extended, axis=0),
                     log_spectrogram=True)[2]

# Get the frequency and time axes
f, t, _ = spectrogram(np.expand_dims(ts_extended, axis=0),
                      log_spectrogram=False)

# Plot the spectrograms as images
im_list = [Sx[0], Sx_log[0]]
im_title = [
    'Spectrogram without log transform', 'Spectrogram with log transform'
]
fig, ax_list = plt.subplots(1, 2, figsize=(15, 3))

for i, ax in enumerate(ax_list):
Exemple #5
0
# Generowanie danych testujacych model - klasa DataGenerator
###########################################################

# Maksymalna dlugosc przebiegu
max_length = sequence_length

# Parametry spektrogramu
sequence_length = sequence_length_max 
spectrogram_nperseg = 64
spectrogram_noverlap = 32
n_classes = len(label_df.label.unique())
batch_size = 32

# Wymiary obrazu okreslone za pomoca wymiarow spektrogramu
Sx_log = spectrogram(np.expand_dims(ts, axis = 0),
                     nperseg = spectrogram_nperseg,
                     noverlap = spectrogram_noverlap,
                     log_spectrogram = True)[2]
dim = Sx_log[0].shape

# Inicjalizacja parametrow klasy DataGenerator

params = {'batch_size': batch_size,
          'dim': dim,
          'nperseg': spectrogram_nperseg,
          'noverlap': spectrogram_noverlap,
          'n_channels': 1,
          'sequence_length': sequence_length,
          'n_classes': n_classes,
          'shuffle': True}

Exemple #6
0
f1, PSD = signal.periodogram(ts, fs, 'flattop', scaling='density')
fig, ax2 = plt.subplots(figsize=(15, 3))
ax2.plot(f1, PSD, 'b')
#ax2.set(xlabel = 'Frequency [Hz]', xlim = [0, time[-1]], xticks = np.arange(0, time[-1]+5, 10))
#ax2.set(ylabel = 'PSD')
plt.title('Power spectral density (PSD)', fontsize=15)
fig.savefig('physionet_ECG_PSD.png', bbox_inches='tight', dpi=150)

########################################
#### 3. SPECTROGRAM ####################
########################################

from physionet_processing import spectrogram

# Convert ECG into spectrograms without and with log transform
Sx = spectrogram(np.expand_dims(ts, axis=0), log_spectrogram=False)[2]
Sx_log = spectrogram(np.expand_dims(ts, axis=0), log_spectrogram=True)[2]

# Get the frequency and time axes
f, t, _ = spectrogram(np.expand_dims(ts, axis=0), log_spectrogram=False)

# Plot the spectrograms as images
im_list = [Sx[0], Sx_log[0]]
im_title = [
    'Spectrogram without log transform', 'Spectrogram with log transform'
]
fig, ax_list = plt.subplots(1, 2, figsize=(15, 3))

for i, ax in enumerate(ax_list):

    ax.imshow(np.transpose(im_list[i]), aspect='auto', cmap='jet')