-
Notifications
You must be signed in to change notification settings - Fork 0
/
phenotyping.py
614 lines (458 loc) · 20.1 KB
/
phenotyping.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
# coding=utf-8
# MIT License
# Copyright (c) 2022 Carnegie Mellon University, Auton Lab
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Utilities to phenotype individuals based on similar survival
characteristics."""
from random import random
import numpy as np
import pandas as pd
from copy import deepcopy
from sklearn import cluster, decomposition, mixture
from sklearn.metrics import auc
from auton_survival.utils import _get_method_kwargs
from auton_survival.experiments import CounterfactualSurvivalRegressionCV
class Phenotyper:
"""Base class for all phenotyping methods."""
def __init__(self, random_seed=0):
self.random_seed = random_seed
self.fitted = False
class IntersectionalPhenotyper(Phenotyper):
"""A phenotyper that phenotypes by performing an exhaustive cartesian
product on prespecified set of categorical and numerical variables.
Parameters
-----------
cat_vars : list of python str(s), default=None
List of column names of categorical variables to phenotype on.
num_vars : list of python str(s), default=None
List of column names of continuous variables to phenotype on.
num_vars_quantiles : tuple of floats, default=(0, .5, 1.0)
A tuple of quantiles as floats (inclusive of 0 and 1) used to
discretize continuous variables into equal-sized bins.
features : pd.DataFrame
A pandas dataframe with rows corresponding to individual
samples and columns as covariates.
phenotypes : list
List of lists containing all possible combinations of specified
categorical and numerical variable values.
"""
def __init__(self, cat_vars=None, num_vars=None,
num_vars_quantiles=(0, .5, 1.0), random_seed=0):
super().__init__(random_seed=random_seed)
if isinstance(cat_vars, str): cat_vars = [cat_vars]
if isinstance(num_vars, str): num_vars = [num_vars]
if cat_vars is None: cat_vars = []
if num_vars is None: num_vars = []
assert len(cat_vars+num_vars) != 0, "Please specify intersectional Groups"
self.cat_vars = cat_vars
self.num_vars = num_vars
self.num_vars_quantiles = num_vars_quantiles
self.fitted = False
def fit(self, features):
"""Fit the phenotyper by finding all possible intersectional groups
on a passed set of features.
Parameters
-----------
features : pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
Trained instance of intersectional phenotyper.
"""
self.cut_bins = {}
self.min_max = {}
for num_var in self.num_vars:
binmin = float(features[num_var].min())
binmax = float(features[num_var].max())
self.min_max[num_var] = binmin, binmax
_, self.cut_bins[num_var] = pd.qcut(features[num_var],
self.num_vars_quantiles,
retbins=True)
self.fitted = True
return self
def predict(self, features):
"""Phenotype out of sample test data.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
--------
np.array:
a numpy array containing a list of strings that define
subgroups from all possible combinations of specified categorical
and numerical variables.
"""
assert self.fitted, "Phenotyper must be `fitted` before calling `phenotype`."
features = deepcopy(features)
for num_var in self.num_vars:
var_min, var_max = self.min_max[num_var]
features.loc[features[num_var]>=var_max, [num_var]] = var_max
features.loc[features[num_var]<=var_min, [num_var]] = var_min
features[num_var] = pd.cut(features[num_var], self.cut_bins[num_var],
include_lowest=True)
phenotypes = [group.tolist() for group in features[self.cat_vars+self.num_vars].values]
phenotypes = self._rename(phenotypes)
phenotypes = np.array(phenotypes)
return phenotypes
def _rename(self, phenotypes):
"""Helper function to clean the phenotype names.
Parameters
-----------
phenotypes : list
List of lists containing all possible combinations of specified
categorical and numerical variable values.
Returns
--------
list:
python list of a list of strings that define subgroups.
"""
ft_names = self.cat_vars + self.num_vars
renamed = []
for i in range(len(phenotypes)):
row = []
for j in range(len(ft_names)):
row.append(ft_names[j]+":"+str(phenotypes[i][j]))
renamed.append(" & ".join(row))
return renamed
def fit_predict(self, features):
"""Fit and perform phenotyping on a given dataset.
Parameters
-----------
features : pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array:
A numpy array containing a list of strings that define
subgroups from all possible combinations of specified categorical
and numerical variables.
"""
return self.fit(features).predict(features)
class ClusteringPhenotyper(Phenotyper):
"""Phenotyper that performs dimensionality reduction followed by clustering.
Learned clusters are considered phenotypes and used to group samples based
on similarity in the covariate space.
Parameters
-----------
features: pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
clustering_method : str, default='kmeans'
The clustering method applied for phenotyping.
Options include:
- `kmeans`: K-Means Clustering
- `dbscan`: Density-Based Spatial Clustering of Applications with
Noise (DBSCAN)
- `gmm`: Gaussian Mixture
- `hierarchical`: Agglomerative Clustering
dim_red_method: str, default=None
The dimensionality reductions method applied.
Options include:
- `pca` : Principal Component Analysis
- `kpca` : Kernel Principal Component Analysis
- `nnmf` : Non-Negative Matrix Factorization
- None : dimensionality reduction is not applied.
random_seed : int, default=0
Controls the randomness and reproducibility of called functions
kwargs: dict
Additional arguments for dimensionality reduction and clustering
Please include dictionary key and item pairs specified by the following
scikit-learn modules:
- `pca` : sklearn.decomposition.PCA
- `nnmf` : sklearn.decomposition.NMF
- `kpca` : sklearn.decomposition.KernelPCA
- `kmeans` : sklearn.cluster.KMeans
- `dbscan` : sklearn.cluster.DBSCAN
- `gmm` : sklearn.mixture.GaussianMixture
- `hierarchical` : sklearn.cluster.AgglomerativeClustering
"""
_VALID_DIMRED_METHODS = ['pca', 'kpca', 'nnmf', None]
_VALID_CLUSTERING_METHODS = ['kmeans', 'dbscan', 'gmm', 'hierarchical']
def __init__(self, clustering_method = 'kmeans', dim_red_method = None,
random_seed=0, **kwargs):
assert clustering_method in ClusteringPhenotyper._VALID_CLUSTERING_METHODS, "Please specify a valid Clustering method"
assert dim_red_method in ClusteringPhenotyper._VALID_DIMRED_METHODS, "Please specify a valid Dimensionality Reduction method"
# Raise warning if "hierarchical" is used with dim_redcution
if (clustering_method in ['hierarchical']) and (dim_red_method is not None):
print("WARNING: Are you sure you want to run hierarchical clustering on decomposed features?.",
"Such behaviour is atypical.")
# Dimensionality Reduction Step:
if dim_red_method is not None:
if dim_red_method == 'pca':
dim_red_model = decomposition.PCA
elif dim_red_method == 'nnmf':
dim_red_model = decomposition.NMF
elif 'kpca' in dim_red_method:
dim_red_model = decomposition.KernelPCA
else:
raise NotImplementedError("Dimensionality Reduction method: "+dim_red_method+ " Not Implemented.")
if clustering_method == 'kmeans':
clustering_model= cluster.KMeans
elif clustering_method == 'dbscan':
clustering_model = cluster.DBSCAN
elif clustering_method == 'gmm':
clustering_model = mixture.GaussianMixture
elif clustering_method == 'hierarchical':
clustering_model = cluster.AgglomerativeClustering
else:
raise NotImplementedError("Clustering method: "+clustering_method+ " Not Implemented.")
self.clustering_method = clustering_method
self.dim_red_method = dim_red_method
c_kwargs = _get_method_kwargs(clustering_model, kwargs)
if clustering_method == 'gmm':
if 'covariance_type' not in c_kwargs:
c_kwargs['covariance_type'] = 'diag'
c_kwargs['n_components'] = c_kwargs.get('n_clusters', 3)
self.clustering_model = clustering_model(random_state=random_seed,
**c_kwargs)
if dim_red_method is not None:
d_kwargs = _get_method_kwargs(dim_red_model, kwargs)
if dim_red_method == 'kpca':
if 'kernel' not in d_kwargs:
d_kwargs['kernel'] = 'rbf'
d_kwargs['n_jobs'] = -1
d_kwargs['max_iter'] = 500
self.dim_red_model = dim_red_model(random_state=random_seed,
**d_kwargs)
def fit(self, features):
"""Perform dimensionality reduction and train an instance
of the clustering algorithm.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual
samples and columns as covariates.
Returns
--------
Trained instance of clustering phenotyper.
"""
if self.dim_red_method is not None:
print("Fitting the following Dimensionality Reduction Model:\n",
self.dim_red_model)
self.dim_red_model = self.dim_red_model.fit(features)
features = self.dim_red_model.transform(features)
else:
print("No Dimensionaity reduction specified...\n Proceeding to learn clusters with the raw features...")
print("Fitting the following Clustering Model:\n", self.clustering_model)
self.clustering_model = self.clustering_model.fit(features)
self.fitted = True
return self
def _predict_proba_kmeans(self, features):
"""Estimate the probability of belonging to a cluster by computing
the distance to cluster center normalized by the sum of distances
to other clusters.
Parameters
-----------
features: pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array:
A numpy array of probability estimates of sample association to
learned subgroups.
"""
#TODO:MAYBE DO THIS IN LOG SPACE?
negative_exp_distances = np.exp(-self.clustering_model.transform(features))
probs = negative_exp_distances/negative_exp_distances.sum(axis=1).reshape((-1, 1))
#assert int(np.sum(probs)) == len(probs), 'Not valid probabilities'
return probs
def predict_proba(self, features):
"""Peform dimensionality reduction, clustering, and estimate probability
estimates of sample association to learned clusters, or subgroups.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array:
a numpy array of the probability estimates of sample association to
learned subgroups.
"""
assert self.fitted, "Phenotyper must be `fitted` before calling \
`phenotype`."
if self.dim_red_method is not None:
features = self.dim_red_model.transform(features)
if self.clustering_method == 'gmm':
return self.clustering_model.predict_proba(features)
elif self.clustering_method == 'kmeans':
return self._predict_proba_kmeans(features)
def predict(self, features):
"""Peform dimensionality reduction, clustering, and extract phenogroups
that maximize the probability estimates of sample association to
specific learned clusters, or subgroups.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array:
a numpy array of phenogroup labels
"""
assert self.fitted, "Phenotyper must be `fitted` before calling \
`phenotype`."
return np.argmax(self.predict_proba(features), axis=1)
def fit_predict(self, features):
"""Fit and perform phenotyping on a given dataset.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array
a numpy array of the probability estimates of sample association
to learned clusters.
"""
return self.fit(features).predict(features)
class SurvivalVirtualTwinsPhenotyper(Phenotyper):
"""Phenotyper that estimates the potential outcomes under treatment and
control using a counterfactual Deep Cox Proportional Hazards model,
followed by regressing the difference of the estimated counterfactual
Restricted Mean Survival Times using a Random Forest regressor."""
_VALID_PHENO_METHODS = ['rfr']
_DEFAULT_PHENO_HYPERPARAMS = {}
_DEFAULT_PHENO_HYPERPARAMS['rfr'] = {'n_estimators': 50,
'max_depth': 5}
def __init__(self,
cf_method='dcph',
phenotyping_method='rfr',
cf_hyperparams=None,
random_seed=0,
**phenotyper_hyperparams):
assert cf_method in CounterfactualSurvivalRegressionCV._VALID_CF_METHODS, "\
Invalid Counterfactual Method: "+cf_method
assert phenotyping_method in self._VALID_PHENO_METHODS, "Invalid Phenotyping Method:\
"+phenotyping_method
self.cf_method = cf_method
self.phenotyping_method = phenotyping_method
if cf_hyperparams is None:
cf_hyperparams = {}
self.phenotyper_hyperparams = phenotyper_hyperparams
self.cf_hyperparams = cf_hyperparams
self.random_seed = random_seed
def fit(self, features, outcomes, interventions, metric,
horizon):
"""Fit a counterfactual model and regress the difference of the estimated
counterfactual Restricted Mean Survival Time using a Random Forest regressor.
Parameters
-----------
features: pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
outcomes : pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns 'time' and 'event'.
interventions : np.array
Boolean numpy array of treatment indicators. True means individual
was assigned a specific treatment.
metric : str, default='ibs'
Metric used to evaluate model performance and tune hyperparameters.
Options include:
- 'auc': Dynamic area under the ROC curve
- 'brs' : Brier Score
- 'ibs' : Integrated Brier Score
- 'ctd' : Concordance Index
horizon : np.float
The event horizon at which to compute the counterfacutal RMST for
regression.
Returns
-----------
Trained instance of Survival Virtual Twins Phenotyer.
"""
cf_model = CounterfactualSurvivalRegressionCV(model=self.cf_method,
hyperparam_grid=self.cf_hyperparams)
self.cf_model = cf_model.fit(features, outcomes, interventions, metric)
times = np.unique(outcomes.time.values)
cf_predictions = self.cf_model.predict_counterfactual_survival(features,
times.tolist())
ite_estimates = cf_predictions[1] - cf_predictions[0]
ite_estimates = [estimate[times < horizon] for estimate in ite_estimates]
times = times[times < horizon]
# Compute rmst for each sample based on user-specified event-horizon
rmst = np.array([auc(times, i) for i in ite_estimates])
if self.phenotyping_method == 'rfr':
from sklearn.ensemble import RandomForestRegressor
pheno_model = RandomForestRegressor(**self.phenotyper_hyperparams)
pheno_model.fit(features.values, rmst)
self.pheno_model = pheno_model
self.fitted = True
return self
def predict_proba(self, features):
"""Estimate the probability that the Restrictred Mean Survival Time under
the Treatment group is greater than that under the control group.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array
a numpy array of the phenogroup probabilties in the format
[control_group, treated_group].
"""
phenotype_preds= self.pheno_model.predict(features)
preds_surv_greater = (phenotype_preds - phenotype_preds.min()) / (phenotype_preds.max() - phenotype_preds.min())
preds_surv_less = 1 - preds_surv_greater
preds = np.array([[preds_surv_less[i], preds_surv_greater[i]]
for i in range(len(features))])
return preds
def predict(self, features):
"""Extract phenogroups that maximize probability estimates.
Parameters
-----------
features: pd.DataFrame
a pandas dataframe with rows corresponding to individual samples
and columns as covariates.
Returns
-----------
np.array
a numpy array of the phenogroup labels
"""
return np.argmax(self.predict_proba(features), axis=1)
def fit_predict(self, features, outcomes, interventions, horizon):
"""Fit and perform phenotyping on a given dataset.
Parameters
-----------
features: pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns as covariates.
outcomes : pd.DataFrame
A pandas dataframe with rows corresponding to individual samples
and columns 'time' and 'event'.
treatment_indicator : np.array
Boolean numpy array of treatment indicators. True means individual
was assigned a specific treatment.
horizon : np.float
The event horizon at which to compute the counterfacutal RMST for
regression.
Returns
-----------
np.array
a numpy array of the phenogroup labels.
"""
return self.fit(features, outcomes, interventions, horizon).predict(features)