Skip to content

Commit 271d7df

Browse files
committed
Make some changes
1 parent 7e2d9df commit 271d7df

10 files changed

+194
-127
lines changed

src/ExperimentalData.jl

+161
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
export ExperimentalData
2+
export initializeExperimentalData
3+
4+
"""
5+
ExperimentalData{T}
6+
7+
`mutable struct` stores all parameters.
8+
9+
# Parameters
10+
11+
## Physical Properties
12+
* `wavelength` of the laser
13+
14+
## General Properties
15+
16+
* `dtype`: datatype used for reconstructions. `Float32` is usually faster, especially on GPUs.
17+
* `propagator`:
18+
19+
## Probe
20+
* `dxp`: pixel size
21+
* `Np`: number of pixels
22+
* `xp`: 1D coordinates
23+
* `Lp`: field of view probe (field size)
24+
* `zp`: distance to next plane
25+
26+
## Object
27+
* `dxo`: pixel size
28+
* `No`: number of pixels
29+
* `xo`: 1D coordinates
30+
* `Lo`: field of view probe (field size)
31+
* `zo`: distance to next plane
32+
33+
## Detector
34+
* `dxd`: pixel size
35+
* `Nd`: number of pixels
36+
* `xd`: 1D coordinates
37+
* `Ld`: field of view probe (field size)
38+
39+
40+
"""
41+
@kwdef mutable struct ExperimentalData{T}
42+
wavelength::T
43+
# probe sampling
44+
dxp::T
45+
Np::Int
46+
xp::Vector{T}
47+
Lp::T
48+
zp::Union{T, Nothing}
49+
entrancePupilDiameter::Union{T, Nothing}
50+
# object sampling
51+
dxo::T
52+
No::Int
53+
xo::Vector{T}
54+
Lo::T
55+
zo::T
56+
# detector sampling
57+
dxd::T
58+
Nd::Int
59+
xd::Vector{T}
60+
Ld::T
61+
# the general data type which is enforced
62+
dtype::Type{T}
63+
ptychogram::Union{Nothing, Array{T, N}} where N
64+
encoder::Union{Nothing, Array{T, 2}}
65+
end
66+
67+
"""
68+
ExperimentalData(fileName::String)
69+
70+
Fill the `ExperimentalData` struct with data from a `*.hdf5` file.
71+
"""
72+
function ExperimentalData(fileName::String, mode=CPM::CPM)
73+
# open h5 file
74+
fid = HDF5.h5open(fileName)
75+
76+
# function to extract number from a vector wrap
77+
r_number(x) = read(fid, x)[begin]
78+
# read arrays, take full
79+
r_array(x) = read(fid, x)
80+
# call a simple constructor and fill with data
81+
expData = initializeExperimentalData(
82+
# those numbers are stored as arrays, take first element
83+
wavelength=r_number("wavelength"),
84+
No=r_number("No"),
85+
Nd=r_number("Nd"),
86+
dxd=r_number("dxd"),
87+
zo=r_number("zo"),
88+
entrancePupilDiameter=r_number("encoder"),
89+
# those are arrays, keep them
90+
ptychogram=r_array("ptychogram"),
91+
encoder=r_array("encoder"),
92+
)
93+
94+
return expData
95+
end
96+
97+
"""
98+
initializeExperimentalData(<kwargs>)
99+
100+
Function to return parameter object `mutable struct` storing
101+
all meta information needed for reconstruction.
102+
Note that you can access all those parameters but many
103+
of them are connected hence to fill the struct we only need a few of
104+
them.
105+
"""
106+
function initializeExperimentalData(;
107+
wavelength=DEFAULT_WAVELENGTH,
108+
No=2^7,
109+
Nd=2^9,
110+
dxd=4.5e-6,
111+
zo=50e-3,
112+
dtype=Float32,
113+
zp=nothing,
114+
entrancePupilDiameter=nothing,
115+
ptychogram=nothing,
116+
encoder=nothing,
117+
)
118+
119+
# detector
120+
Ld = Nd * dxd
121+
xd = FourierTools.fftpos(Ld, Nd)
122+
# probe
123+
dxp = wavelength * zo / Ld
124+
Np = Nd
125+
Lp = dxp*Np
126+
xp = FourierTools.fftpos(Lp, Np)
127+
# object
128+
dxo = dxp
129+
Lo = dxo * No
130+
xo = FourierTools.fftpos(Lo, No)
131+
132+
# anonymous function to convert to the dtype
133+
_dtype(x) = isnothing(x) ? x : dtype(x)
134+
135+
136+
# fill struct
137+
ExperimentalData(
138+
wavelength=_dtype(wavelength),
139+
# probe sampling
140+
dxp=_dtype(dxp),
141+
Np=Np,
142+
xp=_dtype.(xp),
143+
Lp=_dtype(Lp),
144+
zp=_dtype(zp),
145+
# object sampling,
146+
dxo=_dtype(dxo),
147+
No=No,
148+
xo=_dtype.(xo),
149+
Lo=_dtype(Lo),
150+
zo=_dtype(zo),
151+
# detector sampling,
152+
dxd=_dtype(dxd),
153+
Nd=Nd,
154+
entrancePupilDiameter=dtype(entrancePupilDiameter),
155+
xd=_dtype.(xd),
156+
Ld=_dtype(Ld),
157+
# the general data type which is enforced,
158+
dtype=dtype,
159+
ptychogram=dtype.(ptychogram)),
160+
encoder=dtype.(encoder)
161+
end

