def mds_plot(dataset_name, bits, data_path, mat_path): raw_data = sio.loadmat(data_path) data = raw_data['data'] window_size = int(raw_data['subLen'][0][0]) mat_file = sio.loadmat(mat_path) compressible = mat_file['compressible'] hypothesis = mat_file['hypothesis'] idx_bitsave = mat_file['idx_bitsave'] try: cut_off = np.where(np.diff(idx_bitsave[:, 1]) > 0)[0][0] except IndexError as error: print('{} - {}'.format(dataset_name, bits)) return valid_idx = idx_bitsave[:, 0][:cut_off] valid_h = np.intersect1d(valid_idx, hypothesis) valid_c = np.intersect1d(valid_idx, compressible) assert (valid_h.shape[0] + valid_c.shape[0] == valid_idx.shape[0]) Y, _ = pattern_mds(valid_idx.astype(int), window_size, data) fig, ax = plt.subplots() h, c = [], [] for i in range(valid_idx.shape[0]): if idx_bitsave[i, 2] == 0: h.append([Y[i, 0], Y[i, 1]]) else: c.append([Y[i, 0], Y[i, 1]]) h = np.array(h) c = np.array(c) ax.scatter(h[:, 0], h[:, 1], marker='x', color='C0', label='hypothesis') ax.scatter(c[:, 0], c[:, 1], marker='o', color='C1', label='compressible') ax.legend(loc=2) ax.grid(True) plt.title('MDS Visual of {}'.format(dataset_name)) plt.savefig('./result/gaussian/{}-{}bits-mds.png'.format( dataset_name, bits)) plt.clf()
d[:-4], bit_dict[d[:-4]])) idx_bitsave = result['idx_bitsave'].reshape(-1, 3) cut_off = np.where(np.diff(idx_bitsave[:, 1]) > 0)[0] cut_off = cut_off[0] if cut_off.shape[0] != 0 else idx_bitsave.shape[0] idx = idx_bitsave[:, 0][:cut_off].astype(np.int) bad_label = [] for i in idx: if np.any(np.abs(i - tp) < 0.2 * window_size): bad_label.append(1) else: bad_label.append(0) mds2 = pattern_mds(idx, window_size, data)[0] if mds2.shape[1] < 2: f = plt.figure() plt.title('{} - Gaussian'.format(d[:-4])) plt.axis('off') f.savefig('./mds/{}/{}-Gaussian.png'.format(d[:-4], d[:-4]), bbox_inches='tight') plt.show() continue hit = [] not_hit = [] for i in range(mds2.shape[0]): if bad_label[i] == 1: hit.append([mds2[i, 0], mds2[i, 1]])
def mds_plot(dataset_name, bits, data_path, mat_path): os.makedirs('./result/gaussian/{}'.format(dataset_name)) raw_data = sio.loadmat(data_path) data = raw_data['data'] tp = raw_data['labIdx'] - 1 p = 0.2 window_size = int(raw_data['subLen'][0][0]) mat_file = sio.loadmat(mat_path) compressible = mat_file['compressible'] hypothesis = mat_file['hypothesis'] idx_bitsave = mat_file['idx_bitsave'] compress_table = ast.literal_eval(mat_file['compress_table'][0]) try: cut_off = np.where(np.diff(idx_bitsave[:, 1]) > 0)[0][0] except IndexError as error: print('{} - {}'.format(dataset_name, bits)) return valid_idx = idx_bitsave[:, 0][:cut_off] valid_h = np.intersect1d(valid_idx, hypothesis) valid_c = np.intersect1d(valid_idx, compressible) assert (valid_h.shape[0] + valid_c.shape[0] == valid_idx.shape[0]) Y, _ = pattern_mds(valid_idx.astype(int), window_size, data) fig, ax = plt.subplots() h, c, fp_h, fp_c = [], [], [], [] fp_h_idx, fp_c_idx = [], [] for i in range(valid_idx.shape[0]): if idx_bitsave[i, 2] == 0: if validation(valid_idx[i], tp, p, window_size): h.append([Y[i, 0], Y[i, 1]]) else: fp_h.append([Y[i, 0], Y[i, 1]]) fp_h_idx.append(valid_idx[i]) else: if validation(valid_idx[i], tp, p, window_size): c.append([Y[i, 0], Y[i, 1]]) else: fp_c.append([Y[i, 0], Y[i, 1]]) fp_c_idx.append(valid_idx[i]) h = np.array(h) c = np.array(c) fp_h = np.array(fp_h) fp_c = np.array(fp_c) for h in fp_h_idx: coms = compress_table[h] for c in coms: if c not in fp_c_idx and c in valid_idx: print('fp_h -> tp_c') print('{}, {}'.format(h, c)) else: print('fp_h -> fp_c') print(fp_h.shape) print(fp_c.shape)
if __name__ == '__main__': data_sets = os.listdir('../eval_data/all') for d in data_sets: if not os.path.exists('./mds/{}'.format(d[:-4])): os.makedirs('./mds/{}'.format(d[:-4])) # if d not in ['Wine.mat', 'TwoLeadECG.mat', 'FaceFour.mat']: # continue file = sio.loadmat('../eval_data/all/{}'.format(d)) window_size = file['subLen'][0][0] data = file['data'] tp = file['labIdx'].squeeze() - 1 mds = pattern_mds(tp, window_size, data)[0] f = plt.figure() plt.scatter(mds[:, 0], mds[:, 1], s=0.5 * plt.rcParams['lines.markersize'] ** 2) plt.title(d[:-4]) plt.axis('off') f.savefig('./mds/{}/{}.png'.format(d[:-4], d[:-4]), bbox_inches='tight') plt.show() randidx = np.random.permutation(data.shape[0] - window_size + 1) randidx = randidx[:tp.shape[0]] bad_label = [] for i in randidx: if np.any(np.abs(i - tp) < 0.2 * window_size): bad_label.append(1)