Esempio n. 1
0
def read_a_mat(eg_data, label):
    '''
        given clips eeg data of one person, preprocess and merge them 
    '''
    # Final Data to return
    train_data, train_label, test_data, test_label, \
        validate_data, validate_label = [], [], [], [] , [], []        
    
    # Loop All Clips of A man
    for i in range(15):
        all_sample_number, all_samples = [], []
        for key_word in ['de_LDS', 'de_movingAve']:
            A_key = key_word + str(i + 1)
            clip_a_data = eg_data[A_key]
            _, b, _ = clip_a_data.shape
            all_sample_number.append(b)
            
        # Min Sample
        min_sample = np.min(all_sample_number)
        for key_word in ['de_LDS', 'de_movingAve']:
            A_key = key_word + str(i + 1)
            clip_a_data = eg_data[A_key]
            clip_a_data = np.transpose(clip_a_data, (1, 0, 2))
            clip_a_data = clip_a_data[:min_sample, :, :]
            all_samples.append(clip_a_data)
            
        # A Clip Data
        clip_data = vstack_list(all_samples)
        clip_label = label[i] + 1 
        clip_label = clip_label * np.ones([int(len(clip_data)), 1])
        # print('\tClip %d shape:' % (i + 1), clip_data.shape)

            
        # Train and Test
        if (i + 1) in train_clips:
            train_data.extend(clip_data)
            train_label.extend(clip_label)
        elif (i + 1) in test_clips:
            test_data.extend(clip_data)
            test_label.extend(clip_label)   
        elif (i + 1) in validate_clips:
            validate_data.extend(clip_data)
            validate_label.extend(clip_label)     
    
    # Join data
    one_train_data = np.transpose(np.array(train_data), (0, 2, 1))
    one_train_label = np.array(train_label).reshape(-1, 1)
    one_test_data = np.transpose(np.array(test_data), (0, 2, 1))
    one_test_label = np.array(test_label).reshape(-1, 1)
    one_validate_data = np.transpose(np.array(validate_data), (0, 2, 1))
    one_validate_label = np.array(validate_label).reshape(-1, 1)
    
    # print(np.array(one_train_data).shape, np.array(one_test_data).shape, np.array(one_validate_data).shape)
    # print(one_train_label.shape, one_test_label.shape, one_validate_data.shape)
    
    return one_train_data, one_train_label, one_validate_data, \
           one_validate_label, one_test_data, one_test_label
Esempio n. 2
0
def load_all_person(label, tmp_file):
    ''' 
        Load all person's eeg files
        Fetch all data for train, test, validate
    '''
    all_train_data, all_train_label, all_test_data, all_test_label, \
         all_validate_data, all_validate_label = [], [], [], [], [], []
    one_train_data, one_train_label, one_validate_data, one_validate_label, \
           one_test_data, one_test_label = read_a_mat(tmp_file, label)
    print('One person Shapes:', one_train_data.shape, one_validate_data.shape, \
           one_test_data.shape)
    all_train_data.append(one_train_data)
    all_train_label.append(one_train_label)
    all_validate_data.append(one_validate_data)
    all_validate_label.append(one_validate_label)
    all_test_data.append(one_test_data)
    all_test_label.append(one_test_label)
    all_train_data = vstack_list(all_train_data)    
    all_train_label = vstack_list(all_train_label)
    all_validate_data = vstack_list(all_validate_data)    
    all_validate_label = vstack_list(all_validate_label)
    all_test_data = vstack_list(all_test_data)    
    all_test_label = vstack_list(all_test_label)
    
    # Shuffle
    all_train_data, all_train_label = shuffle_train(all_train_data, all_train_label)
    
    print('Final data shape', all_train_data.shape, all_test_data.shape, all_validate_data.shape)
    return all_train_data, all_train_label, all_validate_data, \
           all_validate_label, all_test_data, all_test_label
Esempio n. 3
0
def load_all_person(label):
    ''' 
        Load all person's eeg files
        Fetch all data for train, test, validate
    '''
    
    all_train_data, all_train_label, all_test_data, all_test_label, \
         all_validate_data, all_validate_label = [], [], [], [], [], []
    label = sio.loadmat(input_path + 'label.mat')['label'][0]  
    i = 0 
    
    for _, _, c in os.walk(input_path):
        for item in c:
            if '_' in item and i <= use_person:
                
                i += 1
                
                # Prepare data 
                tmp_file = sio.loadmat(input_path + item, verify_compressed_data_integrity=False)

                one_train_data, one_train_label, one_validate_data, one_validate_label, \
                       one_test_data, one_test_label = read_a_mat(tmp_file, label)


                # print('One person Shapes:', one_train_data.shape, one_validate_data.shape, \
                #       one_test_data.shape)
                all_train_data.append(one_train_data)
                all_train_label.append(one_train_label)
                all_validate_data.append(one_validate_data)
                all_validate_label.append(one_validate_label)
                all_test_data.append(one_test_data)
                all_test_label.append(one_test_label)
                
    all_train_data = vstack_list(all_train_data)    
    all_train_label = vstack_list(all_train_label)
    all_validate_data = vstack_list(all_validate_data)    
    all_validate_label = vstack_list(all_validate_label)
    all_test_data = vstack_list(all_test_data)    
    all_test_label = vstack_list(all_test_label)
    
    # Shuffle
    # all_train_data, all_train_label = shuffle_train(all_train_data, all_train_label)
    
    return all_train_data, all_train_label, all_validate_data, \
           all_validate_label, all_test_data, all_test_label