src/PtyLab.jl

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,25 @@ module PtyLab
44
using FFTW, FourierTools
55
# for displaying
66
using ColorTypes
7+
# data loading
8+
using HDF5
79

10+
# @kwdef macro
11+
import Base.@kwdef
812

913
# some constants
1014
const DEFAULT_WAVELENGTH = 632.8e-9 :: Float64
1115

1216

17+
# a few types for either Fourier or Classical Ptychography
18+
abstract type ModePtychograpy end
19+
abstract type CPM <: ModePtychograpy end
20+
abstract type FPM <: ModePtychograpy end
21+
22+
1323
include("utils_parameters.jl")
24+
include("ExperimentalData.jl")
1425
include("utils_grid.jl")
1526
include("utils_display.jl")
1627

17-
1828
end

src/propagators.jl

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
"""
3+
Fraunhofer
4+
5+
"""
6+
function Fraunhofer()
7+
8+
9+
10+
end

src/utils_grid.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function grid_regular_rand(grid_size, tile_size, (N, M), rand_offset=10)
5050
i_pos = clamp(random_constrained_wiggle(i, is, rand_offset), 1, grid_size[1] - tile_size[1])
5151
j_pos = clamp(random_constrained_wiggle(j, js, rand_offset), 1, grid_size[2] - tile_size[2])
5252
# save tile
53-
push!(grr.tiles, Tile(i_pos, i_pos + tile_size[1], j_pos, j_pos + tile_size[2]))
53+
push!(grr.tiles, Tile(i_pos, i_pos + tile_size[1] - 1, j_pos, j_pos + tile_size[2] - 1))
5454
end
5555
end
5656
return grr

src/utils_parameters.jl

