-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDP.py
463 lines (439 loc) · 15.1 KB
/
IDP.py
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
"""
IDP analysis using PMBB data and PCA/t-SNE.
Author(s):
Allison Chae
Michael S Yao
Licensed under the MIT License. Copyright 2022 University of Pennsylvania.
"""
import argparse
from io import BytesIO
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd
from pathlib import Path
from PIL import Image
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from scipy.stats import ks_2samp
from typing import Optional, Sequence, Tuple, Union
class IDPExplorer:
def __init__(
self,
idp_datapaths: Sequence[Union[Path, str]],
icd_datapath: Union[Path, str],
seed: Optional[int] = 42,
font_size: int = 20,
use_sans_serif: bool = False,
save_tif: bool = False,
transparent: bool = False
):
"""
Args:
idp_datapaths: filepaths to CSV files with PMBB IDP data.
icd_datapath: filepath to file with PMBB ICD-9 code data.
seed: optional random seed. Default 42.
font_size: font size for plotting. Default 20.
use_sans_serif: whether to use Sans Serif font. Default False.
save_tif: whether to save tif files instead of png. Default False.
transparent: whether to save transparent figures. Default False.
"""
self.idp_datapaths = idp_datapaths
self.icd_datapath = icd_datapath
self._id = "PMBB_ID"
self.diagnoses_key = "Diagnoses"
self.valid_idp_columns = [
self._id,
"LIVER_METRIC_VOLUME",
"LIVER_MEAN_HU",
"SPLEEN_METRIC_VOLUME",
"SPLEEN_MEAN_HU",
"SUBQ_METRIC_VOLUME",
"VISCERAL_METRIC_VOLUME"
]
self.icd_categories = {
"INFECTIOUS": [1, 140],
"NEOPLASMS": [140, 240],
"METABOLIC": [240, 280],
"VASCULAR": [280, 290],
"PSYCHIATRIC": [290, 320],
"NERVOUS": [320, 390],
"CIRCULATORY": [390, 460],
"RESPIRATORY": [460, 520],
"DIGESTIVE": [520, 580],
"GENITOURINARY": [580, 680],
"SKIN": [680, 710],
"MSK": [710, 740],
"CONGENITAL": [740, 760],
"OTHER": [760, 1000]
}
self.icds_references = {
"Obesity": [278, 279],
"Obstructive Sleep Apnea": [327.23, 328.24],
"Hypertension": [401, 402],
"NAFLD": [571, 572],
"Diabetes": [250, 251],
"Genitourinary Diseases": [580, 680],
"Chronic Kidney Disease": [585, 586]
}
self.seed = seed
self.rng = np.random.RandomState(seed)
self.transparent = transparent
self.font_size = font_size
self.use_sans_serif = use_sans_serif
self.transparent = transparent
self.save_tif = save_tif
self.plot_config()
self.data, self.labels = self.fimport()
def fimport(self) -> Tuple[pd.DataFrame]:
"""
Imports data from `self.datapaths` as `pandas` dataframe objects.
Input:
None.
Returns:
data: imported IDP data.
diagnoses: imported ICD-9 code data.
"""
data = None
# Import IDP data.
for fn in self.idp_datapaths:
df = pd.read_csv(fn)
# Only keep the valid IDP columns.
df = df[list(set(df.columns) & set(self.valid_idp_columns))]
# Remove any rows with negative IDP values.
for col, dtype in zip(df.columns, df.dtypes):
if not pd.api.types.is_numeric_dtype(dtype):
continue
df = df[df[col] > 0]
# If there are duplicate IDPs for a patient, just keep the mean
# of that patient's IDP values.
df = df.groupby(self._id).mean().reset_index()
if data is None:
data = df
continue
data = pd.merge(data, df, on=self._id)
# Import ICD-9 data.
df = pd.read_csv(self.icd_datapath, sep="\t")
cols = df.columns.drop(self._id)
def nonzero(row, cols): return [float(y) for y in cols[~(row == 0)]]
diagnoses = df.drop([self._id], axis=1).apply(
lambda x: nonzero(x, cols), axis=1
)
diagnoses = diagnoses.to_frame(name=self.diagnoses_key)
diagnoses[self._id] = df[self._id]
return data, diagnoses
def visualize(
self,
savedir: Optional[Union[Path, str]] = None,
n_components: int = 1,
dim_reduction_method: str = "PCA",
use_labels: bool = True,
verbose: bool = True
) -> None:
"""
Plots IDP principal component(s) as a function of different diagnoses.
Input:
savedir: directory to save plots to. Default no plots saved.
n_components: number of principal components to plot. One of
[1, 2]. Default 1.
dim_reduction_method: dimensionality reduction method. One of
[`PCA`, `TSNE`]. Default `PCA`.
use_labels: whether to plot labels using (a), (b), (c), etc.
verbose: flag for verbose outputs.
Returns:
None.
"""
idp_X = self.data.drop([self._id], axis=1).to_numpy()
idp_Z = StandardScaler().fit_transform(idp_X)
n_components = min(max(n_components, 1), 2)
if dim_reduction_method.upper() == "TSNE":
idp_embedding = TSNE(
n_components=n_components,
learning_rate="auto",
init="pca",
perplexity=50
).fit_transform(idp_Z)
elif dim_reduction_method.upper() == "PCA":
pca = PCA(n_components=n_components)
idp_embedding = pca.fit_transform(idp_Z)
print("Explained Variance (%):", pca.explained_variance_ratio_)
else:
raise ValueError(
f"Unrecognized dim reduction method {dim_reduction_method}"
)
label = 97 # Unicode for `a`.
for (diagnosis, (icd_low, icd_high)), co in zip(
self.icds_references.items(), self.colors
):
positive = set([])
negative = set([])
for labels, _id in zip(
self.labels[self.diagnoses_key], self.labels[self._id]
):
labels = np.array(labels)
if np.any((icd_low <= labels) & (labels < icd_high)):
if _id in negative:
negative.remove(_id)
positive.add(_id)
else:
negative.add(_id)
positive = np.array(list(positive))
negative = np.array(list(negative))
positive = np.where(
np.in1d(self.data[self._id].to_numpy(), positive)
)
negative = np.where(
np.in1d(self.data[self._id].to_numpy(), negative)
)
plt.figure(figsize=(10, 5 * n_components))
if n_components == 2:
plt.scatter(
idp_embedding[negative, 0],
idp_embedding[negative, -1],
alpha=0.5,
color="#8f8f8f",
label="_nolegend_"
)
plt.scatter(
idp_embedding[positive, 0],
idp_embedding[positive, -1],
alpha=0.5,
color=co,
label=diagnosis
)
if dim_reduction_method.upper() == "TSNE":
plt.xlabel("IDP t-SNE Dimension 1")
plt.ylabel("IDP t-SNE Dimension 2")
else:
plt.xlabel("IDP PCA Dimension 1")
plt.ylabel("IDP PCA Dimension 2")
plt.legend(loc="lower right")
elif n_components == 1:
idp_embedding = np.squeeze(idp_embedding)
num_pos, num_neg = len(positive[0]), len(negative[0])
if verbose:
print(
diagnosis + ":",
num_pos,
"[positive] /",
num_neg,
"[negative]"
)
plt.hist(
idp_embedding[negative],
color="#8f8f8f",
edgecolor="black",
label="_nolegend",
alpha=0.6,
bins=50,
weights=np.ones_like(idp_embedding[negative]) / num_neg
)
plt.hist(
idp_embedding[positive],
color=co,
edgecolor="black",
label=diagnosis,
alpha=0.7,
bins=50,
weights=np.ones_like(idp_embedding[positive]) / num_pos
)
p_value = ks_2samp(
idp_embedding[positive], idp_embedding[negative]
).pvalue
plt.annotate(
self.p_value_str(p_value),
xy=(0.025, 0.95),
xycoords="axes fraction",
color="black",
horizontalalignment="left",
verticalalignment="top"
)
if dim_reduction_method.upper() == "TSNE":
plt.xlabel("IDP t-SNE Dimension 1")
else:
plt.xlabel("IDP PCA Dimension 1")
plt.ylabel("Frequency")
plt.legend(loc="upper right")
if use_labels:
plt.annotate(
"(" + chr(label) + ")",
xy=(0.0, 1.05),
xycoords="axes fraction",
fontsize=24,
weight="bold"
)
label += 1
if savedir is None or len(savedir) == 0:
plt.show()
elif self.save_tif:
tmp = BytesIO()
plt.savefig(
tmp,
dpi=600,
transparent=self.transparent,
bbox_inches="tight",
format="png"
)
fin = Image.open(tmp)
fin.save(
os.path.join(
savedir, diagnosis.lower().replace(" ", "_") + ".tif"
),
)
fin.close()
else:
plt.savefig(
os.path.join(
savedir, diagnosis.lower().replace(" ", "_") + ".png"
),
transparent=self.transparent,
dpi=600,
bbox_inches="tight"
)
plt.close()
def plot_config(self) -> None:
"""
Plot configuration variables.
Input:
None.
Returns:
None.
"""
matplotlib.rcParams["mathtext.fontset"] = "stix"
if self.use_sans_serif:
matplotlib.rcParams['font.family'] = "Arial"
else:
matplotlib.rcParams["font.family"] = "STIXGeneral"
matplotlib.rcParams.update({"font.size": self.font_size})
self.colors = [
"#55752F",
"#E37E00",
"#6E8E84",
"#90353B",
"#1A476F",
"#938DD2",
"#C10534",
"#CAC27E",
"#A0522D",
"#7B92A8",
"#2D6D66",
"#9C8847",
"#BFA19C",
"#FFD200",
"#89969B"
]
def p_value_str(self, p: float) -> str:
"""
Converts a p value into an string annotation for plots.
Input:
p: input p value (assumed to be less than 1).
Returns:
Formatted p value string.
"""
if p > 1 or p <= 0:
raise ValueError("p values should be between 0 and 1, got {p}.")
if p < 0.001:
return r"$p < 0.001$"
elif p < 0.01 or 0.04 < p < 0.05:
return fr"$p = {p:.3f}$"
return fr"$p = {p:.2f}$"
def build_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="PMBB IDP Explorer")
datadir = "./data"
parser.add_argument(
"--idp_datapaths",
type=str,
nargs="+",
default=[
os.path.join(datadir, "steatosis_run_2_merge.csv"),
os.path.join(datadir, "visceral_merge_log_run_11_1_20.csv")
],
help="Filepaths to CSV files with PMBB IDP data."
)
parser.add_argument(
"--icd_datapath",
type=str,
default=os.path.join(
datadir,
"PMBB-Release-2020-2.2_phenotype_PheCode-matrix-ct-studies.txt"
),
help="Filepath to file with PMBB ICD-9 diagnosis code data."
)
parser.add_argument(
"--savedir",
type=str,
default=None,
help="Directory to save plots to. Default plots are not saved."
)
parser.add_argument(
"--dim_reduction_method",
type=str,
default="PCA",
choices=["PCA", "TSNE"],
help="Dimensionality reduction method. One of [`PCA`, `TSNE`]."
)
parser.add_argument(
"--n_components",
type=int,
default=1,
choices=[1, 2],
help="Number of principal components to plot. One of [`1`, `2`]."
)
parser.add_argument(
"--add_sublabels",
action="store_true",
help="Whether to add figure sublabeling, i.e. (a). Default no labels."
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Optional random seed. Default 42."
)
parser.add_argument(
"--font_size",
type=int,
default=20,
help="Font size for plotting. Default 20."
)
parser.add_argument(
"--use_sans_serif",
action="store_true",
help="Whether to use Sans Serif font for plotting. Default False."
)
parser.add_argument(
"--save_tif",
action="store_true",
help="Saves figures as tif files instead of png files. Default False."
)
parser.add_argument(
"--transparent",
action="store_true",
help="Saves transparent figures. Default False."
)
return parser.parse_args()
def main():
args = build_args()
explorer = IDPExplorer(
idp_datapaths=args.idp_datapaths,
icd_datapath=args.icd_datapath,
seed=args.seed,
font_size=args.font_size,
use_sans_serif=args.use_sans_serif,
save_tif=args.save_tif,
transparent=args.transparent
)
if args.savedir is not None and len(args.savedir) > 0:
if not os.path.isdir(args.savedir):
os.mkdir(args.savedir)
explorer.visualize(
savedir=args.savedir,
n_components=args.n_components,
dim_reduction_method=args.dim_reduction_method,
use_labels=args.add_sublabels,
verbose=True
)
if __name__ == "__main__":
main()