Commit aeb25645 authored by daniel.schellhorn's avatar daniel.schellhorn
Browse files

added non-relative path references to GetDate while implementing zip...

added non-relative path references to GetDate while implementing zip extraction, adding SetProxyPythonPath.py in phase and links in experiments in phase
parent dd7370d1
import SetProxPythonPath
from proxtoolbox.experiments.phase.phaseExperiment import PhaseExperiment
from proxtoolbox.utils.mypoissonrnd import mypoissonrnd
from proxtoolbox.utils.gaussian import gaussian
......@@ -117,7 +119,7 @@ class CDI_Experiment(PhaseExperiment):
self.rt_data = Cell(1)
self.rt_data[0] = M
# standard for the main program is that
# standard for the main program is that
# the data field is the magnitude SQUARED
# in Luke.m this is changed to the magnitude.
self.data = Cell(1)
......@@ -127,10 +129,10 @@ class CDI_Experiment(PhaseExperiment):
self.support_idx = np.nonzero(S)
self.sets = 2
# use the abs_illumination field to represent the
# use the abs_illumination field to represent the
# support constraint.
self.abs_illumination = S
# initial guess
tmp_rnd = (np.random.rand(N, N)).T # to match Matlab
self.u0 = S * tmp_rnd
......
import SetProxPythonPath
from proxtoolbox.experiments.phase.phaseExperiment import PhaseExperiment
from proxtoolbox.utils import fft, ifft
from proxtoolbox.utils.loadMatFile import loadMatFile
......@@ -43,7 +44,7 @@ class CDP_Experiment(PhaseExperiment):
}
return defaultParams
def __init__(self,
def __init__(self,
warmup_iter = 0,
**kwargs):
"""
......@@ -66,14 +67,14 @@ class CDP_Experiment(PhaseExperiment):
"""
Load CDP dataset. Create the initial iterate.
"""
# Implementation of the Wirtinger Flow (WF) algorithm presented in the paper
# Implementation of the Wirtinger Flow (WF) algorithm presented in the paper
# "Phase Retrieval via Wirtinger Flow: Theory and algorithms"
# by E. J. Candes, X. Li, and M. Soltanolkotabi
# integrated into the ProxToolbox by
# integrated into the ProxToolbox by
# Russell Luke, September 2016.
# The input data are coded diffraction patterns about a random complex
# valued image.
# valued image.
# for debug:
# used predefined randomly generated data for the case where n1 = 128, n2 = 1
......@@ -84,7 +85,7 @@ class CDP_Experiment(PhaseExperiment):
debug_image = None
debug_masks = None
debug_z0 = None
# make image
if debug:
if n2 == 1:
......@@ -103,9 +104,9 @@ class CDP_Experiment(PhaseExperiment):
self.truth = x
self.norm_truth = norm(x,'fro')
self.truth_dim = x.shape
## Make masks and linear sampling operators
L = self.product_space_dimension # Number of masks
L = self.product_space_dimension # Number of masks
if debug:
masks = debug_masks
else:
......@@ -115,14 +116,14 @@ class CDP_Experiment(PhaseExperiment):
masks = np.random.choice(np.array([1j, -1j, 1, -1]),(L,n2))
else:
masks = np.random.choice(np.array([1j, -1j, 1, -1]),(n1,n2,L))
# Sample magnitudes and make masks
# Sample magnitudes and make masks
temp = random_sample(masks.shape) #works like rand but accepts tuple as argument
masks = masks * ( (temp <= 0.2)*sqrt(3) + (temp > 0.2)/sqrt(2) )
self.masks = conj(masks) # Saving the conjugate of the mask saves
# on computing the conjugate every time the
# mapping A (below) is applied.
# mapping A (below) is applied.
if n2 == 1:
# Make linear operators; A is forward map and At its scaled adjoint (At(Y)*numel(Y) is the adjoint)
A = lambda I: fft(conj(masks) * tile(I,[1, L])) # Input is n x 1 signal, output is n x L array
......@@ -134,16 +135,16 @@ class CDP_Experiment(PhaseExperiment):
else:
A = lambda I: fft2(masks * reshape(tile(I,[1, L]), (I.shape[0],I.shape[1], L), order='F')) # Input is n1 x n2 image, output is n1 x n2 x L array
At = lambda Y: mean(masks * ifft2(Y), 2).reshape((n1,n2)) # Input is n1 x n2 X L array, output is n1 x n2 image
# Support constraint: none
self.indicator_ampl = 1
self.abs_illumination = 1
self.illumination = 1
self.support_idx = np.nonzero(x.flatten(order = 'F')) # column-major order'
#alt_support_idx = find(Xi_A.T, 0, "!=")
# Data
# Data
if n2 == 1 or n1 == 1:
Y = abs(A(x))
else:
......@@ -157,28 +158,28 @@ class CDP_Experiment(PhaseExperiment):
self.rt_data = Y
Y = Y**2
self.data = Y
self.norm_data = sum(Y.flatten())/Y.size
normest = sqrt(self.norm_data) # Estimate norm to scale eigenvector
self.norm_data = sum(Y.flatten())/Y.size
normest = sqrt(self.norm_data) # Estimate norm to scale eigenvector
self.norm_rt_data = normest
# Initialization
npower_iter = self.warmup_iter # Number of power iterations
if debug:
# Initialization
npower_iter = self.warmup_iter # Number of power iterations
if debug:
z0 = debug_z0
else:
z0 = randn(n1,n2)
z0 = z0/norm(z0,'fro') # Initial guess
_tic = time.time()
# Power iterations
for _tt in range(npower_iter):
z0 = z0/norm(z0,'fro') # Initial guess
_tic = time.time()
# Power iterations
for _tt in range(npower_iter):
z0 = At(Y*A(z0))
z0 = z0/norm(z0)
_toc = time.time()
z = normest * z0 # Apply scaling
z = normest * z0 # Apply scaling
if n2 == 1:
_Relerrs = norm(x - exp(-1j*angle(trace(matmul(x.T.conj(), z)))) * z, 'fro')/norm(x,'fro')
self.u0 = tile(z,[1,L])
......@@ -188,7 +189,7 @@ class CDP_Experiment(PhaseExperiment):
else:
_Relerrs = norm(x - exp(-1j*angle(trace(matmul(x.T.conj(), z)))) * z, 'fro')/norm(x,'fro')
self.u0 = reshape(tile(z,[1, L]), (z.shape[0], z.shape[1], L), order='F') # order='F': to match Matlab
# The following is put here for now
if self.formulation == 'cyclic':
if self.Nx == 1:
......@@ -202,7 +203,7 @@ class CDP_Experiment(PhaseExperiment):
# converted from Matlab makes sense
self.product_space_dimension = self.sets
self.sets = 2
#print('Run time of initialization:', toc-tic, 's')
#print('Relative error after initialization:', Relerrs)
#print('\n')
......@@ -213,9 +214,9 @@ class CDP_Experiment(PhaseExperiment):
Determine the prox operators to be used for this experiment
"""
super(CDP_Experiment, self).setupProxOperators() # call parent's method
if self.formulation == 'cyclic':
self.proxOperators = []
self.proxOperators = []
self.nProx = self.sets
for _j in range(self.nProx):
self.proxOperators.append('P_CDP_cyclic')
......@@ -225,11 +226,11 @@ class CDP_Experiment(PhaseExperiment):
self.proxOperators = []
self.proxOperators.append('P_diag')
self.proxOperators.append('P_CDP')
self.propagator = 'Propagator_FreFra'
self.inverse_propagator = 'InvPropagator_FreFra'
def createTestImage(self):
"""
......@@ -247,8 +248,8 @@ class CDP_Experiment(PhaseExperiment):
2-D array masks
z0 : ndarray
initial guess
"""
"""
image = np.array([[-0.947246643957371 - 2.01621153886074j],\
[0.540149747070348 - 1.52496021647706j],\
[-0.216602140976276 + 0.458677002745254j],\
......@@ -506,7 +507,7 @@ class CDP_Experiment(PhaseExperiment):
[0.00000000000000 - 0.707106781186548j, -0.707106781186548 + 0.00000000000000j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 - 0.707106781186548j, 0.00000000000000 - 1.73205080756888j, 0.707106781186548 + 0.00000000000000j, 0.00000000000000 - 0.707106781186548j, 0.707106781186548 + 0.00000000000000j, -0.707106781186548 + 0.00000000000000j, 0.707106781186548 + 0.00000000000000j],\
[-0.707106781186548 + 0.00000000000000j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 - 0.707106781186548j, 0.707106781186548 + 0.00000000000000j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 + 1.73205080756888j, -0.707106781186548 + 0.00000000000000j, 0.00000000000000 + 1.73205080756888j],\
[-0.707106781186548 + 0.00000000000000j, 1.73205080756888 + 0.00000000000000j, 0.00000000000000 - 0.707106781186548j, 1.73205080756888 + 0.00000000000000j, -0.707106781186548 + 0.00000000000000j, 0.00000000000000 - 1.73205080756888j, -0.707106781186548 + 0.00000000000000j, 0.00000000000000 + 1.73205080756888j, 0.00000000000000 + 0.707106781186548j, 0.00000000000000 - 0.707106781186548j]])
z0 = np.array([0.0848802595989635,0.134817251982901,
-0.0550294990313983,0.0207696058654980,
0.0572518900492754,0.0304482413548984,
......@@ -573,6 +574,3 @@ class CDP_Experiment(PhaseExperiment):
0.0645047500348074,-0.0627622327830579])
z0 = np.reshape(z0, (128, 1)) # transform 1-D array z0 into 128x1 matrix
return image, masks, z0
import SetProxPythonPath
from proxtoolbox.experiments.phase.phaseExperiment import PhaseExperiment
from proxtoolbox import proxoperators
import numpy as np
......@@ -48,7 +49,7 @@ class Elser_Experiment(PhaseExperiment):
'graphics': 1
}
return defaultParams
def __init__(self, Atoms=100, category='E', **kwargs):
super(Elser_Experiment, self).__init__(**kwargs)
......@@ -67,21 +68,20 @@ class Elser_Experiment(PhaseExperiment):
"""
Load Elser dataset. Create the initial iterate.
"""
#make sure input data can be found, otherwise download it
#make sure input data can be found, otherwise download it
GetData.getData('Elser')
#defaultParams = getDefaultParameters()
# So far only Nx to set the desired
# resolution
newres = self.Nx
# The following value for snr does not work well in Python
# as this creates numerical issues with Poisson noise
# as this creates numerical issues with Poisson noise
# (data_ball is far too small)
# snr = self.data_ball
# Instead, we choose the following bigger value which
# Instead, we choose the following bigger value which
# works well.
snr = 1e-8
snr = 1e-8
print('Loading data.')
fn="".join(['../InputData/Phase/Elser/data', str(self.Atoms), self.category])
......@@ -99,7 +99,7 @@ class Elser_Experiment(PhaseExperiment):
self.rt_data = Cell(1)
self.rt_data[0] = M
# standard for the main program is that
# standard for the main program is that
# the data field is the magnitude SQUARED
# in Luke.m this is changed to the magnitude.
self.data = Cell(1)
......@@ -109,9 +109,9 @@ class Elser_Experiment(PhaseExperiment):
self.support_idx = np.nonzero(S)
self.sets = 2
# The support constraint is sparsity determined by the number of atoms indicated in the
# data file name...this needs to be installed
# The support constraint is sparsity determined by the number of atoms indicated in the
# data file name...this needs to be installed
# initial guess: follow Elser's initialization...
def setupProxOperators(self):
......@@ -135,12 +135,12 @@ class Elser_Experiment(PhaseExperiment):
self.nProx = self.sets
# Now we adjust data structures and prox operators according to the algorithm
# What follows wa set up for JWST and works in that context. Not
# What follows wa set up for JWST and works in that context. Not
# sure how much of this is trasferrable to Elsers problems (DRL 14.08.2020)
if self.algorithm_name == 'ADMM':
# set-up variables in cells for block-wise algorithms
# ADMM has three blocks of variables, primal, auxilliary (primal
# variables in the image space of some linear mapping) and dual.
# variables in the image space of some linear mapping) and dual.
# we sort these into a 3D cell.
u0 = self.u0
self.u0 = Cell(3)
......@@ -158,7 +158,7 @@ class Elser_Experiment(PhaseExperiment):
self.u0[1][j] = fft2(self.FT_conv_kernel[j]*tmp_u0) / (self.Nx*self.Ny)
self.u0[2][j] = self.u0[1][j] / self.lambda_0
self.proxOperators.append('Prox_primal_ADMM_indicator')
self.proxOperators.append('Approx_Pphase_FreFra_ADMM_Poisson')
self.proxOperators.append('Approx_Pphase_FreFra_ADMM_Poisson')
elif self.algorithm_name == 'AvP2':
# u_0 should be a cell...we change it into a cell of cells
u0 = self.u0
......@@ -170,7 +170,7 @@ class Elser_Experiment(PhaseExperiment):
P_Diag_prox = P_Diag_class(self)
tmp_u = P_Diag_prox.eval(self.u0[2])
self.u0[1] = tmp_u[0]
self.u0[0] = u0[self.product_space_dimension-1]
self.u0[0] = u0[self.product_space_dimension-1]
elif self.algorithm_name == 'PHeBIE':
# set up variables in cells for block-wise, implicit-explicit algorithms
u0 = self.u0
......@@ -195,7 +195,7 @@ class Elser_Experiment(PhaseExperiment):
self.masks = Cell(L)
for j in range(L):
self.masks[j] = self.indicator_ampl * self.FT_conv_kernel[j]
self.data_zeros = None
self.data_zeros = None
self.FT_conv_kernel = self.masks
self.proxOperators[1] = 'Approx_Pphase_JWST_Wirt'
self.use_farfield_formula = True
......@@ -206,7 +206,7 @@ class Elser_Experiment(PhaseExperiment):
Generate graphical output from the solution
"""
# display plot of far field data
# display plot of far field data
# figure(123)
f, (ax1) = subplots(1, 1,
figsize=(self.figure_width, self.figure_height),
......@@ -263,7 +263,7 @@ class Elser_Experiment(PhaseExperiment):
dpi = self.figure_dpi)
self.createImageSubFigure(f, ax1, abs(u), titles[0])
self.createImageSubFigure(f, ax2, real(u), titles[1])
ax3.semilogy(changes)
ax3.set_xlabel(label)
ax3.set_ylabel('Log of change in iterates')
......@@ -277,12 +277,10 @@ class Elser_Experiment(PhaseExperiment):
f.suptitle(title)
plt.subplots_adjust(hspace = 0.3) # adjust vertical space (height) between subplots (default = 0.2)
plt.subplots_adjust(wspace = 0.3) # adjust horizontal space (width) between subplots (default = 0.2)
show()
if __name__ == "__main__":
Elser = Elser_Experiment(Atoms=200, category='E', algorithm='RRR', lambda_0=0.95, lambda_max=0.95, anim=True)
Elser.run()
Elser.show()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment