import numpy as np from tqdm import tqdm import scipy.io as scio def intoBins(data, n_bins): k_labels = np.zeros((len(data), n_bins+1)) bin_size = (np.max(data)+0.0001-np.min(data)) / n_bins for i in tqdm(range(len(data))): index = (data[i]-np.min(data)) // bin_size k_labels[i, int(index)] = 1 return k_labels def preprocessing(n_data, data_length, degrees_of_freedom, n_labels, n_bins, path='2500\\2500'): # save input all_data = np.zeros((n_data, degrees_of_freedom+n_labels+1+1, data_length)) for i in tqdm(range(2500)): file_name = f'{path}\\Data{i+1}.mat' data = scio.loadmat(file_name)['Data'][:data_length, :] data = np.transpose(data) all_data[i] = data f_and_xs = all_data[:, 1:2+degrees_of_freedom, :] mean = np.mean(f_and_xs, (0, 2)) std = np.std(f_and_xs, (0, 2)) f_and_xs = f_and_xs - np.reshape(mean, (1, -1, 1)) f_and_xs = f_and_xs / np.reshape(std, (1, -1, 1)) dict = {'f_and_xs': f_and_xs} # save labels for i in range(n_labels): label = all_data[:, 2+degrees_of_freedom+i, 0] bins = intoBins(label, n_bins)[:, :-1] dict[f'l_{i}'] = bins np.save('dataset.npy', dict) # preprocessing(2500, 10000, 6, 3, 10, path='2500\\2500') def load_dataset(path='dataset.npy'): """ :return: f_and_xs: numpy array of size [sample_number, channels, sample_length] label_0, label_1, label_2: one-hot encodes of size [sample_number, number_bins] """ r = np.load(path, allow_pickle=True).item() f_and_xs = r['f_and_xs'] label_0 = r['l_0'] label_1 = r['l_1'] label_2 = r['l_2'] return f_and_xs, label_0, label_1, label_2 f_and_xs, label_0, label_1, label_2 = load_dataset()