-119
Original file line numberDiff line numberDiff line change
@@ -1,119 +0,0 @@
1-
import Base.@kwdef
2-
3-
export init_Parameters
4-
5-
"""
6-
Parameters{T}
7-
8-
Struct storing all parameters.
9-
"""
10-
@kwdef struct Parameters{T}
11-
wavelength::T
12-
# probe sampling
13-
dxp::T
14-
Np::Int
15-
xp::Vector{T}
16-
Lp::T
17-
zp::Union{T, Nothing}
18-
# object sampling
19-
dxo::T
20-
No::Int
21-
xo::Vector{T}
22-
Lo::T
23-
zo::T
24-
# detector sampling
25-
dxd::T
26-
Nd::Int
27-
xd::Vector{T}
28-
Ld::T
29-
# the general data type which is enforced
30-
dtype::Type{T}
31-
end
32-
33-
"""
34-
init_Parameters(<kwargs>)
35-
36-
Function to return parameter object storing
37-
all meta information needed for reconstruction.
38-
Note that can access all those parameters but many
39-
of them are connected hence to fill the struct we only need a few of
40-
them.
41-
42-
# Parameters
43-
44-
## Physical Properties
45-
* `wavelength` of the laser
46-
47-
## General Properties
48-
49-
* `dtype`: datatype used for reconstructions. `Float32` is usually faster, especially on GPUs.
50-
51-
## Probe
52-
* `dxp`: pixel size
53-
* `Np`: number of pixels
54-
* `xp`: 1D coordinates
55-
* `Lp`: field of view probe (field size)
56-
* `zp`: distance to next plane
57-
58-
## Object
59-
* `dxo`: pixel size
60-
* `No`: number of pixels
61-
* `xo`: 1D coordinates
62-
* `Lo`: field of view probe (field size)
63-
* `zo`: distance to next plane
64-
65-
## Detector
66-
* `dxd`: pixel size
67-
* `Nd`: number of pixels
68-
* `xd`: 1D coordinates
69-
* `Ld`: field of view probe (field size)
70-
71-
"""
72-
function init_Parameters(;
73-
wavelength=DEFAULT_WAVELENGTH,
74-
No=2^7,
75-
Nd=2^9,
76-
dxd=4.5e-6,
77-
zo=50e-3,
78-
dtype=Float32,
79-
zp=nothing
80-
)
81-
82-
Ld = Nd * dxd
83-
# probe stuff
84-
dxp = wavelength * zo / Ld
85-
Np = Nd
86-
Lp = dxp*Np
87-
xp = FourierTools.fftpos(Lp, Np)
88-
# object stuff
89-
dxo = dxp
90-
Lo = dxo * No
91-
xo = FourierTools.fftpos(Lo, No)
92-
xd = FourierTools.fftpos(Ld, Nd)
93-
94-
_dtype(x) = isnothing(x) ? x : dtype(x)
95-
96-
Parameters(
97-
wavelength=_dtype(wavelength),
98-
# probe sampling
99-
dxp=_dtype(dxp),
100-
Np=Np,
101-
xp=_dtype.(xp),
102-
Lp=_dtype(Lp),
103-
zp=_dtype(zp),
104-
# object sampling,
105-
dxo=_dtype(dxo),
106-
No=No,
107-
xo=_dtype.(xo),
108-
Lo=_dtype(Lo),
109-
zo=_dtype(zo),
110-
# detector sampling,
111-
dxd=_dtype(dxd),
112-
Nd=Nd,
113-
xd=_dtype.(xd),
114-
Ld=_dtype(Ld),
115-
# the general data type which is enforced,
116-
dtype=dtype)
117-
end
118-
119-

test/ExperimentalData.jl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@testset "Test parameter dtype setting" begin
2+
@test typeof(initializeExperimentalData()) === PtyLab.ExperimentalData{Float32}
3+
@test typeof(initializeExperimentalData(dtype=Float64)) === PtyLab.ExperimentalData{Float64}
4+
end

test/runtests.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ using Test
33

44
# tests
55
include("utils_grid.jl")
6-
include("utils_parameters.jl")
6+
include("ExperimentalData.jl")

test/utils_grid.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
@test t.j₁ >= 1
2121
@test t.i₂ <= grid_size[1]
2222
@test t.j₂ <= grid_size[2]
23-
@test (-t.i₁ + t.i₂) == tile_size[1]
24-
@test (-t.j₁ + t.j₂) == tile_size[2]
23+
@test (-t.i₁ + t.i₂ + 1) == tile_size[1]
24+
@test (-t.j₁ + t.j₂ + 1) == tile_size[2]
2525
end
2626
end
2727

test/utils_parameters.jl

-3
This file was deleted.

test_type.jl

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
using Pkg
2+
Pkg.activate(".")
3+
using PtyLab
4+
init_Parameters()

0 commit comments

Comments
 (0)