def draw_activation_pattern(ax, model, spectrogram, feature_layer_name='reshape_1'): """ Draws a t-sne of a layer's feature vector to a matplotlib plot. Args: ax: A matplotlib axis to plot to. model: The neural network used to make predictions. spectrogram: A spectrogram of an audio fragment. feature_layer_name: The name of the layer whose output vector will be visualized. """ feature_layer = model.get_layer(feature_layer_name).output input_placeholder = model.input trimmed_network = backend.function([input_placeholder], [feature_layer]) spectrogram = [spectrogram] output_batch = trimmed_network(spectrogram)[0] for output in output_batch: squeezed = output embedding = TSNE(n_components=1).fit_transform(squeezed) ys = embedding.flatten() ys = (ys - ys.min()) / (ys.max() - ys.min()) nr_frames = ys.shape[0] colors = ys xs = np.arange(0, nr_frames) ax.scatter(xs, ys, c=colors)
def tsne_f(embeddings,y): ''' Visualize t-SNE embeddings of the embeddings ''' plt.clf() X_embedded = TSNE(n_components=2, perplexity = 20, metric = 'cityblock').fit_transform(embeddings) X_embedded_flattened = X_embedded.flatten('F') fashion_scatter(np.array(X_embedded), y) return X_embedded,X_embedded_flattened
def tsne(signalruns): event_list = list() count = 0 for signalrun in signalruns: signalrun_doc = schema.Signalrun.find_one(signalrun=signalrun['id']) event_docs = schema.Event.find(signalrun=signalrun_doc) events = [(doc.start_time, doc.stop_time) for doc in event_docs] event_list.append({ 'eid': count, 'events': events, 'start': signalrun_doc.signal.start_time, 'end': signalrun_doc.signal.stop_time, 'signalrun_id': str(signalrun_doc.id) }) count += 1 dist_matrix = np.empty((count, count)) for i in range(count): for j in range(i + 1): if i == j: dist_matrix[i, j] = 0 else: dist_matrix[i, j] = event_overlap_distance( event_list[i]['events'], event_list[j]['events'], min(event_list[i]['start'], event_list[j]['start']), max(event_list[i]['end'], event_list[j]['end'])) dist_matrix[j, i] = dist_matrix[i, j] def func_dist(a, b): return dist_matrix[int(a[0]), int(b[0])] X = np.arange(count) X = X.reshape(-1, 1).astype(int) X_embedded = TSNE(n_components=1, metric=func_dist).fit_transform(X) X_embedded = X_embedded.flatten() X_sorted = [{'i': i, 'v': v} for i, v in enumerate(X_embedded)] X_sorted = sorted(X_sorted, key=lambda x: x['v']) for i, d in enumerate(X_sorted): event_list[d['i']]['order'] = i signalruns_ordered = [None] * len(event_list) for i in range(len(event_list)): signalruns_ordered[event_list[i]['order']] = signalruns[i] return signalruns_ordered
def tsne_n1(x): value = TSNE(n_components=1).fit_transform(x) arr = value.flatten() return arr
if part.__contains__('WSAS'): phases = ['Base', 'Anes'] if part.__contains__('MCD'): phases = ['eyesclosed1', 'emergencefirst5'] label_part = label[data['ID'] == part] X_part = X[data['ID'] == part].copy() # drop columns with nan X_part = X_part[:, ~np.isnan(X_part).any(axis=0)] X_2 = TSNE(n_components=2).fit_transform(X_part) X_1 = TSNE(n_components=1).fit_transform(X_part) data_one = pd.DataFrame( np.transpose(np.vstack((X_1.flatten(), label_part)))) data_one.columns = ['conn_1D', 'label'] oneD.append(data_one) mean_base = np.mean(X_1[label_part == 0]) mean_anes = np.mean(X_1[label_part == 1]) dist_tmp = max(mean_base, mean_anes) - min(mean_base, mean_anes) Dist.append(dist_tmp) meanA.append(mean_anes) meanB.append(mean_base) fig = plt.figure(figsize=(10, 10)) scatter = plt.scatter(X_2[:, 0], X_2[:, 1], marker='o', c=label_part) plt.scatter(X_2[:, 0], X_2[:, 1], marker='o', c=label_part) plt.legend(handles=scatter.legend_elements()[0], labels=phases) plt.title(part)
label = np.zeros(len(data['Phase'])) label[np.where(data['Phase'] == 'Anes')] = 1 #label[np.where(data['Phase']=='Sedoff')]=2 #label[np.where(data['Phase']=='Sedon1')]=3 #label[np.where(data['Phase']=='eyesclosed1')]=4 #label[np.where(data['Phase']=='emergencefirst5')]=5 #plt.plot(label) #plt.show() #phases = ['Base', 'Anes','Sedoff', 'Sedon1','eyesclosed1', 'emergencefirst5'] #phases = ['Base', 'Anes','Sedoff', 'Sedon1'] phases = ['Base', 'Anes'] participants = np.unique(data['ID']) len(participants) X_1 = TSNE(n_components=1).fit_transform(X) data_one = pd.DataFrame( np.transpose(np.vstack((X_1.flatten(), label, outcome)))) data_one.columns = ['conn_1D', 'label', 'outcome'] data_one.index = data.index ax = sns.displot(data=data_one, x="conn_1D", hue="label", kind="kde", col="outcome", fill=True) ax.set_titles("{col_name} participants") plt.show()
dist_matrix = np.empty((count, count)) for i in range(count): for j in range(i + 1): if i == j: dist_matrix[i, j] = 0 else: dist_matrix[i, j] = event_overlap_distance( event_list[i]['events'], event_list[j]['events'], min(event_list[i]['start'], event_list[j]['start']), max(event_list[i]['end'], event_list[j]['end'])) dist_matrix[j, i] = dist_matrix[i, j] def func_dist(a, b): return dist_matrix[int(a[0]), int(b[0])] X = np.arange(count) X = X.reshape(-1, 1).astype(int) X_embedded = TSNE(n_components=1, metric=func_dist).fit_transform(X) X_embedded = X_embedded.flatten() X_sorted = [{'i': i, 'v': v} for i, v in enumerate(X_embedded)] X_sorted = sorted(X_sorted, key=lambda x: x['v']) for i, d in enumerate(X_sorted): event_list[d['i']]['order'] = i for d in event_list: print(d)
fig = plt.figure(figsize=(10, 10)) ax = fig.gca(projection='3d') scatter = ax.scatter(data_3[:, 0], data_3[:, 1], data_3[:, 2], marker='o', c=label) ax.scatter(data_3[:, 0], data_3[:, 1], data_3[:, 2], marker='o', c=label) ax.plot(data_3[:, 0], data_3[:, 1], data_3[:, 2], linewidth=0.6, color='k') ax.set_title(p_id) pdf.savefig() plt.close() data_1 = TSNE(n_components=1).fit_transform(data) data_one = pd.DataFrame(np.transpose(np.vstack((data_1.flatten(), label)))) data_one.columns = ['conn_1D', 'label'] oneD.append(data_one) mean_base = np.mean(data_1[label == 0]) mean_anes = np.mean(data_1[label == 1]) dist_tmp = max(mean_base, mean_anes) - min(mean_base, mean_anes) Dist.append(dist_tmp) meanA.append(mean_anes) meanB.append(mean_base) for i, p in enumerate(P_IDS): data = oneD[i] mean_anes = meanA[i] mean_base = meanB[i] a = sns.displot(data=data, x="conn_1D", hue="label", kind="kde", fill=True)