-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_crazy_stuff_with_abu.py
More file actions
349 lines (254 loc) · 9.48 KB
/
Copy pathtest_crazy_stuff_with_abu.py
File metadata and controls
349 lines (254 loc) · 9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 10 13:51:22 2025
@author: natasha
"""
import numpy as np
import pandas as pd
import os
import math
import matplotlib.pyplot as plt
import glob
from scipy.stats import chi2_contingency, zscore, ttest_rel, chisquare, power_divergence, percentileofscore
import scipy.stats as stats
import seaborn as sns
from matplotlib.legend_handler import HandlerTuple
import shutil
from scipy.interpolate import make_interp_spline, BSpline
from scipy.ndimage import gaussian_filter1d
from scipy.optimize import curve_fit
import piecewise_regression
import umap
from sklearn.preprocessing import StandardScaler
from tqdm import tqdm
from scipy.ndimage import gaussian_filter
from matplotlib.colors import TwoSlopeNorm
from sklearn.decomposition import PCA
from pickle import load
# ==============================================================================
# Load data and get setup
# ==============================================================================
dirname = '/home/natasha/Desktop/clustering_data/'
file_path = os.path.join(dirname, 'clustering_df_update.pkl')
df = pd.read_pickle(file_path)
transition_file_path = os.path.join(dirname, 'scaled_mode_tau_cut.pkl')
transition_df = pd.read_pickle(transition_file_path)
# Remove any data for df that does not have an associated transition time in scaled_mode_tau
df['basename'] = df['basename'].str.lower() # All basenames to lowercase
transition_df['basename'] = transition_df['basename'].str.lower() # All basenames to lowercase
transition_df = transition_df.rename(columns={'taste': 'taste_num'}) # NEW changed column name.
tau_basenames = transition_df.basename.unique() # Find all basenames in transition_df
df = df.loc[df['basename'].isin(tau_basenames)] # Keep only basenames
# Manually removed this specific data:
df = df[~((df['basename'] == 'km50_5tastes_emg_210911_104510_copy') & (df['taste'] == 1))]
df = df[~((df['basename'] == 'km50_5tastes_emg_210911_104510_copy') & (df['taste'] == 4))]
obj_path = os.path.join(dirname, 'pca_obj.pkl')
with open(obj_path, 'rb') as file:
pca_obj = load(file)
# ==============================================================================
# Check PCA
# ==============================================================================
check_filtered_df = df[df['cluster_num'].isin([0, 1, 2])]
check_norm_interp = check_filtered_df['segment_norm_interp']
check_norm_interp = check_norm_interp.apply(pd.Series)
X_pca_check = pca_obj.transform(check_norm_interp)
X_pca_from_df = np.array([features[-3:] for features in check_filtered_df['raw_features']])
# Check if they're close?
np.allclose(X_pca_check, X_pca_from_df)
# =============================================================================
# Reconstructing single dimensions of PCA
# =============================================================================
min_pc = X_pca_check.min(axis=0)
max_pc = X_pca_check.max(axis=0)
X,Y = np.meshgrid(
np.linspace(-2,2, 5),
np.linspace(2,-2, 5),
)
# x_list = []
# y_list = []
recon_list = []
# for this_x, this_y in zip(X.flatten(), Y.flatten()):
for ind_1 in range(X.shape[0]):
for ind_2 in range(X.shape[1]):
this_x = X[ind_1, ind_2]
this_y = Y[ind_1, ind_2]
recon = pca_obj.inverse_transform([this_x, this_y, 0])
x_list.append(this_x)
y_list.append(this_y)
recon_list.append(recon)
fig, ax = plt.subplots(*X.shape,
sharex=True,
sharey=True,
figsize = (10,10))
count = 0
for ind_1 in range(X.shape[0]):
for ind_2 in range(X.shape[1]):
this_x = X[ind_1, ind_2]
this_y = Y[ind_1, ind_2]
ax[ind_1, ind_2].plot(recon_list[count])
ax[ind_1, ind_2].set_title(f"PC0 = {this_x}, PC1 = {this_y}")
count += 1
plt.tight_layout()
###############
PC0_vals = []
PC1_vals = []
cluster_labels = []
for idx, row in check_filtered_df.iterrows():
PC0_vals.append(row['features'][-3])
PC1_vals.append(row['features'][-2])
cluster_labels.append(row['cluster_num'])
plt.figure(figsize=(10,10))
scatter = plt.scatter(PC0_vals, PC1_vals, c=cluster_labels, cmap='viridis', alpha=0.7)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.xlabel("PC0")
plt.ylabel("PC1")
# Compute cluster means
df_plot = pd.DataFrame({
'PC0': PC0_vals,
'PC1': PC1_vals,
'cluster': cluster_labels
})
cluster_means = df_plot.groupby('cluster')[['PC0', 'PC1']].mean()
cluster_means = cluster_means.reset_index()
cmap = plt.get_cmap('viridis')
norm = plt.Normalize(min(cluster_labels), max(cluster_labels))
colors = cmap(norm(cluster_means['cluster'].values))
# Plot the cluster means as large red dots
plt.scatter(cluster_means['PC0'],
cluster_means['PC1'],
facecolors = colors,
s=200,
marker='o',
edgecolors='black')
plt.legend()
plt.title("PC0 vs PC1 Colored by Cluster")
plt.show()
###############
from sklearn.neighbors import KernelDensity
density_centers = []
# Convert to DataFrame
df_plot = pd.DataFrame({
'PC0': PC0_vals,
'PC1': PC1_vals,
'cluster': cluster_labels
})
for cluster_id in sorted(df_plot['cluster'].unique()):
cluster_points = df_plot[df_plot['cluster'] == cluster_id][['PC0', 'PC1']].values
# Fit KDE
kde = KernelDensity(bandwidth=0.1, kernel='gaussian')
kde.fit(cluster_points)
# Score the density of each point
log_dens = kde.score_samples(cluster_points)
# Find the point with highest density
max_idx = np.argmax(log_dens)
densest_point = cluster_points[max_idx]
density_centers.append(densest_point)
density_centers = np.array(density_centers)
plt.figure(figsize=(10,10))
scatter = plt.scatter(PC0_vals, PC1_vals, c=cluster_labels, cmap='viridis', alpha=0.7)
plt.xlim(-2,2)
plt.ylim(-2,2)
plt.xlabel("PC0")
plt.ylabel("PC1")
plt.scatter(
density_centers[:, 0], density_centers[:, 1],
facecolors='red',
edgecolors='black',
s=300,
linewidths=2,
marker='X',
label='KDE Peak'
)
plt.legend()
plt.show()
# =============================================================================
#
# =============================================================================
cluster_range = [0, 1, 2]
i_dont_know = []
for i in cluster_range:
filtered_df = df[df['cluster_num'] == i]
X_pca = np.array([features[-3:] for features in filtered_df['features']])
mean_vector = np.mean(X_pca, axis=0)
i_dont_know.append(mean_vector)
# i_dont_know_array = np.array(i_dont_know)
# =============================================================================
#
# =============================================================================
from sklearn.linear_model import LinearRegression
# lr = LinearRegression()
# lr.fit(check_norm_interp, X_pca_from_df)
# lr_project = lr.predict(check_norm_interp)
X = check_norm_interp.copy()
X = np.concatenate([X, np.ones((len(X),1))], axis=1)
y = X_pca_from_df.copy()
B = np.linalg.pinv(X.T @ X) @ X.T @ y
# lr_project = check_norm_interp @ B[:-1] + B[-1]
lr_project = X@B
fig, ax = plt.subplots(1,2, sharex=True, sharey=True, figsize=(10,10))
ax[0].imshow(X_pca_from_df, interpolation=None, aspect='auto',
vmin = -3, vmax = 3)
ax[1].imshow(lr_project, interpolation=None, aspect='auto',
vmin = -3, vmax = 3)
B_inv = np.linalg.pinv(B)
# y := means of the MTM clusters in feature space
# shape = datapoints, PCAs
# X_inv_from_y = (y - B[-1]) @ B_inv
X_inv_from_y = y @ B_inv
X_inv_from_y = X_inv_from_y[:, :-1]
plt.imshow(X_inv_from_y, interpolation=None, aspect='auto',)
plt.plot(X_inv_from_y[:100].T)
# y = i_dont_know
# y := means of the MTM clusters in feature space
# shape = datapoints, PCAs
inv_proj = []
for y in i_dont_know:
X_inv_from_y = (y - B[-1])[None,:] @ B_inv
X_inv_from_y = X_inv_from_y[:, :-1]
inv_proj.append(X_inv_from_y)
inv_proj_array = np.squeeze(np.array(inv_proj))
plt.plot(inv_proj_array.T)
plt.title("Is this right??")
plt.show()
# =============================================================================
# Make new PCA
# =============================================================================
from sklearn.decomposition import PCA
pca_new = PCA(n_components=3)
pca_new.fit(check_norm_interp)
X_pca_new = pca_new.transform(check_norm_interp)
# ==============================================================================
# Reconstruct waveforms from PCA
# ==============================================================================
cluster_range = [0, 1, 2]
colors = {
0: '#4285F4',
1: '#88498F',
2: '#0CBABA',
}
mean_features = []
mean_waveforms = []
for i in cluster_range:
filtered_df = df[df['cluster_num'] == i]
X_pca = np.array([features[-3:] for features in filtered_df['features']])
X_reconstructed = pca_obj.inverse_transform(X_pca)
for waveform in X_reconstructed:
plt.plot(waveform, color=colors[i], alpha=0.1)
plt.title(f"Inverse-Transformed Waveforms\nCluster {i}")
plt.show()
mean_feature = np.mean(X_pca, axis=0)
mean_features.append(mean_feature)
prototypical_waveform = np.mean(X_reconstructed, axis=0)
mean_waveforms.append(prototypical_waveform)
explained_variance_ratio = pca_obj.explained_variance_ratio_
for w in mean_features:
waveform = pca_obj.inverse_transform(w)
plt.plot(waveform)
plt.title("Reconstructing waveforms from mean PCA values")
plt.show()
for w in mean_waveforms:
plt.plot(w)
plt.title("Mean of reconstructed waveforms")
plt.show()