mirror of
https://github.com/c-sooyoung/fold_slice.git
synced 2026-09-17 19:39:08 +09:00
initial commit
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
% Calculate ZincBlende Bandstructure from Walter Harrison's book with an
|
||||
% added extra s* state for the Conduction band (labelled as t since * inside
|
||||
% variable names is not allowed) as in Vogl's paper
|
||||
|
||||
%Onsite energies are given for cation then anion (same for elemental)
|
||||
% e.g. ecs is energy for cation s, eap is energy for anion p.
|
||||
|
||||
hm = 7.62; % hbar^2/m in eV.A^2
|
||||
|
||||
% Cubic lattice constant
|
||||
alat = 5.65; % GaAs lattice constant
|
||||
|
||||
% GaAs
|
||||
% Onsite Matrix elements (in eV)
|
||||
eas = -8.3431;
|
||||
eap = 1.0414;
|
||||
eat = 8.5914;
|
||||
|
||||
ecs = -2.6569;
|
||||
ecp = 3.6686;
|
||||
ect = 6.7386;
|
||||
|
||||
|
||||
%vectors to neighboring cations
|
||||
d1 = alat/4 * [1 1 1];
|
||||
d2 = alat/4 * [1 -1 -1];
|
||||
d3 = alat/4 * [-1 1 -1];
|
||||
d4 = alat/4 * [-1 -1 1];
|
||||
d = [d1; d2; d3; d4];
|
||||
|
||||
%phase factors
|
||||
g0 = inline('exp(i*k*transpose(d(1,:)))+exp(i*k*transpose(d(2,:)))+exp(i*k*transpose(d(3,:)))+exp(i*k*transpose(d(4,:)))','k','d');
|
||||
g1 = inline('exp(i*k*transpose(d(1,:)))+exp(i*k*transpose(d(2,:)))-exp(i*k*transpose(d(3,:)))-exp(i*k*transpose(d(4,:)))','k','d');
|
||||
g2 = inline('exp(i*k*transpose(d(1,:)))-exp(i*k*transpose(d(2,:)))+exp(i*k*transpose(d(3,:)))-exp(i*k*transpose(d(4,:)))','k','d');
|
||||
g3 = inline('exp(i*k*transpose(d(1,:)))-exp(i*k*transpose(d(2,:)))-exp(i*k*transpose(d(3,:)))+exp(i*k*transpose(d(4,:)))','k','d');
|
||||
|
||||
%Interatomic Matrix Elements (eV) using Harrison's universal parameters
|
||||
dn = norm(d1);
|
||||
|
||||
% Composite Matrix elements
|
||||
Ess = -1.4054* hm/dn^2;
|
||||
Esp = 1.0392*(hm/dn^2);
|
||||
Exx = 0.3111*(hm/dn^2);
|
||||
Exy = 0.8298*(hm/dn^2);
|
||||
Etp = 0.9549* hm/dn^2; % Harrison parameter for s*ps from Vtps*dn^2/hm
|
||||
Ett = 0; % In Dow's model no coupling between s* states on adjacent atoms is allowed
|
||||
|
||||
|
||||
% Zone boundary and number of k points
|
||||
qbz = 2*pi/alat;
|
||||
nk=20;
|
||||
j=1;
|
||||
|
||||
E=1;clear E;
|
||||
kx=1; clear kx;
|
||||
for q=0:qbz/nk:qbz,
|
||||
k = [q 0 0];
|
||||
|
||||
%LCAO hamiltonian for the zincblende structure
|
||||
% Calculate Upper Half of the LCAO Hamiltonian Matrix and use its
|
||||
% Hermiticity to get lower half
|
||||
Hu = [[ ecs/2 Ess *g0(k,d) 0 0 0 Esp*g1(k,d) Esp * g2(k,d) Esp* g3(k,d) 0 0];
|
||||
[ 0 eas/2 -Esp * conj(g1(k,d)) -Esp * conj(g2(k,d)) -Esp * conj(g3(k,d)) 0 0 0 0 0];
|
||||
[ 0 0 ecp/2 0 0 Exx * g0(k,d) Exy * g3(k,d) Exy * g2(k,d) 0 -Etp * g1(k,d)];
|
||||
[ 0 0 0 ecp/2 0 Exy * g3(k,d) Exx * g0(k,d) Exy * g1(k,d) 0 -Etp * g2(k,d) ];
|
||||
[ 0 0 0 0 ecp/2 Exy * g2(k,d) Exy * g1(k,d) Exx * g0(k,d) 0 -Etp * g3(k,d)];
|
||||
[ 0 0 0 0 0 eap/2 0 0 Etp * g1(k,d) 0 ];
|
||||
[ 0 0 0 0 0 0 eap/2 0 Etp * g2(k,d) 0 ];
|
||||
[ 0 0 0 0 0 0 0 eap/2 Etp * g3(k,d) 0 ];
|
||||
[ 0 0 0 0 0 0 0 0 eat/2 Ett *g0(k,d) ];
|
||||
[ 0 0 0 0 0 0 0 0 0 eat/2];
|
||||
];
|
||||
|
||||
%Its adjoint
|
||||
Hd = (Hu)';
|
||||
|
||||
%The full Hermitian Matrix
|
||||
H = Hu+Hd;
|
||||
|
||||
%Calculate Eigenvalues
|
||||
E(j,:) = eig(H)';
|
||||
kx(j)=q;
|
||||
j=j+1;
|
||||
|
||||
end;
|
||||
|
||||
|
||||
plot(kx,E,'k');
|
||||
ylabel('Energy (eV)');
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
%BACK_PROJ backpropagate the updated projections (multilayer slices) into
|
||||
%the full 3D volume, call thi function separatelly for real and imaginary
|
||||
%part of the projections
|
||||
%
|
||||
% volData_upd = back_proj(projData_upd,theta,Npx_vol)
|
||||
%
|
||||
% Inputs:
|
||||
% **projData_upd - stacked 2D array, linearized difference of the complex-valued projections, ie log( P_new .* conj(P_orig) ./ |P_orig|^2)
|
||||
% **theta - scalar, angle of the provided projection
|
||||
% **Npx_vol - dimensions of the full volume
|
||||
% *returns*
|
||||
% ++volData_upd - 3D array, linearized update of the tomographic volume
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function volData_upd = back_proj(projData_upd,theta,Npx_vol)
|
||||
|
||||
projData_upd = gpuArray(projData_upd); % difference after ptycho update
|
||||
|
||||
assert(isreal(projData_upd), 'Inputs are assumed to be real valued')
|
||||
|
||||
|
||||
|
||||
Nlayers = size(projData_upd,3);
|
||||
|
||||
|
||||
% permute to see it from the top direction
|
||||
projData_upd = permute(projData_upd, [2,3,1]);
|
||||
|
||||
% interpolate back to the full size
|
||||
if Nlayers == 1
|
||||
% faster option for single layer
|
||||
volData_upd = repmat(projData_upd* (Nlayers/Npx_vol(2)) ,[1,Npx_vol(2),1]);
|
||||
else
|
||||
|
||||
|
||||
%% periodic boundary
|
||||
binning = ceil(Npx_vol(2)/Nlayers/4); % in order to reduce computational requirements, use a combination of interpolation FFT and simple and fast upsampling
|
||||
Nl_upsample = Npx_vol(2) / binning;
|
||||
assert(Nl_upsample == round(Nl_upsample), 'Volume size cannot be split for binning')
|
||||
% revert the 0.5px offset along the beam direction , process real and imag part separatelly
|
||||
% to avoid mixing
|
||||
|
||||
projData_upd = projData_upd * (Nlayers/Nl_upsample/binning);
|
||||
|
||||
%% %%% centered FFT interpolation %%%
|
||||
|
||||
% pre-fft transform
|
||||
projData_upd = fft(projData_upd,[], 2);
|
||||
% apply 0.5 pixel shift
|
||||
projData_upd = utils.imshift_fft_ax(projData_upd,0.5,2, false);
|
||||
% interpolate to the full size
|
||||
volData_upd = utils.interpolateFT_ax(projData_upd,Nl_upsample,2, false);
|
||||
% post-ifft transform
|
||||
volData_upd = real(ifft(volData_upd,[], 2));
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
% finally quickly upsample to the full required size
|
||||
volData_upd = utils.unbinning_2D(volData_upd, [1, binning]);
|
||||
end
|
||||
|
||||
|
||||
% back rotate to the basic orientation , use bilinear interpolation
|
||||
% because it is much faster than FFT rotation
|
||||
volData_upd = utils.imrotate_ax(volData_upd, -theta, 3, 0, 'bicubic');
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
%% run asynchronous ptychography solver for ptychotomo iterative method
|
||||
%% load one of the projections in queue, process, save results, load the next one ....
|
||||
|
||||
|
||||
clear
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%% BASIC SETTINGS %%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
initial_settings = 'p_initial.mat'; % where are saved basic settings for the solver (p structure)
|
||||
|
||||
base_path = core.find_base_package;
|
||||
tomo_path = '../cSAXS_matlab_tomo/'; % !! hardcoded path to tomography !! FIXME
|
||||
|
||||
ptycho_path = './';
|
||||
GPU_id = []; % [] == automatically select the next availible GPU, use share memory to communicate between multiple solvers
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
addpath(base_path)
|
||||
addpath(fullfile(ptycho_path, 'utils'))
|
||||
addpath(tomo_path)
|
||||
import utils.*
|
||||
|
||||
load(initial_settings)
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% use shared memory id = 0 to cummunicate between matlab which GPU to use
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
% choose GPU to use
|
||||
if ~isempty(GPU_id)
|
||||
p.gpu_id = GPU_id;
|
||||
else
|
||||
data = [];
|
||||
shm_mem = shm(true,20123213,false);
|
||||
try [shm_mem, data] = shm_mem.attach; end
|
||||
if ~isempty(data)
|
||||
verbose(0, 'Last GPU %i', data)
|
||||
GPU_id = 1+mod(data, gpuDeviceCount);
|
||||
data(1) = GPU_id;
|
||||
shm_mem.free;
|
||||
shm_mem.upload(GPU_id)
|
||||
[shm_mem, data] = shm_mem.attach;
|
||||
else
|
||||
GPU_id = 1;
|
||||
shm_mem.upload(GPU_id);
|
||||
end
|
||||
shm_mem.detach
|
||||
|
||||
utils.verbose(-1, 'Using GPU %i', GPU_id)
|
||||
end
|
||||
|
||||
p0.gpu_id = GPU_id;
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
p0.verbose_level = 3;
|
||||
|
||||
verbose(p0.verbose_level)
|
||||
verbose(0,'RECONSTRUCTING ON GPU %i', GPU_id)
|
||||
|
||||
while true
|
||||
[p, status] = scans.get_queue(p0, false);
|
||||
if status == false
|
||||
verbose(0,'Waiting for data')
|
||||
verbose(-1)
|
||||
pause(0.5)
|
||||
continue
|
||||
end
|
||||
|
||||
verbose(0)
|
||||
|
||||
verbose(0,'Processing %i', p.scan_number)
|
||||
|
||||
% prepare path
|
||||
prepare_data_path = sprintf(p.prepare_data_path, p.scan_number);
|
||||
|
||||
%% run the ptycho solver
|
||||
try
|
||||
pout = ptychotomo.ptycho_solver_distributed(GPU_id, prepare_data_path);
|
||||
catch err
|
||||
warning(err.message)
|
||||
end
|
||||
% move to finished files
|
||||
try
|
||||
scans.queue.update_queue.filelist(p)
|
||||
end
|
||||
verbose(0)
|
||||
pause(0.1)
|
||||
end
|
||||
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
%COMPRESS_VOLUME this function should be used to reduce the data volume
|
||||
%stored on GPU, probably conversion to float16 should be implemented here,
|
||||
%now this function is only void
|
||||
%
|
||||
% volData = compress_volume(volData, par)
|
||||
%
|
||||
% Inputs:
|
||||
% **volData - 3D array, linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **par - parameter structure
|
||||
% *returns*
|
||||
% ++volData - compressed data volume
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function volData = compress_volume(volData, par)
|
||||
% convert to uint8
|
||||
|
||||
% assert(isa(volData, 'single') || (isa(volData, 'gpuArray') && strcmpi(classUnderlying(volData), 'single')), 'Expected input should be single')
|
||||
%
|
||||
% if isa(volData, 'gpuArray')
|
||||
% volData = arrayfun(@kernel, volData, par.min_value, par.max_value);
|
||||
% else
|
||||
% volData = 1+1i - complex(real(volData-par.min_value) / real(par.max_value - par.min_value), ...
|
||||
% imag(volData-par.min_value) / imag(par.max_value - par.min_value)) ;
|
||||
% volData = uint8(volData*255);
|
||||
% end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
function volData = kernel(volData, min_value, max_value)
|
||||
|
||||
volData = 1+1i - complex(real(volData-min_value) / real(max_value - min_value), ...
|
||||
imag(volData-min_value) / imag(max_value - min_value)) ;
|
||||
|
||||
volData = uint8(volData*255);
|
||||
|
||||
|
||||
end
|
||||
@@ -0,0 +1,110 @@
|
||||
%DECOMPRESS_VOLUME this function should be used to decompress the reduced data volume
|
||||
%stored on GPU, probably conversion to from float16 to float32 should be implemented here,
|
||||
%now this function is only void
|
||||
%
|
||||
% volData = decompress_volume(volData, par)
|
||||
%
|
||||
% Inputs:
|
||||
% **volData - 3D array, linearized tomographic volume after compression
|
||||
% **par - parameter structure
|
||||
% *returns*
|
||||
% ++volData - decompressed data volume
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function volData = decompress_volume(volData, par)
|
||||
% convert to singles
|
||||
|
||||
% assert(isa(volData, 'uint8') || (isa(volData, 'gpuArray') && strcmpi(classUnderlying(volData), 'uint8')), 'Expected input should be single')
|
||||
%
|
||||
%
|
||||
%
|
||||
%
|
||||
% if isa(volData, 'gpuArray')
|
||||
% volData = arrayfun(@kernel, volData, par.min_value, par.max_value);
|
||||
% else
|
||||
%
|
||||
% volData = single(volData) / 255;
|
||||
% volData = -(volData - ( 1+1i ));
|
||||
% volData = complex(real(volData) * real(par.max_value - par.min_value) + real(par.min_value), ...
|
||||
% imag(volData) * imag(par.max_value - par.min_value) + imag(par.min_value)) ;
|
||||
%
|
||||
%
|
||||
% end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
function volData = kernel(volData, min_value, max_value)
|
||||
|
||||
volData = single(volData) / 255;
|
||||
volData = - (volData - ( 1+1i ));
|
||||
volData = complex(real(volData) * real(max_value - min_value) + real(min_value), ...
|
||||
imag(volData) * imag(max_value - min_value) + imag(min_value)) ;
|
||||
|
||||
|
||||
end
|
||||
@@ -0,0 +1,132 @@
|
||||
%FWD_PROJ get complex projections for multilayer ptychography from the provided 3D volume generate
|
||||
%
|
||||
% projData = fwd_proj(volData, theta,Nlayers, type)
|
||||
%
|
||||
% Inputs:
|
||||
% **volData - 3D array, linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **theta - scalar, angle of the provided projection
|
||||
% **Nlayers - int, number of layers to split the projected volume
|
||||
% **type - either @real or @imag, each part is processed separatelly to avoid mixing during iterpolation in the rotation
|
||||
% *returns*
|
||||
% ++projData - multilayer object for ptychography
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function projData = fwd_proj(volData, theta,Nlayers, type)
|
||||
import utils.crop_pad
|
||||
|
||||
|
||||
% pick real or imaginary
|
||||
fun = str2func(type);
|
||||
volData = fun(volData);
|
||||
|
||||
if ~isreal(volData)
|
||||
error('Process real and imaginary part separatelly')
|
||||
end
|
||||
|
||||
% rotate volume to the given angle
|
||||
projData = utils.imrotate_ax(volData, theta, 3, 0, 'bicubic');
|
||||
|
||||
|
||||
Nw = size(projData,2);
|
||||
|
||||
|
||||
% permute to see it from the beam direction
|
||||
if Nlayers == 1
|
||||
% fast option for single layer
|
||||
projData = mean(projData,2)*Nw/Nlayers;
|
||||
else
|
||||
% slower option for multilayer
|
||||
|
||||
|
||||
%% periodic boundary interpolate real and imaginary part separatelly
|
||||
% first decimate the data quickly by binning
|
||||
binning = ceil(Nw/Nlayers/4);
|
||||
projData = utils.binning_2D(projData, [1, binning]); % in order to reduce computational requirements, use a combination of interpolation FFT and simple and fast upsampling
|
||||
Nw = size(projData,2);
|
||||
% now do more precise downsampling by centered FFT interpolation
|
||||
|
||||
%% %%% centered FFT interpolation %%%
|
||||
|
||||
% pre-fft tranform
|
||||
projData = fft(projData,[], 2);
|
||||
% interpolate + 0.5 px shift
|
||||
projData = utils.interpolateFT_ax(projData,Nlayers,2, false);
|
||||
projData = utils.imshift_fft_ax(projData,-0.5,2, false);
|
||||
% post-ifft transform
|
||||
projData = real(ifft(projData,[], 2));
|
||||
projData = projData * (binning*Nw/Nlayers);
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
end
|
||||
|
||||
|
||||
% permute the axis so the the projection is seen along the beam
|
||||
projData = permute(projData, [3,1,2]);
|
||||
|
||||
|
||||
end
|
||||
@@ -0,0 +1,217 @@
|
||||
%GATHER_DISTRIBUTED_RECONSTRUCTIONS gather object reconstructions from the shared
|
||||
% memory, calculate difference from the initial reconstruction, and apply
|
||||
% update into the full tomographic volume.
|
||||
%
|
||||
% [volData,projData_new, fourier_error, update_norm] = ...
|
||||
% gather_distributed_reconstructions(volData, projData_model, ptycho_results, par)
|
||||
%
|
||||
% Inputs:
|
||||
% **volData - 3D array, linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **projData_model - structure that contain complex projection, initial guess and other values related to the currently processed angle
|
||||
% **ptycho_results - output from ptychography, if empty, load it from the share memory array
|
||||
% **par - parameter structure for ptychotomo
|
||||
%
|
||||
% *returns*
|
||||
% ++volData - updated tomographic volume
|
||||
% ++projData_new - updated projection structure
|
||||
% ++fourier_error - vector, fourier error reported by ptychography
|
||||
% ++update_norm - scalar, difference between original and new tomo volume
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function [volData,projData_new, fourier_error, update_norm] = ...
|
||||
gather_distributed_reconstructions(volData, projData_model, ptycho_results, par)
|
||||
|
||||
|
||||
% prepare path
|
||||
p.prepare_data_path = sprintf(par.prepare_data_path, projData_model.scan_id);
|
||||
Npx_proj = size(projData_model.object);
|
||||
|
||||
|
||||
%% GATHER DATA FROM SHARED MEMORY
|
||||
if ~isempty(ptycho_results) && isstruct(ptycho_results)
|
||||
% avoid loading from disk if the cached data are available
|
||||
data.pout = ptycho_results;
|
||||
data.ferr = nan;
|
||||
else
|
||||
% load reoonstruction from disk, calculate optimal update for given angle, use it to calculate
|
||||
% optimal volume update
|
||||
|
||||
for ii = 1:3
|
||||
% try twice before failing
|
||||
try
|
||||
data = load([p. prepare_data_path, '/output_reconstruction.mat'], 'pout', 'ferr');
|
||||
assert(isfield(data, 'ferr'), 'Results not loeaded correctly')
|
||||
break
|
||||
end
|
||||
pause(0.5)
|
||||
end
|
||||
if ~exist('data', 'var')
|
||||
error('Reconstruction %s not found', p. prepare_data_path)
|
||||
end
|
||||
for ii = 1:3
|
||||
try
|
||||
% gather to the shared memory
|
||||
[ptycho_results, data_shm] = ptycho_results.attach();
|
||||
break
|
||||
end
|
||||
pause(0.5)
|
||||
end
|
||||
if isempty(ptycho_results)
|
||||
keyboard
|
||||
end
|
||||
if ~exist('data_shm', 'var')
|
||||
error('Shared data not found')
|
||||
end
|
||||
|
||||
data.pout.object{1} = data_shm;% force matlab to allocate new memory
|
||||
if numel(data_shm) ~= prod(Npx_proj)
|
||||
warning('Wrong object size returned')
|
||||
keyboard
|
||||
end
|
||||
if isreal(data_shm) || any(all(all(imag(data_shm)==0)))
|
||||
warning('Something went wrong, skipping projection update')
|
||||
% something wrong happened with the data in the shared memory, it
|
||||
% seems that the complex part was lost at least partly -> skip
|
||||
% this update and try next projection
|
||||
projData_new = projData_model;
|
||||
fourier_error= nan;
|
||||
update_norm= nan;
|
||||
return
|
||||
end
|
||||
|
||||
clear ptycho_results
|
||||
|
||||
end
|
||||
|
||||
%% GET UPDATE FROM THE COMPLEX VALUED PROJECTION
|
||||
|
||||
projData_new = projData_model; % make a new structure by a compy of the previous one
|
||||
% return sorted for ptychography
|
||||
projData_new.object = utils.Garray(squeeze(data.pout.object{1}(:,:,:,end:-1:1)));
|
||||
projData_new.probe = squeeze(data.pout.probes);
|
||||
projData_new.positions = data.pout.positions;
|
||||
|
||||
% enforce full transmission in the nonmeasured regions
|
||||
missing = (projData_new.weight)==0;
|
||||
projData_new.object = projData_new.object .* ~missing + missing;
|
||||
|
||||
% compare the model object_c and the updated object, use the difference to find new object_c
|
||||
projData_new.object_c = ptychotomo.prepare_projections(projData_new.object, Npx_proj, data.pout.asize, false, projData_model.object_c, projData_model.weight);
|
||||
projData_new.object = gather(projData_new.object);
|
||||
projData_new.weight = gather(projData_new.weight);
|
||||
|
||||
|
||||
projData_c_upd = gpuArray(projData_new.object_c - projData_model.object_c);
|
||||
|
||||
|
||||
% subtract potential offset in the global phase or aplitude
|
||||
offset_tot = 0;
|
||||
for ii = 1:3
|
||||
offset = mean(mean(mean((projData_c_upd),3) .* projData_new.weight)) ./ mean(mean( projData_new.weight));
|
||||
projData_c_upd = (projData_c_upd-offset) .* projData_new.weight;
|
||||
offset_tot = offset_tot + offset;
|
||||
end
|
||||
utils.verbose(1,'Offset removal gather %3.2e+%3.2ei', real(offset_tot), imag(offset_tot));
|
||||
|
||||
|
||||
|
||||
|
||||
if 2*sum(math.norm2(projData_c_upd)) > 0.5
|
||||
warning('Probably some convergence issue')
|
||||
keyboard
|
||||
end
|
||||
|
||||
%%%%%%%%%%%%%%%%%% BACKPROJECT THE UPDATE TO THE VOLUME %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
[volData, update_norm] = ptychotomo.update_volume(volData, projData_c_upd, par.update_step, projData_model, par);
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
if update_norm > 0.1
|
||||
warning('Probably some convergence issue')
|
||||
keyboard
|
||||
end
|
||||
|
||||
% store the filtered / regularized version for the next iteration guess
|
||||
projData_new.object_c = gather((projData_model.object_c + projData_c_upd));
|
||||
% already a version with corrections
|
||||
projData_new.object = exp(projData_new.object_c);
|
||||
|
||||
|
||||
|
||||
|
||||
try
|
||||
fourier_error = data.ferr ;
|
||||
catch
|
||||
fourier_error = nan(2,1);
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
@@ -0,0 +1,330 @@
|
||||
%PREPARE_DISTRIBUTED_DATA use current tomographic volume reconstruction volData and
|
||||
% previous projection reconstructions projData_rec to create a new initial
|
||||
% guess for the ML ptychography
|
||||
%
|
||||
% [projData_model, projData_rec, shm_mem] = prepare_distributed_data(p, volData, projData_rec, layer_distance, par, init_level, save_init_guess)
|
||||
%
|
||||
% Inputs:
|
||||
% **p - ptycho p structure
|
||||
% **volData - 3D array, linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **projData_rec - structure that contains complex projection reconstructed in the previous iteration
|
||||
% **layer_distance - vector, distance between layers in meters
|
||||
% **par - parameter structure for ptychotomo
|
||||
% **init_level - flag: -1 == very first initialization, 0 = no initialization, 1 == force ptychography cache to update
|
||||
%
|
||||
% *returns*
|
||||
% ++projData_model - model projection structure
|
||||
% ++projData_rec - reconstructed projection structure
|
||||
% ++shm_mem - @shm class object that contain reference to the shared memory
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
|
||||
function [projData_model, projData_rec, shm_mem] = prepare_distributed_data(p, volData, projData_rec, layer_distance, par, init_level, save_init_guess)
|
||||
|
||||
try
|
||||
%% create initial guess for ptychography, clear object_c if number of layers in increased
|
||||
if isempty(projData_rec.object)
|
||||
projData_rec.object = exp(projData_rec.object_c);
|
||||
warning('Object was missing in projData_rec')
|
||||
end
|
||||
Npx_proj = size(projData_rec.object);
|
||||
|
||||
|
||||
Nlayers = length(layer_distance)+1;
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%% GET PROJECTION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
projData_model = get_forward_model(projData_rec, volData, Nlayers);
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
% move on GPU
|
||||
projData_rec.object_c = utils.Garray(projData_rec.object_c);
|
||||
|
||||
expand_layers = size(projData_rec,3) < Nlayers;
|
||||
if expand_layers
|
||||
% cheaper version but less exact
|
||||
projData_rec.object_c = repmat(sum(projData_rec.object_c,3),1,1,Nlayers)/Nlayers;
|
||||
projData_rec.object = [];
|
||||
end
|
||||
|
||||
|
||||
% find reliability region of the current model
|
||||
Npx_proj_small = size(projData_model.object_c);
|
||||
win = utils.Garray(single(tukeywin(Npx_proj_small(2), 0.2))'.*single(tukeywin(Npx_proj_small(1), 0.2)));
|
||||
win = utils.crop_pad(win, Npx_proj(1:2));
|
||||
win_shifted = utils.imshift_fast(win, projData_rec.position_offset(1), projData_rec.position_offset(2));
|
||||
|
||||
|
||||
weight = utils.Garray(projData_rec.weight) ;
|
||||
if init_level == -1 % first initialization
|
||||
% estimate air region
|
||||
W_tmp = weight .* exp(-abs(sum(projData_model.object_c,3))) .* win_shifted;
|
||||
% remove offset between model and the reconstruction.
|
||||
projData_rec = remove_offset_initial(projData_model,projData_rec, W_tmp, Npx_proj_small);
|
||||
% run only the initialization
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
%% merge phase and amplitude from tomo and measurements
|
||||
projData_model.object_c = win_shifted .*projData_model.object_c + (1-win_shifted).* projData_rec.object_c;
|
||||
|
||||
if ~isempty(weight)
|
||||
projData_rec.object_c = projData_rec.object_c .* (weight~=0);
|
||||
projData_model.object_c = projData_model.object_c .* (weight~=0);
|
||||
end
|
||||
|
||||
|
||||
catch err
|
||||
err
|
||||
keyboard
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
%% get the complex transmission
|
||||
object = exp(projData_model.object_c);
|
||||
projData_model.object = object;
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% STORE PREPARED PROJECTION FOR ASYNCHRONOUS SOLVERS %%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
object = gather(object);
|
||||
|
||||
if (init_level && par.force_initialization)
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
% precache the already initalized p structures to make the solver faster %
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
p.ds = [];
|
||||
p.use_gpu = true;
|
||||
p.scan = [];
|
||||
p. scan_number = projData_rec.scan_id;
|
||||
p. rotation_angle = projData_rec.angle;
|
||||
p. proj_id = projData_rec.proj_id;
|
||||
|
||||
%% prepare data
|
||||
p. prepare_data_path = sprintf(par.prepare_data_path, p.scan_number);
|
||||
|
||||
p.object = [];
|
||||
p.probe = [];
|
||||
p.probes = [];
|
||||
p.positions_real = [];
|
||||
p.positions_orig = [];
|
||||
%% store data into some fast cache
|
||||
save('-v6', [p.prepare_data_path, '/prepared_data.mat'], 'p')
|
||||
end
|
||||
|
||||
|
||||
%% prepare the initial guess and store it
|
||||
|
||||
p = struct();
|
||||
p. proj_id = projData_rec.proj_id;
|
||||
p. scan_number = projData_rec.scan_id;
|
||||
p. rotation_angle = projData_rec.angle;
|
||||
p. prepare_data_path = sprintf(par.prepare_data_path, p.scan_number);
|
||||
p. force_preparation_data = false;
|
||||
p. remove_object_ambiguity= false;
|
||||
p. initial_probe_file = '';
|
||||
p. prefix = '';
|
||||
p. run_name = '' ;
|
||||
p. save_path = '';
|
||||
|
||||
% the input guess should be already optimally shifted
|
||||
p. position_offset = [0,0];
|
||||
|
||||
|
||||
if utils.verbose()
|
||||
engine0.use_display = 1;
|
||||
p. use_display = 0;
|
||||
p. verbose_level = 3;
|
||||
engine0.verbose_level = 3;
|
||||
else
|
||||
p. use_display = 0;
|
||||
p. verbose_level = -1;
|
||||
engine0.verbose_level = -1;
|
||||
end
|
||||
|
||||
% initialize the reconstruction, load prepared data
|
||||
engine0.delta_z = layer_distance;
|
||||
engine0.N_layer = size(object,3);
|
||||
|
||||
|
||||
if init_level == 1
|
||||
engine0.initial_probe_rescaling = true;
|
||||
else
|
||||
engine0.initial_probe_rescaling = false;
|
||||
end
|
||||
|
||||
|
||||
|
||||
%% load initial guesses
|
||||
p.probes = single(projData_rec.probe);
|
||||
p.positions = projData_rec.positions;
|
||||
|
||||
|
||||
|
||||
% ptychography expects the layers in opposite order
|
||||
object = object(:,:,end:-1:1);
|
||||
|
||||
p.object{1} = gather(reshape(object, [size(object,1), size(object,2),1,size(object,3)]));
|
||||
|
||||
p.object_size = [size(object,1), size(object,2)];
|
||||
|
||||
ferr = nan; % fourier error is initialized as nan
|
||||
|
||||
% prevent saving useless variables
|
||||
try; p = rmfield(p, 'simulation'); end
|
||||
try; p = rmfield(p, 'probe'); end
|
||||
|
||||
|
||||
if save_init_guess
|
||||
|
||||
if ~exist(p.prepare_data_path, 'dir')
|
||||
mkdir(p. prepare_data_path);
|
||||
end
|
||||
|
||||
|
||||
% upload to the shared memory
|
||||
shm_mem = shm(false, p.proj_id);
|
||||
shm_mem.upload(p.object{1});
|
||||
shm_mem.detach
|
||||
|
||||
% delete from p-struct , it is already in the shared memory
|
||||
p.object = [];
|
||||
% save only the empty p-structure
|
||||
save([p. prepare_data_path, '/prepared_initial_guess.mat'] , 'p', 'ferr', 'engine0', '-v6')
|
||||
else
|
||||
shm_mem = [];
|
||||
end
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
function projData_model = get_forward_model(projData_rec, volData, Nlayers)
|
||||
|
||||
|
||||
Npx_vol = size(volData);
|
||||
Nblock = ceil(prod(Npx_vol)*8 / 1e9); % split into 2GB blocks
|
||||
Npx_proj = size(projData_rec.object);
|
||||
|
||||
% block by block get the projection model
|
||||
projData_model = projData_rec; % create a copy of the input structure
|
||||
projData_model.object_c = gpuArray.zeros([Npx_vol([3,1]),Nlayers], 'single');
|
||||
for ii = 1:Nblock
|
||||
ind = 1+(ii-1)*ceil(Npx_vol(3)/Nblock):min((ii)*ceil(Npx_vol(3)/Nblock), Npx_vol(3));
|
||||
|
||||
volData_block = volData(:,:,ind);
|
||||
volData_block = utils.Garray(volData_block);
|
||||
|
||||
[object_r] = ptychotomo.fwd_proj(volData_block, projData_rec.angle, Nlayers, 'real');
|
||||
[object_i] = ptychotomo.fwd_proj(volData_block, projData_rec.angle, Nlayers, 'imag');
|
||||
|
||||
projData_model.object_c(ind,:,:) = complex(object_r, object_i);
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
% pad the geprojData_modelnerated projection to the size of the measured
|
||||
% projections
|
||||
projData_model.object_c = utils.crop_pad(projData_model.object_c,Npx_proj(1:2),0);
|
||||
|
||||
if any(any(round(projData_rec.position_offset) ~= projData_rec.position_offset))
|
||||
error('Noninterger shift may result in mixing real and imaginary data')
|
||||
end
|
||||
|
||||
|
||||
% apply shifts found by prealignement
|
||||
projData_model.object_c = utils.imshift_fast(projData_model.object_c, ...
|
||||
projData_rec.position_offset(1), ...
|
||||
projData_rec.position_offset(2));
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
function [projData_rec, offset] = remove_offset_initial(projData_model,projData_rec, weight, Npx_proj_small)
|
||||
import utils.*
|
||||
|
||||
W = crop_pad(weight, Npx_proj_small) ;
|
||||
diff = crop_pad(sum(projData_model.object_c,3), Npx_proj_small) - crop_pad(sum(projData_rec.object_c,3), Npx_proj_small);
|
||||
% empirical estimation of weights to subtract the phase / amplitude offset
|
||||
|
||||
offset = mean(mean(mean(W.*diff,2))) ./ mean(mean(W,2));
|
||||
|
||||
% subtract offset plane
|
||||
projData_rec.object_c = projData_rec.object_c + offset ;
|
||||
|
||||
verbose(0,'Offset removal %3.2e+%3.2ei ', real(offset), imag(offset));
|
||||
|
||||
end
|
||||
@@ -0,0 +1,148 @@
|
||||
%PREPARE_PROJECTIONS, use ptycho reconstruction object_0 and initial guess
|
||||
%object_guess_c to calculate updated linearized complex object_c, i.e. object_0 = exp(object_c)
|
||||
% -> projections needs to be unwrapped
|
||||
%
|
||||
% object_c = prepare_projections(object_0, Npx_proj, probe_size, merge_layers, object_guess_c, weight)
|
||||
%
|
||||
% Inputs:
|
||||
% **object_0 - stack of 2D reconstructed layers from ptychography
|
||||
% **Npx_proj - size of the tomography projections
|
||||
% **probe_size - p.asize value
|
||||
% **merge_layers - bool, if true, merge all layers to one before unwrapping
|
||||
% **object_guess_c - stack of 2D layers used as initial guess for from ptychography
|
||||
% **weight - array denoting reliability for different regions of object_0 array
|
||||
%
|
||||
% *returns*
|
||||
% ++object_c - linearized complex projection, ie object_0 = exp(object_c)
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function object_c = prepare_projections(object_0, Npx_proj, probe_size, merge_layers, object_guess_c, weight)
|
||||
% get updated unwrapped object_c given the new object estiamte and the original unwrapped
|
||||
% object before the ptycho iteration
|
||||
|
||||
if nargin < 4
|
||||
merge_layers = false;
|
||||
end
|
||||
|
||||
%% smoothly merge with the last reconstruction at this angle
|
||||
object_0 = utils.crop_pad(object_0,Npx_proj(1:2));
|
||||
|
||||
if nargin > 4 && ~isempty(object_guess_c)
|
||||
% get only difference from the previous and then use this for
|
||||
% unwrapping
|
||||
object_guess = exp(object_guess_c);
|
||||
object = (object_0 .* conj(object_guess)) ./ (1e-4+abs(object_guess).^2);
|
||||
clear object_guess
|
||||
else
|
||||
object = object_0;
|
||||
end
|
||||
|
||||
|
||||
%% multilayer expantion
|
||||
|
||||
% find reliability region
|
||||
if merge_layers
|
||||
object = prod(object,3);
|
||||
end
|
||||
|
||||
win = tukeywin(Npx_proj(2), probe_size(2)/Npx_proj(2)/2)'.*tukeywin(Npx_proj(1), probe_size(1)/Npx_proj(1)/2);
|
||||
|
||||
%% empirical way to estimate the unwrapping region
|
||||
weight = utils.Garray(weight .* win);
|
||||
if ~isempty(object_guess_c)
|
||||
Niter_unwrap = 3; % assume that initial guess is good enough
|
||||
else
|
||||
Niter_unwrap = 10; % try more iterations in the first step
|
||||
end
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
unwrapped_phase = tomo.unwrap2D_fft2_split(object ,[],0, weight, [], [], [], Niter_unwrap);
|
||||
attenuation = log(max(1e-1,abs(object_0)));
|
||||
|
||||
if ~isempty(object_guess_c)
|
||||
% make the fit exact, maybe not needed , useful when the update changes are small
|
||||
unwrapped_phase = unwrapped_phase + angle(object .* exp(-1i*unwrapped_phase)) .* weight;
|
||||
|
||||
|
||||
% limit maximal absorbtion to 90% and tranmission to 100%
|
||||
object_c = complex(attenuation, imag(object_guess_c)+unwrapped_phase);
|
||||
else
|
||||
|
||||
object_c = complex(attenuation , unwrapped_phase);
|
||||
end
|
||||
|
||||
% zero the empty regions
|
||||
object_c = object_c .* (object_0 ~= 0);
|
||||
|
||||
|
||||
if merge_layers
|
||||
object_c = repmat(object_c,1,1,Npx_proj(3))/Npx_proj(3);
|
||||
end
|
||||
|
||||
end
|
||||
@@ -0,0 +1,106 @@
|
||||
%GET_GOLDEN_RATIO_GROUPS use the provided angle to select Ngroups groups containing
|
||||
%the most orthogonal set of projections
|
||||
%
|
||||
% group_ind = get_golden_ratio_groups(theta_all, Ngroups)
|
||||
%
|
||||
% Inputs:
|
||||
% **theta_all - list of angles in degrees
|
||||
% **Ngroups - number of sets to split the angles
|
||||
% *returns*
|
||||
% ++group_ind - cells containing indices for each set
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function group_ind = get_golden_ratio_groups(theta_all, Ngroups)
|
||||
Nangles = length(theta_all);
|
||||
|
||||
if all(theta_all == theta_all(1))
|
||||
group_ind = {};
|
||||
for ll = 1:Ngroups
|
||||
ind = 1+(ll-1)*ceil(Nangles/Ngroups):min(Nangles, (ll)*ceil(Nangles/Ngroups));
|
||||
group_ind{end+1} = (ind);
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
theta_aux = theta_all;
|
||||
Nangles = length(theta_all);
|
||||
start_index = 200;
|
||||
|
||||
for ll = 1:Nangles
|
||||
% golden ratio angle selector
|
||||
gold_angle = mod((ll + start_index) * (1+sqrt(5))/2* 360, 360);
|
||||
theta = theta_aux(math.argmin(abs(theta_aux-gold_angle)));
|
||||
theta_aux = setdiff(theta_aux, theta);
|
||||
index(ll) = find(theta_all== theta);
|
||||
end
|
||||
|
||||
group_ind = {};
|
||||
for ll = 1:Ngroups
|
||||
ind = 1+(ll-1)*ceil(Nangles/Ngroups):min(Nangles, (ll)*ceil(Nangles/Ngroups));
|
||||
group_ind{end+1} = index(ind);
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,203 @@
|
||||
/*
|
||||
SET_TO_BLOCK_GPU Set complex views to a large complex object without memory copy of the large volume
|
||||
|
||||
set_to_block_gpu(volData, volData_block, uint16(ind));
|
||||
|
||||
Inputs:
|
||||
**volData - 3D array, single precision
|
||||
**volData_block - 3D array, that will be written into the volData
|
||||
**ind - 1D array, uint16, indices into which lines of volData will be volData_block written
|
||||
Outputs:
|
||||
none, results are written to volData directly
|
||||
|
||||
Recompile:
|
||||
mexcuda -output +engines/+GPU/+shared/private/set_views_gpu_mex +engines/+GPU/+shared/private/set_views_gpu_mex.cu
|
||||
*/
|
||||
|
||||
#include "mex.h"
|
||||
#include "gpu/mxGPUArray.h"
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
typedef const unsigned int cuint;
|
||||
typedef const uint16_T cuint16;
|
||||
|
||||
// unfortunatelly ~10800 is the maximum of const memory
|
||||
const unsigned int MAX_IND_READ = 10800;
|
||||
static const unsigned MAX_IND_READ_DEV = MAX_IND_READ;
|
||||
__constant__ uint16_T gC_ind[MAX_IND_READ_DEV];
|
||||
|
||||
|
||||
int checkLastError(char * msg)
|
||||
{
|
||||
cudaError_t cudaStatus = cudaGetLastError();
|
||||
if (cudaStatus != cudaSuccess) {
|
||||
char err[512];
|
||||
sprintf(err, "setprojection failed \n %s: %s. \n", msg, cudaGetErrorString(cudaStatus));
|
||||
mexPrintf(err);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Device code
|
||||
*/
|
||||
|
||||
|
||||
__global__ void addToArray_c( float2 const * sarray, float2 * larray, cuint Nx,cuint Ny, cuint Npos) {
|
||||
// Location in a 3D matrix
|
||||
int idx= blockIdx.x * blockDim.x + threadIdx.x;
|
||||
int idy= blockIdx.y * blockDim.y + threadIdx.y;
|
||||
if ( idx < Nx & idy < Ny ) {
|
||||
int idz, id_large, id_small;
|
||||
for(idz = 0; idz < Npos; idz++)
|
||||
{
|
||||
id_large = idx + Nx*idy + Nx*Ny*(gC_ind[idz]-1); // go only through some of the indices
|
||||
id_small = idx + Nx*idy + Nx*Ny*idz;
|
||||
|
||||
|
||||
larray[id_large].x = sarray[ id_small ].x;
|
||||
larray[id_large].y = sarray[ id_small ].y;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void mexFunction(int nlhs, mxArray *plhs[],
|
||||
int nrhs, const mxArray *prhs[])
|
||||
{
|
||||
|
||||
|
||||
int i ;
|
||||
char const * const errId = "parallel:gpu:mexGPUExample:InvalidInput";
|
||||
char const * const errMsg = "Invalid input to MEX file.";
|
||||
|
||||
// Check for proper number of arguments.
|
||||
if (nrhs != 3)
|
||||
mexErrMsgTxt("Three input arguments required");
|
||||
|
||||
|
||||
// Input must be of type single.
|
||||
for (i=0; i < 2; i++) {
|
||||
if ( !mxIsGPUArray(prhs[i]) ){
|
||||
printf("Input %d is not cell array\n",i+1);
|
||||
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
|
||||
}
|
||||
}
|
||||
// Input must be of type int16.
|
||||
for (i=2; i<3; i++){
|
||||
if (mxIsUint16(prhs[i]) != 1){
|
||||
printf("Input %d is not integer\n",i+1);
|
||||
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type uint16.");
|
||||
}
|
||||
}
|
||||
|
||||
// It cannot be one-dimensional
|
||||
if(mxGetNumberOfDimensions(prhs[0]) > 3) {
|
||||
printf("The 1st input argument must have at least three dimensions.");
|
||||
mexErrMsgIdAndTxt("MexError:ptycho","wrong number of dimensions");
|
||||
}
|
||||
// It cannot be more than 3-dimensional
|
||||
if(mxGetNumberOfDimensions(prhs[0]) > 3) {
|
||||
printf("The 1st input argument must have at most three dimensions.");
|
||||
mexErrMsgIdAndTxt("MexError:ptycho","wrong number of dimensions");
|
||||
}
|
||||
|
||||
// Check that arrays are complex
|
||||
|
||||
|
||||
const mxGPUArray * m_sarray = mxGPUCreateFromMxArray(prhs[1]);
|
||||
if ((mxGPUGetClassID(m_sarray) != mxSINGLE_CLASS)) {
|
||||
mexPrintf("m_sarray\n");
|
||||
mexErrMsgIdAndTxt(errId, errMsg);
|
||||
}
|
||||
|
||||
// write results directly to the output array
|
||||
mxGPUArray * m_larray = const_cast<mxGPUArray*>(mxGPUCreateFromMxArray(prhs[0])); // mxGPUCopyFromMxArray(prhs[1]);
|
||||
if ((mxGPUGetClassID(m_larray) != mxSINGLE_CLASS)) {
|
||||
mexPrintf("m_larray\n");
|
||||
mexErrMsgIdAndTxt(errId, errMsg);
|
||||
}
|
||||
|
||||
if (mxGPUGetComplexity(m_larray) != mxCOMPLEX) {
|
||||
mexPrintf("m_larray complexity has to be the COMPLEX \n");
|
||||
mexErrMsgIdAndTxt(errId, errMsg);
|
||||
}
|
||||
|
||||
|
||||
if (mxGPUGetComplexity(m_larray) != mxGPUGetComplexity(m_sarray)) {
|
||||
mexPrintf("m_larray and m_sarray complexity has to be the same \n");
|
||||
mexErrMsgIdAndTxt(errId, errMsg);
|
||||
}
|
||||
|
||||
|
||||
|
||||
const mxGPUArray * m_ind = mxGPUCreateFromMxArray(prhs[2]);
|
||||
if ((mxGPUGetClassID(m_ind) != mxUINT16_CLASS)) {
|
||||
mexPrintf("m_ind\n");
|
||||
mexErrMsgIdAndTxt(errId, errMsg);
|
||||
}
|
||||
const uint16_T * p_ind = (uint16_T *)mxGPUGetDataReadOnly(m_ind);
|
||||
|
||||
|
||||
|
||||
// Get dimension of probe and object
|
||||
const unsigned int Ndims = (unsigned int)mxGPUGetNumberOfDimensions(m_sarray);
|
||||
const mwSize * Np_l = mxGPUGetDimensions(m_larray);
|
||||
const mwSize * Np_s = mxGPUGetDimensions(m_sarray);
|
||||
const unsigned int Npos = mxGPUGetNumberOfElements(m_ind);
|
||||
|
||||
|
||||
|
||||
|
||||
if (Ndims == 3 && Npos > Np_s[2]) {
|
||||
printf("wrong size of update / positions %i", Ndims);
|
||||
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
|
||||
}
|
||||
|
||||
|
||||
cudaMemcpyToSymbol(gC_ind, p_ind, Npos*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
|
||||
checkLastError("after cudaMemcpyToSymbol pos");
|
||||
|
||||
|
||||
// Choose a reasonably sized number of threads in each dimension for the block.
|
||||
int const threadsPerBlockEachDim = 32;
|
||||
// Compute the thread block and grid sizes based on the board dimensions.
|
||||
int const blocksPerGrid_M = (Np_s[0] + threadsPerBlockEachDim - 1) / threadsPerBlockEachDim;
|
||||
int const blocksPerGrid_N = (Np_s[1] + threadsPerBlockEachDim - 1) / threadsPerBlockEachDim;
|
||||
int const blocksPerGrid_O = 1;
|
||||
|
||||
dim3 const dimBlock(blocksPerGrid_M, blocksPerGrid_N, blocksPerGrid_O);
|
||||
dim3 const dimThread(threadsPerBlockEachDim, threadsPerBlockEachDim, 1);
|
||||
|
||||
checkLastError("after dimThread");
|
||||
|
||||
checkLastError("after cudaMemcpyToSymbol");
|
||||
|
||||
// ================== call the right kernel ===================
|
||||
|
||||
const float2 * p_sarray = (float2 *)mxGPUGetDataReadOnly(m_sarray);
|
||||
float2 * p_larray = (float2 *)mxGPUGetData(m_larray);
|
||||
addToArray_c<<<dimBlock, dimThread>>>(p_sarray,p_larray, Np_l[0],Np_l[1], Npos);
|
||||
|
||||
|
||||
checkLastError("after kernel");
|
||||
|
||||
mxGPUDestroyGPUArray(m_larray);
|
||||
mxGPUDestroyGPUArray(m_ind);
|
||||
|
||||
cudaThreadSynchronize();
|
||||
|
||||
// plhs[0] = mxGPUCreateMxArrayOnGPU(m_larray);
|
||||
mxGPUDestroyGPUArray(m_sarray);
|
||||
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,196 @@
|
||||
%PTYCHO_SOLVER_DISTRIBUTED solve asynchronously ptychography, from
|
||||
%"prepare_data_path" load parameters of the current reconstruction, data
|
||||
%and from shared memory load the corresponding initial guess of the object
|
||||
% Finally, run ptychography and return results
|
||||
%
|
||||
% [pout, ferr] = ptycho_solver_distributed(GPU_id, prepare_data_path)
|
||||
%
|
||||
% Inputs:
|
||||
% **GPU_id - id of the used GPU
|
||||
% **prepare_data_path - path to the stored initialization files
|
||||
%
|
||||
% *returns*
|
||||
% ++pout - output from ptychography
|
||||
% ++ferr - fourier error reported by ptychography
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function [pout, ferr] = ptycho_solver_distributed(GPU_id, prepare_data_path)
|
||||
|
||||
% fast loading to save time
|
||||
%p = core.ptycho_recons(p, true);
|
||||
% IT IS 2X FASTER TO LOAD IT FROM CACHE
|
||||
load([prepare_data_path, '/prepared_data.mat'], 'p')
|
||||
p.use_display = 0;
|
||||
p.plot.interval = inf;
|
||||
|
||||
[p.fmag, p.fmask, p.positions] = io.load_prepared_data(fullfile( prepare_data_path, sprintf('S%05i_data_%ix%i.h5', p.scan_number, p.asize(1), p.asize(2)) ));
|
||||
|
||||
|
||||
%% load initial guess + specific settings for this angles
|
||||
init = load([prepare_data_path, '/prepared_initial_guess.mat']);
|
||||
% get from the shared memory
|
||||
shm_mem = shm(true, init.p.proj_id);
|
||||
[shm_mem, shm_data] = shm_mem.attach();
|
||||
init.p.object{1} = shm_data+eps;% force matlab to allocate new memory
|
||||
|
||||
|
||||
% apply setttings from initial guess
|
||||
for item = fieldnames(init.p)'
|
||||
if ~isempty(init.p.(item{1}))
|
||||
p.(item{1}) = init.p.(item{1});
|
||||
end
|
||||
end
|
||||
for item = fieldnames(init.engine0)'
|
||||
p.engines{1}.(item{1}) = init.engine0.(item{1});
|
||||
end
|
||||
|
||||
|
||||
p.gpu_id = GPU_id;
|
||||
|
||||
|
||||
|
||||
if isfield(p, 'gpu_id')
|
||||
gpu = gpuDevice();
|
||||
if p.gpu_id ~= gpu.Index
|
||||
gpuDevice(double(p.gpu_id));
|
||||
end
|
||||
end
|
||||
|
||||
if strcmpi(p.engines{1}.name , 'GPU')
|
||||
p.object{1} = gpuArray(p.object{1});
|
||||
p.probes = gpuArray(p.probes);
|
||||
end
|
||||
|
||||
|
||||
%% run all engines
|
||||
utils.verbose(struct('prefix', {'ptycho'}))
|
||||
for ieng=1:length(p.engines)
|
||||
p.ieng = ieng;
|
||||
|
||||
% engine call
|
||||
utils.verbose(1, 'Calling engine %s', p.engines{ieng}.name)
|
||||
utils.verbose(struct('prefix', {p.engines{ieng}.name}))
|
||||
|
||||
%% run engine
|
||||
|
||||
try
|
||||
[pout, fdb] = core.run_engine(p,ieng);
|
||||
catch err
|
||||
disp(err)
|
||||
keyboard
|
||||
end
|
||||
|
||||
utils.verbose(struct('prefix', {'ptycho'}))
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
try
|
||||
ferr = pout.error_metric.value([1,end]);
|
||||
catch
|
||||
ferr = nan(2,1);
|
||||
end
|
||||
|
||||
% delete input data, save time in storing / loading
|
||||
pout.fmag = [];
|
||||
pout.fmask = [];
|
||||
pout.probe = [];
|
||||
pout.simulation = [];
|
||||
|
||||
status = true;
|
||||
for ii = 1:5
|
||||
try
|
||||
shm_mem.free;
|
||||
shm_mem.upload(pout.object{1})
|
||||
status = false;
|
||||
break
|
||||
catch err
|
||||
warning(err.message)
|
||||
end
|
||||
pause(0.1)
|
||||
end
|
||||
|
||||
if status
|
||||
warning('Uploading results to shared mem failed')
|
||||
keyboard
|
||||
end
|
||||
|
||||
% upload to shared memory
|
||||
% utils.add_to_3D_projection(shm_mem.data_handle{1}, real(pout.object{1}), [0,0], 1, false);
|
||||
% utils.add_to_3D_projection(shm_mem.data_handle{2}, imag(pout.object{1}), [0,0], 1, false);
|
||||
|
||||
|
||||
|
||||
% delete the larger arrays from the p-structure
|
||||
% pout.probes = [];
|
||||
pout.object = [];
|
||||
|
||||
% rewrite the structure with the initial guess and store there results
|
||||
save([pout. prepare_data_path, '/output_reconstruction.mat'] ,'ferr', 'pout', '-v6')
|
||||
utils.verbose(-1,'Save data %s', [pout. prepare_data_path, '/output_reconstruction.mat'])
|
||||
|
||||
end
|
||||
@@ -0,0 +1,649 @@
|
||||
% TOMO_SOLVER_DISTRIBUTED tomography solver that loads reconstruction from
|
||||
% ptychography, process them and use them to update the current tomgoraphy
|
||||
% reconstruction. Then create new estimates of ptychography objects
|
||||
%
|
||||
% volData = tomo_solver_distributed(par, volData, projData_rec, p0, theta_all, scan_ids)
|
||||
%
|
||||
% Inputs:
|
||||
% **par - parameter structure
|
||||
% **volData - initial guess of the tomographic volumes, values are linearized, ie projection = exp(sum(volData,1)) = prod(exp(volData),1)
|
||||
% **projData_rec - initial guess of the projections for each angle, probe, positions, etc
|
||||
% **p0 - basic parameters for the ptychography solver
|
||||
% **theta_all - all angles in degrees
|
||||
% **scan_ids - scan numbers that correspons to each angle value
|
||||
% *returns*
|
||||
% ++volData - final reconstruction of the tomographic volume
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
|
||||
function volData = tomo_solver_distributed(par, volData, projData_rec, p0, theta_all, scan_ids)
|
||||
import utils.*
|
||||
|
||||
% be sure to clean GPU first
|
||||
reset(gpuDevice)
|
||||
|
||||
p0.queue.path = par.queue_path;
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%%%% create the queue %%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
system(['rm -rf ', par.queue_path]);
|
||||
% if exist(par.queue_path, 'dir')
|
||||
% rmdir(par.queue_path, 's');
|
||||
% end
|
||||
Nangles = length(theta_all);
|
||||
|
||||
try; mkdir([par.queue_path, '/pending/']); end
|
||||
try; mkdir([par.queue_path, '/done/']); end
|
||||
|
||||
system('rm temp/*/prepared_data.mat ');
|
||||
|
||||
|
||||
% minimum p structure needed to create the queue
|
||||
p_init.prepare_data_path = par.prepare_data_path;
|
||||
|
||||
% create file queue
|
||||
for ii = 1:Nangles
|
||||
utils.progressbar(ii,length(scan_ids));
|
||||
p = p_init;
|
||||
p.scan_number = scan_ids(ii);
|
||||
save('-v6',fullfile(par.queue_path, sprintf('pending/scan%05d.mat',scan_ids(ii))), 'p');
|
||||
end
|
||||
|
||||
|
||||
initial_settings = 'p_initial.mat';
|
||||
|
||||
save(initial_settings, 'p0');
|
||||
|
||||
|
||||
|
||||
Npx_vol = size(volData);
|
||||
assert(Npx_vol(1)==Npx_vol(2), 'Horizontal volume size has to be symmetric')
|
||||
assert(mod(Npx_vol(1),64) == 0, 'Input volum esize should be dividable by 64')
|
||||
thickness = p0.thickness; % Total thickness of the simulated sample if multiple layers are provided;
|
||||
|
||||
Nangles = length(projData_rec);
|
||||
|
||||
% choosed optimally distributed the angles in the 180 deg range to minimize overlap
|
||||
angle_step = median(diff(theta_all));
|
||||
init_angle = math.argmin(abs(theta_all - 180-angle_step*par.downsample_angles/2));
|
||||
angle_indices = [1:par.downsample_angles:init_angle-1, init_angle:par.downsample_angles:Nangles];
|
||||
volData_hist = {[],[]};
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
% initialize one dataset
|
||||
[p0] = core.initialize_ptycho(p0);
|
||||
p0.prepare_data_path = par.prepare_data_path;
|
||||
|
||||
|
||||
gpu = gpuDevice();
|
||||
|
||||
|
||||
|
||||
%% create some loost support mask r
|
||||
if par.apply_support
|
||||
m_volData = abs(mean(volData,3));
|
||||
support_mask = utils.imgaussfilt2_fft(m_volData, 20) > mean(m_volData(:))/2;
|
||||
support_mask = utils.imgaussfilt3_conv(support_mask, [10,10,0]);
|
||||
volData = volData .* support_mask;
|
||||
else
|
||||
support_mask = 1;
|
||||
end
|
||||
%% move arrays to GPU
|
||||
par.support_mask = Garray(support_mask);
|
||||
par.norm_full = norm(volData(:));
|
||||
|
||||
% move to uint8 - the volume has to be on GPU !! moving from and to GPU
|
||||
% causes too much overhead
|
||||
volData = ptychotomo.compress_volume(volData, par);
|
||||
volData = Garray(volData);
|
||||
|
||||
% prepate support masks for the projections using the "support_mask" calculated from the
|
||||
% reconstructed tomography volume
|
||||
par.weight = sum(support_mask);
|
||||
par.weight = utils.crop_pad(par.weight, [1,p0.object_size(2)]);
|
||||
par.weight = 1-par.weight / max(par.weight);
|
||||
par.support_mask = support_mask;
|
||||
|
||||
% store all volumes per iteration
|
||||
volData_all = cell(par.Niter,1);
|
||||
|
||||
|
||||
|
||||
disp('Reconstructing')
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% iterativelly solve ptycho tomo task %%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
update_norm = nan(par.Niter, Nangles, par.Niter_inner);
|
||||
fourier_error = nan(par.Niter, Nangles,2);
|
||||
update_norm_acc = nan(par.Niter, 1);
|
||||
hist_numbers = zeros(Nangles,1);
|
||||
ptycho_results = cell(Nangles,1); % store temporally data used for reconstruction , avoid data storing
|
||||
|
||||
|
||||
Npx_proj = [p0.object_size, 1];
|
||||
|
||||
proj_number_list = []; % list of projections yet in procesing
|
||||
|
||||
|
||||
t_plot = tic;
|
||||
for iter = 1:par.Niter
|
||||
if iter >= max(par.ptycho_reconstruct_start, par.ptycho_ML_reconstruct_start)
|
||||
Nlayers = min(2^ceil((iter+1 - par.ptycho_ML_reconstruct_start)), par.Nlayers_max );
|
||||
else
|
||||
Nlayers = 1;
|
||||
end
|
||||
|
||||
Npx_proj(3) = Nlayers;
|
||||
|
||||
|
||||
full_reconstruction = mod(iter,par.ptycho_interval) == 0 && iter >= par.ptycho_reconstruct_start;
|
||||
|
||||
fprintf('Free GPU mem: %3.4gGB \n', gpu.AvailableMemory / 1e9)
|
||||
|
||||
fprintf('Iteration %i/%i Nlayers %i full_reconstruction %i \n', iter, par.Niter, Nlayers, full_reconstruction)
|
||||
layer_distance = thickness / Nlayers * ones(1,Nlayers-1) ;
|
||||
|
||||
Ngroups = length(angle_indices);
|
||||
|
||||
group_ind = get_golden_ratio_groups(theta_all(angle_indices), Ngroups);
|
||||
|
||||
|
||||
%%%%% start accelerated gradients %%%%%%%%%%%%%%
|
||||
if iter == par.ptycho_accel_start
|
||||
volData_hist{1} = ptychotomo.decompress_volume(gather(volData), par);
|
||||
volData_hist{2} = volData_hist{1};
|
||||
elseif iter > par.ptycho_accel_start
|
||||
beta = (iter-par.ptycho_accel_start+1)/(iter-par.ptycho_accel_start+3);
|
||||
|
||||
volData_hist{1} = volData_hist{2};
|
||||
volData_hist{2} = ptychotomo.decompress_volume(gather(volData), par);
|
||||
|
||||
upd = beta*(volData_hist{1}- volData_hist{2});
|
||||
update_norm_acc(iter) = norm(upd(:));
|
||||
|
||||
if ~isfinite(update_norm_acc(iter))
|
||||
keyboard
|
||||
end
|
||||
if all(isfinite(update_norm_acc([iter, iter-1]))) && (update_norm_acc(iter) > update_norm_acc(iter-1))
|
||||
% reset the accelerated gradients
|
||||
warning('Reset accelerated gradients')
|
||||
volData_hist{1} = ptychotomo.decompress_volume(gather(volData), par);
|
||||
volData_hist{2} = volData_hist{1};
|
||||
par.ptycho_accel_start = iter;
|
||||
else
|
||||
% else move in the accelerated direction
|
||||
volData = volData_hist{2} + upd;
|
||||
end
|
||||
|
||||
volData = ptychotomo.compress_volume(volData, par);
|
||||
volData = utils.Garray(volData); % move it back to GPU after gathering for acceleratin computation
|
||||
clear upd
|
||||
end
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
verbose(struct('prefix', {'ptychotomo'}))
|
||||
|
||||
for outer_loop = 1:par.Niter_inner
|
||||
fprintf('Outer loop %i \n', outer_loop)
|
||||
|
||||
tic
|
||||
|
||||
group_id = 1;
|
||||
while group_id <= Ngroups || ~isempty(proj_number_list)
|
||||
if verbose() < 0
|
||||
utils.progressbar(group_id, Ngroups)
|
||||
end
|
||||
clear volData_upd_full
|
||||
|
||||
|
||||
verbose(0,'PREPARING')
|
||||
try
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%%%%%%% prepare new initial guess if needed %%%%%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
proj_number_list_update = [];
|
||||
while length(proj_number_list) < par.max_queue_length && group_id <= Ngroups
|
||||
|
||||
ind = angle_indices(group_ind{group_id});
|
||||
|
||||
% calculate how the projection should look like and store the initial guess to "initial_guess.mat"
|
||||
for ll = 1:length(ind)
|
||||
proj_number = ind(ll);
|
||||
scan_number = scan_ids(proj_number);
|
||||
|
||||
if ~isempty(dir(sprintf('%s/pending/scan%05i.mat', par.queue_path,scan_number)))
|
||||
verbose(0, 'Preparing %i', scan_number)
|
||||
|
||||
verbose(0);
|
||||
[projData_model{proj_number}, projData_rec{proj_number}, ptycho_results{proj_number}] = ...
|
||||
ptychotomo.prepare_distributed_data(p0, volData, projData_rec{proj_number},layer_distance, par, ...
|
||||
iter == par.ptycho_reconstruct_start, iter >= par.ptycho_reconstruct_start );
|
||||
|
||||
if iter >= par.ptycho_reconstruct_start
|
||||
% file is ready for processing, move it to queue
|
||||
io.movefile_fast(sprintf('%s/pending/scan%05i.mat', par.queue_path,scan_number), sprintf('%s/scan%05i.mat', par.queue_path,scan_number));
|
||||
else
|
||||
% skip ptychography reconstrution, pretent that the file is already finished
|
||||
io.movefile_fast(sprintf('%s/pending/scan%05i.mat', par.queue_path,scan_number), sprintf('%s/done/scan%05i.mat', par.queue_path,scan_number));
|
||||
end
|
||||
hist_numbers(proj_number) = hist_numbers(proj_number) +1;
|
||||
proj_number_list_update(end+1) = proj_number;
|
||||
proj_number_list(end+1) = proj_number;
|
||||
continue
|
||||
else
|
||||
verbose(0, 'Waiting for scan %i to be returned to /pending/', scan_number)
|
||||
end
|
||||
end
|
||||
group_id = group_id + 1;
|
||||
|
||||
end
|
||||
catch err
|
||||
keyboard
|
||||
end
|
||||
|
||||
|
||||
clear scan_number proj_number
|
||||
|
||||
%%%%%%% run the reconstruction using the generated queue files %%%
|
||||
|
||||
|
||||
if iter < par.ptycho_reconstruct_start
|
||||
verbose(0,'TOMO RECONSTRUCTING')
|
||||
for proj_number = proj_number_list_update
|
||||
% save previous object estimation and pretend that they were just calculated
|
||||
proj = projData_rec{proj_number};
|
||||
pout.object{1} = exp(proj.object_c);
|
||||
pout.probes = proj.probe;
|
||||
pout.positions = proj.positions;
|
||||
pout.asize = p0.asize;
|
||||
ferr = nan;
|
||||
ptycho_results{proj_number} = pout;
|
||||
end
|
||||
prepare_data_path = sprintf(p0.prepare_data_path, scan_ids(proj_number));
|
||||
else
|
||||
verbose(0,'PTYCHOTOMO RECONSTRUCTING')
|
||||
end
|
||||
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
|
||||
verbose(struct('prefix', {'ptychotomo'}))
|
||||
verbose(0,'GATHERING')
|
||||
|
||||
%%%%%%%%%%%%%% apply the recent reconstructions into the 3D volume %%%%%%
|
||||
% load the stored reconstructions and return corresponding object update
|
||||
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%%% start gathering reconstructions %%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
volData_upd_full = 0;
|
||||
proj_number = proj_number_list(1);
|
||||
scan_number = scan_ids(proj_number);
|
||||
proj_number_list(1) = [];
|
||||
scan_file_path = sprintf('%s/done/scan%05d.mat', par.queue_path, scan_number);
|
||||
|
||||
|
||||
timeout_gathering = 0.1; % [s]
|
||||
ii = 0;
|
||||
while true
|
||||
verbose(0)
|
||||
if ii * timeout_gathering > par.wait_time_solver
|
||||
% skip projetions that were not delivered in more than
|
||||
% "wait_time_solver" time
|
||||
warning('Failed to gather %s', scan_file_path)
|
||||
break
|
||||
end
|
||||
if exist(scan_file_path, 'file')
|
||||
break
|
||||
else
|
||||
% wait for solver to prepare the reconstruction
|
||||
pause(timeout_gathering)
|
||||
if ii == 0; verbose(0,'Waiting for reconstruction %i', scan_number); end
|
||||
ii = ii + 1;
|
||||
continue
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
if ii * timeout_gathering > par.wait_time_solver
|
||||
continue
|
||||
end
|
||||
|
||||
p0.scan_number = scan_number;
|
||||
|
||||
|
||||
verbose(0)
|
||||
verbose(0, 'Gathering %i', p0.scan_number)
|
||||
|
||||
if iter >= par.start_3D_reconstruction
|
||||
par.update_step = par.lambda/length(ind)/outer_loop;
|
||||
par.update_step = complex(par.update_step / 5, par.update_step); % much slower convergence for amplitude
|
||||
else
|
||||
par.update_step = 0;
|
||||
end
|
||||
|
||||
try
|
||||
if ii * timeout_gathering < par.wait_time_solver
|
||||
% calculate and accumulate update of the 3D volume
|
||||
[volData,projData_rec{proj_number}, fourier_error(iter,proj_number,:), update_norm(iter,proj_number,outer_loop)] = ...
|
||||
ptychotomo.gather_distributed_reconstructions(volData, projData_model{proj_number}, ptycho_results{proj_number},par);
|
||||
end
|
||||
catch err
|
||||
warning('gather_distributed_reconstructions failed: %s', err.message)
|
||||
end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
% file is already used, move it back to pending ...
|
||||
io.movefile_fast(scan_file_path, sprintf('%s/pending/scan%05i.mat', par.queue_path,p0.scan_number));
|
||||
|
||||
|
||||
% release shared memory
|
||||
if isa(ptycho_results{proj_number}, 'shm')
|
||||
ptycho_results{proj_number}.protected = false; % make it possible to delete the SHM
|
||||
ptycho_results{proj_number}.detach;
|
||||
ptycho_results{proj_number}.free() ; % free shared memory
|
||||
ptycho_results{proj_number} = [];
|
||||
end
|
||||
|
||||
% save memory
|
||||
projData_model{proj_number}.object = gather(projData_model{proj_number}.object);
|
||||
projData_model{proj_number}.object_c = gather(projData_model{proj_number}.object_c);
|
||||
ptycho_results{proj_number} = [];
|
||||
|
||||
|
||||
if math.norm2(projData_rec{proj_number}.probe) > 2.1
|
||||
keyboard
|
||||
end
|
||||
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
%% %%%%%%%%%%% PLOT PROGRESS %%%%%%%%%%%%
|
||||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
||||
|
||||
if par.debug || toc(t_plot) > par.plot_every
|
||||
plotting.smart_figure(24)
|
||||
subplot(2,2,1)
|
||||
plotting.imagesc3D(-imag( ptychotomo.decompress_volume(volData(:,:,ceil(end/2)),par)));
|
||||
caxis(gather(math.sp_quantile(-imag(volData(:,:,100)), [5e-3, 1-5e-3], 10)))
|
||||
axis off image
|
||||
colormap bone
|
||||
title('Current reconstruction')
|
||||
subplot(2,2,2)
|
||||
ind_ok = find(any(~isnan(fourier_error(:,:,1)),2));
|
||||
plot(ind_ok,fourier_error(ind_ok,:,1), '-')
|
||||
hold all
|
||||
plot(ind_ok,fourier_error(ind_ok,:,2), '--')
|
||||
plot(ind_ok,nanmean(fourier_error(ind_ok,:,1),2), 'k-', 'Linewidth', 2)
|
||||
plot(ind_ok,nanmean(fourier_error(ind_ok,:,2),2), 'k--', 'Linewidth', 2)
|
||||
hold off
|
||||
set(gca, 'xscale', 'log')
|
||||
set(gca, 'yscale', 'log')
|
||||
grid on
|
||||
axis tight
|
||||
title('Fourier error (from ptycho)')
|
||||
subplot(2,2,3)
|
||||
plot(update_norm(:,:,1))
|
||||
hold all
|
||||
plot(update_norm_acc, 'k--', 'Linewidth', 2)
|
||||
plot(nanmean(update_norm(:,:,1),2), 'k', 'Linewidth', 2)
|
||||
plot(nanmean(update_norm(:,:,end),2), 'k:', 'Linewidth', 2)
|
||||
hold off
|
||||
set(gca, 'xscale', 'log')
|
||||
set(gca, 'yscale', 'log')
|
||||
grid on
|
||||
title('Update difference (in tomo)')
|
||||
axis tight
|
||||
subplot(2,2,4)
|
||||
plotting.imagesc3D(exp(crop_pad(sum(projData_rec{proj_number}.object_c,3), [Npx_vol(3), Npx_vol(1)])))
|
||||
axis xy off image
|
||||
title('Example of a projection')
|
||||
%drawnow
|
||||
|
||||
plotting.smart_figure(15222)
|
||||
subplot(2,2,1)
|
||||
plotting.imagesc3D(abs(exp(sum(projData_rec{proj_number}.object_c,3))))
|
||||
colorbar
|
||||
colormap bone
|
||||
axis xy off image
|
||||
caxis([0.8, 1.1])
|
||||
title('Ptycho - abs')
|
||||
subplot(2,2,2)
|
||||
plotting.imagesc3D(angle(exp(sum(projData_rec{proj_number}.object_c,3))))
|
||||
colorbar
|
||||
colormap bone
|
||||
axis xy off image
|
||||
title('Model - phase')
|
||||
|
||||
subplot(2,2,3)
|
||||
plotting.imagesc3D(abs(exp(sum(projData_model{proj_number}.object_c,3))))
|
||||
colorbar
|
||||
colormap bone
|
||||
axis xy off image
|
||||
title('Model - abs')
|
||||
caxis([0.8, 1.1])
|
||||
subplot(2,2,4)
|
||||
plotting.imagesc3D(angle(exp(sum(projData_model{proj_number}.object_c,3))))
|
||||
colorbar
|
||||
colormap bone
|
||||
title('Model - phase')
|
||||
|
||||
axis xy off image
|
||||
|
||||
|
||||
|
||||
if par.Niter_inner > 1
|
||||
plotting.smart_figure(13123)
|
||||
plot(squeeze(update_norm(max(1,iter-1),:,:))')
|
||||
hold all
|
||||
plot(nanmedian(squeeze(update_norm(max(1,iter-1),:,:)),1) ,'k','Linewidth', 2)
|
||||
hold off
|
||||
set(gca, 'xscale', 'log')
|
||||
set(gca, 'yscale', 'log')
|
||||
title(sprintf('Inner loop convergence iter = %i Nlayers = %i', iter, Nlayers))
|
||||
axis tight
|
||||
end
|
||||
|
||||
|
||||
drawnow
|
||||
|
||||
t_plot = tic;
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
toc
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
try
|
||||
|
||||
|
||||
volData = ptychotomo.decompress_volume(volData, par);
|
||||
|
||||
% plotting.smart_figure(123123)
|
||||
% subplot(1,2,1)
|
||||
% plotting.imagesc3D(log(mean(abs(math.fftshift_2D(fft2(exp(gather(volData))))),3)))
|
||||
% axis image off
|
||||
% colormap(plotting.colormaps.franzmap)
|
||||
% title('FFT of reconstruction')
|
||||
% subplot(1,2,2)
|
||||
% plotting.imagesc3D(log(mean(abs(math.fftshift_2D(fft2(exp(gather(volData_upd_full))))),3)))
|
||||
% axis image off
|
||||
% colormap(plotting.colormaps.franzmap)
|
||||
% title('FFT of last update')
|
||||
% suptitle(sprintf('Iter %i Nlayers %i fullrecons %i', iter, Nlayers, full_reconstruction))
|
||||
|
||||
|
||||
%%
|
||||
plotting.smart_figure(234)
|
||||
frame = ceil(size(volData,3)/2);
|
||||
subplot(1,2,1)
|
||||
plotting.imagesc3D(-imag(volData(:,:,frame)));
|
||||
caxis(gather(math.sp_quantile(-imag(volData(:,:,frame)), [5e-3, 1-5e-3], 10)))
|
||||
axis off image
|
||||
colorbar
|
||||
colormap bone
|
||||
title('Phase')
|
||||
subplot(1,2,2)
|
||||
plotting.imagesc3D(-real(volData(:,:,frame)));
|
||||
caxis(gather(math.sp_quantile(-real(volData(:,:,frame)), [5e-3, 1-5e-3], 10)))
|
||||
colorbar
|
||||
axis off image
|
||||
colormap bone
|
||||
title('Absorbtion')
|
||||
plotting.suptitle(sprintf('Reconstruction in %i-th iteration', iter))
|
||||
%%
|
||||
|
||||
img = -gather(imag(volData(:,:,frame)));
|
||||
img = img / math.sp_quantile(img, 1-5e-3, 1);
|
||||
img = max(0, min(1, img));
|
||||
if ~exist('./results_ptychotomo', 'dir'); mkdir('results_ptychotomo'); end
|
||||
imwrite(gray2ind(img), bone, sprintf('results_ptychotomo/img_3d_ptycho_iter%i_Nlayers_%i.png', iter, Nlayers))
|
||||
|
||||
|
||||
if ~par.debug
|
||||
%% save a preview on disk
|
||||
% suptitle(sprintf('Iter %i Nlayers %i fullrecons %i', iter, Nlayers, full_reconstruction))
|
||||
% print('-dpng', '-f234', '-r300', sprintf('results_ptychotomo/progress_3d_ptycho_iter%i_Nlayers_%i.png', iter, Nlayers))
|
||||
end
|
||||
|
||||
catch err
|
||||
warning('Plotting failed')
|
||||
keyboard
|
||||
end
|
||||
|
||||
|
||||
|
||||
volData_all{iter} = gather(volData(:,:,ceil(end/2)));
|
||||
|
||||
% save memory
|
||||
volData_upd_full = [];
|
||||
|
||||
volData = ptychotomo.compress_volume(volData, par);
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
%% show final results
|
||||
|
||||
for iter = 1:par.Niter-1
|
||||
try
|
||||
volupd(:,:,iter) =volData_all{iter+1} - volData_all{iter};
|
||||
volprev(:,:,iter) =volData_all{iter};
|
||||
end
|
||||
end
|
||||
|
||||
for iter = 1:par.Niter-2
|
||||
try
|
||||
a = volupd(:,:,iter);
|
||||
b = volupd(:,:,iter+1);
|
||||
C(iter) = abs(corr(a(:), b(:)));
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
figure
|
||||
subplot(1,2,1)
|
||||
plotting.imagesc3D(-imag(volprev))
|
||||
colormap bone
|
||||
axis off image
|
||||
title('Phase')
|
||||
subplot(1,2,2)
|
||||
plotting.imagesc3D(-real(volprev))
|
||||
colormap bone
|
||||
axis off image
|
||||
title('Absorbtion')
|
||||
plotting.suptitle('Reconstruction quality in each iteration')
|
||||
|
||||
figure
|
||||
plotting.imagesc3D(volupd)
|
||||
axis off image
|
||||
title('Complex volume update in each iteration')
|
||||
|
||||
|
||||
|
||||
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,158 @@
|
||||
%UPDATE_VOLUME apply the updated projection information to the shared
|
||||
%tomographic volume using a gradient descent method
|
||||
%
|
||||
% [volData, update_norm] = update_volume(volData, projData_c_upd, update_step, projData_model, par)
|
||||
%
|
||||
%
|
||||
% Inputs:
|
||||
% **volData - 3D array, linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **projData_upd - stacked 2D array, linearized difference of the complex-valued projections, ie log( P_new .* conj(P_orig) ./ |P_orig|^2)
|
||||
% **updated_step - complex scalar, update step length for the gradient descent method, should be < 1
|
||||
% **projData_model - structure that contain information about the model projection at the current angle
|
||||
% **par - parameter structure for the ptychotomo method
|
||||
% *returns*
|
||||
% **volData - 3D array, updated linearized tomographic volume (ie tranmission == exp(sum(volData,1)) == prod(exp(volData)) )
|
||||
% **update_norm - scalar, relative tomo volume change since between the original and update volData
|
||||
|
||||
|
||||
|
||||
% Academic License Agreement
|
||||
%
|
||||
% Source Code
|
||||
%
|
||||
% Introduction
|
||||
% • This license agreement sets forth the terms and conditions under which the PAUL SCHERRER INSTITUT (PSI), CH-5232 Villigen-PSI, Switzerland (hereafter "LICENSOR")
|
||||
% will grant you (hereafter "LICENSEE") a royalty-free, non-exclusive license for academic, non-commercial purposes only (hereafter "LICENSE") to use the PtychoShelves
|
||||
% computer software program and associated documentation furnished hereunder (hereafter "PROGRAM").
|
||||
%
|
||||
% Terms and Conditions of the LICENSE
|
||||
% 1. LICENSOR grants to LICENSEE a royalty-free, non-exclusive license to use the PROGRAM for academic, non-commercial purposes, upon the terms and conditions
|
||||
% hereinafter set out and until termination of this license as set forth below.
|
||||
% 2. LICENSEE acknowledges that the PROGRAM is a research tool still in the development stage. The PROGRAM is provided without any related services, improvements
|
||||
% or warranties from LICENSOR and that the LICENSE is entered into in order to enable others to utilize the PROGRAM in their academic activities. It is the
|
||||
% LICENSEE’s responsibility to ensure its proper use and the correctness of the results.”
|
||||
% 3. THE PROGRAM IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR
|
||||
% A PARTICULAR PURPOSE AND NONINFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. IN NO EVENT SHALL THE LICENSOR, THE AUTHORS OR THE COPYRIGHT
|
||||
% HOLDERS BE LIABLE FOR ANY CLAIM, DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF OR IN CONNECTION WITH THE PROGRAM OR THE USE
|
||||
% OF THE PROGRAM OR OTHER DEALINGS IN THE PROGRAM.
|
||||
% 4. LICENSEE agrees that it will use the PROGRAM and any modifications, improvements, or derivatives of PROGRAM that LICENSEE may create (collectively,
|
||||
% "IMPROVEMENTS") solely for academic, non-commercial purposes and that any copy of PROGRAM or derivatives thereof shall be distributed only under the same
|
||||
% license as PROGRAM. The terms "academic, non-commercial", as used in this Agreement, mean academic or other scholarly research which (a) is not undertaken for
|
||||
% profit, or (b) is not intended to produce works, services, or data for commercial use, or (c) is neither conducted, nor funded, by a person or an entity engaged
|
||||
% in the commercial use, application or exploitation of works similar to the PROGRAM.
|
||||
% 5. LICENSEE agrees that it shall make the following acknowledgement in any publication resulting from the use of the PROGRAM or any translation of the code into
|
||||
% another computing language:
|
||||
% "Data processing was carried out using the PtychoShelves package developed by the Science IT and the coherent X-ray scattering (CXS) groups, Paul
|
||||
% Scherrer Institut, Switzerland."
|
||||
%
|
||||
% Additionally, any publication using the package, or any translation of the code into another computing language should cite
|
||||
% K. Wakonig, H.-C. Stadler, M. Odstrčil, E.H.R. Tsai, A. Diaz, M. Holler, I. Usov, J. Raabe, A. Menzel, M. Guizar-Sicairos, PtychoShelves, a versatile
|
||||
% high-level framework for high-performance analysis of ptychographic data, J. Appl. Cryst. 53(2) (2020). (doi: 10.1107/S1600576720001776)
|
||||
% and for difference map:
|
||||
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379–382 (2008).
|
||||
% (doi: 10.1126/science.1158573),
|
||||
% for maximum likelihood:
|
||||
% P. Thibault and M. Guizar-Sicairos, Maximum-likelihood refinement for coherent diffractive imaging, New J. Phys. 14, 063004 (2012).
|
||||
% (doi: 10.1088/1367-2630/14/6/063004),
|
||||
% for LSQ-ML:
|
||||
% M. Odstrčil, A. Menzel, and M. Guizar-Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Opt. Express 26(3), 3108 (2018).
|
||||
% (doi: 10.1364/OE.26.003108),
|
||||
% for mixed coherent modes:
|
||||
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 68–71 (2013). (doi: 10.1038/nature11806),
|
||||
% and/or for multislice:
|
||||
% E. H. R. Tsai, I. Usov, A. Diaz, A. Menzel, and M. Guizar-Sicairos, X-ray ptychography with extended depth of field, Opt. Express 24, 29089–29108 (2016).
|
||||
% (doi: 10.1364/OE.24.029089),
|
||||
% and/or for OPRP:
|
||||
% M. Odstrcil, P. Baksh, S. A. Boden, R. Card, J. E. Chad, J. G. Frey, W. S. Brocklesby, Ptychographic coherent diffractive imaging with orthogonal probe relaxation.
|
||||
% Opt. Express 24.8 (8360-8369) 2016. (doi: 10.1364/OE.24.008360).
|
||||
% 6. Except for the above-mentioned acknowledgment, LICENSEE shall not use the PROGRAM title or the names or logos of LICENSOR, nor any adaptation thereof, nor the
|
||||
% names of any of its employees or laboratories, in any advertising, promotional or sales material without prior written consent obtained from LICENSOR in each case.
|
||||
% 7. Ownership of all rights, including copyright in the PROGRAM and in any material associated therewith, shall at all times remain with LICENSOR, and LICENSEE
|
||||
% agrees to preserve same. LICENSEE agrees not to use any portion of the PROGRAM or of any IMPROVEMENTS in any machine-readable form outside the PROGRAM, nor to
|
||||
% make any copies except for its internal use, without prior written consent of LICENSOR. LICENSEE agrees to place the following copyright notice on any such copies:
|
||||
% © All rights reserved. PAUL SCHERRER INSTITUT, Switzerland, Laboratory for Macromolecules and Bioimaging, 2017.
|
||||
% 8. The LICENSE shall not be construed to confer any rights upon LICENSEE by implication or otherwise except as specifically set forth herein.
|
||||
% 9. DISCLAIMER: LICENSEE shall be aware that Phase Focus Limited of Sheffield, UK has an international portfolio of patents and pending applications which relate
|
||||
% to ptychography and that the PROGRAM may be capable of being used in circumstances which may fall within the claims of one or more of the Phase Focus patents,
|
||||
% in particular of patent with international application number PCT/GB2005/001464. The LICENSOR explicitly declares not to indemnify the users of the software
|
||||
% in case Phase Focus or any other third party will open a legal action against the LICENSEE due to the use of the program.
|
||||
% 10. This Agreement shall be governed by the material laws of Switzerland and any dispute arising out of this Agreement or use of the PROGRAM shall be brought before
|
||||
% the courts of Zürich, Switzerland.
|
||||
|
||||
function [volData, update_norm] = update_volume(volData, projData_c_upd, update_step, projData_model, par)
|
||||
% update the volData using the provided projection update
|
||||
% projData_c_upd and update_step
|
||||
|
||||
Npx_vol = size(volData);
|
||||
|
||||
position_offset = -projData_model.position_offset;
|
||||
if any(any(round(position_offset) ~= position_offset))
|
||||
error('Noninterger shift may result in mixing real and imaginary data')
|
||||
end
|
||||
|
||||
% apply integer shift to correct for shifts of the projections
|
||||
projData_c_upd = utils.imshift_fast(projData_c_upd, position_offset(1), position_offset(2));
|
||||
|
||||
% crop back to projection size
|
||||
projData_c_upd = utils.crop_pad(projData_c_upd,[Npx_vol(3),Npx_vol(1)]);
|
||||
|
||||
|
||||
Nblock = ceil(prod(Npx_vol)*8 / 1e9); % split into 2GB blocks
|
||||
update_norm = 0;
|
||||
|
||||
for ii = 1:Nblock
|
||||
ind = 1+(ii-1)*ceil(Npx_vol(3)/Nblock):min((ii)*ceil(Npx_vol(3)/Nblock), Npx_vol(3));
|
||||
|
||||
%% update the reconstructed volume
|
||||
args = {projData_model.angle, [Npx_vol(1:2), length(ind)]};
|
||||
[volData_upd_r] = ptychotomo.back_proj(real(projData_c_upd(ind,:,:)),args{:});
|
||||
[volData_upd_i] = ptychotomo.back_proj(imag(projData_c_upd(ind,:,:)),args{:});
|
||||
|
||||
volData_upd = complex(volData_upd_r, volData_upd_i);
|
||||
update_norm = update_norm + gather(norm(volData_upd(:)));
|
||||
|
||||
if update_norm / par.norm_full > 0.5
|
||||
keyboard
|
||||
end
|
||||
% update only the processed block
|
||||
if update_step > 0 && update_norm / par.norm_full < 0.5
|
||||
%% update the full volume, vary the update speed in dependence on number of layers -> avoid instabilities
|
||||
volData_block = volData(:,:,ind);
|
||||
volData_block = arrayfun(@object_constraint_kernel,volData_block, par.support_mask, update_step, volData_upd, par.max_value, par.min_value);
|
||||
%% call MEX function
|
||||
set_to_block_gpu(volData, volData_block, uint16(ind));
|
||||
end
|
||||
|
||||
|
||||
end
|
||||
|
||||
update_norm = update_norm / par.norm_full ;
|
||||
|
||||
|
||||
|
||||
%utils.verbose(0, 'update_norm %g', update_norm)
|
||||
end
|
||||
|
||||
|
||||
%% auxiliary function for GPU, use GPU arrayfun for fast inplace processing
|
||||
|
||||
function volData = object_constraint_kernel(volData, support_mask, update_step, volData_update, max_value, min_value)
|
||||
|
||||
% separated update_step / converegnce rate for phase and absorbtion
|
||||
volData_update = complex(real(update_step) * real(volData_update), imag(update_step) * imag(volData_update));
|
||||
volData = volData .* support_mask;
|
||||
|
||||
|
||||
volData = volData + volData_update;
|
||||
|
||||
volData_r = real(volData);
|
||||
volData_i = imag(volData);
|
||||
|
||||
% apply some clipping of the output values
|
||||
volData_r = min(real(max_value), max(real(min_value), volData_r));
|
||||
volData_i = min(imag(max_value), max(imag(min_value), volData_i));
|
||||
|
||||
volData = complex(volData_r, volData_i) ;
|
||||
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user