-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathexamplePreprocess.py
133 lines (117 loc) · 4.13 KB
/
examplePreprocess.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
# This script contains some example of preprocessing an dataset into a hdf5 file that can be read into the
# PtyLab dataset.
import numpy as np
import matplotlib.pylab as plt
import imageio
import tqdm
from skimage.transform import rescale
import glob
import os
import h5py
filePathForRead = r"D:\Du\Workshop\ptylab.m\lenspaper4\AVT camera (GX1920)"
filePathForSave = r"D:\Du\Workshop\ptylab.py\example_data"
# D:\Du\Workshop\fracmat\lenspaper4\AVT camera (GX1920)
# D:/fracmat/ptyLab/lenspaper4/AVT camera (GX1920)
os.chdir(filePathForRead)
fileName = "Lenspaper"
# wavelength
wavelength = 450e-9
# binning
binningFactor = 4
# padding for superresolution
padFactor = 1
# set magnification if any objective lens is used
magfinication = 1
# object detector distance (initial guess)
zo = 19.23e-3
# set detection geometry
# A: camera to closer side of stage (allows to bring camera close in transmission)
# B: camera to further side of stage (doesn't allow to bring close in transmission),
# or other way around in reflection
# C: objective + tube lens in transmission
measurementMode = "A"
# camera
camera = "GX"
if camera == "GX":
N = 1456
M = 1456
dxd = (
4.54e-6 * binningFactor / magfinication
) # effective detector pixel size is magnified by binning
backgroundOffset = 100 # globally subtracted from raw data (diffraction intensities), play with this value
elif camera == "Hamamatsu":
N = 2**11
M = 2**11
dxd = 6.5e-6 * binningFactor / magfinication
backgroundOffset = 0
# number of frames is calculated automatically
framesList = glob.glob("*" + ".tif")
framesList.sort()
numFrames = len(framesList) - 1
# read background
dark = imageio.imread("background.tif")
# read empty beam (if available)
# binning
ptychogram = np.zeros(
(numFrames, N // binningFactor * padFactor, N // binningFactor * padFactor),
dtype=np.float32,
)
# read frames
pbar = tqdm.trange(numFrames, leave=True)
for k in pbar:
# get file name
pbar.set_description("reading frame" + framesList[k])
temp = imageio.imread(framesList[k]).astype("float32") - dark - backgroundOffset
temp[temp < 0] = 0 # todo check if data type is single
# crop
temp = temp[
M // 2 - N // 2 : M // 2 + N // 2 - 1, M // 2 - N // 2 : M // 2 + N // 2 - 1
]
# binning
temp = rescale(
temp, 1 / binningFactor, order=0
) # order = 0 takes the nearest-neighbor
# flipping
if measurementMode == "A":
temp = np.flipud(temp)
elif measurementMode == "B":
temp = np.rot90(temp, axes=(0, 1))
elif measurementMode == "C":
temp = np.rot90(np.flipud(temp), axes=(0, 1))
# zero padding
ptychogram[k] = np.pad(temp, (padFactor - 1) * N // binningFactor // 2)
# set experimental specifications:
entrancePupilDiameter = 1000e-6
# detector coordinates
Nd = ptychogram.shape[-1] # number of detector pixels
Ld = Nd * dxd # effective size of detector
xd = np.arange(-Nd // 2, Nd // 2) * dxd # 1D coordinates in detector plane
Xd, Yd = np.meshgrid(xd, xd) # 2D coordinates in detector plane
# get positions
# get file name (this assumes there is only one text file in the raw data folder)
positionFileName = glob.glob("*" + ".txt")[0]
# take raw data positions
T = np.genfromtxt(positionFileName, delimiter=" ", skip_header=2)
# convert to micrometer
encoder = (T - T[0]) * 1e-6
# show positions
plt.figure(figsize=(5, 5))
plt.plot(encoder[:, 1] * 1e6, encoder[:, 0] * 1e6, "o-")
plt.xlabel("(um))")
plt.ylabel("(um))")
plt.show()
# export data
exportBool = True
if exportBool:
os.chdir(filePathForSave)
hf = h5py.File(fileName + ".hdf5", "w")
hf.create_dataset("ptychogram", data=ptychogram, dtype="f")
hf.create_dataset("encoder", data=encoder, dtype="f")
hf.create_dataset("binningFactor", data=(binningFactor,), dtype="i")
hf.create_dataset("dxd", data=(dxd,), dtype="f")
hf.create_dataset("Nd", data=(Nd,), dtype="i")
hf.create_dataset("zo", data=(zo,), dtype="f")
hf.create_dataset("wavelength", data=(wavelength,), dtype="f")
hf.create_dataset("entrancePupilDiameter", data=(entrancePupilDiameter,), dtype="f")
hf.close()
print("An hd5f file has been saved")