-
Notifications
You must be signed in to change notification settings - Fork 62
/
AbstractSustain.py
1125 lines (876 loc) · 56.8 KB
/
AbstractSustain.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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###
# pySuStaIn: a Python implementation of the Subtype and Stage Inference (SuStaIn) algorithm
#
# If you use pySuStaIn, please cite the following core papers:
# 1. The original SuStaIn paper: https://doi.org/10.1038/s41467-018-05892-0
# 2. The pySuStaIn software paper: https://doi.org/10.1016/j.softx.2021.100811
#
# Please also cite the corresponding progression pattern model you use:
# 1. The piece-wise linear z-score model (i.e. ZscoreSustain): https://doi.org/10.1038/s41467-018-05892-0
# 2. The event-based model (i.e. MixtureSustain): https://doi.org/10.1016/j.neuroimage.2012.01.062
# with Gaussian mixture modeling (i.e. 'mixture_gmm'): https://doi.org/10.1093/brain/awu176
# or kernel density estimation (i.e. 'mixture_kde'): https://doi.org/10.1002/alz.12083
# 3. The model for discrete ordinal data (i.e. OrdinalSustain): https://doi.org/10.3389/frai.2021.613261
#
# Thanks a lot for supporting this project.
#
# Authors: Peter Wijeratne (p.wijeratne@ucl.ac.uk) and Leon Aksman (leon.aksman@loni.usc.edu)
# Contributors: Arman Eshaghi (a.eshaghi@ucl.ac.uk), Alex Young (alexandra.young@kcl.ac.uk), Cameron Shand (c.shand@ucl.ac.uk)
###
from abc import ABC, abstractmethod
from tqdm.auto import tqdm
import numpy as np
import scipy.stats as stats
from matplotlib import pyplot as plt
import matplotlib.colors as mcolors
from pathlib import Path
import pickle
import csv
import os
import multiprocessing
from functools import partial, partialmethod
import time
import pathos
#*******************************************
#The data structure class for AbstractSustain. It has no data itself - the implementations of AbstractSustain need to define their own implementations of this class.
class AbstractSustainData(ABC):
@abstractmethod
def __init__(self):
pass
@abstractmethod
def getNumSamples(self):
pass
@abstractmethod
def getNumBiomarkers(self):
pass
@abstractmethod
def getNumStages(self):
pass
@abstractmethod
def reindex(self, index):
pass
#*******************************************
class AbstractSustain(ABC):
def __init__(self,
sustainData,
N_startpoints,
N_S_max,
N_iterations_MCMC,
output_folder,
dataset_name,
use_parallel_startpoints,
seed=None):
# The initializer for the abstract class
# Parameters:
# sustainData - an instance of an AbstractSustainData implementation
# N_startpoints - number of startpoints to use in maximum likelihood step of SuStaIn, typically 25
# N_S_max - maximum number of subtypes, should be 1 or more
# N_iterations_MCMC - number of MCMC iterations, typically 1e5 or 1e6 but can be lower for debugging
# output_folder - where to save pickle files, etc.
# dataset_name - for naming pickle files
# use_parallel_startpoints - boolean for whether or not to parallelize the maximum likelihood loop
# seed - random number seed
assert(isinstance(sustainData, AbstractSustainData))
self.__sustainData = sustainData
self.N_startpoints = N_startpoints
self.N_S_max = N_S_max
self.N_iterations_MCMC = N_iterations_MCMC
self.num_cores = multiprocessing.cpu_count()
self.output_folder = output_folder
self.dataset_name = dataset_name
if isinstance(seed, int):
self.seed = seed
elif isinstance(seed, float):
self.seed = int(seed)
elif seed is None:
# Select random seed if none given
self.seed = np.random.default_rng().integers((2**32)-1)
# Create global rng to create process-specific rngs
self.global_rng = np.random.default_rng(self.seed)
self.use_parallel_startpoints = use_parallel_startpoints
if self.use_parallel_startpoints:
np_version = float(np.__version__.split('.')[0] + '.' + np.__version__.split('.')[1])
assert np_version >= 1.18, "numpy version must be >= 1.18 for parallelization to work properly."
self.pool = pathos.multiprocessing.ProcessingPool() #pathos.multiprocessing.ParallelPool()
self.pool.ncpus = multiprocessing.cpu_count()
else:
self.pool = pathos.serial.SerialPool()
#********************* PUBLIC METHODS
def run_sustain_algorithm(self, plot=False, plot_format="png", **kwargs):
# Externally called method to start the SuStaIn algorithm after initializing the SuStaIn class object properly
ml_sequence_prev_EM = []
ml_f_prev_EM = []
pickle_dir = os.path.join(self.output_folder, 'pickle_files')
if not os.path.isdir(pickle_dir):
os.mkdir(pickle_dir)
if plot:
fig0, ax0 = plt.subplots()
for s in range(self.N_S_max):
pickle_filename_s = os.path.join(pickle_dir, self.dataset_name + '_subtype' + str(s) + '.pickle')
pickle_filepath = Path(pickle_filename_s)
if pickle_filepath.exists():
print("Found pickle file: " + pickle_filename_s + ". Using pickled variables for " + str(s) + " subtype.")
pickle_file = open(pickle_filename_s, 'rb')
loaded_variables = pickle.load(pickle_file)
#self.stage_zscore = loaded_variables["stage_zscore"]
#self.stage_biomarker_index = loaded_variables["stage_biomarker_index"]
#self.N_S_max = loaded_variables["N_S_max"]
samples_likelihood = loaded_variables["samples_likelihood"]
samples_sequence = loaded_variables["samples_sequence"]
samples_f = loaded_variables["samples_f"]
ml_sequence_EM = loaded_variables["ml_sequence_EM"]
ml_sequence_prev_EM = loaded_variables["ml_sequence_prev_EM"]
ml_f_EM = loaded_variables["ml_f_EM"]
ml_f_prev_EM = loaded_variables["ml_f_prev_EM"]
pickle_file.close()
else:
print("Failed to find pickle file: " + pickle_filename_s + ". Running SuStaIn model for " + str(s) + " subtype.")
ml_sequence_EM, \
ml_f_EM, \
ml_likelihood_EM, \
ml_sequence_mat_EM, \
ml_f_mat_EM, \
ml_likelihood_mat_EM = self._estimate_ml_sustain_model_nplus1_clusters(self.__sustainData, ml_sequence_prev_EM, ml_f_prev_EM) #self.__estimate_ml_sustain_model_nplus1_clusters(self.__data, ml_sequence_prev_EM, ml_f_prev_EM)
seq_init = ml_sequence_EM
f_init = ml_f_EM
ml_sequence, \
ml_f, \
ml_likelihood, \
samples_sequence, \
samples_f, \
samples_likelihood = self._estimate_uncertainty_sustain_model(self.__sustainData, seq_init, f_init) #self.__estimate_uncertainty_sustain_model(self.__data, seq_init, f_init)
ml_sequence_prev_EM = ml_sequence_EM
ml_f_prev_EM = ml_f_EM
# max like subtype and stage / subject
N_samples = 1000
ml_subtype, \
prob_ml_subtype, \
ml_stage, \
prob_ml_stage, \
prob_subtype, \
prob_stage, \
prob_subtype_stage = self.subtype_and_stage_individuals(self.__sustainData, samples_sequence, samples_f, N_samples) #self.subtype_and_stage_individuals(self.__data, samples_sequence, samples_f, N_samples)
if not pickle_filepath.exists():
if not os.path.exists(self.output_folder):
os.makedirs(self.output_folder)
save_variables = {}
save_variables["samples_sequence"] = samples_sequence
save_variables["samples_f"] = samples_f
save_variables["samples_likelihood"] = samples_likelihood
save_variables["ml_subtype"] = ml_subtype
save_variables["prob_ml_subtype"] = prob_ml_subtype
save_variables["ml_stage"] = ml_stage
save_variables["prob_ml_stage"] = prob_ml_stage
save_variables["prob_subtype"] = prob_subtype
save_variables["prob_stage"] = prob_stage
save_variables["prob_subtype_stage"] = prob_subtype_stage
save_variables["ml_sequence_EM"] = ml_sequence_EM
save_variables["ml_sequence_prev_EM"] = ml_sequence_prev_EM
save_variables["ml_f_EM"] = ml_f_EM
save_variables["ml_f_prev_EM"] = ml_f_prev_EM
pickle_file = open(pickle_filename_s, 'wb')
pickle_output = pickle.dump(save_variables, pickle_file)
pickle_file.close()
n_samples = self.__sustainData.getNumSamples() #self.__data.shape[0]
#order of subtypes displayed in positional variance diagrams plotted by _plot_sustain_model
self._plot_subtype_order = np.argsort(ml_f_EM)[::-1]
#order of biomarkers in each subtypes' positional variance diagram
self._plot_biomarker_order = ml_sequence_EM[self._plot_subtype_order[0], :].astype(int)
# plot results
if plot:
figs, ax = self._plot_sustain_model(
samples_sequence=samples_sequence,
samples_f=samples_f,
n_samples=n_samples,
biomarker_labels=self.biomarker_labels,
subtype_order=self._plot_subtype_order,
biomarker_order=self._plot_biomarker_order,
save_path=Path(self.output_folder) / f"{self.dataset_name}_subtype{s}_PVD.{plot_format}",
**kwargs
)
for fig in figs:
fig.show()
ax0.plot(range(self.N_iterations_MCMC), samples_likelihood, label="Subtype " + str(s+1))
# save and show this figure after all subtypes have been calculcated
if plot:
ax0.legend(loc='upper right')
fig0.tight_layout()
fig0.savefig(Path(self.output_folder) / f"MCMC_likelihoods.{plot_format}", bbox_inches='tight')
fig0.show()
return samples_sequence, samples_f, ml_subtype, prob_ml_subtype, ml_stage, prob_ml_stage, prob_subtype_stage
def cross_validate_sustain_model(self, test_idxs, select_fold = [], plot=False):
# Cross-validate the SuStaIn model by running the SuStaIn algorithm (E-M
# and MCMC) on a training dataset and evaluating the model likelihood on a test
# dataset.
# Parameters:
# 'test_idxs' - list of test set indices for each fold
# 'select_fold' - allows user to just run for a single fold (allows the cross-validation to be run in parallel).
# leave this variable empty to iterate across folds sequentially.
if not os.path.exists(self.output_folder):
os.makedirs(self.output_folder)
pickle_dir = os.path.join(self.output_folder, 'pickle_files')
if not os.path.isdir(pickle_dir):
os.mkdir(pickle_dir)
if select_fold != []:
if np.isscalar(select_fold):
select_fold = [select_fold]
else:
select_fold = np.arange(len(test_idxs)) #test_idxs
Nfolds = len(select_fold)
is_full = Nfolds == len(test_idxs)
loglike_matrix = np.zeros((Nfolds, self.N_S_max))
for fold in tqdm(select_fold, "Folds: ", Nfolds, position=0, leave=True):
indx_test = test_idxs[fold]
indx_train = np.array([x for x in range(self.__sustainData.getNumSamples()) if x not in indx_test])
sustainData_train = self.__sustainData.reindex(indx_train)
sustainData_test = self.__sustainData.reindex(indx_test)
ml_sequence_prev_EM = []
ml_f_prev_EM = []
for s in range(self.N_S_max):
pickle_filename_fold_s = os.path.join(pickle_dir, self.dataset_name + '_fold' + str(fold) + '_subtype' + str(s) + '.pickle')
pickle_filepath = Path(pickle_filename_fold_s)
if pickle_filepath.exists():
print("Loading " + pickle_filename_fold_s)
pickle_file = open(pickle_filename_fold_s, 'rb')
loaded_variables = pickle.load(pickle_file)
ml_sequence_EM = loaded_variables["ml_sequence_EM"]
ml_sequence_prev_EM = loaded_variables["ml_sequence_prev_EM"]
ml_f_EM = loaded_variables["ml_f_EM"]
ml_f_prev_EM = loaded_variables["ml_f_prev_EM"]
samples_likelihood = loaded_variables["samples_likelihood"]
samples_sequence = loaded_variables["samples_sequence"]
samples_f = loaded_variables["samples_f"]
mean_likelihood_subj_test = loaded_variables["mean_likelihood_subj_test"]
pickle_file.close()
samples_likelihood_subj_test = self._evaluate_likelihood_setofsamples(sustainData_test, samples_sequence, samples_f)
else:
ml_sequence_EM, \
ml_f_EM, \
ml_likelihood_EM, \
ml_sequence_mat_EM, \
ml_f_mat_EM, \
ml_likelihood_mat_EM = self._estimate_ml_sustain_model_nplus1_clusters(sustainData_train, ml_sequence_prev_EM, ml_f_prev_EM)
seq_init = ml_sequence_EM
f_init = ml_f_EM
ml_sequence, \
ml_f, \
ml_likelihood, \
samples_sequence, \
samples_f, \
samples_likelihood = self._estimate_uncertainty_sustain_model(sustainData_train, seq_init, f_init)
samples_likelihood_subj_test = self._evaluate_likelihood_setofsamples(sustainData_test, samples_sequence, samples_f)
mean_likelihood_subj_test = np.mean(samples_likelihood_subj_test,axis=1)
ml_sequence_prev_EM = ml_sequence_EM
ml_f_prev_EM = ml_f_EM
save_variables = {}
save_variables["ml_sequence_EM"] = ml_sequence_EM
save_variables["ml_sequence_prev_EM"] = ml_sequence_prev_EM
save_variables["ml_f_EM"] = ml_f_EM
save_variables["ml_f_prev_EM"] = ml_f_prev_EM
save_variables["samples_sequence"] = samples_sequence
save_variables["samples_f"] = samples_f
save_variables["samples_likelihood"] = samples_likelihood
save_variables["mean_likelihood_subj_test"] = mean_likelihood_subj_test
pickle_file = open(pickle_filename_fold_s, 'wb')
pickle_output = pickle.dump(save_variables, pickle_file)
pickle_file.close()
if is_full:
loglike_matrix[fold, s] = np.mean(np.sum(np.log(samples_likelihood_subj_test + 1e-250),axis=0))
if not is_full:
print("Cannot calculate CVIC and loglike_matrix without all folds. Rerun cross_validate_sustain_model after all folds calculated.")
return [], []
print(f"Average test set log-likelihood for each subtype model: {np.mean(loglike_matrix, 0)}")
if plot:
import pandas as pd
import pylab
df_loglike = pd.DataFrame(data = loglike_matrix, columns = ["Subtype " + str(i+1) for i in range(self.N_S_max)])
df_loglike.boxplot(grid=False, fontsize=15)
for i in range(self.N_S_max):
y = df_loglike[["Subtype " + str(i+1)]]
x = np.random.normal(1+i, 0.04, size=len(y)) # Add some random "jitter" to the x-axis
pylab.plot(x, y, 'r.', alpha=0.2)
pylab.savefig(Path(self.output_folder) / 'Log_likelihoods_cv_folds.png')
pylab.show()
CVIC = np.zeros(self.N_S_max)
for s in range(self.N_S_max):
for fold in range(Nfolds):
pickle_filename_fold_s = os.path.join(pickle_dir, self.dataset_name + '_fold' + str(fold) + '_subtype' + str(s) + '.pickle')
pickle_filepath = Path(pickle_filename_fold_s)
pickle_file = open(pickle_filename_fold_s, 'rb')
loaded_variables = pickle.load(pickle_file)
mean_likelihood_subj_test = loaded_variables["mean_likelihood_subj_test"]
pickle_file.close()
if fold == 0:
mean_likelihood_subj_test_cval = mean_likelihood_subj_test
else:
mean_likelihood_subj_test_cval = np.concatenate((mean_likelihood_subj_test_cval, mean_likelihood_subj_test), axis=0)
CVIC[s] = -2*sum(np.log(mean_likelihood_subj_test_cval))
print("CVIC for each subtype model: " + str(CVIC))
return CVIC, loglike_matrix
def combine_cross_validated_sequences(self, N_subtypes, N_folds, plot_format="png", **kwargs):
# Combine MCMC sequences across cross-validation folds to get cross-validated positional variance diagrams,
# so that you get more realistic estimates of variance within event positions within subtypes
pickle_dir = os.path.join(self.output_folder, 'pickle_files')
#*********** load ML sequence for full model for N_subtypes
pickle_filename_s = os.path.join(pickle_dir, self.dataset_name + '_subtype' + str(N_subtypes-1) + '.pickle')
pickle_filepath = Path(pickle_filename_s)
assert pickle_filepath.exists(), "Failed to find pickle file for full model with " + str(N_subtypes) + " subtypes."
pickle_file = open(pickle_filename_s, 'rb')
loaded_variables_full = pickle.load(pickle_file)
ml_sequence_EM_full = loaded_variables_full["ml_sequence_EM"]
ml_f_EM_full = loaded_variables_full["ml_f_EM"]
#REMOVED SO THAT PLOT_SUBTYPE_ORDER WORKS THE SAME HERE AS IN run_sustain_algorithm
#re-index so that subtypes are in descending order by fraction of subjects
# index_EM_sort = np.argsort(ml_f_EM_full)[::-1]
# ml_sequence_EM_full = ml_sequence_EM_full[index_EM_sort,:]
# ml_f_EM_full = ml_f_EM_full[index_EM_sort]
for i in range(N_folds):
#load the MCMC sequences for this fold's model of N_subtypes
pickle_filename_fold_s = os.path.join(pickle_dir, self.dataset_name + '_fold' + str(i) + '_subtype' + str(N_subtypes-1) + '.pickle')
pickle_filepath = Path(pickle_filename_fold_s)
assert pickle_filepath.exists(), "Failed to find pickle file for fold " + str(i)
pickle_file = open(pickle_filename_fold_s, 'rb')
loaded_variables_i = pickle.load(pickle_file)
ml_sequence_EM_i = loaded_variables_i["ml_sequence_EM"]
ml_f_EM_i = loaded_variables_i["ml_f_EM"]
samples_sequence_i = loaded_variables_i["samples_sequence"]
samples_f_i = loaded_variables_i["samples_f"]
mean_likelihood_subj_test = loaded_variables_i["mean_likelihood_subj_test"]
pickle_file.close()
# Really simple approach: choose order based on this fold's fraction of subjects per subtype
# It doesn't work very well when the fractions of subjects are similar across subtypes
#mean_f_i = np.mean(samples_f_i, 1)
#iMax_vec = np.argsort(mean_f_i)[::-1]
#iMax_vec = iMax_vec.astype(int)
#This approach seems to work better:
# 1. calculate the Kendall's tau correlation matrix,
# 2. Flatten the matrix into a vector
# 3. Sort the vector, then unravel the flattened indices back into matrix style (x, y) indices
# 4. Find the order in which this fold's subtypes first appear in the sorted list
corr_mat = np.zeros((N_subtypes, N_subtypes))
for j in range(N_subtypes):
for k in range(N_subtypes):
corr_mat[j,k] = stats.kendalltau(ml_sequence_EM_full[j,:], ml_sequence_EM_i[k,:]).correlation
set_full = []
set_fold_i = []
i_i, i_j = np.unravel_index(np.argsort(corr_mat.flatten())[::-1], (N_subtypes, N_subtypes))
for k in range(len(i_i)):
if not i_i[k] in set_full and not i_j[k] in set_fold_i:
set_full.append(i_i[k].astype(int))
set_fold_i.append(i_j[k].astype(int))
index_set_full = np.argsort(set_full).astype(int) #np.argsort(set_full)[::-1].astype(int)
iMax_vec = [set_fold_i[i] for i in index_set_full]
assert(np.all(np.sort(iMax_vec)==np.arange(N_subtypes)))
if i == 0:
samples_sequence_cval = samples_sequence_i[iMax_vec,:,:]
samples_f_cval = samples_f_i[iMax_vec, :]
else:
samples_sequence_cval = np.concatenate((samples_sequence_cval, samples_sequence_i[iMax_vec,:,:]), axis=2)
samples_f_cval = np.concatenate((samples_f_cval, samples_f_i[iMax_vec,:]), axis=1)
n_samples = self.__sustainData.getNumSamples()
#ADDED HERE BECAUSE THIS MAY BE CALLED BY CALLED FOR A RANGE OF N_S_max, AS IN simrun.py
# order of subtypes displayed in positional variance diagrams plotted by _plot_sustain_model
plot_subtype_order = np.argsort(ml_f_EM_full)[::-1]
# order of biomarkers in each subtypes' positional variance diagram
plot_biomarker_order = ml_sequence_EM_full[plot_subtype_order[0], :].astype(int)
figs, ax = self._plot_sustain_model(
samples_sequence=samples_sequence_cval,
samples_f=samples_f_cval,
n_samples=n_samples,
cval=True,
biomarker_labels=self.biomarker_labels,
subtype_order=plot_subtype_order,
biomarker_order=plot_biomarker_order,
**kwargs
)
# If saving is being done here
if "save_path" not in kwargs:
# Handle separated subtypes
if len(figs) > 1:
# Loop over each figure/subtype
for num_subtype, fig in zip(range(N_subtypes), figs):
# Nice confusing filename
plot_fname = Path(
self.output_folder
) / f"{self.dataset_name}_subtype{N_subtypes - 1}_subtype{num_subtype}-separated_PVD_{N_folds}fold_CV.{plot_format}"
# Save the figure
fig.savefig(plot_fname, bbox_inches='tight')
fig.show()
# Otherwise default single plot
else:
fig = figs[0]
# save and show this figure after all subtypes have been calculcated
plot_fname = Path(
self.output_folder
) / f"{self.dataset_name}_subtype{N_subtypes - 1}_PVD_{N_folds}fold_CV.{plot_format}"
# Save the figure
fig.savefig(plot_fname, bbox_inches='tight')
fig.show()
#return samples_sequence_cval, samples_f_cval, kendalls_tau_mat, f_mat #samples_sequence_cval
def subtype_and_stage_individuals(self, sustainData, samples_sequence, samples_f, N_samples):
# Subtype and stage a set of subjects. Useful for subtyping/staging subjects that were not used to build the model
nSamples = sustainData.getNumSamples() #data_local.shape[0]
nStages = sustainData.getNumStages() #self.stage_zscore.shape[1]
n_iterations_MCMC = samples_sequence.shape[2]
select_samples = np.round(np.linspace(0, n_iterations_MCMC - 1, N_samples))
N_S = samples_sequence.shape[0]
temp_mean_f = np.mean(samples_f, axis=1)
ix = np.argsort(temp_mean_f)[::-1]
prob_subtype_stage = np.zeros((nSamples, nStages + 1, N_S))
prob_subtype = np.zeros((nSamples, N_S))
prob_stage = np.zeros((nSamples, nStages + 1))
for i in range(N_samples):
sample = int(select_samples[i])
this_S = samples_sequence[ix, :, sample]
this_f = samples_f[ix, sample]
_, \
_, \
total_prob_stage, \
total_prob_subtype, \
total_prob_subtype_stage = self._calculate_likelihood(sustainData, this_S, this_f)
total_prob_subtype = total_prob_subtype.reshape(len(total_prob_subtype), N_S)
total_prob_subtype_norm = total_prob_subtype / np.tile(np.sum(total_prob_subtype, 1).reshape(len(total_prob_subtype), 1), (1, N_S))
total_prob_stage_norm = total_prob_stage / np.tile(np.sum(total_prob_stage, 1).reshape(len(total_prob_stage), 1), (1, nStages + 1)) #removed total_prob_subtype
#total_prob_subtype_stage_norm = total_prob_subtype_stage / np.tile(np.sum(np.sum(total_prob_subtype_stage, 1), 1).reshape(nSamples, 1, 1), (1, nStages + 1, N_S))
total_prob_subtype_stage_norm = total_prob_subtype_stage / np.tile(np.sum(np.sum(total_prob_subtype_stage, 1, keepdims=True), 2).reshape(nSamples, 1, 1),(1, nStages + 1, N_S))
prob_subtype_stage = (i / (i + 1.) * prob_subtype_stage) + (1. / (i + 1.) * total_prob_subtype_stage_norm)
prob_subtype = (i / (i + 1.) * prob_subtype) + (1. / (i + 1.) * total_prob_subtype_norm)
prob_stage = (i / (i + 1.) * prob_stage) + (1. / (i + 1.) * total_prob_stage_norm)
ml_subtype = np.nan * np.ones((nSamples, 1))
prob_ml_subtype = np.nan * np.ones((nSamples, 1))
ml_stage = np.nan * np.ones((nSamples, 1))
prob_ml_stage = np.nan * np.ones((nSamples, 1))
for i in range(nSamples):
this_prob_subtype = np.squeeze(prob_subtype[i, :])
if (np.sum(np.isnan(this_prob_subtype)) == 0):
this_subtype = np.where(this_prob_subtype == np.max(this_prob_subtype))
try:
ml_subtype[i] = this_subtype
except:
ml_subtype[i] = this_subtype[0][0]
if this_prob_subtype.size == 1 and this_prob_subtype == 1:
prob_ml_subtype[i] = 1
else:
try:
prob_ml_subtype[i] = this_prob_subtype[this_subtype]
except:
prob_ml_subtype[i] = this_prob_subtype[this_subtype[0][0]]
this_prob_stage = np.squeeze(prob_subtype_stage[i, :, int(ml_subtype[i])])
if (np.sum(np.isnan(this_prob_stage)) == 0):
this_stage = np.where(this_prob_stage == np.max(this_prob_stage))
ml_stage[i] = this_stage[0][0]
prob_ml_stage[i] = this_prob_stage[this_stage[0][0]]
return ml_subtype, prob_ml_subtype, ml_stage, prob_ml_stage, prob_subtype, prob_stage, prob_subtype_stage
# ********************* PROTECTED METHODS
def _estimate_ml_sustain_model_nplus1_clusters(self, sustainData, ml_sequence_prev, ml_f_prev):
# Given the previous SuStaIn model, estimate the next model in the
# hierarchy (i.e. number of subtypes goes from N to N+1)
#
#
# OUTPUTS:
# ml_sequence - the ordering of the stages for each subtype for the next SuStaIn model in the hierarchy
# ml_f - the most probable proportion of individuals belonging to each subtype for the next SuStaIn model in the hierarchy
# ml_likelihood - the likelihood of the most probable SuStaIn model for the next SuStaIn model in the hierarchy
N_S = len(ml_sequence_prev) + 1
if N_S == 1:
# If the number of subtypes is 1, fit a single linear z-score model
print('Finding ML solution to 1 cluster problem')
ml_sequence, \
ml_f, \
ml_likelihood, \
ml_sequence_mat, \
ml_f_mat, \
ml_likelihood_mat = self._find_ml(sustainData)
print('Overall ML likelihood is', ml_likelihood)
else:
# If the number of subtypes is greater than 1, go through each subtype
# in turn and try splitting into two subtypes
_, _, _, p_sequence, _ = self._calculate_likelihood(sustainData, ml_sequence_prev, ml_f_prev)
ml_sequence_prev = ml_sequence_prev.reshape(ml_sequence_prev.shape[0], ml_sequence_prev.shape[1])
p_sequence = p_sequence.reshape(p_sequence.shape[0], N_S - 1)
p_sequence_norm = p_sequence / np.tile(np.sum(p_sequence, 1).reshape(len(p_sequence), 1), (N_S - 1))
# Assign individuals to a subtype (cluster) based on the previous model
ml_cluster_subj = np.zeros((sustainData.getNumSamples(), 1)) #np.zeros((len(data_local), 1))
for m in range(sustainData.getNumSamples()): #range(len(data_local)):
ix = np.argmax(p_sequence_norm[m, :]) + 1
#TEMP: MATLAB comparison
#ml_cluster_subj[m] = ix*np.ceil(np.random.rand())
ml_cluster_subj[m] = ix # FIXME: should check this always works, as it differs to the Matlab code, which treats ix as an array
ml_likelihood = -np.inf
for ix_cluster_split in range(N_S - 1):
this_N_cluster = sum(ml_cluster_subj == int(ix_cluster_split + 1))
if this_N_cluster > 1:
# Take the data from the individuals belonging to a particular
# cluster and fit a two subtype model
print('Splitting cluster', ix_cluster_split + 1, 'of', N_S - 1)
ix_i = (ml_cluster_subj == int(ix_cluster_split + 1)).reshape(sustainData.getNumSamples(), )
sustainData_i = sustainData.reindex(ix_i)
print(' + Resolving 2 cluster problem')
this_ml_sequence_split, _, _, _, _, _ = self._find_ml_split(sustainData_i)
# Use the two subtype model combined with the other subtypes to
# inititialise the fitting of the next SuStaIn model in the
# hierarchy
this_seq_init = ml_sequence_prev.copy() # have to copy or changes will be passed to ml_sequence_prev
#replace the previous sequence with the first (row index zero) new sequence
this_seq_init[ix_cluster_split] = (this_ml_sequence_split[0]).reshape(this_ml_sequence_split.shape[1])
#add the second new sequence (row index one) to the stack of sequences,
#so that you now have N_S sequences instead of N_S-1
this_seq_init = np.hstack((this_seq_init.T, this_ml_sequence_split[1])).T
#initialize fraction of subjects in each subtype to be uniform
this_f_init = np.array([1.] * N_S) / float(N_S)
print(' + Finding ML solution from hierarchical initialisation')
this_ml_sequence, \
this_ml_f, \
this_ml_likelihood, \
this_ml_sequence_mat, \
this_ml_f_mat, \
this_ml_likelihood_mat = self._find_ml_mixture(sustainData, this_seq_init, this_f_init)
# Choose the most probable SuStaIn model from the different
# possible SuStaIn models initialised by splitting each subtype
# in turn
# FIXME: these arrays have an unnecessary additional axis with size = N_startpoints - remove it further upstream
if this_ml_likelihood[0] > ml_likelihood:
ml_likelihood = this_ml_likelihood[0]
ml_sequence = this_ml_sequence[:, :, 0]
ml_f = this_ml_f[:, 0]
ml_likelihood_mat = this_ml_likelihood_mat[0]
ml_sequence_mat = this_ml_sequence_mat[:, :, 0]
ml_f_mat = this_ml_f_mat[:, 0]
print('- ML likelihood is', this_ml_likelihood[0])
else:
print(f'Cluster {ix_cluster_split + 1} of {N_S - 1} too small for subdivision')
print(f'Overall ML likelihood is', ml_likelihood)
return ml_sequence, ml_f, ml_likelihood, ml_sequence_mat, ml_f_mat, ml_likelihood_mat
#********************************************
def _find_ml(self, sustainData):
# Fit the maximum likelihood model
#
# OUTPUTS:
# ml_sequence - the ordering of the stages for each subtype
# ml_f - the most probable proportion of individuals belonging to each subtype
# ml_likelihood - the likelihood of the most probable SuStaIn model
partial_iter = partial(self._find_ml_iteration, sustainData)
seed_sequences = np.random.SeedSequence(self.global_rng.integers(1e10))
pool_output_list = self.pool.map(partial_iter, seed_sequences.spawn(self.N_startpoints))
if ~isinstance(pool_output_list, list):
pool_output_list = list(pool_output_list)
ml_sequence_mat = np.zeros((1, sustainData.getNumStages(), self.N_startpoints)) #np.zeros((1, self.stage_zscore.shape[1], self.N_startpoints))
ml_f_mat = np.zeros((1, self.N_startpoints))
ml_likelihood_mat = np.zeros(self.N_startpoints)
for i in range(self.N_startpoints):
ml_sequence_mat[:, :, i] = pool_output_list[i][0]
ml_f_mat[:, i] = pool_output_list[i][1]
ml_likelihood_mat[i] = pool_output_list[i][2]
ix = np.argmax(ml_likelihood_mat)
ml_sequence = ml_sequence_mat[:, :, ix]
ml_f = ml_f_mat[:, ix]
ml_likelihood = ml_likelihood_mat[ix]
return ml_sequence, ml_f, ml_likelihood, ml_sequence_mat, ml_f_mat, ml_likelihood_mat
def _find_ml_iteration(self, sustainData, seed_seq):
#Convenience sub-function for above
# Get process-appropriate Generator
rng = np.random.default_rng(seed_seq)
# randomly initialise the sequence of the linear z-score model
seq_init = self._initialise_sequence(sustainData, rng)
f_init = [1]
this_ml_sequence, \
this_ml_f, \
this_ml_likelihood, \
_, \
_, \
_ = self._perform_em(sustainData, seq_init, f_init, rng)
return this_ml_sequence, this_ml_f, this_ml_likelihood
#********************************************
def _find_ml_split(self, sustainData):
# Fit a mixture of two models
#
#
# OUTPUTS:
# ml_sequence - the ordering of the stages for each subtype
# ml_f - the most probable proportion of individuals belonging to each subtype
# ml_likelihood - the likelihood of the most probable SuStaIn model
N_S = 2
partial_iter = partial(self._find_ml_split_iteration, sustainData)
seed_sequences = np.random.SeedSequence(self.global_rng.integers(1e10))
pool_output_list = self.pool.map(partial_iter, seed_sequences.spawn(self.N_startpoints))
if ~isinstance(pool_output_list, list):
pool_output_list = list(pool_output_list)
ml_sequence_mat = np.zeros((N_S, sustainData.getNumStages(), self.N_startpoints))
ml_f_mat = np.zeros((N_S, self.N_startpoints))
ml_likelihood_mat = np.zeros((self.N_startpoints, 1))
for i in range(self.N_startpoints):
ml_sequence_mat[:, :, i] = pool_output_list[i][0]
ml_f_mat[:, i] = pool_output_list[i][1]
ml_likelihood_mat[i] = pool_output_list[i][2]
ix = [np.where(ml_likelihood_mat == max(ml_likelihood_mat))[0][0]] #ugly bit of code to get first index where likelihood is maximum
ml_sequence = ml_sequence_mat[:, :, ix]
ml_f = ml_f_mat[:, ix]
ml_likelihood = ml_likelihood_mat[ix]
return ml_sequence, ml_f, ml_likelihood, ml_sequence_mat, ml_f_mat, ml_likelihood_mat
def _find_ml_split_iteration(self, sustainData, seed_seq):
#Convenience sub-function for above
# Get process-appropriate Generator
rng = np.random.default_rng(seed_seq)
N_S = 2
# randomly initialise individuals as belonging to one of the two subtypes (clusters)
min_N_cluster = 0
while min_N_cluster == 0:
vals = rng.random(sustainData.getNumSamples())
cluster_assignment = np.ceil(N_S * vals).astype(int)
# Count cluster sizes
# Guarantee 1s and 2s counts with minlength=3
# Ignore 0s count with [1:]
cluster_sizes = np.bincount(cluster_assignment, minlength=3)[1:]
# Get the minimum cluster size
min_N_cluster = cluster_sizes.min()
# initialise the stages of the two models by fitting a single model to each of the two sets of individuals
seq_init = np.zeros((N_S, sustainData.getNumStages()))
for s in range(N_S):
index_s = cluster_assignment.reshape(cluster_assignment.shape[0], ) == (s + 1)
temp_sustainData = sustainData.reindex(index_s)
temp_seq_init = self._initialise_sequence(sustainData, rng)
seq_init[s, :], _, _, _, _, _ = self._perform_em(temp_sustainData, temp_seq_init, [1], rng)
f_init = np.array([1.] * N_S) / float(N_S)
# optimise the mixture of two models from the initialisation
this_ml_sequence, \
this_ml_f, \
this_ml_likelihood, _, _, _ = self._perform_em(sustainData, seq_init, f_init, rng)
return this_ml_sequence, this_ml_f, this_ml_likelihood
#********************************************
def _find_ml_mixture(self, sustainData, seq_init, f_init):
# Fit a mixture of models
#
#
# OUTPUTS:
# ml_sequence - the ordering of the stages for each subtype for the next SuStaIn model in the hierarchy
# ml_f - the most probable proportion of individuals belonging to each subtype for the next SuStaIn model in the hierarchy
# ml_likelihood - the likelihood of the most probable SuStaIn model for the next SuStaIn model in the hierarchy
N_S = seq_init.shape[0]
partial_iter = partial(self._find_ml_mixture_iteration, sustainData, seq_init, f_init)
seed_sequences = np.random.SeedSequence(self.global_rng.integers(1e10))
pool_output_list = self.pool.map(partial_iter, seed_sequences.spawn(self.N_startpoints))
if ~isinstance(pool_output_list, list):
pool_output_list = list(pool_output_list)
ml_sequence_mat = np.zeros((N_S, sustainData.getNumStages(), self.N_startpoints))
ml_f_mat = np.zeros((N_S, self.N_startpoints))
ml_likelihood_mat = np.zeros((self.N_startpoints, 1))
for i in range(self.N_startpoints):
ml_sequence_mat[:, :, i] = pool_output_list[i][0]
ml_f_mat[:, i] = pool_output_list[i][1]
ml_likelihood_mat[i] = pool_output_list[i][2]
ix = np.where(ml_likelihood_mat == max(ml_likelihood_mat))
ix = ix[0]
ml_sequence = ml_sequence_mat[:, :, ix]
ml_f = ml_f_mat[:, ix]
ml_likelihood = ml_likelihood_mat[ix]
return ml_sequence, ml_f, ml_likelihood, ml_sequence_mat, ml_f_mat, ml_likelihood_mat
def _find_ml_mixture_iteration(self, sustainData, seq_init, f_init, seed_seq):
#Convenience sub-function for above
# Get process-appropriate Generator
rng = np.random.default_rng(seed_seq)
ml_sequence, \
ml_f, \
ml_likelihood, \
samples_sequence, \
samples_f, \
samples_likelihood = self._perform_em(sustainData, seq_init, f_init, rng)
return ml_sequence, ml_f, ml_likelihood, samples_sequence, samples_f, samples_likelihood
#********************************************
def _perform_em(self, sustainData, current_sequence, current_f, rng):
# Perform an E-M procedure to estimate parameters of SuStaIn model
MaxIter = 100
N = sustainData.getNumStages() #self.stage_zscore.shape[1]
N_S = current_sequence.shape[0]
current_likelihood, _, _, _, _ = self._calculate_likelihood(sustainData, current_sequence, current_f)
terminate = 0
iteration = 0
samples_sequence = np.nan * np.ones((MaxIter, N, N_S))
samples_f = np.nan * np.ones((MaxIter, N_S))
samples_likelihood = np.nan * np.ones((MaxIter, 1))
samples_sequence[0, :, :] = current_sequence.reshape(current_sequence.shape[1], current_sequence.shape[0])
current_f = np.array(current_f).reshape(len(current_f))
samples_f[0, :] = current_f
samples_likelihood[0] = current_likelihood
while terminate == 0:
candidate_sequence, \
candidate_f, \
candidate_likelihood = self._optimise_parameters(sustainData, current_sequence, current_f, rng)
HAS_converged = np.fabs((candidate_likelihood - current_likelihood) / max(candidate_likelihood, current_likelihood)) < 1e-6
if HAS_converged:
#print('EM converged in', iteration + 1, 'iterations')
terminate = 1
else:
if candidate_likelihood > current_likelihood:
current_sequence = candidate_sequence
current_f = candidate_f
current_likelihood = candidate_likelihood
samples_sequence[iteration, :, :] = current_sequence.T.reshape(current_sequence.T.shape[0], N_S)
samples_f[iteration, :] = current_f
samples_likelihood[iteration] = current_likelihood
if iteration == (MaxIter - 1):
terminate = 1
iteration = iteration + 1
ml_sequence = current_sequence
ml_f = current_f
ml_likelihood = current_likelihood
return ml_sequence, ml_f, ml_likelihood, samples_sequence, samples_f, samples_likelihood
def _calculate_likelihood(self, sustainData, S, f):
# Computes the likelihood of a mixture of models
#
#
# OUTPUTS:
# loglike - the log-likelihood of the current model
# total_prob_subj - the total probability of the current SuStaIn model for each subject
# total_prob_stage - the total probability of each stage in the current SuStaIn model
# total_prob_cluster - the total probability of each subtype in the current SuStaIn model
# p_perm_k - the probability of each subjects data at each stage of each subtype in the current SuStaIn model
M = sustainData.getNumSamples() #data_local.shape[0]
N_S = S.shape[0]
N = sustainData.getNumStages() #self.stage_zscore.shape[1]
f = np.array(f).reshape(N_S, 1, 1)
f_val_mat = np.tile(f, (1, N + 1, M))
f_val_mat = np.transpose(f_val_mat, (2, 1, 0))
p_perm_k = np.zeros((M, N + 1, N_S))
for s in range(N_S):
p_perm_k[:, :, s] = self._calculate_likelihood_stage(sustainData, S[s]) #self.__calculate_likelihood_stage_linearzscoremodel_approx(data_local, S[s])
total_prob_cluster = np.squeeze(np.sum(p_perm_k * f_val_mat, 1))
total_prob_stage = np.sum(p_perm_k * f_val_mat, 2)
total_prob_subj = np.sum(total_prob_stage, 1)
loglike = np.sum(np.log(total_prob_subj + 1e-250))
return loglike, total_prob_subj, total_prob_stage, total_prob_cluster, p_perm_k
def _estimate_uncertainty_sustain_model(self, sustainData, seq_init, f_init):
# Estimate the uncertainty in the subtype progression patterns and
# proportion of individuals belonging to the SuStaIn model
#
#
# OUTPUTS:
# ml_sequence - the most probable ordering of the stages for each subtype found across MCMC samples
# ml_f - the most probable proportion of individuals belonging to each subtype found across MCMC samples
# ml_likelihood - the likelihood of the most probable SuStaIn model found across MCMC samples
# samples_sequence - samples of the ordering of the stages for each subtype obtained from MCMC sampling
# samples_f - samples of the proportion of individuals belonging to each subtype obtained from MCMC sampling
# samples_likeilhood - samples of the likelihood of each SuStaIn model sampled by the MCMC sampling
# Perform a few initial passes where the perturbation sizes of the MCMC uncertainty estimation are tuned
seq_sigma_opt, f_sigma_opt = self._optimise_mcmc_settings(sustainData, seq_init, f_init)
# Run the full MCMC algorithm to estimate the uncertainty
ml_sequence, \
ml_f, \
ml_likelihood, \
samples_sequence, \
samples_f, \
samples_likelihood = self._perform_mcmc(sustainData, seq_init, f_init, self.N_iterations_MCMC, seq_sigma_opt, f_sigma_opt)
return ml_sequence, ml_f, ml_likelihood, samples_sequence, samples_f, samples_likelihood
def _optimise_mcmc_settings(self, sustainData, seq_init, f_init):
# Optimise the perturbation size for the MCMC algorithm
n_iterations_MCMC_optimisation = int(1e4) # FIXME: set externally
n_passes_optimisation = 3
seq_sigma_currentpass = 1
f_sigma_currentpass = 0.01 # magic number
N_S = seq_init.shape[0]
for i in range(n_passes_optimisation):
_, _, _, samples_sequence_currentpass, samples_f_currentpass, _ = self._perform_mcmc( sustainData,
seq_init,
f_init,
n_iterations_MCMC_optimisation,
seq_sigma_currentpass,
f_sigma_currentpass)
samples_position_currentpass = np.zeros(samples_sequence_currentpass.shape)