-
Notifications
You must be signed in to change notification settings - Fork 1
/
Example_IV_Helpers.py
63 lines (48 loc) · 1.97 KB
/
Example_IV_Helpers.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
import m2aia as m2
import tensorflow as tf
import numpy as np
# Running variance
def running_variance_update(existingAggregate, newValue):
(count, mean, deltaM2) = existingAggregate
count += 1
delta = newValue - mean
mean += delta / count
delta2 = newValue - mean
deltaM2 += delta * delta2
return (count, mean, deltaM2)
def running_variance_finalize(existingAggregate):
(count, mean, deltaM2) = existingAggregate
(mean, variance, sampleVariance) = (mean, deltaM2 / count, deltaM2 / (count - 1))
return (mean, variance, sampleVariance)
class BatchSequence(tf.keras.utils.Sequence):
def __init__(self, dataset: m2.Dataset.BaseDataSet , batch_size: int, shuffle: bool=True):
super().__init__()
self.gen = m2.BatchGenerator(dataset, batch_size, shuffle)
def __len__(self):
return self.gen.__len__()
def on_epoch_end(self):
self.gen.on_epoch_end()
def __getitem__(self, index):
X, Y = self.gen.__getitem__(index)
return X, Y
def image_variance(I : m2.ImzMLReader):
# initialize running variance calculation
existingAggregate = (0, np.zeros_like(I.GetXAxis()), np.zeros_like(I.GetXAxis()))
# update the variance
for i in range(I.GetNumberOfSpectra()):
_, ys = I.GetSpectrum(i)
existingAggregate = running_variance_update(existingAggregate, ys)
# finalize running variance
_, var, _ = running_variance_finalize(existingAggregate)
return var
def image_list_variance(images):
# initialize running variance calculation
existingAggregate = (0, np.zeros_like(images[0].GetXAxis()), np.zeros_like(images[0].GetXAxis()))
for I in images:
# update the variance
for i in range(I.GetNumberOfSpectra()):
_, ys = I.GetSpectrum(i)
existingAggregate = running_variance_update(existingAggregate, ys)
# finalize running variance
_, var, _ = running_variance_finalize(existingAggregate)
return var