-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
1190 lines (1018 loc) · 50 KB
/
database.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
import numpy as np
import h5py
from PIL import Image
import io
import sys
from scipy import sparse
from os import path
# Author: Seung Yun "Simon" Lee
# Date: June 5, 2019
# defines Database object that works both as a temporary storage space and permanent storage space in accordance with
# BioActvive specification
# note: refer to README_Database.md for example uses and explanation of variables
# TempDB and TempDB both serves as storage option in temporary space, and is
# automatically initialized when Database object is initiated.
# TempDB class is optimized for dense-matrix
class TempDB:
def __init__(self, dim_arr, der, dvars, avars, overwrite):
self.dim_arr = dim_arr
self.der = der
self.dvars = dvars
self.avars = avars
self.overwrite = overwrite
if overwrite:
self.tempdata = np.full(dim_arr, np.nan, dtype = object)
self.tempder = []
self.tempdvars = []
self.tempavars = []
if der != 0:
for i in range(der):
self.tempder.append(np.full(dim_arr, np.nan, dtype = object))
if dvars != 0:
for i in range(dvars):
self.tempdvars.append(np.full(dim_arr, np.nan, dtype = object))
if avars != 0:
for i in range(avars):
self.tempavars.append(np.full(dim_arr, np.nan, dtype = object))
else:
self.tempdata = np.empty(dim_arr, dtype = object)
for ind in zip(*np.where(self.tempdata == None)):
self.tempdata[ind] = []
self.tempder = []
self.tempdvars = []
if der != 0:
for i in range(der):
temp = np.empty(dim_arr, dtype = object)
for ind in zip(*np.where(temp == None)):
temp[ind] = []
self.tempder.append(temp)
if dvars != 0:
for i in range(dvars):
temp = np.empty(dim_arr, dtype = object)
for ind in zip(*np.where(temp == None)):
temp[ind] = []
self.tempdvars.append(temp)
if avars != 0:
for i in range(avars):
temp = np.empty(dim_arr, dtype = object)
for ind in zip(*np.where(temp == None)):
temp[ind] = []
self.tempavars.append(temp)
self.tempind = Index_storage(dim_arr, der, dvars, avars, overwrite)
self.resetind = Index_storage(dim_arr, der, dvars, avars, overwrite)
def write(self, tup, destination):
data = tup[0]
ind = tup[1:]
if isinstance(data, list):
data = np.asarray(data)
if self.overwrite:
if destination == "Raw Data":
self.tempdata[ind] = data
elif destination[:12] == "Derived Data":
self.tempder[int(destination[13:]) - 1][ind] = data
elif destination[:18] == "Dependent Variable":
self.tempdvars[int(destination[19:]) - 1][ind] = data
elif destination[:19] == "Associated Variable":
self.tempavars[int(destination[20:]) - 1][ind] = data
else:
if destination == "Raw Data":
self.tempdata[ind].append(data)
elif destination[:12] == "Derived Data":
self.tempder[int(destination[13:]) - 1][ind].append(data)
elif destination[:18] == "Dependent Variable":
self.tempdvars[int(destination[19:]) - 1][ind].append(data)
elif destination[:19] == "Associated Variable":
self.tempavars[int(destination[20:]) - 1][ind].append(data)
self.tempind.store_ind(ind, destination)
self.resetind.store_ind(ind, destination)
def show(self, ind, location = "Raw Data"):
if location == "Raw Data":
data = self.tempdata
elif location[:12] == "Derived Data":
data = self.tempder[int(location[13:]) - 1]
elif location[:18] == "Dependent Variable":
data = self.tempdvars[int(location[19:]) - 1]
elif location[:19] == "Associated Variable":
data = self.tempavars[int(location[20:]) - 1][ind]
for idx in ind:
data = data[idx]
return data
def exists(self, ind, location):
return self.tempind.hasdata(ind, location)
def reset(self):
self.resetind = Index_storage(self.dim_arr, self.der,
self.dvars, self.avars, self.ovewrite)
# TempDB_Sparse class is optimized for sparse-matrix
class TempDB_Sparse:
def __init__(self, dim_arr, der, dvars, avars, overwrite, continuous=False):
self.dim_arr = dim_arr
self.der = der
self.dvars = dvars
self.avars = avars
self.overwrite = overwrite
self.continuous = continuous
self.tempdata = {}
self.tempder = {}
self.tempdvars = {}
self.tempavars = {}
if der != 0:
self.tempder = []
for i in range(der):
self.tempder.append(dict())
if dvars != 0:
self.tempdvars = []
for i in range(dvars):
self.tempdvars.append(dict())
if avars != 0:
self.tempavars = []
for i in range(avars):
self.tempavars.append(dict())
if not continuous:
self.tempind = Index_storage(dim_arr, der, dvars, avars, overwrite)
self.resetind = Index_storage(dim_arr, der, dvars, avars, overwrite)
else:
self.tempind = Value_storage(dim_arr, der, dvars, avars, overwrite)
self.resetind = Index_storage(dim_arr, der, dvars, avars, overwrite)
def write(self, tup, destination):
data = tup[0]
ind = tup[1:]
if isinstance(data, list):
data = np.asarray(data)
if self.overwrite:
if destination == "Raw Data":
self.tempdata[ind] = data
elif destination[:12] == "Derived Data":
self.tempder[int(destination[13:]) - 1][ind] = data
elif destination[:18] == "Dependent Variable":
self.tempdvars[int(destination[19:]) - 1][ind] = data
elif destination[:19] == "Associated Variable":
self.tempavars[int(destination[20:]) - 1][ind] = data
else:
if destination == "Raw Data":
if ind not in self.tempdata:
self.tempdata[ind] = []
self.tempdata[ind].append(data)
elif destination[:12] == "Derived Data":
if ind not in self.tempder[int(destination[13:]) - 1]:
self.tempder[int(destination[13:]) - 1][ind] = []
self.tempder[int(destination[13:]) - 1][ind].append(data)
elif destination[:18] == "Dependent Variable":
if ind not in self.tempdvars[int(destination[19:]) - 1]:
self.tempdvars[int(destination[19:]) - 1][ind] = []
self.tempdvars[int(destination[19:]) - 1][ind].append(data)
elif destination[:19] == "Associated Variable":
if ind not in self.tempavars[int(destination[20:]) - 1]:
self.tempavars[int(destination[20:]) - 1][ind] = []
self.tempdvars[int(destination[20:]) - 1][ind].append(data)
self.tempind.store_ind(ind, destination)
self.resetind.store_ind(ind, destination)
def show(self, ind, location = "Raw Data"):
if isinstance(ind, (np.ndarray, list)):
ind = tuple(ind)
try:
if location == "Raw Data":
data = self.tempdata[ind]
elif location[:12] == "Derived Data":
data = self.tempder[int(location[13:]) - 1][ind]
elif location[:18] == "Dependent Variable":
data = self.tempdvars[int(location[19:]) - 1][ind]
elif location[:19] == "Associated Variable":
data = self.tempavars[int(location[20:]) - 1][ind]
except KeyError:
data = np.nan
return data
def exists(self, ind, location):
return self.tempind.hasdata(ind, location)
def reset(self):
self.resetind = Index_storage(self.dim_arr, self.der, self.dvars, self.avars, self.overwrite)
class Value_storage:
def __init__(self, dim_arr, der, dvars, avars, overwrite):
self.dim_arr = dim_arr
self.vars = len(dim_arr)
self.overwrite = overwrite
if overwrite:
self.storeind = [{} for i in range(1+der+dvars+avars)] #storeind will be a dict, key = ivars, val = boolean
else:
self.storeind = [{} for i in range(1+der+dvars+avars)] #storeind will be a dict, key = ivars, val = number of entries
self.locations = ["Raw Data"]
for i in range(der):
self.locations.append("Derived Data " + str(i + 1))
for i in range(dvars):
self.locations.append("Dependent Variable " + str(i + 1))
for i in range(avars):
self.locations.append("Associated Variable " + str(i + 1))
# transforms a numpy indices into integer index of listTuples
def transform_ind(self, ind):
total = 0
for i in range(self.vars):
total += ind[i] * np.prod(self.dim_arr[i+1:])
return int(total)
# transforms an integer index of listTuples into indices of numpy array
def transform_num(self, num):
base = []
for i in range(self.vars):
ind = int(num / np.prod(self.dim_arr[i+1:]))
num -= ind * np.prod(self.dim_arr[i+1:])
base.append(ind)
return tuple(base)
def transform_location(self, location):
return self.locations.index(location)
def store_ind(self, ind, location):
locationIndex = self.transform_location(location)
if self.overwrite:
self.storeind[locationIndex][ind] = True
else:
self.storeind[locationIndex][ind] = self.storeind.get(ind, 0)+1
def store_inds(self, inds, location = "Raw Data"):
for ind in inds:
self.store_ind(ind, location)
# returns number of elements that's been stored
# see if better optimization is possible
def numelems(self, location = "Raw Data"):
return sum(list(self.storeind.values()))
# returns true if a data exists in that location
def hasdata(self, ind, location):
locationIndex = self.transform_location(location)
self.transform_location(location)
if self.overwrite:
return self.storeind[locationIndex][ind]
else:
return self.storeind[locationIndex][ind] != 0
def numentries(self, ind, location):
locationIndex = self.transform_location(location)
if self.overwrite:
raise Exception("Overwrite flag is present; this function cannot be called")
return self.storeind[locationIndex][ind]
# returns tuples of all data available
def alldata(self, location):
locationIndex = self.transform_location(location)
inds = list(self.storeind[locationIndex].keys())
return inds
# returns tuples of all data available for a specific independent variable
# if you want index for example 3rd independent variable's 0s, input (2, 0)
# 0 - indexed
def ivardata(self, ivar, location):
allinds = self.alldata(location)
return [elem for elem in allinds if elem[ivar[0]] == ivar[1]]
# Index_storage class is an object that stores indices in sparse-matrix format
# it's optimized for the needs of BioActive software in that it encompasses possibilities
# of multiple datatypes from an experiment, such as Raw Data, Derived Data, and Dependent Variables.
# The rows corresponds to the numerical representation of independent variable tuple
# The columns corresponds to the variable type, which is specified in locations attribute of
# this object.
class Index_storage:
def __init__(self, dim_arr, der, dvars, avars, overwrite):
self.dim_arr = dim_arr
self.vars = len(dim_arr)
self.overwrite = overwrite
if overwrite:
self.storeind = sparse.lil_matrix((np.prod(dim_arr), 1 + der + dvars + avars), dtype = np.dtype("?"))
else:
self.storeind = sparse.lil_matrix((np.prod(dim_arr), 1 + der + dvars + avars), dtype = np.dtype(np.uint8))
self.locations = ["Raw Data"]
for i in range(der):
self.locations.append("Derived Data " + str(i + 1))
for i in range(dvars):
self.locations.append("Dependent Variable " + str(i + 1))
for i in range(avars):
self.locations.append("Associated Variable " + str(i + 1))
# transforms a numpy indices into integer index of listTuples
def transform_ind(self, ind):
total = 0
for i in range(self.vars):
total += ind[i] * np.prod(self.dim_arr[i+1:])
return int(total)
# transforms an integer index of listTuples into indices of numpy array
def transform_num(self, num):
base = []
for i in range(self.vars):
ind = int(num / np.prod(self.dim_arr[i+1:]))
num -= ind * np.prod(self.dim_arr[i+1:])
base.append(ind)
return tuple(base)
def transform_location(self, location):
return self.locations.index(location)
def store_ind(self, ind, location):
if self.overwrite:
self.storeind[self.transform_ind(ind), self.transform_location(location)] = True
else:
self.storeind[self.transform_ind(ind), self.transform_location(location)] += 1
def store_inds(self, inds, location = "Raw Data"):
for ind in inds:
self.store_ind(ind, location)
# returns number of elements that's been stored
def numelems(self, location = "Raw Data"):
return self.storeind[..., self.transform_location(location)].count_nonzero()
# returns true if a data exists in that location
def hasdata(self, ind, location):
if self.overwrite:
return self.storeind[self.transform_ind(ind), self.transform_location(location)]
else:
return self.storeind[self.transform_ind(ind), self.transform_location(location)] != 0
def numentries(self, ind, location):
if self.overwrite:
raise Exception("Overwrite flag is present; this function cannot be called")
return self.storeind[self.transform_ind(ind), self.transform_location(location)]
# returns tuples of all data available
def alldata(self, location):
inds = self.storeind[..., self.transform_location(location)].nonzero()
return [self.transform_num(ind) for ind in inds[0]]
# returns tuples of all data available for a specific independent variable
# if you want index for example 3rd independent variable's 0s, input (2, 0)
# 0 - indexed
def ivardata(self, ivar, location):
allinds = self.alldata(location)
return [elem for elem in allinds if elem[ivar[0]] == ivar[1]]
# Database object acts as a mediator between the temporary storage space and permanent
# HDF5 strorage, and is optimized for the BioActive software. The temporary storage component
# is handled through self.temp variable which is an temporary storage object, and permanent
# storage is handled through self.f which is a pointer to the HDF5 file on the disc.
# Database object has multiple functions that links between the temporary space and the
# permanent space, along with other functions to relate with CampaignObject which is explained
# in detail in README_Database.md
# The user can essentially treat the Database object as a black-box, but more explanations on how
# the data is passed around is detailed in the comments within this script.
class Database:
# 0. Initialization
def __init__(self, dim_arr: object, filename: object, mode: object = 0, der: object = 0,
dvars: object = 0, avars: object = 0, reset: object = False, sparse: object = True,
ask: object = False, overwrite: object = True, continuous: object = False, proactive: object = False) -> object:
# HDF5 attributes (filename, flag, f)
if ask:
name = input("Type the name of the database: ")
filename = name
self.filename = filename + ".hdf5"
# add a flag if the file already exists; pick up from where we left off
if path.exists(self.filename):
self.flag = True
else:
self.flag = False
self.f = h5py.File(self.filename,'a')
# basic attributes (dim_arr, vars, mode, der, dvars, avars, locations, current, overwrite, maxentry)
self.dim_arr = dim_arr # range of ivars
self.vars = len(dim_arr) # number of ivars
self.mode = mode
self.der = der
self.dvars = dvars
self.avars = avars
self.locations = ["Raw Data"]
self.proactive = proactive
for i in range(der):
self.locations.append("Derived Data " + str(i + 1))
for i in range(dvars):
self.locations.append("Dependent Variable " + str(i + 1))
for i in range(avars):
self.locations.append("Associated Variable " + str(i + 1))
if "History" not in self.f:
self.current = 0
else:
self.current = self.getcurrent()
self.dtypes = {}
self.overwrite = overwrite
if not overwrite:
self.maxentry = {}
for location in self.locations:
self.maxentry[location] = 0
# delete all existing data if reset tag is true
if reset:
keys = self.f.keys()
for link in keys:
del self.f[link]
if overwrite:
tup = tuple(dim_arr)
if dvars != 0 and not "Dependent Variable" in self.f:
self.f.create_group("Dependent Variable")
if der != 0 and not "Derived Data" in self.f:
self.f.create_group("Derived Data")
if avars != 0 and not "Associated Variable" in self.f:
self.f.create_group("Associated Variable")
# temporary database attributes (temp)
if sparse:
self.temp = TempDB_Sparse(dim_arr, der = der, dvars = dvars, avars = avars,
overwrite = overwrite, continuous = continuous)
else:
self.temp = TempDB(dim_arr, der = der, dvars = dvars,
avars = avars, overwrite = overwrite)
Database.storeind = property(lambda self: self.temp.tempind.alldata("Raw Data"))
# memorizes indices where the actual data is stored
if "Indices" not in self.f:
if overwrite:
self.f.create_dataset("Indices", tuple(dim_arr) + (1 + der + dvars + avars,), dtype = np.bool)
else:
self.f.create_dataset("Indices", tuple(dim_arr) + (1 + der + dvars + avars,), dtype = np.uint8)
self.f.close()
# 1. Helper Functions
# transforms a numpy indices into integer index of listTuples
def transform_ind(self, ind):
total = 0
for i in range(self.vars):
total += ind[i] * np.prod(self.dim_arr[i+1:])
return int(total)
# transforms an integer index of listTuples into indices of numpy array
def transform_num(self, num):
base = []
for i in range(self.vars):
ind = int(num / np.prod(self.dim_arr[i+1:]))
num -= ind * np.prod(self.dim_arr[i+1:])
base.append(ind)
return tuple(base)
# 2. Linkage between TempDB and HDF5
# helper function that signals when the flush function should be called
# called when the user calls store
# idea is that when storing data into the temporary space, we would not want
# to use too much temporary space, so when this flush condition is satisfied,
# Database will automatically flush data into the permanent space.
def flushcondition(self):
# the commented code below returns true when temporary database has
# size above 10MB
###################### return get_size(self.temp) > 10
return False
# writes all data in temporary database into HDF5 permanent storage
# data is still present in the temporary space until the program terminates
def flush(self):
for destination in self.locations:
# storage to HDF5
if self.overwrite:
self.flush_overwrite(destination)
else:
self.flush_nonoverwrite(destination)
self.temp.reset()
# helper function that flushes all data when overwrite tag is true
def flush_overwrite(self, destination):
f = h5py.File(self.filename, "r+")
ind = self.locations.index(destination)
# variable for tempDB
location = destination
# pathway formatting
if destination[:12] == "Derived Data":
destination = "/Derived Data/" + destination[13:]
elif destination[:18] == "Dependent Variable":
destination = "/Dependent Variable/" + destination[19:]
elif destination[:19] == "Associated Variable":
destination = "/Associated Variable/" + destination[20:]
# hardcoded due to limitation of indexing in h5py (v. 2.9.0)
if self.vars == 2:
for i1, i2 in self.temp.resetind.alldata(location):
# possible bug fix--only temporary or only should be temporary
try:
f[destination][i1, i2] = self.temp.show([i1, i2], location)
except:
f[destination][i1, i2] = [self.temp.show([i1, i2], location)]
f["Indices"][i1, i2, ind] = True
elif self.vars == 3:
for i1, i2, i3 in self.temp.resetind.alldata(location):
f[destination][i1, i2, i3] = self.temp.show([i1, i2, i3], location)
f["Indices"][i1, i2, i3, ind] = True
elif self.vars == 4:
for i1, i2, i3, i4 in self.temp.resetind.alldata(location):
f[destination][i1, i2, i3, i4] = \
self.temp.show([i1, i2, i3, i4], location)
f["Indices"][i1, i2, i3, i4, ind] = True
elif self.vars == 5:
for a, b, c, d, e in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e] = \
self.temp.show([a, b, c, d, e], location)
f["Indices"][a, b, c, d, e, ind] = True
elif self.vars == 6:
for a, b, c, d, e, f0 in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e, f0] = \
self.temp.show([a, b, c, d, e, f0], location)
f["Indices"][a, b, c, d, e, f0, ind] = True
elif self.vars == 7:
for a, b, c, d, e, f0, g in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e, f0, g] = \
self.temp.show([a, b, c, d, e, f0, g], location)
f["Indices"][a, b, c, d, e, f0, g, ind] = True
elif self.vars == 8:
for a, b, c, d, e, f0, g, h in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e, f0, g, h] = \
self.temp.show([a, b, c, d, e, f0, g, h], location)
f["Indices"][a, b, c, d, e, f0, g, h, ind] = True
elif self.vars == 9:
for a, b, c, d, e, f0, g, h, i in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e, f0, g, h, i] = \
self.temp.show([a, b, c, d, e, f0, g, h, i], location)
f["Indices"][a, b, c, d, e, f0, g, h, i, ind] = True
elif self.vars == 10:
for a, b, c, d, e, f0, g, h, i, j in self.temp.resetind.alldata(location):
f[destination][a, b, c, d, e, f0, g, h, i, j] = \
self.temp.show([a, b, c, d, e, f0, g, h, i, j], location)
f["Indices"][a, b, c, d, e, f0, g, h, i, j, ind] = True
f.close()
# helper function that flushes all data when overwrite tag is false
def flush_nonoverwrite(self, destination):
f = h5py.File(self.filename, "r+")
ind = self.locations.index(destination)
# variable for tempDB
location = destination
# pathway formatting
if destination[:12] == "Derived Data":
destination = "/Derived Data/" + destination[13:]
elif destination[:18] == "Dependent Variable":
destination = "/Dependent Variable/" + destination[19:]
if self.vars == 2:
for i1, i2 in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((i1, i2), location) - self.temp.resetind.numentries((i1, i2),
location)
data = self.temp.show([i1, i2], location)[offset:]
for datum in data:
# possible bug fix--only temporary or only should be temporary
try:
f["/Entry number " + str(offset + 1) + "/" + destination][i1, i2] = datum
except:
f["/Entry number " + str(offset + 1) + "/" + destination][i1, i2] = [datum]
offset += 1
f["Indices"][i1, i2, ind] += 1
elif self.vars == 3:
for i1, i2, i3 in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((i1, i2, i3), location) - \
self.temp.resetind.numentries((i1, i2, i3), location)
data = self.temp.show([i1, i2, i3], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][i1, i2, i3] = datum
offset += 1
f["Indices"][i1, i2, i3, ind] += 1
elif self.vars == 4:
for i1, i2, i3, i4 in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((i1, i2, i3, i4), location) -\
self.temp.resetind.numentries((i1, i2, i3, i4), location)
data = self.temp.show([i1, i2, i3, i4], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][i1, i2, i3, i4] = datum
offset += 1
f["Indices"][i1, i2, i3, i4, ind] += 1
elif self.vars == 5:
for a, b, c, d, e in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e), location) - \
self.temp.resetind.numentries((a, b, c, d, e), location)
data = self.temp.show([a, b, c, d, e], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e] = datum
offset += 1
f["Indices"][a, b, c, d, e, ind] += 1
elif self.vars == 6:
for a, b, c, d, e, f0 in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e, f0), location) - \
self.temp.resetind.numentries((a, b, c, d, e, f0), location)
data = self.temp.show([a, b, c, d, e, f0], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e, f0] = datum
offset += 1
f["Indices"][a, b, c, d, e, f0, ind] += 1
elif self.vars == 7:
for a, b, c, d, e, f0, g in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e, f0, g), location) - \
self.temp.resetind.numentries((a, b, c, d, e, f0, g), location)
data = self.temp.show([a, b, c, d, e, f0, g], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e, f0, g] = datum
offset += 1
f["Indices"][a, b, c, d, e, f0, g, ind] += 1
elif self.vars == 8:
for a, b, c, d, e, f0, g, h in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e, f0, g, h), location) - \
self.temp.resetind.numentries((a, b, c, d, e, f0, g, h), location)
data = self.temp.show([a, b, c, d, e, f0, g, h], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e, f0, g, h] = datum
offset += 1
f["Indices"][a, b, c, d, e, f0, g, h, ind] += 1
elif self.vars == 9:
for a, b, c, d, e, f0, g, h, i in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e, f0, g, h, i), location) - \
self.temp.resetind.numentries((a, b, c, d, e, f0, g, h, i), location)
data = self.temp.show([a, b, c, d, e, f0, g, h, i], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e, f0, g, h, i] = datum
offset += 1
f["Indices"][a, b, c, d, e, f0, g, h, i, ind] += 1
elif self.vars == 10:
for a, b, c, d, e, f0, g, h, i, j in self.temp.resetind.alldata(location):
offset = self.temp.tempind.numentries((a, b, c, d, e, f0, g, h, i, j), location) - \
self.temp.resetind.numentries((a, b, c, d, e, f0, g, h, i, j), location)
data = self.temp.show([a, b, c, d, e, f0, g, h, i, j], location)[offset:]
for datum in data:
f["/Entry number " + str(offset + 1) + "/" + destination][a, b, c, d, e, f0, g, h, i, j] = datum
offset += 1
f["Indices"][a, b, c, d, e, f0, g, h, i, j, ind] += 1
f.close()
# 3. Main functionalities (storage and retrieval)
# store function
# REQUIRES: array of tuples (data, i1, ..., in) in which i represent the
# indices of independent variables for n-dimensional experimental space
# if images were to be passed in, the data part will be string of image file
# ENSURES: data is stored in temporary database. Nothing's returned
# Data is stored first inside the temporary space, and is only moved to the
# disc when flush is called.
def store(self, arr, destination = "Raw Data"):
f = h5py.File(self.filename,'r+')
# automatic flushing mechanism
if self.flushcondition():
self.flush()
# dataset is not initialized with the database
# instead, the store function will detect the datatype of the first data
# being passed in, and initialize the dataset according to that type.
# It is IMPERATIVE to keep the datatype the same within the same "destination"
if destination not in self.dtypes.keys():
self.dtypes[destination] = dtype_extraction(arr[0][0])
if self.requirenew(arr[0], destination):
self.makenewdset(destination)
for elem in arr:
if self.mode == 1 and destination == "Raw Data":
self.image_store(elem, destination)
else:
self.local_store(elem, destination)
f.close()
# helper function that stores data in temporary storage object
def local_store(self, tup, destination):
self.temp.write(tup, destination)
# helper function that stores images as binary of the image file
def image_store(self, tup, destination):
data = open(tup[0], "rb").read()
data = np.fromstring(data, dtype = "uint8")
self.temp.write((data,) + tup[1:], destination)
# helper function that checks if new dataset should be initialized
def requirenew(self, elem, destination):
f = h5py.File(self.filename,'r+')
if self.overwrite:
if destination == "Raw Data":
res = destination not in f
elif destination[:12] == "Derived Data":
res = destination[13:] not in f["Derived Data"]
elif destination[:18] == "Dependent Variable":
res = destination[19:] not in f["Dependent Variable"]
elif destination[:19] == "Associated Variable":
res = destination[20:] not in f["Associated Variable"]
else:
res = self.temp.tempind.numentries(elem[1:], destination) == self.maxentry[destination]
f.close()
return res
# helper function that creates appropriate dataset if it doesn't exist already
def makenewdset(self, destination):
f = h5py.File(self.filename,'r+')
if self.overwrite:
if destination == "Raw Data":
if self.mode == 0:
dt = self.dtypes[destination]
elif self.mode == 1:
dt = h5py.special_dtype(vlen = np.dtype("uint8"))
f.create_dataset("Raw Data", tuple(self.dim_arr),\
dtype = dt)
elif destination[:12] == "Derived Data":
f["Derived Data"].create_dataset(destination[13:],\
tuple(self.dim_arr), dtype = self.dtypes[destination])
elif destination[:18] == "Dependent Variable":
f["Dependent Variable"].create_dataset(destination[19:],\
tuple(self.dim_arr), dtype = self.dtypes[destination])
else:
self.maxentry[destination] += 1
entrynum = self.maxentry[destination]
name = "Entry number " + str(entrynum)
if name not in f:
f.create_group(name)
if self.der != 0:
f[name].create_group("Derived Data")
if self.dvars != 0:
f[name].create_group("Dependent Variable")
if self.avars != 0:
f[name].create_group("Associated Variable")
if destination == "Raw Data":
f[name].create_dataset("Raw Data", tuple(self.dim_arr),\
dtype = self.dtypes[destination])
elif destination[:12] == "Derived Data":
f["/" + name + "/Derived Data"].create_dataset(destination[13:],\
tuple(self.dim_arr), dtype = self.dtypes[destination])
elif destination[:18] == "Dependent Variable":
f["/" + name + "/Dependent Variable"].create_dataset(destination[19:],\
tuple(self.dim_arr), dtype = self.dtypes[destination])
f.close()
# retrieve function
# input takes different meaning depending on the flag. Refer to README_Database.md
# data is read from the temporary space if archived == False, and from the HDF5
# if archived == True, assuming that the data has been flushed previously.
# flag = "one" just return one element
# flag = "all" returns every data we have
# flag = "arr" returns all elements for the indices within the array
# flag = "ivar" returns all elements for a specific element of a specific independent variable
# flag = "entry" returns all data in related entry as a tuple
def retrieve(self, getfrom, location = "Raw Data", archived = False, flag = "one", entry = -1):
if flag == "one":
res = self.retrieve_one(getfrom, location, archived)
elif flag == "arr":
result = []
for ind in getfrom:
result.append((self.retrieve(ind, location,
archived, flag = "one", entry = entry),) + ind)
res = result
elif flag == "all":
res = self.retrieve(self.temp.tempind.alldata(location),
location, archived, flag = "arr", entry = entry)
elif flag == "ivar":
res = self.retrieve(self.temp.tempind.ivardata(getfrom, location),
location, archived, flag = "arr", entry = entry)
elif flag == "entry":
res = (self.retrieve(getfrom, location = "Raw Data", archived = archived,
flag = "one", entry = entry),)
for location in self.locations[1:]:
res += (self.retrieve(getfrom, location = location,
archived = archived, flag = "one", entry = entry),)
else:
raise Exception("Wrong flag ... the options are one, arr, all, ivar, and entry")
if entry != -1 and self.overwrite:
raise Exception("Cannot manipulate entry variable in overwrite mode")
elif entry != -1 and flag == "one":
try:
res = res[entry - 1]
except IndexError:
print("Requested entry does not exist for that experiment. The highest entry for that experiment is %s" % \
self.temp.tempind.numentries(getfrom, location))
res = np.nan
if self.proactive and flag == "one" and location == "Raw Data":
# Calculates the confidence of a given label based on its experimental history. Assumes that an experiment
# with confidence c has a c% chance of being correct and (1-c)% chance of being a random incorrect label
# TODO Currently hard coding dcarCategories for UberSL. Need a way to get from campaignObject
dVarCategories = [0, 1]
# retrieves the history of experiment confidences
confidenceData = self.retrieve(getfrom, location="Derived Data 2", flag='one')
# retrieves the results of the experiments
experimentResults = self.retrieve_one(getfrom, location, archived)
# Set up a likelihood entry for every possible result
likelihoods = [0] * len(dVarCategories)
for ind in range(len(likelihoods)):
likelihood = 1
for expInd in range(len(experimentResults)):
experimentRes = experimentResults[expInd]
# If experimental result matches the outcome whose likelihood we are calculating,
# multiply by confidence
if experimentRes == dVarCategories[ind]:
likelihood *= confidenceData[expInd]
else:
# Otherwise multiply by probability of randomly selecting result from the remaining choices
likelihood *= (1 - confidenceData[expInd]) / (len(dVarCategories) - 1)
likelihoods[ind] = likelihood
# return the result of the most likely label, as well as the likelihood
labelConfidence = np.max(likelihoods) / np.sum(likelihoods)
res = (dVarCategories[np.argmax(likelihoods)], labelConfidence)
return res
# returns the data at the given index
# will raise error if the requested data doesn't exist
def retrieve_one(self, ind, location, archived):
if not self.temp.exists(ind, location):
raise DataDoesNotExistException("Data doesn't exist inside the database")
# assumed that when raw data is image, we would like to work with the first
# dependent variable instead
if self.mode == 1 and location == "Raw Data":
location = "Dependent Variable 1"
if not archived:
data = self.temp.show(ind, location)
elif self.overwrite:
f = h5py.File(self.filename, "r")
# pathway formatting
if location[:12] == "Derived Data":
location = "/Derived Data/" + location[13:]
elif location[:18] == "Dependent Variable":
location = "/Dependent Variable/" + location[19:]
data = f[location]
for idx in ind:
data = data[idx]
f.close()
else:
f = h5py.File(self.filename, "r")
maxnum = f.get("Indices")[..., self.locations.index(location)][ind]
# pathway formatting
if location[:12] == "Derived Data":
location = "/Derived Data/" + location[13:]
elif location[:18] == "Dependent Variable":
location = "/Dependent Variable/" + location[19:]
data = []
for i in range(maxnum):
datatemp = f["/Entry number " + str(i + 1) + "/" + location]
for idx in ind:
datatemp = datatemp[idx]
data.append(datatemp)
f.close()
if self.mode == 1 and location == "Raw Data":
data = Image.open(io.BytesIO(data))
return data
# 4. Campaign Object compatibility
# the codes in this section is specific to BattleshipReal campaign and will
# need campaign object to have breakpoints functions
# The group "CampaignObject" in the root group of the HDF5 file is analogous
# to the campaignObject in BioActive; the HDF5 version only saves the historical
# attributes such as ESC, accuracy, etc. Other attributes are re-initialized
# when the campaign is run again
def campaignflush(self, campaignObject):
self.flush()
campaignObject.batch -= 1
nosavelist = ["data", "ESS", "plotting", "goalTether", "modelData",\
"activeLearner", "fetchData", "breakpoint", "xs", "ys"]
returnaslist = []
returnasdict = []
for label in dir(campaignObject):
if label not in nosavelist and label[0] != "_":
data = getattr(campaignObject, label)
self.addattr(label, data)
# memorize attributes that has to come out as list
if isinstance(data, list):
returnaslist.append(label)
if isinstance(data, dict):
returnasdict.append(label)
self.addattr("returnaslist", returnaslist)
self.addattr("returnasdict", returnasdict)
campaignObject.batch += 1
# add attributes to the group "CampaignObject" to save attributes from the
# campaignObject
def addattr(self, label, item):
f = h5py.File(self.filename, "r+")
if "CampaignObject" not in f:
f.create_group("CampaignObject")
grp = f["CampaignObject"]
if label == "model":
# dimmaj (0 and 1)
for i in range(len(item)):
for j, grouping in enumerate(item[i]):
newlb = "model" + str(i) + "_" + str(j)
grp.attrs[newlb] = grouping
elif isinstance(item, dict):
grp.attrs[label] = str(item)
else:
grp.attrs[label] = item
f.close()
# load back all the data in HDF5 into temporary database
# also load back all the attributes to campaignObject
def load(self, campaignObject):
f = h5py.File(self.filename, "r")
# loading indices into temporary space
res = []
for i in range(len(self.locations)):
indmat = f.get("Indices")[..., i].flatten()
res.append(indmat)
self.temp.tempind.storeind = sparse.lil_matrix(np.transpose(res))
# loading data into temporary space
for location in self.locations:
for ind in self.temp.tempind.alldata(location):
data = self.retrieve(ind, location, archived = True)
tup = (data,) + ind
self.temp.write(tup, location)