-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathanalysis_utils.py
More file actions
430 lines (370 loc) · 12.2 KB
/
Copy pathanalysis_utils.py
File metadata and controls
430 lines (370 loc) · 12.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.lines import Line2D
from matplotlib.colors import rgb2hex
import seaborn as sns
import pandas as pd
import umap
# make random np operations reproducible
np.random.seed(0)
def get_2D_umap_embeddings(feature_data: np.ndarray, random_state: int = 0):
"""
get 2D umap embeddings for numpy array as x and y vectors
Parameters
----------
feature_data : np.ndarray
feature data to find embeddings for
random_state : int, optional
random state for umap embeddings, by default 0
Returns
-------
np.ndarray, np.ndarray
X data vector, y data vector
"""
# create umap object for dimension reduction
reducer = umap.UMAP(random_state=random_state, n_components=2)
# Fit UMAP and extract latent vars 1-2
embedding = reducer.fit_transform(feature_data)
embedding = np.transpose(embedding)
# convert to seaborn-recognizable vectors
x_data = embedding[0]
y_data = embedding[1]
return x_data, y_data
def show_2D_umap_from_embeddings(
x_data: np.ndarray,
y_data: np.ndarray,
metadata_series: pd.Series,
save_path=None,
point_size: int = 5,
alpha: float = 1,
palette: str = "bright",
):
"""
show 2D umap from 2D UMAP embeddings, save if desired
Parameters
----------
x_data : np.ndarray
vector with X coordinates
y_data : np.ndarray
vector with Y coordinates
metadata_series : pd.Series
metadata for how to color umap points
save_path : pathlib.Path, optional
path to save umap image, by default None
point_size : int, optional
size of umap points, by default 5
alpha : float, optional
opacity of umap points, by default 1
palette : str, optional
color palette used to color points, by default "bright"
"""
plt.figure(figsize=(15, 12))
# Produce scatterplot with umap data, using metadata to color points
sns_plot = sns.scatterplot(
palette=palette,
x=x_data,
y=y_data,
hue=metadata_series.tolist(),
alpha=alpha,
linewidth=0,
s=point_size,
)
# Adjust legend
sns_plot.legend(
loc="center left", bbox_to_anchor=(1, 0.5), title=metadata_series.name
)
# Label axes, title
sns_plot.set_xlabel("UMAP 1")
sns_plot.set_ylabel("UMAP 2")
sns_plot.set_title("2 Dimensional UMAP")
# save umap
if not save_path == None:
plt.savefig(save_path, bbox_inches="tight")
plt.plot()
def get_class_colors(classes_list: list, palette: str) -> dict:
"""
get class colors dictionary from a class list
Parameters
----------
classes_list : list
list of classes to get colors from
palette : str
seaborn palette name to get colors from
Returns
-------
dict
dictionary with class names as keys and hex color strings as values
"""
class_colors = {}
cmap = sns.color_palette(palette, len(classes_list))
for index, class_name in enumerate(classes_list):
class_colors[class_name] = rgb2hex(cmap[index])
return class_colors
def show_1D_umap(
feature_data: np.ndarray,
metadata_series: pd.Series,
class_colors: dict,
save_path=None,
point_size: int = 5,
alpha: float = 1,
):
"""
show (and save) 1D umap, colored by metadata
classes not included in colored_classes will be colored gray
Parameters
----------
feature_data : np.ndarray
data for features to plot
metadata_series : pd.Series
metadata used to color data
class_colors : dict
colors for classes, any classes not specified will be gray
save_path : _type_, optional
where to save umap, by default None, by default None
point_size : int, optional
size of umap points, by default 5
alpha : float, optional
opacity of umap points,, by default 1
"""
# create umap object for dimension reduction
reducer = umap.UMAP(random_state=0, n_components=1)
# Fit UMAP and extract latent vars
embedding = pd.DataFrame(reducer.fit_transform(feature_data), columns=["UMAP1"])
# add phenotypic class to embeddings
embedding[metadata_series.name] = metadata_series.tolist()
# create random y distribution to space out points
y_distribution = np.random.rand(feature_data.shape[0])
embedding["y_distribution"] = y_distribution.tolist()
fig = plt.figure(figsize=(15, 15))
ax = fig.gca()
legend_elements = []
# keep track of if "other" classes exist
other_classes_exist = False
# add each phenotypic class to 1d graph and legend
for index, metadata_class in enumerate(
embedding[metadata_series.name].unique().tolist()
):
class_embedding = embedding.loc[
embedding[metadata_series.name] == metadata_class
]
x = class_embedding["UMAP1"]
y = class_embedding["y_distribution"]
# color by class or gray if it should not be colored
if metadata_class in class_colors.keys():
color = class_colors[metadata_class]
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label=metadata_class,
markerfacecolor=color,
markersize=10,
)
)
else:
other_classes_exist = True
color = "#808080"
ax.scatter(x, y, c=color, marker="o", alpha=alpha, s=point_size)
# add "other" to legend if there are "other" classes
if other_classes_exist:
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label="Other",
markerfacecolor=color,
markersize=10,
)
)
plt.legend(handles=legend_elements, loc="center left", bbox_to_anchor=(1, 0.5))
# Label axes, title
ax.set_xlabel("UMAP 1")
ax.set_ylabel("Random Distribution")
ax.set_title("1 Dimensional UMAP")
# save umap
if not save_path == None:
plt.savefig(save_path, bbox_inches="tight")
plt.show()
def show_2D_umap(
feature_data: np.ndarray,
metadata_series: pd.Series,
class_colors: dict,
save_path=None,
point_size: int = 5,
alpha: float = 1,
):
"""
show (and save) 2D umap, colored by metadata
classes not included in colored_classes will be colored gray
Parameters
----------
feature_data : np.ndarray
data for features to plot
metadata_series : pd.Series
metadata used to color data
class_colors : dict
colors for classes, any classes not specified will be gray
save_path : _type_, optional
where to save umap, by default None, by default None
point_size : int, optional
size of umap points, by default 5
alpha : float, optional
opacity of umap points,, by default 1
"""
# create umap object for dimension reduction
reducer = umap.UMAP(random_state=0, n_components=2)
# Fit UMAP and extract latent vars
embedding = pd.DataFrame(
reducer.fit_transform(feature_data), columns=["UMAP1", "UMAP2"]
)
# add phenotypic class to embeddings
embedding[metadata_series.name] = metadata_series.tolist()
fig = plt.figure(figsize=(15, 15))
ax = fig.gca()
legend_elements = []
# keep track of if "other" classes exist
other_classes_exist = False
# add each phenotypic class to 1d graph and legend
for index, metadata_class in enumerate(
embedding[metadata_series.name].unique().tolist()
):
class_embedding = embedding.loc[
embedding[metadata_series.name] == metadata_class
]
x = class_embedding["UMAP1"]
y = class_embedding["UMAP2"]
# color by class or gray if it should not be colored
if metadata_class in class_colors.keys():
color = class_colors[metadata_class]
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label=metadata_class,
markerfacecolor=color,
markersize=10,
)
)
else:
other_classes_exist = True
color = "#808080"
ax.scatter(x, y, c=color, marker="o", alpha=alpha, s=point_size)
# add "other" to legend if there are "other" classes
if other_classes_exist:
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label="Other",
markerfacecolor=color,
markersize=10,
)
)
plt.legend(handles=legend_elements, loc="center left", bbox_to_anchor=(1, 0.5))
# Label axes, title
ax.set_xlabel("UMAP 1")
ax.set_ylabel("UMAP 2")
ax.set_title("2 Dimensional UMAP")
# save umap
if not save_path == None:
plt.savefig(save_path, bbox_inches="tight")
plt.show()
return embedding
def show_3D_umap(
feature_data: np.ndarray,
metadata_series: pd.Series,
class_colors: dict,
save_path=None,
point_size: int = 5,
alpha: float = 1,
):
"""
show (and save) 3D umap, colored by metadata
classes not included in colored_classes will be colored gray
Parameters
----------
feature_data : np.ndarray
data for features to plot
metadata_series : pd.Series
metadata used to color data
class_colors : dict
colors for classes, any classes not specified will be gray
save_path : _type_, optional
where to save umap, by default None, by default None
point_size : int, optional
size of umap points, by default 5
alpha : float, optional
opacity of umap points,, by default 1
"""
# create umap object for dimension reduction
reducer = umap.UMAP(random_state=0, n_components=3)
# Fit UMAP and extract latent vars
embedding = pd.DataFrame(
reducer.fit_transform(feature_data), columns=["UMAP1", "UMAP2", "UMAP3"]
)
# add phenotypic class to embeddings
embedding[metadata_series.name] = metadata_series.tolist()
fig = plt.figure(figsize=(15, 15))
ax = fig.gca(projection="3d")
legend_elements = []
# keep track of if "other" classes exist
other_classes_exist = False
# add each phenotypic class to 3d graph and legend
for index, metadata_class in enumerate(
embedding[metadata_series.name].unique().tolist()
):
class_embedding = embedding.loc[
embedding[metadata_series.name] == metadata_class
]
x = class_embedding["UMAP1"]
y = class_embedding["UMAP2"]
z = class_embedding["UMAP3"]
# color by class or gray if it should not be colored
if metadata_class in class_colors.keys():
color = class_colors[metadata_class]
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label=metadata_class,
markerfacecolor=color,
markersize=10,
)
)
else:
other_classes_exist = True
color = "#808080"
ax.scatter(x, y, z, c=color, marker="o", alpha=alpha, s=point_size)
# add "other" to legend if there are "other" classes
if other_classes_exist:
legend_elements.append(
Line2D(
[0],
[0],
marker="o",
color="w",
label="Other",
markerfacecolor=color,
markersize=10,
)
)
plt.legend(handles=legend_elements, loc="center left", bbox_to_anchor=(1, 0.5))
# Label axes, title
ax.set_xlabel("UMAP 1")
ax.set_ylabel("UMAP 2")
ax.set_zlabel("UMAP 3")
ax.set_title("3 Dimensional UMAP")
# save umap
if not save_path == None:
plt.savefig(save_path, bbox_inches="tight")
plt.show()