forked from scvae/scvae
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
executable file
·3654 lines (2864 loc) · 123 KB
/
data.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
# ======================================================================== #
#
# Copyright (c) 2017 - 2018 scVAE authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# ======================================================================== #
import os
import gzip
import tarfile
import pickle
import struct
import random
import re
from bs4 import BeautifulSoup
import pandas
import tables
import json
import numpy
import scipy.sparse
import scipy.io
import sklearn.preprocessing
import stemming.porter2 as stemming
from functools import reduce
import seaborn
from time import time
from auxiliary import (
formatDuration,
normaliseString, properString, isfloat,
downloadFile, copyFile
)
preprocess_suffix = "preprocessed"
original_suffix = "original"
preprocessed_extension = ".sparse.h5"
maximum_duration_before_saving = 30 # seconds
data_sets = {
"Macosko-MRC": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"values": {
"full": "ftp://ftp.ncbi.nlm.nih.gov/geo/series/GSE63nnn/GSE63472/suppl/GSE63472_P14Retina_merged_digital_expression.txt.gz"
},
"labels": {
"full": "http://mccarrolllab.com/wp-content/uploads/2015/05/retina_clusteridentities.txt"
}
},
"loading function": lambda x: loadMouseRetinaDataSet(x),
"example type": "counts",
"class palette": {
0: (0., 0., 0.),
1: (0.92, 0.24, 0.10),
2: (0.89, 0.60, 0.14),
3: (0.78, 0.71, 0.18),
4: (0.80, 0.74, 0.16),
5: (0.79, 0.76, 0.16),
6: (0.81, 0.80, 0.18),
7: (0.77, 0.79, 0.11),
8: (0.77, 0.80, 0.16),
9: (0.73, 0.78, 0.14),
10: (0.71, 0.79, 0.15),
11: (0.68, 0.78, 0.20),
12: (0.65, 0.78, 0.15),
13: (0.63, 0.79, 0.12),
14: (0.63, 0.80, 0.17),
15: (0.61, 0.78, 0.16),
16: (0.57, 0.78, 0.14),
17: (0.55, 0.78, 0.16),
18: (0.53, 0.79, 0.14),
19: (0.52, 0.80, 0.16),
20: (0.47, 0.80, 0.17),
21: (0.44, 0.80, 0.13),
22: (0.42, 0.80, 0.16),
23: (0.42, 0.79, 0.13),
24: (0.12, 0.79, 0.72),
25: (0.13, 0.64, 0.79),
26: (0.00, 0.23, 0.88),
27: (0.00, 0.24, 0.90),
28: (0.13, 0.23, 0.89),
29: (0.22, 0.23, 0.90),
30: (0.33, 0.22, 0.87),
31: (0.42, 0.23, 0.89),
32: (0.53, 0.22, 0.87),
33: (0.59, 0.24, 0.93),
34: (0.74, 0.14, 0.67),
35: (0.71, 0.13, 0.62),
36: (0.74, 0.09, 0.55),
37: (0.74, 0.08, 0.50),
38: (0.73, 0.06, 0.44),
39: (0.74, 0.06, 0.38),
},
"label superset": {
"Horizontal": [1],
"Retinal ganglion": [2],
"Amacrine": [i for i in range(3, 24)],
"Rods": [24],
"Cones": [25],
"Bipolar": [i for i in range(26, 34)],
"Müller glia": [34],
"Others": [i for i in range(35, 40)],
"No class": [0]
},
"sorted superset class names": [
"Horizontal",
"Retinal ganglion",
"Amacrine",
"Rods",
"Cones",
"Bipolar",
"Müller glia"
],
"literature probabilities": {
"Horizontal": 0.5 / 100,
"Retinal ganglion": 0.5 / 100,
"Amacrine": 0.07,
"Rods": 79.9 / 100,
"Cones": 2.1 / 100,
"Bipolar": 7.3 / 100,
"Müller glia": 2.8 / 100,
"Others": 0
},
"excluded classes": [
0
],
"excluded superset classes": [
"No class"
],
"heat map normalisation": {
"name": "Macosko",
"label": lambda symbol:
"$\log ({} / n \\times 10^{{4}} + 1)$".format(symbol),
"function": lambda values, normalisation:
numpy.log(values / normalisation * 1e4 + 1)
},
"PCA limits": {
"full": {
"PC1": {
"minimum": -250,
"maximum": 1750
},
"PC2": {
"minimum": -700,
"maximum": 700
}
},
"training": {
"PC1": {
"minimum": -250,
"maximum": 1750
},
"PC2": {
"minimum": -700,
"maximum": 700
}
},
"validation": {
"PC1": {
"minimum": -300,
"maximum": 800
},
"PC2": {
"minimum": -300,
"maximum": 500
}
},
"test": {
"PC1": {
"minimum": -200,
"maximum": 800
},
"PC2": {
"minimum": -400,
"maximum": 400
}
}
}
},
"10x-MBC-20k": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"values": {
"full": "http://cf.10xgenomics.com/samples/cell-exp/1.3.0/1M_neurons/1M_neurons_neuron20k.h5"
},
"labels": {
"full": None
}
},
"loading function": lambda x: load10xDataSet(x),
"example type": "counts"
},
"10x-MBC": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"values": {
"full": "http://cf.10xgenomics.com/samples/cell-exp/1.3.0/1M_neurons/1M_neurons_filtered_gene_bc_matrices_h5.h5"
},
"labels": {
"full": None
}
},
"loading function": lambda x: load10xDataSet(x),
"example type": "counts"
},
"10x-PIC-L": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"all": {
"CD56+ NK cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cd56_nk/cd56_nk_filtered_gene_bc_matrices.tar.gz",
"CD19+ B cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/b_cells/b_cells_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD25+ regulatory T cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/regulatory_t/regulatory_t_filtered_gene_bc_matrices.tar.gz"
},
},
"loading function": lambda x: loadDIMMSCsCombined10xDataSet(x),
"example type": "counts"
},
"10x-PIC-T": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"all": {
"CD8+/CD45RA+ naive cytotoxic T cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/naive_cytotoxic/naive_cytotoxic_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD25+ regulatory T cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/regulatory_t/regulatory_t_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD45RA+/CD25- naive T cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/naive_t/naive_t_filtered_gene_bc_matrices.tar.gz"
},
},
"loading function": lambda x: loadDIMMSCsCombined10xDataSet(x),
"example type": "counts"
},
"10x-PIC": {
"tags": {
"example": "cell",
"feature": "gene",
"type": "count",
"item": "transcript"
},
"URLs": {
"all": {
"CD14+ Monocytes": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cd14_monocytes/cd14_monocytes_filtered_gene_bc_matrices.tar.gz",
"CD19+ B Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/b_cells/b_cells_filtered_gene_bc_matrices.tar.gz",
"CD34+ Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cd34/cd34_filtered_gene_bc_matrices.tar.gz",
"CD4+ Helper T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cd4_t_helper/cd4_t_helper_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD25+ Regulatory T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/regulatory_t/regulatory_t_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD45RA+/CD25- Naive T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/naive_t/naive_t_filtered_gene_bc_matrices.tar.gz",
"CD4+/CD45RO+ Memory T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/memory_t/memory_t_filtered_gene_bc_matrices.tar.gz",
"CD56+ Natural Killer Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cd56_nk/cd56_nk_filtered_gene_bc_matrices.tar.gz",
"CD8+ Cytotoxic T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/cytotoxic_t/cytotoxic_t_filtered_gene_bc_matrices.tar.gz",
"CD8+/CD45RA+ Naive Cytotoxic T Cells": "http://cf.10xgenomics.com/samples/cell-exp/1.1.0/naive_cytotoxic/naive_cytotoxic_filtered_gene_bc_matrices.tar.gz"
},
},
"loading function": lambda x: loadDIMMSCsCombined10xDataSet(x),
"example type": "counts"
},
"TCGA-Kallisto": {
"tags": {
"example": "sample",
"feature": "gene ID",
"type": "count",
"item": "transcript"
},
"URLs": {
"values": {
"full": "https://toil.xenahubs.net/download/tcga_Kallisto_est_counts.gz"
},
"labels": {
"full": "https://tcga.xenahubs.net/download/TCGA.PANCAN.sampleMap/PANCAN_clinicalMatrix.gz"
},
"feature mapping": {
"full": "https://toil.xenahubs.net/download/gencode.v23.annotation.transcript.probemap.gz"
}
},
"loading function": lambda x: loadTCGADataSet(x),
"example type": "counts"
},
"TCGA-RSEM": {
"tags": {
"example": "sample",
"feature": "gene ID",
"type": "count",
"item": "transcript"
},
"URLs": {
"values": {
"full": "https://toil.xenahubs.net/download/tcga_gene_expected_count.gz"
},
"labels": {
"full": "https://tcga.xenahubs.net/download/TCGA.PANCAN.sampleMap/PANCAN_clinicalMatrix.gz"
},
"feature mapping": {
"full": "https://toil.xenahubs.net/download/gencode.v23.annotation.gene.probeMap.gz"
}
},
"loading function": lambda x: loadTCGADataSet(x),
"example type": "counts"
},
"MNIST (original)": {
"tags": {
"example": "digit",
"feature": "pixel",
"type": "count",
"item": "intensity"
},
"URLs": {
"values": {
"training":
"http://yann.lecun.com/exdb/mnist/train-images-idx3-ubyte.gz",
"test":
"http://yann.lecun.com/exdb/mnist/t10k-images-idx3-ubyte.gz"
},
"labels": {
"training":
"http://yann.lecun.com/exdb/mnist/train-labels-idx1-ubyte.gz",
"test":
"http://yann.lecun.com/exdb/mnist/t10k-labels-idx1-ubyte.gz"
},
},
"loading function": lambda x: loadMNISTDataSet(x),
"maximum value": 255,
"example type": "images",
"feature dimensions": (28, 28)
},
"MNIST (normalised)": {
"tags": {
"example": "digit",
"feature": "pixel",
"type": "value",
"item": "intensity"
},
"URLs": {
"all": {
"full": "http://deeplearning.net/data/mnist/mnist.pkl.gz"
}
},
"loading function": lambda x: loadNormalisedMNISTDataSet(x),
"maximum value": 1,
"example type": "images",
"feature dimensions": (28, 28)
},
"MNIST (binarised)": {
"tags": {
"example": "digit",
"feature": "pixel",
"type": "value",
"item": "intensity"
},
"preprocessing methods": ["binarise"],
"URLs": {
"values": {
"training":
"http://www.cs.toronto.edu/~larocheh/public/datasets/binarized_mnist/binarized_mnist_train.amat",
"validation":
"http://www.cs.toronto.edu/~larocheh/public/datasets/binarized_mnist/binarized_mnist_valid.amat",
"test":
"http://www.cs.toronto.edu/~larocheh/public/datasets/binarized_mnist/binarized_mnist_test.amat"
},
"labels": {
"training": None,
"validation": None,
"test": None
},
},
"loading function": lambda x: loadBinarisedMNISTDataSet(x),
"maximum value": 1,
"example type": "images",
"feature dimensions": (28, 28)
},
"Reuters": {
"tags": {
"example": "document",
"feature": "word",
"type": "count",
"item": "word"
},
"URLs": {
"all": {
"full": "http://www.daviddlewis.com/resources/testcollections/reuters21578/reuters21578.tar.gz"
}
},
"loading function": lambda x: loadReutersDataSet(x)
},
"20 Newsgroups": {
"tags": {
"example": "document",
"feature": "word",
"type": "count",
"item": "word"
},
"URLs": {
"all": {
"full":
"http://qwone.com/~jason/20Newsgroups/20news-bydate.tar.gz"
}
},
"loading function": lambda x: load20NewsgroupsDataSet(x),
"example type": "counts"
},
"blobs": {
"URLs": {
"all": {
"full": "http://people.compute.dtu.dk/maxvo/datasets/blobs.pkl.gz"
}
},
"loading function": lambda x: loadSampleDataSet(x),
"example type": "dummy"
},
"circles": {
"URLs": {
"all": {
"full": "http://people.compute.dtu.dk/maxvo/datasets/circles.pkl.gz"
}
},
"loading function": lambda x: loadSampleDataSet(x),
"example type": "dummy"
},
"moons": {
"URLs": {
"all": {
"full": "http://people.compute.dtu.dk/maxvo/datasets/moons.pkl.gz"
}
},
"loading function": lambda x: loadSampleDataSet(x),
"example type": "dummy"
},
"sample": {
"URLs": {
"all": {
"full": "http://people.compute.dtu.dk/chegr/data-sets/count_samples.pkl.gz"
}
},
"loading function": lambda x: loadSampleDataSet(x),
"example type": "counts"
},
"sample (sparse)": {
"URLs": {
"all": {
"full": "http://people.compute.dtu.dk/chegr/data-sets/count_samples_sparse.pkl.gz"
}
},
"loading function": lambda x: loadSampleDataSet(x),
"example type": "counts"
},
"development": {
"URLs": {},
"loading function": lambda x: loadDevelopmentDataSet(
number_of_examples = 10000,
number_of_features = 5 * 5,
scale = 10,
update_probability = 0.0001
),
# "example type": "images",
"example type": "counts",
"feature dimensions": (5, 5),
"class palette": {
0: (0, 0, 0),
1: (1, 0, 0),
2: (0, 1, 0),
3: (0, 0, 1)
},
"label superset": {
"Rods": [1],
"Cones": [2, 3],
"No class": [0]
},
"sorted superset class names": [
"Rods",
"Cones"
],
"literature probabilities": {
"Rods": 0.8,
"Cones": 0.2
},
"excluded classes": [
0
],
"excluded superset classes": [
"No class"
],
"heat map normalisation": {
"name": "Macosko",
"label": lambda symbol:
"$\log ({} / n \\times 10^{{4}} + 1)$".format(symbol),
"function": lambda values, normalisation:
numpy.log(values / normalisation * 1e4 + 1)
},
"PCA limits": {
"full": {
"PC1": {
"minimum": -500,
"maximum": 2000
},
"PC2": {
"minimum": -500,
"maximum": 1000
}
},
"training": {
"PC1": {
"minimum": -500,
"maximum": 2000
},
"PC2": {
"minimum": -500,
"maximum": 1000
}
},
"validation": {
"PC1": {
"minimum": -500,
"maximum": 1500
},
"PC2": {
"minimum": -500,
"maximum": 1000
}
},
"test": {
"PC1": {
"minimum": -500,
"maximum": 1500
},
"PC2": {
"minimum": -300,
"maximum": 700
}
}
}
}
}
DIMM_SC_class_mapper = {
"CD14+ monocytes": ["CD14+ Monocytes"],
"CD19+ B cells": [
"CD19+ B Cells",
"CD19+ B cells"
],
"CD34+ cells": ["CD34+ Cells"],
"CD4+ helper T cells": ["CD4+ Helper T Cells"],
"CD4+/CD25+ regulatory T cells": [
"CD4+/CD25+ Regulatory T Cells",
"CD4+/CD25+ regulatory T cells"
],
"CD4+/CD45RA+/CD25- naïve T cells": [
"CD4+/CD45RA+/CD25- Naive T Cells",
"CD4+/CD45RA+/CD25- naive T cells"
],
"CD4+/CD45RO+ memory T cells": ["CD4+/CD45RO+ Memory T Cells"],
"CD56+ natural killer cells": [
"CD56+ Natural Killer Cells",
"CD56+ NK cells"
],
"CD8+ cytotoxic T cells": ["CD8+ Cytotoxic T Cells"],
"CD8+/CD45RA+ naïve cytotoxic T cells": [
"CD8+/CD45RA+ Naive Cytotoxic T Cells",
"CD8+/CD45RA+ naive cytotoxic T cells"
]
}
class DataSet(object):
def __init__(self, input_file_or_name,
values = None,
total_standard_deviations = None,
explained_standard_deviations = None,
preprocessed_values = None, binarised_values = None,
labels = None, class_names = None,
example_names = None, feature_names = None, map_features = False,
feature_selection = [], feature_parameter = None,
example_filter = [],
preprocessing_methods = [], preprocessed = None,
binarise_values = False,
noisy_preprocessing_methods = [],
kind = "full", version = "original",
directory = "data"):
super(DataSet, self).__init__()
# Name of data set and optional entry for data sets dictionary
self.name, data_set_dictionary = parseInput(input_file_or_name)
# Directories and paths for data set
self.directory = os.path.join(directory, self.name)
self.preprocess_directory = os.path.join(self.directory,
preprocess_suffix)
self.original_directory = os.path.join(self.directory,
original_suffix)
self.preprocessedPath = preprocessedPathFunction(
self.preprocess_directory, self.name)
# Save data set dictionary if necessary
if data_set_dictionary:
saveDataSetDictionaryAsJSONFile(data_set_dictionary,
self. directory)
# Find data set
self.title = findDataSet(self.name, directory)
# Tags (with names for examples, feature, and values) of data set
self.tags = dataSetTags(self.title)
# Example type for data set
self.example_type = dataSetExampleType(self.title)
# Maximum value of data set
self.maximum_value = dataSetMaximumValue(self.title)
# Discreteness
self.discreteness = self.example_type == "counts" \
or (self.maximum_value != None and self.maximum_value == 255)
# Feature dimensions for data set
self.feature_dimensions = dataSetFeatureDimensions(self.title)
# Literature probabilities for data set
self.literature_probabilities = dataSetLiteratureProbabilities(self.title)
# Class mapper for data set
self.class_mapper = dataSetClassMapper(self.title)
# Label super set for data set
self.label_superset = dataSetLabelSuperset(self.title)
self.superset_labels = None
self.number_of_superset_classes = None
# Label palette for data set
self.class_palette = dataSetClassPalette(self.title)
self.superset_class_palette = supersetClassPalette(
self.class_palette, self.label_superset)
# Excluded classes for data set
self.excluded_classes = dataSetExcludedClasses(self.title)
# Excluded classes for data set
self.excluded_superset_classes = dataSetExcludedSupersetClasses(
self.title)
# Values and their names as well as labels in data set
self.values = None
self.total_standard_deviations = None
self.explained_standard_deviations = None
self.count_sum = None
self.normalised_count_sum = None
self.preprocessed_values = None
self.binarised_values = None
self.labels = None
self.example_names = None
self.feature_names = None
self.class_names = None
self.number_of_examples = None
self.number_of_features = None
self.number_of_classes = None
self.update(
values = values,
total_standard_deviations = total_standard_deviations,
explained_standard_deviations = explained_standard_deviations,
preprocessed_values = preprocessed_values,
binarised_values = binarised_values,
labels = labels,
example_names = example_names,
feature_names = feature_names,
class_names = class_names
)
# Predicted labels
self.predicted_cluster_ids = None
self.predicted_labels = None
self.predicted_class_names = None
self.number_of_predicted_classes = None
self.predicted_class_palette = None
self.predicted_label_sorter = None
self.predicted_superset_labels = None
self.predicted_superset_class_names = None
self.number_of_predicted_superset_classes = None
self.predicted_superset_class_palette = None
self.predicted_superset_label_sorter = None
# Sorted class names for data set
sorted_class_names = dataSetSortedClassNames(self.title)
self.label_sorter = createLabelSorter(sorted_class_names)
sorted_superset_class_names = dataSetSortedClassNames(self.title,
superset = True)
self.superset_label_sorter = createLabelSorter(
sorted_superset_class_names)
# Feature mapping
self.map_features = map_features
self.feature_mapping = None
# Feature selection
self.feature_selection = feature_selection
self.feature_parameter = feature_parameter
# Example filterering
self.example_filter = example_filter
if example_filter:
self.example_filter = example_filter[0]
if len(example_filter) > 1:
self.example_filter_parameters = example_filter[1:]
else:
self.example_filter_parameters = None
else:
self.example_filter = None
self.example_filter_parameters = None
# Preprocessing methods
self.preprocessing_methods = preprocessing_methods
self.binarise_values = binarise_values
if preprocessed is None:
data_set_preprocessing_methods = \
dataSetPreprocessingMethods(self.title)
if data_set_preprocessing_methods:
self.preprocessed = True
else:
self.preprocessed = False
else:
self.preprocessed = preprocessed
if self.preprocessed:
self.preprocessing_methods = data_set_preprocessing_methods
# Kind of data set (full, training, validation, test)
self.kind = kind
# Split indices for training, validation, and test sets
self.split_indices = None
# Version of data set (original, reconstructed)
self.version = version
# Heat map normalisation for data set
self.heat_map_normalisation = dataSetHeatMapNormalisation(self.title)
# PCA limits for data set
self.pca_limits = dataSetPCALimits(self.title, self.kind)
# Noisy preprocessing
self.noisy_preprocessing_methods = noisy_preprocessing_methods
if self.preprocessed:
self.noisy_preprocessing_methods = []
if self.noisy_preprocessing_methods:
self.noisy_preprocess = preprocessingFunctionForDataSet(
self.title, self.noisy_preprocessing_methods,
self.preprocessedPath,
noisy = True
)
else:
self.noisy_preprocess = None
if self.kind == "full":
print("Data set:")
print(" title:", self.title)
if self.map_features:
print(" feature mapping: if available")
if self.feature_selection:
print(" feature selection:", self.feature_selection)
if self.feature_parameter:
print(" parameter:", self.feature_parameter)
else:
print(" parameter: default")
else:
print(" feature selection: none")
if self.example_filter:
print(" example filter:", self.example_filter)
if self.example_filter_parameters:
print(" parameter(s):",
", ".join(self.example_filter_parameters))
else:
print(" example filter: none")
if not self.preprocessed and self.preprocessing_methods:
print(" processing methods:")
for preprocessing_method in self.preprocessing_methods:
print(" ", preprocessing_method)
elif self.preprocessed:
print(" processing methods: already done")
else:
print(" processing methods: none")
if not self.preprocessed and self.noisy_preprocessing_methods:
print(" noisy processing methods:")
for preprocessing_method in self.noisy_preprocessing_methods:
print(" ", preprocessing_method)
print()
@property
def number_of_values(self):
return self.number_of_examples * self.number_of_features
@property
def class_probabilities(self):
if self.label_superset:
labels = self.superset_labels
class_names = self.superset_class_names
excluded_classes = self.excluded_superset_classes
else:
labels = self.labels
class_names = self.class_names
excluded_classes = self.excluded_classes
class_probabilities = {class_name: 0 for class_name in class_names}
total_count_sum = 0
for label in labels:
if label in excluded_classes:
continue
class_probabilities[label] += 1
total_count_sum += 1
class_names_with_zero_probability = []
for name, count in class_probabilities.items():
if count == 0:
class_names_with_zero_probability.append(name)
class_probabilities[name] = count / total_count_sum
for name in class_names_with_zero_probability:
class_probabilities.pop(name)
return class_probabilities
@property
def has_values(self):
return self.values is not None
@property
def has_preprocessed_values(self):
return self.preprocessed_values is not None
@property
def has_binarised_values(self):
return self.binarised_values is not None
@property
def has_labels(self):
return self.labels is not None
@property
def has_superset_labels(self):
return self.superset_labels is not None
@property
def has_predictions(self):
return self.has_predicted_labels or self.has_predicted_cluster_ids
@property
def has_predicted_labels(self):
return self.predicted_labels is not None
@property
def has_predicted_superset_labels(self):
return self.predicted_superset_labels is not None
@property
def has_predicted_cluster_ids(self):
return self.predicted_cluster_ids is not None
def update(self, values = None,
total_standard_deviations = None,
explained_standard_deviations = None,
preprocessed_values = None,
binarised_values = None, labels = None,
example_names = None, feature_names = None, class_names = None):
if values is not None:
self.values = values
self.count_sum = self.values.sum(axis = 1).reshape(-1, 1)
if isinstance(self.count_sum, numpy.matrix):
self.count_sum = self.count_sum.A
self.normalised_count_sum = self.count_sum / self.count_sum.max()
M_values, N_values = values.shape
if example_names is not None:
self.example_names = example_names
assert len(self.example_names.shape) == 1, \
"The list of example names is multi-dimensional: {}."\
.format(self.example_names.shape)
M_examples = self.example_names.shape[0]
assert M_values == M_examples, \
"The number of examples in the value matrix ({}) "\
.format(M_values) + \
"is not the same as the number of example names ({})."\
.format(M_examples)
if feature_names is not None:
self.feature_names = feature_names
assert len(self.feature_names.shape) == 1, \
"The list of feature names is multi-dimensional: {}."\
.format(self.feature_names.shape)
N_features = self.feature_names.shape[0]
assert N_values == N_features, \
"The number of features in the value matrix ({}) "\
.format(N_values) + \
"is not the same as the number of feature names ({})."\
.format(N_features)
self.number_of_examples = M_values
self.number_of_features = N_values
else:
if example_names is not None and feature_names is not None:
self.example_names = example_names
self.feature_names = feature_names
if labels is not None:
if self.class_mapper:
labels = labels.tolist()
labels = [
properString(label, self.class_mapper, normalise = False)
for label in labels
]
labels = numpy.array(labels)
self.labels = labels
if class_names is not None:
self.class_names = class_names
else:
self.class_names = numpy.unique(self.labels).tolist()
self.class_id_to_class_name = {}
self.class_name_to_class_id = {}