-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbase.py
More file actions
1192 lines (976 loc) · 55.8 KB
/
base.py
File metadata and controls
1192 lines (976 loc) · 55.8 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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Base class for all modules
"""
import time
import json
import re
import copy
import numpy as np
import pandas as pd
import os
import nibabel as nib
from scipy import io
from joblib import Memory
import abc
from abc import abstractmethod, ABCMeta
from imblearn.over_sampling import (RandomOverSampler, SMOTE, ADASYN, BorderlineSMOTE, SMOTENC)
from imblearn.under_sampling import (RandomUnderSampler,
ClusterCentroids,
NearMiss,
InstanceHardnessThreshold,
CondensedNearestNeighbour,
EditedNearestNeighbours,
RepeatedEditedNearestNeighbours,
AllKNN,
NeighbourhoodCleaningRule,
OneSidedSelection)
from imblearn.combine import SMOTEENN, SMOTETomek
from sklearn.metrics import make_scorer, accuracy_score, auc, f1_score, mean_absolute_error
from sklearn.model_selection import GridSearchCV, RandomizedSearchCV
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.decomposition import PCA, NMF
from sklearn.feature_selection import SelectPercentile, SelectKBest, SelectFromModel, f_classif,f_regression, RFE,RFECV, VarianceThreshold, mutual_info_classif, SelectFromModel
from sklearn.svm import LinearSVC, SVC, SVR
from sklearn.linear_model import (LinearRegression, LogisticRegression, Lasso,
LassoCV, RidgeCV, Ridge,
RidgeClassifier, BayesianRidge, ElasticNetCV
)
from sklearn.gaussian_process import GaussianProcessClassifier, GaussianProcessRegressor
from sklearn.tree import DecisionTreeClassifier
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor, AdaBoostClassifier
from sklearn.model_selection import KFold, StratifiedKFold, ShuffleSplit
from sklearn.pipeline import Pipeline
class BaseMachineLearning(object):
"""Base class for all machine learning
Parameters:
----------
configuration_file: file string
configuration file containing all inputs
Attributes:
----------
method_feature_preprocessing_: list of sklearn object or None
param_feature_preprocessing_: list of sklearn object or None
method_dim_reduction_: list of sklearn object or None
param_dim_reduction_: list of sklearn object or None
method_feature_selection_: list of sklearn object or None
param_feature_selection_: list of sklearn object or None
method_unbalance_treatment_: list of sklearn object or None
param_unbalance_treatment_: list of sklearn object or None
machine_learning_type_: str
method_machine_learning_: list of sklearn object or None
param_machine_learning_: list of sklearn object or None
method_model_evaluation_: list of sklearn object or None
param_model_evaluation_: list of sklearn object or None
model_: machine learning model, e.g., sklearn gridSearch model
param_search_: parameter for search of machine learning pipeline
"""
def __init__(self, configuration_file):
self.configuration_file = configuration_file
self._random_state = 0
self._gridcv_k = 3
self._search_strategy = "grid"
self._n_jobs = 2
def get_configuration_(self):
"""Get and parse the configuration file
"""
with open(self.configuration_file, 'r', encoding='utf-8') as config:
configuration = config.read()
self.configuration = json.loads(configuration)
return self
def get_preprocessing_parameters(self):
self.method_feature_preprocessing_ = None
self.param_feature_preprocessing_ = {}
feature_preprocessing = self.configuration.get('feature_engineering', {}).get('feature_preprocessing', None)
if feature_preprocessing and (list(feature_preprocessing.keys())[0] != 'None'):
self.method_feature_preprocessing_ = [list(feature_preprocessing.keys())[0] if list(feature_preprocessing.keys())[0] != 'None' else None]
self.method_feature_preprocessing_ = [self.security_eval(self.method_feature_preprocessing_[0])]
for key in feature_preprocessing.keys():
for key_ in feature_preprocessing.get(key).keys():
if key_ != []:
for key__ in feature_preprocessing.get(key).get(key_).keys():
param = feature_preprocessing.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
param = self.security_eval(param)
self.param_feature_preprocessing_.update({"feature_preprocessing__"+key_: [param]})
# Fix the random_state for replication of results
if self.method_feature_preprocessing_ and ("random_state" in self.method_feature_preprocessing_[0].get_params().keys()):
self.param_feature_preprocessing_.update({"feature_preprocessing__"+'random_state': [self._random_state]})
self.param_feature_preprocessing_ = None if self.param_feature_preprocessing_ == {} else self.param_feature_preprocessing_
return self
def get_dimension_reduction_parameters(self):
self.method_dim_reduction_ = None
self.param_dim_reduction_ = {}
dimension_reduction = self.configuration.get('feature_engineering', {}).get('dimreduction', None)
if dimension_reduction and (list(dimension_reduction.keys())[0] != 'None'):
self.method_dim_reduction_ = [self.security_eval(list(dimension_reduction.keys())[0] if list(dimension_reduction.keys())[0] != 'None' else None)]
for key in dimension_reduction.keys():
for key_ in dimension_reduction.get(key).keys():
if key_ != []:
for key__ in dimension_reduction.get(key).get(key_).keys():
param = dimension_reduction.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
param = self.security_eval(param)
if not isinstance(param, (list, tuple)):
param = [param]
self.param_dim_reduction_.update({"dim_reduction__"+key_: param})
# Fix the random_state for replication of results
if self.method_dim_reduction_ and ("random_state" in self.method_dim_reduction_[0].get_params().keys()):
self.param_dim_reduction_.update({"dim_reduction__"+'random_state': [self._random_state]})
self.param_dim_reduction_ = None if self.param_dim_reduction_ == {} else self.param_dim_reduction_
return self
def get_feature_selection_parameters(self):
self.method_feature_selection_ = None
self.param_feature_selection_ = {}
feature_selection = self.configuration.get('feature_engineering', {}).get('feature_selection', None)
if feature_selection and (list(feature_selection.keys())[0] != 'None'):
for key in feature_selection.keys():
for key_ in feature_selection.get(key).keys():
if key_ != []:
for key__ in feature_selection.get(key).get(key_).keys():
param = feature_selection.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
param = self.security_eval(param)
if not isinstance(param, (list, tuple)):
param = [param]
self.param_feature_selection_.update({"feature_selection__"+key_:param})
# Methods
self.method_feature_selection_ = list(feature_selection.keys())[0] if list(feature_selection.keys())[0] != 'None' else None
# Update point
if "RFE" in self.method_feature_selection_:
self.method_feature_selection_ = "RFE(estimator=SVC(kernel='linear'))"
if self.method_feature_selection_ == 'SelectFromModel(LassoCV())':
self.param_feature_selection_ = None
if self.method_feature_selection_ == 'SelectFromModel(ElasticNetCV())':
self.method_feature_selection_ = 'SelectFromModel(ElasticNetCV('
for keys in list(self.param_feature_selection_.keys()):
param_ = keys.split('__')[1]
value_ = self.param_feature_selection_[keys]
self.method_feature_selection_ = self.method_feature_selection_+ f'{param_}={value_},'
self.method_feature_selection_ = self.method_feature_selection_ + '))'
self.param_feature_selection_ = None
self.method_feature_selection_ = [self.security_eval(self.method_feature_selection_)]
# Fix the random_state for replication of results
if self.method_feature_selection_ and ("random_state" in self.method_feature_selection_[0].get_params().keys()):
self.param_feature_selection_.update({"feature_selection__"+'random_state': [self._random_state]})
self.param_feature_selection_ = None if self.param_feature_selection_ == {} else self.param_feature_selection_
return self
def get_unbalance_treatment_parameters(self):
self.method_unbalance_treatment_ = None
self.param_unbalance_treatment_ = {}
unbalance_treatment = self.configuration.get('feature_engineering', {}).get('unbalance_treatment', None)
if unbalance_treatment and (list(unbalance_treatment.keys())[0] != 'None'):
self.method_unbalance_treatment_ = (self.security_eval(list(unbalance_treatment.keys())[0]) if list(unbalance_treatment.keys())[0] != 'None' else None)
for key in unbalance_treatment.keys():
for key_ in unbalance_treatment.get(key).keys():
if key_ != []:
for key__ in unbalance_treatment.get(key).get(key_).keys():
param = unbalance_treatment.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
param = self.security_eval(param)
if not isinstance(param, (list, tuple)):
param = [param]
self.param_unbalance_treatment_.update({"unbalance_treatment__"+key_:param})
# Fix the random_state for replication of results
if self.method_unbalance_treatment_ and "random_state" in self.method_unbalance_treatment_.get_params().keys():
self.method_unbalance_treatment_.set_params(**{"random_state": self._random_state})
self.param_unbalance_treatment_.update({"unbalance_treatment__"+'random_state': [self._random_state]})
self.param_unbalance_treatment_ = None if self.param_unbalance_treatment_ == {} else self.param_unbalance_treatment_
return self
def get_machine_learning_parameters(self):
self.method_machine_learning_ = None
self.param_machine_learning_ = {}
machine_learning = self.configuration.get('machine_learning', None)
self.machine_learning_type_ = list(machine_learning.keys()) if machine_learning else None
if self.machine_learning_type_ is None:
raise ValueError("There is no keys for machine_learning")
elif len(self.machine_learning_type_) > 1:
raise RuntimeError("Currently, easylearn only supports one type of machine learning")
for keys in machine_learning:
machine_learning = machine_learning.get(keys, None)
if machine_learning and (list(machine_learning.keys())[0] != 'None'):
# TODO: This place will update for supporting multiple estimators
self.method_machine_learning_ = [self.security_eval(list(machine_learning.keys())[0] if list(machine_learning.keys())[0] != 'None' else None)]
for key in machine_learning.keys():
for key_ in machine_learning.get(key).keys():
if key_ != []:
for key__ in machine_learning.get(key).get(key_).keys():
param = machine_learning.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
# for example, DecisionTreeClassifier(max_depth=1) is a parameter of AdaBoostClassifier()
# Because a [sklearn] object has a
param = self.security_eval(param)
if not isinstance(param, (list, tuple)):
param = [param]
# TODO: Design a method to set params
self.param_machine_learning_.update({"estimator__"+key_: param})
# Fix the random_state for replication of results
if self.method_machine_learning_ and "random_state" in self.method_machine_learning_[0].get_params().keys():
self.param_machine_learning_.update({"estimator__"+'random_state': [self._random_state]})
self.param_machine_learning_ = None if self.param_machine_learning_ == {} else self.param_machine_learning_
return self
def get_model_evaluation_parameters(self):
self.method_model_evaluation_ = None
self.param_model_evaluation_ = {}
self.statistical_analysis = self.configuration.get('model_evaluation', {}).get("Statistical_analysis", None)
if self.statistical_analysis:
self.configuration.get('model_evaluation', {}).pop("Statistical_analysis")
model_evaluation = self.configuration.get('model_evaluation', None)
if model_evaluation and (list(model_evaluation.keys())[0] != 'None'):
self.method_model_evaluation_ = list(model_evaluation.keys())[0] if list(model_evaluation.keys())[0] != 'None' else None
for key in model_evaluation.keys():
for key_ in model_evaluation.get(key).keys():
if key_ != []:
for key__ in model_evaluation.get(key).get(key_).keys():
param = model_evaluation.get(key).get(key_).get(key__)
param = 'None' if param == '' else param
# Parse parameters: if param is digits str or containing "(" and ")", we will eval the param
# for example, DecisionTreeClassifier(max_depth=1) is a parameter of AdaBoostClassifier()
# Because a [sklearn] object has a
if type(param) is str: # selected_dataset is list
param = self.security_eval(param)
self.param_model_evaluation_.update({key_: param})
# ------Give parameter to method------
pme = ""
ik_end = len(self.param_model_evaluation_) - 1
for ik, key_pme in enumerate(self.param_model_evaluation_):
if ik != ik_end:
pme = pme + f"{key_pme}={self.param_model_evaluation_[key_pme]}" + ", "
else:
pme = pme + f"{key_pme}={self.param_model_evaluation_[key_pme]}"
self.method_model_evaluation_ = self.method_model_evaluation_.split("(")[0] + "(" + pme + self.method_model_evaluation_.split("(")[1]
self.method_model_evaluation_ = self.security_eval(self.method_model_evaluation_)
return self
def get_statistical_analysis_parameters(self):
self.method_statistical_analysis_ = list(self.statistical_analysis.keys())[0]
# parameters
# FIX the logical and extending to...
self.param_statistical_analysis_ = None
for key1 in self.statistical_analysis.keys():
for key2 in self.statistical_analysis.get(key1, {}).keys():
for key3 in self.statistical_analysis[key1].get(key2, {}).keys():
self.param_statistical_analysis_ = self.statistical_analysis.get(key1, {}).get(key2, {}).get(key3, {})
self.param_statistical_analysis_ = self.security_eval(self.param_statistical_analysis_)
return self
def get_visualization_parameters(self):
self.configuration.get('visualization', None)
def get_all_inputs(self):
self.get_configuration_()
self.get_preprocessing_parameters()
self.get_dimension_reduction_parameters()
self.get_feature_selection_parameters()
self.get_unbalance_treatment_parameters()
self.get_machine_learning_parameters()
self.get_model_evaluation_parameters()
self.get_statistical_analysis_parameters()
self.make_sklearn_search_model_()
return self
@staticmethod
def security_eval(expression):
"""Security evaluation of python expression
FIX: 'eval' had security hole
"""
iseval = (
(
bool(re.search(r'\d', expression)) or
(expression == 'None') or
(bool(re.search(r'\(', expression)) and bool(re.search(r'\)', expression)))
)
and
(
expression != 'l1' and
expression != 'l2'
# not bool(re.search('del', expression)) and
# not bool(re.search('open', expression)) and
# not bool(re.search('move', expression)) and
# not bool(re.search('copy', expression))
)
)
if iseval:
evaluated_expression = eval(expression)
else:
evaluated_expression = expression
return evaluated_expression
def make_sklearn_search_model_(self, metric=accuracy_score):
"""Construct pipeline_
Currently, the pipeline_ only supports one specific method for corresponding method,
e.g., only supports one dimension reduction method for dimension reduction.
In the next version, the pipeline_ will support multiple methods for each corresponding method.
Parameters:
----------
metric: sklearn metric object, such as accuracy_score, auc, f1_score. Default is accuracy_score
Metric is used evaluate model using cross validation in search strategy, such as GridSearchCV.
Returns:
-------
model_
"""
self.memory = Memory(location=os.path.dirname(self.configuration_file), verbose=False)
# Construct sklearn pipeline
self.pipeline_ = Pipeline(steps=[
('feature_preprocessing','passthrough'),
('dim_reduction', 'passthrough'),
('feature_selection', 'passthrough'),
('estimator', 'passthrough'),
],
memory=self.memory
)
# Set parameters of search CV
self.param_search_ = {}
if self.method_feature_preprocessing_:
self.param_search_.update({'feature_preprocessing':self.method_feature_preprocessing_})
if self.param_feature_preprocessing_:
self.param_search_.update(self.param_feature_preprocessing_)
if self.method_dim_reduction_:
self.param_search_.update({'dim_reduction':self.method_dim_reduction_})
if self.param_dim_reduction_:
self.param_search_.update(self.param_dim_reduction_)
if self.method_feature_selection_:
self.param_search_.update({'feature_selection': self.method_feature_selection_})
if self.param_feature_selection_:
self.param_search_.update(self.param_feature_selection_)
if self.method_machine_learning_:
self.param_search_.update({'estimator': self.method_machine_learning_})
if self.param_machine_learning_:
self.param_search_.update(self.param_machine_learning_)
# If no parameters' length greater than 1, using sklearn pipeline for speed up, instead of GridSearchCV or RandomizedSearchCV.
self.is_search = self.get_is_search(self.param_search_)
if not self.is_search:
if self.method_feature_preprocessing_:
self.pipeline_.set_params(**{'feature_preprocessing':self.method_feature_preprocessing_[0]})
if self.param_feature_preprocessing_:
mapping = self.parse_search_params(self.param_feature_preprocessing_)
self.pipeline_['feature_preprocessing'].set_params(**mapping)
if self.method_dim_reduction_:
self.pipeline_.set_params(**{'dim_reduction':self.method_dim_reduction_[0]})
if self.param_dim_reduction_:
mapping = self.parse_search_params(self.param_dim_reduction_)
self.pipeline_['dim_reduction'].set_params(**mapping)
if self.method_feature_selection_:
self.pipeline_.set_params(**{'feature_selection': self.method_feature_selection_[0]})
if self.param_feature_selection_:
mapping = self.parse_search_params(self.param_feature_selection_)
self.pipeline_['feature_selection'].set_params(**mapping)
if self.method_machine_learning_:
self.pipeline_.set_params(**{'estimator': self.method_machine_learning_[0]})
if self.param_machine_learning_:
mapping = self.parse_search_params(self.param_machine_learning_)
self.pipeline_['estimator'].set_params(**mapping)
# Building model
if "Classification" in self.machine_learning_type_:
cv = StratifiedKFold(n_splits=self._gridcv_k, random_state=self._random_state, shuffle=True) # Default is StratifiedKFold
else:
cv = KFold(n_splits=self._gridcv_k, random_state=self._random_state, shuffle=True)
if self.is_search:
if self._search_strategy == 'grid':
self.model_ = GridSearchCV(
self.pipeline_, n_jobs=self._n_jobs, param_grid=self.param_search_, cv=cv,
scoring = make_scorer(metric), refit=True
)
elif self._search_strategy == 'random':
self.model_ = RandomizedSearchCV(
self.pipeline_, n_jobs=self._n_jobs, param_distributions=self.param_search_, cv=cv,
scoring = make_scorer(metric), refit=True, n_iter=self.n_iter_of_randomedsearch,
)
else:
print("Please specify which search strategy!\n")
return
else:
self.model_ = self.pipeline_
return self
@staticmethod
def get_is_search(dictionary):
""" Identify whether search params (grid search or random search) or just using pipeline
"""
is_search = False
for key in dictionary:
if dictionary[key] and len(dictionary[key]) > 1:
is_search = True
break
return is_search
@staticmethod
def parse_search_params(dictionary):
""" When just using pipeline and not search parameters
I use 'set_params' to set parameters for pipeline to save running time.
"""
mapping = {}
for key in dictionary:
mapping.update({key.split("__")[1]:dictionary[key][0]})
return mapping
def save_weight(self, weights=None, out_dir=None):
"""Save contribution weight of features for each modality
Parameters:
----------
weights: list of numpy.ndarray
Contribution weights of each fold of each modality (e.g. 5-fold cross validation)
out_dir: str
Output directory
Returns:
-------
None
"""
mean_wei = np.reshape(np.mean(weights, axis=0), [-1,])
for group in self.mask_:
loc_start = 0
for im, modality in enumerate(self.mask_[group]):
mask = self.mask_[group][modality]
mean_weight = np.zeros(mask.shape)
# Updating mask location index
n_features = mask.sum()
loc_end = loc_start + n_features
mean_weight[mask] = mean_wei[loc_start:loc_end]
# Save
if self.data_format_[group][modality] in ["nii","gz"]:
out_name_wei = os.path.join(out_dir, f"weight_{modality}.nii.gz")
if os.path.exists(out_name_wei):
time_ = time.strftime('%Y%m%d%H%M%S')
out_name_wei = os.path.join(out_dir, f"weight_{modality}_{time_}.nii.gz")
mean_weight2nii = nib.Nifti1Image(mean_weight, self.affine_[group][modality])
mean_weight2nii.to_filename(out_name_wei)
else:
out_name_wei = os.path.join(out_dir, f"weight_{modality}.csv")
if os.path.exists(out_name_wei):
time_ = time.strftime('%Y%m%d%H%M%S')
out_name_wei = os.path.join(out_dir, f"weight_{modality}_{time_}.csv")
if len(np.shape(mean_weight)) > 1:
np.savetxt(out_name_wei, mean_weight, delimiter=',')
else:
pd.Series(mean_weight).to_csv(out_name_wei, header=False)
# Updating mask location index
loc_start += n_features # n_features in this point is the value in previous iteration
break # Assuming the size of the same modality in different group are matching for each other
# TODO
class MakeModel():
"""Make a machine learning model
make_sklearn_search_model_ should be integrated into the this class
Parameters:
----------
pass
Attributes:
----------
fit:
predict:
returns:
-------
pass
"""
def __init__(self):
baseml = BaseMachineLearning()
def make_sklearn_search_model_(self, metric=accuracy_score):
"""Construct pipeline_
Currently, the pipeline_ only supports one specific method for corresponding method,
e.g., only supports one dimension reduction method for dimension reduction.
In the next version, the pipeline_ will support multiple methods for each corresponding method.
Parameters:
----------
metric: sklearn metric object, such as accuracy_score, auc, f1_score. Default is accuracy_score
Metric is used evaluate model using cross validation in search strategy, such as GridSearchCV.
Returns:
-------
model_
"""
self.memory = Memory(location=os.path.dirname(self.configuration_file), verbose=False)
# Construct sklearn pipeline
self.pipeline_ = Pipeline(steps=[
('feature_preprocessing','passthrough'),
('dim_reduction', 'passthrough'),
('feature_selection', 'passthrough'),
('estimator', 'passthrough'),
],
memory=self.memory
)
# Set parameters of search CV
self.param_search_ = {}
if baseml.method_feature_preprocessing_:
self.param_search_.update({'feature_preprocessing':self.method_feature_preprocessing_})
if baseml.param_feature_preprocessing_:
self.param_search_.update(self.param_feature_preprocessing_)
if baseml.method_dim_reduction_:
self.param_search_.update({'dim_reduction':self.method_dim_reduction_})
if baseml.param_dim_reduction_:
self.param_search_.update(self.param_dim_reduction_)
if baseml.method_feature_selection_:
self.param_search_.update({'feature_selection': self.method_feature_selection_})
if baseml.param_feature_selection_:
self.param_search_.update(self.param_feature_selection_)
if baseml.method_machine_learning_:
self.param_search_.update({'estimator': self.method_machine_learning_})
if baseml.param_machine_learning_:
self.param_search_.update(self.param_machine_learning_)
# If no parameters' length greater than 1, using sklearn pipeline for speed up, instead of GridSearchCV or RandomizedSearchCV.
self.is_search = self.get_is_search(self.param_search_)
if not self.is_search:
if self.method_feature_preprocessing_:
self.pipeline_.set_params(**{'feature_preprocessing':self.method_feature_preprocessing_[0]})
if self.param_feature_preprocessing_:
mapping = self.parse_search_params(self.param_feature_preprocessing_)
self.pipeline_['feature_preprocessing'].set_params(**mapping)
if self.method_dim_reduction_:
self.pipeline_.set_params(**{'dim_reduction':self.method_dim_reduction_[0]})
if self.param_dim_reduction_:
mapping = self.parse_search_params(self.param_dim_reduction_)
self.pipeline_['dim_reduction'].set_params(**mapping)
if self.method_feature_selection_:
self.pipeline_.set_params(**{'feature_selection': self.method_feature_selection_[0]})
if self.param_feature_selection_:
mapping = self.parse_search_params(self.param_feature_selection_)
self.pipeline_['feature_selection'].set_params(**mapping)
if self.method_machine_learning_:
self.pipeline_.set_params(**{'estimator': self.method_machine_learning_[0]})
if self.param_machine_learning_:
mapping = self.parse_search_params(self.param_machine_learning_)
self.pipeline_['estimator'].set_params(**mapping)
# Building model
if "Classification" in list(self.configuration.get("machine_learning").keys()):
cv = StratifiedKFold(n_splits=self._gridcv_k, random_state=self._random_state, shuffle=True) # Default is StratifiedKFold
else:
cv = KFold(n_splits=self._gridcv_k, random_state=self._random_state, shuffle=True) # Default is StratifiedKFold
if self.is_search:
if self._search_strategy == 'grid':
self.model_ = GridSearchCV(
self.pipeline_, n_jobs=self._n_jobs, param_grid=self.param_search_, cv=cv,
scoring = make_scorer(metric), refit=True
)
elif self._search_strategy == 'random':
self.model_ = RandomizedSearchCV(
self.pipeline_, n_jobs=self._n_jobs, param_distributions=self.param_search_, cv=cv,
scoring = make_scorer(metric), refit=True, n_iter=self.n_iter_of_randomedsearch,
)
else:
print("Please specify which search strategy!\n")
return
else:
self.model_ = self.pipeline_
return self
#%% ==========================================================================
class DataLoader():
"""Load datasets according to different data types and handle extreme values
Parameters:
----------
configuration_file: file string
configuration file containing all inputs
Attributes:
----------
targets_: ndarray of shape (n_samples, )
features_: ndarray of shape (n_samples, n_features)
mask_: dictionary, each element contains a mask of a modality of a group
data_format_: str, data format such as 'nii', 'mat'
affine_: 4 by 4 matrix, image affine
id_: subject id
Notes:
-----
1. Easylearn allows users to input multiple modalities for one group.
Then, easylearn will feed features combined multiple modalities into machine learning model.
2. If there is only one input file for one modality,
then data in this file should have a column of "__ID__" (unique idenfity),
otherwise easylearn will take the first column as "__ID__".
So that easylearn can match cases between modalities and match modalities with targets and covariates.
If this file
3. If ther are multiple input files for one modality, then the files name must contain r'.*(sub.?[0-9].*).*' for
extracting unique idenfity information to match like above.
For example one file name contains strings of "sub-008.nii".
4. Easylearn only allows users to input targets as one integer by type in the GUI
(only for classification) or a file path for one group.
If the input targets is a file, then data in the file should have a column of "__ID__" and a column of "__Targets__",
otherwise easylearn will take the first column as "__ID__", and the second as "__Targets__".
If the input targets is a integer(only for classification) ,
then easylearn will assign the integer as target for all case in the group.
5. Easylearn allows users to input covariates, such as age, gender. Now, only file can be input as the covariates.
If user given easylearn a covariates file, then data in the file should have a column of "__ID__",
otherwise easylearn will take the first column as "__ID__".
"""
def __init__(self, configuration_file):
self.configuration_file = configuration_file
# Generate type2fun dictionary
# TODO: Extended to handle other formats
self.type2fun = {
".nii": self.read_nii,
".img": self.read_nii,
".mat": self.read_mat,
".txt": self.read_csv,
".csv": self.read_csv,
".xlsx": self.read_excel,
".xls": self.read_excel,
".npy": self.read_ndarray,
}
def get_configuration_(self):
"""Get and parse the configuration file
"""
with open(self.configuration_file, 'r', encoding='utf-8') as config:
configuration = config.read()
self.configuration = json.loads(configuration)
return self
def load_data(self):
self.get_configuration_()
load_data = self.configuration.get('data_loading', None)
#%% ==========================================Check datasets=================================
# NOTE.: That check whether the feature dimensions of the same modalities in different groups are equal
# is placed in the next section.
targets = {}
self.covariates_ = {}
for i, gk in enumerate(load_data.keys()):
# Check the number of modality across all group is equal
if i == 0:
n_mod = len(load_data.get(gk).get("modalities").keys())
else:
if n_mod != len(load_data.get(gk).get("modalities").keys()):
raise ValueError("The number of modalities in each group is not equal, check your inputs")
return
n_mod = len(load_data.get(gk).get("modalities").keys())
# Get targets
targets_input = load_data.get(gk).get("targets")
targets[gk] = self.read_targets(targets_input)
# Get covariates
covariates_input = load_data.get(gk).get("covariates")
if (isinstance(covariates_input, str) and
covariates_input.strip() != "" and
(not os.path.isfile(covariates_input))): # Easylearn only supports file input for covariates
raise ValueError("Easylearn only supports file input for covariates, check your covariates for {gk}")
self.covariates_[gk] = self.base_read(covariates_input)
# Check the number of files in each modalities in the same group is equal
for j, mk in enumerate(load_data.get(gk).get("modalities").keys()):
modality = load_data.get(gk).get("modalities").get(mk)
# Filses
input_files = modality.get("file")
if j == 0:
n_file = self.get_file_len(input_files) # Initialize n_file
else:
if n_file != self.get_file_len(input_files): # Left is previous, right is current loop
raise ValueError(f"The number of files in each modalities in {gk} is not equal, check your inputs")
return
n_file = self.get_file_len(input_files) # Update n_file
# Check the number of targets in each modalities is equal to the number of files
# If the type of targets is list, and number of files are not equal to targets, then raise error
if (isinstance(targets[gk],list)) and (n_file != len(targets[gk])):
raise ValueError(f"The number of files in {mk} of {gk} is not equal to the number of targets, check your inputs")
return
# Check the number of lines of covariates in each modalities is equal to the number of files
# If covariates is not int (0), and number of files are not equal to covariates, then raise error
if (not isinstance(self.covariates_[gk],int)) and (n_file != len(self.covariates_[gk])):
raise ValueError(f"The number of files in {mk} of {gk} is not equal to its' number of covariates, check your inputs")
return
#%% ==========================================Get selected datasets =================================
shape_of_data = {}
feature_applied_mask_all = {}
feature_applied_mask_and_add_otherinfo = {}
col_drop = {}
self.mask_ = {}
self.data_format_ = {}
self.affine_ = {}
for gi, gk in enumerate(load_data.keys()):
col_drop[gk] = ["__Targets__"]
shape_of_data[gk] = {}
feature_applied_mask_and_add_otherinfo[gk] = {}
feature_applied_mask_all[gk] = {}
self.mask_[gk] = {}
self.data_format_[gk] = {}
self.affine_[gk] = {}
for jm, mk in enumerate(load_data.get(gk).get("modalities").keys()):
modality = load_data.get(gk).get("modalities").get(mk)
# Get files
# If only input one file for one modality,
# then I think the file contained multiple cases' data
input_files = modality.get("file")
n_file = self.get_file_len(input_files)
if len(input_files) == 1:
one_file_per_modality = True
else:
one_file_per_modality = False
# Get features' format and affine for each modality
# I think all files in on modality are in the same format
# So I take the first file in corresponding modality as example file
# TODO: other situations
self.data_format_[gk][mk], self.affine_[gk][mk] = self.get_data_format(input_files[0])
# Get Features
all_features = self.read_file(input_files, False)
if one_file_per_modality:
all_features_ = list(all_features)[0]
else:
all_features_ = False
# Get cases' name (unique ID) in this modality
# If one_file_per_modality = False, then each file name must contain r'.*(sub.?[0-9].*).*'
# If one_file_per_modality = True and all_features_ is DataFrame,
# then the DataFrame must have header of "__ID__" which contain the unique_identifier,
# otherwise easylearn will take the first column as "__ID__".
if isinstance(all_features_, pd.core.frame.DataFrame) and ("__ID__" not in all_features_.columns):
# raise ValueError(f"The dataset of {input_files} did not have '__ID__' column, check your dataset")
unique_identifier = all_features_.iloc[:,0] # Take the first column as __ID__
print(f"The dataset of {input_files} did not have '__ID__' column, easylearn take the first column as ID\n")
elif isinstance(all_features_, pd.core.frame.DataFrame) and ("__ID__" in all_features_.columns):
unique_identifier = pd.DataFrame(all_features_["__ID__"])
all_features_.drop("__ID__", axis=1, inplace=True)
all_features = [all_features_]
elif isinstance(all_features_, np.ndarray):
all_features_ = pd.DataFrame(all_features_)
all_features = [all_features_]
unique_identifier = pd.DataFrame(all_features_.iloc[:,0], dtype=np.str) # Take the first column as __ID__
unique_identifier.columns = ["__ID__"]
all_features = [all_features_.iloc[:,1:]]
else:
autogen = True if (isinstance(targets[gk],int)) else False
unique_identifier = self.extract_id(input_files, autogen) # Multiple files
# Apply mask to feature
mask_input = modality.get("mask")
# Do not extract triangule matrix when read mask file
self.mask_[gk][mk] = self.base_read(mask_input)
if not isinstance(self.mask_[gk][mk], int): # If have mask
# TODO: Allow uses to set threshold for mask, now code only set zero threshold.
self.mask_[gk][mk] = self.mask_[gk][mk] != 0
# Apply mask
feature_applied_mask = [fa[self.mask_[gk][mk]] for fa in all_features]
feature_applied_mask = np.array(feature_applied_mask)
else:
feature_applied_mask = [fa for fa in all_features]
feature_applied_mask, self.mask_[gk][mk] = self.get_upper_tri_mat(feature_applied_mask, one_file_per_modality)
feature_applied_mask = np.array(feature_applied_mask)
feature_applied_mask = feature_applied_mask.reshape(n_file,-1)
# Concat feature across different modalities and groups
# In addition, the second and later modality are sorted according with the first one using pd.merge method
feature_applied_mask = pd.concat([unique_identifier, pd.DataFrame(feature_applied_mask)], axis=1)
if jm == 0:
feature_applied_mask_all[gk] = feature_applied_mask
else:
feature_applied_mask_all[gk] = pd.merge(feature_applied_mask_all[gk], feature_applied_mask, left_on="__ID__", right_on="__ID__")
# Dropout __ID__ from feature_applied_mask_all
unique_identifier_ = feature_applied_mask_all[gk]["__ID__"] # Get the final unique_identifier
feature_applied_mask_all[gk].drop("__ID__", axis=1, inplace=True)
#%% =====================Match targets and covariates with unique_identifier_==============================
# NOTE. subj-name is come from the first modality due to the second and later modality are sorted
# according with the first one using pd.merge method
# Sort targets and check
if (isinstance(targets[gk],int)):
targets[gk] = [targets[gk] for ifile in range(n_file)]
targets[gk] = pd.DataFrame(targets[gk])
targets[gk]["__ID__"] = unique_identifier_
targets[gk].rename(columns={0: "__Targets__"}, inplace=True)
elif isinstance(targets[gk], pd.core.frame.DataFrame) and ("__ID__" not in targets[gk].columns):
# raise ValueError(f"The targets of {gk} did not have '__ID__' column, check your targets")
print(f"The targets of {gk} did not have '__ID__' column, easylearn take the first column as ID\n")
# Take the first column as __ID__, and the second column as __Targets__
targets[gk].columns = ["__ID__", "__Targets__"]
elif isinstance(targets[gk], np.ndarray):
targets[gk] = pd.DataFrame(targets[gk])
# Take the first column as __ID__, and the second as __Targets__
targets[gk].rename(columns={0:"__ID__", 1:"__Targets__"}, inplace=True)
targets[gk] = pd.merge(unique_identifier_, targets[gk], left_on="__ID__", right_on="__ID__", how='inner')
if targets[gk].shape[0] != n_file:
raise ValueError(f"The subjects' ID in targets is not totally matched with its' data file name in {mk} of {gk} , check your ID in targets or check your data file name")
# Sort covariates and check
if (not isinstance(self.covariates_[gk],int)): # User have given covariates
if isinstance(self.covariates_[gk], pd.core.frame.DataFrame) and ("__ID__" not in self.covariates_[gk].columns):
# raise ValueError(f"The covariates of {gk} did not have 'ID' column, check your covariates")
print(f"The covariates of {gk} did not have 'ID' column, easylearn take the first column as ID\n")
colname = list(self.covariates_[gk].columns)
colname[0] = "__ID__"
self.covariates_[gk].columns = colname
elif isinstance(self.covariates_[gk], np.ndarray):
self.covariates_[gk] = pd.DataFrame(self.covariates_[gk])
self.covariates_[gk].rename(columns={0:"__ID__"}, inplace=True) # Take the first column as __ID__
self.covariates_[gk] = pd.merge(unique_identifier_, self.covariates_[gk], left_on="__ID__", right_on="__ID__")
if self.covariates_[gk].shape[0] != n_file:
raise ValueError(f"The subjects' ID in covariates is not totally matched with its' data file name in {mk} of {gk} , check your ID in covariates or check your data file name")
# Check whether the feature dimensions of the same modalities in different groups are equal
shape_of_data[gk][mk] = feature_applied_mask_all[gk].shape
if gi == 0:
gk_pre = gk
else:
if shape_of_data[gk_pre][mk][-1] != shape_of_data[gk][mk][-1]:
raise ValueError(f"Feature dimension of {mk} in {gk_pre} is {shape_of_data[gk_pre][mk][-1]} which is not equal to {mk} in {gk}: {shape_of_data[gk][mk][-1]}, check your inputs")
# Concat datasets across different group
unique_identifier_ = pd.DataFrame([f"{gk}_{ui}" for ui in unique_identifier_])
if gi == 0:
self.id_ = unique_identifier_
self.targets_ = targets[gk]["__Targets__"]
self.features_ = feature_applied_mask_all[gk]
else:
self.id_ = pd.concat([self.id_, unique_identifier_])
self.targets_ = pd.concat([self.targets_, targets[gk]["__Targets__"]])
self.features_ = pd.concat([self.features_, feature_applied_mask_all[gk]], axis=0)
self.id_ = self.id_.values
self.targets_ = np.float64(self.targets_.values)
self.features_ = np.float64(self.features_.values)
return self
#%% ========================utilt functions========================
def get_file_len(self, files):
"""If the files lenght is 1, then the length is the length of content of the files
"""
file_len = len(files)
if file_len == 1:
all_features = self.read_file(files, False)
all_features = [fe for fe in all_features][0]
file_len = len(all_features)
return file_len
def del_id(self, all_features, input_files):
"""Delete "__ID__" in each DataFrame in all_features
At last, the all_features contains only the feature
Parameters:
----------
all_features: list of DataFrames or ndarray
All features
input_files: list of
"""
all_features_ = list()
for df, file in zip(all_features, input_files):
if isinstance(df, pd.core.frame.DataFrame) and ("__ID__" not in df.columns):
raise ValueError(f"The dataset of {file} did not have '__ID__' column, check your dataset")
elif isinstance(df, pd.core.frame.DataFrame) and ("__ID__" in df.columns):
df.drop("__ID__", axis=1, inplace=True)