-
Notifications
You must be signed in to change notification settings - Fork 0
/
pydata.py
executable file
·171 lines (125 loc) · 3.97 KB
/
pydata.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
# -*- coding: utf-8 -*-
import numpy as np
import inpystem
from inpystem.tools.PCA import PcaHandler
class Data:
"""Class that defines the methods to handle data for the paper.
"""
def __init__(self, key, th, seed=0):
"""Initialization function
"""
self.key_DATA = key
# Getting PCA-thresholded noise-free data. ---------------------
#
# Get noise-free acquisition in PCA space.
nf_acq = inpystem.load_key(
key, ndim=3,
dev={'PCA_transform': True, 'PCA_th': th},
verbose=False
)
# Get noise-free acquisition after PCA thresholding in true
# space
nf_spim = nf_acq.inverse_transform(nf_acq.data)
# Adding noise. ------------------------------------------------
#
# Set noise levels.
snr = 26.75
P = np.mean(nf_spim**2)
self.sig = np.sqrt(P/10**(snr/10))
# Draw noise matrix
if seed is not None:
np.random.seed(seed)
noise_mat = np.random.randn(*nf_spim.shape)
# Construct noised images with sig1 and sig2
#
m, n, M = nf_spim.shape
# Add noise to the data.
n_spim_tmp = nf_spim + self.sig * noise_mat
# Perform PCA to noised data
handler = PcaHandler(n_spim_tmp, PCA_th=th, verbose=False)
# Get output
Y_PCA, InfoOut = handler.Y_PCA, handler.InfoOut
# Store all information.
self.std = Y_PCA.std()
self.Y = Y_PCA/Y_PCA.std()
self.H = InfoOut['H']
self.Ym = InfoOut['Ym']
self.d = InfoOut['d']
# PCA-th noise-free data in full space
self.X = nf_spim
# PCA-th noise-free data in PCA-thresholded space
self.Xy = nf_acq.data
# Save __init__ arguments
self.th = th
def inverse_transform(self, Y_PCA):
"""
"""
m, n, M = self.X.shape
Y_PCA = Y_PCA * self.std
back_data = self.H @ Y_PCA.reshape((m*n, self.th)).T
return (back_data).T.reshape((m, n, M)) + self.Ym
def draw_noise(self):
Data.__init__(self.key_DATA, self.th, None)
class R1(Data):
"""Defines the HR1 data for the paper.
"""
def __init__(self, th=9):
"""__init__ function.
"""
self.key = 'R1'
Data.__init__(self, 'HR-Spim12', th)
class R2(Data):
"""Defines the HR1 data for the paper.
"""
def __init__(self, th=7):
"""__init__ function.
"""
self.key = 'R2'
Data.__init__(self, 'HR-Spim4-2-ali', th)
class Synth(Data):
"""Defines the HR1 data for the paper.
"""
def __init__(self, th=4):
"""__init__ function.
"""
self.key = 'S'
Data.__init__(self, 'HR-Synth', th)
class Real:
"""Class that defines the methods to handle data for the paper.
"""
def __init__(self):
"""Initialization function
"""
self.key = 'Real'
# Get noise-free acquisition in PCA space.
#
th = 7
acq = inpystem.load_key(
'HR-Spim4-2-ali', ndim=3,
dev={'PCA_transform': True, 'PCA_th': th},
verbose=False
)
Y_PCA = acq.data
acq_X = inpystem.load_key(
'HR-Spim4-2-ali', ndim=3,
dev={'PCA_transform': False},
verbose=False
)
self.X = acq_X.data
self.std = Y_PCA.std()
self.Y = Y_PCA/Y_PCA.std()
self.H = acq.PCA_info['H']
self.Ym = acq.PCA_info['Ym']
self.d = acq.PCA_info['d']
self.sig = acq.PCA_info['sigma']
# Save __init__ arguments
self.th = th
def inverse_transform(self, Y_PCA):
"""
"""
m, n, M = self.X.shape
Y_PCA = Y_PCA * self.std
back_data = self.H @ Y_PCA.reshape((m*n, self.th)).T
return (back_data).T.reshape((m, n, M)) + self.Ym
def draw_noise(self):
self.__init__(self.PATH, self.th)