|
- import os
- from sklearn.manifold import TSNE
- import matplotlib.pyplot as plt
- import numpy as np
- import librosa
- import scipy.io as scio
- import mat4py
-
- def load_lfm_channel(mat_path):
- result = np.array(mat4py.loadmat(mat_path)['h_lfm'])
- return result
-
- def normalization(x):
- x = x - np.mean(x)
- x = x / np.max(np.abs(x))
- return x
-
- def load_audio(path):
- recv_wav_signal, _ = librosa.load(path, sr=128000)
- recv_wav_signal = normalization(recv_wav_signal)
- return recv_wav_signal
-
- def get_all_channel_toOneMatrix_tSNEResult(all3k_channel_dir, do_sort=True):
- all_channel_num = len(os.listdir(all3k_channel_dir))
-
- all_ABC_channel_matrix = np.zeros((all_channel_num, 5121))
- all_ABC_item_namelist = []
- all_ABC_labelist = []
-
- index = 0
- for item in os.listdir(all3k_channel_dir):
- if '.mat' in item and 'comm' in item:
- comm_index = item.split('_')[0] # 3 depth use same sending bits
- all_ABC_item_namelist.append(item)
- # lfm_channel = load_lfm_channel(os.path.join(all3k_channel_dir, item))
- # all_ABC_channel_matrix[index, :] = lfm_channel.reshape(-1)
- index+=1
-
- if do_sort:
- sorted_idx = np.argsort(all_ABC_item_namelist)
- all_ABC_item_namelist.sort()
-
- for i, each_item_path in enumerate(all_ABC_item_namelist):
- lfm_channel = load_lfm_channel(os.path.join(all3k_channel_dir, each_item_path))
- all_ABC_channel_matrix[i, :] = lfm_channel.reshape(-1)
-
- return all_ABC_channel_matrix, all_ABC_item_namelist, all_ABC_labelist
-
-
-
-
- if __name__ == '__main__':
- all3k_h_lfm_channel_dir = '/userhome/wave_training_old/UWA_haoxinhu_2023/h_lfm/tSNE_h_lfm/'
- finegrained_group_percent = None # choose接近聚类中心前50%的数据samples组成新group
- do_sort = False
-
- all_channel, all_channel_namelist, all_channel_label = get_all_channel_toOneMatrix_tSNEResult(all3k_h_lfm_channel_dir, do_sort=do_sort)
-
- tSNE = TSNE(n_components=2, random_state=15)
- data = tSNE.fit_transform(all_channel)
-
- save_dir = '/userhome/wave_training_old/channel_plots/all_channel_tSNE_haoxinhu2023/'
- if not os.path.exists(save_dir):
- os.makedirs(save_dir, exist_ok=True)
-
- label_list = []
- gruop1_dict = {}
- gruop2_dict = {}
-
- for idx, item in enumerate(data):
- # 1: 3ksamples_grouped_data abnormal:([-∞,-70], []); fine-group1:([-20, 28], [15, 65]); fine-group2:([-30, 26], [-50, -10])
- # 2: 3ksamples_grouped_data abnormal:([-∞,-70], []); group1:([-70, ~], [6.5, ~]); group2:([-70, ~], [~, 6.5])
-
- # ############################# 1 ##################################
- # if data[idx, 0] < -70:
- # c = 'y'
- # class_name = 'abnormal'
- # elif -20 <= data[idx, 0] <= 28 and 15 <= data[idx, 1] <= 65:
- # c = 'r'
- # class_name = '1'
- # elif -30 <= data[idx, 0] <= 26 and -50 <= data[idx, 1] <= -10:
- # c = 'b'
- # class_name = '2'
- # else:
- # c = 'g'
- # class_name = 'abnormal'
- # ############################# 1 ##################################
-
- ############################# 2 ##################################
- if not do_sort:
- if data[idx, 0] < -70:
- c = 'y'
- class_name = 'abnormal'
- elif -70 <= data[idx, 1] and 6.5 <= data[idx, 1]:
- c = 'r'
- class_name = '1'
- elif -70 <= data[idx, 1] and data[idx, 1] <= 6.5:
- c = 'b'
- class_name = '2'
- else:
- c = 'g'
- class_name = 'abnormal'
- else:
- if data[idx, 1] > 65:
- c = 'y'
- class_name = 'abnormal'
- elif 7.2 <= data[idx, 0] and data[idx, 1] <= 65:
- c = 'r'
- class_name = '2'
- elif data[idx, 0] <= 7.2 and data[idx, 1] <= 65:
- c = 'b'
- class_name = '1'
- else:
- c = 'g'
- class_name = 'abnormal'
- ############################# 2 ##################################
- label_list.append(class_name)
- plt.scatter(data[idx, 0], data[idx, 1], color=c)
-
- plt.tight_layout()
- plt.savefig(os.path.join(save_dir, 'new_h_lfm_tSNE_NoSort.png'), dpi=300)
- plt.close()
-
- save_namelist_f = open(os.path.join(save_dir, 'plotting_rank_namelist_new.txt'), 'w+')
- if finegrained_group_percent is None:
- for i, channel_name in enumerate(all_channel_namelist):
- save_namelist_f.write('{} {} class-{}\n'.format(str(i), channel_name, label_list[i]))
- else:
- pass
- save_namelist_f.close()
|