/
reconstruct_sim_experiment_multichannel.py
178 lines (155 loc) · 6.78 KB
/
reconstruct_sim_experiment_multichannel.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
"""
Reconstruct multichannel SIM images of argoSIM slide pattern of closely spaced line pairs.
"""
import datetime
import numpy as np
from numpy import fft
import pickle
import tifffile
from pathlib import Path
import matplotlib
matplotlib.use("TkAgg")
import mcsim.analysis.sim_reconstruction as sim
from localize_psf import fit_psf, affine, rois
tstamp = datetime.datetime.now().strftime('%Y_%m_%d_%H;%M;%S')
# load data (data can be downloaded from Zenodo as described in README)
root_dir = Path("data")
fname_raw_data = root_dir / "argosim_line_pairs.tif"
use_gpu = False
# ############################################
# load image data, channel/angle/phase
# ############################################
ncolors = 2
nangles = 3
nphases = 3
nx = 2048
ny = 2048
imgs = tifffile.imread(fname_raw_data).reshape([ncolors, nangles, nphases, ny, nx])
# ############################################
# set ROI to reconstruction, [cy, cx]
# ############################################
roi = rois.get_centered_rois([791, 896], [850, 851])[0]
nx_roi = roi[3] - roi[2]
ny_roi = roi[1] - roi[0]
# ############################################
# set physical data
# ############################################
na = 1.3
pixel_size = 0.065
emission_wavelengths = [0.519, 0.580]
excitation_wavelengths = [0.465, 0.532]
# ############################################
# set OTF
# ############################################
# determined by fit to measured OTF
otf_attenuation_params = np.array([2.446])
def otf_fn(f, fmax): return 1 / (1 + (f / fmax * otf_attenuation_params[0]) ** 2) * fit_psf.circ_aperture_otf(f, 0, na, 2 * na / fmax)
# ############################################
# load affine transformations from DMD to camera
# ############################################
affine_fnames = [root_dir / "2021-02-03_09;43;06_affine_xform_blue_z=0.pkl",
root_dir / "2021-02-03_09;43;06_affine_xform_green_z=0.pkl"]
affine_xforms = []
for p in affine_fnames:
with open(p, 'rb') as f:
affine_xforms.append(pickle.load(f)['affine_xform'])
# ############################################
# load DMD pattern information
# ############################################
dmd_pattern_data_fpath = [root_dir / "sim_patterns_period=6.01_nangles=3.pkl",
root_dir / "sim_patterns_period=6.82_nangles=3.pkl"]
frqs_dmd = np.zeros((2, 3, 2))
phases_dmd = np.zeros((ncolors, nangles, nphases))
for kk in range(ncolors):
ppath = dmd_pattern_data_fpath[kk]
xform = affine_xforms[kk]
with open(ppath, 'rb') as f:
pattern_data = pickle.load(f)
# DMD intensity frequency and phase (twice electric field frq/phase)
frqs_dmd[kk] = 2 * pattern_data['frqs']
phases_dmd[kk] = 2 * pattern_data['phases']
dmd_nx = pattern_data['nx']
dmd_ny = pattern_data['ny']
# ############################################
# do SIM reconstruction for each color
# ############################################
for kk in range(ncolors):
# ###########################################
# construct otf matrix
# ###########################################
fmax = 1 / (0.5 * emission_wavelengths[kk] / na)
fx = fft.fftshift(fft.fftfreq(nx_roi, pixel_size))
fy = fft.fftshift(fft.fftfreq(ny_roi, pixel_size))
ff = np.sqrt(np.expand_dims(fx, axis=0) ** 2 +
np.expand_dims(fy, axis=1) ** 2)
otf = otf_fn(ff, fmax)
otf[ff >= fmax] = 0
# ###########################################
# guess frequencies/phases
# ###########################################
frqs_guess = np.zeros((nangles, 2))
phases_guess = np.zeros((nangles, nphases))
for ii in range(nangles):
for jj in range(nphases):
# estimate frequencies based on affine_xform
frqs_guess[ii, 0],\
frqs_guess[ii, 1],\
phases_guess[ii, jj] = \
affine.xform_sinusoid_params_roi(frqs_dmd[kk, ii, 0],
frqs_dmd[kk, ii, 1],
phases_dmd[kk, ii, jj],
[dmd_ny, dmd_nx],
roi,
xform)
# convert frequencies from 1/mirrors to 1/um
frqs_guess = frqs_guess / pixel_size
# ###########################################
# initialize SIM reconstruction
# ###########################################
save_dir = root_dir / f"{tstamp:s}_sim_reconstruction_{excitation_wavelengths[kk] * 1e3:.0f}nm"
imgset = sim.SimImageSet.initialize({"pixel_size": pixel_size, "na": na, "wavelength": emission_wavelengths[kk]},
imgs[kk, :, :, roi[0]:roi[1], roi[2]:roi[3]],
otf=otf,
wiener_parameter=0.1,
frq_estimation_mode="band-correlation",
frq_guess=frqs_guess,
phase_estimation_mode="wicker-iterative",
phases_guess=phases_guess,
combine_bands_mode="fairSIM",
fmax_exclude_band0=0.4,
normalize_histograms=False,
background=100,
gain=2,
min_p2nr=0.5,
use_gpu=use_gpu)
# ###########################################
# run reconstruction
# ###########################################
imgset.reconstruct(compute_widefield=True,
compute_os=True,
compute_deconvolved=True,
compute_mcnr=True)
# ###########################################
# print parameters
# ###########################################
imgset.print_parameters()
# ###########################################
# save reconstruction results
# ###########################################
fname_out = imgset.save_imgs(save_dir,
# format="tiff",
format="zarr",
save_raw_data=False,
save_patterns=False,
)
# ###########################################
# save diagnostic plots
# ###########################################
imgset.plot_figs(save_dir,
diagnostics_only=True,
figsize=(20, 10),
imgs_dpi=300)
# ###########################################
# show reconstruction in napari
# ###########################################
# sim.show_sim_napari(fname_out)