Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
nam
ProxPython
Commits
8bc6b016
Commit
8bc6b016
authored
May 16, 2019
by
Russell Luke
Browse files
up to self.illumination = config['abs_illumination']
parent
b1a2d979
Changes
6
Hide whitespace changes
Inline
Side-by-side
proxtoolbox/Problems/OrbitalTomog/.ipynb_checkpoints/Untitled-checkpoint.ipynb
View file @
8bc6b016
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
scipy.io
import
loadmat
%
pylab
inline
```
%%%% Output: stream
Populating the interactive namespace from numpy and matplotlib
%% Cell type:code id: tags:
```
python
inp
=
loadmat
(
'../../../InputData/OrbitalTomog/coronen_homo1_fourier_noise15.mat'
)
imshow
(
inp
[
'I2'
])
# Berechnung und Speicherung als png-Datei des Supports
# Autokorrelation
threshold_autocorr
=
0.1
;
support0
=
np
.
fft
.
fftshift
(
np
.
fft
.
ifft2
(
np
.
fft
.
ifftshift
(
inp
[
'I2'
])));
cond2
=
(
support0
<
threshold_autocorr
*
np
.
amax
(
support0
)).
astype
(
uint
);
subplot
(
121
)
imshow
(
support0
.
real
)
colorbar
()
subplot
(
122
)
imshow
(
cond2
)
colorbar
()
# Anfangsbedingungen
support
=
support0
;
F0
=
inp
[
'I2'
]
**
0.5
;
ph_init
=
2
*
np
.
pi
*
np
.
random
.
rand
(
F0
.
shape
);
# ph_init = np.angle(np.fft.fft2(ph_init));
u0
=
F0
*
np
.
exp
(
1j
.
*
ph_init
);
previous
=
np
.
fft
.
fftshift
(
np
.
fft
.
ifft2
(
np
.
fft
.
ifftshift
(
u0
)));
```
%%%% Output: execute_result
<matplotlib.colorbar.Colorbar at 0x7f0dcab00198>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
imshow
(
inp
[
"I2"
])
```
%%%% Output: execute_result
<matplotlib.image.AxesImage at 0x7f0dca741358>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
inp
=
loadmat
(
'../../../InputData/OrbitalTomog/coronen_homo1_fourier_noise15.mat'
)
%
Parametereinstellung
N_ER
=
12
;
N_HIO
=
0
;
N_RAAR
=
0
;
threshold_autocorr
=
0.1
;
beta
=
0.9
;
N_Ges
=
N_ER
+
N_HIO
+
N_RAAR
;
n_Ges
=
0
;
%
Berechnung
und
Speicherung
als
png
-
Datei
des
Supports
%
Autokorrelation
support0
=
fftshift
(
ifft2
(
ifftshift
(
I2
)));
cond2
=
support0
<
threshold_autocorr
*
max
(
support0
(:));
support0
=
ones
(
size
(
support0
));
support0
(
cond2
)
=
0
;
figure
(
1
);
imagesc
(
support0
);
daspect
([
1
1
1
]);
zoom
(
1
);
print
(
gcf
,
'-dpng'
,
'support0'
,
'-r150'
);
%%
%
Anfangsbedingungen
support
=
support0
;
F0
=
I2
.
^
0.5
;
ph_init
=
rand
(
size
(
F0
));
ph_init
=
angle
(
fft2
(
ph_init
));
G2
=
F0
.
*
exp
(
1j
.
*
ph_init
);
previous
=
fftshift
(
ifft2
(
ifftshift
(
G2
)));
%%
%
Erstellung
eines
neuen
Ordners
mit
Angaben
ueber
die
gewaehlten
Parameter
name_folder
=
sprintf
(
'ER_%.f_HIO_%.f_RAAR_%.f_beta_%.2f'
,
N_ER
,
N_HIO
,
N_RAAR
,
beta
);
mkdir
(
name_folder
)
oldFolder
=
cd
(
name_folder
);
%
Speichern
der
gewaehlten
Parameter
fid
=
fopen
(
'Parameter.txt'
,
'w'
);
fprintf
(
fid
,
'N_ER
\t
%.f
\r\n
N_HIO
\t
%.f
\r\n
N_RAAR
\t
%.f
\r\n
threshold_autocorr
\t
%.1f
\r\n
beta
\t
%.2f'
,
N_ER
,
N_HIO
,
N_RAAR
,
threshold_autocorr
,
beta
);
fclose
(
fid
);
%
Der
Fehler
wird
nach
jeder
Iteration
in
der
Datei
'error.txt'
%
abgespeichert
fid
=
fopen
(
'error.txt'
,
'w'
);
E
=
zeros
(
N_Ges
,
1
);
%%
%
HIO
-
Algorithmus
```
%% Cell type:code id: tags:
```
python
imshow
(
np
.
ones
((
16
,
16
)))
```
%%%% Output: execute_result
<matplotlib.image.AxesImage at 0x7f46afb32588>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
ff
=
{
'a'
:
1
,
'b'
:
3
}
```
%% Cell type:code id: tags:
```
python
if
'c'
not
in
ff
:
ff
[
'c'
]
=
2
ff
```
%%%% Output: execute_result
{'a': 1, 'b': 3, 'c': 2}
%% Cell type:code id: tags:
```
python
```
...
...
proxtoolbox/Problems/OrbitalTomog/Coronene_demo.py
View file @
8bc6b016
import
sys
sys
.
path
.
append
(
'..'
)
from
.
import
coronene_test
from
.phase
import
Phase
sys
.
path
.
append
(
'../../../'
)
# from .
import
coronene_test
# base config
import
orbitaltomog_data_processor
# extends config
from
phase
import
Phase
# sys.path.append('../proxtoolbox/Problems/Phase')
orbit
=
Phase
(
c
or
onene_test
.
new
_config
)
orbit_config
=
orbitaltomog_data_processor
.
data_processor
(
coronene_test
.
new_config
)
orbit
=
Phase
(
or
bit
_config
)
orbit
.
solve
()
orbit
.
show
()
\ No newline at end of file
proxtoolbox/Problems/OrbitalTomog/Untitled.ipynb
View file @
8bc6b016
%% Cell type:code id: tags:
```
python
import
numpy
as
np
from
scipy.io
import
loadmat
%
pylab
inline
```
%%%% Output: stream
Populating the interactive namespace from numpy and matplotlib
%% Cell type:code id: tags:
```
python
inp
=
loadmat
(
'../../../InputData/OrbitalTomog/coronen_homo1_fourier_noise15.mat'
)
imshow
(
inp
[
'I2'
])
# Berechnung und Speicherung als png-Datei des Supports
# Autokorrelation
threshold_autocorr
=
0.1
;
support0
=
np
.
fft
.
fftshift
(
np
.
fft
.
ifft2
(
np
.
fft
.
ifftshift
(
inp
[
'I2'
])));
cond2
=
(
support0
<
threshold_autocorr
*
np
.
amax
(
support0
)).
astype
(
uint
);
subplot
(
121
)
imshow
(
support0
.
real
)
colorbar
()
subplot
(
122
)
imshow
(
cond2
)
colorbar
()
# Anfangsbedingungen
support
=
support0
;
F0
=
inp
[
'I2'
]
**
0.5
;
ph_init
=
2
*
np
.
pi
*
np
.
random
.
rand
(
F0
.
shape
);
# ph_init = np.angle(np.fft.fft2(ph_init));
u0
=
F0
*
np
.
exp
(
1j
.
*
ph_init
);
previous
=
np
.
fft
.
fftshift
(
np
.
fft
.
ifft2
(
np
.
fft
.
ifftshift
(
u0
)));
```
%%%% Output: execute_result
<matplotlib.colorbar.Colorbar at 0x7f0dcab00198>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
imshow
(
inp
[
"I2"
])
```
%%%% Output: execute_result
<matplotlib.image.AxesImage at 0x7f0dca741358>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
inp
=
loadmat
(
'../../../InputData/OrbitalTomog/coronen_homo1_fourier_noise15.mat'
)
%
Parametereinstellung
N_ER
=
12
;
N_HIO
=
0
;
N_RAAR
=
0
;
threshold_autocorr
=
0.1
;
beta
=
0.9
;
N_Ges
=
N_ER
+
N_HIO
+
N_RAAR
;
n_Ges
=
0
;
%
Berechnung
und
Speicherung
als
png
-
Datei
des
Supports
%
Autokorrelation
support0
=
fftshift
(
ifft2
(
ifftshift
(
I2
)));
cond2
=
support0
<
threshold_autocorr
*
max
(
support0
(:));
support0
=
ones
(
size
(
support0
));
support0
(
cond2
)
=
0
;
figure
(
1
);
imagesc
(
support0
);
daspect
([
1
1
1
]);
zoom
(
1
);
print
(
gcf
,
'-dpng'
,
'support0'
,
'-r150'
);
%%
%
Anfangsbedingungen
support
=
support0
;
F0
=
I2
.
^
0.5
;
ph_init
=
rand
(
size
(
F0
));
ph_init
=
angle
(
fft2
(
ph_init
));
G2
=
F0
.
*
exp
(
1j
.
*
ph_init
);
previous
=
fftshift
(
ifft2
(
ifftshift
(
G2
)));
%%
%
Erstellung
eines
neuen
Ordners
mit
Angaben
ueber
die
gewaehlten
Parameter
name_folder
=
sprintf
(
'ER_%.f_HIO_%.f_RAAR_%.f_beta_%.2f'
,
N_ER
,
N_HIO
,
N_RAAR
,
beta
);
mkdir
(
name_folder
)
oldFolder
=
cd
(
name_folder
);
%
Speichern
der
gewaehlten
Parameter
fid
=
fopen
(
'Parameter.txt'
,
'w'
);
fprintf
(
fid
,
'N_ER
\t
%.f
\r\n
N_HIO
\t
%.f
\r\n
N_RAAR
\t
%.f
\r\n
threshold_autocorr
\t
%.1f
\r\n
beta
\t
%.2f'
,
N_ER
,
N_HIO
,
N_RAAR
,
threshold_autocorr
,
beta
);
fclose
(
fid
);
%
Der
Fehler
wird
nach
jeder
Iteration
in
der
Datei
'error.txt'
%
abgespeichert
fid
=
fopen
(
'error.txt'
,
'w'
);
E
=
zeros
(
N_Ges
,
1
);
%%
%
HIO
-
Algorithmus
```
%% Cell type:code id: tags:
```
python
imshow
(
np
.
ones
((
16
,
16
)))
```
%%%% Output: execute_result
<matplotlib.image.AxesImage at 0x7f46afb32588>
%%%% Output: display_data

%% Cell type:code id: tags:
```
python
ff
=
{
'a'
:
1
,
'b'
:
3
}
```
%% Cell type:code id: tags:
```
python
if
'c'
not
in
ff
:
ff
[
'c'
]
=
2
ff
```
%%%% Output: execute_result
{'a': 1, 'b': 3, 'c': 2}
%% Cell type:code id: tags:
```
python
```
...
...
proxtoolbox/Problems/OrbitalTomog/orbitaltomog_data_processor.py
View file @
8bc6b016
...
...
@@ -22,8 +22,8 @@ def data_processor(config):
# Anfangsbedingungen
ph_init
=
2
*
np
.
pi
*
np
.
random
.
rand
(
ny
,
nx
)
# ph_init = np.angle(np.fft.fft2(ph_init));
config
[
'u0'
]
=
inp
*
np
.
exp
(
1j
*
ph_init
)
previous
=
fft
.
fftshift
(
fft
.
ifft2
(
fft
.
ifftshift
(
config
[
'u0'
])))
config
[
'u
_
0'
]
=
inp
*
np
.
exp
(
1j
*
ph_init
)
previous
=
fft
.
fftshift
(
fft
.
ifft2
(
fft
.
ifftshift
(
config
[
'u
_
0'
])))
plt
.
figure
(
figsize
=
(
15
,
4
))
plt
.
subplot
(
131
)
...
...
@@ -39,3 +39,10 @@ def data_processor(config):
plt
.
colorbar
()
plt
.
title
(
"Initial support"
)
plt
.
show
()
# Other settings
config
[
'fresnel_nr'
]
=
0
config
[
'FT_conv_kernel'
]
=
1
config
[
'use_farfield_formula'
]
=
True
return
config
proxtoolbox/Problems/OrbitalTomog/phase.py
View file @
8bc6b016
...
...
@@ -5,299 +5,155 @@ from proxtoolbox import Algorithms
from
proxtoolbox
import
ProxOperators
from
proxtoolbox.ProxOperators.proxoperators
import
ProxOperator
from
proxtoolbox.Problems.Phase
import
Graphics
# from .proxoperators import ProxOperator
#from phase_retrieval.back_end_utilities import norm
from
numpy.linalg
import
norm
import
numpy
as
np
import
h5py
from
numpy
import
square
,
sqrt
,
nonzero
,
size
from
numpy
import
square
,
sqrt
class
Phase
(
Problem
):
"""
Phase Problem
"""
config
=
{
}
# This line does not do anything.
def
__init__
(
self
,
new_config
=
{}
):
config
=
{
}
def
__init__
(
self
,
new_config
):
"""
The initialization of a Phase instance takes the default configuration
and updates the parameters with the arguments in new_config.
Parameters
----------
new_config : dict, optional - Parameters to initialize the problem. If unspecified, the default config is used.
new_config : dict with non-empty keys:
object, constraint, experiment, distance, algorithm
Ny, Nx
noise
MAXIT, TOL
diagnostic
data, data_norm, support,
"""
self
.
config
=
new_config
self
.
config
=
new_config
#self.back_end = back_end
#call data processor, read data
module
=
__import__
(
self
.
config
[
'data_filename'
])
data_processor
=
getattr
(
module
,
self
.
config
[
'data_filename'
])
data_processor
(
self
.
config
)
#
module = __import__(self.config['data_filename'])
#
data_processor = getattr(module, self.config['data_filename'])
#
data_processor(self.config)
if
'Nz'
not
in
self
.
config
:
self
.
config
[
'Nz'
]
=
1
#If method_config[formulation is does not exist, i.e. not specified in
#the *_in.m file, use the product space as the default.
# If method_config['formulation'] does not exist, use the product space as the default.
if
'formulation'
in
self
.
config
:
formulation
=
self
.
config
[
'formulation'
]
else
:
formulation
=
'product space'
# Set the projectors and inputs based on the types of constraints and
# experiments
proxoperators
=
[
''
,
''
,
''
]
# Set the projectors and inputs based on the types of constraints and experiments
used_proxoperators
=
[
''
,
''
,
''
]
# Projector 1 (real / object space)
if
self
.
config
[
'constraint'
]
==
'support only'
:
proxoperators
[
0
]
=
'P_S'
used_
proxoperators
[
0
]
=
'P_S'
elif
self
.
config
[
'constraint'
]
==
'real and support'
:
proxoperators
[
0
]
=
'P_S_real'
elif
self
.
config
[
'constraint'
]
==
'nonnegative and support'
:
proxoperators
[
0
]
=
'P_SP'
elif
self
.
config
[
'constraint'
]
==
'amplitude only'
:
proxoperators
[
0
]
=
'P_amp'
elif
self
.
config
[
'constraint'
]
==
'phase on support'
:
proxoperators
[
0
]
=
'P_Amod'
elif
self
.
config
[
'constraint'
]
==
'minimum amplitude'
:
proxoperators
[
0
]
=
'P_min_amp'
used_proxoperators
[
0
]
=
'P_S_real'
elif
self
.
config
[
'constraint'
]
==
'nonnegative and support'
:
used_proxoperators
[
0
]
=
'P_SP'
elif
self
.
config
[
'constraint'
]
==
'amplitude only'
:
used_proxoperators
[
0
]
=
'P_amp'
proxoperators
[
1
]
=
'Approx_P_FreFra_Poisson'
# Projector 2 (k / Fourier space)
# used_proxoperators[1] = 'P_M' # 'Approx_P_FreFra_Poisson'
used_proxoperators
[
1
]
=
'Approx_P_FreFra_Poisson'
self
.
config
[
'proxoperators'
]
=
[]
for
prox
in
proxoperators
:
for
prox
in
used_
proxoperators
:
if
prox
!=
''
:
self
.
config
[
'proxoperators'
].
append
(
getattr
(
ProxOperators
,
prox
))
# input.Proj1_input.F=F; % is it any more expensive to pass everything
# into the projectors rather than just a selection of data and
# parameters? If not, and we pass everything anyway, there is no need
# to create a new structure element.
if
'product_space_dimension'
not
in
self
.
config
:
self
.
config
[
'product_space_dimension'
]
=
1
# set the animation program:
self
.
config
[
'animation'
]
=
'Phase_animation'
#
# if you are only working with two sets but
# want to do averaged projections
# (= alternating projections on the product space)
# or RAAR on the product space (=swarming), then
# you will want to change product_space_dimension=2
# and adjust your input files and projectors accordingly.
# you could also do this within the data processor
self
.
config
[
'animation'
]
=
'Phase_animation'
# if you are only working with two sets but want to do averaged projections (= alternating projections on the
# product space) or RAAR on the product space (=swarming), then you will want to change
# product_space_dimension=2 and adjust your input files and projectors accordingly. you could also do this
# within the data processor
self
.
config
[
'TOL2'
]
=
1e-15
#To estimate the gap in the sequential formulation, we build the
# appropriate point in the product space. This allows for code reuse.
# Note for sequential diversity diffraction, input.Proj1 is the "RCAAR"
# version of the function.
# To estimate the gap in the sequential formulation, we build the appropriate point in the product space.
# This allows for code reuse. Note for sequential diversity diffraction, input.Proj1 is the "RCAAR" version
# of the function.
if
formulation
==
'sequential'
:
for
j
in
range
(
self
.
config
[
'product_space_dimension'
]):
self
.
config
[
'proj_iter'
]
=
j
proj1
=
self
.
config
[
'proxoperators'
][
0
](
self
.
config
)
u_1
[:,:,
j
]
=
proj1
.
work
(
self
.
config
[
'u_0'
])
self
.
config
[
'proj_iter'
]
=
mod
(
j
,
config
[
'product_space_dimension'
])
+
1
proj1
=
self
.
config
[
'proxoperators'
][
0
](
self
.
config
)
u_1
[:,:,
j
]
=
proj1
.
work
(
self
.
config
[
'u_0'
])
end
else
:
#i.e. formulation=='product space'
proj1
=
self
.
config
[
'proxoperators'
][
0
](
self
.
config
)
u_1
=
proj1
.
work
(
self
.
config
[
'u_0'
])
proj2
=
self
.
config
[
'proxoperators'
][
1
](
self
.
config
)
u_2
=
proj2
.
work
(
u_1
)
raise
NotImplementedError
(
"This needs careful matlab -> python translation"
)
# for j in range(self.config['product_space_dimension']):
# self.config['proj_iter'] = j
# proj1 = self.config['proxoperators'][0](self.config)
# u_1[:, :, j] = proj1.work(self.config['u_0'])
# self.config['proj_iter'] = mod(j, config['product_space_dimension']) + 1
# proj1 = self.config['proxoperators'][0](self.config)
# u_1[:, :, j] = proj1.work(self.config['u_0'])
# end
else
:
# i.e. formulation=='product space'
proj_1
=
self
.
config
[
'proxoperators'
][
0
](
self
.
config
)
#, self.back_end)
u_1
=
proj_1
.
work
(
self
.
config
[
'u_0'
])
proj_2
=
self
.
config
[
'proxoperators'
][
1
](
self
.
config
)
#, self.back_end)
u_2
=
proj_2
.
work
(
u_1
)
# estimate the gap in the relevant metric
if
self
.
config
[
'Nx'
]
==
1
or
self
.
config
[
'Ny'
]
==
1
:
tmp_gap
=
square
(
norm
(
u_1
-
u_2
)
/
self
.
config
[
'norm_rt_data
'
])
if
self
.
config
[
'Nx'
]
==
1
or
self
.
config
[
'Ny'
]
==
1
:
# 1D problem
tmp_gap
=
square
(
norm
(
u_1
-
u_2
)
/
self
.
config
[
'
norm_data'
])
#
norm_rt_data
elif
self
.
config
[
'product_space_dimension'
]
==
1
:
tmp_gap
=
(
norm
(
u_1
-
u_2
)
/
self
.
config
[
'norm_
rt_
data'
])
**
2
tmp_gap
=
(
norm
(
u_1
-
u_2
)
/
self
.
config
[
'norm_data'
])
**
2
# norm_rt_data
else
:
tmp_gap
=
0
tmp_gap
=
0
for
j
in
range
(
self
.
config
[
'product_space_dimension'
]):
# compute (||P_Sx-P_Mx||/norm_data)^2:
tmp_gap
=
tmp_gap
+
(
norm
(
u_1
[:,:,
j
]
-
u_2
[:,:,
j
])
/
self
.
config
[
'norm_rt_data'
])
**
2
gap_0
=
sqrt
(
tmp_gap
)
tmp_gap
=
tmp_gap
+
(
norm
(
u_1
[:,
:,
j
]
-
u_2
[:,
:,
j
])
/
self
.
config
[
'norm_data'
])
**
2
# norm_rt_data
gap_0
=
sqrt
(
tmp_gap
)
# sets the set fattening to be a percentage of the initial gap to the unfattened set with respect to the
# relevant metric (KL or L2), that percentage given by input.data_ball input by the user.
self
.
config
[
'data_ball'
]
=
self
.
config
[
'data_ball'
]
*
gap_0
# the second tolerance relative to the order of magnitude of the metric
self
.
config
[
'TOL2'
]
=
self
.
config
[
'data_ball'
]
*
1e-15
# self.config['proxoperators']
self
.
algorithm
=
getattr
(
algorithms
,
self
.
config
[
'algorithm'
])(
self
.
config
)
#, self.back_end)
# sets the set fattening to be a percentage of the
# initial gap to the unfattened set with
# respect to the relevant metric (KL or L2),
# that percentage given by
# input.data_ball input by the user.
self
.
config
[
'data_ball'
]
=
self
.
config
[
'data_ball'
]
*
gap_0
# the second tolerance relative to the oder of
# magnitude of the metric
self
.
config
[
'TOL2'
]
=
self
.
config
[
'data_ball'
]
*
1e-15
self
.
config
[
'proxoperators'
]
self
.
algorithm
=
getattr
(
Algorithms
,
self
.
config
[
'algorithm'
])(
self
.
config
)
def
_presolve
(
self
):
"""
Prepares argument for actual solving routine
"""
pass
# Actually nothing for the phase problem
def
_solve
(
self
):
"""
Runs the algorithm to solve the given sudoku problem
Runs the algorithm to solve the given problem