initial commit

This commit is contained in:
2026-08-07 15:56:42 +09:00
commit 91ad25aca9
1012 changed files with 159314 additions and 0 deletions
@@ -0,0 +1,69 @@
% GARRAY wrap the GPU functions for user so that CPU and GPU code is identical
%
% array = Garray(array)
%
% ** array numerical array
% returns
% ++ array GPU numerical array
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 array = Garray(array)
global use_gpu
array = utils.Garray(array, use_gpu);
end
@@ -0,0 +1,75 @@
% GFUN wrap the arrayfun function so that CPU and GPU code is identical
% when GPU is not available, assume that matlab dimension expansion is enough
%
% varargout = Gfun(varargin)
%
% ** varargin {function, arguments, ...}
% returns
% ++ varargout outputs
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 varargout = Gfun(varargin)
import core.*
global use_gpu
varargout = cell(nargout,1);
if use_gpu
[varargout{:}] = arrayfun(varargin{:});
else
[varargout{:}] = varargin{1}(varargin{2:end});
end
end
@@ -0,0 +1,70 @@
% GGATHER wrap the GPU functions for user so that CPU and GPU code is identical
%
% array = Ggather(array)
%
% ** array GPU array
% returns
% ++ array CPU array
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 array = Ggather(array)
global use_gpu
if use_gpu
array = gather(array);
end
end
@@ -0,0 +1,73 @@
% GPAGEFUN wrapper around GPU pagefun function to get similar functionality for GPU and CPU arrays
% out = Gpagefun(fun, varargin)
%
% ** varargin {function, arguments, ...}
% returns
% ++ varargout outputs
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 out = Gpagefun(fun, varargin)
% wrapper around GPU pagefun function
if isa(varargin{1}, 'gpuArray')
out = pagefun(fun,varargin{:});
else
Npages = size(varargin{1},3);
for ii = 1:Npages
out(:,:,ii) = fun(varargin{1}(:,:,ii), varargin{2}(:,:,ii));
end
end
end
@@ -0,0 +1,97 @@
% GZEROS wrap the GPU functions for user so that CPU and GPU code is identical
%
% array = Gzeros(varargin)
%
% ** varargin identical to "zeros"
% returns
% ++ array identical to "zeros", only created on GPU if possible
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 array = Gzeros(varargin)
global use_gpu
% if islogical(varargin{end}) && varargin{end} == true
% type = single(1i); % directly create complex array !!
% else
% type = single(1);
% end
%
if length(varargin) > 1 && islogical(varargin{end})
varargin = varargin(1:end-1);
isComplex = true;
else
isComplex = false;
end
% if use_gpu
% % move this scalar to GPU to tell matlab what we want
% type = gpuArray(type);
% end
%
% array = zeros(varargin{:}, 'like', type);
if use_gpu
array = gpuArray.zeros(varargin{:}, 'single');
else
array = zeros(varargin{:}, 'single');
end
if isComplex
array = complex(array);
end
end
@@ -0,0 +1,90 @@
% CHECK_AVAIL_MEMORY check and possibly report the available GPU memory
%
% memory_out = check_avail_memory(show)
%
% ** show (bool) if true, print results
% returns
% ++ memory_out (scalar) availible GPU memory in GB
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 memory_out = check_avail_memory(show)
import utils.verbose
if nargin == 0; show = false; end
global use_gpu gpu
if isempty(use_gpu); use_gpu = false; end
if use_gpu
memory = gpu.AvailableMemory / gpu.TotalMemory;
if memory < 0.03
error('MATLAB:LowGPUMem','Low gpu memory : %3.0f%%', memory*100)
elseif (show && memory < 0.2) || verbose()> 2
if verbose()> 4
dbstack(1)
end
verbose(4,'Free gpu memory: %3.3g%%', gpu.AvailableMemory / gpu.TotalMemory*100)
end
if nargout
memory_out = gpu.AvailableMemory / 1e9;
end
else
if nargout
memory_out = 1;
end
end
end
@@ -0,0 +1,157 @@
% ESTIMATE_REQUIRED_GPU_MEMORY Estimate GPU memory required to run reconstruction with provided parameters
%
% [required_mem , data_mem, object_mem, required_fft_mem] = ...
% estimate_required_GPU_memory(self, par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% returns
% ++ required_mem total required mem
% ++ data_mem mem to store data
% ++ object_mem mem to store object
% ++ required_fft_mem mem to run FFT
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [required_mem , data_mem, object_mem, required_fft_mem] = estimate_req_memory(self, par, grouping)
% estimate how much GPU memoty will be needed to run the code
import utils.*
import engines.GPU_MS.shared.*
if nargin < 3
grouping = par.grouping;
end
if par.gpu_id < 1
% ie no GPU is used
required_mem = nan; data_mem = nan; object_mem = nan; required_fft_mem = nan;
return
end
required_mem = 0;
data_mem = 0;
object_mem = 0;
verbose(2, 'Checking available GPU memory ID:%i', par.gpu_id)
if par.keep_on_gpu
data_class = class(self.diffraction);
switch data_class
case 'single'
byte_size = 4;
case 'uint16'
byte_size = 2;
case {'uint8','int8'}
byte_size = 1;
end
% keep data on GPU
data_mem = data_mem + prod(self.Np_p)*self.Npos*byte_size / 2^(2*par.upsampling_data_factor); % self.diffraction can be either cell or array, so the size is calculated from par
data_mem = data_mem + numel(self.mask); % bool (uint8 in matlab)
data_mem = data_mem + numel(self.noise)*4; % single
% keep views on GPU
if any(strcmpi(par.method, {'DM'}))
required_mem = required_mem + 8*prod(self.Np_p)*self.Npos*par.probe_modes;
end
end
% very empirical guess , assuming FFT memory requirement ~6*8*numel(x)
required_fft_mem = (2)*6*8*prod(self.Np_p)*grouping; % empirically tested
required_mem = required_mem + required_fft_mem;
% basic arrays: obj_proj, chi
% add for multiple layers by ZC
if isfield(par,'Nlayers')
Nlayers=par.Nlayers;
else
Nlayers=1;
end
required_mem = required_mem + 2*4*2*prod(self.Np_p)*grouping*par.Nmodes; % added Nmodes by ZC
% multilayers of probe added by ZC
required_mem = required_mem + 2*4*prod(self.Np_p)*Nlayers*grouping*par.Nmodes + 2*4*prod(self.Np_p)*(grouping+par.Nmodes-1);
if any(~isinf([par.probe_position_search, par.probe_fourier_shift_search]))
% memory needed to keep a probe for each position in the grouping
required_mem = required_mem + 2*4*prod(self.Np_p)*grouping;
end
if is_method(par, 'PIE') && par.variable_probe
% size of probe stored for each scan position
required_mem = required_mem + 4*2*prod(self.Np_p)*self.Npos;
end
if is_method(par, 'ML') && par.momentum
% account for data stored for momentum estimate
momentum_mem = 2 + 1 ; % 2 previous steps + velocity map
object_mem = object_mem + momentum_mem*4*2*prod(self.Np_o)*numel(self.object);
end
if is_method(par, 'ML') && par.accelerated_gradients_start < par.number_iterations
% account for data stored for accelerated gradient
object_mem = object_mem + 2*4*2*prod(self.Np_o)*numel(self.object);
end
if ~par.share_probe
% size of probe stored for each scan position
required_mem = required_mem + 4*2*prod(self.Np_p)*grouping;
end
if par.apply_subpix_shift || par.variable_probe
% memory needed for subpixel shifted probe
required_mem = required_mem + 4*2*prod(self.Np_p)*grouping*par.Nmodes; % add par.Nmodes by ZC
end
% size of the object, object update, local object illumination,
% total object illumination
object_mem = object_mem + 4*prod(self.Np_o) * ( (2+2+1)*numel(self.object) );
% add object and data
required_mem = required_mem + data_mem;
required_mem = required_mem+object_mem;
end
@@ -0,0 +1,147 @@
% ESTIMATE_REQUIRED_GPU_MEMORY Estimate GPU memory required to run reconstruction with provided parameters
%
% [required_mem , data_mem, object_mem, required_fft_mem] = ...
% estimate_required_GPU_memory(self, par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% returns
% ++ required_mem total required mem
% ++ data_mem mem to store data
% ++ object_mem mem to store object
% ++ required_fft_mem mem to run FFT
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [required_mem , data_mem, object_mem, required_fft_mem] = estimate_req_memory(self, par, grouping)
% estimate how much GPU memoty will be needed to run the code
import utils.*
import engines.GPU_MS.shared.*
if nargin < 3
grouping = par.grouping;
end
if par.gpu_id < 1
% ie no GPU is used
required_mem = nan; data_mem = nan; object_mem = nan; required_fft_mem = nan;
return
end
required_mem = 0;
data_mem = 0;
object_mem = 0;
verbose(2, 'Checking available GPU memory ID:%i', par.gpu_id)
if par.keep_on_gpu
data_class = class(self.diffraction);
switch data_class
case 'single'
byte_size = 4;
case 'uint16'
byte_size = 2;
case {'uint8','int8'}
byte_size = 1;
end
% keep data on GPU
data_mem = data_mem + prod(self.Np_p)*self.Npos*byte_size / 2^(2*par.upsampling_data_factor); % self.diffraction can be either cell or array, so the size is calculated from par
data_mem = data_mem + numel(self.mask); % bool (uint8 in matlab)
data_mem = data_mem + numel(self.noise)*4; % single
% keep views on GPU
if any(strcmpi(par.method, {'DM'}))
required_mem = required_mem + 8*prod(self.Np_p)*self.Npos*par.probe_modes;
end
end
% very empirical guess , assuming FFT memory requirement ~6*8*numel(x)
required_fft_mem = (2)*6*8*prod(self.Np_p)*grouping; % empirically tested
required_mem = required_mem + required_fft_mem;
% basic arrays: obj_proj, chi
required_mem = required_mem + 2*4*2*prod(self.Np_p)*grouping;
if any(~isinf([par.probe_position_search, par.probe_fourier_shift_search]))
% memory needed to keep a probe for each position in the grouping
required_mem = required_mem + 2*4*prod(self.Np_p)*grouping;
end
if is_method(par, 'PIE') && par.variable_probe
% size of probe stored for each scan position
required_mem = required_mem + 4*2*prod(self.Np_p)*self.Npos;
end
if is_method(par, 'ML') && par.momentum
% account for data stored for momentum estimate
momentum_mem = 2 + 1 ; % 2 previous steps + velocity map
object_mem = object_mem + momentum_mem*4*2*prod(self.Np_o)*numel(self.object);
end
if is_method(par, 'ML') && par.accelerated_gradients_start < par.number_iterations
% account for data stored for accelerated gradient
object_mem = object_mem + 2*4*2*prod(self.Np_o)*numel(self.object);
end
if ~par.share_probe
% size of probe stored for each scan position
required_mem = required_mem + 4*2*prod(self.Np_p)*grouping;
end
if par.apply_subpix_shift || par.variable_probe
% memory needed for subpixel shifted probe
required_mem = required_mem + 4*2*prod(self.Np_p)*grouping;
end
% size of the object, object update, local object illumination,
% total object illumination
object_mem = object_mem + 4*prod(self.Np_o) * ( (2+2+1)*numel(self.object) );
% add object and data
required_mem = required_mem + data_mem;
required_mem = required_mem+object_mem;
end
@@ -0,0 +1,107 @@
% INITIALIZE wrap the GPU functions for user so that CPU and GPU code is identical
%
% param = initialize(param)
%
% ** par structure containing parameters for the engines
% returns
% ++ par updated structure containing parameters for the engines
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 param= initialize(param)
import utils.verbose
global use_gpu gpu
if exist('gpuArray', 'class') && param.use_gpu && gpuDeviceCount
verbose(2, 'Initializing GPU %i ... ', param.gpu_id )
try
gpu= gpuDevice;
catch err
if strcmp(err.identifier,'parallel:gpu:array:NoPCTLicense')
disp('No free licences for parallel toolbox')
pause(10)
gpu= gpuDevice;
else
rethrow(err)
end
end
if ~isempty(param.gpu_id) && gpu.Index ~= param.gpu_id
gpu= gpuDevice(param.gpu_id);
else
% let matlab choose, useful for parfor
end
verbose(1, 'GPU %i initialized ... ', gpu.Index )
param.gpu_id = gpu.Index;
use_gpu = true;
elseif ~param.use_gpu
use_gpu = false;
verbose(0,'Using CPU implementation, calculation can be slower')
gpu = [];
elseif param.use_gpu && gpuDeviceCount==0
warning('No GPU devices available, switching to CPU ... ')
use_gpu = false;
end
param.use_gpu = use_gpu;
% initial persistent variables in Garray
utils.Garray([], use_gpu);
end
@@ -0,0 +1,105 @@
% MOVE_FROM_GPU gather reconstruction back from GPU to RAM
%
% self = move_from_gpu(self, move_from_GPU = true)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** move_from_GPU (bool) if true (default), data will be gathered from GPU
% returns:
% ** self structure containing outputs
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = move_from_gpu(self, move_from_GPU)
import engines.GPU_MS.GPU_wrapper.*
for i = 1:length(self.modes)
for field = {'weights', 'ASM_factor', 'cASM_factor', 'FAR_factor', 'cFAR_factor'}
if isfield(self.modes{i}, field{1})
self.modes{i}.(field{1}) = [];
end
end
end
for field = {'probe_support', 'background', 'intensity_corr', 'probe_fourier_shift', 'probe_evolution'}
if isfield(self, field{1})
self.(field{1}) = Ggather(self.(field{1}));
end
end
if nargin < 2 || move_from_GPU
self.diffraction = Ggather(self.diffraction);
self.noise = Ggather(self.noise);
self.mask = Ggather(self.mask);
end
for i = 1:numel(self.probe)
self.probe{i} = Ggather(self.probe{i});
end
for i = 1:numel(self.object)
self.object{i} = Ggather(self.object{i});
end
for i = 1:numel(self.illum_sum)
self.illum_sum{i} = Ggather(self.illum_sum{i});
end
if isfield(self, 'phase')
for i = 1:length(self.phase)
self.phase{i} = Ggather(self.phase{i});
end
end
end
@@ -0,0 +1,141 @@
% MOVE_TO_GPU move reconstruction from RAM to GPU
%
% [self, cache] = move_to_gpu(self,cache, move_data, split_data)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** move_data (bool) if true (default), data will be moved on GPU as well
% ** split_data if the data grouping is fixed, the data will be stored in cell for each group separatelly, important for large datasets
%
% returns:
% ++ self self structure moved to GPU
% ++ cache cache structure moved to GPU
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [self, cache] = move_to_gpu(self,cache, move_data, split_data)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
verbose(0, 'Moving data to GPU')
verbose(0, 'Free GPU memory %3.2fGB', check_avail_memory)
for i = 1:numel(self.probe)
self.probe{i} = complex(Garray(self.probe{i}));
end
for i = 1:numel(self.object)
self.object{i} = complex(Garray(self.object{i}));
end
if isfield(self, 'phase')
for i = 1:length(self.phase)
self.phase{i} = Garray(self.phase{i});
end
end
for i = 1:length(self.modes)
for field = {'weights','probe_rel_intensity', ...
'ASM_factor', 'cASM_factor', ...
'FAR_factor', 'cFAR_factor', ...
'probe_support', 'probe_support', ...
'support_back_propagation_factor', 'support_propagation_factor'}
try self.modes{i}.(field{1}) = Garray(self.modes{i}.(field{1}));end
end
end
% verbose(0, 'Free GPU memory %g ', check_avail_memory)
for field = {'deconv_matrix', 'probe_support', 'background', 'mask'}
try self.(field{1}) = Garray(self.(field{1}));end
end
for field = { 'apodwin', 'background_profile', 'background_weight', 'US_diffraction', 'V_diffraction', 'MAX_ILLUM', 'blur_kernel'}
try cache.(field{1}) = Garray(cache.(field{1}));end
end
for i = 1:numel(cache.illum_sum_0)
cache.illum_sum_0{i} = Garray(cache.illum_sum_0{i});
end
check_avail_memory
if nargin < 2 || move_data
if split_data
% split data into cells for each scan -> avoid memory
% limitations for too many joined scans and make data loading
% faster
if isfield(cache, 'preloaded_indices_compact')
assert(length(cache.preloaded_indices_compact) == 1, 'Dataset splitting implemented only for single set, use MLc method or smaller dataset')
ind = cache.preloaded_indices_compact{1}.indices;
elseif isfield(cache, 'preloaded_indices_simple')
assert(length(cache.preloaded_indices_simple) == 1, 'Dataset splitting implemented only for single set, use MLc method or smaller dataset')
ind = cache.preloaded_indices_simple{1}.indices;
else
error('Data splitting implemented only for DM or MLc solvers ')
end
for ii = 1:length(ind)
diffraction{ii} = Garray(self.diffraction(:,:,ind{ii}));
end
self.diffraction = diffraction;
else
self.diffraction = Garray(self.diffraction);
end
self.noise = Garray(self.noise);
self.mask = Garray(self.mask);
end
verbose(0, 'Data moved to GPU')
verbose(0, 'Free GPU memory %3.2fGB', check_avail_memory)
end
@@ -0,0 +1,216 @@
% ADD_MOMENTUM_OBJECT speed up convergence by adding momentum / integral term into the
% update direction for the MLc method
%
% [self, cache] = add_momentum_object(self, cache, par, object_upd_sum, iter, fourier_error)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** par structure containing parameters for the engines
% ** object_upd_sum cell of arrays containing update direction from the LSQML method
% ** iter current iteation numebr
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ cache structure with precalculated values to avoid unnecessary overhead
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [self, cache] = add_momentum_object(self, cache, par, object_upd_sum, iter, fourier_error, beta_object)
import utils.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
verbose(1, 'Adding momentum to object')
object_modes = size(self.object,1);
% create velocity maps in the first iterations
if iter == par.object_change_start
cache.object_upd_sum = cell(object_modes,1);
for ll = 1:object_modes
for jj = 1:par.Nlayers
cache.velocity_map_object{ll,jj} = Gzeros(self.Np_o);
cache.object_upd_sum{ll,jj} = [];
end
end
end
% store previous update directions
for ll = 1:object_modes
for jj = 1:par.Nlayers
upd = object_upd_sum{ll,jj}(cache.object_ROI{:})* mean(beta_object(:,jj));
upd = upd / norm2(upd);
cache.object_upd_sum{ll,jj} = [cache.object_upd_sum{ll,jj}, {upd}];
end
end
% how many steps are stored to calculate optimal friction
momentum_memory = 2;
% use Fourier error to avoid issues with convergence
ind_compare = find(all(~isnan(fourier_error),2),3,'last');
if length(ind_compare) > 2
merr = mean(fourier_error,2);
ferr_ok = max(merr(ind_compare([1,2]))) > min(merr(ind_compare([2,3])));
else
ferr_ok = true;
end
if iter > momentum_memory+par.object_change_start
for ll = 1:object_modes
for jj = 1:par.Nlayers
cache.object_upd_sum{ll,jj}(1) = []; % delete the oldest stored object update
corr_level = nan;
if ferr_ok
% caculate correlation between updated to estimate optimal
% friction, !! NOTE that cache.object_upd_sum contains only the
% object_ROI region !!
%
switch momentum_memory
case 2, [aux{1}, aux{2}] = compare_upd_directions_2(cache.object_upd_sum{ll,jj}{:});
case 3, [aux{1}, aux{2}, aux{3}] = compare_upd_directions_2(cache.object_upd_sum{ll,jj}{:});
othewise, error('Not implemented')
end
for kk = 1:momentum_memory
corr_level(kk) = real(Ggather(mean2(aux{kk})));
end
end
if ferr_ok && all(corr_level > 0 )
% estimate optimal friction from previous steps
poly_fit = polyfit(0:momentum_memory,[0,log(corr_level)],1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gain = par.momentum; % smaller -> lower relative speed (less momentum)
friction = 0.5*max(-poly_fit(1),0); % smaller -> longer memory, more momentum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update object by the momentum gradient
[self.object{ll,jj}, cache.velocity_map_object{ll,jj}] = update_momentum(self.object{ll,jj}, cache.velocity_map_object{ll,jj}, object_upd_sum{ll,jj}, friction, gain, cache.illum_sum_0{ll}, cache.MAX_ILLUM(ll));
else
% error was increased or updates are not well correlated, skip acceleration
gain = 0; friction = inf;
cache.velocity_map_object{ll,jj} = cache.velocity_map_object{ll,jj}/2;
end
if verbose()> 1
norm_upd = norm2(object_upd_sum{ll,jj});
norm_vmap = norm2(gain.*cache.velocity_map_object{ll,jj});
verbose(2,['Object %i Corr=', repmat('1:%5.2f ',1,momentum_memory)],ll, corr_level )
verbose(0, 'Momentum: friction=%3.1e \tacceleration %1.1fx',friction, 1+norm_vmap/norm_upd)
end
end
end
% plotting.smart_figure(25454)
% img = cat(1,cat(2, cache.velocity_map_object{:}) .* cat(2,cache.illum_sum_0{:}), cat(2, object_upd_sum{:}) .* cat(2,cache.illum_sum_0{:}));
% aimg = abs(img);
% img = min(aimg, quantile(aimg(:), 0.99)) .* img ./ (aimg+1e-3);
% plotting.imagesc3D(img(1:4:end, 1:4:end))
% axis off image xy
% drawnow
try
if verbose()> 2
plotting.smart_figure(121)
subplot(1,2,1)
plotting.imagesc3D(cache.velocity_map_object{1,1})
axis off image
title(sprintf('Velocity, iter=%i', iter))
subplot(1,2,2)
plotting.imagesc3D(object_upd_sum{1,1})
axis off image
title('Gradient')
drawnow
end
catch
keyboard
end
end
end
function [object, Vmap] = update_momentum(object, Vmap, Vmap_upd, friction, gain, weight, w_max)
% auxiliary function
weight = weight ./ (0.1*w_max+weight);
Vmap = (1-friction)*Vmap + Vmap_upd;
object = object + weight.*gain.*Vmap;
end
function [out1, out2] = compare_upd_directions_2(upd1, upd2, upd3)
% compare updates
upd3 = conj(upd3);
out2 = upd1 .* upd3;
out1 = upd2 .* upd3;
end
function [out1, out2, out3] = compare_upd_directions_3(upd1, upd2, upd3, upd4)
% compare updates
upd4 = conj(upd4);
out3 = upd1 .* upd4;
out2 = upd2 .* upd4;
out1 = upd3 .* upd4;
end
@@ -0,0 +1,197 @@
% ADD_MOMENTUM_PROBE speed up convergence by adding momentum / integral term into the
% update direction for the MLc method
%
% [self, cache] = add_momentum_probe(self, cache, par, probe_upd, iter, fourier_error)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** par structure containing parameters for the engines
% ** probe_upd cell of arrays containing update direction from the LSQML method
% ** iter current iteation numebr
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ cache structure with precalculated values to avoid unnecessary overhead
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [self, cache] = add_momentum_probe(self, cache, par, probe_upd, iter, fourier_error, beta_probe)
import utils.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
verbose(1, 'Adding momentum to probe')
Nmodes = size(self.probe,1);
% create velocity maps in the first iterations
if iter == par.probe_change_start
cache.probe_upd_sum = cell(Nmodes,1);
for ll = 1:Nmodes
cache.velocity_map_probe{ll} = Gzeros(self.Np_p);
cache.probe_upd_sum{ll} = [];
end
end
% store previous update directions
for ll = 1:Nmodes
probe_upd{ll} = probe_upd{ll}(:,:,:,1); % accelerate only the fundamental probe in the variable probe extension
probe_upd{ll} = probe_upd{ll} .* mean(beta_probe(:));
upd = probe_upd{ll} ./ (norm2(probe_upd{ll})+eps);
cache.probe_upd_sum{ll} = [cache.probe_upd_sum{ll}, {upd}];
end
% how many steps are stored to calculate optimal friction
momentum_memory = 3;
if iter > momentum_memory+par.probe_change_start
for ll = 1:Nmodes
probe_modes= size(self.probe{ll},3);
cache.probe_upd_sum{ll}(1) = []; % delete the oldest stored probe update
% caculate correlation between updated to estimate optimal
% friction,
[aux{1}, aux{2}, aux{3}] = compare_upd_directions(cache.probe_upd_sum{ll}{:});
for kk = 1:momentum_memory
corr_level(kk,:) = real(Ggather(mean2(aux{kk})));
end
% use Fourier error to avoid issues with convergence
ind_compare = find(all(~isnan(fourier_error),2),3,'last');
if length(ind_compare) > 2
merr = mean(fourier_error,2);
ferr_ok = max(merr(ind_compare([1,2]))) > min(merr(ind_compare([2,3])));
else
ferr_ok = true;
end
if all(corr_level(:) > 0 ) && ferr_ok
try
% estimate optimal friction from previous steps
poly_fit = polyfit(repmat(0:momentum_memory,probe_modes,1),[zeros(1,probe_modes);log(corr_level)]',1);
catch
keyboard
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gain = par.momentum; % smaller -> lower relative speed (less momentum)
friction = 0.5*max(-poly_fit(1),0); % smaller -> longer memory, more momentum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% update probe by the momentum gradient , accelerate only the fundamental probe in the variable probe extension
[self.probe{ll}(:,:,:,1), cache.velocity_map_probe{ll}] = update_momentum(self.probe{ll}(:,:,:,1), cache.velocity_map_probe{ll}, probe_upd{ll}, friction, gain);
else
% error was increased or updates are not well correlated, skip acceleration
gain = 0; friction = inf;
cache.velocity_map_probe{ll} = cache.velocity_map_probe{ll}/2;
end
if verbose() > 0
norm_upd = mean(norm2(probe_upd{ll}));
norm_vmap = eps+mean(norm2(gain.*cache.velocity_map_probe{ll}));
verbose(2,['Probe %i Corr=', repmat('1:%5.2f ',1,momentum_memory)],ll, corr_level(:,1) )
verbose(0, 'Momentum: friction=%3.1e \tacceleration %1.1fx',friction(:,1), 1+norm_vmap./norm_upd(1))
end
end
% plotting.smart_figure(25454)
% img = cat(1,cat(2, cache.velocity_map_probe{:}) .* cat(2,cache.illum_sum_0{:}), cat(2, probe_upd_sum{:}) .* cat(2,cache.illum_sum_0{:}));
% aimg = abs(img);
% img = min(aimg, quantile(aimg(:), 0.99)) .* img ./ (aimg+1e-3);
% plotting.imagesc3D(img(1:4:end, 1:4:end))
% axis off image xy
% drawnow
try
if verbose() > 2
plotting.smart_figure(1213)
subplot(1,2,1)
plotting.imagesc3D(cache.velocity_map_probe{1,1})
axis off image
title(sprintf('Velocity, iter=%i, norm=%g', iter, norm2(cache.velocity_map_probe{1,1})))
subplot(1,2,2)
plotting.imagesc3D(probe_upd{1})
axis off image
title(sprintf('Gradient, norm=%g',norm2(probe_upd{1})))
drawnow
end
catch
keyboard
end
end
end
function [probe, Vmap] = update_momentum(probe, Vmap, Vmap_upd, friction, gain)
% auxiliary function
Vmap = (1-friction)*Vmap + Vmap_upd;
probe = probe + gain.*Vmap;
end
function [out1, out2, out3] = compare_upd_directions(upd1, upd2, upd3, upd4)
% compare updates
upd4 = conj(upd4);
out3 = upd1 .* upd4;
out2 = upd2 .* upd4;
out1 = upd3 .* upd4;
end
@@ -0,0 +1,97 @@
% GET_FORWARD_MODEL from the provided object and probe calculate the exit wave
%
% [self, probe, obj_proj, psi] = get_forward_model(self, obj_proj, par, cache, g_ind, p_ind, scan_ids, layer_ids)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** obj_proj [Nx,Ny,N] array, just a preallocated array on GPU, can be empty
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** p_ind indices containg corresponding probe id for each processed position
% ** scan_ids determines to which scan correponds each of the position
% ** layer_ids id of the solved layer for multilayer ptycho
%
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ probe either [Nx,Nx,1] or [Nx,Nx,N] aarray of shared probe or variable probe that differs for each position
% ++ obj_proj [Nx,Ny,N] array, views of the object for each scan position
% ++ psi [Nx,Ny,N] array, complex valued exit-wave (psi = P*O)
%
% see also: engines.GPU_MS.LSQML
function [self, probe, obj_proj, psi] = get_forward_model(self, obj_proj, par, cache, g_ind, p_ind, scan_ids, layer_ids)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.LSQML.*
import math.*
import utils.*
import plotting.*
if isempty(obj_proj{1})
for ll = 1:par.object_modes
obj_proj{ll} = Gzeros([self.Np_p, 0], true);
end
end
% allocate memory for probes. Added by ZC
% probe = self.probe;
if par.Nlayers > 1
probe=cell(par.probe_modes,par.Nlayers+1);
end
% get illumination probe
for ll = 1:par.probe_modes
if (ll == 1 && (par.variable_probe || par.variable_intensity))
% add variable probe (OPRP) part into the constant illumination
probe{ll,1} = get_variable_probe(self.probe{ll}, self.probe_evolution(g_ind,:),p_ind{ll});
else
% store the normal (constant) probe(s)
probe{ll,1} = self.probe{min(ll,end)}(:,:,min(end,p_ind{ll}),1);
end
if (ll == 1 && par.apply_subpix_shift && isinf(self.z_distance(end))) || is_used(par,'fly_scan')
% only in farfield mode
probe{ll,1} = apply_subpx_shift(probe{ll,1}, self.modes{min(end,ll)}.sub_px_shift(g_ind,:) );
end
if (ll == 1)
probe{ll,1} = apply_subpx_shift_fft(probe{ll,1}, self.modes{1}.probe_fourier_shift(g_ind,:));
end
end
% allocate memory for psi. Added by ZC
psi=cell(max(par.object_modes, par.probe_modes),1);
% get projection of the object and probe
for layer = 1:par.Nlayers
for ll = 1:max(par.object_modes, par.probe_modes)
llo = min(ll, par.object_modes);
llp = min(ll, par.probe_modes);
% get objects projections
obj_proj{llo} = get_views(self.object, obj_proj{llo},layer_ids(layer),llo, g_ind, cache, scan_ids,[]);
if (ll == 1 && par.apply_subpix_shift && ~isinf(self.z_distance(end)))
% only in nearfield mode , apply shift in the opposite direction
obj_proj{ll} = apply_subpx_shift(obj_proj{ll} .* cache.apodwin, -self.modes{min(end,ll)}.sub_px_shift(g_ind,:) ) ./ cache.apodwin;
end
% get exitwave after each layer
psi{ll} = probe{llp,layer} .* obj_proj{llo};
%modified by YJ: no need to do another propagation after the
%last object layer
if layer < par.Nlayers
% fourier propagation
[psi{ll}] = fwd_fourier_proj(psi{ll} , self.modes{layer}, g_ind);
if par.Nlayers > 1
probe{llp,layer+1} = psi{llp};
end
end
end
end
% At this point, psi is the exit wave after the last object layer
% Now propagate the wave function to far-field (detector) plane
% This is same as using fwd_fourier_proj w. distance = inf and no camera angle refinement
for ll= 1:max(par.object_modes, par.probe_modes)
psi{ll} = fft2_safe(psi{ll}); % fully farfield
end
end
@@ -0,0 +1,143 @@
% GET_OPTIMAL_LSQ_STEP calculate the optimal step lenght for given update directions and chi array
%
% [beta_probe, beta_object] = get_optimal_LSQ_step(self,chi,dO,dP,O,P, p_ind, par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** chi [Nx,Ny,N] array, difference between original and updated exit-wave
% ** dO [Nx,Ny,N] array, object update direction
% ** dP [Nx,Ny,N] array, probe update direction
% ** O [Nx,Ny,N] array, object views
% ** P [Nx,Ny,1] or [Nx,Ny,N] array, single or variable probe
% ** p_ind indices containg corresponding probe id for each processed position
% ** par structure containing parameters for the engines
%
% returns:
% ++ beta_probe optimal probe step
% ++ beta_object optimal object step
%
% see also: engines.GPU_MS.LSQML
function [beta_probe, beta_object] = get_optimal_LSQ_step(self,chi,dO,dP,O,P, p_ind, par)
% find optimal step in the LSQ sense
import engines.GPU_MS.GPU_wrapper.*
import math.*
global gpu use_gpu
if ~isempty(gpu); wait(gpu); end
grouping = size(chi,3);
lambda_0 = eps(single(1)) / prod(self.Np_p);
lambda_LSQ = 0.1; % add some small regularization to avoid instabilities
if use_gpu
% fast mex based CUDA version
if size(dP,3) == grouping
p_ind = 1:grouping; % one update for each position
elseif size(dP,3) == 1 && numel(p_ind) == 1
p_ind = ones(size(chi,3),1); % use only the one update given
elseif max(p_ind) <= size(dP,3) && numel(p_ind) == grouping
else
warning('Checkme, untested option')
keyboard
end
try
[AA, Atb] = get_LSQ_step_mex(chi,dO,dP,O,P,lambda_0, uint8(p_ind));
catch err
if any(strcmp(err.identifier, { 'MATLAB:UndefinedFunction','MATLAB:mex:ErrInvalidMEXFile'}))
path = fullfile(replace(mfilename('fullpath'), mfilename, ''), 'private');
mexcuda('-output', [path, '/get_LSQ_step_mex'], [path, '/get_LSQ_step_mex.cu'])
[AA, Atb] = get_LSQ_step_mex(chi,dO,dP,O,P,lambda_0, uint8(p_ind));
else
rethrow(err)
end
end
AA = Ggather(AA);
Atb = Ggather(Atb);
% is seems to be faster to get it first from GPU and then apply some
% oprations because the matrices are too small
AA = sum(AA,4);
Atb = sum(Atb,4);
AA = AA + lambda_LSQ*diag(diag(mean(AA,3)));
% the system of equations is so small that solving on CPU is good enough
[x1, x2] = solve_LSQ(AA(1,1,:), AA(2,1,:), AA(1,2,:), AA(2,2,:), Atb(1,1,:), Atb(2,1,:));
LSQ_step = cat(1, x1, x2);
else
if ~( par.share_probe || length(unique(p_ind)) == 1 )
% in case of multiple scans !!
% replicate the update back to the original dP size
dP = dP(:,:,p_ind);
end
tic
% prevent ill posed inversion, ideally it should be Garray(mean(abs(AA1)+abs(AA4))/2) but it i show
[AA1,AA2,AA4, Atb1,Atb2] = ...
Gfun(@get_optimal_step_lsq, chi,dO,dP,...
O,P, lambda_0);
AA1 = sum2(AA1);
AA2 = sum2(AA2);
AA4 = sum2(AA4);
Atb1 = sum2(Atb1);
Atb2 = sum2(Atb2);
% it seems faster to solve it on GPU than using pagefun on GPU
AA1 = Ggather(AA1);AA2 = Ggather(AA2);AA4 = Ggather(AA4);Atb1 = Ggather(Atb1);Atb2 = Ggather(Atb2);
AA3 = conj(AA2);
lambda = 0.5; % add some small regularization to avoid unstabilities
I = lambda*[mean(AA1), mean(AA4)];
AA = [ AA1+I(1), AA2; AA3, AA4+I(2)];
Atb= [ Atb1; Atb2];
[x1, x2] = solve_LSQ(AA(1,1,:), AA(2,1,:), AA(1,2,:), AA(2,2,:), Atb(1,1,:), Atb(2,1,:));
LSQ_step = cat(1, x1, x2);
end
LSQ_step = max(0, real(LSQ_step));
LSQ_step = Ggather(LSQ_step);
% prevent unwanted oscilation of step gets too high
beta_probe = LSQ_step(2,1,:);
beta_object = LSQ_step(1,1,:);
beta_probe = (par.beta_probe *par.beta_LSQ)* beta_probe;
beta_object = (par.beta_object*par.beta_LSQ)* beta_object;
end
function [AA1,AA2,AA4, Atb1,Atb2] = ...
get_optimal_step_lsq(chi,dO,dP,O,P, lambda)
% fast kernel for estimation of optimal P and object steps
dOP = dO.*P;
dPO = dP.*O;
cdOP = conj(dOP);
cdPO = conj(dPO);
AA1 = real(dOP .* cdOP)+lambda;
AA2 = (dOP .* cdPO);
AA4 = real(dPO .* cdPO)+lambda;
Atb1 = real(cdOP .* chi);
Atb2 = real(cdPO .* chi);
end
function [x1, x2] = solve_LSQ(AA1, AA2, AA3, AA4, Atb1, Atb2)
% GPU kernel to solve simple 2x2 system of equations
det = (AA1.*AA4 - AA2.*AA3);
x1 = -conj(AA2.*Atb2-AA4.*Atb1) ./ det;
x2 = conj(AA1.*Atb2-AA3.*Atb1) ./ det;
end
@@ -0,0 +1,89 @@
% GET_VARIABLE_PROBE calculate variable probe for given mean/var probe and its evolution inputs
%
% probe = get_variable_probe(probe, probe_evolution, p_ind)
%
% ** probe [Nx,Ny,probe_modes,variable_modes] array, variable probe
% ** probe_evolution [Npos,variable_modes] array containing evolution of the varaible modes for each position
% ** p_ind indices containg corresponding probe id for each processed position
%
% returns:
% ++ probe [Nx,Ny,N] array, a different probe for each scan position
%
% see also: engines.GPU_MS.LSQML
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 probe = get_variable_probe(probe, probe_evolution, p_ind)
Np_p = [size(probe,1),size(probe,2)];
probe = probe(:,:,p_ind,:);
if length(p_ind) == 1 % in case that only single probe is called
probe = reshape(probe,prod(Np_p),[]);
probe = reshape(probe * probe_evolution', Np_p(1), Np_p(2), []);
else % in case multiple scans with unshared probe
probe_out = 0;
for ii = 1:size(probe,4)
probe_out = probe_out + probe(:,:,:,ii).*reshape(probe_evolution(:,ii),1,1,[]);
end
probe = probe_out;
end
end
@@ -0,0 +1,465 @@
/*
Set complex views to complex object
mexcuda -output +engines/+GPU/get_optimal_LSQ_step_mex +engines/+GPU/get_optimal_LSQ_step_mex.cu
*/
#include "mex.h"
#include "gpu/mxGPUArray.h"
#include <math.h>
#include <stdio.h>
#include <iostream>
#include <list>
typedef const unsigned int cuint;
typedef const uint16_T cuint16;
#define MAX_BLOCK_DIM_SIZE 65535
/*
* Device code
*/
// allocate shared memory so that all functions can see it
extern __shared__ float sdata[];
const unsigned int MAX_IND_READ = 10000;
__constant__ uint8_T gC_pind[MAX_IND_READ];
int checkLastError(char * msg)
{
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
char err[512];
sprintf(err, "get_optimal_LSQ_step_ker failed \n %s: %s. \n", msg, cudaGetErrorString(cudaStatus));
mexPrintf(err);
return 1;
}
return 0;
}
/*********** fast inplace version of LSQ step calculation *************/
template <unsigned int blockSize>
__device__ void calculate_AA_matrix( const float2 *P_f, const float2 *O_f,const float2 *dP_f, const float2 *dO_f, const float2 *chi_f,
float &AA1, float2 &AA2,float2 &AA3, float &AA4,
float &Atb1, float &Atb2 , const float lambda,
cuint Np_x, cuint Np_y, cuint Npixz, cuint idz,
cuint Nblocks, const bool single_probe, cuint id, cuint tid)
{
float2 dO, dP, O, P, chi;
// load to local memory
cuint id3 = id + Np_x*Np_y*idz;
O = O_f[id3] ;
dO = dO_f[id3] ;
chi = chi_f[id3];
if (single_probe) {
// single shared 2D probe
P = P_f[id];
dP = dP_f[id];
} else {
// unshared probe => size(dp,3) == Nscans
dP = dP_f[id + (gC_pind[idz]-1)*Np_x*Np_y];
// position in 3D array
P = P_f[id3];
}
// make auxiliary variables
float2 dOP, dPO, cdPO, cdOP;
// dOP = dO.*P;
dOP.x = dO.x * P.x - dO.y * P.y;
dOP.y = dO.y * P.x + dO.x * P.y;
// dPO = dP.*O;
dPO.x = dP.x * O.x - dP.y * O.y;
dPO.y = dP.y * O.x + dP.x * O.y;
// cdOP = conj(dOP);
cdOP.x = dOP.x;
cdOP.y = -dOP.y;
// cdPO = conj(dPO);
cdPO.x = dPO.x;
cdPO.y = -dPO.y;
// AA1 = abs(dOP).^2+lambda;
AA1 = dOP.x * dOP.x + dOP.y * dOP.y ;
// AA2 = (dOP .* cdPO);
AA2.x = dOP.x * cdPO.x - dOP.y * cdPO.y ;
AA2.y = dOP.x * cdPO.y + dOP.x * cdPO.y ;
// AA3 = conj(AA2);
AA3.x = AA2.x;
AA3.y = -AA2.y;
// AA4 = abs(dPO)^2+lambda;
AA4 = dPO.x * dPO.x + dPO.y * dPO.y ;
// Atb1 = real(cdOP .* chi);
Atb1 = cdOP.x*chi.x - cdOP.y*chi.y;
// Atb2 = real(cdPO .* chi);
Atb2 = cdPO.x*chi.x - cdPO.y*chi.y;
// add to the shared gpu memory
sdata[tid ] = AA1;
sdata[tid+ blockSize] = AA2.x;
sdata[tid+2*blockSize] = AA2.y;
sdata[tid+3*blockSize] = AA3.x;
sdata[tid+4*blockSize] = AA3.y;
sdata[tid+5*blockSize] = AA4;
sdata[tid+6*blockSize] = Atb1;
sdata[tid+7*blockSize] = Atb2;
}
template <unsigned int blockSize>
__device__ void add_to_shared_array( cuint tid, cuint offset )
{
// another loop unrolling
sdata[tid + 0*blockSize] += sdata[tid + 0*blockSize + offset];
sdata[tid + 1*blockSize] += sdata[tid + 1*blockSize + offset];
sdata[tid + 2*blockSize] += sdata[tid + 2*blockSize + offset];
sdata[tid + 3*blockSize] += sdata[tid + 3*blockSize + offset];
sdata[tid + 4*blockSize] += sdata[tid + 4*blockSize + offset];
sdata[tid + 5*blockSize] += sdata[tid + 5*blockSize + offset];
sdata[tid + 6*blockSize] += sdata[tid + 6*blockSize + offset];
sdata[tid + 7*blockSize] += sdata[tid + 7*blockSize + offset];
}
template <unsigned int blockSize>
__device__ void reduce_shared_array( cuint tid )
{
// do reduction in shared mem using unrolled loops
if (blockSize >= 1024){ if (tid < 512) { add_to_shared_array<blockSize>(tid,512); } __syncthreads(); }
if (blockSize >= 512) { if (tid < 256) { add_to_shared_array<blockSize>(tid,256); } __syncthreads(); }
if (blockSize >= 256) { if (tid < 128) { add_to_shared_array<blockSize>(tid,128); } __syncthreads(); }
if (blockSize >= 128) { if (tid < 64) { add_to_shared_array<blockSize>(tid,64); } __syncthreads(); }
// why not do the same for all
if (blockSize >= 64) { if (tid < 32) { add_to_shared_array<blockSize>(tid,32); } __syncthreads(); }
if (blockSize >= 32) { if (tid < 16) { add_to_shared_array<blockSize>(tid,16); } __syncthreads(); }
if (blockSize >= 16) { if (tid < 8) { add_to_shared_array<blockSize>(tid,8); } __syncthreads(); }
if (blockSize >= 8) { if (tid < 4) { add_to_shared_array<blockSize>(tid,4); } __syncthreads(); }
if (blockSize >= 4) { if (tid < 2) { add_to_shared_array<blockSize>(tid,2); } __syncthreads(); }
if (blockSize >= 2) { if (tid < 1) { add_to_shared_array<blockSize>(tid,1); } __syncthreads(); }
}
// fast kernel for estimation of optimal probe and object steps
template <unsigned int blockSize>
__global__ void get_optimal_LSQ_step_ker( float2 const * P_f, float2 const * O_f,float2 const * dP_f, float2 const * dO_f,
float2 const * chi_f, const float lambda,
float2 * AA, float * Atb, cuint Np_x,cuint Np_y, cuint Npixz, cuint Nblocks, const bool single_probe) {
const mwSize tid = threadIdx.x;
// do only every second block
//cuint i = blockIdx.x*(blockSize*2) + threadIdx.x;
const mwSize i = blockIdx.x*(blockDim.x) + threadIdx.x;
const mwSize N2 = Np_x*Np_y;
mwSize AA_page_id, Atb_page_id;
float2 AA2, AA3;
float AA1, AA4, Atb1, Atb2;
for(int n = 0; n < 8; n++)
sdata[tid + n*blockSize ] = 0 ;
if(i < N2)
{
//for(int n = 0; n < 8*blockSize; n++)
// sdata[n] = 0 ;
// Page in a 3D matrix
for(int idz = 0; idz < Npixz; idz++)
{
unsigned int ii = i ;
// empty the share memory
for(int n = 0; n < 8; n++)
sdata[tid + n*blockSize ] = 0 ;
// get coeficients for the AA matrix + right size Atb vector and add them to the shared array
calculate_AA_matrix<blockSize>(P_f ,O_f ,dP_f ,dO_f ,chi_f ,
AA1, AA2, AA3, AA4, Atb1, Atb2, lambda, Np_x, Np_y, Npixz, idz, Nblocks,single_probe, ii, tid);
__syncthreads();
// reduce the shared memory data
reduce_shared_array<blockSize>( tid );
// write result for this block to global mem
if (tid == 0) {
// store data to the AA matrix
AA_page_id = 4*idz + 4*Npixz*blockIdx.x;
Atb_page_id = 2*idz + 2*Npixz*blockIdx.x;
AA[ 0 + AA_page_id].x = sdata[0*blockSize];
AA[ 0 + AA_page_id].y = 0; // needs to be set to zero or initalized to zero when created
AA[ 1 + AA_page_id].x = sdata[1*blockSize];
AA[ 1 + AA_page_id].y = sdata[2*blockSize];
AA[ 2 + AA_page_id].x = sdata[3*blockSize];
AA[ 2 + AA_page_id].y = sdata[4*blockSize];
AA[ 3 + AA_page_id].x = sdata[5*blockSize];
AA[ 3 + AA_page_id].y = 0;
Atb[ 0 + Atb_page_id] = sdata[6*blockSize];
Atb[ 1 + Atb_page_id] = sdata[7*blockSize];
}
}
}
}
unsigned int nextPow2( unsigned int x ) {
--x;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return ++x;
}
void getNumBlocksAndThreads(int n, int maxBlocks, int maxThreads, int &blocks, int &threads)
{
threads = (n < maxThreads*2) ? nextPow2((n + 1)/ 2) : maxThreads;
blocks = (n + (threads * 2 - 1)) / (threads * 2);
blocks = min(maxBlocks, blocks);
}
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
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 != 7)
mexErrMsgTxt("Seven input arguments required");
const mxGPUArray * m_chi = mxGPUCreateFromMxArray(prhs[0]);
if ((mxGPUGetClassID(m_chi) != mxSINGLE_CLASS) || (mxGPUGetComplexity(m_chi) != mxCOMPLEX)) {
mexPrintf("m_chi\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_chi = (float2 *)mxGPUGetDataReadOnly(m_chi);
const mxGPUArray * m_dO = mxGPUCreateFromMxArray(prhs[1]);
if ((mxGPUGetClassID(m_dO) != mxSINGLE_CLASS) || (mxGPUGetComplexity(m_dO) != mxCOMPLEX)) {
mexPrintf("m_dO\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_dO = (float2 *)mxGPUGetDataReadOnly(m_dO);
const mxGPUArray * m_dP = mxGPUCreateFromMxArray(prhs[2]);
if ((mxGPUGetClassID(m_dP) != mxSINGLE_CLASS) || (mxGPUGetComplexity(m_dP) != mxCOMPLEX)) {
mexPrintf("m_dP\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_dP = (float2 *)mxGPUGetDataReadOnly(m_dP);
const mxGPUArray * m_O = mxGPUCreateFromMxArray(prhs[3]);
if ((mxGPUGetClassID(m_O) != mxSINGLE_CLASS) || (mxGPUGetComplexity(m_O) != mxCOMPLEX)) {
mexPrintf("m_O\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_O = (float2 *)mxGPUGetDataReadOnly(m_O);
const mxGPUArray * m_P = mxGPUCreateFromMxArray(prhs[4]);
if ((mxGPUGetClassID(m_P) != mxSINGLE_CLASS) || (mxGPUGetComplexity(m_P) != mxCOMPLEX)) {
mexPrintf("m_P\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_P = (float2 *)mxGPUGetDataReadOnly(m_P);
const mxGPUArray * m_P_ind = mxGPUCreateFromMxArray(prhs[6]);
if (mxGPUGetClassID(m_P_ind) != mxUINT8_CLASS) {
mexPrintf("m_P_ind class %i\n", mxGPUGetClassID(m_P_ind));
mexErrMsgIdAndTxt(errId, errMsg);
}
const uint8_T * p_P_ind = (uint8_T *)mxGPUGetDataReadOnly(m_P_ind);
const unsigned int Npos = mxGPUGetNumberOfElements(m_P_ind);
if (Npos > MAX_IND_READ) {
mexErrMsgIdAndTxt(errId, "Maximal size of input block exceeded");
}
// Get dimension of probe and object
const unsigned int Ndims = (unsigned int)mxGPUGetNumberOfDimensions(m_chi);
if (Ndims != 3) {
mexErrMsgIdAndTxt(errId, "Inputs has to be 3 dimensional\n");
}
const mwSize * Npix = mxGPUGetDimensions(m_chi);
const mwSize * Npix_probe = mxGPUGetDimensions(m_P);
const mwSize * Npix_probe_upd = mxGPUGetDimensions(m_dP);
const mwSize Ndims_probe = mxGPUGetNumberOfDimensions(m_P);
const mwSize Ndims_probe_upd = mxGPUGetNumberOfDimensions(m_dP);
if ((Npix[2] != Npos)) {
mexErrMsgIdAndTxt(errId, "Number of probe indices has to match size of inputs (%i vs %i) \n", Npix[2], Npos);
}
if ((Npix_probe[2] != Npix[2]) && (Ndims_probe != 2)) {
mexErrMsgIdAndTxt(errId, "Dimension of probe has to match size of inputs (%i vs %i) \n", Npix_probe[2], Npix[2]);
}
float lambda = mxGetScalar(prhs[5]);
const bool single_probe =Ndims_probe == 2 ;
cudaMemcpyToSymbol(gC_pind, p_P_ind, Npos*sizeof(uint8_T), 0, cudaMemcpyHostToDevice);
checkLastError("after cudaMemcpyToSymbol pos");
// Choose a reasonably sized number of threads in each dimension for the block.
int maxThreads = 1024; // number of threads per block, does not work with 1024, I dont know why
int threads = 0, blocks = 0;
cuint n = Npix[0]*Npix[1];
threads = (n < maxThreads) ? nextPow2((n + 1)/ 2) : maxThreads;
blocks = (n + (threads - 1)) / (threads );
dim3 dimBlock(threads, 1, 1);
dim3 dimGrid(blocks, 1, 1);
// allocation size needed for shared GPU memory , it needs to reduce 2 float2 elements and 4 float elements
int smemSize = 8* threads * sizeof(float);
//mexPrintf("threads %i blocks %i smemSize %i \n", threads, blocks, smemSize);
// allocate output fields
mwSize matrix_size[4] = {2,2,Npix[2],blocks};
mxGPUArray * m_AA = mxGPUCreateGPUArray(
4,
matrix_size,
mxSINGLE_CLASS,
mxCOMPLEX,
MX_GPU_DO_NOT_INITIALIZE); // MX_GPU_DO_NOT_INITIALIZE , MX_GPU_INITIALIZE_VALUES
float2 * p_AA = (float2 *)mxGPUGetData(m_AA);
mwSize vector_size[4] = {2,1,Npix[2],blocks};
mxGPUArray * m_Atb = mxGPUCreateGPUArray(
4,
vector_size,
mxSINGLE_CLASS,
mxREAL,
MX_GPU_DO_NOT_INITIALIZE);
float * p_Atb = (float *)mxGPUGetData(m_Atb);
checkLastError("after dimThread");
switch (threads)
{
case 1024:
get_optimal_LSQ_step_ker< 1024><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 512:
get_optimal_LSQ_step_ker< 512><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 256:
get_optimal_LSQ_step_ker< 256><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 128:
get_optimal_LSQ_step_ker< 128><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 64:
get_optimal_LSQ_step_ker< 64><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 32:
get_optimal_LSQ_step_ker< 32><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 16:
get_optimal_LSQ_step_ker< 16><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 8:
get_optimal_LSQ_step_ker< 8><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 4:
get_optimal_LSQ_step_ker< 4><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 2:
get_optimal_LSQ_step_ker< 2><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
case 1:
get_optimal_LSQ_step_ker< 1><<< dimGrid, dimBlock, smemSize>>>( p_P, p_O,p_dP, p_dO, p_chi, lambda,
p_AA, p_Atb, Npix[0], Npix[1], Npix[2], blocks, single_probe);
break;
}
checkLastError("after kernel");
cudaThreadSynchronize();
checkLastError("after kernel");
// Wrap the result up as a MATLAB gpuArray for return.
plhs[0] = mxGPUCreateMxArrayOnGPU(m_AA);
plhs[1] = mxGPUCreateMxArrayOnGPU(m_Atb);
mxGPUDestroyGPUArray(m_P);
mxGPUDestroyGPUArray(m_O);
mxGPUDestroyGPUArray(m_dP);
mxGPUDestroyGPUArray(m_dO);
mxGPUDestroyGPUArray(m_chi);
mxGPUDestroyGPUArray(m_AA);
mxGPUDestroyGPUArray(m_Atb);
return;
}
@@ -0,0 +1,136 @@
% REFINE_OBJECT_UPDATE calculate improved update direction
% apply "overlap" constraint to get better estimate of the update direction
%
% [object_upd_sum,object_update_proj, cache] = ...
% refine_object_update(self, object_update_proj,object_upd_sum,layer_ids,scan_ids,g_ind,par, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** object_update_proj [Nx, Ny, N] array, estimate of the object update for each scan position, ie conj(P)*chi
% ** object_upd_sum cell of object sized arrays containg previous optimal updates
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** layer_ids id of the solved layer for multilayer ptycho
% ** scan_ids determines to which scan correponds each of the position
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ object_upd_sum cell of object sized arrays containg updated optimal update
% ++ object_update_proj [Nx, Ny, N] array, estimate of the refiend object update for each scan position,
% ++ cache structure with precalculated values to avoid unnecessary overhead
%
%
% see also: engines.GPU_MS.LSQML
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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_upd_sum,object_update_proj, cache] = ...
refine_object_update(self, object_update_proj,object_upd_sum,layer_ids,scan_ids,g_ind,par, cache)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
if par.share_object
obj_ids = 1;
else
obj_ids = unique(scan_ids);
end
if ~isinf(self.z_distance)
% only in nearfield mode , apply shift in the opposite direction
object_update_proj = apply_subpx_shift(object_update_proj .* cache.apodwin, self.modes{1}.sub_px_shift(g_ind,:) ) ./ cache.apodwin;
end
% calculate update direction
% apply "overlap" constraint to get better estimate
% of the update directin
if is_method(par, 'MLs')
for ll_tmp = obj_ids; object_upd_sum{ll_tmp,layer_ids}(:) = eps*1i; end
end
if par.delta_p == 0 % || par.Nlayers > 1 % layer_ids > 1
%no preconditioner as in the original ML method
object_upd_sum = set_views(object_upd_sum,object_update_proj,layer_ids,obj_ids, g_ind, cache, scan_ids);
%plotting.smart_figure(1231)
%plotting.imagesc3D(object_upd_sum{1,layer_ids})
%drawnow
object_update_proj = get_views(object_upd_sum,object_update_proj,layer_ids,obj_ids, g_ind, cache, scan_ids);
elseif par.delta_p > 0
% damped LSQ method (preconditioned update)
object_upd_sum = set_views(object_upd_sum,object_update_proj,layer_ids,obj_ids, g_ind, cache, scan_ids);
for ll_tmp = obj_ids
object_upd_precond{ll_tmp,1} = Gfun(@object_sum_update_Gfun, object_upd_sum{ll_tmp,layer_ids}, cache.illum_sum_0{ll_tmp},cache.MAX_ILLUM(ll_tmp)*(par.delta_p));
end
object_update_proj = get_views(object_upd_precond,object_update_proj,1,obj_ids, g_ind, cache, scan_ids);
if is_method(par, 'MLs')
object_upd_sum(:,layer_ids) = object_upd_precond(obj_ids);
end
else
error('Unimplemented option')
end
end
function object_upd_sum = object_sum_update_Gfun(object_upd_sum, obj_illum_sq_sum, max)
% final update is just weighted mean of the updared object views
object_upd_sum = object_upd_sum ./ sqrt(obj_illum_sq_sum.^2+ max.^2);
end
@@ -0,0 +1,139 @@
% REFINE_PROBE_UPDATE calculate improved update direction
% apply "overlap" constraint to get better estimate of the update direction
% also updates the variable probe estimate
%
% [ self,m_probe_update, probe_update, cache] = ...
% refine_probe_update(self, obj_proj, probe_update, chi,layer_ids,probe_id,p_ind,g_ind, par, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** obj_proj [Nx, Ny, N] array, estimate of the object update for each scan position, ie conj(P)*chi
% ** probe_update cell of object sized arrays containg previous optimal updates
% ** chi indices corresponding to the current group that is solved in parallel
% ** layer_ids id of the solved layer for multilayer ptycho
% ** probe_id id of incoherent probe mode
% ** p_ind indices containg corresponding probe id for each processed position
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ object_upd_sum cell of object sized arrays containg updated optimal update
% ++ object_update_proj [Nx, Ny, N] array, estimate of the refiend object update for each scan position,
% ++ cache structure with precalculated values to avoid unnecessary overhead
%
%
% see also: engines.GPU_MS.LSQML
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [ self,m_probe_update, probe_update, cache] = ...
refine_probe_update(self, obj_proj, probe_update, chi,layer_ids,probe_id,p_ind,g_ind, par, cache)
% get probe update direction
import engines.GPU_MS.shared.*
import engines.GPU_MS.LSQML.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
if layer_ids > 1 % in case of multilayer object
m_probe_update = [];
return
end
if (probe_id == 1 && par.apply_subpix_shift && isinf(self.z_distance(end))) || is_used(par,'fly_scan')
probe_update = apply_subpx_shift(probe_update , -self.modes{min(end,probe_id)}.sub_px_shift(g_ind,:) );
end
if probe_id == 1
probe_update = apply_subpx_shift_fft(probe_update, -self.modes{min(end,probe_id)}.probe_fourier_shift(g_ind,:));
end
if par.share_probe || length(unique(p_ind)) == 1
% BETTER WAY: assume that sum(|obj_proj|^2,3) is close to 1
% and additionally use weighting based on confidence given by illum_sum_0
% => apriory weighting giving less importance to the less
% illuminated regions
%weight_proj = cache.illum_sum_0{1} ./ (cache.illum_sum_0{1}+0.01*cache.MAX_ILLUM(1));
%weight_proj = get_views({weight_proj},[],1,1, g_ind, cache);
%m_probe_update = mean( weight_proj.* probe_update,3);
% or originally was used simple average , good for object >> probe
m_probe_update = mean(probe_update,3); % calculate single update for all current positions
else % unshared probe and multiple scans in one group (ie shared object)
probe_ids = unique(p_ind);
m_probe_update = Gzeros([self.Np_p,length(probe_ids)], true);
for probe_id =probe_ids(:)'
m_probe_update(:,:,probe_id) = mean(probe_update(:,:,p_ind == probe_id),3); % calculate one update for each scan
end
end
if (par.variable_probe || par.variable_intensity) && probe_id == 1
% ORTHOGONAL PROBE RELAXATION (OPRP) EXTENSION - allow
% variable probe wavefront
% Odstrcil, M., et al. "Ptychographic coherent diffractive imaging with orthogonal probe relaxation." Optics express 24.8 (2016): 8360-8369.
% iterate over all sub probes
[self.probe{probe_id}, self.probe_evolution] = ...
update_variable_probe(self, self.probe{probe_id}, self.probe_evolution, m_probe_update,probe_update, obj_proj, chi,cache.illum_sum_0{probe_id}, p_ind, g_ind, cache, par);
end
% % apply probe constraints
% if probe_id == 1 && (check_option(self,'probe_support') || check_option(self,'probe_support_fft'))
% m_probe_update = apply_probe_contraints(m_probe_update, self.modes{probe_id});
% end
end
@@ -0,0 +1,162 @@
% UPDATE_OBJECT calculate improved update direction
% apply "overlap" constraint to get better estimate of the update direction
%
% [object, object_upd_sum] = update_object(self, object, object_upd_sum, layer,object_ids, g_ind, scan_ids, par, cache, beta_object)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** object cell of object arrays
% ** object_upd_sum cell of object sized arrays containg previous optimal updates
% ** layer_ids id of the solved layer for multilayer ptycho
% ** object_ids id of incoherent object mode (not implemented)
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** scan_ids determines to which scan correponds each of the position
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** beta_object (scalar) relaxation parameter of the update step
%
% returns:
% ++ object cell of object arrays, after update
% ++ object_upd_sum cell of object sized arrays containg updated optimal update
%
%
% see also: engines.GPU_MS.LSQML
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 foobject_idswing 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 foobject_idswing 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, object_upd_sum] = update_object(self, object, object_upd_sum, layer,object_ids, g_ind, scan_ids, par, cache, beta_object)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
if par.share_object
obj_ids = 1;
else
obj_ids = unique([scan_ids{:}]);
end
% take single optimal value, works well for most of samples
% it is possible to use different weighting for each scan position but
% it may become less stable in some cases -> robusness is preferred
% in case of the MLc method take minimum of the LSQ updates from all
% subsets
if is_method(par, 'MLc')
% preconditioner should be applied on the total sum of all object_upd_sum
if par.delta_p > 0 % && par.Nlayers == 1
for kk = obj_ids
object_upd_sum{kk,layer} = Gfun(@object_sum_update_Gfun, object_upd_sum{kk,layer}, cache.illum_sum_0{kk},cache.MAX_ILLUM(kk)*(par.delta_p));
end
end
end
% calculate optimal step, apply at least different step for each object (scan)
for i = 1:length(g_ind)
for kk = obj_ids
ind = g_ind{i}(scan_ids{i} == kk);
if isempty(ind)
beta_object_avg(i,kk) = nan;
else
beta_object_avg(i,kk) = trimmean(beta_object(ind, layer), 10); %20240929 SK Edit
end
end
end
% take the most pesimistic estimate of the per object
beta_object_avg=nanmin(beta_object_avg,[],1);
% update each of the objects separately
for kk = obj_ids
if beta_object_avg(kk) > 0
object_upd_sum{kk,layer} = object_upd_sum{kk,layer}*beta_object_avg(kk);
object{kk,layer} = object{kk,layer}+object_upd_sum{kk,layer};
end
end
if verbose()> 3
% show applied subsets (update amplitude) and probe update amplitude
plotting.smart_figure(11)
Nobj = size(object_upd_sum,1);
for ll = 1:Nobj
subplot(Nobj,2,1+Nobj*(ll-1))
cla()
o = object_upd_sum{ll,layer}(cache.object_ROI{:});
o = min(abs(o), quantile(abs(o(:)), 0.999)) .* o ./ abs(o);
plotting.imagesc3D(o);
axis image xy off
hold all
fprintf('Object update norm: %g\n', norm2(object_upd_sum{ll,layer}(cache.object_ROI{:})))
% try
% for k = 1:length(g_ind)
% for i = unique(scan_ids{k})
% plot(self.probe_positions_0(g_ind{k}(scan_ids{k}==i),1)+self.Np_o(2)/2,self.probe_positions_0(g_ind{k}(scan_ids{k}==i),2)+self.Np_o(1)/2, '.')
% end
% end
% end
end
title('Object update')
end
end
function object_upd_sum = object_sum_update_Gfun(object_upd_sum, obj_illum_sq_sum, max)
% final update is just weighted mean of the updared object views
object_upd_sum = object_upd_sum ./ sqrt(obj_illum_sq_sum.^2+ max.^2);
end
@@ -0,0 +1,125 @@
% UPDATE_PROBE calculate improved update direction
% apply "overlap" constraint to get better estimate of the update direction
%
% probe = update_probe(probe, m_probe_update, par, p_ind, g_ind, beta_probe, Nind)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** object_update_proj [Nx, Ny, N] array, estimate of the object update for each scan position, ie conj(P)*chi
% ** object_upd_sum cell of object sized arrays containg previous optimal updates
% ** p_ind indices containg corresponding probe id for each processed position
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** beta_probe (scalar) relaxation parameter of the update step
% ** Nind (int) number of groups that are solved serially
% returns:
% ++ probe cell of the updated probes
%
%
% see also: engines.GPU_MS.LSQML
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 probe = update_probe(probe, m_probe_update, par, p_ind, g_ind, beta_probe, Nind)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import utils.verbose
%% update probe
beta_probe = beta_probe(g_ind);
probe_ids = unique(p_ind);
if is_method(par, 'MLc')
beta_probe = beta_probe / Nind; % in order to make the compact version closer to original ML method, the accumulated probe step per iteration should be 1
end
if (par.share_probe || par.Nscans == 1) && size(probe,3)==1
% most simple case, no multiprobe needed
probe = probe + m_probe_update .* mean(beta_probe);
elseif length(probe_ids) == 1
% variable probe extension with shared probe
probe(:,:,probe_ids,1) = probe(:,:,probe_ids,1) + m_probe_update .* mean(beta_probe);
else % unshared probe
% update each of the probes separately
for id = probe_ids(:)'
ind = p_ind == id;
beta = mean(beta_probe(ind));
probe(:,:,id,1) = Gfun(@upd_probe_Gfun, probe(:,:,id,1),m_probe_update(:,:,min(end,id)), beta);
end
end
if verbose()> 3
% show applied subsets (update amplitude) and probe update
% amplitude
plotting.smart_figure(11)
probe_modes = size(m_probe_update,3);
for ll = 1:probe_modes
subplot(probe_modes,2,2+probe_modes*(ll-1))
p = fftshift(fft2(fftshift(m_probe_update(:,:,ll))));
p = min(abs(p), quantile(abs(p(:)), 0.999)) .* p ./ abs(p);
plotting.imagesc3D(p);
axis off image xy
end
title('Probe update')
colormap bone
drawnow
end
end
function probe = upd_probe_Gfun(probe,probe_update, alpha_p)
probe = probe + alpha_p.*probe_update;
end
@@ -0,0 +1,224 @@
% UPDATE_VARIABLE_PROBE approximation of the OPRP method to get only the first orthogonal
% vector describing the probe
%
% [probe, probe_evolution] = ...
% update_variable_probe(self,probe, probe_evolution, m_probe_update,probe_update, obj_proj, chi, weights, p_ind, g_ind, cache, par)
%
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** probe [Nx,Nx,probe_modes,variable_modes] variable probe modes
% ** probe_evolution [Npos,variable_modes] array containing evolution of the varaible modes for each position
% ** m_probe_update precalculated value of mean(dP,3)
% ** probe_update probe update, ie conj(O)*chi
% ** obj_proj [Nx,Ny,N] array, views of the object for each scan position
% ** chi [Nx,Ny,N] array, difference between original and updated exit-wave
% ** weights array of relaxation values for object pixel, reduce weight of regions with weak illumination in the variable probe calculation
% ** p_ind indices containg corresponding probe id for each processed position
% ** g_ind indices corresponding to the current group that is solved in parallel
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** par structure containing parameters for the engines
%
% returns:
% ++ probe [Nx,Nx,probe_modes,variable_modes] updated variable modes
% ++ probe_evolution [Npos,variable_modes] updated array containing evolution of the varaible modes for each position
%
%
% see also: engines.GPU_MS.LSQML
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [probe, probe_evolution] = ...
update_variable_probe(self,probe, probe_evolution, m_probe_update,probe_update, obj_proj, chi, weights, p_ind, g_ind, cache, par)
import math.*
import plotting.*
import engines.GPU_MS.GPU_wrapper.*
uids = unique(p_ind);
for kk = uids
if length(uids) == 1 % single or shared probe between the scans
% avoid memory copy is possible
probe_update_tmp = probe_update;
chi_tmp = chi;
obj_proj_tmp = obj_proj;
g_ind_tmp = g_ind;
block_size = length(g_ind);
else
% otherwise
block_ind = p_ind == kk;
probe_update_tmp = probe_update(:,:,block_ind);
obj_proj_tmp = obj_proj(:,:,block_ind);
chi_tmp = chi(:,:,block_ind);
g_ind_tmp = g_ind(block_ind);
block_size= sum(block_ind);
end
if par.variable_probe
% use some relaxation to avoid too faster changes
relax_U = min(0.1,block_size/self.Npos);
relax_V = 1;
% make probe_update_tmp orthogonal to the average update
probe_update_tmp = probe_update_tmp - m_probe_update(:,:,min(end,kk));
for ii = 1:par.variable_probe_modes
var_probe = probe(:,:,kk,1+ii);
probe_evol = probe_evolution(g_ind_tmp,1+ii); % evolution of the 1th SVD mode coeficient
[var_probe,probe_evol,probe_update_tmp] = get_first_SVD_mode(probe_update_tmp, var_probe, probe_evol,relax_U,relax_V, obj_proj_tmp, chi_tmp);
if ii < par.variable_probe_modes
% subtract projection of the updated var_probe from the
% probe_update_tmp to enforce orthogonality between the
% modes
projection = sum2(probe_update_tmp .* conj(var_probe)) ./ sum2(abs(var_probe).^2);
probe_update_tmp = probe_update_tmp - projection .* var_probe;
end
% return the updated vector to the probe array
probe(:,:,kk,1+ii) = var_probe;
probe_evolution(g_ind_tmp,1+ii) = probe_evol;
end
end
if par. variable_intensity
% correction to account for variable intensity
mean_probe = probe(:,:,kk,1);
% compare P*0 and chi to estimate best update of the intensity
[nom, denom] = Gfun(@get_coefs_intensity,chi_tmp, mean_probe, obj_proj_tmp);
probe_evolution(g_ind_tmp,1) = probe_evolution(g_ind_tmp,1) + 0.1* squeeze(Ggather(sum2(nom)./ sum2(denom)));
end
end
if any(g_ind==1) && utils.verbose() > 3
self.probe{1} = probe;
self.probe_evolution = probe_evolution;
plot_variable_probe(self, par)
drawnow
end
end
function [var_probe,probe_evol, probe_update] = get_first_SVD_mode(probe_update, var_probe, probe_evol, relax_U,relax_V, obj_proj, chi)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import plotting.*
% get a weighting function => avoid effect of too strong noise
% around edges of the reconstructed region => improve robustness againts outliers
% weights = weights / max2(weights);
% weight_proj = get_views( weights, Gzeros(size(chi)),1,1, g_ind, cache);
weight_proj = 1;
%% calculate terms needed to calculate update of the variable probe
% => U term in SVD decomposition
[resid, proj, probe_update] = Gfun(@get_SVD_update,probe_update, weight_proj, var_probe, reshape(probe_evol,1,1,[]), norm(probe_evol));
% get update the variable probe
var_probe_upd = mean( resid .* mean2(proj), 3);
% apply update, prevent too large changes at the beginning of the covergence
var_probe = var_probe + relax_U*var_probe_upd / norm2(var_probe_upd);
%% equivalent but much slower code
%[U,S,V] = svd(reshape(weight_proj.*(probe_update - m_probe_update), prod(self.Np_p),[]), 0);
%var_probe = var_probe + relax_U*reshape(U(:,1), self.Np_p);
% keep the eigenprobe normalized
var_probe = var_probe ./ norm2(var_probe);
%% calculate optimal OPRP evolution coeficients
[num, denum] = Gfun(@get_SVD_evol,var_probe, obj_proj, chi);
num =Ggather(mean2(num));
denum = Ggather(mean2(denum));
% perform relaxed update => improve robustness againts outliers
probe_evol_upd = squeeze(num ./ (denum + 0.1*mean(denum,3))); % caclulate regularized update
% add to the coefficients that are already used in the currently
% used probe{1} variable
probe_evol = probe_evol + relax_V*probe_evol_upd;
end
%% GPU kernel merging
% SVD approximation => calculation of U
function [resid, proj, probe_update] = get_SVD_update(probe_update, weight_proj, var_probe, probe_evol, probe_evol_norm)
resid = weight_proj .* probe_update;
proj = (real(conj(resid) .* var_probe)+ probe_evol) / probe_evol_norm^2;
end
% SVD approximation => calculation of S*V
function [num, denum] = get_SVD_evol(var_probe, obj_proj, chi)
psi = var_probe .* obj_proj;
denum = abs( psi ).^2;
num = real(chi .* conj(psi));
end
function [nom1, denom1] = get_coefs_intensity(xi, P, O)
OP = O.*P;
nom1 = real(conj(OP) .* xi);
denom1 = abs(OP).^2;
end
@@ -0,0 +1,123 @@
% APPLY_SVD_FILTER The core of the variable probe (OPRP) code:
% find SVD decomposition and limit the probe into several orthogonal
% modes
% additional prior knowledge can be also included
%
% [probe, probe_evolution] = apply_SVD_filter(probe, Nmodes, mode)
%
% ** probe [Nx,Ny,N] variable probe for each position
% ** Nmodes (int) number of variable modes
% ** mode structure containing parameters for selected probe mode
% returns
% ++ probe [Nx,Ny,variable_modes] variable probe modes
% ++ probe_evolution [Npos,variable_modes] updated array containing evolution of the varaible modes for each position
%
% see also: engines.GPU_MS.PIE
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [probe, probe_evolution] = apply_SVD_filter(probe, Nmodes, mode)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import math.*
import utils.*
Np = size(probe);
[U,S,V] = fsvd(reshape((probe),[],Np(3)) ,Nmodes);
% if any(diag(S.^2)/sum(diag(S.^2)) < 1e-3) % 2e-3 is the weakest that FSVD can recover
% try
% warning('Running full SVD (maybe use less OPR modes) (weak modes %i/%i) ', Ggather(sum(diag(S.^2)/sum(diag(S.^2)) < 2e-3)), Nmodes)
% [U,S,V] = svd(reshape((probe),[],Np(3)) ,0);
% U = single(U(:,1:Nmodes));
% S = single(S(1:Nmodes,1:Nmodes));
% V = single(V(:,1:Nmodes));
% catch
% keyboard
% end
% end
U = reshape(U, Np(1),Np(2),1,[]);
U = apply_probe_contraints(U, mode);
V(:,1) = mean(V(:,1)) + 0.99*(V(:,1) - mean(V(:,1)));
V(:,2:end) = mean(V(:,2:end)) + 0.99*(V(:,2:end) - mean(V(:,2:end)));
%% remove outliers
aV = abs(V);
MAX = quantile(aV,0.99);
V = min(aV, MAX) .* (V ./ (aV+1e-3));
probe_evolution = (S*V').';
probe = U;
avg = mean(abs(probe_evolution(:,1)),1);
probe = probe*avg;
probe_evolution = probe_evolution / avg;
end
@@ -0,0 +1,329 @@
% FOURIER_RING_CORRELATION simplified but faster version of the FRC code
%
% [score, object] = fourier_ring_correlation(object_1, object_2, varargin)
%
% ** object_1 array reconstructed object
% ** object_2 array reconstructed object from an independend scan
% ** varargin see code for more details
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [score, object] = fourier_ring_correlation(object_1, object_2, varargin)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
import plotting.*
par = inputParser;
par.addParameter('px_scale', 1 , @isnumeric )
par.addParameter('auto_crop', false, @islogical )
par.addParameter('plot_results', true , @islogical ) % use white background
par.addParameter('smoothing', 0 , @isnumeric ) % smooth over N pixels
par.addParameter('Nrings', 20 , @isnumeric ) % smooth over N pixels
par.addParameter('crop', 0 , @isnumeric ) % crop image by N pixels
par.addParameter('flip_horizontal', false , @islogical ) % flip second image horizontally
par.addParameter('fft_phase_removal_guess', false , @islogical ) % flip second image horizontally
par.addParameter('weights', {} , @iscell ) % cell array of weights
par.addParameter('find_shift', true, @islogical ) % cell array of weights
par.parse(varargin{:})
r = par.Results;
if r.flip_horizontal
object_2 = fliplr(object_2);
end
Npix = min(size(object_1), size(object_2));
object = {object_1, object_2};
if r.crop > 0
for ii = 1:2
object{ii} = crop_pad(object{ii}, Npix-r.crop);
end
if ~isempty(r.weights)
for ii = 1:2
r.weights{ii} = crop_pad(r.weights{ii}, Npix-r.crop);
end
end
end
Npix = min(size(object{1}), size(object{2}));
for ii = 1:2
object{ii} = object{ii} / mean(abs(object{1}(:)) );
W{ii} = tukeywin(Npix(1), 0.2) .* tukeywin(Npix(2),0.2)';
if ~isempty(r.weights)
W{ii} = W{ii} .* single(r.weights{ii});
end
end
score.shift = [0,0];
if r.find_shift
for kk = 1:4
Npix = size(object{1});
[X,Y] = meshgrid(-Npix(2)/2+1:Npix(2)/2,-Npix(1)/2+1:Npix(1)/2);
object{1} = utils.stabilize_phase(object{1}, object{2}, 'fourier_guess', r.fft_phase_removal_guess);
for ii = 1:2
phasor{ii} = object{ii} ./ (abs(object{ii}) + 1e-3*mean(abs(object{ii}(:))));
fobject{ii} = fft2(single(W{ii}.*(phasor{ii}-mean(phasor{ii}(:)))));
end
% high pass filter
Wf = Garray(fftshift(exp(- 1./ ((X.^2+Y.^2)/(Npix(1)/50)^2))));
[output] = utils.dftregistration( Wf.* fobject{1}, Wf.* fobject{2},100);
object{2} = imshift_fft(object{2}, output(4), output(3));
ROI = { (1+max(0,ceil(output(3)))):(Npix(1)+min(0, floor(output(3)))) , ...
(1+max(0,ceil(output(4)))):(Npix(2)+min(0, floor(output(4))))};
object{1} = object{1}(ROI{:});
object{2} = object{2}(ROI{:});
for j = 1:2
W{j} = W{j}(ROI{:});
end
verbose(3,'Image shifted by %g %g px', output([4,3]))
score.shift = score.shift + Ggather([output(4), output(3)]);
% subplot(1,2,1)
% plotting.imagesc3D(object{1}); axis off image
% subplot(1,2,2)
% plotting.imagesc3D(object{2}); axis off image
% drawnow
if all(abs(output(3:4)) < 0.5)
break
end
end
end
Npix = size(object{1});
[object{1}] = utils.stabilize_phase(object{1}, object{2}, abs(object{2}), 'binning', 4 , 'fourier_guess', r.fft_phase_removal_guess);
if r.flip_horizontal
score.shift(1) = -score.shift(1);
end
for ii = 1:2
object{ii} = object{ii} ./ mean(abs(object{ii}(:)));
end
W = sqrt(W{1} .* W{2});
ROI_compare = get_ROI(W>0.1*max(W(:))); % compare only the reliable ROIs
W = tukeywin(length(ROI_compare{1}),0.2) .* tukeywin(length(ROI_compare{2}),0.2)';
for ii = 1:2
fobject{ii} = fft2(W.*object{ii}(ROI_compare{:}));
end
for ii = 1:2
fobject{ii} = fftshift(fobject{ii});
fobject_norm{ii} = abs(fobject{ii}).^2;
end
fcorr = fobject{1} .* conj(fobject{2});
binning = ceil(Npix/2 / r.Nrings);
fcorr = conv2(fcorr, ones(binning) / prod(binning), 'same');
fcorr = fcorr(1:binning(1):end, 1:binning(2):end);
for ii = 1:2
fobject_norm{ii} = conv2(fobject_norm{ii}, ones(binning) / prod(binning), 'same');
fobject_norm{ii} = fobject_norm{ii}(1:binning(1):end, 1:binning(2):end);
end
Npix= size(fcorr);
x = single(-Npix(2)/2+0.5:Npix(2)/2-0.5)/(Npix(2)/2);
y = single(-Npix(1)/2+0.5:Npix(1)/2-0.5)/(Npix(1)/2);
if length(r.px_scale) > 1 && r.px_scale(1) > r.px_scale(2)
y = y .* r.px_scale(2) / r.px_scale(1);
elseif length(r.px_scale) > 1 && r.px_scale(1) < r.px_scale(2)
x = x .* r.px_scale(1) / r.px_scale(2);
end
[X,Y] = meshgrid(x, y);
R_mat = sqrt(X.^2 + Y.^2);
Rmax = 0.98;
R0 = 0.01;
R_all = linspace(R0, Rmax, min(r.Nrings, min(Npix)));
for ii = 1:(length(R_all)-1)
R = R_all(ii);
ring = find((R_mat > R_all(ii)) & (R_mat < R_all(ii+1)));
fcorr_values{1}(ii) = abs(sum(fcorr(ring)) ./ sqrt(sum(fobject_norm{1}(ring)) .* sum(fobject_norm{2}(ring))));
n_values(ii) = length(ring); % sum(ring(:));
end
spatial_freq = R_all(1:end-1);
n_values = n_values .* prod(binning);
% 1-bit curve
T = (0.5+2.41./sqrt(n_values)) ./ (1.5+1.41./sqrt(n_values));
% 1/2 bit curve
% T = (0.21+1.91 ./sqrt(n_values)) ./ (1.21+0.91./sqrt(n_values));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fcorr_values{1} = Ggather(fcorr_values{1});
AUC = nanmean(fcorr_values{1}); % area undear curve criterion
score.AUC = AUC;
score.thresh = T;
score.FRC = fcorr_values{1};
score.spatial_freq = spatial_freq;
score.SSNR = 2 * score.FRC ./ (1-score.FRC);
score.SNR_avg = nansum(score.SSNR .* spatial_freq) / sum(spatial_freq);
Ts = smooth(T);
if r.smoothing > 0
score.FRC = imgaussfilt(score.FRC,r.smoothing);
end
[x0,y0,iout,jout] = intersections(spatial_freq,score.FRC,spatial_freq, Ts,false);
if all(score.FRC >= Ts')
score.resolution = 1;
elseif all(score.FRC <= Ts')
score.resolution = 0;
elseif any(x0 > 0.1)
score.resolution = min(x0(x0 > 0.1));
else
score.resolution = min(x0);
end
if r.plot_results
subplot(1,2,1)
hold all
b = plot(spatial_freq,score.FRC+randn*0.1,'LineWidth', 2);
h = plot(spatial_freq, Ts, 'k--', 'LineWidth', 2);
plot(x0, y0, 'o')
xlabel('Spatial frequency / Nyquist')
% ylabel('FRC')
ylabel(sprintf('Fourier ring correlation, AUC=%3.3g', AUC))
hold off
ylim([0,1])
xlim([0,1])
% r = vline(resolution, '-k');
legend([b, h], 'FRC', '1 bit threshold','Location','Best');
grid on
subplot(1,2,2)
hold all
plot(score.spatial_freq, score.SSNR);
try; vline(score.resolution); end
hline(1)
set(gca, 'yscale', 'log')
hold off
grid on
ylabel(sprintf('Spectral SNR, SNR_{avg}=%3.3g', score.SNR_avg))
% width = 10;
% aspect_ratio=4/3;
% height = width / aspect_ratio;
% % set size of the resulting image
% set(gcf, 'PaperPosition', [1.5 1.5 width height]);
plotting.suptitle(sprintf('Resolution=%.3gnm AuC=%.3g', min(r.px_scale) / score.resolution * 1e9,AUC))
end
verbose(3,'AUC %g', score.AUC)
verbose(3,'SNR %g', score.SNR_avg)
verbose(3,'resolution %g (%g nm)', score.resolution, min(r.px_scale) / score.resolution*1e9)
try
verbose('SSNR 0.1 %g 0.5 %g, 0.9 %g \n', log10(quantile(score.SSNR, [0.1, 0.5, 0.9] )))
end
end
@@ -0,0 +1,213 @@
% ONLINE_FSC_ESTIMATE online estimation of the fourier shell correlation curve to estimation of optimal convergence
% compare two scans and estimate FSC and other statistics
%
% score = online_FSC_estimate(self, par, cache, score_0, iter)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** score_0 [] or a structure with outputs from previous online estimation of FSC curve
%
% returns:
% ++ score structure with outputs from online estimation of FSC curve
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 score = online_FSC_estimate(self, par, cache, score_0, iter)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
import plotting.*
import engines.GPU_MS.*
if check_option(self, 'object_orig')
self.object{end+1,1} = cat(3,self.object_orig{1,:});
end
compared_indices = (2:size(self.object,1))-1;
% take product of the reconstructed images, eDOF
%% refererene image
selected_ROI = cache.object_ROI;
selected_ROI{2} = selected_ROI{2}(ceil(end/10):floor(end*9/10));
obj{1} = cat(3, self.object{1,:});
obj{1} = Garray(obj{1});
for ll = compared_indices
%% compared image
% take product of the reconstructed images, eDOF
obj_compared = cat(3,self.object{ll+1,:});
if ~isempty(score_0) && ~isempty(score_0{ll})
obj_compared = imshift_fft(obj_compared, score_0{ll}.shift);
end
obj{2} = Garray(obj_compared);
%% get at least some empirical esitmation of reliability -> for selection of compared ROI
for kk = 1:2
ind = [1,min(ll+1, length(cache.illum_sum_0))];
W{kk} = cache.illum_sum_0{ind(kk)}(selected_ROI{:});
W{kk} = W{kk} > 0.5*mean(W{kk});
% W{kk} = imfill(W{kk}, 'holes');
if any(W{kk}(:)==0)
Npix = size(W{kk});
downscale = 10;
W{kk} = real(utils.interpolateFT(W{kk}, ceil(Npix / downscale)));
try; W{kk} = Garray(imerode( Ggather(W{kk})>0.1, strel('disk', ceil(self.Np_p(1)/8/downscale)))); end
W{kk} = (utils.imgaussfilt3_conv(W{kk}, mean(self.Np_p)/8/downscale));
W{kk} = max(0,real(utils.interpolateFT(W{kk},Npix)));
end
end
clear obj_0
Wshared = sqrt(W{1}.*W{2});
for kk = 1:2
W{kk} = Wshared;
end
if size(obj{1},3) > 1 ||size(obj{2},3) > 1
Nl_shifts = 4;
else
Nl_shifts = 1;
end
for kk = 1:Nl_shifts
for ii = 1:2
Nlayers = size(obj{ii},3);
horiz_shifts = linspace(-(kk-1), (kk-1), Nlayers)';
shift = [horiz_shifts, zeros(Nlayers,1)];
if kk > 1 && ii == 1
shift = shift - score{ll,kk-1}.shift;
end
% apply different shift on each layer -> minic rotation
obj_tmp{ii} = prod(imshift_fft(obj{ii}, shift),3);
obj_tmp{ii} = obj_tmp{ii}(selected_ROI{:});
end
[score{ll,kk},obj_out] = analysis.fourier_ring_correlation(obj_tmp{:},...
'smoothing', 1, 'crop', ceil(self.Np_p / 4) , 'plot_results', false, 'px_scale', self.pixel_size, 'weights', W);
if ~isempty(score_0) && ~isempty(score_0{ll})
score{ll,kk}.shift = score{ll,kk}.shift + score_0{ll}.shift ;
end
if ll == compared_indices(end) && verbose > 2
plotting.smart_figure(2121)
img = angle(cat(3,obj_out{:}));
plotting.imagesc3D(img); axis off image xy ;
caxis(Ggather(math.sp_quantile(img, [0.01, 0.99],10)))
title('Aligned frames used for FSC estimation')
drawnow
end
% fprintf('========== total object shift ====== %g %g\n', score{end}.shift)
score{ll,kk}.iter = iter;
score{ll,kk}.positions = self.modes{1}.probe_positions;
score{ll,kk}.positions_0 = self.modes{1}.probe_positions_0;
%score{ll,kk}.intensity = self.modes{1}.weights;
score{ll,kk}.probe_fourier_shift = self.modes{1}.probe_fourier_shift;
end
end
plotting.smart_figure(4554)
clf
subplot(1,2,1)
linestyle = {'-','--',':'};
hold all
for kk = 1:Nl_shifts
for ll = compared_indices
if isempty(score{ll,kk}); continue; end
b(ll) = plot(score{ll,kk}.spatial_freq,score{ll,kk}.FRC,linestyle{1+mod(ll-1,end)},'LineWidth', 2);
legend_names{ll} = sprintf('FRC scans 1 vs %i', ll+1);
end
h = plot(score{ll,1}.spatial_freq, score{ll,1}.thresh, 'k--', 'LineWidth', 2);
end
xlabel('Spatial frequency / Nyquist')
ylabel(sprintf('Fourier ring correlation, AUC=%3.3g', score{ll,1}.AUC))
hold off
ylim([0,1])
xlim([0,1])
legend([b, h], legend_names{:}, '1 bit threshold','Location','Best');
grid on
subplot(1,2,2)
hold all
for kk = 1:Nl_shifts
for ll = compared_indices
if isempty(score{ll,kk}); continue; end
score{ll,kk}.SSNR(~isfinite(score{ll}.SSNR) | score{ll,kk}.SSNR <= 0) = nan;
plot(score{ll,kk}.spatial_freq, score{ll,kk}.SSNR);
end
end
hline(1)
set(gca, 'yscale', 'log')
hold off
grid on
ylabel(sprintf('Spectral SNR, SNR_{avg}=%3.3g', score{ll,1}.SNR_avg))
plotting.suptitle(sprintf('Resolution %3.3gnm', mean(self.pixel_size) / score{ll,1}.resolution * 1e9))
end
@@ -0,0 +1,77 @@
% PLOT_BACKGROUND_INTENSITY plot estiamtion of background for each of the scan positions
%
% plot_background_intensity(self,probe, background)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** probe structure containing parameters for the engines
% ** probe [Nx,Ny,variable_modes] complex array with probe
% ** background [Npos,1] array with background intensity
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_background_intensity(self,probe, background)
plotting.smart_figure(11231)
subplot(1,2,1)
show_spatial_distribution(self.probe_positions_0, background, false, false)
axis off image
colorbar
title('Background distribution')
subplot(1,2,2)
imagesc(abs(probe))
axis off image
end
@@ -0,0 +1,97 @@
% PLOT_FRC_ANALYSIS plot evolution of the resolution and SNR estimated from the FRC
%
% plot_frc_analysis(score, par)
%
% ** score structure with outputs from online estimation of FSC curve
% ** par structure containing parameters for the engines
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_frc_analysis(score, par)
N = size(score,1)-1;
resolution = nan(N,1);
for i = 1:N
iteration(i) = score{i+1,1}.iter;
try; resolution(i) = score{i+1,1}.resolution; end
SNR(i) = score{i+1,1}.SNR_avg;
AUC(i) = score{i+1,1}.AUC;
end
plotting.smart_figure(123132)
subplot(1,3,1)
semilogx(iteration, medfilt1(resolution, 'truncate'))
xlim([1, par.number_iterations])
xlabel('Iteration')
ylabel('Spatial frequency/Nyquist')
title('FRC resolution')
grid on
subplot(1,3,2)
semilogx(iteration, medfilt1(SNR, 'truncate' ))
xlim([1, par.number_iterations])
xlabel('Iteration')
ylabel('SNR')
title('Average signal to noise ratio')
grid on
subplot(1,3,3)
semilogx(iteration, medfilt1(AUC, 'truncate' ))
xlim([1, par.number_iterations])
xlabel('Iteration')
ylabel('AUC')
title('Area under FRC curve')
grid on
plotting.suptitle('Fourier ring resolution analysis')
end
@@ -0,0 +1,292 @@
% PLOT_GEOM_CORRECTIONS plot position refinement statistics - position errors, directions and weights
%
% plot_geom_corrections(self, mode, object, iter, par, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** mode structure containing reconstruction parameters related to the selected incoherent mode
% ** object cell of arrays, reconstructed object
% ** iter current iteration number
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% FUNCTION plot_geom_corrections(self, mode, object, iter, par, cache)
% plot positiones updates
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_geom_corrections(self, mode, object, iter, par, cache)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import plotting.*
import utils.*
pos = mode.probe_positions;
pos_0 = self.probe_positions_0;
Nplots = 4*(iter >= par.probe_position_search && ~isempty(par.probe_geometry_model)) ...
+ (iter >= par.estimate_NF_distance) + ...
(iter >= par.detector_rotation_search) + ...
(iter >= par.detector_scale_search);
if ~ishandle(16165)
plotting.smart_figure(16165)
set(gcf,'Outerposition',[100 100 Nplots*330 400]) %[left, bottom, width, height
else
plotting.smart_figure(16165)
end
plot_id = 0;
if iter >= par.probe_position_search && ~isempty(par.probe_geometry_model)
clf()
x_iters=[par.probe_position_search:iter]; % correction of iteration index in x-axis by ZC
subplot(1,Nplots,1)
hold all
plot(x_iters,mode.scales , '-'); axis tight
ylabel('Relative pixel scaling correction [-]')
xlabel('Iteration')
hold off
grid on
title('Scales')
subplot(1,Nplots,2)
plot(x_iters,mode.rotation , '-'); axis tight
ylabel('Rotation [deg]')
title('Rotation')
xlabel('Iteration')
grid on
subplot(1,Nplots,3)
plot(x_iters,mode.shear , '-'); axis tight
ylabel('Shear [deg]')
title('Shear')
xlabel('Iteration')
grid on
subplot(1,Nplots,4)
plot(x_iters,mode.asymmetry*100 , '-'); axis tight
ylabel('Asymmetry [%]')
title('Asymmetry')
xlabel('Iteration')
grid on
plot_id = 4;
end
if iter >= par.estimate_NF_distance
subplot(1,Nplots,plot_id+1)
plot(mode.distances * 1e6 , '-');
axis tight
grid on
ylabel('Propagation distance [um]')
title('Nearfield propagation distance')
xlabel('Iteration')
plot_id = plot_id + 1;
end
if iter >= par.detector_rotation_search
subplot(1,Nplots,plot_id+1)
plot(mode.probe_rotation,'-');
axis tight
grid on
ylabel('Detector rotation angle [deg]')
title('Detector rotation')
xlabel('Iteration')
plot_id = plot_id + 1;
end
if iter >= par.detector_scale_search
subplot(1,Nplots,plot_id+1)
plot((1+mode.probe_scale_upd),'-');
axis tight
grid on
ylabel('Detector optimal scaling [-]')
title('Relative pixel scale')
xlabel('Iteration')
plot_id = plot_id + 1;
end
plotting.suptitle('Evolution of geometry parameters')
%modified by YJ: remove check_option(par, 'probe_geometry_model') to
%plot position correction even without geom refinement
%if iter >= par.probe_position_search && check_option(par, 'probe_geometry_model')
if iter >= par.probe_position_search
%modified by YJ for electron pty
if isfield(par,'beam_source') && strcmp(par.beam_source, 'electron')
unitFactor = 1;
scaleFactor = 0.1;
unitLabel = 'A';
else %X-ray
unitFactor = 1e9;
scaleFactor = 1e6;
unitLabel = 'nm';
end
% substract the geometry model to show only residuum
pos_err = pos - mode.probe_positions_model ;
% subtract average error per scan
for kk = 1:par.Nscans
ind = self.reconstruct_ind{kk};
pos_err(ind,:) = pos_err(ind,:) - mean(pos_err(ind,:));
end
pos = pos+ self.Np_o([2,1])/2;
marker_colors = {'r', 'b', 'g', 'k'};
scale = self.pixel_size*scaleFactor;
plotting.smart_figure(455454)
clf()
subplot(2,2,1)
aobject = angle(object);
range = sp_quantile(aobject(cache.object_ROI{:}), [1e-3, 1-1e-3],10);
aobject = (aobject - range(1)) / (range(2) - range(1));
grids = {(-ceil(self.Np_o(2)/2):ceil(self.Np_o(2)/2)-1)*scale(2), ...
(-ceil(self.Np_o(1)/2):ceil(self.Np_o(1)/2)-1)*scale(1)};
imagesc(grids{:}, aobject, [-2, 1]); % reduce contrast
colormap bone
axis xy
hold on
if isfield(par,'beam_source') && strcmp(par.beam_source, 'electron')
ylabel('Position [nm]')
else
ylabel('Position [\mum]')
end
pos_scales = (pos-self.Np_o([2,1])/2) .* scale([2,1]);
for i = 1:length(self.reconstruct_ind)
id = self.reconstruct_ind{i};
if any(mode.probe_positions_weight)
% plot importance
scatter(pos_scales(id,1), pos_scales(id,2), max(mode.probe_positions_weight(id,:),[],2)*20, marker_colors{1+mod(i,4)})
end
mean_err = mean(std(pos_err));
range = max(pos) - min(pos);
up = 0.02 * min(range) / mean_err;
rounding_order = 10^floor(log10(up));
up = ceil(up / rounding_order)*rounding_order;
quiver( pos_scales(id,1), pos_scales(id,2), scale(1)*pos_err(id,1)*up, scale(2)*pos_err(id,2)*up, 0, marker_colors{1+mod(i,4)})
end
hold off
axis equal xy tight
range = [min(pos_scales(:,1)), max(pos_scales(:,1)), min(pos_scales(:,2)), max(pos_scales(:,2))];
axis(range)
title(sprintf('Position errors, upscaled %ix', up))
subplot(2,2,3)
plot(mean(mode.probe_positions_weight,2), 'b.-')
ylim([0, max(mean(mode.probe_positions_weight,2))])
hold all
for i = 1:length(self.reconstruct_ind)
vline(self.reconstruct_ind{i}(end), '-r')
end
hold off
axis tight
ylabel('Importance weights')
xlabel('Position #')
title('Relative importance weights for geometry model')
subplot(2,2,2)
yyaxis left
h = plot(pos_err(:,1), 'w.');
axis tight
ylabel('Position error [px]')
yyaxis right
plot(pos_err(:,1)*self.pixel_size(2)*unitFactor, 'b.-')
axis tight
xlabel('Position #')
ylabel(strcat('Position error [',unitLabel,']'))
hold all
for i = 1:length(self.reconstruct_ind)
vline(self.reconstruct_ind{i}(end), '-r')
end
hold off
title( 'Horizontal')
grid on
%legend({sprintf('STD=%3.2g nm', std(pos_err(:,1)*self.pixel_size(2)*1e9) )})
legend({sprintf(strcat('STD=%3.2g ',unitLabel), std(pos_err(:,1)*self.pixel_size(2)*unitFactor) )})
subplot(2,2,4)
yyaxis left
h = plot(pos_err(:,2), 'w.');
ylabel('Position error [px]')
axis tight
yyaxis right
plot(pos_err(:,2)*self.pixel_size(1)*unitFactor, 'b.-')
axis tight
%ylabel('Position error [nm]')
ylabel(strcat('Position error [',unitLabel,']'))
xlabel('Position #')
hold all
title( 'Vertical')
legend({sprintf(strcat('STD=%3.2g ',unitLabel), std(pos_err(:,2)*self.pixel_size(1)*unitFactor) )})
grid on
for i = 1:length(self.reconstruct_ind)
vline(self.reconstruct_ind{i}(end), '-r')
end
hold off
plotting.suptitle('Random position errors after subtraction of geometry model')
try
if length(self.reconstruct_ind) == 2 && verbose() > 1 && length(self.reconstruct_ind{1}) == length(self.reconstruct_ind{2})
disp('Correlation between two scans')
corr( pos_err(self.reconstruct_ind{1},:), pos_err(self.reconstruct_ind{2},:) )
end
end
end
end
@@ -0,0 +1,81 @@
% PLOT_GEOM_CORRECTIONS plot evolution of intensity correction
%
% plot_geom_corrections(self)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_intensity_corr(self)
plotting.smart_figure(131231)
subplot(1,2,1)
corr = abs(self.intensity_corr/median(self.intensity_corr));
plot(corr)
axis([1,self.Npos, 0, max(corr)])
title('Intensity evolution')
subplot(1,2,2)
show_spatial_distribution(self.probe_positions_0, abs( self.intensity_corr), false, false)
axis off image
colorbar
title('Intensity distribution')
end
@@ -0,0 +1,145 @@
% PLOT_OBJECT_MODES incoherent object modes / layers / objects belonging to multiple scans
%
% plot_object_modes(self, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent object{ii}:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_object_modes(self, cache)
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import utils.*
import math.*
import plotting.*
[Nscans,Nlayers] = size(self.object);
ROI = cache.object_ROI;
for ii = 1:Nscans
object{ii} = cat(3,self.object{ii,:});
object{ii} = Ggather(object{ii}(ROI{:},:));
illum{ii} = cache.illum_sum_0{ii}(ROI{:});
end
plotting.smart_figure(302809)
kk = 1;
scale = self.pixel_size;
Np_o = [size(object{1},1),size(object{1},2)];
Np_o(2) = Np_o(2) * Nlayers;
for ii = 1:Nscans
grids = {(-ceil(Np_o(2)/2):ceil(Np_o(2)/2)-1)*scale(2), ...
(-ceil(Np_o(1)/2):ceil(Np_o(1)/2)-1)*scale(1)};
amp_obj = abs(object{ii});
% consider only the illuminated region
ROI_mask = illum{ii} >= 0.5*quantile(illum{ii}(:), 0.9);
[ROI] = get_ROI(ROI_mask);
ROI_mask = repmat(ROI_mask,1,1,size(amp_obj,3));
RANGE_amp = sp_quantile(amp_obj(ROI_mask),[5e-3,1-5e-3], 4)';
RANGE_amp(2) = max(RANGE_amp(2), RANGE_amp(1)+1e-6);
[~, gamma] = stabilize_phase(object{ii}(ROI{:},:));
ang_object = -angle(object{ii}.*gamma);
RANGE_angle = sp_quantile(ang_object(ROI_mask),[1e-3,1-1e-3], 4)';
for jj = 1:Nlayers
% avoid plotting residua in not illuminated regions for object{ii}
resid_mask = cache.illum_sum_0{ii}(ROI{:})/ cache.MAX_ILLUM(ii) > 0.1;
resid_mask = imfill(gather(resid_mask), 'holes'); % gpuArray and imfill seems to be very unstable
residues = resid_mask(2:end,2:end) & (abs(utils.findresidues(object{ii}(:,:,jj))) > 0.1);
[X,Y] = find(residues);
end
ax(2*kk-1)=subplot(2,Nscans,ii);
imagesc(grids{:},reshape(amp_obj, Np_o))
if diff(RANGE_amp)>0;caxis(RANGE_amp); end
title(sprintf('Scan %i (L:%i)', ii, jj))
ylabel(sprintf('Amplitude - <%3.2g ; %3.2g>', RANGE_amp))
axis xy tight image
colormap bone
set(gca,'TickLength',[0 0])
set(gca,'XTick',[],'YTick',[])
ax(2*kk)=subplot(2,Nscans,Nscans+kk);
imagesc(grids{:},reshape(ang_object, Np_o))
hold all
plot(Y,X,'or')
hold off
if diff(RANGE_angle)>0; caxis((RANGE_angle')); end
ylabel(sprintf('Phase - <%3.2g ; %3.2g>', RANGE_angle))
axis xy tight image
colormap bone
set(gca,'TickLength',[0 0])
set(gca,'XTick',[],'YTick',[])
kk = kk + 1 ;
end
linkaxes(ax, 'xy')
end
@@ -0,0 +1,137 @@
% PLOT_PROBE_MODES plot incoherent probe modes
%
% plot_probe_modes(self, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent probe:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_probe_modes(self, par)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
import math.*
import plotting.*
for i = 1:par.probe_modes
power(i) = Ggather(mean2(abs(self.probe{i}(:,:,1)).^2));
end
power = power / sum(power);
grids = {(-ceil(self.Np_p(2)/2):ceil(self.Np_p(2)/2)-1)*self.pixel_size(2), ...
(-ceil(self.Np_p(1)/2):ceil(self.Np_p(1)/2)-1)*self.pixel_size(1)};
plotting.smart_figure(46456)
for i = 1:par.probe_modes
mode = Ggather(mean(mean(self.probe{i},3),4));
ax(2*i-1)=subplot(2,par.probe_modes,i);
RANGE = sp_quantile(abs(mode),[1e-3,1-5e-3], 4)';
RANGE(2) = max(RANGE(2), RANGE(1)+1e-6);
amode = abs(mode);
imagesc3D(grids{:},amode)
if diff(RANGE)>0;caxis(RANGE); end
title(sprintf('Mode %i, P:%3.2g', i, power(i)))
ylabel(sprintf('Amplitude - <%3.2g ; %3.2g>', RANGE))
axis image xy
colormap bone
set(gca,'TickLength',[0 0])
set(gca,'XTick',[],'YTick',[])
ax(2*i)=subplot(2,par.probe_modes,par.probe_modes+i);
arg = -angle(utils.stabilize_phase(mode));
RANGE_arg = sp_quantile(arg,[1e-3,1-1e-3], 4)';
imagesc3D(grids{:},arg )
if diff(RANGE_arg)>0; caxis((RANGE_arg')); end
ylabel(sprintf('Phase - <%3.2g ; %3.2g>', RANGE_arg))
axis image xy
colormap bone
set(gca,'TickLength',[0 0])
set(gca,'XTick',[],'YTick',[])
end
linkaxes(ax, 'xy')
if par.probe_modes > par.Nscans % dont run for multiscan
reconstruct_ind = [self.reconstruct_ind{:}];
if par.variable_probe
plotting.smart_figure(id+1)
clf
power = power / sum(power);
for i = 1:length(self.probe)
pos = self.probe{i}.probe_positions;
pos = pos(:,[2,1]);
pos(:,1) = -pos(:,1);
subplot(2,1,1)
hold all
plot(power(i))
hold off
title('Variable incoherent probe')
xlabel('Normalized mode power')
subplot(2,par.probe_modes,par.probe_modes+i)
scatter(pos(reconstruct_ind,:),gather(W(reconstruct_ind)), 20);
axis off image
end
end
end
end
@@ -0,0 +1,181 @@
% PLOT_RESULTS show current reconstruction and errors during ptychography
%
% plot_results(self, cache, par, fourier_error,probe_positions)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** par structure containing parameters for the engines
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
% ** probe_positions array [Npos,2] with probe positions for the main coherence mode
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_results(self, cache, par, fourier_error,probe_positions)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
import math.*
import plotting.*
likelihood = lower(par.likelihood);
try
verbose(1,'Plotting ... ')
Np_o = self.Np_o;
Npos = length(probe_positions);
reconstruct_ind = [self.reconstruct_ind{:}];
ind = find(any(~isnan(fourier_error),2));
probe = Ggather(self.probe{1}(:,:,1,1));
% show extended DoF projection through layers of first scan
object = prod(cat(3,self.object{1,:}),3);
if par.fourier_ptycho
object = fft2(fftshift(object));
end
object = object(cache.object_ROI{:});
plotting.smart_figure(10)
clf()
number_iterations = size(fourier_error,1);
ha = tight_subplot(2,2,[.01 .01],[.01 .01],[.01 .01]);
axes(ha(1))
pixel_size = self.pixel_size .* cosd(par.sample_rotation_angles([1,2]));
params = {'scale', pixel_size,'enhance_contrast', true};
probe_positions = probe_positions - repmat([mean(cache.object_ROI{2})-Np_o(2)/2, mean(cache.object_ROI{1})-Np_o(1)/2],Npos,1);
imagesc_hsv(object ,params{:});
if ~par.fourier_ptycho
% avoid plotting residua in not illuminated regions
resid_mask = cache.illum_sum_0{1}(cache.object_ROI{:})/ cache.MAX_ILLUM(1) > 0.1;
% find residua to plot
residues = resid_mask(2:end, 2:end) & (abs(utils.findresidues(object)) > 0.1);
[X,Y] = find(residues);
if length(probe_positions) < 2e3
points = probe_positions(reconstruct_ind, :);
hold all
plot(points(:,1)*pixel_size(2)*1e6, points(:,2)*pixel_size(1)*1e6, '.w')
plot((Y-size(object,2)/2)*pixel_size(2)*1e6,(X-size(object,1)/2)*pixel_size(1)*1e6,'ow')
hold off
end
end
axis xy
ylabel('Reconstruction in fake colors')
axes(ha(3))
probe = utils.prop_free_nf(probe, self.lambda, sum(self.z_distance(1:end-1))/2, self.pixel_size);
imagesc_hsv(probe, params{:} );
axis xy
ylabel('Contrast enhanced probe')
subplot(2,2,2)
fourier_error(fourier_error == 0) = nan;
if strcmpi(likelihood, 'poisson')
fourier_error = (bsxfun(@minus, fourier_error, fourier_error(1,:)));
end
if ~isempty(ind) %if there is somethign to plot
hold all
plot(ind, fourier_error(ind,reconstruct_ind), '-')
ind_missing = ~ismember(1:self.Npos, reconstruct_ind);
if any(ind_missing)
plot(ind, fourier_error(ind,ind_missing), '--')
end
plot(ind, nanmean(fourier_error(ind,~ind_missing)'),'k', 'LineWidth', 3)
plot(ind, nanmedian(fourier_error(ind,~ind_missing)'),'k--', 'LineWidth', 3)
hold off
grid on
set(gca, 'xscale', 'log')
if strcmpi(likelihood, 'L1')
set(gca, 'yscale', 'log')
end
xlim([1, number_iterations])
% ignore the first iteration error in plotting
try ylim([min2(fourier_error(2:end,:)), max2(fourier_error(2:end,:))]); end
switch likelihood
case 'poisson', title('Relative neg-likelihood change');
case 'l1', title('Fourier error');
end
end
subplot(2,2,4)
if length(ind) > 1
err = fourier_error(ind(end) , reconstruct_ind) ;
pos = pixel_size([2,1]).*probe_positions(reconstruct_ind,:);
%% compatibility with the CPU code
pos(:,2) = -pos(:,2);
show_spatial_distribution(Ggather(pos), Ggather(err), false, length(probe_positions) < 2e3)
axis off equal
end
title('Spatial distribution of error')
catch err
warning('Error during plotting: %s', err.message)
keyboard
disp('plotting failed')
end
end
@@ -0,0 +1,110 @@
% PLOT_VARIABLE_PROBE plot SVD decomposition of the probes to show their differences
%
% plot_variable_probe(self, par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 plot_variable_probe(self, par)
import engines.GPU_MS.GPU_wrapper.*
import plotting.*
import math.*
probe = self.probe{1};
% probe = fftshift_2D(fft2( fftshift_2D( self.probe{1})));
probe_evolution= real(self.probe_evolution);
plotting.smart_figure(12131)
ax(1)=subplot(2,1+par.variable_probe_modes,1);
imagesc_hsv(probe(:,:,:,1))
axis xy off
title('Constant mode')
for ii = 1:par.variable_probe_modes
ax(ii+1)=subplot(2,1+par.variable_probe_modes,1+ii);
imagesc_hsv(probe(:,:,:,1+ii))
axis xy off
title(sprintf('Variable mode %i', ii))
end
subplot(2,1,2)
plot( probe_evolution(:,1)-1 , 'k' );
hold on
plot( probe_evolution(:,2:end))
hold off
for kk = 1:length(self.reconstruct_ind)
vline(self.reconstruct_ind{kk}(end),'r--')
end
hold off
axis tight
if par.variable_probe && par.variable_intensity
legend({'Intensity correction', 'Variable mode evol'}, 'Location', 'best')
elseif par.variable_intensity
legend({'Intensity correction'}, 'Location', 'best')
else
legend({'Variable mode evol'}, 'Location', 'best')
end
xlabel('Position #')
ylabel('Relative mode importance')
title('Evolution of each variable probe mode')
linkaxes(ax, 'xy');
end
@@ -0,0 +1,202 @@
% IMAGESC_HSV for plotting complex valued arrays , similar to imagesc3D but with more options
% imagesc_hsv(varargin)
%
% ** varargin see the code
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 imagesc_hsv(varargin)
import utils.*
import math.*
par = inputParser;
par.addOptional('data', [])
par.addParameter('scale', nan , @isnumeric )
par.addParameter('clim', [] , @isnumeric )
par.addParameter('inverse', false , @islogical ) % use white background
par.addParameter('show_ROI', false , @islogical ) % show only intersting area
par.addParameter('points', [] , @isnumeric ) % plot dots
par.addParameter('enhance_contrast', false , @islogical ) % plot dots
par.addParameter('axis', [] , @isnumeric ) % plot dots
par.addParameter('stabilize_phase', true , @islogical ) % plot dots
par.addParameter('show', true , @islogical ) % plot dots
par.parse(varargin{:})
r = par.Results;
data = r.data;
clim = r.clim;
if all(data(:) == 0)
warning('Empty data to plot')
return
end
[W,H] = size(data);
if ~isempty(r.axis)
X = linspace(r.axis(1),r.axis(2),W)*1e6;
Y = linspace(r.axis(3),r.axis(4),H)*1e6;
else
if ~isnan(r.scale)
scale = ones(2,1).*r.scale(:);
X = [-W/2:W/2-1]* scale(1)*1e6;
Y = [-H/2:H/2-1]* scale(2)*1e6;
else
X = 1:W; Y = 1:H;
end
end
if r.show_ROI
asum = abs(sum(data,3));
try
T1 = (graythresh_new((sum(asum,1))));
T2 = (graythresh_new((sum(asum,2))));
asum(:,sum(asum,1) < T1) = 0;
asum(sum(asum,2) < T2,:) = 0;
[ROI] = get_ROI(asum > 0.01*quantile(asum(:), 0.99), 0);
data = data(ROI{:});
X = X(ROI{1});
Y = Y(ROI{2});
catch
warning('ROI estimation failed')
end
end
[W,H] = size(data);
if ~isempty(clim)
ind_min = abs(data) < clim(1);
ind_max = abs(data) > clim(2);
data(ind_min) = data(ind_min) ./ abs(data(ind_min)) * clim(1);
data(ind_max) = data(ind_max) ./ abs(data(ind_max)) * clim(2);
end
adata = abs(data);
alpha = 1e-3;
tmp= sort(adata(:));
MAX = tmp(ceil(end*(1-alpha)));
ind = adata > MAX;
data(ind) = MAX * data(ind) ./ abs(data(ind));
if r.enhance_contrast
data = data ./ sqrt(alpha+abs(data));
clim = sqrt(clim);
end
if r.stabilize_phase
data = stabilize_phase(data, abs(data), abs(data), 'remove_ramp', false);
end
adata = abs(data);
if isempty(clim)
range = sp_quantile(adata(:), [1e-2, 1-1e-2],10);
else
range = clim;
end
adata = (adata - range(1) ) ./ ( range(2) - range(1) );
ang_data = angle(data);
if r.enhance_contrast && r.stabilize_phase
ang_range = max(abs(sp_quantile(ang_data(:), [1e-2, 1-1e-2],10)));
ang_range = max(1e-3, ang_range);
ang_data = 2*pi*ang_data ./ (2* ang_range);
end
if r.inverse
hue = mod(ang_data+1.5*pi, 2*pi)/(2*pi);
hsv_data = [ hue(:) , adata(:), ones(W*H,1) ];
else
hue = mod(ang_data+2.5*pi, 2*pi)/(2*pi);
hsv_data = [ hue(:) , ones(W*H,1), adata(:) ];
end
hsv_data = min(max(0, hsv_data),1);
rgb_data = hsv2rgb(hsv_data);
rgb_data = reshape(rgb_data, W,H,3);
rgb_data = min(1,rgb_data);
if r.show
hh = imagesc(Y,X, rgb_data );
axis image
end
if r.show
% Get the parent Axes of the image
axis image
if ~isempty(r.points) && ~any(isnan(r.scale))
hold on
points = r.scale.*1e6.*r.points;
plot( points(:,1),points(:,2), '.w')
hold off
end
end
end
@@ -0,0 +1,369 @@
function [x0,y0,iout,jout] = intersections(x1,y1,x2,y2,robust)
%INTERSECTIONS Intersections of curves.
% Computes the (x,y) locations where two curves intersect. The curves
% can be broken with NaNs or have vertical segments.
%
% Example:
% [X0,Y0] = intersections(X1,Y1,X2,Y2,ROBUST);
%
% where X1 and Y1 are equal-length vectors of at least two points and
% represent curve 1. Similarly, X2 and Y2 represent curve 2.
% X0 and Y0 are column vectors containing the points at which the two
% curves intersect.
%
% ROBUST (optional) set to 1 or true means to use a slight variation of the
% algorithm that might return duplicates of some intersection points, and
% then remove those duplicates. The default is true, but since the
% algorithm is slightly slower you can set it to false if you know that
% your curves don't intersect at any segment boundaries. Also, the robust
% version properly handles parallel and overlapping segments.
%
% The algorithm can return two additional vectors that indicate which
% segment pairs contain intersections and where they are:
%
% [X0,Y0,I,J] = intersections(X1,Y1,X2,Y2,ROBUST);
%
% For each element of the vector I, I(k) = (segment number of (X1,Y1)) +
% (how far along this segment the intersection is). For example, if I(k) =
% 45.25 then the intersection lies a quarter of the way between the line
% segment connecting (X1(45),Y1(45)) and (X1(46),Y1(46)). Similarly for
% the vector J and the segments in (X2,Y2).
%
% You can also get intersections of a curve with itself. Simply pass in
% only one curve, i.e.,
%
% [X0,Y0] = intersections(X1,Y1,ROBUST);
%
% where, as before, ROBUST is optional.
% Version: 2.0, 25 May 2017
% Author: Douglas M. Schwarz
% Email: dmschwarz=ieee*org, dmschwarz=urgrad*rochester*edu
% Real_email = regexprep(Email,{'=','*'},{'@','.'})
% Theory of operation:
%
% Given two line segments, L1 and L2,
%
% L1 endpoints: (x1(1),y1(1)) and (x1(2),y1(2))
% L2 endpoints: (x2(1),y2(1)) and (x2(2),y2(2))
%
% we can write four equations with four unknowns and then solve them. The
% four unknowns are t1, t2, x0 and y0, where (x0,y0) is the intersection of
% L1 and L2, t1 is the distance from the starting point of L1 to the
% intersection relative to the length of L1 and t2 is the distance from the
% starting point of L2 to the intersection relative to the length of L2.
%
% So, the four equations are
%
% (x1(2) - x1(1))*t1 = x0 - x1(1)
% (x2(2) - x2(1))*t2 = x0 - x2(1)
% (y1(2) - y1(1))*t1 = y0 - y1(1)
% (y2(2) - y2(1))*t2 = y0 - y2(1)
%
% Rearranging and writing in matrix form,
%
% [x1(2)-x1(1) 0 -1 0; [t1; [-x1(1);
% 0 x2(2)-x2(1) -1 0; * t2; = -x2(1);
% y1(2)-y1(1) 0 0 -1; x0; -y1(1);
% 0 y2(2)-y2(1) 0 -1] y0] -y2(1)]
%
% Let's call that A*T = B. We can solve for T with T = A\B.
%
% Once we have our solution we just have to look at t1 and t2 to determine
% whether L1 and L2 intersect. If 0 <= t1 < 1 and 0 <= t2 < 1 then the two
% line segments cross and we can include (x0,y0) in the output.
%
% In principle, we have to perform this computation on every pair of line
% segments in the input data. This can be quite a large number of pairs so
% we will reduce it by doing a simple preliminary check to eliminate line
% segment pairs that could not possibly cross. The check is to look at the
% smallest enclosing rectangles (with sides parallel to the axes) for each
% line segment pair and see if they overlap. If they do then we have to
% compute t1 and t2 (via the A\B computation) to see if the line segments
% cross, but if they don't then the line segments cannot cross. In a
% typical application, this technique will eliminate most of the potential
% line segment pairs.
%
%
% Copyright (c) 2017, Douglas M. Schwarz
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
%
% Input checks.
if verLessThan('matlab','7.13')
error(nargchk(2,5,nargin)) %#ok<NCHKN>
else
narginchk(2,5)
end
% Adjustments based on number of arguments.
switch nargin
case 2
robust = true;
x2 = x1;
y2 = y1;
self_intersect = true;
case 3
robust = x2;
x2 = x1;
y2 = y1;
self_intersect = true;
case 4
robust = true;
self_intersect = false;
case 5
self_intersect = false;
end
% x1 and y1 must be vectors with same number of points (at least 2).
if sum(size(x1) > 1) ~= 1 || sum(size(y1) > 1) ~= 1 || ...
length(x1) ~= length(y1)
error('X1 and Y1 must be equal-length vectors of at least 2 points.')
end
% x2 and y2 must be vectors with same number of points (at least 2).
if sum(size(x2) > 1) ~= 1 || sum(size(y2) > 1) ~= 1 || ...
length(x2) ~= length(y2)
error('X2 and Y2 must be equal-length vectors of at least 2 points.')
end
% Force all inputs to be column vectors.
x1 = x1(:);
y1 = y1(:);
x2 = x2(:);
y2 = y2(:);
% Compute number of line segments in each curve and some differences we'll
% need later.
n1 = length(x1) - 1;
n2 = length(x2) - 1;
xy1 = [x1 y1];
xy2 = [x2 y2];
dxy1 = diff(xy1);
dxy2 = diff(xy2);
% Determine the combinations of i and j where the rectangle enclosing the
% i'th line segment of curve 1 overlaps with the rectangle enclosing the
% j'th line segment of curve 2.
% Original method that works in old MATLAB versions, but is slower than
% using binary singleton expansion (explicit or implicit).
% [i,j] = find( ...
% repmat(mvmin(x1),1,n2) <= repmat(mvmax(x2).',n1,1) & ...
% repmat(mvmax(x1),1,n2) >= repmat(mvmin(x2).',n1,1) & ...
% repmat(mvmin(y1),1,n2) <= repmat(mvmax(y2).',n1,1) & ...
% repmat(mvmax(y1),1,n2) >= repmat(mvmin(y2).',n1,1));
% Select an algorithm based on MATLAB version and number of line
% segments in each curve. We want to avoid forming large matrices for
% large numbers of line segments. If the matrices are not too large,
% choose the best method available for the MATLAB version.
if n1 > 1000 || n2 > 1000 || verLessThan('matlab','7.4')
% Determine which curve has the most line segments.
if n1 >= n2
% Curve 1 has more segments, loop over segments of curve 2.
ijc = cell(1,n2);
min_x1 = mvmin(x1);
max_x1 = mvmax(x1);
min_y1 = mvmin(y1);
max_y1 = mvmax(y1);
for k = 1:n2
k1 = k + 1;
ijc{k} = find( ...
min_x1 <= max(x2(k),x2(k1)) & max_x1 >= min(x2(k),x2(k1)) & ...
min_y1 <= max(y2(k),y2(k1)) & max_y1 >= min(y2(k),y2(k1)));
ijc{k}(:,2) = k;
end
ij = vertcat(ijc{:});
i = ij(:,1);
j = ij(:,2);
else
% Curve 2 has more segments, loop over segments of curve 1.
ijc = cell(1,n1);
min_x2 = mvmin(x2);
max_x2 = mvmax(x2);
min_y2 = mvmin(y2);
max_y2 = mvmax(y2);
for k = 1:n1
k1 = k + 1;
ijc{k}(:,2) = find( ...
min_x2 <= max(x1(k),x1(k1)) & max_x2 >= min(x1(k),x1(k1)) & ...
min_y2 <= max(y1(k),y1(k1)) & max_y2 >= min(y1(k),y1(k1)));
ijc{k}(:,1) = k;
end
ij = vertcat(ijc{:});
i = ij(:,1);
j = ij(:,2);
end
elseif verLessThan('matlab','9.1')
% Use bsxfun.
[i,j] = find( ...
bsxfun(@le,mvmin(x1),mvmax(x2).') & ...
bsxfun(@ge,mvmax(x1),mvmin(x2).') & ...
bsxfun(@le,mvmin(y1),mvmax(y2).') & ...
bsxfun(@ge,mvmax(y1),mvmin(y2).'));
else
% Use implicit expansion.
[i,j] = find( ...
mvmin(x1) <= mvmax(x2).' & mvmax(x1) >= mvmin(x2).' & ...
mvmin(y1) <= mvmax(y2).' & mvmax(y1) >= mvmin(y2).');
end
% Find segments pairs which have at least one vertex = NaN and remove them.
% This line is a fast way of finding such segment pairs. We take
% advantage of the fact that NaNs propagate through calculations, in
% particular subtraction (in the calculation of dxy1 and dxy2, which we
% need anyway) and addition.
% At the same time we can remove redundant combinations of i and j in the
% case of finding intersections of a line with itself.
if self_intersect
remove = isnan(sum(dxy1(i,:) + dxy2(j,:),2)) | j <= i + 1;
else
remove = isnan(sum(dxy1(i,:) + dxy2(j,:),2));
end
i(remove) = [];
j(remove) = [];
% Initialize matrices. We'll put the T's and B's in matrices and use them
% one column at a time. AA is a 3-D extension of A where we'll use one
% plane at a time.
n = length(i);
T = zeros(4,n);
AA = zeros(4,4,n);
AA([1 2],3,:) = -1;
AA([3 4],4,:) = -1;
AA([1 3],1,:) = dxy1(i,:).';
AA([2 4],2,:) = dxy2(j,:).';
B = -[x1(i) x2(j) y1(i) y2(j)].';
% Loop through possibilities. Trap singularity warning and then use
% lastwarn to see if that plane of AA is near singular. Process any such
% segment pairs to determine if they are colinear (overlap) or merely
% parallel. That test consists of checking to see if one of the endpoints
% of the curve 2 segment lies on the curve 1 segment. This is done by
% checking the cross product
%
% (x1(2),y1(2)) - (x1(1),y1(1)) x (x2(2),y2(2)) - (x1(1),y1(1)).
%
% If this is close to zero then the segments overlap.
% If the robust option is false then we assume no two segment pairs are
% parallel and just go ahead and do the computation. If A is ever singular
% a warning will appear. This is faster and obviously you should use it
% only when you know you will never have overlapping or parallel segment
% pairs.
if robust
overlap = false(n,1);
warning_state = warning('off','MATLAB:singularMatrix');
% Use try-catch to guarantee original warning state is restored.
try
lastwarn('')
for k = 1:n
T(:,k) = AA(:,:,k)\B(:,k);
[unused,last_warn] = lastwarn; %#ok<ASGLU>
lastwarn('')
if strcmp(last_warn,'MATLAB:singularMatrix')
% Force in_range(k) to be false.
T(1,k) = NaN;
% Determine if these segments overlap or are just parallel.
overlap(k) = rcond([dxy1(i(k),:);xy2(j(k),:) - xy1(i(k),:)]) < eps;
end
end
warning(warning_state)
catch err
warning(warning_state)
rethrow(err)
end
% Find where t1 and t2 are between 0 and 1 and return the corresponding
% x0 and y0 values.
in_range = (T(1,:) >= 0 & T(2,:) >= 0 & T(1,:) <= 1 & T(2,:) <= 1).';
% For overlapping segment pairs the algorithm will return an
% intersection point that is at the center of the overlapping region.
if any(overlap)
ia = i(overlap);
ja = j(overlap);
% set x0 and y0 to middle of overlapping region.
T(3,overlap) = (max(min(x1(ia),x1(ia+1)),min(x2(ja),x2(ja+1))) + ...
min(max(x1(ia),x1(ia+1)),max(x2(ja),x2(ja+1)))).'/2;
T(4,overlap) = (max(min(y1(ia),y1(ia+1)),min(y2(ja),y2(ja+1))) + ...
min(max(y1(ia),y1(ia+1)),max(y2(ja),y2(ja+1)))).'/2;
selected = in_range | overlap;
else
selected = in_range;
end
xy0 = T(3:4,selected).';
% Remove duplicate intersection points.
[xy0,index] = unique(xy0,'rows');
x0 = xy0(:,1);
y0 = xy0(:,2);
% Compute how far along each line segment the intersections are.
if nargout > 2
sel_index = find(selected);
sel = sel_index(index);
iout = i(sel) + T(1,sel).';
jout = j(sel) + T(2,sel).';
end
else % non-robust option
for k = 1:n
[L,U] = lu(AA(:,:,k));
T(:,k) = U\(L\B(:,k));
end
% Find where t1 and t2 are between 0 and 1 and return the corresponding
% x0 and y0 values.
in_range = (T(1,:) >= 0 & T(2,:) >= 0 & T(1,:) < 1 & T(2,:) < 1).';
x0 = T(3,in_range).';
y0 = T(4,in_range).';
% Compute how far along each line segment the intersections are.
if nargout > 2
iout = i(in_range) + T(1,in_range).';
jout = j(in_range) + T(2,in_range).';
end
end
% Plot the results (useful for debugging).
% plot(x1,y1,x2,y2,x0,y0,'ok');
function y = mvmin(x)
% Faster implementation of movmin(x,k) when k = 1.
y = min(x(1:end-1),x(2:end));
function y = mvmax(x)
% Faster implementation of movmax(x,k) when k = 1.
y = max(x(1:end-1),x(2:end));
@@ -0,0 +1,115 @@
% SHOW_SPATIAL_DISTRIBUTION plot distribution of a variable, you can also use scatter or scatter_hsv
%
% show_spatial_distribution(pos, values, symmetrize, plot_points, range, px_scale )
%
% ** pos positions for each value
% ** val plotted values
% ** symmetrize (bool) if true make the caxis symmetric around 0
% ** plot_points (bool) if true plot the positions where are provided values located
% ** range array 2x1 of min / max range
% ** px_scale size of a single pixel
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 show_spatial_distribution(pos, values, symmetrize, plot_points, range, px_scale )
pos = double(pos);
values = squeeze(double(values));
if nargin < 3; symmetrize = false; end
if nargin < 4; plot_points = true; end
if nargin < 5 || isempty(range); range = [min(values(:)), max(values(:))]; end
if nargin < 6; px_scale = 1; end
if range(1) == range(2)
range(1) = 0;
range(2) = max(range(1),1);
range = sort(range);
end
% remove missing data
missing = isnan(values);
pos(missing,:) = [];
values(missing) = [];
ax = [min(pos(:,1)), max(pos(:,1)), min(pos(:,2)), max(pos(:,2))];
N = max(100, 4*sqrt(length(pos)));
XI = linspace(ax(1), ax(2), N);
YI = linspace(ax(3), ax(4), N)';
warning('off','all')
Z = griddata(pos(:,1),pos(:,2),real(values),XI,YI,'linear');
if ~isreal(values)
Z = Z + 1i*griddata(pos(:,1),pos(:,2),imag(values),XI,YI,'linear');
end
warning('on','all')
if isreal(Z)
imagesc(px_scale*XI, px_scale*YI, Z, range)
colormap gray
else
imagesc_hsv(Z)
end
if plot_points
hold on
plot(px_scale*pos(:,1), px_scale*pos(:,2), 'wo')
hold off
axis equal tight
end
end
@@ -0,0 +1,129 @@
% PTYCHO_PLOT_WRAPPER wrapper around the default ptychoshelves plotting routine
%
% ptycho_plot_wrapper(self, par, fourier_error)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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.
%
%% PLOTTING
function ptycho_plot_wrapper(self, par, fourier_error)
%% wrapper to the cSAXS default plotting function
import engines.GPU_MS.GPU_wrapper.*
p = par.p;
p.object_size = ceil(p.object_size .* ( self.Np_p ./ p.asize)); % modify the size in case of presolver with different probe size
p.asize = self.Np_p;
p.numobjs = size(self.object,1);
Nlayers = size(self.object,2);
p.object = {};
for ii = 1:p.numobjs
p.object_size(ii,:) = self.Np_o;
p.object{ii} = [];
for jj = 1:Nlayers
p.object{ii}(:,:,1,jj) = Ggather(utils.crop_pad(self.object{ii,jj}, p.object_size));
end
end
p.object_modes = par.object_modes;
p.probe_modes = par.probe_modes;
p.probes = [];
for ii = 1:p.probe_modes
p.probes(:,:,:,ii) = Ggather(self.probe{ii}(:,:,:,1));
end
p.dx_spec=[self.pixel_size]/self.relative_pixel_scale;
p.engines = {struct()};
iterations = Ggather(find(any(~isnan(fourier_error),2)));
p.engines{1}.error_metric_final = struct();
p.engines{1}.error_metric_final.iteration=iterations;
p.engines{1}.error_metric_final.value = Ggather(fourier_error( iterations,:));
p.engines{1}.error_metric_final.method = par.method;
p.engines{1}.error_metric_final.err_metric = par.likelihood;
position_offset = 1+floor((p.object_size-self.Np_p)/2);
for ii = 1:p.numscans
ind = p.scanidxs{ii};
p.positions(ind,:) = self.modes{1}.probe_positions(ind,[2,1]) + position_offset(p.share_object_ID(ii),:);
end
p.plot.extratitlestring = '';
p.plot.show_only_FOV = true;
p.plot.mask_bool = false;
p.plot.log_scale = [1 1];
p.plot.subplwinobj_dir = 'vertical';
p.plot.show_layers = true;
p.plot.residua = true;
if isempty(p.plot.obtitlestring)
p.plot.obtitlestring = [core.generate_scan_name(p) ' '];
end
if isempty(p.plot.prtitlestring)
p.plot.prtitlestring = [core.generate_scan_name(p) ' '];
end
if par.share_object
p.share_object_ID = ones(p.numobjs,1);
else
p.share_object_ID = 1:p.numobjs;
end
core.analysis.plot_results(p, 'final', true)
end
@@ -0,0 +1,241 @@
% REPORT_REFINED_GEOMETRY report results of the geometry refinenement in a readable way
%
% p = report_refined_geometry(self, param, p)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** param structure containing parameters for the engines
% ** p ptychoshelves p structure
%
% returns:
% ** p updated ptychoshelves p structure
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 p = report_refined_geometry(self, param, p)
import utils.*
scale = 1;
%% GENERATE REPORT ABOUT GEOMETRY REFINEMENT
if isempty(p.affine_matrix)
p.affine_matrix = diag([1,1]);
end
% aux function for printing results
mat2str=@(matrix)sprintf(' [%.4g , %.4g ; %.4g , %.4g ] ', reshape(matrix',[],1));
if ~isempty(self.affine_matrix) && param.probe_position_search < param.number_iterations && ~isempty(param.probe_geometry_model)
for ii = 1:length(self.affine_matrix)
self.affine_matrix{ii} = rot90(self.affine_matrix{ii},2)'; % rotation is important to match the coordinates with other engines
end
for ii = 1:length(self.affine_matrix)
p.affine_matrix_refined{ii} = p.affine_matrix * self.affine_matrix{ii};
end
if param.Nscans == 2 && param.share_object && param.mirror_objects
%% use mirrored scans to refine scanning geometry
verbose(0, '========================================================= ')
verbose(0, '==== Geometry parameters for shared 0/180 deg scans ===== ')
verbose(0, '========================================================= ')
verbose(0, '')
% find difference between 0 and 180 ,
affine_mat_relative = sqrtm(self.affine_matrix{1} * self.affine_matrix{2})*p.affine_matrix;
% keep only nondiagonal terms
affine_mat_relative = eye(2) + (1-eye(2)).*affine_mat_relative;
verbose(0, '=============== RELATIVE (0vs180deg) GEOMETRY REFINEMENT ===============')
verbose(0, '(apply p.affine_matrix manually to your template)')
verbose(0, 'p.affine_matrix = %s ', mat2str(affine_mat_relative))
[~, ~, rotation, shear] = math.decompose_affine_matrix(affine_mat_relative);
verbose(0, 'This correponds to the following parameters: [rotation=%.3fdeg , shear=%.3fdeg] ', [rotation, shear])
% find affine matrix that stays contant when moving from 0 to
% 180 deg, include also the diagonal terms from original affine
% matrix
affine_mat_global = sqrtm(self.affine_matrix{1} * ( [1,-1;-1,1] .* self.affine_matrix{2}));
affine_mat_global = affine_mat_global* diag(diag(p.affine_matrix));
verbose(0, '====================================================================================')
verbose(0, '')
scale = mean(diag(affine_mat_global));
else
%% use conventional scans to refine scanning geometry
median_affine_matrix = median(cat(3,p.affine_matrix_refined{:}),3);
verbose(0, '')
verbose(0, '========= 2D PTYCHO GEOMETRY REFINEMENT, apply manually to your template ===========')
verbose(0, 'p.affine_matrix = %s' , mat2str(median_affine_matrix))
verbose(0, '====================================================================================')
verbose(0, '')
verbose(0, 'Advanced: ======================== AFFINE CORRECTION OF SCANNER AXIS ====================')
verbose(0, 'Advanced: (for control system of piezo scanner, important for calibration of cSAXS fast FZP scanner)')
verbose(0, 'Advanced: correction_matrix = inv(p.affine_matrix) = %s ', mat2str(inv(median_affine_matrix)))
verbose(0, 'Advanced: ===============================================================================')
verbose(0, 'Note: Use scans at 0 and 180 deg with eng.share_object == true && eng.mirror_objects == true to get estimation of the 0vs180deg affine matrix requied for ptychotomography')
verbose(0, '')
verbose(0, '')
verbose(0, '==== Geometry parameters for each scan===== ')
for ii = 1:length(p.affine_matrix_refined)
[scale, asymmetry, rotation, shear] = math.decompose_affine_matrix(p.affine_matrix_refined{ii});
verbose(0, 'Scan #%i: [scale=%.4f , asymmetry=%.3f , rotation=%.3fdeg , shear=%.3fdeg, shift = %.1f %.1fpx ] ', [p.scan_number(ii), scale, asymmetry, rotation, shear, self.shift_scans(:,ii)'])
end
scale = mean(diag(median_affine_matrix));
end
%% evaluate results if the simulated geometry
if isfield(p,'simulation') && check_option(p.simulation,'affine_matrix')
% report for simulation
verbose(-2, '')
verbose(-2, '========== IDEAL AFFINE MATRIX vs RECONSTRUCTED AFFINE MATRIX ====')
verbose(-2, 'ideal_affine_matrix = %s ', mat2str(p.simulation.affine_matrix))
if param.Nscans == 2 && param.share_object && param.mirror_objects
affine_mat = diag(diag(affine_mat_global)) + affine_mat_relative - eye(2);
else
affine_mat = median_affine_matrix;
end
verbose(-2, 'refined_affine_matrix = %s ', mat2str(affine_mat))
verbose(-2, '==================================================================')
verbose(-2, '')
end
end
if param.number_iterations > param.detector_rotation_search && ~isempty(param.probe_geometry_model)
if isfield(p,'simulation') && check_option(p.simulation,'sample_rotation_angles')
% report for simulation
verbose(-2, '')
verbose(-2, '==== SIMULATION: IDEAL vs RECONSTRUCTED DETECTOR ROTATION CORRECTION =======')
verbose(-2, 'ideal camera rotation = %.3f deg reconstructed camera rotation = %.3f deg', p.simulation.sample_rotation_angles(3), self.detector_rotation(1))
verbose(-2, '=============================================================================')
verbose(-2, '')
else
% report for real data
verbose(0, '')
verbose(0, '========== RECONSTRUCTED DETECTOR ROTATION CORRECTION =====================')
verbose(0, '(misalignement between detector and the rotation axis, correct by camera rotation)')
verbose(0, 'Reconstructed camera rotation = %.3f deg', self.detector_rotation(1) + param.sample_rotation_angles(3))
verbose(0, '=============================================================================')
verbose(0, '')
end
end
if param.number_iterations > param.detector_scale_search && ~isempty(param.probe_geometry_model)
if isfield(p,'simulation') && isfield(p.simulation, 'affine_matrix') && param.detector_scale_search
% report for simulation
verbose(-2, '')
verbose(-2, '============ SIMULATION: IDEAL vs RECONSTRUCTED DETECTOR SCALE =============')
if check_option(p.simulation, 'z')
scale_z = p.z / p.simulation.z;
else
scale_z = 1;
end
verbose(-2, 'ideal scale = %.3f reconstructed scale = %.3f ', 1/(mean(diag(p.simulation.affine_matrix)) * scale_z), scale/self.detector_scale)
verbose(-2, '=============================================================================')
verbose(-2, '')
else
% report for real data
verbose(0, '')
verbose(0, '========== RECONSTRUCTED DETECTOR SCALE CORRECTION ========================')
verbose(0, '(relative scaling error of the provided reconstruction pixel p.dx_spec )')
verbose(0, 'reconstructed scale = %.3f ', scale/self.detector_scale)
verbose(0, '=============================================================================')
verbose(0, '')
end
end
if isinf(self.z_distance) && ...
((param.detector_scale_search < param.number_iterations) ...
|| (param.probe_position_search < param.number_iterations && any(ismember(param.probe_geometry_model, 'scale'))))
verbose(-2, '')
verbose(-2, '========== RECONSTRUCTED DETECTOR DISTANCE CORRECTION =====================')
if isfield(p,'simulation') && check_option(p.simulation, 'z')
% report for simulation
verbose(-2, '==== Compare ideal (simulated) distance and distance refined by ptychography')
if isfield(p.simulation, 'affine_matrix')
aff_corr_scale = mean(diag(p.simulation.affine_matrix));
else
aff_corr_scale = 1;
end
verbose(-2, 'ideal camera distance = %.4f estimated camera distance = %.4f', p.simulation.z/aff_corr_scale, p.z / (scale * self.detector_scale))
else
% report for measurements
verbose(-2, '(needs to be corrected by adjusting p.z parameter in the template)')
verbose(-2, '==== Scale error corresponds to the following p.z value')
verbose(-2, 'p.z = %.4f (error=%.2g%%)', p.z/(scale*self.detector_scale), 100*(1/(scale*self.detector_scale)-1))
if param.probe_position_search < param.number_iterations && ~isempty(param.probe_geometry_model)
verbose(0, '(corrected p.affine_matrix to be used with the new p.z value, add manually to your template)')
if exist('median_affine_matrix', 'var')
affine_mat = median_affine_matrix;
else
affine_mat = affine_mat_relative;
end
verbose(0, 'p.affine_matrix = %s ', mat2str(affine_mat / (scale/self.detector_scale) ))
end
end
verbose(-2, '=============================================================================')
end
end
@@ -0,0 +1,178 @@
% INITIAL_CHECKS check if the inputs are valid or try to correct them
%
% [self,par] = initial_checks(self, par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ par structure containing parameters for the engines
function [self,par] = check_inputs(self, par)
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
[self.Np_o(1),self.Np_o(2),~] = size(self.object{1});
[self.Np_p(1),self.Np_p(2),~] = size(self.probe{1});
par.Nrec = 1;
par.Nscans = length(self.reconstruct_ind);
if ischar(par.extension)
par.extension = {par.extension};
end
for ii = 1:numel(self.object)
assert(all(isfinite(self.object{ii}(:))), 'Provided object contains nan / inf')
end
for ii = 1:numel(self.probe)
assert(all(isfinite(self.probe{ii}(:))), 'Provided probes contain nan / inf')
end
Np_d = size(self.diffraction);
if any(self.Np_p ~= Np_d(1:2)) % && isempty(self.modF_ROI)
error('Size of probe and data is different')
end
tmp = self.diffraction(1:self.Np_p(1)*7:end); % get some small sample
tmp = tmp * 2^(par.upsampling_data_factor*2); % remove upsampling effects
if par.compress_data && max(abs((tmp - round(tmp)))) > 0.2
verbose(1,'Data are not integers, cannot use compression')
par.compress_data = false;
end
%%%%%%%%%%%%% accelerated solver %%%%%%%%%%%%%%%%%%%
if par.accelerated_gradients_start < par.number_iterations && ~is_method(par, 'MLs')
verbose(3, 'accelerated_gradients_start < number_iterations is supported only for MLc engine')
par. accelerated_gradients_start = inf;
end
% if par.accelerated_gradients_start < par.number_iterations && par.momentum > 0 && is_method(par, 'ML')
% error('accelerated_gradients_start < inf cannot be used if momemtum > 0 ')
% end
%%%%%%%%%%%%% variable probe %%%%%%%%%%%%%%%%%%%%%%%%
if ~par.variable_probe
par.variable_probe_modes = 0;
end
if par.variable_probe && par.variable_probe_modes > 0 && ~is_method(par, {'PIE', 'ML'})
warning('Variable probe implemented only for PIE and ML')
par.variable_probe = false;
end
if par.variable_probe && par.variable_probe_modes == 0
error('Choose more than 0 variable_probe_modes for OPRP')
par.variable_probe_modes = 1;
end
if par.variable_probe && ~par.share_probe && is_method(par, 'PIE')
par.share_probe = true;
% variable probe means automatically shared variable probe
end
if ~is_method(par, {'PIE', 'ML'}) && strcmpi(par.likelihood, 'poisson')
warning('Poisson likelihood supported only for PIE methods')
par.likelihood = 'L1';
end
if ~ismember(lower( par.likelihood), {'l1','poisson'})
error('Unsupported error estimation')
end
%%%%%%%%%%%%%% check if position correction is allowed
if ~ is_method(par, {'PIE', 'ML'}) && par.probe_position_search < par.number_iterations
verbose(2, 'Position correction supported only for PIE/ML methods ')
par.probe_position_search = inf;
end
if any(~ismember(par.probe_geometry_model, {'scale', 'asymmetry', 'rotation', 'shear'}))
missing_option = setdiff(par.probe_geometry_model, {'scale', 'asymmetry', 'rotation', 'shear'} );
error('Unsupported geometry model option: "%s"', missing_option{1})
end
if par.probe_position_search < par.number_iterations && par.detector_scale_search < par.number_iterations && any(ismember(par.probe_geometry_model,'scale'))
error('Do not use probe_position_search with probe_geometry_model==''scale'' and detector_scale_search together')
end
%%%%%% checks for the multilayer method %%%%%%%%%%%%%%%%
%Note: self.z_distance is first initialized in load_from_p.m, where a
%vacuum layer is appended: self.z_distance = [p.delta_z, inf] for far-field
par.Nlayers = length(self.z_distance);
if par.Nlayers > 1 && isinf(self.z_distance(end))
% Added by ZC: exclude the last vacuum (inf) layer for multisluce
par.Nlayers = par.Nlayers - 1;
end
assert(sum(~isfinite(self.z_distance)) <= 1, 'Provided distanced of layers are not possible to be used')
if par.Nlayers > 1 && ~is_method(par, {'PIE', 'ML'})
error('Multilayer extension is supported only for PIE/ML methods')
end
% Added by ZC. allow user to specify the layer used for position correction
if ~isfield(par,'layer4pos') || isempty(par.layer4pos)
par.layer4pos = ceil(par.Nlayers/2);
end
%%%%%%%%%% fast scanning %%%%%%%%%%%%%%%%%%%%%%%%%
if is_used(par, 'fly_scan') && ~is_method(par, {'PIE', 'ML'})
error('Fly scan is supported only for PIE/ML methods')
end
if is_used(par, 'fly_scan')
if par.Nmodes == 1
warning('Flyscan has no effect with a single mode')
par.extension = setdiff(par.extension, 'fly_scan');
par.apply_subpix_shift= true;
end
par.Nrec = par.Nmodes;
% par.apply_multimodal_update = true;
end
%%%%%%%% nearfield %%%%%%%%%%%%%%%%%%%%%%%%%%%
if par.estimate_NF_distance < par.number_iterations && isinf(self.z_distance(end))
error('estimate_NF_distance valid only for nearfield mode')
end
%%%%%%%%%%%%%%% OTHER %%%%%%%%%%%%%%%%%%%%
if strcmpi(par.likelihood, 'poisson') && par.background_detection && ~isinf(par.background_detection)
error('Background detection does not work well with Poisson likelihood')
end
if prod(self.Np_p) *self.Npos > intmax('int32') && par.keep_on_gpu && is_method(par, {'MLs', 'ePIE'})
warning('Dataset as more than 2147483647 elements (max of int32). Set par.keep_on_gpu to false')
par.keep_on_gpu = false;
end
if any(self.noise(:) == 0) && par.relax_noise
warning('Some values of expected noise are 0')
self.noise = max(0.5, self.noise);
end
if par.Nrec > max([par.Nmodes, par.probe_modes , par.object_modes])
warning('Number of modes is too high')
end
if length(self.probe_positions) ~= self.Npos
self.probe_positions = [];
end
if par.mirror_objects && par.Nscans ~= 2
error('Object mirroring is supported only for two scans')
end
%%%%%% position correction %%%%%
if ~is_method(par, {'PIE', 'ML'}) && par.probe_position_search < par.number_iterations
warning('Position corrections works only for PIE/ML methods')
end
if is_method(par, {'PIE', 'ML'}) && par.probe_position_search < par.number_iterations && ~(par.apply_subpix_shift || is_used(par,'fly_scan'))
verbose(2,'Subpixel shifting is strongly recommended for position refinement => enforcing par.apply_subpix_shift = true')
par.apply_subpix_shift = true;
end
end
@@ -0,0 +1,131 @@
% INITIALIZE generate list of default parameters
% [param] = initialize
%
%
% returns:
% ++ param structure containing parameters for the engines
function [param] = get_defaults
%%%%%%%%%%%%%% GPU SETTINGS %%%%%%%%%%%%%%%%%%%%%%%%%%
param.use_gpu = true; % use GPU if possible
param.keep_on_gpu = true; % keep the data all the time on GPU
param.compress_data = true; % apply online compress on the GPU data
param.gpu_id = []; % default GPU id, [] means choosen by matlab
param.check_gpu_load = true;
param.obj_size_limit_on_gpu = inf; % maximum object size (in MB) allowed on gpu. Automatically use cpu if exceed the limit.
%% basic recontruction parameters
%% PIE
param.beta_object = 1;
param.beta_probe = 1; % step size, faster convergence , more instable ??
%% DM
param.pfft_relaxation = 0.1;
param.probe_inertia = 0.3; % add inertia to the probe reconstruction to avoid oscilations
%% general
param.share_probe = true;
param.share_object = false;
param.delta = 0; % press values to zero out of the probe area !! illim < max*delta is removed
param.relax_noise = 0.0; % relaxation for noise, lower => slower convergence, more robust
param.positivity_constraint_object = 0; % enforce weak positivity in object
param.amplitude_threshold_object = inf; % enforce maximum amplitude to object. Values larger than the threshold is set to 1
param.Nmodes = 1; % number of multi apertures , always better to start wih one !!
param.probe_modes = 1; % number of probes
param.object_modes = 1; % number of multi apertures , always better to start wih one !!
param.probe_change_start = 1; % iteration when the probe reconstruction is started
param.object_change_start = 1;% iteration when the object reconstruction is started
param.number_iterations = 300 ;
param.grouping = inf;
param.method = 'MLs';
param.likelihood = 'L1' ; % l1 or poisson, - choose which likelihood should be used for solver, poisson is suported only for PIE
param.verbose_level = 1;
param.plot_results_every = 50;
%tilted_sample_LB : I don't think this is needed
param.tilt_x = 0.0; %%LB
param.tilt_y = 0.0; %%LB
param.remove_residues = false; % autodetect and remove phase residua
param.extension = '';
%% data handling
param.upsampling_data_factor = 0; % assume that the data were created by upsampling using function utils.unbinning
param.damped_mask = 5e-3; % if damped_mask = 0 -> do nothing, if 1>x>0 -> push masked regions weakly towards measured magnitude value in each iteration
param.background_detection = false;
param.background_width = inf;
%% ADVANCED OPTIONS
param.object_regular = [0, 0]; % enforce smoothness !!!, use between [0-0.1 ]
param.remove_object_ambiguity = true; % remove intensity ambiguity between the object and the probes
param.variable_probe = false; % Use SVD to account for variable illumination during a single (coupled) scan
param.apply_subpix_shift = false; % apply FFT-based subpixel shift, important for good position refinement but it is slow
param.probe_geometry_model = {'scale', 'asymmetry', 'rotation', 'shear'}; % list of free parameters in the geometry model
param.probe_position_search = inf;
param.apply_relaxed_position_constraint = true; %added by YJ: allow position update without geom model constraint
param.update_pos_weight_every = inf; %added by YJ: allow position weight to be updated multiple times. Default = inf: only calculate once
param.max_pos_update_shift = 0.1; %added by YJ: allow user to specify the maximum position update allowed in each iteration. Default = 0.1 (pixel).
param.probe_position_search_momentum = 0; % added by YJ. enable momentum acceleration for position correction. Default = 0: no acceleration.
param.probe_fourier_shift_search = inf;
param.estimate_NF_distance = inf;
param.detector_rotation_search = inf; % rotation of the detector axis with respect to the sample axis, similar as rotation option in the position refinement geometry model but works also for 0/180deg rotation shared scans
param.detector_scale_search = inf; % pixel scale of the detector, can be used to refine propagation distance in ptycho
param.apply_multimodal_update = false; % use thibault modes to get higher signal, it can cause isses, not real gain if blur method is used
param.probe_backpropagate = 0;
param.beta_LSQ = 0.9; % use predictive step length
param.delta_p = 0.1; % LSQ damping constant
param.variable_probe_modes = 1; % OPRP settings
param.variable_probe_smooth = 0;% OPRP settings
param.variable_intensity = false; % account fort variable intensity
param.relaxed_object_constrain = 0; % enforce known object (inputs.object_orig)
param.probe_position_error_max = 10e-9; % max expected error of the stages
param.probe_fourier_shift_search = inf;
param.momentum = 0; % use mementume accelerated gradient decsent method
param.regularize_layers = 0; % 0<R<1 -> apply regularization on the reconstructed layers
param.preshift_ML_probe = true; % multilayer ptycho extension: if true, assume that the provided probe is reconstructed in center of the sample.
param.layer4pos = []; % Added by ZC. speficy which layer is used for position correction
param.init_layer_select = []; % Added by YJ. Select layers in the initial object for pre-processing If empty (default): use all layers.
param.init_layer_preprocess = ''; % Added by YJ. Specify how to pre-process initial layers
% '' or 'all' (default): use all layers (do nothing)
% 'avg': average all layers
% 'avg1': keep one averaged layer
% 'interp': interpolate layers using spline method. Need to specify desired depths in init_layer_interp
param.init_layer_interp = []; % Specify desired depths for interpolation. The depths of initial are [1:Nlayer_init]. If empty (default), no interpolation
param.init_layer_append_mode = ''; % Added by YJ. Specify how to initialize extra layers
% '' or 'vac' (default): add vacuum layers
% 'edge': append 1st or last layers
% 'avg': append averaged layer
param.init_layer_scaling_factor = 1; % Added by YJ. Scale all layers. Default: 1 (no scaling). Useful when delta_z is changed
param.initial_probe_rescaling = true; % find the optimal scaling correction for the provided probe guess in the initial iteration
param.accelerated_gradients_start = inf; % use accelerated gradients to speed up the convergence
param.align_shared_objects = false; % align multiple objects from various scans
% extra analysis
param.get_fsc_score = false; % measure evolution of the Fourier ring correlation during convergence
param.mirror_objects = false; % mirror objects, useful for 0/180deg scan sharing
param.align_shared_objects = false; % align the objects before sharing them onto single one
% fly scans
param.flyscan_offset = 0;
param.flyscan_dutycycle = 1;
%rng('default');
%rng('shuffle');
% convergence check - stop reconstruction if fourier error is larger than the previous one by given (relative) threshold.
param.fourier_error_threshold = inf; % default: no convergence check.
% I/O
param.save_init_probe = false; % Added by YJ. If true, save initial probe function in the .mat output file. Default is false.
param.save_images = {'obj_ph','obj_ph_sum','obj_ph_stack','probe'}; % Added by YJ. Save intermediate results as tiff images.
% Options: {'obj_ph','obj_ph_sum','obj_ph_stack','obj_mag','obj_ph_sum','obj_mag_stack','probe_mag','probe'}
end
@@ -0,0 +1,161 @@
% GET_PARALLEL_BLOCKS Find the optimal groups to be solved in parallel on GPU/CPU
%
%[cache, par] = get_parallel_blocks(self, par, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ par structure containing parameters for the engines
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [cache, par] = get_parallel_blocks(self, par, cache)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
import engines.GPU_MS.shared.*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% FIND MAXIMAL GROUP SIZE IF GPU IS USED %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
grouping_0 = par.grouping;
par.grouping = round(min(self.Npos, par.grouping));
global gpu
if par.use_gpu
%% !! very empirical estimation of the GPU memory requirements !!!
% use the estimated memory requirements to prevent low memory issues
[required_mem_constant] = estimate_req_memory(self, par, 0);
[required_mem] = estimate_req_memory(self, par, 1);
% precheck size of the block and try to optimize size of the groups
% allowed for given GPU
max_group_size = floor( (gpu.AvailableMemory - required_mem_constant) ./ (required_mem - required_mem_constant));
max_group_size = min(self.Npos, max_group_size);
verbose(1,'Maximal possible grouping %i', max_group_size);
% if group size was set to infinity, is the maximal group size possible
if isinf(grouping_0)
par.grouping= max_group_size;
else
% otherwise use max_group_size as a top limit
par.grouping = min(par.grouping, max_group_size);
end
% adjust grouping to minimize overhead -> make the group sizes more
% equal
if is_method(par, {'ML', 'PIE'})
% allows to calculate several scans together
par.grouping = ceil(self.Npos/ceil(self.Npos/par.grouping));
else
% consider each scan separatelly
Npos_scan = cellfun(@length, self.reconstruct_ind);
par.grouping = max(ceil(Npos_scan./ceil(Npos_scan./par.grouping)));
end
if par.grouping ~= grouping_0
verbose(1,'Optimal grouping was changed from %i to %i ', grouping_0, par.grouping);
end
if par.grouping < 1
error('Too low memory, use smaller dataset or try ePIE')
end
verbose(1,'Selected grouping %i', par.grouping);
else
if is_method(par, {'DM', 'ML'})
par.grouping = self.Npos;
end
end
% precalculate distance matrix for pseudo ePIE / hPIE / MLs to get
% least overlapping indices
if is_method(par, {'ML', 'PIE'})
if self.Npos/par.Nscans < 1e3 %added by YJ to save memory
for ll = 1:par.Nscans
dist_mat = single(distmat(self.probe_positions_0(self.reconstruct_ind{ll},:)));
dist_mat(dist_mat==0 | dist_mat > max(self.Np_p)/2) = inf;
cache.distances_matrix{ll} = dist_mat;
end
end
end
if is_method(par, 'MLc')
% get higly overlapping subsets of indices for PIE / ML
[cache.preloaded_indices_compact{1}.indices,cache.preloaded_indices_compact{1}.scan_ids] = ...
get_close_indices(self, cache, par );
elseif is_method(par, {'MLs', 'PIE'})
% preload order of indices , generate several of them to add randomness
for i = 1:min(par.number_iterations,10)
[cache.preloaded_indices_sparse{i}.indices,cache.preloaded_indices_sparse{i}.scan_ids] = ...
get_nonoverlapping_indices(self, cache, par );
end
end
% get just some predefined sets of indices - RAAR, DM , !! order
% does not matter
[cache.preloaded_indices_simple{1}.indices,cache.preloaded_indices_simple{1}.scan_ids] = ...
get_scanning_indices(self, cache, par );
end
@@ -0,0 +1,696 @@
% INITIALIZE_SOLVER initialize GPU ptycho reconstruction, generate cache values, fftshift data, etc
%
% [self, cache] = initialize_solver(self,par)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ cache structure with precalculated values to avoid unnecessary overhead
function [self, cache] = init_solver(self,par)
import engines.GPU_MS.shared.*
import math.*
import utils.*
import plotting.*
import engines.GPU_MS.GPU_wrapper.*
verbose(struct('prefix','GPU/CPU_MS-engine-init'))
par.Nscans = length(self.reconstruct_ind); %number of scans
cache.skip_ind = setdiff(1:self.Npos,[self.reconstruct_ind{:}]); % wrong datasets to skip
if ~any(self.probe_support(:))
self.probe_support = [];
end
%% avoid probe to be larger than a certain oversampling !!!!
if isempty(self.probe_support)
par.probe_backpropagate = 0;
end
if ~isempty(self.background) && any(self.background(:) > 0)
Background = self.background;
elseif par.background_detection
Background = 0;
else
Background = []; % array of background light
end
Noise = [];
%% prepare data / noise / mask
if par.relax_noise && ~isempty(self.noise) && strcmp(par.likelihood, 'L1')
Noise = self.noise;
Noise = (sqrt(posit(self.diffraction + Noise)) - sqrt(posit(self.diffraction - Noise)))/2;
Noise(self.diffraction == 0) = 1;
disp('Using measured noise')
Noise = max(0.5, Noise);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% PREPARE MASK AND DATA %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% prepare mask , note that bool in matlab has size of uint8 !!
cache.mask_indices = [];
if any(self.mask(:))
Mask = [];
% single mask
if all(all(mean(self.mask,3) == self.mask(:,:,1)))
Mask = self.mask(:,:,1);
else
% mask for each scan
for ll = 1:par.Nscans
ind = self.reconstruct_ind{ll};
%if there is only one repeated mask over whole scan
if all(all(all(bsxfun(@eq, self.mask(:,:,ind), self.mask(:,:,ind(1))))))
Mask(:,:,ll) = self.mask(:,:,ind(1));
end
cache.mask_indices(ind) = ll;
end
end
if isempty(Mask)
% mask for each position
Mask = self.mask; % otherwise just store original
cache.mask_indices(ind) = 1:self.Npos;
end
% important to save memory
if all(Mask(:) == 1 | Mask(:) == 0)
Mask = logical(Mask );
else
Mask = uint8(Mask*255); % if there are nonlogical values in mask, store them as uint8 to save memory
end
else
Mask = [];
end
%% prepare diffraction data
Diffraction = self.diffraction; % diffraction is intensity, not amplitude, comment by ZC
if par.upsampling_data_factor
% downsample the data down to original size to save memory
Diffraction = utils.binning_2D(Diffraction, 2^par.upsampling_data_factor) * (2^(2*par.upsampling_data_factor));
if ~isempty(Mask)
Mask = utils.binning_2D(Mask, 2^par.upsampling_data_factor) == 1;
end
end
Diffraction = single(max(0,Diffraction));
if ~isempty(Mask)
if size(Mask,3) == par.Nscans && par.Nscans > 1
for ll = 1:par.Nscans
ind = self.reconstruct_ind{ll};
Diffraction(:,:,ind) = Diffraction(:,:,ind) .* ~Mask(:,:,ll);
end
else
Diffraction = Diffraction .* ~Mask;
end
end
if ~isinf(self.z_distance(end)) % && mod(Ninf,2)~=0
% assume inputs already fftshifted, but in case of nearfield
% fftshift it back for the ASM propagator
Noise = fftshift_2D(Noise);
Diffraction = fftshift_2D(Diffraction);
Mask = fftshift_2D(Mask);
end
%%%% compress data if requested %%%%%%
if par.compress_data
DATA_MAX = quantile(max2(abs(Diffraction)), 1-1e-2);
C_factor_0 = 2; % compression factor >=2 seems to be safe, >=4 is pratically lossless
if par.compress_data == 1 || DATA_MAX < 2^(2*8) / C_factor_0^2
Diffraction = sqrt(single(Diffraction));
if DATA_MAX < 2^(2*8) / C_factor_0^2
% simple sqrt compression to 8 bits
verbose(1, 'Online data compression to 8-bits')
Diffraction = uint8(C_factor_0*Diffraction);
cache.C_factor = C_factor_0;
elseif DATA_MAX < 2^(2*16) / 16^2
% failsafe option: sqrt compression to 16 bits
verbose(1, 'Online data compression to 16-bits')
cache.C_factor = 16; % use compression factor 16, to be super safe just because we have space
Diffraction = uint16(cache.C_factor*Diffraction);
else
error('Online compression will fail')
end
elseif par.compress_data == 2
% SVD subtraction compression to 8 bits (failsafe is compression to 16bits)
% additionally remove some SVD modes
Diffraction = sqrt(single(Diffraction));
Nmodes = par.Nscans;
[U,S,V] = fsvd(reshape(Diffraction,prod(self.Np_p),[]), Nmodes);
ind_relevant = diag(S).^2/sum(diag(S).^2) > 1e-2; % more than 1% of power
cache.US_diffraction = (U(:,ind_relevant)*S(ind_relevant,ind_relevant));
cache.V_diffraction = V(:,ind_relevant);
svd_Diffraction = round(reshape(cache.US_diffraction*cache.V_diffraction',[self.Np_p, self.Npos]));
%% compress
cDiffraction = single(Diffraction) - svd_Diffraction;
% reestimate optimal compression factor to keep values < 128
C_factor = min(C_factor_0, 128/quantile(max2(abs(cDiffraction)), 1-1e-2));
if C_factor > 3
verbose(1, 'Online data compression to 8-bits + SVD')
cache.C_factor = C_factor;
cache.US_diffraction = cache.US_diffraction;
cache.V_diffraction = cache.V_diffraction*C_factor;
Diffraction = int8(cDiffraction*C_factor);
elseif DATA_MAX < 2^(2*16) / 16^2
% sqrt compression to 16 bits
%warning(sprintf('Too high online compression of data, it may cause problems\n Compression factor is %2.2f but should be >= 2\n Switching from 8 to 16bits',C_factor))
verbose(1, 'Online data compression to 16-bits')
C_factor = 16;
cache.C_factor = C_factor;
Diffraction = uint16(C_factor*Diffraction);
else
error('Online compression will fail')
end
clear svd_Diffraction cDiffraction
else
error('Unimplented level of compression')
end
else
% precalculate sqrt from the data, store as singles
Diffraction = sqrt(single(max(0,Diffraction))); % diffraction is amplitude, comment by ZC
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% write back the data arrays
self.diffraction = Diffraction; % diffraction is amplitude, comment by ZC
self.mask = Mask;
self.noise = Noise;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% PREPARE GEOMETRY, PROPAGATION, MODES%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% precalculate ASM factor for propagation distance recovery
[ASM_difference] = near_field_evolution_gradient(self.Np_p, self.lambda, self.pixel_size .*self.Np_p );
cache.ASM_difference = fftshift(ASM_difference);
% custom propagator to account for tilted plane sample - this chuck is
% basically not used since the lines bellow are commented out - LB
if any(par.p.sample_rotation_angles(1:2)) && check_option(par.p, 'apply_tilted_plane_correction', 'propagation')
% get propagators to the tilted plane
[tilted_plane_propagate_fwd, tilted_plane_propagate_back] = ...
get_tilted_plane_propagators(Garray(self.probe{1}), ...
[par.p.sample_rotation_angles(1:2),0],...
self.lambda, self.pixel_size);
else
tilted_plane_propagate_fwd = []; tilted_plane_propagate_back = [];
end
if ~iscell(self.affine_matrix)
self.affine_matrix = {self.affine_matrix};
end
%Note: par.Nmodes is # of probe modes (p.probe_modes). Assigned in
%load_from_p.m
% modes = cell(max(par.Nmodes, par.Nlayers),1);
modes = cell(par.Nlayers,1); % corrected by Zhen Chen
%Comment by YJ: par.Nmodes seems to be # of modes for A-fly scan
% for i = 1:max(par.Nmodes, par.Nlayers)
%par.Nlayers equals to the # of slices in the object (excluding the
%vacuum layer)
for i = 1:par.Nlayers % modified by ZC
verbose(2,'Creating new modes files ')
modes{i}.lambda = self.lambda;
% decompose affine matrix into scale, asymmetry, rotation, shear
%affine = scale*[1+asym/2,0; 0,1-asym/2]*[cosd(rot), sind(rot); -sind(rot), cosd(rot)] * [1,0;tand(shear),1];
affine_matrix = self.affine_matrix{min(i,end)};
[scale, asymmetry, rotation, shear] = decompose_affine_matrix(affine_matrix);
% store initial geometry parameters
modes{i}.scales = repmat(scale, 1,par.Nscans);
modes{i}.asymmetry = repmat(asymmetry, 1,par.Nscans);
modes{i}.shear = repmat(shear, 1,par.Nscans);
modes{i}.rotation = repmat(rotation, 1,par.Nscans);
modes{i}.affine_matrix = repmat(affine_matrix, 1,1,par.Nscans);
modes{i}.shift_scans = zeros(2, par.Nscans);
modes{i}.probe_scale_upd = 0;
modes{i}.probe_rotation = ones(1,par.Nscans) * par.sample_rotation_angles(3); % one rotation per scan
if par.mirror_objects
modes{i}.probe_rotation = modes{i}.probe_rotation .* [1,-1]; % flip the coordinates for mirrored object (ie 0 vs 180deg rotation)
end
modes{i}.probe_rotation_all = zeros(self.Npos,1);
for jj = 1:par.Nscans
modes{i}.probe_rotation_all(self.reconstruct_ind{jj}) = modes{i}.probe_rotation(jj); % one rotation per scan
end
distance = self.z_distance(min(end,i));
if ~isinf(distance)
verbose(2, 'Layer %i distance %g um ', i, distance*1e6 )
end
modes{i}.distances = distance;
if is_used(par, 'fly_scan') && (~isfield(modes{i}, 'probe_positions') || isempty(modes{i}.probe_positions) )
%% get positions for fly scans
self = prepare_flyscan_positions(self, par);
modes{i}.probe_positions = self.modes{i}.probe_positions; %added by YJ. seems like a bug
modes{i}.probe_positions_0 = self.probe_positions_0; %added by YJ. seems like a bug
else
%% get positions for normal tomo
try % try to reuse the positions of there are saved
modes{i}.probe_positions = self.modes{i}.probe_positions;
verbose(2,'Using saved positions')
catch
if (modes{i}.scales(end) == modes{1}.scales(end)) && ~isempty(self.probe_positions)
modes{i}.probe_positions = self.probe_positions;
verbose(0,'Using saved positions')
else
verbose(2,'Using original positions')
modes{i}.probe_positions = (affine_matrix*self.probe_positions_0')';
end
end
try
modes{i}.probe_positions_0 = self.modes{i}.probe_positions_0;
catch
modes{i}.probe_positions_0 = self.probe_positions_0;
end
end
modes{i}.probe_positions_update = { zeros(size(modes{i}.probe_positions)) };
modes{i}.probe_positions_all = {modes{i}.probe_positions};
modes{i}.probe_positions_weight = zeros(self.Npos, 1);
if isfield(self, 'probe_fourier_shift') && ~isempty(self.probe_fourier_shift) && i == 1
modes{i}.probe_fourier_shift = self.probe_fourier_shift;
else
modes{i}.probe_fourier_shift = zeros(self.Npos,2);
end
if ~isempty(self.probe_support) && i <= par.Nrec
modes{i}.probe_support = self.probe_support;
if i == 1
verbose(2,'Using real-space probe support')
end
else
modes{i}.probe_support = [];
end
if ~isempty(self.probe_support_fft) && i <= par.Nrec && ~check_option(par,'probe_support_tem')
modes{i}.probe_support_fft = fftshift(self.probe_support_fft);
if i == 1
verbose(2,'Using far-field probe support')
end
elseif check_option(par,'probe_support_tem') % not shift for TEM aperture, by Zhen Chen
modes{i}.probe_support_fft = self.probe_support_fft;
else
modes{i}.probe_support_fft = [];
end
F = mean( self.pixel_size)^2 .* mean(self.Np_p) / (modes{i}.lambda * modes{i}.distances);
if F ~= 0
verbose(3,'Nearfield propagation: Fresnel number/Npix %3.3g', F)
end
scale = modes{i}.scales(end);
modes{i}.ASM_factor = [] ;
modes{i}.cASM_factor = [] ;
if ~isinf(modes{i}.distances(end)) % Forward Fresnel propagator in k-space, ASM, commented by ZC
%% near field factor
%ASM = exp( modes{i}.distances(end)* cache.ASM_difference);
% modified by YJ: use H instead of dH (which is an approximation)
%[~,ASM,~,~] = near_field_evolution(ones(self.Np_p), modes{i}.distances(end), self.lambda, self.pixel_size .*self.Np_p, true );
tiltx = par.tilt_x*1e-3;
tilty = par.tilt_y*1e-3;
[~,ASM,~,~] = near_field_evolution(ones(self.Np_p), modes{i}.distances(end), self.lambda, self.pixel_size .*self.Np_p, true,tiltx,tilty );
ASM = fftshift(ASM);
modes{i}.ASM_factor = ASM;
modes{i}.cASM_factor = conj(ASM);
end
%% far field factor
modes{i}.FAR_factor = [];
modes{i}.cFAR_factor = conj(modes{i}.FAR_factor);
if isinf( par.probe_backpropagate)
modes{i}.support_fwd_propagation_factor = inf;
modes{i}.support_back_propagation_factor = -inf;
elseif par.probe_backpropagate ~= 0
[~, modes{i}.support_fwd_propagation_factor] = utils.prop_free_nf( self.probe{1}(:,:,1), par.probe_backpropagate,...
modes{i}.lambda, self.pixel_size ./ scale );
modes{i}.support_fwd_propagation_factor = fftshift( modes{i}.support_fwd_propagation_factor );
modes{i}.support_back_propagation_factor = conj(modes{i}.support_fwd_propagation_factor);
else
modes{i}.support_fwd_propagation_factor = [];
modes{i}.support_back_propagation_factor = [];
end
%I am a bit confused on why this is commented out?? - the tilted
%plane propogators are caleed in the ptycho solver
% modes{i}.tilted_plane_propagate_fwd = tilted_plane_propagate_fwd;
%modes{i}.tilted_plane_propagate_back = tilted_plane_propagate_back;
modes{i}.tilted_plane_propagate_fwd = [];
modes{i}.tilted_plane_propagate_back = [];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% PREPARE PROBES, INCOHERENT MODES %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
probe_0 = mean(self.probe{1},3);
probe = cell(1,par.probe_modes); % Added by ZC and fixed by YJ
for i = 1:par.probe_modes
try
probe{i} = self.probe{i};
%% test if the probe size ok for the variable probe settings etc
assert( size(probe{i},4) == 1+par.variable_probe_modes || ...
~(par.variable_probe) || i > 1)
assert((size(probe{i},3) ==1 || par.variable_probe) || ...
(size(probe{i},3) == par.Nscans && ~par.share_probe) ) % no variable prob extension and multiple probes used
assert(size(probe{i},3) == par.Nscans || par.share_probe || par.variable_probe, 'Wrong probe size for not shared probe option')
catch
if i <= par.Nrec || is_used(par, 'fly_scan')
verbose(2, 'Creating probe')
if ~par.share_probe && size(probe{i},3) == 1
% dont share probe between scans
probe{i} = repmat(probe_0,[1,1,par.Nscans]);
end
if (par.variable_probe && par.variable_probe_modes > 0) && i == 1
verbose(2,'Creating variable probe ')
probe{i}(:,:,:,2:1+par.variable_probe_modes) = ...
randn([self.Np_p, size(probe{i},3), par.variable_probe_modes])+randn([self.Np_p,size(probe{i},3), par.variable_probe_modes])*1i;
continue
end
end
if length(probe) < i % none of above
% simply create slightly shifted modes in fourier domain, it is useful for
% inital guess of incoherent modes after orthogonalization
step = median(diff(self.probe_positions_0));
probe{i} = 0.01*fftshift(imshift_fft(fftshift(probe_0), randn, randn, false));
end
% fill the unreconstructed positions if the OPRP method is used
if par.variable_probe && is_method(par, 'PIE') && i ==1
ind_wrong = setdiff(1:self.Npos, [self.reconstruct_ind{:}]);
probe{i}(:,:,ind_wrong) = repmat(mean(probe{i},3),1,1,length(ind_wrong));
end
end
end
if par.probe_modes > par.Nrec
% orthogonalization of incoherent probe modes
if is_used(par, 'fly_scan')
probe_tmp = probe;
% orthogonalize the modes with all the other shifted modes
for i = 1:par.Nrec
dx = median(modes{i}.probe_positions - modes{1}.probe_positions);
probe_tmp{i} = imshift_fft(probe_tmp{i}, dx);
end
probe_tmp = ortho_modes(probe_tmp); % perform othogonalization
probe(1+par.Nrec:par.probe_modes) = probe_tmp(1+par.Nrec:par.probe_modes);
else
ind = [1,1+par.Nrec:par.probe_modes]; % skip polyvave/multilayer probe_tmp
probe(ind) = ortho_modes_eig(probe(ind)); %% slightly better
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% PREPARE OBJECT, MULTILAYER OBJECT %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% updated illumination
aprobe2 = abs(self.probe{1}(:,:,1)).^2;
for ll = 1:par.Nscans
if par.share_object
ind = [self.reconstruct_ind{:}];
else
ind = self.reconstruct_ind{ll};
end
[cache.oROI_s{1}] = find_reconstruction_ROI( modes{1}.probe_positions,self.Np_o, self.Np_p);
% avoid oscilations by adding momentum term
illum_sum_0{ll} = Ggather(set_views(Gzeros(self.Np_o), Garray(aprobe2), 1,1, ind, cache));
end
%% multilayer extension
% modified by YJ for more dynamic initialization
% Step 1: choose specific layers from initial object file
N_layer_input_obj = size(self.object,2);
par.init_layer_select(par.init_layer_select<0) = [];
par.init_layer_select(par.init_layer_select>N_layer_input_obj) = [];
if ~isempty(par.init_layer_select)
object_temp = cell(size(self.object,1),length(par.init_layer_select));
for ll = 1:par.Nscans
for jj=1:length(par.init_layer_select)
object_temp{ll,jj} = self.object{ll,par.init_layer_select(jj)};
end
end
self.object = object_temp;
end
% Step 2: pre-process layers
switch par.init_layer_preprocess
case 'avg' % only use the averaged layer
verbose(0,'Average initial layers')
for ll = 1:par.Nscans
obj_avg = prod(cat(3,self.object{ll,:}),3);
obj_avg = abs(obj_avg).*exp(1i*phase_unwrap(angle(obj_avg))/size(self.object,2));
for jj=1:size(self.object,2)
self.object{ll,jj} = obj_avg;
end
end
case 'avg1' % only use the averaged layer
verbose(0,'Average initial layers and only keep one')
object_temp = cell(size(self.object,1),1);
for ll = 1:par.Nscans
obj_avg = prod(cat(3,self.object{ll,:}),3);
obj_avg = abs(obj_avg).*exp(1i*phase_unwrap(angle(obj_avg))/size(self.object,2));
object_temp{ll,1} = obj_avg;
end
self.object = object_temp;
case 'interp' % interpolate layers
if ~isempty(par.init_layer_interp)
verbose(0,'Interpolate %d initial layers to %d layers', size(self.object,2), length(par.init_layer_interp))
for ll = 1:par.Nscans
obj_temp = cat(3,self.object{ll,:});
[N_obj_y,N_obj_x,N_obj_z] = size(obj_temp);
[X,Y,Z] = meshgrid(linspace(1,N_obj_x,N_obj_x),linspace(1,N_obj_y,N_obj_y),linspace(1,N_obj_z,N_obj_z));
[Xq,Yq,Zq] = meshgrid(linspace(1,N_obj_x,N_obj_x),linspace(1,N_obj_y,N_obj_y),par.init_layer_interp);
obj_temp = interp3(X,Y,Z,obj_temp,Xq,Yq,Zq,'spline');
for jj=1:size(obj_temp,3)
self.object{ll,jj} = obj_temp(:,:,jj);
end
end
end
case {'','all'} % default: keep all layers
% nothing to do
otherwise
error('Invalid init_layer_preprocess!')
end
% Step 3: add or remove layers based on par.Nlayers
if size(self.object,2) > par.Nlayers
warning('Initial object has more layers than Nlayers')
for ll = 1:par.Nscans
self.object{ll,1} = prod(cat(3,self.object{ll,:}),3);
end
self.object(:,2:end) = [];
end
if size(self.object,2) < par.Nlayers
N_add = par.Nlayers - size(self.object,2);
verbose(0,'Add %d more layers from %d layer(s)', N_add, size(self.object,2))
for ll = 1:size(self.object,1) %loop over scans
obj{ll} = self.object(ll,:);
switch par.init_layer_append_mode
case 'avg' %Not sure when this is useful, but I'll keep it for now
verbose(0,'Append averaged layer')
obj_pre = prod(cat(3,self.object{ll,:}),3);
obj_pre = abs(obj_pre).*exp(1i*phase_unwrap(angle(obj_pre))/size(self.object,2));
obj_post = obj_pre;
case 'edge'
verbose(0,'Append 1st/last layer')
obj_pre = self.object{ll,1};
obj_post = self.object{ll,end};
case {'','vac'}
verbose(0,'Append vacuum layer')
%obj_pre = ones(self.Np_o, 'single') + 1e-9i*randn(self.Np_o, 'single');
obj_pre = ones(self.Np_o, 'single');
obj_post = obj_pre;
otherwise
error('Invalid init_layer_append_mode!')
end
for ii = 1:N_add
if mod(ii, 2) == 1
obj{ll}{end+1} = obj_post; % add slice at the end
else
obj{ll}(2:end+1) = obj{ll};
obj{ll}{1} = obj_pre; % add slice at the beginning
end
end
end
self.object = cat(1, obj{:}); %combine all scans
end
% if object has more layers but only one is needed
if size(self.object,2) > 1 && par.Nlayers == 1
for ll = 1:par.Nscans
object{ll,1} = prod(cat(3,self.object{ll,:}),3);
end
self.object = object;
end
% At this point: size(self.object,3) should equal to par.Nlayers
%{
%% MO's code, should be useless now. I'll keep it for now in case of
bugs in steps 1-3.
for j = 1:par.Nlayers
for i = 1:max(1, par.Nscans * ~par.share_object) % loop over scans
try
object{i,j} = self.object{min(end,i),j};
object{i,j}(1);
catch
verbose(0, 'add transparent slice') % add extra layers
%% add fully transparent slice at the end
object{i,j} = ones(self.Np_o, 'single');
if size(self.object,2) == 1
% swap order of the new layers to keep the original
% reconstruction in center
object(i,:) = object(i,end:-1:1);
end
end
end
end
%}
% Step 4: assign self.object to object and rescale layers if needed
if par.init_layer_scaling_factor~=1
verbose(0,'Rescale each layer by %f', par.init_layer_scaling_factor)
end
for j = 1:par.Nlayers
for i = 1:max(1, par.Nscans * ~par.share_object) % loop over scans
if par.init_layer_scaling_factor~=1
object_temp = self.object{min(end,i),j};
object_temp_ph = phase_unwrap(angle(object_temp))*par.init_layer_scaling_factor;
object{i,j} = abs(object_temp).*exp(1i.*object_temp_ph);
else
object{i,j} = self.object{min(end,i),j};
end
end
end
for i = 1:numel(object)
object{i} = single(object{i});
object{i} = complex(object{i});
end
for i = 1:numel(probe)
probe{i} = single(probe{i});
probe{i} = complex(probe{i});
end
%% STORE RESULTS TO SELF CLASS
self.object = object;
self.probe = probe;
self.modes = modes;
self.diffraction = Diffraction;
self.noise = Noise;
self.mask = Mask;
self.background = reshape(Background,1,1,[]);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%% PRECALCULATE USEFUL VALUES %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isfield(self, 'probe_evolution' )
% initial coefficients for OPRP approximation
self.probe_evolution(:,1) = ones(self.Npos,1); % first mode is constant
end
new_probe_modes_ind = 1+(size(self.probe_evolution,2):par.variable_probe_modes);
self.probe_evolution(:,new_probe_modes_ind) = 1e-6*randn(self.Npos,length(new_probe_modes_ind));
if par.variable_probe
pnorm = norm2(self.probe{1});
self.probe{1}(:,:,:,2:end) = self.probe{1}(:,:,:,2:end) ./ pnorm(1,1,:,2:end);
self.probe_evolution(:,2:end) = self.probe_evolution(:,2:end) .* squeeze(mean(pnorm(1,1,:,2:end),3))';
end
if par.background_detection || ~isempty(self.background)
%% auto-estimate background correction distribution
if isempty(self.mask)
mask = 0;
else
mask = self.mask;
end
if par.background_detection
background_weight = sum(self.diffraction.^2,3) ./ max(1,sum(~mask,3));
background_weight = imgaussfilt(background_weight,1);
background_weight = 1./sqrt(max(1e-3, background_weight)) .* ~any(mask,3);
background_weight = max(0,background_weight - 0.3*mean(background_weight(:)));
cache.background_weight = ( fftshift_2D(background_weight / sum2(background_weight)));
end
if isinf(par.background_width)
cache.background_profile = 1;
else
mdiffr = Garray(fftshift(mean(get_modulus(self,cache,1:self.Npos,false).^2,3)));
W = par.background_width;
X = (-self.Np_p(1):self.Np_p(1)-1);
Y = (-self.Np_p(2):self.Np_p(2)-1);
[X,Y] = meshgrid(X,Y);
background_profile = exp(-sqrt( (X/W(1)).^2 +(Y/W(1)).^2));
background_profile = conv2(mdiffr,background_profile, 'same');
background_profile = background_profile / max2(background_profile);
background_profile = utils.crop_pad(background_profile,self.Np_p);
cache.background_profile = gather(fftshift(background_profile));
end
if ~isempty(self.diffraction_deform_matrix)
apply_deform = @(x,D)single(reshape(full(D * double(reshape(x,[],size(x,3)))), size(x)));
% apply deformation effects caused by tilted sample , be sure to enforce the mask before
% interpolation, the hotpixels can spread around after the correction
if isscalar(cache.background_profile)
cache.background_profile = ones(self.Np_p, 'single');
end
cache.background_profile = apply_deform(cache.background_profile, self.diffraction_deform_matrix');
end
else
cache.background_profile_weight = 1;
end
for ll = 1:par.Nscans
illum_sum_0{ll} = Ggather(illum_sum_0{ll});
cache.MAX_ILLUM(ll) = max(illum_sum_0{ll}(:));
cache.illum_sum_0{ll} = illum_sum_0{ll};
end
%% precalculate illumination ROIs
cache = precalculate_ROI(self,cache, Ggather(sqrt(aprobe2)));
%% prepare mask needed for subpixel shifts of object views
cache.apodwin = single(0.1+0.9*tukeywin(self.Np_p(1),0.05) .* tukeywin(self.Np_p(2), 0.05)');
if par.initial_probe_rescaling
%% initial rescaling of probe intensity , just a very rough guess
% modified by ZC, propagate probe to far field
mean_aPsi = mean2(abs(fft2_safe(self.probe{1}(:,:,1))).^2);
% old method: self.modes{end} is the vaccum layer
%mean_aPsi = mean2(abs(fwd_fourier_proj(self.probe{1}(:,:,1), self.modes{end})).^2);
mean_diffraction_intensity = mean(mean2(self.diffraction(:,:,randi(self.Npos, [10,1])).^2)); % take roughly average intensity % bug fixed by Zhen Chen, previous no ^2
for ii = 1:par.probe_modes
self.probe{ii} = self.probe{ii} * sqrt( mean_diffraction_intensity / mean_aPsi);
end
end
end
@@ -0,0 +1,457 @@
% LOAD_FROM_P load parameters from the p-structure to param and self structures for GPU
% engine
%
% [self, param] = load_from_p(self, param, p)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** param structure containing parameters for the engines
% ** p ptychoshelves p structure
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ++ param structure containing parameters for the engines
function [self, param, p] = load_from_p(param, p)
import math.*
import utils.*
import engines.GPU_MS.shared.*
[Np_p(1),Np_p(2),Npos] = size( p.fmag);
self.reconstruct_ind = p.scanidxs;
%% load default variables with different name from the main ptycho code
param.Nmodes = p.probe_modes;
param.plot_results_every = p.plot.interval;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% additional features added by YJ
%for output intermediated results,
param.fout = p.fout;
if isfield(p, 'save_results_every')
param.save_results_every = p.save_results_every;
if param.save_results_every<=p.number_iterations
verbose(0, 'Intermediate results will be saved in the directory below every %i iterations.', param.save_results_every)
verbose(0,param.fout)
end
end
if isfield(p, 'extraPrintInfo')
param.extraPrintInfo = p.extraPrintInfo;
end
if isfield(p, 'affine_matrix')
param.affine_matrix_init = p.affine_matrix;
end
if isfield(p, 'beam_source')
param.beam_source = p.beam_source;
end
if isfield(p, 'TV_lambda')
param.TV_lambda = p.TV_lambda;
end
if isfield(p,'avg_photon_threshold') && p.avg_photon_threshold>=0
avg_photon_threshold = p.avg_photon_threshold;
else %default
if isfield(param,'beam_source') && strcmp(param.beam_source,'electron')
avg_photon_threshold = 0.0001;
else
avg_photon_threshold = 0.01;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%
% if defined in p, use from p otherwise use defaults in param
try;param.object_regular = p.reg_mu; end
try;param.probe_inertia = p.probe_regularization; end
param.tilt_x = p.tilt_x; %%LB
param.tilt_y = p.tilt_y; %%LB
if check_option(p,'opt_errmetric','poisson')
param.likelihood = 'poisson';
else
param.likelihood = 'L1';
end
if get_option(p,'background_width') && get_option(p,'binning')
param.background_width = param.background_width / 2^p.binning;
end
%% load variables from the main ptycho code and merge it with the defaults
for field = fieldnames(param)'
field = field{1};
if isfield(p, field)
param.(field) = p.(field);
end
end
% set verbosity for GPU engine
param.verbose_level = max(-2,p.verbose_level-2); % adjust verbosity for GPU code , verbose_level 0 is enough for commmon use
verbose(param.verbose_level)
% load additional reconstructed parameters , otherwise use default
for item = {{'background',[]}, {'intensity_corr',[]}, {'probe_fourier_shift',[]}, {'rotation',0},{'shear',0},{'relative_pixel_scale',1}}
item = item{1};
if isfield(p, item{1}) && ~isempty(p.(item{1}))
self.(item{1}) = p.(item{1});
else
self.(item{1}) = item{2};
end
end
if any(ismember(fieldnames(p), {'shear', 'rotation', 'relative_pixel_scale'})) && isfield(p, 'positions_0')
warning('Reseting probe positions to original values')
p.positions = p.positions_0;
p = rmfield(p, 'positions_0');
end
if isempty(p.affine_matrix)
p.affine_matrix = diag([1,1]);
end
self.diffraction_deform_matrix = [];
if ~check_option(p,'asize_presolve')
param.Np_p_presolve = [];
else
param.Np_p_presolve = min(p.asize_presolve, p.asize);
end
% Other parameters
param.fourier_ptycho = check_option(p,'fourier_ptycho');
param.upsampling_data_factor = p.detector.upsampling;
% Offaxis ptychography correction
if check_option(p, 'sample_rotation_angles')
param. sample_rotation_angles = p.sample_rotation_angles; % 3x1 vector rotation around [X,Y,beam] axes in degrees , apply a correction accounting for tilted plane oR the sample and ewald sphere curvature (high NA correction)
else
param. sample_rotation_angles = [0,0,0]; % conventional ptychography
end
self.pixel_size = p.dx_spec;
self.Np_p = Np_p;
Nscans = length(self.reconstruct_ind);
%%%%%%%%%%%%%%%%%%%%%%
%% load probes%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
if param.share_probe
p.share_probe_ID(:) = 1; % enforce single object if sharing is requested
end
% normalization for consistency with the other CPU engines
probes = single(p.probes ./ (prod(sqrt(Np_p))*2*p.renorm));
% force to a column cell convenient for multilayer, Added by ZC
%self.probe = cell (min(p.probe_modes, size(probes,4)),1);
for i = 1:min(p.probe_modes, size(probes,4))
% variable probe
if isfield(p, 'probe_PCA') && ~isempty(p.probe_PCA) && i == 1 && size(p.probe_PCA.eigen_vec,1) == p.asize(1) && param.variable_probe && is_method(param, 'PIE')
verbose(1,'Loading PCA probe (%i)', i)
self.probe{i} = reshape(p.probe_PCA.eigen_vec,prod(p.asize),[]) * p.probe_PCA.evolution' /(prod(sqrt(Np_p))*2*p.renorm);% normalization for consistency with the CPU code;
elseif isfield(p, 'probe_variable') && ~isempty(p.probe_variable) && i == 1 && size(p.probe_variable.eigen_vec,1) == p.asize(1) && param.variable_probe && is_method(param, 'ML')
% ML methods, OPRP approx
verbose(0,'Loading variable probe (%i)', i)
% constant part
self.probe{i}(:,:,:,1) = probes(:,:,:,1);
% variable part
self.probe{i}(:,:,:,2) = p.probe_variable.eigen_vec /(prod(sqrt(Np_p))*2*p.renorm);
% evolution of the variable part
self.probe_evolution = p.probe_variable.evolution ;
assert(length(self.probe_evolution)==Npos, 'Wrong size of variable probe coefficients')
else
% constant probe
verbose(1,'Loading constant probe (%i)', i)
if param.share_probe
self.probe{i} = mean(probes(:,:,:,i),3);
else
if size(p.probes,3) == Nscans
% one probe for each scan
self.probe{i} = probes(:,:,:,i);
else
% rather take only first to avoid issues
self.probe{i} = probes(:,:,1,i);
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% prepare support contraints%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% provide estimate of the probe support
if check_option(p, 'probe_mask') && check_option(p,'use_probe_support') && any(p.probe_mask)
self.probe_support = ~p.probe_mask;
elseif check_option(p,'probe_support_radius') && p.probe_support_radius < sqrt(2)
% very useful for DM code
[X,Y] = meshgrid((-p.asize(2)/2+1:p.asize(2)/2)/p.asize(2), (-p.asize(1)/2+1:p.asize(1)/2)/p.asize(1));
self.probe_support = sqrt(X.^2+Y.^2) < p.probe_support_radius/2;
else
self.probe_support = [];
end
% estimate of the probe support in detector plane
if check_option(p,'probe_support_fft') && ~check_option(p, 'prop_regime', 'nearfield') && ~check_option(p,'probe_support_tem') % exclude TEM aperture mask by ZC
if ~check_option(p.model, 'probe_focal_length') && ~check_option(p.model, 'probe_outer_zone_width')
error('Missing model.probe_focal_length and model.probe_outer_zone_width of Fresnel zone plate' )
end
if ~check_option(p.model, 'probe_outer_zone_width')
p.model.probe_outer_zone_width = p.lambda * p.model.probe_focal_length / p.model.probe_diameter;
end
FZP_cone_diameter = p.lambda* p.z/(p.model.probe_outer_zone_width * p.ds);
% add some extra space
FZP_cone_diameter = FZP_cone_diameter * 1.2;
[X,Y] = meshgrid(-p.asize(2)/2+1:p.asize(2)/2, -p.asize(1)/2+1:p.asize(1)/2);
self.probe_support_fft = utils.imgaussfilt2_fft(sqrt(X.^2+Y.^2) < FZP_cone_diameter/2, FZP_cone_diameter/50);
af_probe = sqrt(abs(fftshift(fft2(self.probe{1}(:,:,1)))));
[cx, cy] = center(max(0,af_probe-0.1*max(af_probe(:))));
self.probe_support_fft = imshift_fast(self.probe_support_fft, -cx, -cy,[], 'nearest');
self.probe_support_fft = max(0, min(1, self.probe_support_fft));
verbose(1, 'Using farfield probe support constraint')
elseif check_option(p,'probe_support_tem') % TEM aperture mask by ZC
mask_dp=abs(fft2(p.probe_initial));
mask_dp(mask_dp>1.0)=1;
mask_dp(mask_dp<0.1)=0;
self.probe_support_fft = logical(mask_dp);
else
self.probe_support_fft = [];
end
%%%%%%%%%%%%%%%%%%%%%%
%% load object%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%
if param.share_object
p.share_object_ID(:) = 1; % enforce single object if sharing is requested
end
% correct positions for sample tilt
positions = p.positions ;
if isfield(p, 'positions_0')
positions_0 = p.positions_0 ;
end
verbose(1, 'Load and pad object and probe')
% update current size of the object
for i = 1:length(p.object)
for j = 1:size(p.object{i},4) % load multiple layers of the object
self.object{i,j} = p.object{i}(:,:,1,j);
end
p.object_size(i,:) = size(self.object{i,1});
end
% calculate optimal size !! find minimal object to fit all scans
Np_o = max(p.object_size,[],1);
% expand object size if the probe p
if p.number_iterations > check_option(p, 'probe_position_search') && is_method(param, {'ML', 'PIE'})
extra = 0.2; % add plenty of extra space for geometry refinement
else
extra = 0.05; % do just a little of extra space
end
% shift the positions to account for the expanded object size AND
% center them !!! (GPU code assumes positions to be centered, better for scale / probe positions are unknown)
%%%%%%%%%%%%%%%%%%%%%%
%% load positions %%%%
%%%%%%%%%%%%%%%%%%%%%%
for i = unique(p.share_object_ID)
ind = [self.reconstruct_ind{p.share_object_ID == i}];
position_offset = 1+floor((max(positions(ind,:))-min(positions(ind,:)))/2 + min(positions(ind,:)) );
if isfield(p, 'positions_0')
self.probe_positions_0(ind,:) = positions_0(ind,:) - position_offset;
self.probe_positions(ind,:) = positions(ind,:) - position_offset;
else
self.probe_positions_0(ind,:) = positions(ind,:) - position_offset;
self.probe_positions = [];
end
end
% get object extent
self.Np_o = max(Np_o, ceil((1+extra) * ( self.Np_p + (max(self.probe_positions_0) - min(self.probe_positions_0)) )));
% store object size without padding, useful for plotting
p.object_size = max(p.object_size, ceil(( self.Np_p + (max(self.probe_positions_0) - min(self.probe_positions_0)) )));
self.probe_positions_0 = self.probe_positions_0(:,[2,1]); %swap x&y axis
if ~isempty(self.probe_positions)
self.probe_positions = self.probe_positions(:,[2,1]);
end
self.Npos = Npos;
% only a relative correction with respect to the affine matrix already
% applied in p-struct
for ii = 1:Nscans
self.affine_matrix{ii} = diag([1,1]);
end
%%%%%%%%%%%%%%%%%%%%%%
%% adjust object %%%%%
%%%%%%%%%%%%%%%%%%%%%%
for i = 1:size(self.object,1)
for layer = 1:size(self.object,2)
% if needed expand the object to allow position refinement
% and shift for consistency with the CPU code
self.object{i,layer} = imshift_fast(self.object{i,layer},1,1,self.Np_o, 'nearest', mean(self.object{i,layer}(:)));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% load data, mask noise %%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
verbose(1, 'Preparing data and masks')
assert(all(isfinite(p.fmag(:))), 'Provide p.fmag contains NaN/Inf')
self.noise = [];
self.diffraction = (single(p.fmag .* p.fmask) / single(p.renorm) ).^2;
self.mask = (~p.fmask);
if check_option(p, 'damped_mask')
% if relaxed mask is used, try to fill the smallest gaps (hot pixels) by neighbors
mask_ind = find(self.mask);
self.diffraction(mask_ind) = self.diffraction(min(mask_ind+1, numel(self.diffraction)));
end
low_photon_count_dp = sum(sum(self.diffraction)) / prod(p.asize) < avg_photon_threshold;
if any(low_photon_count_dp)
error('%0.2f%% diffraction patterns has average photon count < %f', sum(low_photon_count_dp)/size(self.diffraction,3)*100, avg_photon_threshold)
end
%% automatic data centering / flipping / tilted plane correction
if check_option(p, 'auto_center_data') || check_option(p, 'custom_data_flip') || check_option(p, 'sample_rotation_angles')
self.diffraction = fftshift_2D(self.diffraction);
self.mask = fftshift_2D(self.mask);
warning on
warning off backtrace
if check_option(p, 'auto_center_data')
warning('Enforcing automatic data centering')
for ii = 1:Nscans
[x0,y0]=math.center(abs(fftshift(fft2(fftshift(self.probe{1}(:,:,min(end,ii)))))));
W = mean2(self.diffraction(:,:,self.reconstruct_ind{ii}));
W = ((W - min(W)) / (max(W)-min(W))).^4; % give more weight to the more transpared regions (air)
avg_pattern = mean(W.*sqrt(max(0,single(self.diffraction(:,:,self.reconstruct_ind{ii})))),3);
[x,y]=math.center(avg_pattern);
x = round(x-x0); y = round(y-y0);
self.diffraction(:,:,self.reconstruct_ind{ii}) = imshift_fast(self.diffraction(:,:,self.reconstruct_ind{ii}),x,y);
self.mask(:,:,self.reconstruct_ind{ii}) = imshift_fast(self.mask(:,:,self.reconstruct_ind{ii}), x,y);
fprintf('Data in scan %i shifted by %i %i pixels\n', ii, x,y);
end
if ~isempty(self.probe_support_fft )
self.probe_support_fft = imshift_fast(self.probe_support_fft , x,y);
end
end
% apply custom flip of the diffraction data
if check_option(p, 'custom_data_flip') && any(p.custom_data_flip)
warning('Applying custom data flip: %i %i %i ', p.custom_data_flip(1), p.custom_data_flip(2), p.custom_data_flip(3))
if p.custom_data_flip(1)
self.diffraction = flipud(self.diffraction);
self.mask = flipud(self.mask);
end
if p.custom_data_flip(2)
self.diffraction = fliplr(self.diffraction);
self.mask = fliplr(self.mask);
end
if p.custom_data_flip(3)
self.diffraction = permute(self.diffraction, [2,1,3]);
self.mask = permute(self.mask, [2,1,3]);
end
end
%
if isfield(p, 'sample_rotation_angles') && any(p.sample_rotation_angles) && check_option(p, 'apply_tilted_plane_correction', 'diffraction')
%% OFFAXIS PTYCHOGRAPHY CORRECTION
if utils.verbose > -1
warning('Applying tilted plane correction: %3.5g %3.3g %3.3g\n Note that current implementation assumes low NA illumination, if this is not true, the central diffraction cone can be malformed', p.sample_rotation_angles(1), p.sample_rotation_angles(2), p.sample_rotation_angles(3))
end
% create matrix of deformation to apply effects similar to
deform_mat = get_tilted_plane_correction_matrix(max(self.Np_p), ...
p.z ,p.detectors{1}.pixel_size, ...
p.sample_rotation_angles(1),...
p.sample_rotation_angles(2),...
p.sample_rotation_angles(3));
if self.Np_p(1) ~= self.Np_p(2)
% quick fix for asymmetric probe dimensions
blank = true(self.Np_p);
blank_ind = find(utils.crop_pad(blank,[ max(self.Np_p), max(self.Np_p)]));
deform_mat = deform_mat(blank_ind,blank_ind);
end
apply_deform = @(x,D)single(reshape(full(D * double(reshape(x,[],size(x,3)))), size(x)));
plotting.smart_figure(3423)
ax(1)=subplot(1,2,1);
imagesc(log(1+max(self.diffraction, [],3))); axis off image xy ; colormap(plotting.franzmap)
title('Diffraction BEFORE tilted plane correction')
% apply deformation effects caused by tilted sample , be sure to enforce the mask before
% interpolation, the hotpixels can spread around after the correction
self.diffraction = apply_deform(self.diffraction .* ~self.mask, deform_mat');
self.mask = apply_deform(self.mask, deform_mat') > 0;
% plotting.imagesc3D(log(1+max(self.diffraction,[],3))); grid on
ax(2)=subplot(1,2,2);
imagesc(log(1+max(self.diffraction, [],3))); axis off image xy ; colormap(plotting.franzmap)
title('Diffraction AFTER tilted plane correction')
plotting.suptitle('Effect of tilted ptychography correction')
linkaxes(ax, 'xy')
drawnow
self.diffraction_deform_matrix = deform_mat;
end
warning on
self.diffraction = ifftshift_2D(self.diffraction);
self.mask = ifftshift_2D(self.mask);
end
self.filename = [p.detector.data_prefix, p.run_name];
% other basic parameters
self.path = '';
if check_option(p, 'prop_regime', 'nearfield')
self.z_distance = p.z;
else
self.z_distance = inf; % farfield
end
% multilayer extension
if isfield(p, 'delta_z')
assert(all(isfinite(p.delta_z)), 'Some of the provided layer distanced delta_z is not finite' )
self.z_distance = [p.delta_z(:)', self.z_distance];
end
self.lambda = p.lambda;
if check_option(p,'diff_pattern_blur')
self.diff_pattern_blur=p.diff_pattern_blur;
else
self.diff_pattern_blur = 0; % incoherent smoothing
end
self.modes = [];
% keep p structure for plotting purposes
param.p = p;
% if requested, shift the average probe to center and shift the object
% to correspond to the probe shift
if check_option(p, 'auto_center_probe')
[x,y] = center(mean(abs(self.probe{1}(:,:,:,1))));
for ii = 1:numel(self.probe)
self.probe{ii} = imshift_fft(self.probe{ii}, -x,-y);
end
for ii = 1:numel(self.object)
self.object{ii} = imshift_fft(self.object{ii}, -x, -y);
end
end
% check if all is ok (remove in future !!!)
positions = self.probe_positions_0(:,[2,1]);
positions = bsxfun(@plus, positions, ceil(self.Np_o/2-self.Np_p/2));
positions = round(positions);
range = ([min(positions(:,1)), max(positions(:,1))+ Np_p(1), min(positions(:,2)), max(positions(:,2))+ Np_p(2)]);
if range(1) < 0 || range(2) > self.Np_o(1) || range(3) < 0 || range(4) > self.Np_o(2)
warning('Object size is too small, not enough space for probes !! ')
end
end
@@ -0,0 +1,113 @@
% PREPARE_FLYSCAN_POSITIONS from finit number of measured position interpolate possitions for each
% measured frame when fly scan is used
%
% self = prepare_flyscan_positions(self, par)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = prepare_flyscan_positions(self, par)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
import plotting.*
jumps = diff(self.probe_positions_0);
step = median(jumps,1);
jumps = sum(abs(jumps),2);
% empirical condition
jumps = find(jumps > 10*median(jumps));
if length(jumps) < par.Nscans
% assume that smooth path is used
%% ADVANCED FLY SCAN - SPIRAL
for ii = 1:par.Nscans
assert(~any(isfinite(par.probe_position_search)), 'Position refinement and fly scans not suported')
ind = self.reconstruct_ind{ii};
pos = self.probe_positions_0(ind,:);
[ang, rad] = cart2pol(pos(:,1)-pos(1,1), pos(:,2)-pos(1,2));
ang = unwrap(ang);
% get interpolate d positions of the sub probes
ang_all = ang + (par.flyscan_offset -0.5+linspace(0,par.flyscan_dutycycle*(par.Nmodes-1)/par.Nmodes, par.Nmodes) ).*[diff(ang);0];
rad_all = interp1(ang, rad, ang_all, 'pchip');
[X,Y] = pol2cart(ang_all, rad_all);
for ll = 1:par.Nmodes
self.modes{ll}.probe_positions(ind,:) = pos(1,1:2) + [X(:,ll), Y(:,ll)];
if iter == 1; self.probe{ll} = self.probe{1}; end
end
end
else
%% ADVANCED FLY SCAN - LINE SCAN
% interpolate the other modes into new positions
pos = self.modes{1}.probe_positions;
for ll = 1:par.Nmodes
ratio = par.flyscan_dutycycle*(ll-1)/par.Nmodes;
self.modes{ll}.probe_positions = pos(min((1:self.Npos)+1,self.Npos),:)*ratio + (1-ratio)*pos;
if ~isempty(jumps)
% expected step continuation
self.modes{ll}.probe_positions(jumps,:) = bsxfun(@plus, self.modes{ll}.probe_positions(jumps-1,:),step);
end
end
end
end
@@ -0,0 +1,157 @@
% DISTMAT Compute a Distance Matrix for One or Two Sets of Points
%
%
%
% Copyright (c) 2015, Joseph Kirk
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
% * Redistributions of source code must retain the above copyright
% notice, this list of conditions and the following disclaimer.
% * Redistributions in binary form must reproduce the above copyright
% notice, this list of conditions and the following disclaimer in
% the documentation and/or other materials provided with the distribution
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
% Filename: distmat.m
%
% Description: Computes a matrix of pair-wise distances between points in
% A and B, using one of {euclidean,cityblock,chessboard} methods
%
% Author:
% Joseph Kirk
% jdkirk630@gmail.com
%
% Date: 02/27/15
%
% Release: 2.0
%
% Inputs:
% A - (required) MxD matrix where M is the number of points in D dimensions
% B - (optional) NxD matrix where N is the number of points in D dimensions
% if not provided, B is set to A by default
% METHOD - (optional) string specifying one of the following distance methods:
% 'euclidean' Euclidean distance (default)
% 'taxicab','manhattan','cityblock' Manhattan distance
% 'chebyshev','chessboard','chess' Chebyshev distance
% 'grid','diag' Diagonal grid distance
%
% Outputs:
% DMAT - MxN matrix of pair-wise distances between points in A and B
%
% Usage:
% dmat = distmat(a)
% -or-
% dmat = distmat(a,b)
% -or-
% dmat = distmat(a,method)
% -or-
% dmat = distmat(a,b,method)
%
% Example:
% % Pairwise Euclidean distances within a single set of 2D points
% xy = 10*rand(25,2); % 25 points in 2D
% dmat = distmat(xy);
% figure; plot(xy(:,1),xy(:,2),'.');
% for i=1:25, text(xy(i,1),xy(i,2),[' ' num2str(i)]); end
% figure; imagesc(dmat); colorbar
%
% Example:
% % Pairwise Manhattan distances within a single set of 2D points
% xy = 10*rand(25,2); % 25 points in 2D
% dmat = distmat(xy,'cityblock');
% figure; plot(xy(:,1),xy(:,2),'.');
% for i=1:25, text(xy(i,1),xy(i,2),[' ' num2str(i)]); end
% figure; imagesc(dmat); colorbar
%
% Example:
% % Pairwise Chebyshev distances within a single set of 2D points
% xy = 10*rand(25,2); % 25 points in 2D
% dmat = distmat(xy,'chebyshev');
% figure; plot(xy(:,1),xy(:,2),'.');
% for i=1:25, text(xy(i,1),xy(i,2),[' ' num2str(i)]); end
% figure; imagesc(dmat); colorbar
%
% Example:
% % Inter-point Euclidean distances for 2D points
% xy = 10*rand(15,2); % 15 points in 2D
% uv = 10*rand(25,2); % 25 points in 2D
% dmat = distmat(xy,uv);
% figure; plot(xy(:,1),xy(:,2),'.');
% for i=1:15, text(xy(i,1),xy(i,2),[' ' num2str(i)]); end
% figure; plot(uv(:,1),uv(:,2),'.');
% for i=1:25, text(uv(i,1),uv(i,2),[' ' num2str(i)]); end
% figure; imagesc(dmat); colorbar
%
% See also:
%
function dmat = distmat(a,varargin)
% Set defaults
method = 'euclidean';
b = a;
% Error check primary input
if ~isnumeric(a)
error('Expecting a matrix of floating point values for A input.');
end
% Process optional inputs
for var = varargin
arg = var{1};
if ischar(arg)
method = arg;
elseif ~isempty(arg)
b = arg;
end
end
% Check input dimensionality
[na,aDims] = size(a);
[nb,bDims] = size(b);
if (aDims ~= bDims)
error('Input matrices must have the same dimensionality.');
end
% Create index matrices
[j,i] = meshgrid(1:nb,1:na);
% Compute array of inter-point differences
delta = a(i,:) - b(j,:);
% Compute distance by specified method
dmat = zeros(na,nb);
switch lower(method)
case {'euclidean','euclid'}
% Euclidean distance
dmat(:) = sqrt(sum(delta.^2,2));
case {'cityblock','city','block','manhattan','taxicab','taxi'}
% Cityblock distance
dmat(:) = sum(abs(delta),2);
case {'chebyshev','cheby','chessboard','chess'}
% Chebyshev distance
dmat(:) = max(abs(delta),[],2);
case {'grid','diag'}
dmat(:) = max(abs(delta),[],2) + (sqrt(2) - 1)*min(abs(delta),[],2);
otherwise
error('Unrecognized distance method %s',method);
end
end
@@ -0,0 +1,300 @@
% GET_CLOSE_INDICES simple based method to select indices for DM
% !! GPU needs the sets to be with similar , ideally the same sizes !!!
%
% [indices_out, scan_ids_out] = get_close_indices(self, cache, par )
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ indices_out cell of arrays, contain indices of positions processed in parallel
% ++ scan_ids_out cell of arrays, contain scan numbers for each position
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [indices_out, scan_ids_out] = get_close_indices(self, cache, par )
import math.*
import utils.*
grouping = par.grouping;
% in case of a shared scan join together all positions to find the optimal groups
group_across_scans = true;
if par.share_object && group_across_scans
Nsets = 1;
else
Nsets = par.Nscans;
end
cluster_refinement_time = 0;
cluster_time = 0;
% in simplest case process all positions together
if Nsets == 1 && grouping >= self.Npos
indices_out = {[self.reconstruct_ind{:}]};
scan_ids_out{1} = [];
for ii = 1:length(self.reconstruct_ind)
scan_ids_out{1} = [scan_ids_out{1}; ii*ones(length(self.reconstruct_ind{ii}),1)];
end
return
end
%rng default
for kk = 1:Nsets
% take them sequentially but with random offset
if par.share_object && group_across_scans
% join all indices into one large set if the object is shared
indices_0 = [self.reconstruct_ind{:}];
for ii = 1:length(self.reconstruct_ind)
scans_0(self.reconstruct_ind{ii}) = ii;
end
else
indices_0 = self.reconstruct_ind{kk};
scans_0 = kk * ones(size(indices_0)); % scan number
end
N = length(indices_0);
Ngroups=ceil(N/grouping);
positions = self.probe_positions_0(indices_0,:);
Npos = length(positions);
% get initial set distribution
[groups, C, sum_D, D] = get_best_kmeans(positions, Ngroups);
iter = 0;
t0 = tic;
while true
iter= iter +1;
[nbins,bins] = hist(groups, unique(groups));
% if less than 2 types of groups are present, finish
Ngroups_sizes = length(unique(nbins));
% try to find distribution with most similar sets sizes, if not
% easy, end with suboptimal distribution after 50 iterations
if ( Ngroups_sizes <= max(2, ceil(iter/1e3)) && (Ngroups*grouping ~= N || iter > 1e3 )) ...
|| Ngroups_sizes == 1 % choose suboptimal solution if better is not found soon
break
end
% find group with lowest number of members , add new points into
% this group
min_group = bins(argmin(nbins));
large_groups = bins(nbins>grouping);
if isempty(large_groups) || any(ismember(min_group, large_groups)) ; break; end
% choose closest position from the largest group to be moved to the
% smallest group
ind_large = (D(:,min_group) == min(D(ismember(groups, large_groups), min_group)));
groups(ind_large) = min_group;
end
% remove empty groups
ugroups = unique(groups);
Ngroups = length(ugroups);
groups = sum((1:Ngroups) .*(groups == ugroups'),2);
cluster_time = cluster_time + toc(t0);
for ii = 1:Ngroups
C(ii,:) = median(positions(groups == ii,:));
end
for ii = 1:Ngroups
D(:,ii) = (sum((positions - C(ii,:)).^2,2));
end
t0 = tic;
%% find more compact refinement
% find the most distanced points
[~,sind] = sort(D,2);
% positions to be improved -> find the best matching group
optimal_group = sind(:,1);
nonoptimal_ratio_0 = 1;
for iter = 1:10
ind_switch = (groups ~= optimal_group);
nonoptimal_ratio = sum(ind_switch) / numel(ind_switch);
if nonoptimal_ratio > 0
verbose(0, 'Indexes to be switched: %3.2g%% positions', nonoptimal_ratio * 100)
end
if nonoptimal_ratio >= nonoptimal_ratio_0
break
end
nonoptimal_ratio_0 = nonoptimal_ratio;
max_dist_0 = inf;
for i = 1:sum(ind_switch)
% calculate distance for each point from its group center
center_dist = (D(sub2ind(size(D), (1:Npos)', groups)));
max_dist_0 = max(center_dist(ind_switch));
% start from the worst case
ind_worse = find(max(center_dist(ind_switch)) == center_dist, 1, 'first');
% initial group
group_old = groups(ind_worse);
% better fitting group
group_new = optimal_group(ind_worse);
% position to be switched in the new group
ind_new = find(D(:,group_old) == min(D(groups == group_new, group_old)), 1, 'first');
% switch the group members
groups(ind_worse) = group_new;
groups(ind_new) = group_old;
ind_switch([ind_worse, ind_new]) = 0;
if all(ind_switch == 0)
break
end
end
% ind_switch = (groups ~= sind(:,1));
% for ii = Ngroups
% clf
% hold all;
% ind = groups == ii;
% ax = plot(self.probe_positions_0(ind & ind_switch, 1), self.probe_positions_0(ind & ind_switch, 2), 'o');
% ax2 = plot(self.probe_positions_0(ind & ~ind_switch, 1), self.probe_positions_0(ind & ~ind_switch, 2), 'x');
% try; ax2.Color = ax.Color; end
% plot(C(ii,1),C(ii,2),'x','Linewidth', 2)
% % drawnow
% % pause(1)
% end
% title(num2str(iter))
% axis tight equal
% pause(1)
%
end
cluster_refinement_time = cluster_refinement_time + toc(t0);
%% optimally sort the indices to help GPU
[nbins,bins] = hist(groups, unique(groups));
[~,ind] = sort(nbins,2,'descend');
for ii = 1:length(bins)
indices{kk}{ii} = indices_0((groups == bins(ind(ii))));
scan_ids{kk}{ii} = scans_0((groups == bins(ind(ii))));
end
verbose(2,'=== Number of cluster sizes %i', length(unique(nbins)))
end
verbose(0,'=== Position clusters found in %i iterations in %3.2gs', iter, cluster_time)
verbose(0,'=== Position clusters refined in %i iterations in %3.2gs', iter, cluster_refinement_time)
%rng shuffle
if verbose() > 1 && Ngroups_sizes > 1
warning('Unequal group sizes, it may cause slower calculation')
end
indices_out = horzcat(indices{:});
scan_ids_out = horzcat(scan_ids{:});
if Ngroups == 1 && Nsets == 1
%% merge groups from multiple scans into larger chunks if grouping is too large
indices_out = {horzcat(indices_out{:})};
scan_ids_out = {horzcat(scan_ids_out{:})};
end
%% sort them to minimize allocation of new projection matrices
Nitems = cellfun(@length, indices_out);
if all(max(Nitems) - min(Nitems) <= 1) && all(Nitems > 100)
% just neglect one scanning position to keep the bunches with the same
% size -> faster run on GPU
for i = 1:length(indices_out)
indices_out{i} = indices_out{i}(1:min(Nitems));
scan_ids_out{i} = scan_ids_out{i}(1:min(Nitems));
end
else
[~,ind] = sort(Nitems(:),1,'descend' );
indices_out = indices_out(ind);
scan_ids_out = scan_ids_out(ind);
end
end
function [groups, C, sum_D, D] = get_best_kmeans(positions, Ngroups)
% make several guesses to get better Kmean distribution
warning('off','stats:kmeans:FailedToConverge')
for i = 1:10
[groups{i}, C{i}, sum_D{i}, D{i}] = kmeans(positions, Ngroups);
nbins = hist(groups{i}, unique(groups{i}));
score(i) = std(nbins);
end
best = math.argmin(score);
groups = groups{best};
C = C{best};
sum_D = sum_D{best};
D = D{best};
end
@@ -0,0 +1,203 @@
% GET_NONOVERLAPPING_INDICES a heuristic based method to select pseudorandom indices of non overlapping regions
% Note: It can be slow for large number of scanning positions
%
% [indices_out, scan_ids_out] = get_nonoverlapping_indices(self, cache, par )
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ indices_out cell of arrays, contain indices of positions processed in parallel
% ++ scan_ids_out cell of arrays, contain scan numbers for each position
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [indices_out, scan_ids_out] = get_nonoverlapping_indices(self, cache, par )
% find groups accross the scans in order to further minimize overlap
group_across_scans = true; %need to be true for sharing object amongs scans
if group_across_scans
% divide the grouping equally over all the scans
grouping = ceil(par.grouping/par.Nscans);
else
grouping = par.grouping;
end
max_groups = 0;
for kk = 1:par.Nscans
% setdiff sort the indices by size
indices_0 = self.reconstruct_ind{kk}; % remove unwanted from the decision process
ind_start(kk) = min(indices_0)-1;
indices_0 = indices_0 - ind_start(kk); %
Npos_tmp=length(indices_0);
% randomly permutate the indices
indices_0 = indices_0(randperm(Npos_tmp));
max_groups = max(max_groups, ceil(Npos_tmp/grouping));
% fill it with some initial random guess
for ii = 1:ceil(Npos_tmp/grouping)
indices{kk}{ii} = indices_0(1+(ii-1)*grouping : min(Npos_tmp,ii*grouping));
end
% no need for this method ot it calculation would be too long -> use
% just the random initial guess
if (self.Npos/par.Nscans > 1e3 ) || (grouping == 1) || ~isfield(cache, 'distances_matrix')
%%%for ii = 1:length(indices{1}) %why length(indices{1})? Bug?
for ii = 1:length(indices{kk}) %modified by YJ to prevent error when different scans have differernt number of positions
scan_ids{kk}{ii} = ones(1,length(indices{kk}{ii}))*kk; % note their scan origin
end
continue
end % hope that for large number of positions the random statistics will be enough
try
update_score = 0;
for i = 1:ceil(Npos_tmp/grouping)-1
id = indices{kk}{i};
dist_mat_small = cache.distances_matrix{kk}(id,id);
for ii = 0:2*length(indices{kk}{i+1}) % go twice through all positions
j = 1+mod(ii, length(indices{kk}{i+1}));
min_dist = 1./sum(1./dist_mat_small.^2); % find the shortest distance between the probes
if all(isinf(min_dist)) % all(isnan(min_dist))
break
end
[~,min_dist_ind] = min(min_dist);
% make a swap with the j position in i+1 index array
tmp = indices{kk}{i+1}(j);
indices{kk}{i+1}(j) = indices{kk}{i}(min_dist_ind);
indices{kk}{i}(min_dist_ind) = tmp;
% update distance matrix
dist_mat_small_update = cache.distances_matrix{kk}(tmp,indices{kk}{i});
dist_mat_small(min_dist_ind,:) = dist_mat_small_update';
dist_mat_small(:,min_dist_ind) = dist_mat_small_update;
end
update_score = update_score +j;
end
catch
keyboard
end
% fill the last group by the skip indieces but do not expand it
skip_ind = cache.skip_ind(randperm(length(cache.skip_ind)));
indices{kk}{end} = [indices{kk}{end}, skip_ind(1:min(end, grouping-length(indices{kk}{end})))]; % join skip_ind back to the last (smallest) set
for ii = 1:length(indices{kk})
scan_ids{kk}{ii} = ones(1,length(indices{kk}{ii}))*kk; % note their scan origin
end
end
if group_across_scans
indices_out = cell(max_groups,1);
scan_ids_out = cell(max_groups,1);
%% merge groups from difference scans into larger chunks if required
for ii = 1:max_groups
indices_out{ii} = [];
scan_ids_out{ii} = [];
% from each scan add one group
for kk = 1:par.Nscans
if ii <= length(indices{kk})
indices_out{ii} = [indices_out{ii}, indices{kk}{ii}+ind_start(kk)];
scan_ids_out{ii} = [scan_ids_out{ii}, scan_ids{kk}{ii}];
end
end
if length(scan_ids_out) > 1 && length(scan_ids_out{end}) < grouping / 10
% if the a group is too small, merge it with the previous to
% reduce the overhead
indices_out{end-1} = [indices_out{end-1}, indices_out{end}];
scan_ids_out{end-1} = [scan_ids_out{end-1}, scan_ids_out{end}];
scan_ids_out(end) = []; indices_out(end) = [];
end
end
else
indices_out = {};
for ii = 1:par.Nscans
for kk = 1:length(indices{ii})
indices_out = [indices_out, indices{ii}{kk}+ind_start(ii)];
end
end
scan_ids_out = [scan_ids{:}]';
end
%% sort them to minimize allocation of new projection matrices
Nitems = cellfun(@length, indices_out);
if all(max(Nitems) - min(Nitems) <= 1) && all(Nitems > 100)
% just neglect one scanning position to keep the bunches with the same
% size -> faster run on GPU
for i = 1:length(indices_out)
indices_out{i} = indices_out{i}(1:min(Nitems));
scan_ids_out{i} = scan_ids_out{i}(1:min(Nitems));
end
else
[~,ind] = sort(Nitems(:),1,'descend' );
indices_out = indices_out(ind);
scan_ids_out = scan_ids_out(ind);
end
end
@@ -0,0 +1,147 @@
% GET_SCANNING_INDICES simple based method to select indices for DM
%
% [indices_out, scan_ids_out] = get_scanning_indices(self, cache, par )
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ indices_out cell of arrays, contain indices of positions processed in parallel
% ++ scan_ids_out cell of arrays, contain scan numbers for each position
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [indices_out, scan_ids_out] = get_scanning_indices(self, cache, par )
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
grouping = par.grouping;
max_groups = 0;
if self.Npos == grouping && par.Nscans == 1
indices_out = self.reconstruct_ind;
scan_ids_out = {ones(self.Npos,1)};
return
end
for kk = 1:par.Nscans
N = length(self.reconstruct_ind{kk});
% !! indices ordering has to be always the same for DM !!!
indices_0 = self.reconstruct_ind{kk};
max_groups = max(max_groups, ceil(N/grouping));
for ii = 1:ceil(N/grouping)
indices{kk}{ii} = indices_0(1+(ii-1)*grouping : min(end,ii*grouping));
end
% fill the last group by the skip indices but do not expand it
skip_ind = cache.skip_ind(randperm(length(cache.skip_ind)));
indices{kk}{end} = [indices{kk}{end}, skip_ind(1:min(end, grouping-length(indices{kk}{end})))]; % join skip_ind back to the last (smallest) set
for ii = 1:length(indices{kk})
scan_ids{kk}{ii} = kk * ones(1,length(indices{kk}{ii})); % note their scan origin
end
end
% how many scans should be merged to reach the desired grouping
Njoin = ceil(par.grouping / (self.Npos/par.Nscans));
if Njoin > 1 && par.Nscans > 1 && is_method(par, {'PIE', 'ML'})
% join several scan to improve performance
indices_out = cell(ceil(par.Nscans/Njoin),1);
scan_ids_out = cell(ceil(par.Nscans/Njoin),1);
%% merge groups from difference scans into larger chunks
for kk = 1:ceil(par.Nscans/Njoin)
indices_out{kk} = [];
scan_ids_out{kk} = [];
for ii = 1:Njoin
if kk+(ii-1)*ceil(par.Nscans/Njoin) <= par.Nscans
indices_out{kk} = [indices_out{kk}, indices{kk+(ii-1)*ceil(par.Nscans/Njoin)}{1}];
scan_ids_out{kk} = [scan_ids_out{kk}, scan_ids{kk+(ii-1)*ceil(par.Nscans/Njoin)}{1}];
end
end
end
else
indices_out = horzcat(indices{:});
scan_ids_out = horzcat(scan_ids{:});
end
%% sort them to minimize allocation of new projection matrices
Nitems = cellfun(@length, indices_out);
if all(max(Nitems) - min(Nitems) <= 1) && all(Nitems > 100)
% just neglect one scanning position to keep the bunches with the same
% size -> faster run on GPU
for i = 1:length(indices_out)
indices_out{i} = indices_out{i}(1:min(Nitems));
scan_ids_out{i} = scan_ids_out{i}(1:min(Nitems));
end
else
[~,ind] = sort(Nitems(:),1,'descend' );
indices_out = indices_out(ind);
scan_ids_out = scan_ids_out(ind);
end
end
@@ -0,0 +1,167 @@
% RESCALE_INPUTS multigrid scheme method
%
% self = rescale_inputs(self, Np_p_new, rescale_data)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** Np_p_new new size of the rescaled dataset
% ** rescale_data true -> rescale also data + mask + noise arrays
%
% returns:
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = rescale_inputs(self, Np_p_new, rescale_data)
import utils.*
import math.*
if isempty(Np_p_new)
return
end
scale = Np_p_new ./ self.Np_p;
if all(scale == 1)
return
end
self.modes = [];
if ~isempty(self.diffraction_deform_matrix)
verbose(1,'Rescalling with diffraction_deform_matrix is not implemented')
self.diffraction_deform_matrix = [];
end
self.Np_p = ceil(self.Np_p.*scale);
self.Np_o = ceil(self.Np_o.*scale);
if ~isempty(self.probe_positions) && any(self.probe_positions(:) ~= self.probe_positions_0(:))
self.probe_positions = scale([2,1]) .* self.probe_positions;
else
self.probe_positions = [];
end
self.probe_positions_0 = scale([2,1]) .* self.probe_positions_0;
self.pixel_size = self.pixel_size ./ scale;
for i = 1:length(self.probe)
% scale also intensity
self.probe{i} = interpolateFT_addnoise(self.probe{i}, self.Np_p, 1 )./prod(scale);
end
for i = 1:numel(self.object)
self.object{i} = interpolateFT_addnoise(self.object{i}, self.Np_o , 1);
end
if ~isempty(self.probe_support)
self.probe_support = interpolateFT( self.probe_support, self.Np_p) ;
end
if ~isempty(self.probe_support_fft)
self.probe_support_fft = crop_pad( self.probe_support_fft, self.Np_p) ;
end
if rescale_data
self.diffraction = fftshift_2D(self.diffraction);
self.diffraction = crop_pad(self.diffraction, self.Np_p);
self.diffraction = ifftshift_2D(self.diffraction);
% force mask empty by Zhen Chen
self.mask=[];
if ~isempty(self.mask)
fill_value = 0.9; % fill value in case of ptychographic "super resolution"
self.mask = fftshift_2D(self.mask);
self.mask = crop_pad(self.mask, self.Np_p, fill_value);
self.mask = ifftshift_2D(self.mask);
end
if ~isempty(self.noise)
self.noise = fftshift_2D(self.noise);
self.noise = crop_pad(self.noise, self.Np_p);
self.noise = ifftshift_2D(self.noise);
end
end
end
function [ imout ] = interpolateFT_addnoise(im,outsize, noise_amplitude)
% fill the empty regions in the FFT interpolated data by some weak
% random noise
import math.fftshift_2D
import math.ifftshift_2D
import utils.crop_pad
Nout = outsize;
Nin = size(im);
imFT = fftshift_2D(fft2(im));
imout = crop_pad(imFT, outsize);
% add noise to avoid correlation between upsampled imaged from
% interpolation artefacts
imout = imout + noise_amplitude * randn(outsize).*mean(min(abs(imFT)));
imout = ifft2(ifftshift_2D(imout))*(Nout(1)*Nout(2)/(Nin(1)*Nin(2)));
end
@@ -0,0 +1,177 @@
% SAVE_TO_P save parameters and recosntrutions from param and self structures to the p-structure
%
% p_out = save_to_p(self, param, p, fourier_error)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** param structure containing parameters for the engines
% ** p ptychoshelves p structure
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
%
% returns:
% ** p_out updated ptychoshelves p structure
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 p_out = save_to_p(self, param, p, fourier_error)
import utils.*
Np_p = self.Np_p;
p_out = p;
%% return calculated values back to the main code (p-structure)
p_out.probe_modes = length(self.probe);
p_out.numprobs = size(self.probe{1},3);
for ll = 1:p_out.probe_modes
if size(self.probe{ll},3) == self.Npos
% classical OPRP method
for ii = 1:p.numscans
probes(:,:,ii,ll) = mean(self.probe{ll}(:,:,self.reconstruct_ind{ii}),3);
end
% save the variable modes
[X,V] = extract_PCA(self.probe{ll}, param.variable_probe_modes);
p_out.probe_PCA.eigen_vec = X * (prod(sqrt(Np_p))*2*p.renorm);% normalization for consistency with the CPU code
p_out.probe_PCA.evolution = V;
else
% store constant part
probes(:,:,1:size(self.probe{ll},3),ll) = self.probe{ll}(:,:,:,1);
if ndims(self.probe{ll}) == 4
p_ind = zeros(self.Npos,1);
for kk = 1:length(self.reconstruct_ind)
p_ind = p_ind + kk*ismember(1:self.Npos, self.reconstruct_ind{kk})';
end
p_out.probe_variable.eigen_vec = self.probe{ll}(:,:,:,2) * (prod(sqrt(Np_p))*2*p.renorm);% normalization for consistency with the CPU code
p_out.probe_variable.evolution = self.probe_evolution;
end
end
end
% normalization for consistency with the CPU code
probes = probes * (prod(sqrt(Np_p))*2*p.renorm);
p_out.probes = probes;
position_offset = 1+floor((p.object_size-self.Np_p)/2);
% for consistency with the CPU code revert the object to the original size
p_out.numobjs = size(self.object,1);
p_out.object = cell(1,p_out.numobjs);
for i = 1:p_out.numobjs
obj_size = p.object_size(min(end,p.share_object_ID(i)),:);
p_out.object{i} = single([]);
for layer = 1:param.Nlayers
p_out.object{i}(:,:,1,layer) = imshift_fast(self.object{i,layer}, -1,-1, obj_size, 'nearest', mean(self.object{i,layer}(:)));
end
end
for i = 1:p.numscans
id = p.share_object_ID(i);
obj_size = p.object_size(min(end,id),:);
p_out.illum_sum{id} = imshift_fast(self.illum_sum{id}, -1,-1, obj_size, 'nearest');
end
if param.probe_position_search < param.number_iterations
% store the refined positions
p_out.positions = self.probe_positions(:,[2,1]);
% return to the original coordinates
p_out.positions_0 = self.probe_positions_0(:,[2,1]);
for i = 1:length(self.reconstruct_ind)
ind = self.reconstruct_ind{i};
p_out.positions(ind,:) = p_out.positions(ind,:) + position_offset(p.share_object_ID(i),:);
p_out.positions_0(ind,:) = p_out.positions_0(ind,:) + position_offset(p.share_object_ID(i),:);
end
else
for i = 1:length(self.reconstruct_ind)
ind = self.reconstruct_ind{i};
if ~isempty(self.probe_positions)
p_out.positions(ind,:) = self.probe_positions(ind,[2,1]) + position_offset(p.share_object_ID(i),:);
else
p_out.positions(ind,:) = self.probe_positions_0(ind,[2,1]) + position_offset(p.share_object_ID(i),:);
end
end
end
if param.probe_position_search < param.number_iterations || param.detector_rotation_search < param.number_iterations || param.detector_scale_search < param.number_iterations
p_out = engines.GPU_MS.analysis.report_refined_geometry(self, param, p_out);
end
% save additional reconstructed parameters
for item = {'background', 'intensity_corr', 'probe_fourier_shift' }
try
p_out.(item{1}) = self.(item{1});
end
end
% save error metrics
ind_ok = any(~isnan(fourier_error),2); % plot only the reported values
p_out.error_metric.value = nanmean(fourier_error(ind_ok,:),2);
p_out.error_metric.iteration = find(ind_ok);
if strcmp(param.likelihood,'poisson' )
p_out.error_metric.err_metric = 'poisson';
else
p_out.error_metric.err_metric = 'L1';
end
p_out.error_metric.method = ['GPU-',param.method, ' metric:' , param.likelihood ];
end
@@ -0,0 +1,136 @@
% self = align_objects(self )
% align all provided object arrays and apply the same shift on the
% corresponding probe so that the scans can be used for shared object
% reconstructions
%
% Inputs:
% self main data structure
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = align_objects(self)
import engines.GPU_MS.*
import utils.*
import engines.GPU_MS.GPU_wrapper.*
cache.skip_ind = [];
[cache.oROI_s{1},cache.oROI{1}] = shared.find_reconstruction_ROI( self.probe_positions_0,self.Np_o, self.Np_p);
cache.object_ROI = {ceil(self.Np_p(1)/2):self.Np_o(1)-ceil(self.Np_p(1)/2), ...
ceil(self.Np_p(2)/2):self.Np_o(2)-ceil(self.Np_p(2)/2)};
Nobj = size(self.object,1);
shift = [0,0];
obj1 = prod(cat(3,self.object{1,:}),3);
obj1 = Ggather(obj1(cache.object_ROI{:}));
for ll = 1:Nobj
obj2 = prod(cat(3,self.object{ll,:}),3);
obj2 = Ggather(obj2(cache.object_ROI{:}));
[score(ll), object_aligned{ll}] = analysis.fourier_ring_correlation(obj1,obj2,...
'smoothing', 5, 'crop', self.Np_p/4, 'plot_results', false);
shift(ll,:) = score(ll).shift;
end
if any([score(:).AUC] < 0.3)
warning('Alignment of scans %i vs scan %i propably failed, FRC resolution is %3.3g of Nyquist frequency limit', 1, ll, score.resolution)
figure(56476)
subplot(1,Nobj,1)
plotting.imagesc3D(angle(object_aligned{1}{1})); axis off image
grid on
for ll = 1:Nobj
subplot(1,Nobj,ll)
plotting.imagesc3D(angle(object_aligned{ll}{2})); axis off image xy
grid on
end
plotting.suptitle('Objects after alignement, check visually the estimated alignement')
colormap bone
disp('Estimated shifts between the aligned objects')
disp(shift )
end
shift = shift - mean(shift,1);
self.Np_o = self.Np_o + 2*ceil(max(abs(shift(:,[2,1])),[],1));
for layer = 1:size(self.object,2)
for ll = 1:Nobj
% make them all the same size
self.object{ll,layer} = crop_pad(self.object{ll,layer}, self.Np_o, mean(self.object{ll,layer}(:)));
self.object{ll,layer} = utils.imshift_fft(self.object{ll,layer}, shift(ll,:));
end
end
if utils.verbose > -1 && any(max(abs(shift) ./ self.Np_p([2,1])) > 0.25)
warning off backtrace
id = math.argmax(max(abs(shift),[],2));
warning('Alignement of two mirrored scans resulted in maximal probe shift of %3.3g %3.3gpx, \n!! this is more than 50%% of the probe diameter !! \nconsider using better alignement between 0deg and 180deg scans ', -shift(id,1), shift(id,2))
warning on backtrace
end
for ii = 1:length(self.probe)
for ll = 1:Nobj
% apply shift to the probe as well, if needed, replicate the
% probe
probe{ii}(:,:,ll,:) = utils.imshift_fft(self.probe{ii}(:,:,min(end,ll),:), shift(ll,:));
end
end
self.probe = probe ;
end
@@ -0,0 +1,66 @@
% APPLY_PROBE_CONTRAINTS apply support constrains on the probe in the real space, fourier space or any other
% plane if provided, it useds factor in mode.support_back_propagation_factor to perform ASM propagation
%
% probe = apply_probe_contraints(probe, mode)
%
% ** probe complex array with probe / probes
% ** mode structure containing parameters for selected probe mode
%
% returns:
% ** probe complex array with probe / probes
%
%
function probe = apply_probe_contraints(probe, mode)
import math.*
import utils.*
import engines.GPU_MS.shared.*
if ~isempty(mode.probe_support)
% apply support contraint in real space (ir nearfield propagated )
if ~isempty(mode.support_fwd_propagation_factor)
if isscalar(mode.support_fwd_propagation_factor) && isinf(mode.support_fwd_propagation_factor)
probe = fftshift_2D(fft2(fftshift_2D(probe))); % propagate to infinity
else
probe = ifft2(fft2(probe) .* mode.support_propagation_factor);
end
end
%% apply real-space support
probe = probe .* mode.probe_support;
if ~isempty(mode.support_back_propagation_factor)
if isscalar(mode.support_back_propagation_factor) && isinf(mode.support_back_propagation_factor)
probe = fftshift_2D(ifft2(fftshift_2D(probe))); % propagate to infinity
else
probe = ifft2(fft2(probe).* mode.support_back_propagation_factor);
end
end
end
Np_p = size(probe);
if mode.probe_scale_upd(end) > 0 && ~isempty(mode.probe_scale_window)
% apply windowing to avoid boundary issues when subpixel probe
% rescaling is used
probe = probe .* mode.probe_scale_window ;
end
if ~isempty(mode.probe_support_fft) || mode.probe_scale_upd(end) ~= 0
%% apply contraint in the detector plane
% propagate probe on the detector
probe = fwd_fourier_proj(probe, mode);
if ~isempty(mode.probe_support_fft)
probe = probe .* mode.probe_support_fft;
end
if mode.probe_scale_upd(end) < 0 && ~isempty(mode.probe_scale_window)
probe = probe .* mode.probe_scale_window ;
end
% propagate probe back to the sample plane
probe = back_fourier_proj(probe, mode);
end
end
@@ -0,0 +1,111 @@
% APPLY_SUBPX_SHIFT subpixel probe shifting , equivalent just a bit faster than imshift_fft
%
% img = apply_subpx_shift(img, shift)
% apply_subpx_shift() will reset persitent variables that are created on GPU to avoid overhead with CPU / GPU transfer
%
%
% ** img stack images
% ** shift (N,2) array of shifts
% returns
% ++ img shifted image stack
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (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 mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (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, 2908929108 (2016).
% (doi: 10.1364/OE.24.029089).
% 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 img = apply_subpx_shift(img, shift)
% subpixel probe shifting
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
persistent xgrid ygrid
if nargin == 0
xgrid = [];
return
end
if all(shift(:) == 0); return ; end
global use_gpu
shift = single(shift);
x = reshape(shift(:,1),1,1,[]);
y = reshape(shift(:,2),1,1,[]);
Np = size(img);
if size(img,3) ~= size(shift,1) && use_gpu
% ugly trick making matlab GPU FFT faster
img = repmat(img,1,1,size(shift,1));
end
img = fft2_safe(img);
if isempty(xgrid)
% matlab seems to need quite a lot of time to generate it -> preallocate
xgrid = Garray(fftshift((0:Np(2)-1)'/Np(2))-0.5)';
ygrid = Garray(fftshift((0:Np(1)-1)'/Np(1))-0.5);
end
if use_gpu
img = Gfun(@apply_shift_Gfun,img,x,y,xgrid, ygrid);
else
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times, x,xgrid)));
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,y,ygrid)));
end
img = ifft2_safe(img);
end
function img = apply_shift_Gfun(img,x,y,xgrid, ygrid)
img = img .* exp((-2i*pi)*(x*xgrid+y*ygrid));
end
@@ -0,0 +1,98 @@
% APPLY_SUBPX_SHIFT_FFT fast shifting in fourier space (ie apply phase ramp )
%
% img = apply_subpx_shift_fft(img, shift)
%
% ** img stack images
% ** shift (N,2) array of shifts
% returns
% ++ img shifted image stack
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (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 mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (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, 2908929108 (2016).
% (doi: 10.1364/OE.24.029089).
% 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 img = apply_subpx_shift_fft(img, shift)
import engines.GPU_MS.GPU_wrapper.*
if all(shift(:) == 0); return ; end
global use_gpu
persistent grid
shift = Garray(shift);
x = reshape(shift(:,1),1,1,[]);
y = reshape(shift(:,2),1,1,[]);
Np = size(img);
if Np(1) ~= Np(2); error('Not implemented'); end
grid = Garray((0:Np(1)-1)'/Np(1)-0.5);
if isempty(grid)
grid = Garray((0:Np(1)-1)'/Np(1)-0.5);
end
if use_gpu
img = Gfun(@apply_shift_Gfun,img,x,y,grid', grid);
else
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,x,grid')));
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,y,grid)));
end
end
function img = apply_shift_Gfun(img,x,y,xgrid, ygrid)
img = img .* exp((-2i*pi)*(x*xgrid+y*ygrid));
end
@@ -0,0 +1,111 @@
% BACK_FOURIER_PROJ back propagate to the sample plane
%
% Psi = back_fourier_proj(Psi, mode)
%
% ** Psi [Nx,Ny,N] array, propagated complex valued exit-wave Psi = F(P*O)
% ** mode structure containing parameters for selected probe mode
%
% returns
% ++ psi [Nx,Ny,N] array, back-propagated complex valued exit-wave
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [Psi, fmask] = back_fourier_proj(Psi, mode, ind)
import engines.GPU_MS.shared.*
fmask = [];
try
if mode.distances(end) == 0
% no propagation
elseif mode.distances(end) == inf
Psi = ifft2_safe(Psi); % fully farfield
% Psi = fftshift_2D(ifft2_safe(fftshift_2D(Psi))); % fully farfield
elseif mode.distances(end) == -inf
Psi = fft2_safe(Psi); % fully farfield forward
elseif ~isempty(mode.cFAR_factor) % almost far field
Psi = fftshift_2D(ifft2_safe(fftshift_2D(Psi)));
Psi = bsxfun(@times, Psi, mode.cFAR_factor);
elseif ~isempty(mode.cASM_factor) % near field
Psi = fft2_safe(Psi);
Psi = Psi .* mode.cASM_factor;
Psi = ifft2_safe( Psi );
else
error('Not implemented')
end
if mode.probe_scale_upd(end) ~= 0
% apply scaling to the probe , used for camera distance refinement
Psi = utils.imrescale_frft(Psi,1/(1+mode.probe_scale_upd(end)));
end
if nargin > 2 && any(mode.probe_rotation_all)
% apply rotation to the probe , used for camera angle refinement
Psi = utils.imrotate_ax_fft(Psi,mode.probe_rotation_all(ind),3);
end
if ~isempty(mode.tilted_plane_propagate_fwd)
% propagate to tilted plane of the sample (if provided)
Psi = mode.tilted_plane_propagate_fwd(Psi);
end
catch err
if utils.verbose() > 0
warning(err.message)
keyboard
else
rethrow(err)
end
end
end
@@ -0,0 +1,104 @@
% CLEAN_RESIDUA, try to use 2D FFT unwrapping to remove phase residua
%
% self = clean_residua(self)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% returns:
% ++ self self-like structure with final reconstruction
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = clean_residua(self, cache)
ker = gausswin(7) .* gausswin(7)';
ker = ker / math.sum2(ker);
for ii = 1:numel(self.object)
object = self.object{ii};
Nresid(ii) = sum(sum(abs(utils.findresidues(object(cache.object_ROI{:}))) > 0.5));
if Nresid(ii) == 0
continue
end
utils.verbose(1,'Detected %i residua, trying to remove', Nresid(ii))
amp = abs(object);
phase = 0;
W = 2*(1-1./(1+cache.illum_sum_0{1}/max(max(cache.illum_sum_0{1}))));
mean_amp = math.sum2(amp .* W) / math.sum2(W);
W = W .* amp / mean_amp;
for jj = 1:2
phase = phase + math.unwrap2D_fft2(object,[],0,W,0);
object = exp(-1i*phase) .* object;
end
phase = convn( phase, ker, 'same');
%plotting.smart_figure(54655);
%imagesc(angle(exp(-1i*phase) .* self.object{ii}))
self.object{ii} = exp(1i*phase)*mean_amp; % remove amplitude information, it is easy to recover
end
if sum(Nresid)
utils.verbose(1,'Removing residua will result in low\n frequency errors in phase and amplitude, \n !! run DM again with current results as initial guess !!')
end
% imagesc(utils.findresidues( self.object{ii} ))
end
@@ -0,0 +1,75 @@
% EXTRACT_PCA Extract N principal compenents of 3D array x using SVD
%
% [X,V] = extract_PCA(x, N)
%
% ** x ND array
% ** N number of SVD modes
% returns:
% ++ X principal components
% ++ V principal weights
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [X,V] = extract_PCA(x, N)
[U,S,V] = fsvd(reshape(x,[],size(x,3)), N);
X = U*S;
X = reshape(X, size(x,1), size(x,2),[]);
end
@@ -0,0 +1,94 @@
% FFT2_SAFE wrapper around fft2, in case of low memory it will try to repeat Ntimes before giving up
%
% x = fft2_safe(x)
%
% ** x input array
% returns:
% ++ x 2D fft array
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 x = fft2_safe(x)
low_mem_errs = {'id:parallel:gpu:array:OOMForOperation',...
'id:MATLAB:LowGPUMem','MATLAB:LowGPUMem',...
'parallel:gpu:array:OOM',...
'parallel:gpu:device:UnknownCUDAError', ...
'parallel:gpu:array:OOMForOperation', ...
'parallel:gpu:array:FFTInternalError'};
N = 5;
for i = 1:N
try
x = fft2(x);
break
catch ME
if ~any(strcmpi(ME.identifier, low_mem_errs))
disp(N)
rethrow(ME)
end
pause(1)
end
warning('FFT was not able to allocate enough memory')
gpuDevice
dbstack
rethrow(ME)
end
end
@@ -0,0 +1,209 @@
% FIND_GEOM_CORRECTION use current probe positions estimates to update geometry model and
% improve the new probe positions
%
% [self] = find_geom_correction(self,cache, par, iter, best_mode_id, update_position_weight)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** par structure containing parameters for the engines
% ** iter current iteration
% ** best_mode_id strongest mode id
% ** update_position_weight update_position_weight
%
% returns:
% ++ self self-like structure with final reconstruction
%
%
function [self] = find_geom_correction(self,cache, par, iter, best_mode_id, update_position_weight)
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import utils.*
import math.*
mode = self.modes{best_mode_id};
%% constrain the detector rotation
% store only the single update per scan
if iter > par.detector_rotation_search
for ii = 1:length(self.reconstruct_ind)
ind = self.reconstruct_ind{ii};
mrot(ii) = mean(mode.probe_rotation_all(ind));
end
if par.mirror_objects
% enforce zero average rotation if two mirror scans are provided
mrot = mrot - mean(mrot);
end
for ii = 1:length(self.reconstruct_ind)
mode.probe_rotation_all(ind) = mrot(ii) ;
end
mode.probe_rotation(end+1,:) = mrot;
end
if iter <= par.probe_position_search
self.modes{best_mode_id} = mode;
return
end
pos = mode.probe_positions;
pos_0 = mode.probe_positions_0;
%if all(isnan(mode.probe_positions_weight(:))) || all(mode.probe_positions_weight(:)==0)
if update_position_weight %modified by YJ. add this option so users can update poisition weights more than once
%% EMPIRICAL ESTIMATION OF POSITION RELIABILITY
verbose(1,'EMPIRICAL ESTIMATION OF POSITION RELIABILITY\n')
illum = utils.crop_pad(abs(self.probe{1}(:,:,1)).^2, self.Np_p/2);
total_variation = zeros(self.Npos,2, 'single');
for ii = 1:par.Nscans
o_tmp = self.object{min(end,ii), par.layer4pos};
o_tmp = o_tmp ./ max2(abs(o_tmp(cache.object_ROI{:})));
% keep it more memory effecient (important for GPU !! )
Npos = length(self.reconstruct_ind{ii});
for jj = 1:ceil(Npos/par.grouping)
ind = 1+(jj-1)*par.grouping:min(Npos, jj*par.grouping);
obj_proj = get_views(o_tmp,[],1,1,self.reconstruct_ind{ii}(ind),cache);
obj_proj = utils.crop_pad(obj_proj, self.Np_p/2);
[nx, ny,~] = size(obj_proj);
[X,Y] = meshgrid(-ny/2:ny/2-1, -nx/2:nx/2-1);
% suppress edge effects of the FFT derivatives
spatial_filter = exp(-(X.^16+Y.^16)/(min(nx,ny)/2.2)^16);
obj_proj = obj_proj.* spatial_filter;
[dX, dY] = get_img_grad(obj_proj);
clear obj_proj
illum_proj = get_views(utils.imgaussfilt2_fft(cache.illum_sum_0{min(ii,end)},self.Np_p/10),[],1,1,self.reconstruct_ind{ii}(ind),cache);
illum_proj = utils.crop_pad(illum_proj, self.Np_p/2);
dX = abs(dX) .* illum_proj.* illum;
dY = abs(dY) .* illum_proj.* illum;
clear illum_proj
total_variation(self.reconstruct_ind{ii}(ind),:) = Ggather(sqrt(squeeze([mean2(dX),mean2(dY)]))');
clear dX dY
end
end
mode.probe_positions_weight = total_variation.^4./mean(total_variation.^4);
end
probe_positions_weight = double(mode.probe_positions_weight);
jj = size(mode.scales,1)+1;
% find geometry for each scan separatelly
for ii = 1:par.Nscans
ind = self.reconstruct_ind{ii};
C0 = mode.affine_matrix(:,:,ii) - eye(2);
C0 = C0(:);
if par.Nscans > 1 && par.share_object
% it the case of multiple scans allow also freedom of coordinates shifts
pos_fun = @(C)(( [1+C(1), C(2); C(3), 1+C(4)]*pos_0(ind,:)')' + C([5,6])' );
if isfield(mode, 'shift_scans' ) && size(mode.shift_scans,2)>=ii
C0(5:6) = mode.shift_scans(:,ii);
else
C0(5:6) = 0;
end
else
pos_fun = @(C)(( [1+C(1), C(2); C(3), 1+C(4)]*pos_0(ind,:)')' );
end
err_fun = @(C)( probe_positions_weight(ind,:) .* (pos(ind,:) - pos_fun(C)));
options = optimoptions('lsqnonlin','Display','off');
%C(:,ii) = lsqnonlin( err_fun, C0,[],[],options) ;
%% modified by YJ to avoid fitting error
try
C(:,ii) = lsqnonlin( err_fun, C0,[],[],options);
catch
disp('Fitting error during geom correction...')
C(:,ii) = [0,0,0,0,0,0];
end
%% restrict the geometry model only to the allowed degreed of freedom
% ===================================================================
M{ii} = reshape(C(1:4,ii),2,2)+eye(2);
M_fit = M; %added by YJ to keep track of estimated affine matrix
[scale, asymmetry, rotation, shear] = decompose_affine_matrix(M{ii});
if ~ismember('scale', par.probe_geometry_model)
scale = 1;
end
if ~ismember('asymmetry', par.probe_geometry_model)
asymmetry = 0;
end
if ~ismember('rotation', par.probe_geometry_model)
rotation = 0;
end
if ~ismember('shear', par.probe_geometry_model)
shear = 0;
end
M{ii} = compose_affine_matrix(scale, asymmetry, rotation, shear);
% ===================================================================
mode.scales(jj,ii) = scale;
mode.asymmetry(jj,ii) = asymmetry;
mode.rotation(jj,ii) = rotation;
mode.shear(jj,ii) = shear;
if par.Nscans > 1 && par.share_object
mode.shift_scans(:,ii) = C(5:6,ii);
else
mode.shift_scans(:,ii) = [0,0];
end
% store initial guess
mode.affine_matrix(:,:,ii) = M{ii};
% calculate ideal model positions
pos_model(ind,:) = pos_fun([reshape(M{ii} - eye(2), [],1); mode.shift_scans(:,ii)]);
end
self.affine_matrix = M;
self.affine_matrix_fit = M_fit; %added by YJ to keep track of estimated affine matrix
verbose(2,['----- Geom. correction ', repmat('%3.3g ', 1,length(C))], C)
% use average
resid_pos= pos - pos_model;
% ignore errors in the global shift of the positions
for ii = 1:par.Nscans
ind = self.reconstruct_ind{ii};
resid_pos(ind,:) = resid_pos(ind,:) - mean(resid_pos(ind,:));
end
err = abs(resid_pos);
max_err = par.probe_position_error_max ./ self.pixel_size .* self.relative_pixel_scale;
verbose(1, '==== AVG position error %3.2g px MAX error %3.2g LIMIT %3.2g px ', mean(err(:)), max(err(:)), max(max_err))
%modified by YJ: add par.apply_relaxed_position_constraint to allow position update without constraints from geometry model.
%Useful if there are big jumps in positions
if par.apply_relaxed_position_constraint
%% apply only relaxed constrain on the probe positions !!!
relax = 0.1;
% constrain more the probes in flat regions
W = relax*(1- (probe_positions_weight./ (1+probe_positions_weight)));
% penalize positions that are further than max_err from origin
W = min(10*relax, W+max(0,err - max_err).^2 ./ max_err.^2 ); % avoid travel larger than max error
else
W=0; %no geom model imposed to regularize positions
end
% allow free movement in depenence on realibility and max allowed error
pos_new = pos .*(1-W)+W.*pos_model;
mode.probe_positions = pos_new;
mode.probe_positions_model = pos_model;
if any(isnan(mode.probe_positions(:)))
keyboard
end
self.modes{best_mode_id} = mode;
end
@@ -0,0 +1,106 @@
% FIND_RECONSTRUCTION_ROI precalculate the reconstruction regions for CPU and GPU
%
% [oROI, oROI_vec, sub_px_shift] = find_reconstruction_ROI( positions,Np_o, Np_p )
%
% ** positions Npox*2 vector of scanning positions
% ** Np_o object size
% ** Np_p probe size
%
% returns:
% ++ oROI cell array contaning range for each view
% ++ oROI_vec cell array contaning range for each view in vector shape
% ++ sub_px_shift subpixel rounding errors, used for subpixel shift
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [oROI, oROI_vec, sub_px_shift] = find_reconstruction_ROI( positions,Np_o, Np_p )
positions = positions(:,[2,1]);
positions = positions + ceil(Np_o/2-Np_p/2);
sub_px_shift = positions - round(positions);
sub_px_shift = sub_px_shift(:,[2,1]); % return to the original XY coordinates
positions = round(positions);
range = [min(positions), max(positions)+ Np_p];
if any(range(1:2) < 0) || any(range(3:4) > Np_o)
error('Object size is too small, not enough space for probes !! \nposition range: %i %i %i %i, \nobject size: %i %i ', range(1), range(2), range(3), range(4), Np_o(1), Np_o(2))
end
oROI = cell(2,1);
for dim = 1:2
oROI{dim} = [positions(:,dim),positions(:,dim)+ Np_p(dim)-1]-1;
oROI{dim} = uint32(oROI{dim});
end
if nargout > 1
Npos = length(positions);
oROI_vec = cell(Npos,2);
for ii = 1:Npos
for i = 1:2
oROI_vec{ii,i} = oROI{i}(ii,1):oROI{i}(ii,2);
end
end
end
end
@@ -0,0 +1,150 @@
% self = flip_mirror_scan(self, align_objects )
% join two scans obtained at 0 and 180 degrees to get a better estiamte
% of geometry errors
%
% Inputs:
% self main data structure
% align_objects find optimal shifts between the scans to match the 0 and 180 deg scan
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 self = flip_mirror_scan(self, align_objects)
import engines.GPU_MS.*
import utils.*
import engines.GPU_MS.GPU_wrapper.*
if nargin < 2
align_objects = true; % find optimal shifts between the scans to match the 0 and 180 deg scan
end
verbose(1,'=== Flipping 2nd scan ==== ')
assert(length(self.reconstruct_ind) == 2, 'Two mirrored (0vs180deg) scans are required')
for layer = 1:size(self.object,2)
self.object{2,layer} = fliplr(self.object{min(end,2),layer});
end
ind = self.reconstruct_ind{2};
%% FLIP THE 2ND SCAN POSITIONS
% keep the position distance from the reconstruction edge
self.probe_positions_0 = flip_positions(self, self.probe_positions_0, ind);
if ~isempty(self.probe_positions)
self.probe_positions = flip_positions(self, self.probe_positions, ind);
end
%% flip other inputs
for ii = 1:length(self.probe)
self.probe{ii}(:,:,2,:) = fliplr(self.probe{ii}(:,:,min(end,2),:));
end
if ~isempty(self.diffraction)
self.diffraction(:,:,ind) = fliplr(self.diffraction(:,:,ind));
end
if ~isempty(self.mask)
if size(self.mask,3) == 1
self.mask = repmat(self.mask, 1, 1, self.Npos);
end
self.mask(:,:,ind) = fliplr(self.mask(:,:,ind));
end
if isfield(self, 'affine_matrix')
% flip nondiagonal terms of the affine matrix for account for the flipping
self.affine_matrix{2} = self.affine_matrix{2} .* [1,-1; -1,1];
end
if align_objects
% using crosscorrelation find optimal shift between objects
self = shared.align_objects(self);
else
% move the probe back to the center of the asize
for ii = 1:2
[cx, cy] = math.center(abs(self.probe{1}(:,:,ii,1)).^2);
self.probe{1}(:,:,ii,:) = utils.imshift_fft(self.probe{1}(:,:,ii,:), -cx, -cy);
self.object{ii} = utils.imshift_fft(self.object{ii}, -cx, -cy);
end
end
end
function pos_0 = flip_positions(self, pos_0, ind)
% flip positions so that the flipped object does not move after
% reconstruction
pos = pos_0(ind,1);
offset = ceil(self.Np_o(2)/2-self.Np_p(2)/2);
pos = -(pos + offset);
left_offset = -max(pos);
right_offset = self.Np_o(2) -max(pos)-self.Np_p(2) ;
pos = pos + right_offset - left_offset;
pos = pos - offset;
pos_0(ind,1) = pos ;
end
@@ -0,0 +1,107 @@
% FWD_FOURIER_PROJ forward propagate to the detector plane
% Psi = fwd_fourier_proj(Psi, mode)
%
% ** psi [Nx,Ny,N] array, back-propagated complex valued exit-wave
% ** mode structure containing parameters for selected probe mode
%
% returns
% ++ Psi [Nx,Ny,N] array, propagated complex valued exit-wave Psi = F(P*O)
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [Psi] = fwd_fourier_proj(Psi, mode, ind)
import engines.GPU_MS.shared.*
try
if ~isempty(mode.tilted_plane_propagate_back)
% propagate from tilted plane of the sample to flat plane of the detector (if provided)
Psi = mode.tilted_plane_propagate_back(Psi);
end
if nargin > 2 && any(mode.probe_rotation_all)
% apply rotation to the probe , used for camera angle refinement
Psi = utils.imrotate_ax_fft(Psi,-mode.probe_rotation_all(ind),3);
end
if mode.probe_scale_upd(end) ~= 0
% apply scaling to the probe , used for camera distance refinement
Psi = utils.imrescale_frft(Psi,1+mode.probe_scale_upd(end));
end
if mode.distances(end) == 0
% no propagation
elseif mode.distances(end) == inf
Psi = fft2_safe(Psi); % fully farfield
% Psi = fftshift_2D(fft2_safe(fftshift_2D(Psi))); % fully farfield
elseif mode.distances(end) == -inf
Psi = ifft2_safe(Psi); % fully farfield backward
elseif ~isempty(mode.FAR_factor) % almost far field
Psi = bsxfun(@times, Psi, mode.FAR_factor);
Psi = fftshift_2D(fft2_safe(fftshift_2D(Psi)));
elseif ~isempty(mode.ASM_factor) % near field transformation
Psi = fft2_safe(Psi);
Psi = Psi .* mode.ASM_factor;
Psi = ifft2_safe(Psi);
else
error('Not implemented')
end
catch err
if utils.verbose()> 0
warning(err.message)
keyboard
else
rethrow(err)
end
end
end
+125
View File
@@ -0,0 +1,125 @@
% GET_ROI find optimal rectange containing the logical mask
%
% ROI = get_ROI(mask, extent = 0, type = 'any')
%
% ** mask logical array
% ** extend (scalar) relative increase of the found range
% ** type (string) odd, even, pow2, any
%
% returns:
% ++ ROI smallest ROI that contains the mask
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [ROI] = get_ROI(mask, extent, type)
if all(mask(:) == 0)
error('Empty mask')
end
if nargin < 3
type = 'any';
end
if nargin == 1
extent = 0.25;
end
[W,H] = size(mask);
if ~islogical(mask)
error('Not implemented')
end
x = any(mask,2);
y = any(mask,1);
coord = [find(x, 1,'first'), find(x, 1,'last'), find(y, 1,'first'), find(y, 1,'last')];
w = (coord(2) - coord(1));
h = (coord(4) - coord(3));
Cx = (coord(2) +coord(1))/2;
Cy = (coord(4) + coord(3))/2;
coord(1) = floor(Cx - ceil( (0.5 + extent) *w )) ;
coord(2) = ceil(Cx + ceil((0.5 + extent) * w )) ;
coord(3) = floor(Cy - ceil((0.5 + extent) * h )) ;
coord(4) = ceil(Cy + ceil((0.5 + extent) * h )) ;
switch type
case 'odd'
coord(2) = coord(1) + floor((coord(2) - coord(1))/2)*2;
coord(4) = coord(3) + floor((coord(4) - coord(3))/2)*2;
case 'even'
coord(2) = coord(1) + ceil((coord(2) - coord(1))/2)*2-1;
coord(4) = coord(3) + ceil((coord(4) - coord(3))/2)*2-1;
case 'pow2'
w = 2^(nextpow2( coord(2) - coord(1))-2);
h = 2^(nextpow2( coord(4) - coord(3))-2);
Cx = (coord(2) +coord(1))/2;
Cy = (coord(4) + coord(3))/2;
coord(1) = round(Cx - w) ;
coord(2) = round(Cx + w-1) ;
coord(3) = round(Cy - h) ;
coord(4) = round(Cy + h-1) ;
end
coord(coord < 1) = 1;
if coord(2) > W ; coord(2) = W; end
if coord(4) > H ; coord(4) = H; end
ROI = {coord(1):coord(2), coord(3):coord(4)};
end
@@ -0,0 +1,92 @@
% GET_MASK get mask for current list of indices ind
%
% mask = get_mask(self,mask_indices, ind, damped_mask)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** mask_indices indices that determine which mask is needed for each scan position
% ** ind currenly processed indices
% ** damped_mask relaxation constant that push masked values weakly to the measured value, useful for missing pixels when the data were fixed by nearest neighbor
%
% returns:
% ++ mask prepared mask array (true == ignored values)
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 mask = get_mask(self,mask_indices, ind, damped_mask)
import engines.GPU_MS.GPU_wrapper.*
mask = [];
if ~isempty(self.mask)
if size(self.mask, 3) == 1
mask = self.mask;
else
mask = self.mask(:,:,mask_indices(ind));
end
end
mask = Garray(mask);
if (isa(mask, 'gpuArray') && strcmpi(classUnderlying(mask), 'uint8')) || isa(mask, 'uint8')
mask = single(mask)/ 255; % convert back to singles from the storage class
end
% push masked regions towards data value (ie towards zero)
if nargin == 4 && damped_mask > 0
mask = mask .* single(1-damped_mask);
end
end
@@ -0,0 +1,123 @@
% GET_MODULUS load and prepare data for current positions ind
%
% modF = get_modulus(self, cache, ind, move_to_gpu = true, bunch_id)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** ind currenly processed indices
% ** move_to_gpu (bool) if the data should be moved on GPU or left on CPU , default == true
% ** bunch_id number of the processed bunch, used only if the positions are stored as a list of cells with data
%
% returns:
% ++ modF prepared modulus data
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 modF = get_modulus(self, cache, ind, move_to_gpu, bunch_id)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
if ~iscell(self.diffraction)
% if diffraction is just array, load block ind
modF = self.diffraction(:,:,ind);
else
% load from cell array
modF = self.diffraction{bunch_id};
end
if isa(modF, 'gpuArray')
data_class = classUnderlying(modF);
else
data_class = class(modF);
end
if nargin < 4 || move_to_gpu
% load on GPU if not there yet
modF = Garray(modF);
end
switch data_class
case {'uint16', 'uint8'} % simple sqrt lossy online compression
modF = single(modF)/ cache.C_factor;
verbose(5, 'UINT8/16 lossy compression')
case 'int8'
% lossy compression
modF = single(modF) ;
if size(cache.US_diffraction,2) == 1 && isa(modF, 'gpuArray')
% 1 SVD mode - run merged kernel
modF = Gfun(@decompress_N1,modF, reshape(cache.US_diffraction,self.Np_p),...
reshape(cache.V_diffraction(ind),1,1,[]), cache.C_factor);
else % mode SVD modes
modF = modF + round(reshape(cache.US_diffraction*cache.V_diffraction(ind,:)', size(modF)));
modF = modF / cache.C_factor;
end
verbose(5, 'SVD INT8 lossy compression')
case 'single'
otherwise
error('Not implemented class %s', data_class)
end
end
function modF = decompress_N1(modF, US, V, C)
modF = modF + round(US .* V);
modF = modF / C;
end
@@ -0,0 +1,76 @@
% GET_NOISE load and prepare noise estimate for current positions ind
%
% noise = get_noise(self, par, g_ind)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** g_ind processed indices
%
% returns:
% ++ noise noise intensity for selected frames
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 noise = get_noise(self, par, g_ind)
if par.relax_noise && ~isempty(self.noise)
noise = self.noise(:,:,g_ind);
else
noise = [];
end
end
@@ -0,0 +1,85 @@
% GET_OBJECT_EXTENT estimate the minimal area surrounding all the probe positions
%
% Np_o_new = get_object_extent(Np_p, probe_positions, extra_padding)
%
% ** Np_p proeb size
% ** probe_positions (Npos,2) vector of probe positions
% ** extra_padding relative extra padding of the estimated project size
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 Np_o_new = get_object_extent(Np_p, probe_positions, extra_padding)
probe = ones(Np_p);
Np_o = [1e5, 1e5];
Npos = size(probe_positions,1);
Np_p = size(probe);
probe_positions = bsxfun(@minus, probe_positions, (max(probe_positions)+min(probe_positions))/2 );
[oROI] = engines.GPU_MS.shared.find_reconstruction_ROI(probe_positions, Np_o, Np_p );
Range = [Np_o, 0,0];
for i = 1:Npos
Range(1) = min(Range(1), oROI{1}(i,1));
Range(2) = min(Range(2), oROI{2}(i,1));
Range(3) = max(Range(3), oROI{1}(i,2));
Range(4) = max(Range(4), oROI{2}(i,2));
end
Np_o_new = [Range(3) - Range(1), Range(4) - Range(2)];
Np_o_new= ceil(Np_o_new*(1+extra_padding));
end
+199
View File
@@ -0,0 +1,199 @@
% GET_VIEWS extract view for each the provided positions and indices
%
% obj_proj = get_views(object, obj_proj,layer_ids,object_id, indices, cache, scan_ids, skip_ind)
%
% ** object [Nx_o, Ny_o] array or cells containing object
% ** obj_proj [Nx_o, Ny_o, N] preallocated array for the views
% ** layer_ids id of the solved layer for multilayer ptycho
% ** object_id id of the object, ie scan or incoherent mode
% ** indices processed positions
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** scan_ids determines to which scan correponds each of the position
% ** skip_ind list of indices to be skipped
%
% returns:
% ++ obj_proj [Nx_p, Ny_p, N] array with the object views
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 obj_proj = get_views(object, obj_proj,layer_ids,object_id, indices, cache, scan_ids, skip_ind)
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import utils.verbose
global use_gpu
Np_p = [size(obj_proj,1),size(obj_proj,2)];
if size(obj_proj,3) ~= numel(indices) || isempty(obj_proj)
% in the last iter, number of items may not be equal to grouping
if isempty(obj_proj)
obj_proj = Gzeros([Np_p, numel(indices)], true);
else
% should be tiny bit faster
obj_proj = zeros([Np_p, numel(indices)], 'like', obj_proj);
end
end
if nargin < 8
skip_ind = [];
end
skip_ind = [skip_ind, cache.skip_ind];
if nargin > 6
%% !!! call recursivelly -> wrapper for multiscan version !!!!
% get unique IDs of the scans
if isempty(scan_ids)
unq_scans = [];
elseif all(scan_ids == scan_ids(1))
unq_scans = scan_ids(1);
else
unq_scans = unique(scan_ids);
end
if (length(unq_scans)> 1 || length(object) > 1 ) && iscell(object)
if ~isempty(use_gpu) && use_gpu && isa(object{object_id(1)}, 'gpuArray') && isa(obj_proj, 'gpuArray')
% object_modes > 1 not implemented yet
if size(object,1) == 1
% shared object or single object
ind_ok{1} = uint16(1:length(indices));
else
for kk = 1:size(object,1)
ind_ok{kk} = uint16(find(scan_ids == kk));
end
end
% feed data directly to the GPU mex without splitting
obj_proj = get_views_gpu(object(:,layer_ids),obj_proj,cache.oROI_s{min(end,object_id(1))}, indices, ind_ok);
else
% if GPU not available use this "wrapper" around single
% set_projection function
for kk = unq_scans
ind = scan_ids == kk;
skip_ind = indices(~ind); % avoid going through these indices
obj_proj = get_views(object{kk,layer_ids},obj_proj, 1, object_id, indices, cache, [], skip_ind);
end
end
return
end
end
object_id = object_id(1);
try
if iscell(object)
object = object{object_id, layer_ids};
end
catch
keyboard
end
if ~isempty(cache.skip_ind) && ~isempty(skip_ind)
ind_ok = uint16(find(~ismember(indices, [cache.skip_ind,skip_ind]))); % skip wrong patterns
else
ind_ok = uint16(1:length(indices));
end
if isempty(obj_proj)
Np_p = [length(cache.oROI{min(object_id,end)}{1,1}), length(cache.oROI{min(object_id,end)}{1,2})];
obj_proj = Gzeros([Np_p, length(ind_ok)], true);
end
if isa(object, 'gpuArray')
%% USE CUDA MEX FOR GPU
obj_proj = get_views_gpu(object,obj_proj,cache.oROI_s{min(end,object_id)}, indices, ind_ok);
else
%% USE CPU
positions = int32([cache.oROI_s{min(end,object_id)}{1}(indices,1), cache.oROI_s{min(end,object_id)}{2}(indices,1)]);
obj_proj = utils.get_from_3D_projection(obj_proj,object, positions, ind_ok);
end
end
function obj_proj = get_views_gpu(object, obj_proj, ROI, ind, ind_ok)
% Description: simple method to get GPU based projections from object
import utils.verbose
% mexcuda -output +engines/+GPU_MS/get_views_gpu_mex +engines/+GPU/get_views_gpu_mex.cu
import engines.GPU_MS.GPU_wrapper.*
%% do not return matrices, write directly into obj_proj !!!
x = uint16(ROI{1}(ind,1));
y = uint16(ROI{2}(ind,1));
if ~iscell(object); object = {Garray(complex(object))}; end
if ~iscell(ind_ok); ind_ok = {Garray(uint16(ind_ok))}; end
obj_proj = complex(Garray(obj_proj));
try
get_views_gpu_mex( obj_proj, object,x,y, ind_ok);
catch err
verbose(0, 'Recompilation of MEX functions ... ')
if any(strcmp(err.identifier, { 'MATLAB:UndefinedFunction','MATLAB:mex:ErrInvalidMEXFile'}))
path = replace(mfilename('fullpath'), mfilename, '');
mexcuda('-output', [path,'private/get_views_gpu_mex'], [path, 'private/get_views_gpu_mex.cu'])
get_views_gpu_mex( obj_proj, object,x,y, ind_ok);
else
rethrow(err)
end
end
end
@@ -0,0 +1,91 @@
% IFFT2_SAFE wrapper around fft2, in case of low memory it will try to repeat 5-times before giving up
% ** x input array
%
% x = ifft2_safe(x)
%
% ** x input stacked array
% returns:
% ++ x 2D fft array
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 x = ifft2_safe(x)
low_mem_errs = {'id:parallel:gpu:array:OOMForOperation',...
'id:MATLAB:LowGPUMem','MATLAB:LowGPUMem',...
'parallel:gpu:array:OOM',...
'parallel:gpu:device:UnknownCUDAError', ...
'parallel:gpu:array:OOMForOperation',...
'parallel:gpu:array:FFTInternalError'};
N = 5;
for i = 1:N
try
x = ifft2(x);
break
catch ME
if ~any(strcmpi(ME.identifier, low_mem_errs))
rethrow(ME)
end
pause(1)
end
warning('FFT was not able to allocate enough memory')
gpuDevice
dbstack
rethrow(ME)
end
end
@@ -0,0 +1,78 @@
% IS_METHOD try if the method match or at least partially match
%
% match = is_method(par, method)
%
% ** par parameter structure
% ** method method name , even partial will work
% returns
% ++ match (bool) if the current method and string "method" at least partialyl match
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 match = is_method(par, method)
match = 0;
if ~iscell(method)
method = {method};
end
try
for i = 1:length(method)
match = match || contains(lower(par.method), lower(method{i}));
end
catch
keyboard
end
end
+89
View File
@@ -0,0 +1,89 @@
% IS_USED Check if the extension/extensions is set to be used
%
% used = is_used(param, extension)
% Example: is_used(param, {'ext1', 'ext2'})
%
% ++ param parametrer structure
% ++ extension string or cell of the extension names
% returns:
% ** used bool if method is used
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 used = is_used(param, extension)
if ~isfield(param, 'extension'); used = false; return ; end
if ~iscell(param.extension) && ~iscell(extension)
used = strcmpi(param.extension, extension);
else
if ~iscell(extension)
extension = {extension};
end
if ~iscell( param.extension)
param.extension = {param.extension};
end
used = false;
for i = 1:length(param.extension)
for j = 1:length(extension)
used = used | strcmpi(param.extension{i}, extension{j});
if used; break; end
end
if used; break; end
end
end
end
@@ -0,0 +1,135 @@
% FUNCTION [u_1, H, h, dH] = near_field_evolution(u_0, z, lambda, extent, use_ASM_only)
% Description: nearfield evolution function, it automatically swithc
% between ASM and Fraunhofer propagation
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [u_1, H, h, dH] = near_field_evolution(u_0, z, lambda, extent, use_ASM_only,tiltx,tilty)
H = [];
h = [];
u_1 = [];
dH = [];
if nargin < 5
use_ASM_only = false;
end
if nargin < 7
tiltx = 0;
tilty = 0;
end
extent = extent(:)' .* ones(1,2);
if z == 0
H = 1;
u_1 = u_0;
return
end
if z == inf
return
end
Npix = size(u_0);
xgrid = (0.5+(-Npix(1)/2:Npix(1)/2-1))/Npix(1);
ygrid = (0.5+(-Npix(2)/2:Npix(2)/2-1))/Npix(2);
k = 2 * pi / lambda(1);
% Undesamplling parameter
F = mean( extent.^2 ./ (lambda(1) .* z .* Npix ));
%if abs(F) < 1 && ~use_ASM_only
if abs(F) < 1 && ~use_ASM_only && tiltx == 0 && tilty == 0
% farfield propagation
warning('Farfield regime, F/Npix=%g', F )
Xrange = xgrid*extent(1);
Yrange = ygrid*extent(2);
[X,Y] = meshgrid(Xrange, Yrange);
h = exp(1i*k*z +1i*k/(2*z) * (X'.^2 + Y'.^2));
% this serves as low pass filter for the far nearfield
H = ifftshift(fft2(fftshift(h)));
H = H / abs(H(end/2+1, end/2+1)); % renormalize to conserve flux in image
else
% standard ASM
%kx = 2 * pi .*xgrid / extent(1) * Npix(1) ;
%ky = 2 * pi .*ygrid / extent(2) * Npix(2);
kx = xgrid / extent(1) * Npix(1) ;
ky = ygrid / extent(2) * Npix(2);
[Kx, Ky] = meshgrid(kx, ky);
k2 = Kx'.^2 + Ky'.^2;
%dH = ( -1i*(Kx'.^2+Ky'.^2)/(2*k) );
dH = (-2*pi*1i*k2/(2*k) ); %dH is not used anywhere in the code I think
%H = exp( 1i*z*sqrt( k^2 - Kx'.^2-Ky'.^2)); % it make it a bit more sensitive to z distance
% i don;t know if what yi had made sense to me
H = exp(-1i*pi*lambda*z*k2 + 2i*pi*z*(Kx'.*tan(-tiltx) + Ky'.*tan(-tilty)) );
h = [];
end
u_1 = ifft2( bsxfun(@times, ifftshift(H), fft2(u_0)));
end
@@ -0,0 +1,89 @@
% NEAR_FIELD_EVOLUTION_GRADIENT Gradient of the nearfield evolution function, useful for calcualteion of the optimal propagation
% distance
%
% [dH] = near_field_evolution_gradient(Npix, lambda, extent)
%
% ** Npix size of the propagated window
% lambda wavelenght
% extend lateral size of the window
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [dH] = near_field_evolution_gradient(Npix, lambda, extent)
extent = extent(:)' .* ones(1,2);
xgrid = (0.5+(-Npix(1)/2:Npix(1)/2-1))/Npix(1);
ygrid = (0.5+(-Npix(2)/2:Npix(2)/2-1))/Npix(2);
k = 2 * pi / lambda(1);
% standard ASM
kx = 2 * pi .*xgrid / extent(1) * Npix(1) ;
ky = 2 * pi .*ygrid / extent(2) * Npix(2);
[Kx, Ky] = meshgrid(kx, ky);
dH = ( -1i*(Kx'.^2+Ky'.^2)/(2*k) );
end
@@ -0,0 +1,90 @@
% ORTHO_MODES return orthogonal modes, without any normalization , expect cells
%
% x = ortho_modes(x)
%
% ** x cell array with probes to be orthogonalized
%
% returns:
% ++ x orthogonalized 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 x = ortho_modes(x)
for i = 1:length(x)
power(i) = sum2(abs( mean(x{i},3)).^2);
for j = 1:i-1
mx_j = mean(x{j},3);
mx_i = mean(x{i},3);
proj = sum2(mx_i .* conj(mx_j)) ...
/ sum2(abs(mx_j).^2);
x{i} = x{i} - proj * mx_j;
end
end
% sort them, should improve convergence
[~,ind]=sort(-power);
x(2:end) = x(ind(2:end));
end
%% added by YJ
function x = sum2(x)
x = sum(sum(x,1),2);
end
@@ -0,0 +1,97 @@
% ORTHO_MODES_EIG orthogonal modes using eigen values
%
% x = ortho_modes_eig(x)
%
% ** x cell array with probes to be orthogonalized
%
% returns:
% ++ x orthogonalized 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 x = ortho_modes_eig(x)
N = length(x);
Np = [size(x{1},1),size(x{1},2)];
for i = 1:N
x_tmp{i} = mean(x{i}(:,:,:,1),3);
end
A = zeros(N,N, class(x{1}));
for i = 1:N
for j = 1:N
A(i,j) = (x_tmp{i}(:)'* x_tmp{j}(:));
end
end
[v,d] = eig(A);
[~,di] = sort(-abs(diag(d)));
for j = 1:N
x_new{j} = zeros(Np, class(x{1}));
for i = 1:N
x_new{j} = x_new{j} + x{i}*v(i,j);
end
end
%% sort by energy
x_new = x_new(di);
% dont update the first one (it may interefere with OPR method)
x(2:end) = x_new(2:end);
end
@@ -0,0 +1,85 @@
% PRECALCULATE_ROI precalculate the views areas for given probe_positions for faster calculations
%
% cache = precalculate_ROI(self, cache, probe)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** probe complex array with the current probe, used to estimate probe smallest support
% returns:
% ++ cache structure with precalculated values to avoid unnecessary overhead
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 cache = precalculate_ROI(self, cache, probe)
import math.*
import utils.*
import engines.GPU_MS.shared.*
object_ROI = {round(self.Np_o(1)/2+(min(self.probe_positions_0(:,2)):max(self.probe_positions_0(:,2)))), ...
round(self.Np_o(2)/2+(min(self.probe_positions_0(:,1)):max(self.probe_positions_0(:,1))))};
Np = size(probe,1);
[~, fftshift_idx] = fftshift_2D(probe);
cache.object_ROI = object_ROI;
cache.fftshift_idx = fftshift_idx;
end
@@ -0,0 +1,168 @@
/*
*
*
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 cSAXS
ptychography MATLAB package 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
LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
(doi: 10.1126/science.1158573),
for mixed coherent modes:
P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
for LSQ-ML method
M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
for OPRP method
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." Optics express 24.8 (2016): 8360-8369
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, 2908929108 (2016).
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.
*
Compilation from Matlab:
maybe a tiny bit faster code is generated by
mex -O COPTIMFLAGS='-O2' LDOPTIMFLAGS='-O2' CFLAGS="\$CFLAGS -fopenmp" LDFLAGS="\$LDFLAGS -fopenmp" get_views_cpu_mex.cpp
Usage from Matlab:
get_views_cpu_mex(object,obj_proj,positions, ind_ok);
This code in matlab:
asize = size(probe);
for i=ind_ok
Indy = positions(i,1) + (1:asize(1));
Indx = positions(i,2) + (1:asize(2));
ob(Indy,Indx) = ob(Indy,Indx) + probe;
end
*/
#include "mex.h"
#include <math.h>
#include <stdio.h>
#include <omp.h>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int i;
/* Check for proper number of arguments. */
if (nrhs != 4)
mexErrMsgTxt("Four input arguments required: get_views_cpu_mex(object,obj_proj,positions,ind_ok)");
else if (nlhs != 0)
mexErrMsgTxt("No output argument has to be specified.");
/* Input must be of type single. */
for (i=0; i < 2; i++) {
if (mxIsSingle(prhs[i]) != 1){
printf(" Input %d is not single\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* Input must be of type int32. */
for (i=2; i<nrhs; i++){
if (mxIsInt32(prhs[i]) != 1){
printf("Input %d is not integer\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* It cannot be one-dimensional */
if(mxGetNumberOfDimensions(prhs[0]) < 2) {
printf("The 1st input argument must have at least two 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 */
if(mxIsComplex(prhs[0]) != 1) {
printf("object input argument must be complex-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
}
if(mxIsComplex(prhs[1]) != 1) {
printf("probe input argument must be complex-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
}
float *object_r, *object_i, *projection_r,*projection_i;
const int *positions;
positions = (int*)mxGetData(prhs[2]);
object_r = (float*)mxGetData(prhs[0]);
projection_r = (float*)mxGetData(prhs[1]);
/* get pointers to input data */
object_i = (float*)mxGetImagData(prhs[0]);
projection_i = (float*)mxGetImagData(prhs[1]);
/* Get dimension of probe and object */
const mwSize Ndims = mxGetNumberOfDimensions(prhs[1]);
const mwSize * dims = mxGetDimensions(prhs[1]);
const mwSize No_y = mxGetM(prhs[0]);
const mwSize No_x = mxGetN(prhs[0]);
const mwSize Np_y = dims[0];
const mwSize Np_x = dims[1];
const mwSize Npos = mxGetM(prhs[2]);
if((Npos > dims[2])) {
printf("wrong size of update / positions %i", Ndims);
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
}
mwSize id_small, id_large, pos, col, row;
#pragma omp parallel for private(pos,col, row, id_small, id_large)
for (pos=0;pos<Npos;pos++){
for (col=0;col<Np_x;col++) {
for (row=0;row<Np_y;row++) {
id_small = row + col*Np_y + Np_y*Np_x*pos;
id_large = row + positions[pos] + (col+positions[pos+Npos])*No_y;
projection_r[id_small] = object_r[id_large];
projection_i[id_small] = object_i[id_large];
}
}
}
return;
}
@@ -0,0 +1,260 @@
/*
Get complex views from complex object
mexcuda -output +engines/+GPU/+shared/private/get_views_gpu_mex +engines/+GPU/+shared/private/get_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;
__constant__ uint16_T gC_ind_read[MAX_IND_READ];
__constant__ uint16_T gC_pos_X[MAX_IND_READ];
__constant__ uint16_T gC_pos_Y[MAX_IND_READ];
#define MAX(x,y) (x>y?x:y);
#define MIN(x,y) (x<y?x:y);
#define ABS(x) (x>0?x:-x);
int checkLastError(char * msg)
{
cudaError_t cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
char err[512];
sprintf(err, "getprojection failed \n %s: %s. \n", msg, cudaGetErrorString(cudaStatus));
mexPrintf(err);
return 1;
}
return 0;
}
/*
* Device code
*/
/*********** fast const memory based version ***************/
__global__ void readFromArray_c_fast(float2 * sarray, const float2 * larray,
cuint Np_px,cuint Np_py, cuint Np_pz,cuint Np_ox, cuint Np_oy,
cuint Npos) {
// Location in a 3D matrix
int idx= blockIdx.x * blockDim.x + threadIdx.x;
int idy= blockIdx.y * blockDim.y + threadIdx.y;
int id = blockIdx.z * blockDim.z + threadIdx.z;
if ( idx < Np_px & idy < Np_py & id < Npos)
{
int idz = gC_ind_read[id]-1; // go only through some of the indices
int id_large = gC_pos_X[idz]+idx + Np_ox*(gC_pos_Y[idz]+idy);
int id_small = idx + Np_px*idy + Np_px*Np_py*idz ;
sarray[ id_small ].x = larray[ id_large ].x ;
sarray[ id_small ].y = larray[ id_large ].y ;
}
}
/*********** global memory based version ***************/
__global__ void readFromArray_c(float2 * sarray, const float2 * larray, cuint16* ind_read, cuint16* pos_X, cuint16* posY,
cuint Np_px,cuint Np_py, cuint Np_pz,cuint Np_ox, cuint Np_oy,
cuint Npos) {
// Location in a 3D matrix
int idx= blockIdx.x * blockDim.x + threadIdx.x;
int idy= blockIdx.y * blockDim.y + threadIdx.y;
int id = blockIdx.z * blockDim.z + threadIdx.z;
if ( idx < Np_px & idy < Np_py & id < Npos)
{
int idz = ind_read[id]-1; // go only through some of the indices
int id_large = pos_X[idz]+idx + Np_ox*(posY[idz]+idy);
int id_small = idx + Np_px*idy + Np_px*Np_py*idz ;
sarray[ id_small ].x = larray[ id_large ].x ;
sarray[ id_small ].y = larray[ id_large ].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.";
/* Initialize the MathWorks GPU API. */
//mxInitGPU();
/* Check for proper number of arguments. */
if (nrhs != 5)
mexErrMsgTxt("Five input arguments required");
for (i=0; i < 2; i++) {
if ( !mxIsGPUArray(prhs[i]) && !mxIsCell(prhs[i]) ){
printf("Input %d is not GPU array / cell \n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
// load positions
const mxGPUArray * m_positions_x = mxGPUCreateFromMxArray(prhs[2]);
if ((mxGPUGetClassID(m_positions_x) != mxUINT16_CLASS)) {
mexPrintf("m_positions_x\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const cuint16 * p_positions_x = (cuint16 *)mxGPUGetDataReadOnly(m_positions_x);
const mxGPUArray * m_positions_y = mxGPUCreateFromMxArray(prhs[3]);
if ((mxGPUGetClassID(m_positions_y) != mxUINT16_CLASS)) {
mexPrintf("m_positions_y\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const cuint16 * p_positions_y = (cuint16 *)mxGPUGetDataReadOnly(m_positions_y);
/**** copy of the array is the slowest operation *****/
// Now it is writting directly into the input field !!!
//mxGPUArray * m_obj_proj = mxGPUCopyFromMxArray(prhs[0]);
mxGPUArray * m_obj_proj = const_cast<mxGPUArray*>(mxGPUCreateFromMxArray(prhs[0])); //
if ((mxGPUGetClassID(m_obj_proj) != mxSINGLE_CLASS)) {
mexPrintf("m_obj_proj\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
if (mxGPUGetComplexity(m_obj_proj) != mxCOMPLEX) {
mexPrintf("m_obj_proj is not complex \n");
mexErrMsgIdAndTxt(errId, errMsg);
}
float2 * p_obj_proj = (float2 *)mxGPUGetData(m_obj_proj);
if (!mxIsCell(prhs[1]) || !mxIsCell(prhs[4]))
mexErrMsgIdAndTxt("MexError:ptycho","Object and indices has to be in cell/cells !! ");
if (mxGetNumberOfElements(prhs[1]) != mxGetNumberOfElements(prhs[4]))
mexErrMsgIdAndTxt("MexError:ptycho","Number of objects != number of indices ");
/* Get dimension of probe and object */
const unsigned int Ndims = (unsigned int)mxGPUGetNumberOfDimensions(m_obj_proj);
cuint Ncells = mxGetNumberOfElements(prhs[1]);
const unsigned int Np_pp = mxGPUGetNumberOfElements(m_positions_x);
if (Np_pp < MAX_IND_READ) {
cudaMemcpyToSymbol(gC_pos_X, p_positions_x, Np_pp*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(gC_pos_Y, p_positions_y, Np_pp*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
checkLastError("after cudaMemcpyToSymbol pos");
}
for (int l=0; l<Ncells; l++)
{
// read the cell content
mxArray * mx_object = mxGetCell(prhs[1],l);
mxArray * mx_ind = mxGetCell(prhs[4],l);
int N_ok = mxGetNumberOfElements(mx_ind);
if(N_ok == 0)
continue;
const mxGPUArray * m_object = mxGPUCreateFromMxArray(mx_object);
if ((mxGPUGetClassID(m_object) != mxSINGLE_CLASS)) {
mexPrintf("m_object\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
if (mxGPUGetComplexity(m_object) != mxCOMPLEX) {
mexPrintf("m_object is not complex \n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const float2 * p_object = (float2 *)mxGPUGetDataReadOnly(m_object);
const mxGPUArray * m_ind_ok = mxGPUCreateFromMxArray(mx_ind);
if ((mxGPUGetClassID(m_ind_ok) != mxUINT16_CLASS)) {
mexPrintf("m_ind_ok\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const cuint16 * p_ind_ok = (cuint16 *)mxGPUGetDataReadOnly(m_ind_ok);
const mwSize * Np_o = mxGPUGetDimensions(m_object);
const mwSize * Np_p = mxGPUGetDimensions(m_obj_proj);
const unsigned int Npos = mxGPUGetNumberOfElements(m_ind_ok);
// mexPrintf("Ndims %i Np_o %i %i Np_p %i %i %i Npos %i \n " ,Ndims,Np_o[0],Np_o[1],Np_p[0],Np_p[1],Np_p[2],Npos);
if (Ndims == 3 && Npos > Np_p[2]) {
printf("wrong size of update / positions %i", Ndims);
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
}
// 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_p[0] + threadsPerBlockEachDim - 1) / threadsPerBlockEachDim;
int const blocksPerGrid_N = (Np_p[1] + threadsPerBlockEachDim - 1) / threadsPerBlockEachDim;
int const blocksPerGrid_O = Npos;
// mexPrintf("Threads %i %i %i \n ", blocksPerGrid_M, blocksPerGrid_N, blocksPerGrid_O);
dim3 const dimBlock(blocksPerGrid_M, blocksPerGrid_N, blocksPerGrid_O);
dim3 const dimThread(threadsPerBlockEachDim, threadsPerBlockEachDim, 1);
checkLastError("after dimThread");
//mexPrintf("Blocks %i %i %i \n ", dimThread.x, dimThread.y, dimThread.z);
if (Np_pp > MAX_IND_READ) {
//mexPrintf( "More than %i positions may be slow \n", MAX_IND_READ);
} else {
cudaMemcpyToSymbol(gC_ind_read, p_ind_ok, Npos*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
checkLastError("after cudaMemcpyToSymbol pos");
}
checkLastError("after cudaMemcpyToSymbol");
//============= run the kernel ======================
if (Npos < MAX_IND_READ)
readFromArray_c_fast<<<dimBlock, dimThread>>>(p_obj_proj,p_object, Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos);
else
readFromArray_c<<<dimBlock, dimThread>>>(p_obj_proj,p_object,p_ind_ok, p_positions_x,p_positions_y,Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos);
checkLastError("after kernel");
mxGPUDestroyGPUArray(m_object);
mxGPUDestroyGPUArray(m_ind_ok);
}
cudaThreadSynchronize();
// plhs[0] = mxGPUCreateMxArrayOnGPU(m_obj_proj);
mxGPUDestroyGPUArray(m_obj_proj);
mxGPUDestroyGPUArray(m_positions_x);
mxGPUDestroyGPUArray(m_positions_y);
return;
}
@@ -0,0 +1,193 @@
/*
*
*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 cSAXS
ptychography MATLAB package 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
LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
(doi: 10.1126/science.1158573),
for mixed coherent modes:
P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
for LSQ-ML method
M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
for OPRP method
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." Optics express 24.8 (2016): 8360-8369
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, 2908929108 (2016).
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.
*
*
Compilation from Matlab:
maybe a tiny bit faster code is generated by
mex -O COPTIMFLAGS='-O2' LDOPTIMFLAGS='-O2' set_views_cpu_mex.c
Usage from Matlab:
set_views_cpu_mex(probe,object,positions, Npos);
This code in matlab:
asize = size(probe);
for i=1:Npos
Indy = positions(i,1) + (1:asize(1));
Indx = positions(i,2) + (1:asize(2));
ob(Indy,Indx) = ob(Indy,Indx) + probe;
end
*/
#include "mex.h"
#include <math.h>
#include <stdio.h>
#include <omp.h>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int i;
/* Check for proper number of arguments. */
if (nrhs != 4)
mexErrMsgTxt("Four input arguments required: set_views_cpu_mex(probe,object,positions,Npos)");
else if (nlhs != 0)
mexErrMsgTxt("No output argument has to be specified.");
/* Input must be of type single. */
for (i=0; i < 2; i++) {
if (mxIsSingle(prhs[i]) != 1){
printf("Input %d is not single\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* Input must be of type int32. */
for (i=2; i<nrhs; i++){
if (mxIsInt32(prhs[i]) != 1){
printf("Input %d is not integer\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* It cannot be one-dimensional */
if(mxGetNumberOfDimensions(prhs[0]) < 2) {
printf("The 1st input argument must have at least two 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 */
// if(mxIsComplex(prhs[0]) != 1) {
// printf("object input argument must be complex-valued.");
// mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
// }
// if(mxIsComplex(prhs[1]) != 1) {
// printf("probe input argument must be complex-valued.");
// mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
// }
float *object_r, *object_i, *probe_r,*probe_i;
const int *positions, *ind_ok;
bool cprobe, cobject;
cobject = mxIsComplex(prhs[0]);
cprobe = mxIsComplex(prhs[1]);
if( cobject != cobject)
{
printf("probe/object input argument must be complex/real-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected both complex / real arrays");
}
ind_ok = (int*)mxGetData(prhs[3]);
positions = (int*)mxGetData(prhs[2]);
object_r = (float*)mxGetData(prhs[0]);
probe_r = (float*)mxGetData(prhs[1]);
if(cprobe)
{
/* get pointers to input data */
object_i = (float*)mxGetImagData(prhs[0]);
probe_i = (float*)mxGetImagData(prhs[1]);
}
/* Get dimension of probe and object */
const mwSize Ndims = mxGetNumberOfDimensions(prhs[1]);
const mwSize * dims = mxGetDimensions(prhs[1]);
const mwSize No_y = mxGetM(prhs[0]);
const mwSize No_x = mxGetN(prhs[0]);
const mwSize Np_y = dims[0];
const mwSize Np_x = dims[1];
const mwSize Npos = mxGetM(prhs[2]);
if((mxGetM(prhs[2]) != dims[2]) && (Ndims == 3)) {
printf("wrong size of update / positions %i", Ndims);
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
}
mwSize id_small, id_large, pos, col, row, o, p;
bool flat_probe = Ndims == 2;
#pragma omp parallel for private(p,pos, col, row, id_small, id_large)
for (p=0;p<Npos;p++){
pos = ind_ok[p]-1;
for (col=0;col<Np_x;col++) {
for (row=0;row<Np_y;row++) {
if(flat_probe)
id_small = row + col*Np_y;
else
id_small = row + col*Np_y + Np_y*Np_x*pos;
id_large = row + positions[pos] + (col+positions[pos+Npos])*No_y;
#pragma omp atomic
object_r[id_large] += probe_r[id_small];
if(cprobe)
#pragma omp atomic
object_i[id_large] += probe_i[id_small];
}
}
}
return;
}
@@ -0,0 +1,182 @@
/*
*
*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 cSAXS
ptychography MATLAB package 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
LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
(doi: 10.1126/science.1158573),
for mixed coherent modes:
P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
for LSQ-ML method
M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
for OPRP method
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." Optics express 24.8 (2016): 8360-8369
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, 2908929108 (2016).
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.
*
*
Compilation from Matlab:
maybe a tiny bit faster code is generated by
mex -O COPTIMFLAGS='-O2' LDOPTIMFLAGS='-O2' set_views_cpu_mex_rc.cpp
Usage from Matlab:
set_views_cpu_mex(probe,object,positions, Npos);
This code in matlab:
asize = size(probe);
for i=1:Npos
Indy = positions(i,1) + (1:asize(1));
Indx = positions(i,2) + (1:asize(2));
ob(Indy,Indx) = ob(Indy,Indx) + probe;
end
*/
#include "mex.h"
#include <math.h>
#include <stdio.h>
#include <omp.h>
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
int i;
/* Check for proper number of arguments. */
if (nrhs != 6)
mexErrMsgTxt("Six input arguments required: set_views_cpu_mex_rc(object,obj_upd, illum, illum_upd, positions,ind_ok)");
else if (nlhs != 0)
mexErrMsgTxt("No output argument has to be specified.");
/* Input must be of type single. */
for (i=0; i < 4; i++) {
if (mxIsSingle(prhs[i]) != 1){
printf("Input %d is not single\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* Input must be of type int32. */
for (i=4; i<nrhs; i++){
if (mxIsInt32(prhs[i]) != 1){
printf("Input %d is not integer\n",i+1);
mexErrMsgIdAndTxt("MexError:ptycho","Inputs must be of correct type.");
}
}
/* Check that arrays are complex */
if(mxIsComplex(prhs[0]) != 1) {
printf("first input argument must be complex-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
}
if(mxIsComplex(prhs[1]) == 1) {
printf("second input argument must be real-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected real arrays");
}
if(mxIsComplex(prhs[2]) != 1) {
printf("third input argument must be complex-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected complex arrays");
}
if(mxIsComplex(prhs[3]) == 1) {
printf("fourth input argument must be real-valued.");
mexErrMsgIdAndTxt("MexError:ptycho","Expected real arrays");
}
float *object_r, *object_i, *probe_r,*probe_i, *illum_r, *illum_upd;
const int *positions, *ind_ok;
bool cprobe, cobject;
positions = (int*)mxGetData(prhs[4]);
ind_ok = (int*)mxGetData(prhs[5]);
object_r = (float*)mxGetData(prhs[0]);
object_i = (float*)mxGetImagData(prhs[0]);
illum_r = (float*)mxGetData(prhs[1]);
probe_r = (float*)mxGetData(prhs[2]);
probe_i = (float*)mxGetImagData(prhs[2]);
illum_upd = (float*)mxGetData(prhs[3]);
/* Get dimension of probe and object */
const mwSize Ndims = mxGetNumberOfDimensions(prhs[2]);
const mwSize * dims = mxGetDimensions(prhs[2]);
const mwSize No_y = mxGetM(prhs[0]);
const mwSize No_x = mxGetN(prhs[0]);
const mwSize Np_y = dims[0];
const mwSize Np_x = dims[1];
const mwSize Npos = mxGetNumberOfElements(prhs[5]);
if(mxGetM(prhs[4]) != dims[2]) {
printf("wrong size of update / positions %i", Ndims);
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
}
// mexPrintf("O %i %i P %i %i N %i \n ", No_y, No_x, Np_y, Np_x, Npos);
mwSize id_small, id_small_3D, id_large, pos, col, row, p;
#pragma omp parallel for private(p,pos, col, row, id_small,id_small_3D, id_large)
for (p=0;p<Npos;p++){
pos = ind_ok[p]-1;
for (col=0;col<Np_x;col++) {
for (row=0;row<Np_y;row++) {
id_small = row + col*Np_y;
id_small_3D = id_small + Np_y*Np_x*pos;
id_large = row + positions[pos] + (col+positions[pos+Npos])*No_y;
#pragma omp atomic
object_r[id_large] += probe_r[id_small_3D];
#pragma omp atomic
object_i[id_large] += probe_i[id_small_3D];
#pragma omp atomic
illum_r[id_large] += illum_upd[id_small];
}
}
}
return;
}
@@ -0,0 +1,303 @@
/*
Set complex views to complex object
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_read[MAX_IND_READ_DEV];
__constant__ uint16_T gC_pos_X[MAX_IND_READ_DEV];
__constant__ uint16_T gC_pos_Y[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
*/
/*********** reduction of object projection array to single object ***************/
template <bool useGlobal>
__global__ void addToArray_r( float const * sarray, float * larray, cuint16* ind_read, cuint16* pos_X, cuint16* posY,
cuint Np_px,cuint Np_py, cuint Np_pz,cuint Np_ox, cuint Np_oy,
cuint Npos, const bool isFlat) {
// Location in a 3D matrix
int idx= blockIdx.x * blockDim.x + threadIdx.x;
int idy= blockIdx.y * blockDim.y + threadIdx.y;
if ( idx < Np_px & idy < Np_py ) {
int idz, id_large;
float sarray_val;
for(int id = 0; id < Npos; id++)
{
if (useGlobal) {
// fast const memory based version
idz = gC_ind_read[id]-1; // go only through some of the indices
id_large = gC_pos_X[idz]+idx + Np_ox*(gC_pos_Y[idz]+idy);
} else {
// slower global memory based version
idz = ind_read[id]-1; // go only through some of the indices
id_large = pos_X[idz]+idx + Np_ox*(posY[idz]+idy);
}
int id_small = idx + Np_px*idy ;
if (!isFlat)
id_small = id_small + Np_px*Np_py*idz ;
// prevent extra memory load
sarray_val = (isFlat && (idz > 0)) ? sarray_val: sarray[ id_small ];
//larray[id_large] += sarray_val;
//__syncthreads();
// slowest step, without atomicAdd it misses some values
atomicAdd(&larray[id_large] ,sarray_val);
}
}
}
template <bool useGlobal>
__global__ void addToArray_c( float2 const * sarray, float2 * larray, cuint16* ind_read, cuint16* pos_X, cuint16* posY,
cuint Np_px,cuint Np_py, cuint Np_pz,cuint Np_ox, cuint Np_oy,
cuint Npos, const bool isFlat) {
// Location in a 3D matrix
int idx= blockIdx.x * blockDim.x + threadIdx.x;
int idy= blockIdx.y * blockDim.y + threadIdx.y;
if ( idx < Np_px & idy < Np_py ) {
int idz, id_large;
float2 sarray_val;
for(int id = 0; id < Npos; id++)
{
if (useGlobal) {
// fast const memory based version
idz = gC_ind_read[id]-1; // go only through some of the indices
id_large = gC_pos_X[idz]+idx + Np_ox*(gC_pos_Y[idz]+idy);
} else {
// slower global memory based version
idz = ind_read[id]-1; // go only through some of the indices
id_large = pos_X[idz]+idx + Np_ox*(posY[idz]+idy);
}
int id_small = idx + Np_px*idy ;
if (!isFlat)
id_small = id_small + Np_px*Np_py*idz ;
// prevent extra memory load
sarray_val = (isFlat && (idz > 0)) ? sarray_val: sarray[ id_small ];
//larray[id_large].x += sarray_val.x;
//larray[id_large].y += sarray_val.y;
//__syncthreads();
// slowest step, without atomicAdd it misses some values
atomicAdd(&larray[id_large].x ,sarray_val.x);
atomicAdd(&larray[id_large].y ,sarray_val.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 != 5)
mexErrMsgTxt("Five input arguments required");
// Input must be of type single.
for (i=0; i < 2; i++) {
if ( !mxIsGPUArray(prhs[i]) && !mxIsCell(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<4; 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]) < 2) {
printf("The 1st input argument must have at least two 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_obj_proj = mxGPUCreateFromMxArray(prhs[0]);
if ((mxGPUGetClassID(m_obj_proj) != mxSINGLE_CLASS)) {
mexPrintf("m_obj_proj\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const mxGPUArray * m_positions_x = mxGPUCreateFromMxArray(prhs[2]);
if ((mxGPUGetClassID(m_positions_x) != mxUINT16_CLASS)) {
mexPrintf("m_positions_x\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const uint16_T * p_positions_x = (uint16_T *)mxGPUGetDataReadOnly(m_positions_x);
const mxGPUArray * m_positions_y = mxGPUCreateFromMxArray(prhs[3]);
if ((mxGPUGetClassID(m_positions_y) != mxUINT16_CLASS)) {
mexPrintf("m_positions_y\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const uint16_T * p_positions_y = (uint16_T *)mxGPUGetDataReadOnly(m_positions_y);
if (!mxIsCell(prhs[1]) || !mxIsCell(prhs[4]))
mexErrMsgIdAndTxt("MexError:ptycho","Object and indices has to be in cell/cells !! ");
if (mxGetNumberOfElements(prhs[1]) != mxGetNumberOfElements(prhs[4]))
mexErrMsgIdAndTxt("MexError:ptycho","Number of objects != number of indices ");
cuint Ncells = mxGetNumberOfElements(prhs[1]);
const unsigned int Np_pp = mxGPUGetNumberOfElements(m_positions_y);
if (Np_pp < MAX_IND_READ) {
cudaMemcpyToSymbol(gC_pos_X, p_positions_x, Np_pp*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
cudaMemcpyToSymbol(gC_pos_Y, p_positions_y, Np_pp*sizeof(uint16_T), 0, cudaMemcpyHostToDevice);
checkLastError("after cudaMemcpyToSymbol pos");
}
for (int l=0; l<Ncells; l++)
{
// read the cell content
mxArray * mx_object = mxGetCell(prhs[1],l);
mxArray * mx_ind = mxGetCell(prhs[4],l);
int N_ok = mxGetNumberOfElements(mx_ind);
if(N_ok == 0)
continue;
mxGPUArray * m_object = const_cast<mxGPUArray*>(mxGPUCreateFromMxArray(mx_object)); // mxGPUCopyFromMxArray(prhs[1]);
if ((mxGPUGetClassID(m_object) != mxSINGLE_CLASS)) {
mexPrintf("m_object\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
if (mxGPUGetComplexity(m_object) != mxGPUGetComplexity(m_obj_proj)) {
mexPrintf("m_object and m_obj_proj complexity has to be the same \n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const mxGPUArray * m_ind_ok = mxGPUCreateFromMxArray(mx_ind);
if ((mxGPUGetClassID(m_ind_ok) != mxUINT16_CLASS)) {
mexPrintf("m_ind_ok\n");
mexErrMsgIdAndTxt(errId, errMsg);
}
const uint16_T * p_ind_ok = (uint16_T *)mxGPUGetDataReadOnly(m_ind_ok);
// Get dimension of probe and object
const unsigned int Ndims = (unsigned int)mxGPUGetNumberOfDimensions(m_obj_proj);
const mwSize * Np_o = mxGPUGetDimensions(m_object);
const mwSize * Np_p = mxGPUGetDimensions(m_obj_proj);
const unsigned int Npos = mxGPUGetNumberOfElements(m_ind_ok);
//mexPrintf("Ndims %i Np_o %i %i Np_p %i %i %i Npos %i \n " ,Ndims,Np_o[0],Np_o[1],Np_p[0],Np_p[1],Np_p[3],Npos);
if (Ndims == 3 && Npos > Np_p[2]) {
printf("wrong size of update / positions %i", Ndims);
mexErrMsgIdAndTxt("MexError:ptycho","wrong size of update / positions");
}
if (Npos > MAX_IND_READ) {
//mexPrintf( "More than %i positions may be slow \n", MAX_IND_READ);
} else {
cudaMemcpyToSymbol(gC_ind_read, p_ind_ok, 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_p[0] + threadsPerBlockEachDim - 1) / threadsPerBlockEachDim;
int const blocksPerGrid_N = (Np_p[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");
const bool isFlat = (Ndims == 2);
const bool isComplex = mxGPUGetComplexity(m_obj_proj) == mxCOMPLEX;
// ================== call the right kernel ===================
if (isComplex) {
const float2 * p_obj_proj = (float2 *)mxGPUGetDataReadOnly(m_obj_proj);
float2 * p_object = (float2 *)mxGPUGetData(m_object);
if (Np_pp < MAX_IND_READ)
addToArray_c<true><<<dimBlock, dimThread>>>(p_obj_proj,p_object, p_ind_ok, p_positions_x,p_positions_y ,Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos, isFlat);
else
addToArray_c<false><<<dimBlock, dimThread>>>(p_obj_proj,p_object, p_ind_ok, p_positions_x,p_positions_y ,Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos, isFlat);
} else {
const float * p_obj_proj = (float *)mxGPUGetDataReadOnly(m_obj_proj);
float * p_object = (float *)mxGPUGetData(m_object);
if (Np_pp < MAX_IND_READ)
addToArray_r<true><<<dimBlock, dimThread>>>(p_obj_proj,p_object, p_ind_ok, p_positions_x,p_positions_y ,Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos, isFlat);
else
addToArray_r<false><<<dimBlock, dimThread>>>(p_obj_proj,p_object, p_ind_ok, p_positions_x,p_positions_y ,Np_p[0],Np_p[1],Np_p[2],Np_o[0],Np_o[1], Npos, isFlat);
}
checkLastError("after kernel");
mxGPUDestroyGPUArray(m_object);
mxGPUDestroyGPUArray(m_ind_ok);
}
cudaThreadSynchronize();
// plhs[0] = mxGPUCreateMxArrayOnGPU(m_object);
mxGPUDestroyGPUArray(m_obj_proj);
mxGPUDestroyGPUArray(m_positions_x);
mxGPUDestroyGPUArray(m_positions_y);
return;
}
+193
View File
@@ -0,0 +1,193 @@
% SET_VIEWS Reduce stack of projections into one shared object
%
% object = set_views(object, object_update, layer,object_id, indices, cache, scan_ids, skip_ind)
%
% ** obj_update [Nx_o, Ny_o] array or cells containing object
% ** obj_proj [Nx_p, Ny_p, N] preallocated array for the views
% ** layer_ids id of the solved layer for multilayer ptycho
% ** object_id id of the object, ie scan or incoherent mode
% ** indices processed positions
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** scan_ids determines to which scan correponds each of the position
% ** skip_ind list of indices to be skipped
%
% returns:
% ++ object reduced sum of the views
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 = set_views(object, object_update, layer,object_id, indices, cache, scan_ids, skip_ind)
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import utils.verbose
global use_gpu
if nargin < 8
skip_ind = [];
end
if nargin > 6
%% !!! call recursivelly -> wrapper for multiscan version !!!!
% get unique IDs of the scans
if isempty(scan_ids)
unq_scans = [];
elseif all(scan_ids == scan_ids(1))
unq_scans = scan_ids(1);
else
unq_scans = unique(scan_ids);
end
if length(unq_scans)> 1
if ~isempty(use_gpu) && use_gpu && isa(object{1}, 'gpuArray') && isa(object_update, 'gpuArray')
if size(object,1) == 1
% shared object or single object
ind_ok{1} = uint16(1:length(indices));
else
for kk = 1:size(object,1)
ind_ok{kk} = uint16(find(scan_ids == kk));
end
end
% feed data directly to the GPU mex without splitting
object(:,layer) = set_views_gpu(object(:,layer),object_update,cache.oROI_s{object_id(1)},indices, ind_ok);
else
% ONLY if GPU not available use this "wrapper" around single
% set_projection function
for kk = unq_scans
ind = scan_ids == kk;
skip_ind = indices(~ind); % avoid going through these indices
object{kk,layer} = set_views(object{kk,layer},object_update, object_id, indices, cache,[],skip_ind);
end
end
return
end
end
is_cell = iscell(object);
if is_cell
object_0 = object;
object = object_0{min(end,object_id), layer};
end
if ~isfield(cache,'skip_ind'); cache.skip_ind = []; end
if ~isempty(cache.skip_ind) && ~isempty(skip_ind)
ind_ok = uint16(find(~ismember(indices, [cache.skip_ind,skip_ind]))); % skip wrong patterns
else
ind_ok = uint16(1:length(indices));
end
if isa(object, 'gpuArray')
%% USE CUDA MEX FOR GPU
object = set_views_gpu(object,object_update,cache.oROI_s{min(object_id,end)},indices,ind_ok);
else
%% USE CPU
positions = int32([cache.oROI_s{min(end,object_id)}{1}(indices,1), cache.oROI_s{min(end,object_id)}{2}(indices,1)]);
object = utils.add_to_3D_projection(object_update,object, positions, ind_ok,true);
end
if is_cell
object_0{min(object_id,end), layer}= object;
object = object_0;
end
end
function object = set_views_gpu(object,proj,oROI,ind, ind_ok)
% Description: Set projections to the object function using GPU
% mexcuda -v -output +engines/+GPU/set_views_gpu_mex +engines/+GPU/set_views_gpu_mex.cu
import engines.GPU_MS.GPU_wrapper.*
import utils.verbose
x = uint16(oROI{1}(ind,1));
y = uint16(oROI{2}(ind,1));
return_cell = iscell(object);
if ~iscell(object); object = {Garray(object)}; end
if ~iscell(ind_ok); ind_ok = {Garray(uint16(ind_ok))}; end
for i = 1:length(object)
assert(~isempty(object{i}), 'Input object cannot be empty')
end
for i = 1:length(ind_ok)
assert(~isempty(ind_ok{i}), 'Vector ind_ok cannot be empty')
end
try
set_views_gpu_mex( proj, object,x,y,ind_ok);
catch err
verbose(0, 'Recompilation of MEX functions ... ')
if any(strcmp(err.identifier, { 'MATLAB:UndefinedFunction','MATLAB:mex:ErrInvalidMEXFile'}))
path = replace(mfilename('fullpath'), mfilename, '');
mexcuda('-output', [path,'private/set_views_gpu_mex'], [path, 'private/set_views_gpu_mex.cu'])
set_views_gpu_mex( proj, object,x,y,ind_ok);
else
rethrow(err)
end
end
if ~return_cell
object = object{1};
end
end
@@ -0,0 +1,189 @@
% SET_VIEWS_RC Reduce stack of projections into one shared object
% ! update complex and real object at once
%
% [obj_update,obj_illum] = set_views_rc(obj_update,obj_illum, psi,aprobe2,layer,object_id, indices, cache, scan_ids, skip_ind, object_modes)
%
% ** obj_update [Nx_o, Ny_o] array or cells containing object
% ** obj_illum [Nx_o, Ny_o] array or cells containing illumination sum
% ** obj_proj [Nx_p, Ny_p, N] preallocated array for the views
% ** psi complex valued exit wave (psi ~ P*O)
% ** aprobe2 illumination intensity patch
% ** layer_ids id of the solved layer for multilayer ptycho
% ** object_id id of the object, ie scan or incoherent mode
% ** indices processed positions
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** scan_ids determines to which scan correponds each of the position
% ** skip_ind list of indices to be skipped
% ** object_modes number of incoherent object modes
%
% returns:
% ++ object reduced sum of the views
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [obj_update,obj_illum] = set_views_rc(obj_update,obj_illum, psi,aprobe2,layer,object_id, indices, cache, scan_ids, skip_ind)
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import utils.verbose
global use_gpu
if nargin < 10
skip_ind = [];
end
if nargin > 8 && ~isempty(scan_ids)
%% !!! call recursivelly -> wrapper for multiscan version !!!!
% get unique IDs of the scans
if isempty(scan_ids)
unq_scans = [];
elseif all(scan_ids == scan_ids(1))
unq_scans = scan_ids(1);
else
unq_scans = unique(scan_ids);
end
if length(unq_scans)> 1 || (iscell(obj_update) && length(obj_update) > 1)
if ~isempty(use_gpu) && use_gpu && isa(obj_update{1}, 'gpuArray') && isa(psi, 'gpuArray')
if size(obj_update,1) == 1
% shared object or single object
ind_ok{1} = uint16(1:length(indices));
else
for kk = 1:size(obj_update,1)
ind_ok{kk} = uint16(find(scan_ids == kk));
end
end
% feed data directly to the GPU mex without splitting
[obj_update(:,layer),obj_illum(:,layer)] = set_views_gpu_rc(obj_update(:,layer),obj_illum(:,layer),psi, aprobe2,cache.oROI_s{min(object_id,end)},indices, ind_ok );
else
% if GPU not available use this "wrapper" around single
% set_projection function
for kk = unq_scans
ind = scan_ids == kk;
skip_ind = indices(~ind); % avoid going through these indices
[obj_update{kk,layer},obj_illum{kk,layer}] = set_views_rc(obj_update{kk,layer},obj_illum{kk,layer},psi,aprobe2, layer, object_id, indices, cache,[],skip_ind);
end
end
return
end
end
is_cell = iscell(obj_update);
if is_cell
obj_update_0 = obj_update;
obj_illum_0 = obj_illum;
obj_update = obj_update{object_id, min(end,layer)};
obj_illum = obj_illum{object_id, min(end,layer)};
end
if ~isempty(cache.skip_ind) && ~isempty(skip_ind)
ind_ok = uint16(find(~ismember(indices, [cache.skip_ind,skip_ind]))); % skip wrong patterns
else
ind_ok = uint16(1:length(indices));
end
if ~isempty(use_gpu) && use_gpu && isa(obj_update, 'gpuArray') && isa(psi, 'gpuArray')
%% USE CUDA MEX FOR GPU
[obj_update,obj_illum] = set_views_gpu_rc(obj_update,obj_illum, psi,aprobe2,cache.oROI_s{min(object_id,end)},indices, ind_ok);
else
%% USE CPU
obj_illum = set_views(obj_illum , aprobe2, layer, object_id,indices, cache,scan_ids, skip_ind);
obj_update = set_views(complex(obj_update),complex(psi), layer, object_id, indices, cache,scan_ids, skip_ind);
end
if is_cell
obj_update_0{object_id,min(end,layer)} = obj_update;
obj_illum_0{object_id,min(end,layer)} = obj_illum;
obj_update = obj_update_0;
obj_illum = obj_illum_0;
end
end
function [object_c,object_r] = set_views_gpu_rc(object_c,object_r,proj_c, proj_r,oROI,ind, ind_ok )
% Description: update complex and real object at once
import engines.GPU_MS.GPU_wrapper.*
x = uint16(oROI{1}(ind,1));
y = uint16(oROI{2}(ind,1));
return_cell = iscell(object_c);
if ~iscell(object_c); object_c = {Garray(object_c)}; end
if ~iscell(object_r); object_r = {Garray(object_r)}; end
if ~iscell(ind_ok); ind_ok = {Garray(uint16(ind_ok))}; end
proj_c = complex(proj_c);
set_views_gpu_mex( proj_c, object_c,x,y, ind_ok );
set_views_gpu_mex( proj_r, object_r,x,y, ind_ok );
if ~return_cell
object_c = object_c{1};
object_r = object_r{1};
end
end
@@ -0,0 +1,109 @@
% APPLY_SUBPX_SHIFT subpixel probe shifting , equivalet just a bit faster than imshift_fft
%
% img = apply_subpx_shift(img, shift)
%
% ** img stack images
% ** shift (N,2) array of shifts
% returns
% ++ img shifted imaeg stack
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (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 mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (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, 2908929108 (2016).
% (doi: 10.1364/OE.24.029089).
% 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 img = apply_subpx_shift(img, shift)
% subpixel probe shifting
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
persistent xgrid ygrid
if nargin == 0
xgrid = [];
return
end
if all(shift(:) == 0); return ; end
global use_gpu
shift = single(shift);
x = reshape(shift(:,1),1,1,[]);
y = reshape(shift(:,2),1,1,[]);
Np = size(img);
if size(img,3) ~= size(shift,1) && use_gpu
% ugly trick making matlab GPU FFT faster
img = repmat(img,1,1,size(shift,1));
end
img = fft2_safe(img);
if isempty(xgrid)
% matlab seems to need quite a lot of time to generate it -> preallocate
xgrid = Garray(fftshift((0:Np(2)-1)'/Np(2))-0.5)';
ygrid = Garray(fftshift((0:Np(1)-1)'/Np(1))-0.5);
end
if use_gpu
img = Gfun(@apply_shift_Gfun,img,x,y,xgrid, ygrid);
else
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times, x,xgrid)));
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,y,ygrid)));
end
img = ifft2_safe(img);
end
function img = apply_shift_Gfun(img,x,y,xgrid, ygrid)
img = img .* exp((-2i*pi)*(x*xgrid+y*ygrid));
end
@@ -0,0 +1,98 @@
% SHIFT_PROBE_FFT fast shifting in fourier space (ie apply phase ramp )
%
% img = shift_probe_fft(img, shift)
%
% ** img stack images
% ** shift (N,2) array of shifts
% returns
% ++ img shifted image stack
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (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 mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (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, 2908929108 (2016).
% (doi: 10.1364/OE.24.029089).
% 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 img = shift_probe_fft(img, shift)
import engines.GPU_MS.GPU_wrapper.*
if all(shift(:) == 0); return ; end
global use_gpu
persistent grid
shift = Garray(shift);
x = reshape(shift(:,1),1,1,[]);
y = reshape(shift(:,2),1,1,[]);
Np = size(img);
if Np(1) ~= Np(2); error('Not implemented'); end
grid = Garray((0:Np(1)-1)'/Np(1)-0.5);
if isempty(grid)
grid = Garray((0:Np(1)-1)'/Np(1)-0.5);
end
if use_gpu
img = Gfun(@apply_shift_Gfun,img,x,y,grid', grid);
else
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,x,grid')));
img = bsxfun(@times, img, exp((-2i*pi)*bsxfun(@times,y,grid)));
end
end
function img = apply_shift_Gfun(img,x,y,xgrid, ygrid)
img = img .* exp((-2i*pi)*(x*xgrid+y*ygrid));
end
+298
View File
@@ -0,0 +1,298 @@
%DM Difference-Map algorithm
%
% [self, cache, psi_dash, fourier_error ] = DM(self,par,cache,psi_dash,fourier_error, iter)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** psi_dash complex projection from previous iteration, or emppty in first iteration
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
% ** iter current iteration number
%
% returns:
% ++ self self-like structure with final reconstruction
% ++ cache structure with precalculated values to avoid unnecessary overhead
% ++ psi_dash complex projection from previous iteration, or emppty in first iteration
% ++ fourier_error array [Npos,1] containing evolution of reconstruction error
%
% Publications most relevant to the Difference-Map implementation
% + P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer,
% "High-Resolution Scanning X-ray Diffraction Microscopy," Science 321, 379-382 (2008)
% + P. Thibault, M. Dierolf, O. Bunk, A. Menzel, F. Pfeiffer,
% "Probe retrieval in ptychographic coherent diffractive imaging,"
% Ultramicroscopy 109, 338343 (2009)
function [self, cache, psi_dash, fourier_error ] = DM(self,par,cache,psi_dash,fourier_error, iter)
import utils.*
import math.*
import engines.GPU_MS.shared.*
import engines.GPU_MS.GPU_wrapper.*
psi = cell(par.Nmodes, 1);
Psi = cell(par.Nmodes, 1);
%%%%%%%%%%%%%%%%%%%%%%%%% Difference maps algorithm %%%%%%%%%%%%%%%%%%%
beta = 1;
gamma = 1;
relax_mask = 1; % smoothly change relaxation of the mask
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
probe_norm = norm2(self.probe{1});
object_modes = length(self.object);
probe_amp_corr = [0,0];
for ll = 1:par.object_modes
obj_proj{ll} = Gzeros([self.Np_p, 0], true);
end
for ll = 1:object_modes
obj_illum{ll,1} = Gzeros(self.Np_o);
obj_update{ll,1} = Gzeros(self.Np_o, true);
end
for ll = 1:par.probe_modes
probe_illum{ll} = Gzeros(size(self.probe{ll}));
probe_update{ll} = Gzeros(size(self.probe{ll}), true);
end
% use precalculated blocks to be solved in parallel
indices = cache.preloaded_indices_simple{1}.indices;
scan_ids = cache.preloaded_indices_simple{1}.scan_ids;
Nind = length(indices);
p_ind = cell(max(par.probe_modes, par.object_modes),1);
for jj = 1:Nind
g_ind = indices{jj};
for ll = 1:max(par.probe_modes, par.object_modes)
if par.share_probe || ll > 1 % share incoherent modes
p_ind{ll} = 1;
else
if all(scan_ids{jj} == scan_ids{jj}(1))
p_ind{ll} = scan_ids{jj}(1);
else
p_ind{ll} = scan_ids{jj};
end
end
end
% load on GPU if needed
if ~par.keep_on_gpu
for ll = 1:max(par.probe_modes, par.object_modes)
try; psi_dash{ll,jj} = Garray(psi_dash{ll,jj}); end
end
end
%% fourier propagation
for ll = 1:par.object_modes
obj_proj{ll} = get_views(self.object, obj_proj{ll},1,ll, g_ind, cache, scan_ids{jj},[]);
end
for ll = 1:max(par.probe_modes, par.object_modes)
probe{ll} = self.probe{min(ll,end)}(:,:,p_ind{ll});
psi{ll} = bsxfun(@times, obj_proj{min(ll, end)}, probe{ll});
if isempty(psi_dash{ll,jj}); psi_dash{ll,jj} = psi{ll}; end % initial guess
% P_M (P_0(psi))
Psi{ll} = Gfun(@DM_update_psi, gamma,psi{ll}, psi_dash{ll,jj} );
Psi{ll} = fwd_fourier_proj(Psi{ll}, self.modes{min(ll,end)} );
end
%% load data to GPU (if not loaded yet)
modF = get_modulus(self, cache, g_ind,true,jj);
mask = get_mask(self, cache.mask_indices, g_ind);
% get intensity (modulus) on detector including different corrections
aPsi = get_reciprocal_model(self, Psi, modF, mask,iter, g_ind, par,cache);
if iter > 0 && (par.get_error && (mod(iter,min(20, 2^(floor(2+iter/50)))) == 0 || iter < 20) || iter == par.number_iterations )
[fourier_error(iter,g_ind)] = get_fourier_error(modF, aPsi, [],mask);
end
if ~isempty(mask)
mask = single(1)-mask;
mask = relax_mask + (min(relax_mask, par.pfft_relaxation) - relax_mask ) * mask;
else
mask = par.pfft_relaxation;
end
if iter == 0
% in the first iteration only find optimal scale for the probe
probe_amp_corr(1) = probe_amp_corr(1) + Ggather(sum(modF(:).^2));
probe_amp_corr(2) = probe_amp_corr(2) + Ggather(sum(aPsi(:).^2));
for ll = 1:size(psi_dash,2); psi_dash{ll,jj} = []; end
continue
end
%% fourier (modulus) contraint
Psi = modulus_constraint(modF,aPsi,Psi, mask, [], par, 0 );
aPsi = []; modF = []; mask = [];
for ll = 1:max(par.probe_modes, par.object_modes)
Psi{ll} = back_fourier_proj(Psi{ll}, self.modes{min(end,ll)});
psi_dash{ll,jj} = Gfun(@DM_update, psi_dash{ll,jj} ,beta, Psi{ll},psi{ll});
end
Psi = [];
% get back from GPU if required
if ~par.keep_on_gpu
for ll = 1:max(par.probe_modes, par.object_modes)
psi_dash{ll,jj} = Ggather(psi_dash{ll,jj});
end
end
end
if iter == 0
% apply initial correction for the probe intensity and return
probe_amp_corr = sqrt(probe_amp_corr(1) / probe_amp_corr(2)); %% calculate ratio between modF^2 and aPsi^2
for ii = 1:par.probe_modes
self.probe{ii} = self.probe{ii}*probe_amp_corr;
end
psi_dash = cell(size(psi_dash));
verbose(2,'Probe amplitude corrected by %.3g',probe_amp_corr)
return
end
%% iterative solver of the overlap constraint, important for initial convergence
for kk = 1:10
for ll = 1:object_modes
obj_illum{ll}(:) = 0;
obj_update{ll}(:) = 0i;
end
for ll = 1:par.probe_modes
probe_illum{ll}(:) = 0;
probe_update{ll}(:) = 0i;
end
probe_0 = self.probe;
%% obj_update , obj_illum,probe_update,probe_illum is not reset to make it more stable
for jj = 1:length(indices)
for ll = 1:max(par.probe_modes, par.object_modes)
if par.share_probe || ll > 1 % share incoherent modes
p_ind{ll} = 1;
else
if all(scan_ids{jj} == scan_ids{jj}(1))
p_ind{ll} = scan_ids{jj}(1);
else
p_ind{ll} = scan_ids{jj};
end
end
end
for ll = 1:par.probe_modes
probe{ll} = self.probe{ll}(:,:,p_ind{ll});
cprobe{ll} = conj(probe{ll});
aprobe{ll} = real(probe{ll}.*cprobe{ll});
end
% move to GPU (if not there yet)
for ll = 1:max(par.probe_modes, par.object_modes)
psi_dash{ll,jj} = Garray(psi_dash{ll,jj});
end
g_ind = indices{jj};
for ll = 1:par.object_modes
obj_proj{ll} = get_views(self.object, obj_proj{ll},1,ll, g_ind, cache, scan_ids{jj},[]);
end
if iter >= par.probe_change_start
for ll = 1:par.probe_modes
%% update probe
[probe_update{ll},probe_illum{ll}] = QQ_probe(psi_dash{ll,jj}, obj_proj{min(end,ll)}, probe_update{ll},probe_illum{ll}, p_ind{ll});
end
end
if iter >= par.object_change_start
%% update object
[obj_update,obj_illum] = QQ_object(psi_dash{ll,jj}, obj_update,obj_illum, aprobe{min(ll,end)}, cprobe{min(ll,end)}, g_ind,min(ll, object_modes),scan_ids{jj},cache);
end
% get back from GPU if required
if ~par.keep_on_gpu
for ll = 1:max(par.probe_modes, par.object_modes)
psi_dash{ll,jj} = Ggather(psi_dash{ll,jj});
end
end
end
for ll = 1:max([par.probe_modes, object_modes])
if iter >= par.probe_change_start && ll <= par.probe_modes
% add some inertia to prevent oscilations
self.probe{ll} = update_probe(self, self.probe{ll} , probe_update{ll} , probe_illum{ll} , par, ll);
end
if iter >= par.object_change_start && ll <= object_modes
self.object{ll} = Gfun(@update_object, self.object{ll}, obj_update{ll}, obj_illum{ll}, cache.MAX_ILLUM(ll)*1e-4, par.probe_inertia);
end
end
if iter > par.probe_change_start
% if both object and probe are recontructed, solve the
% realspace constraint iterativelly
min_iter = 1 + par.keep_on_gpu; % if par.keep_on_gpu==true, then the code is so slow that it is not worthy skip the norm calculation
min_change = 0.01;
if kk >= min_iter || verbose >= 2
dprobe = max(norm2(self.probe{1} - probe_0{1}) ./ probe_norm);
verbose(2,'Update probe difference: %3.2f%%', dprobe*100)
end
if kk >= min_iter && dprobe < min_change % change is below 1% and at least 2 iterations were done
break
end
end
end
if verbose > 2
for kk = 1:object_modes
Nresid = sum2(utils.findresidues(self.object{kk}(cache.object_ROI{:})) > 0.1);
if Nresid > 0
verbose(1,'Number of residua in object %i: %i', kk, Nresid)
end
end
end
end
function [probe_update,probe_illum] = QQ_probe(psi, obj_proj, probe_update,probe_illum, p_ind)
import engines.GPU_MS.GPU_wrapper.*
%% update probe
[upd, illum] = Gfun(@QQ_probe_Gfun, psi,obj_proj);
if size(probe_update,3)==1 % shared probe
probe_update = probe_update+sum(upd,3);
probe_illum = probe_illum+sum(illum,3);
else
probe_update(:,:,p_ind) = probe_update(:,:,p_ind)+sum(upd,3);
probe_illum(:,:,p_ind) = probe_illum(:,:,p_ind)+sum(illum,3);
end
end
function norm = get_probe_norm_aux(P0, P1)
norm = abs(P0-P1).^2;
end
function [upd, illum] = QQ_probe_Gfun(psi,proj)
upd = psi .* conj(proj) ;
illum = abs(proj).^2;
end
function [obj_update,obj_illum] = QQ_object(psi,obj_update,obj_illum, aprobe, cprobe, g_ind,ll,scan_ids,cache)
import engines.GPU_MS.shared.*
%% update object
psi = psi .* cprobe;
[obj_update,obj_illum] = set_views_rc(obj_update,obj_illum,psi,aprobe,1,ll, g_ind, cache, scan_ids,[]);
end
function psi = DM_update_psi(gamma,psi, psi_dash )
% real space update function for difference maps
psi = (1+gamma)*psi - gamma*psi_dash;
end
function psi_dash = DM_update(psi_dash,beta, psi_tmp,psi )
% update funciton for difference maps
psi_dash = psi_dash + beta * ( psi_tmp - psi ) ;
end
function object = update_object(object, object_upd, object_illum, delta, inertia)
% apply also some inertia in the object update
object = object*inertia + (1-inertia)*object_upd./ (object_illum+delta);
end
function probe = update_probe(self, probe, probe_update, probe_illum, par, probe_id)
import engines.GPU_MS.shared.*
probe_new = probe_update ./ (probe_illum+1e-6);
% apply probe support on the first probe mode
if probe_id == 1
probe_new = apply_probe_contraints(probe_new, self.modes{probe_id});
end
% add some inertia to prevent oscilations
probe = par.probe_inertia*probe + (1-par.probe_inertia)*probe_new;
end
+417
View File
@@ -0,0 +1,417 @@
%LSQML least squared maximum likelihood solver
%
%[self, cache, fourier_error] = LSQML(self,par,cache,fourier_error,iter)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
% ** iter current iteration number
% returns:
% ++ self self-like structure with final reconstruction
% ++ cache structure with precalculated values to avoid unnecessary overhead
% ++ fourier_error array [Npos,1] containing evolution of reconstruction error
%
function [self, cache, fourier_error] = LSQML(self,par,cache,fourier_error,iter)
import engines.GPU_MS.shared.*
import math.*
import utils.*
import plotting.*
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.LSQML.*
assert( ~(par.Nscans > 1 && par.object_modes > 1), 'Multiobject + multiscan not supported')
% define some useful variables
object_modes = size(self.object,1);
object_upd_sum = cell(object_modes,par.Nlayers);
obj_illum_sum = cell(object_modes,par.Nlayers);
obj_proj = cell(par.object_modes,1);
apply_subpx_shift(); % reset persistent values
for ll = 1:object_modes
for layer = 1:par.Nlayers
obj_illum_sum{ll,layer} = Gzeros(self.Np_o);
object_upd_sum{ll,layer} = Gzeros(self.Np_o, true);
end
end
probe_update_sum = Gzeros(size(self.probe{1}));
probe_amp_corr = [0,0];
%%%%%%%%%%%%%%%%%% LSQ-ML algorithm %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
beta_probe = ones(self.Npos,par.Nlayers);
beta_object = ones(self.Npos,par.Nlayers);
%for ML is more useful to get close/overlapping positions
% use already precalculated indices
if is_method(par, 'MLs')
% load sparse indices
rand_ind = randi(length( cache.preloaded_indices_sparse));
indices = cache.preloaded_indices_sparse{rand_ind}.indices;
scan_ids = cache.preloaded_indices_sparse{rand_ind}.scan_ids;
else % load compact indices
indices = cache.preloaded_indices_compact{1}.indices;
scan_ids = cache.preloaded_indices_compact{1}.scan_ids;
end
Nind = length(indices);
for jj = 1:Nind
layer_ids{jj} = 1:par.Nlayers;
end
% shuffle order but keep same over iterations
if is_method(par, 'MLs')
% shuffle order
ind_range = randperm(Nind);
else
% MLc, call groups in given order, they are sorted by size to make
% execution on GPU more effecient + stable convergence
ind_range = 1:Nind;
end
obj_proj_mean = 0;
%% apply updated in parallel over sets indices{jj}
for jj = ind_range
% list of positions solved in current subiteration
g_ind = indices{jj};
for ll = 1:max(par.probe_modes, par.object_modes)
% generate indices of the used probes
% single probe only
if par.share_probe % share incoherent modes
p_ind{ll} = 1;
else
if all(scan_ids{jj} == scan_ids{jj}(1))
p_ind{ll} = scan_ids{jj}(1);
else
% scan positions from multiple scans are processed in a single bunch
p_ind{ll} = scan_ids{jj};
end
end
end
%% suppress large amplitude of object, quick try by ZC
% if iter >= par.object_change_start && par.Nlayers > 1
% for ll=1:par.Nlayers
% temp = abs(self.object{ll});
% if any(gather(temp(:))>10)
% % keyboard;
% end
% % temp (temp> 2) =1;
% % self.object{ll} = temp.* exp(1i* angle(self.object{ll}));
% end
% end
% estimate forward model, ie wavefront behind the sample
[self, probe, obj_proj, psi] = get_forward_model(self, obj_proj, par,cache, g_ind, p_ind, scan_ids{jj}, layer_ids{jj});
% obj_proj_mean = obj_proj_mean + mean(abs(temp(:)));
%% load data to GPU
modF = get_modulus(self, cache, g_ind,true,jj);
mask = get_mask(self, cache.mask_indices, g_ind, par.damped_mask);
noise = get_noise(self, par, g_ind);
% get intensity (modulus) on detector including different corrections
[aPsi, aPsi2, cache, self] = get_reciprocal_model(self, psi, modF, mask,iter, g_ind, par, cache);
%%%%%%%%%%%%%%%%%%%%%%% LINEAR MODEL CORRECTIONS END %%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%
if iter > 0 && (verbose >= -1 || par.number_iterations > par.plot_results_every) && ...
((mod(iter,min(20, 2^(floor(2+iter/50)))) == 0 || iter < 10) ...
|| verbose()> 2) || any(iter == max(1,[1,par.number_iterations])) % || par.accelerated_gradients_start-1 >= iter
% calculate only sometimes to make it faster
[fourier_error(iter,g_ind)] = get_fourier_error(modF, aPsi, noise,mask, par.likelihood);
fourier_error(isnan(fourier_error)) = max(fourier_error(~isnan(fourier_error)));
fourier_error(isinf(fourier_error)) = max(fourier_error(~isinf(fourier_error)));
if any(~isfinite(fourier_error(iter,g_ind)))
%disp(any(~isfinite(fourier_error(iter,g_ind))))
if par.accelerated_gradients_start < par.number_iterations || par.momentum > 0
error('Convergence failed, error contains NaNs, quitting ... \n%s', 'If repeated, try to set eng.accelerated_gradients_start = inf or eng.momentum = 0; ')
else
error('Convergence failed, error contains NaNs, quitting ... \n%s', 'If repeated, try to set eng.beta_LSQ = 0.5 or less ')
end
end
end
if strcmp(par.likelihood, 'poisson') || iter == 0
[chi,R] = modulus_constraint(modF,aPsi,psi, mask, noise, par, 1);
if iter == 0
% in the first iteration only find optimal scale for the probe
probe_amp_corr(1) = probe_amp_corr(1) + Ggather(sum(modF(:).^2));
probe_amp_corr(2) = probe_amp_corr(2) + Ggather(sum(aPsi(:).^2));
continue
end
else
chi = modulus_constraint(modF,aPsi,psi, mask, noise, par, 1);
end
if ~strcmp(par.likelihood, 'poisson') % soft memory cleanup
mask = []; aPsi = []; noise = []; modF = []; R=[];
end
if iter > par.estimate_NF_distance
% update estimation of the nearfield propagation distance
[self, cache] = gradient_NF_propagation_solver(self,psi(:,end),chi, cache, g_ind);
end
if ~strcmp(par.likelihood, 'poisson') % soft memory cleanup
psi = [];
end
if strcmp(par.likelihood, 'poisson') % calculate only for the first mode
%ll = 1;
%% automatically find optimal step-size, note that for Gauss it is 1 !!
beta_xi = gradient_descent_xi_solver(self,modF, aPsi2, R,mask, g_ind, mean(cache.beta_xi(g_ind)), cache);
R = [];
for ll = 1:max(par.probe_modes, par.object_modes)
chi{ll} = chi{ll} .* beta_xi ;
end
end
% At this point, chi is at the far-field (detector) plane
% Now propagate chi back to the last object layer
% This is same as using back_fourier_proj w. distance = inf and no camera angle refinement
for ll= 1:max(par.object_modes, par.probe_modes)
chi{ll} = ifft2_safe(chi{ll}); % fully farfield inverse fft
end
%% %%%%%%%%%% LSQ optimization code (probe & object updates)%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for layer = par.Nlayers:-1:1
for ll = 1:max(par.probe_modes, par.object_modes)
object_reconstruct = iter >= par.object_change_start && (par.apply_multimodal_update || is_used(par, 'fly_scan') || ll <= par.object_modes );
probe_reconstruct = iter >= par.probe_change_start;
llo = min(par.object_modes, ll);
llp = min(par.probe_modes, ll);
% propagate to the previous layer
if layer < par.Nlayers
chi{ll} = back_fourier_proj(chi{ll}, self.modes{layer},g_ind);
end
if layer ~= par.Nlayers
% if only single layer is used, reuse obj_proj already
% loaded, but avoid storing obj_proj for each layer, rather load it again
obj_proj{llo} = get_views(self.object, obj_proj{llo},layer_ids{jj}(layer),llo, g_ind, cache, scan_ids{jj},[]);
end
% get update directions for each scan positions
if ( probe_reconstruct || layer > 1) && object_reconstruct
[probe_update, object_update_proj] = Gfun(@get_update_both, chi{ll}, obj_proj{llo}, probe{llp,layer});
elseif ( probe_reconstruct || layer > 1)
probe_update = Gfun(@get_update, chi{ll}, obj_proj{llo});
object_update_proj = 0;
else
probe_update = 0; m_probe_update = 0;
object_update_proj = Gfun(@get_update, chi{ll}, probe{llp,layer});
end
% refine single optimal probe update direction (use overlap constraint)
if probe_reconstruct || layer > 1
[self,m_probe_update, probe_update, cache] = refine_probe_update(self, obj_proj{llo}, probe_update, chi{ll},layer,ll,p_ind{ll},g_ind, par, cache);
end
if layer == 1
probe_update = [] ; % soft memory clean
end
% refine single optimal object update direction (use overlap constraint)
if object_reconstruct
[object_upd_sum,object_update_proj, cache] = refine_object_update(self, ...
object_update_proj,object_upd_sum,layer_ids{jj}(layer),scan_ids{jj},g_ind, par, cache);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%% calculate the optimal step %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ll == 1 % && layer == par.Nlayers
if par.beta_LSQ > 0 && object_reconstruct && probe_reconstruct && par.Nlayers == 1 % for Nlayers > 1 seems to be better using gradient_projection_solver in order to keep behaviour the same for each layer !!
% calculate the optimal step using LSQ method
[beta_probe(g_ind,layer), beta_object(g_ind,layer)] = ...
get_optimal_LSQ_step(self,chi{ll},object_update_proj,m_probe_update,obj_proj{llo},probe{llp,layer},p_ind{ll} , par);
elseif par.beta_LSQ > 0 && (object_reconstruct || probe_reconstruct )
% computationally cheaper method that assumes only
% diagonal terms of the AA matrix
[beta_probe(g_ind,layer),beta_object(g_ind,layer)] = ...
gradient_projection_solver(self,chi{ll},obj_proj{llo},probe{llp,layer},...
object_update_proj, m_probe_update,p_ind{ll}, par, cache);
elseif par.beta_LSQ == 0
% use constant step
beta_probe(g_ind,layer) = par.beta_probe;
beta_object(g_ind,layer) = par.beta_object;
end
beta_object(g_ind,layer) = beta_object(g_ind,layer) / par.Nlayers;
if (is_used(par, 'fly_scan') || par.apply_multimodal_update)
beta_object(g_ind,layer) = beta_object(g_ind,layer) / par.probe_modes;
end
end
object_update_proj = []; % soft memory clean
beta_object = max(0.2, beta_object);
beta_probe = max(0, beta_probe);
if any(jj == ind_range(end)) && ll == 1
% show the optimal steps calculation
% if verbose() > 2
% if mod(iter, 5) == 0
% plotting.smart_figure(121122)
% plot(squeeze(real(beta_object)))
% hold all
% plot(squeeze(real(beta_probe)))
% hold off
% legend({'O', 'P'})
% title('Update step for each scan position')
% xlabel('Scan positions #')
% ylabel('Step')
% drawnow
% end
% end
verbose(1,'Average step p%i: %3.3g o%i: %3.3g layer %i', llp,mean(beta_probe(g_ind,layer)),llo,mean(beta_object(g_ind,layer)), layer);
end
%%%%%%%%%%%%%%% apply update with the optimal LSQ step %%%%%%%%%%%%%%%%%
if probe_reconstruct && layer == 1 && ll <= max(par.probe_modes) % multilayer extension -> update probe only from the first layer
self.probe{ll} = update_probe(self.probe{ll}, m_probe_update, par, p_ind{ll}, g_ind, beta_probe, Nind); % finally update also the probe
end
if object_reconstruct && is_method(par, 'MLs')
self.object = update_object(self, self.object, object_upd_sum, layer_ids{jj}(layer), llo, {g_ind}, scan_ids(jj), par, cache, beta_object);
end
if ll == 1 && layer == par.layer4pos %layer == ceil(par.Nlayers/2) % assume that sample in center is best constrained . Changed to variable layer by Zhen Chen
%%%%%%%%%%%%% update other parameters of the ptychography model
if iter >= par.probe_position_search || iter >= par.detector_rotation_search || iter >= par.detector_scale_search
% find optimal position shift that minimize chi{1} in current iteration
[pos_update, pos_rotate_upd,probe_scale_upd, cache] = gradient_position_solver(self, chi{1}, obj_proj{1},probe{1,layer}, g_ind, iter, cache, par);
self.modes{1}.probe_scale_upd(end+1)=self.modes{1}.probe_scale_upd(end)+mean(probe_scale_upd);
self.modes{1}.probe_positions(g_ind,:)=self.modes{1}.probe_positions(g_ind,:)+pos_update;
self.modes{1}.probe_rotation_all(g_ind)=self.modes{1}.probe_rotation_all(g_ind)+squeeze(pos_rotate_upd);
end
if iter > par.probe_fourier_shift_search
% search position corrections in the Fourier space, use
% only informatiom from the first mode, has to be after
% the probes updated , SEARCH ONLY FOR THE FIRST MODE
self.modes{1} = gradient_fourier_position_solver(chi{1}, obj_proj{1},probe{1,layer},self.modes{1}, g_ind);
end
end
if par.Nlayers > 1
% get update direction for the next layer
chi{ll} = probe_update; % .* median(beta_probe(g_ind,layer));
else
chi{ll} = []; % soft memory clean
end
end
end
if iter > par.estimate_NF_distance
%% correct propagation distance if updated
for i = 1:par.Nlayers %par.Nmodes % layers by Zhen Chen
ASM = exp( self.modes{i}.distances(end)* cache.ASM_difference);
self.modes{i}.ASM_factor = ASM;
self.modes{i}.cASM_factor = conj(ASM);
end
end
% to be used for momentum calculation, use only the last layer
if par.momentum
uniq_p_ind = unique(p_ind{ll});
probe_update_sum(:,:,uniq_p_ind,1) = probe_update_sum(:,:,uniq_p_ind,1) + m_probe_update / Nind;
end
%% constraint periodic along propagation,Added by ZC
% regularize_layers works better (set regularize_layers > 0), then this is unnecessary.
% if par.Nlayers > 1 && isfield(par.p,'samelayer') && par.p.samelayer && (iter >= par.p.Nst_samelayer && iter <= par.p.Nend_samelayer)
% object_avg=0;
% for layer=1:par.Nlayers-1 % last layer is fixed to 1s.
% object_avg = object_avg + self.object{layer};
% end
% object_avg = object_avg ./(par.Nlayers-1);
% % object_avg = (object_avg .* exp(1i*angle(self.object{par.Nlayers})))./(par.Nlayers-1);
% for layer=1:par.Nlayers-1
% self.object{layer} = object_avg ;
% end
% end
% % self.object{par.Nlayers}(:)=1;
%
end
if iter == 0
% apply initial correction for the probe intensity and return
probe_amp_corr = sqrt(probe_amp_corr(1) / probe_amp_corr(2)); %% calculate ratio between modF^2 and aPsi^2
for ii = 1:par.probe_modes
self.probe{ii} = self.probe{ii}*probe_amp_corr;
end
verbose(2,'Probe amplitude corrected by %.3g',probe_amp_corr)
return
end
% applying single update emulates behaviour of the original ML method ->
% provides better noise robustness
% advantage is that less memory is needed and no linesearch is required
object_reconstruct = iter >= par.object_change_start; % && (par.apply_multimodal_update || is_used(par, 'fly_scan') || ll <= par.object_modes );
probe_reconstruct = iter >= par.probe_change_start;
% if true, caclulate momentum and use is for acceleration
momentum_acceleration = isfield(par, 'momentum') && par.momentum && par.number_iterations < par.accelerated_gradients_start;
if object_reconstruct && is_method(par, 'MLc')
for ll = 1:par.object_modes
for layer = 1:par.Nlayers
[self.object, object_upd_sum] = update_object(self, self.object, object_upd_sum, layer, ll, indices, scan_ids, par, cache, beta_object(:,layer));
end
end
%% apply momentum update on the object
if momentum_acceleration
[self, cache] = add_momentum_object(self, cache, par, object_upd_sum, iter, fourier_error, beta_object);
end
end
if probe_reconstruct && momentum_acceleration
%% apply momentum update on the probe
[self, cache] = add_momentum_probe(self, cache, par, {probe_update_sum}, iter, fourier_error, beta_probe);
end
%if iter>0
% disp(any(~isfinite(fourier_error(iter,:))))
%end
%% FLY-SCAN: join all subprobes
if iter >= par.probe_change_start
if is_used(par,'fly_scan')
probe_new = 0;
for ll = 1:par.Nmodes
probe_new = probe_new + self.probe{ll}/par.Nmodes;
end
for ll = 1:par.Nmodes
% allow variation of the modes intensity
proj(ll,1,:) = real(sum2( self.probe{ll} .* conj( probe_new)) ./ sum2(abs(probe_new).^2)) ;
self.probe{ll} = proj(ll,1,:) .* probe_new;
% assume constant intensity
% self.probe{ll} = probe_new;
end
end
end
% if iter >= par.probe_change_start
% for ll = 1:par.probe_modes
% self.probe{ll} = apply_probe_contraints(self.probe{ll}, self.modes{min(ll,end)});
% end
% end
end
%% merged CUDA kernels for faster calculations
function update = get_update(chi, proj)
update = chi .* conj(proj);
end
function [update_1, update_2] = get_update_both(chi, proj_1, proj_2)
update_1 = chi .* conj(proj_1);
update_2 = chi .* conj(proj_2);
end
+400
View File
@@ -0,0 +1,400 @@
% PIE - generalized version of the ptychographic iterative engine
%
%[self, cache, fourier_error] = PIE(self,par,cache,fourier_error,iter)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** fourier_error array [Npos,1] containing evolution of reconstruction error
% ** iter current iteration number
% returns:
% ++ self self-like structure with final reconstruction
% ++ cache structure with precalculated values to avoid unnecessary overhead
% ++ fourier_error array [Npos,1] containing evolution of reconstruction error
%
function [self, cache, fourier_error] = PIE(self,par,cache,fourier_error,iter)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
import engines.GPU_MS.shared.*
import engines.GPU_MS.PIE.*
%% MULTILAYER EXTENSION fix or remove in future
par.probe_modes = max(par.Nlayers, par.probe_modes);
par.object_modes = max(par.Nlayers, par.object_modes);
if par.variable_probe
% expand the probe to the full size
probe_0 = self.probe{1};
self.probe{1} = reshape(self.probe{1},prod(self.Np_p),[]);
self.probe{1} = reshape(self.probe{1} * self.probe_evolution', self.Np_p(1), self.Np_p(2), []);
end
par.multilayer_object = par.Nlayers > 1;
par.multilayer_probe = false; % not supported anymore
probe_amp_corr = [0,0];
psi = cell(par.Nmodes, 1);
Psi = cell(par.Nmodes, 1);
probe_max = cell(par.probe_modes,1);
object_max = cell(par.object_modes,1);
aprobe2 = abs(mean(self.probe{1},3)).^2; % update only once per iteration
if (is_method(par, 'ePIE') && ...
... % empirical estimation when the hybrid PIE method should be used
par.grouping > self.Npos/sqrt( pi^2 * mean(Ggather(cache.MAX_ILLUM)) / max(aprobe2(:)))) % || ...% use hybrid ePIE in case of large grouping
% (~isempty(self.modes{end}.ASM_factor) && par.grouping > 1) || ...
% (is_method(par, 'ePIE')&& par.multilayer_object)
par.method = 'hPIE'; % hybrid ePIE
if iter == 1;verbose(1,'Switching to hybrid PIE method '); end
end
if is_method(par, {'hPIE'})
for ll = 1:(par.object_modes*par.Nscans)
for layer = 1:par.Nlayers
obj_illum_sum{ll,layer} = Gzeros(self.Np_o);
object_upd_sum{ll,layer} = Gzeros(self.Np_o, true);
end
end
for ll = 1:par.probe_modes
probe_upd_sum{ll}= (1+1i)*1e-8;
probe_illum_sum{ll} = 1e-8;
end
end
for ll = 1:par.Nlayers
obj_proj{ll} = Gzeros([self.Np_p, par.grouping], true);
end
if par.delta_p
grad = @get_grad_lsq; % dumped least squares gradient (preconditioner)
else
grad = @get_grad_flat; % common PIE gradient
end
%%%%%%%%%%%%%%%%%% ePIE algorithm %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if par.grouping > 1
for ll = 1:par.probe_modes
if ~par.multilayer_object || ll == 1 % update only first layer probe
probe_max{ll} = Ggather(max2(abs(self.probe{ll}))).^2;
end
end
for ll = 1:par.object_modes
if ~par.multilayer_probe || ll == 1 % update only first layer object
object_max{ll} = Ggather(max2(abs(self.object{ll}(cache.object_ROI{:})))).^2;
end
end
end
if any(isnan(object_max{1})) || any(isnan(probe_max{1}))
error('Object or probe is nan, try smaller grouping')
end
% use already precalculated indices
rand_ind = randi(length( cache.preloaded_indices_sparse));
indices = cache.preloaded_indices_sparse{rand_ind}.indices;
scan_ids = cache.preloaded_indices_sparse{rand_ind}.scan_ids;
for ind_ii = randperm(length(indices))
g_ind = indices{ind_ii};
for ll = 1:max([par.probe_modes, par.object_modes,par.Nlayers])
% generate indices of the used probes
if par.variable_probe && ll == 1
p_ind{ll} = g_ind;
% elseif par.multilayer_object && ll > 1
% p_ind{ll} = 1:length(ii);
else % single probe only
if par.share_probe %|| ll > 1 % share incoherent modes
p_ind{ll} = 1;
else
if all(scan_ids{ind_ii} == scan_ids{ind_ii}(1))
p_ind{ll} = scan_ids{ind_ii}(1);
else
p_ind{ll} = scan_ids{ind_ii};
end
end
end
end
%% load data to GPU (if not loaded yet)
modF = get_modulus(self, cache, g_ind);
mask = get_mask(self, cache.mask_indices, g_ind);
noise = get_noise(self, par, g_ind);
% get objects projections
for layer = 1:par.Nlayers
ll = 1;
obj_proj{layer} = get_views(self.object, obj_proj{layer},layer,ll,g_ind, cache, scan_ids{ind_ii},[]);
end
% get illumination probe
for ll = 1:par.probe_modes
if ~par.multilayer_object || ismember(ll, [1,par.Nlayers:par.probe_modes])
probe{ll} = self.probe{min(ll,end)}(:,:,min(end,p_ind{ll}));
end
end
for ll = 1:max([par.probe_modes, par.object_modes, par.Nlayers]) % Nlayers
%% fourier propagation
if (ll == 1 && (par.multilayer_object || par.multilayer_probe) )
probe{ll} = self.probe{min(ll,end)}(:,:,min(end,p_ind{ll}));
end
if ll > 1 && par.multilayer_object || par.grouping == 1 % raw ePIE
probe_max{ll} = max(Ggather(max2(abs(probe{ll})))).^2;
end
if ll > 1 && par.multilayer_probe || par.grouping == 1 % raw ePIE
object_max{ll} = max(Ggather(max2(abs(obj_proj{ll})))).^2;
end
if (ll == 1 && par.apply_subpix_shift)
probe{ll} = apply_subpx_shift(probe{ll}, self.modes{min(end,ll)}.sub_px_shift(g_ind,:) );
end
probe{ll} = apply_subpx_shift_fft(probe{ll}, self.modes{1}.probe_fourier_shift(g_ind,:));
% get projection of the object and probe
psi{ll} = bsxfun(@times, obj_proj{min(ll,end)}, probe{min(ll,end)});
Psi{ll} = fwd_fourier_proj(psi{ll} , self.modes{min(end, ll)});
if ll < par.Nlayers && par.multilayer_object
probe{ll+1} = Psi{ll};
end
if ll < par.Nlayers && par.multilayer_probe
obj_proj{ll+1} = Psi{ll};
end
end
% get intensity (modulus) on detector including different corrections
aPsi = get_reciprocal_model(self, Psi, modF, mask,iter, g_ind, par, cache);
%%%%%%%%%%%%%%%%%%%%%%% LINEAR MODEL CORRECTIONS END %%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%
if iter > 0 && par.get_error && (mod(iter,min(20, 2^(floor(2+iter/50)))) == 0 || (iter < 20)) || iter == par.number_iterations % calculate only sometimes to make it faster
[fourier_error(iter,g_ind)] = get_fourier_error(modF, aPsi, noise,mask, par.likelihood);
end
if (par.multilayer_object || par.multilayer_probe)
[Psi(end),R] = modulus_constraint(modF,aPsi,Psi(end), mask, noise, par, 1); % apply only on the last layer !!!
else
[Psi,R] = modulus_constraint(modF,aPsi,Psi, mask, noise, par,1);
end
if iter == 0
% in the first iteration only find optimal scale for the probe
probe_amp_corr(1) = probe_amp_corr(1) + Ggather(sum(modF(:).^2));
probe_amp_corr(2) = probe_amp_corr(2) + Ggather(sum(aPsi(:).^2));
continue
end
if strcmp(par.likelihood, 'poisson') % calculate only for the first mode
%% automatically find optimal step-size, note that for Gauss it is 1 !!
cache.beta_xi(g_ind) = gradient_descent_chi_solver(self,modF, aPsi2, R,mask, g_ind, mean(cache.beta_xi), cache);
end
if(par.multilayer_object || par.multilayer_probe)
ind_modes = par.Nlayers:-1:1;
else
ind_modes = 1:max(par.probe_modes, par.object_modes);
end
for ll = ind_modes
layer = ll;
chi = back_fourier_proj(Psi{min(end,ll)}, self.modes{min(end,ll)})-psi{min(end,ll)};
%% get optimal gradient lenghts
object_update=0; probe_update=0;m_probe_update= 0;
if iter >= par.object_change_start && (ll <= max(par.Nlayers, par.object_modes) || par.apply_multimodal_update)
object_update = Gfun(grad,chi, probe{min(ll,end)},...
probe_max{min(end,ll)}(1,1,min(end,p_ind{ll})),par.delta_p);
end
if iter >= par.probe_change_start && ll <= max(par.probe_modes, par.Nlayers)
%% find optimal probe update !!!
probe_update = Gfun(grad,chi,obj_proj{min(end,ll)},...
object_max{min(ll,end)}, par.delta_p);
m_probe_update = mean(probe_update,3);
end
if ((ll == 1 && ~(par.multilayer_object || par.multilayer_probe)) || ...
(ll == par.Nlayers && (par.multilayer_object || par.multilayer_probe))) && ...
(par.beta_LSQ || iter >= par.probe_position_search)
%% variable step extension, apply only the first mode except the 3PIE case
%% it will use the same alpha for the higher modes !!!!
[cache.beta_probe(g_ind),cache.beta_object(g_ind)] = gradient_projection_solver(self,chi,obj_proj{min(end,ll)},probe{ll},...
object_update, m_probe_update,p_ind{ll}, par, cache);
if any(g_ind ==1)
verbose(1,'Average alpha p:%3.3g o:%3.3g ', mean(cache.beta_probe),mean(cache.beta_object));
end
end
%%%%%%%%%%%%%%%%%%%%% PROBE UPDATE %%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%
%% update probe first
if ((iter >= par.probe_change_start) && ll <= max(par.probe_modes,par.Nlayers) ...
&& is_method(par, 'PIE')) || ...
(par.multilayer_object && ll > 1 && ll <= par.probe_modes)
% only in case of first layer probe, otherwise update interprobes
beta_probe = get_vals(cache.beta_probe,g_ind) .* get_vals(cache.beta_xi,g_ind);
if is_method(par, {'ePIE', 'hPIE'})
%%%%%%%%%%% update probe %%%%%%%%%%%%%%%%%%%%%%%%%%
probe{ll} = Gfun(@upd_probe_Gfun,probe{ll},probe_update, beta_probe);
if (ll == 1 && par.apply_subpix_shift)
probe{ll} = apply_subpx_shift(probe{ll} , -self.modes{min(end,ll)}.sub_px_shift(g_ind,:));
end
if iter >= par.probe_change_start
if (par.variable_probe && ll == 1)
self.probe{ll}(:,:,p_ind{ll}) = probe{ll}; % slowest line for large datasets !!!!!
elseif (~par.multilayer_object || ll == 1) && ll <= par.probe_modes
self.probe{ll} = mean(probe{ll},3); % merge information from all the shifted probes if needed
end
end
end
end
if iter > par.probe_fourier_shift_search && ll == 1
% search position corrections in the Fourier space, use
% only informatiom from the first mode, has to be after
% the probes updated
self.modes{1} = gradient_fourier_position_solver(chi, obj_proj{1},probe{1},self.modes{1}, g_ind);
end
if iter >= par.probe_position_search
% find optimal position shift that minimize chi{1} in current iteration
[pos_update, cache] = gradient_position_solver(self, chi, obj_proj{1},probe{1,layer}, g_ind, iter, cache);
self.modes{1}.probe_positions(g_ind,:)=self.modes{1}.probe_positions(g_ind,:)+pos_update;
end
%%%%%%%%%%%%%%%%%%%%% OBJECT UPDATE %%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%
if iter >= min([par.object_change_start]) && ....
( ll <= max(par.Nlayers, par.object_modes) || par.apply_multimodal_update )
if ll ~= 1 && ~(par.multilayer_object || par.multilayer_probe) ; continue; end
if iter >= par.object_change_start % && ~(par.multilayer_probe && ll > 1) % the objects are just empty
beta_object = get_vals(cache.beta_object,g_ind) .* get_vals(cache.beta_xi,g_ind);
if par.share_object
obj_ids = 1; % update only the first object
else
obj_ids = unique(scan_ids{ind_ii}); % update only the objects processed in this block
end
if any(beta_object ~= 1)
object_update = bsxfun(@times, object_update, beta_object);
end
if is_method(par, 'ePIE') % use always in nearfield
% classical ePIE, faster constraint application, but it will fail with too high grouping
self.object = set_views(self.object, object_update,layer, obj_ids, g_ind, cache, scan_ids{ind_ii},[]);
elseif is_method(par, 'hPIE') %% hybrid PIE
if par.Nscans == 1 || par.share_object
ind_tmp = 1;
else
ind_tmp = 1+par.object_modes* ((1:par.Nscans)-1);
end
for kk = ind_tmp
obj_illum_sum{kk,layer}(:) = 0;
object_upd_sum{kk,layer}(:) = 0;
end
object_update = bsxfun(@times, object_update, aprobe2); % make is more like dumped LSQ solution
[object_upd_sum,obj_illum_sum] = set_views_rc(object_upd_sum,obj_illum_sum, object_update,aprobe2,layer,obj_ids, g_ind, cache, scan_ids{ind_ii},[]);
for kk = ind_tmp
self.object{kk,layer} = Gfun(@object_update_Gfun, self.object{kk,layer},object_upd_sum{kk,layer}, obj_illum_sum{kk,layer}, cache.MAX_ILLUM(min(kk,end)));
end
else
error('Unimplemented method %s ', par.method)
end
end
end
if ll > 1 && par.multilayer_object
% % apply rescaling to make scaling correction
Psi{ll-1} = probe{ll};
end
if ll > 1 && par.multilayer_probe
Psi{ll-1} = obj_proj{ll} + object_update;
end
end
if check_avail_memory < 0.2 || ~par.keep_on_gpu
% slow step that is not needed if there is enough memory ,
% it can slow down almost twice !!!
clear Psi R aPsi2 psi probe_update object_update chi
if par.keep_on_gpu
warning('Low GPU memory')
end
end
end
if par.multilayer_object
self.probe = self.probe(1);
end
if par.variable_probe && iter >= par.probe_change_start
[self.probe{1}, self.probe_evolution] = apply_SVD_filter(self.probe{1}, par.variable_probe_modes+1, self.modes{1});
elseif par.variable_probe && iter < par.probe_change_start
self.probe{1} = probe_0;
elseif ( ~isempty(self.probe_support)) && iter >= par.probe_change_start
self.probe{1} = apply_probe_contraints(self.probe{1}, self.modes{1});
end
if iter == 0
% apply initial correction for the probe intensity and return
% it seems to be safer to underestimate the probe amplitude for variable probe method
probe_amp_corr = 0.5*sqrt(probe_amp_corr(1) / probe_amp_corr(2)); %% calculate ratio between modF^2 and aPsi^2
for ii = 1:par.probe_modes
self.probe{ii} = self.probe{ii}*probe_amp_corr;
end
verbose(2,'Probe amplitude corrected by %.3g',probe_amp_corr)
return
end
end
function probe = upd_probe_Gfun(probe,probe_update, alpha_p)
probe = probe + alpha_p.*probe_update;
end
function grad = get_grad_flat(chi,proj,max2, delta )
% ePIE method
grad = (1/max2) * chi .* conj(proj) ;
end
function grad = get_grad_lsq(chi,proj,max2, delta )
% dumped LSQ method
aproj = abs(proj) ;
grad = chi .* conj(proj) .* aproj ./ (aproj.^2 + delta*max2)./sqrt(max2);
end
function object = object_update_Gfun(object,object_upd_sum, obj_illum_sum, max)
object = object + object_upd_sum ./ (obj_illum_sum+1e-9* max);
end
function array = get_vals(array, ind)
if isscalar(array)
return
else
array = reshape(array(ind),1,1,[]);
end
end
@@ -0,0 +1,117 @@
% ACCELERATE_GRADIENTS use the Nesterov's Accelerated Gradient method
%
% cache = accelerate_gradients(self, par, cache, iter)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values to avoid unnecessary overhead
% ** iter number of the current iteration
%
% returns:
% ** self updated structure containing inputs
% ** cache structure with precalculated values - stores the previous accelerated gradients updates
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [self,cache] = accelerate_gradients(self, par, cache, iter)
%% accelerated gradients extension, tries to look ahead in direction of the last update
if iter == par.accelerated_gradients_start
cache.object_prev = {self.object, self.object};
cache.probe_prev = {self.probe, self.probe};
for ii = 1:length(cache.illum_sum_0)
cache.update_weights{ii} = cache.illum_sum_0{ii}.^2 ./ (cache.illum_sum_0{ii}.^2 + 0.1*cache.MAX_ILLUM(ii)^2);
end
elseif iter > par.accelerated_gradients_start
object_start = max(par.object_change_start, par.accelerated_gradients_start);
probe_start = max(par.probe_change_start, par.accelerated_gradients_start);
cache.object_prev{1} = cache.object_prev{2};
cache.object_prev{2} = self.object;
cache.probe_prev{1} = cache.probe_prev{2};
cache.probe_prev{2} = self.probe;
% accelerate the object reconstructions
if iter > par.object_change_start
beta = (iter-object_start+1)/(iter-object_start+3);
for ii = 1:size(self.object,1)
for layer = 1:size(self.object,2)
if ii == 1 && utils.verbose() > 3
plotting.smart_figure(555)
update = cache.update_weights{ii}.* (self.object{ii,layer} - cache.object_prev{1}{ii,layer});
plotting.imagesc3D(update)
title('Acceleration step in the Nesterov method')
axis xy off
utils.verbose(0, 'Norm of accelerated gradient = %g', math.norm2(update) )
drawnow
end
self.object{ii,layer} = self.object{ii,layer} + ...
beta*cache.update_weights{ii}.*(self.object{ii,layer} - cache.object_prev{1}{ii,layer});
end
end
end
% accelerate only the first OPR mode of the probe
if iter > par.probe_change_start
beta = (iter-probe_start+1)/(iter-probe_start+3);
for ii = 1:numel(self.probe)
self.probe{ii}(:,:,:,1) = self.probe{ii}(:,:,:,1) + ...
beta.*(self.probe{ii}(:,:,:,1) - cache.probe_prev{1}{ii}(:,:,:,1));
end
end
end
end
@@ -0,0 +1,78 @@
% APPLY_SMOOTHNESS_CONSTRAINT simple weak smoothness constaint by convolution
%
% x = apply_smoothness_constraint(x, alpha)
%
% ** x image stack to be smoothed
% ** alpha relaxation smoothing constant, 0 = no smoothness, 1/8 = maximal smoothness given as conv(x, ones(3)/9)
%
% returns:
% ++ x smoothed array
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 x = apply_smoothness_constraint(x, alpha)
assert(alpha < 1/8, 'Too high smoothing')
if alpha > 0
psf = ones(3)*alpha;
psf(2,2) = 1-8*alpha;
if size(x,3) == 1
x = conv2(x, psf, 'same');
else
x = convn(x, psf, 'same');
end
end
end
@@ -0,0 +1,82 @@
% concatenate values from list of cells contaning structures with arrays
%
% array = cat_struct( struct, name, ind)
%
% ** struct list structures
% ** name field name to be ccntatenated
% ** ind which index to take {'last', 'first'}
%
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 array = cat_struct( struct, name, ind)
array = [];
if nargin < 3
ind = [];
end
for i = 1:length(struct)
s = struct{i}.(name);
if strcmp(ind, 'last')
s = s(end);
elseif strcmp(ind, 'first')
s = s(1);
end
array = [array, s(:)];
end
end
@@ -0,0 +1,126 @@
% GET_FOURIER_ERROR fast calculation of the Fourier plane (at detector) error normalized so that for
% gaussian noise approximation the ideal error should be close to 1
%
% Err = get_fourier_error(modF, aPsi, Noise,Mask, likelihood)
%
% ** modF pre-fftshifted and sqrt-ed data
% ** aPsi reciprocal amplitude model
% ** Noise estimated noise (STD) in each pixel after sqrt transform
% ** Mask masked values, 1 = ignored, 0 = use this pixel
% ** likelihood L1 or poisson
%
% returns:
% ++ Err calculated error for provided positions
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 Err = get_fourier_error(modF, aPsi, Noise,Mask, likelihood)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
import math.*
if ~exist('likelihood', 'var'); likelihood = 'L1'; end
likelihood = lower(likelihood);
% USE Gfun IN ORDER TO MAKE IT FASTER ON GPU
if isempty(Mask) && isempty(Noise)
switch likelihood
case 'l1', Err = Gfun(@get_err,modF, aPsi);
case 'poisson', Err = Gfun(@get_loglik,modF, aPsi);
end
elseif ~isempty(Mask) && isempty(Noise)
switch likelihood
case 'l1', Err = Gfun(@get_err_masked,modF, aPsi, Mask);
case 'poisson', Err = Gfun(@get_loglik_masked,modF, aPsi, Mask);
end
elseif isempty(Mask) && ~isempty(Noise)
Err = Gfun(@get_err_noise,modF, aPsi, Noise);
else
Err = Gfun(@get_err_noise_mask, modF, aPsi, Mask, Noise);
end
switch likelihood
case 'l1', Err = sqrt(squeeze(mean2(Err)))';
case 'poisson', Err = squeeze(mean2(Err))';
otherwise, error('Unsupported likelihood')
end
end
function L = get_loglik(modF, aPsi)
modF2 = modF.^2;
aPsi2 = aPsi.^2;
L = -(modF2 .* log(aPsi2+1e-6) - aPsi2) ;
end
function L = get_loglik_masked(modF, aPsi,Mask)
modF2 = modF.^2;
aPsi2 = aPsi.^2;
L = -(1-Mask) .* (modF2 .* log(aPsi2+1e-6) - aPsi2) ;
end
function E = get_err(modF, aPsi)
E = (modF-aPsi).^2 / (0.5)^2; % 0.5 is correction for the Poisson noise (if we expect single photon precision)
end
function E = get_err_masked(modF, aPsi, Mask)
E = (modF-aPsi).^2 .* (1-Mask) / (0.5)^2;
end
function E = get_err_noise(modF, aPsi, Noise)
E = (modF-aPsi).^2 ./ Noise.^2;
end
function E = get_err_noise_mask(modF, aPsi, Mask, Noise)
E = (modF-aPsi).^2 ./ Noise.^2 .* (1-Mask);
end
@@ -0,0 +1,109 @@
% GET_IMG_GRAD get vertical and horizontal gradient of the image, it is slightly faster on GPU than
% the version in math.get_img_grad
%
% [dX, dY] = get_img_grad(img)
%
% ** img stack of complex images
%
% returns:
% ++ dX,dY - directional gradients of the image
%
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [dX, dY] = get_img_grad(img)
import engines.GPU_MS.GPU_wrapper.*
Np = size(img);
if nargout == 1
fX = fft(img,[],2);
X = (fftshift((0:Np(2)-1)/Np(2))-0.5);
dX = bsxfun(@times, fX,2i*pi*X);
dX = ifft(dX,[],2);
return
end
if nargout > 1
X = (fftshift((0:Np(2)-1)/Np(2))-0.5);
Y = (fftshift((0:Np(1)-1)/Np(1))-0.5)';
% use matlab implicite GPU paralelization
if isa(img, 'gpuArray')
% it is much faster to use 2D fft for GPU despite higher
% computational costs
img = fft2(img);
% make it slightly faster with GPU
[dX, dY] = Gfun(@multiply_gfun, img, X, Y);
dX = ifft2(dX);
dY = ifft2(dY);
else
fX = fft(img,[],2);
fY = fft(img,[],1);
dX = bsxfun(@times, fX,2i*pi*X);
dY = bsxfun(@times, fY,2i*pi*Y);
dX = ifft(dX,[],2);
dY = ifft(dY,[],1);
end
end
end
function [dX, dY]=multiply_gfun(img, X,Y)
dX = img .* (2i*pi)* X;
dY = img .* (2i*pi)* Y;
end
@@ -0,0 +1,287 @@
% GET_RECIPROCAL_MODEL get estimate of the measured intensity from given electric field Psi
%
% [aPsi, aPsi2, cache, self] = get_reciprocal_model(self, Psi, modF, mask,iter, g_ind, par, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** Psi where Psi is the propagated exitwave
% ** modF pre-fftshifted and sqrt-ed data
% ** mask masked values, 1 = ignored, 0 = use this pixel
% ** iter current iteration number
% ** ind processed indices
% ** par structure containing parameters for the engines
% ** cache structure with precalculated values
%
% returns:
% ++ aPsi reciprocal amplitude model
% ++ aPsi reciprocal intensity model
% ++ cache structure with precalculated values
% ++ self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [aPsi, aPsi2, cache, self] = get_reciprocal_model(self, Psi, modF, mask,iter, g_ind, par, cache)
import engines.GPU_MS.GPU_wrapper.*
aPsi2 = [];
if par.probe_modes == 1 && isempty(self.background) && self.diff_pattern_blur == 0 ...
&& ~par.background_detection && strcmpi(par.likelihood, 'l1') && par.upsampling_data_factor == 0
% or the simplest and fastest option: just get absolute value
aPsi = abs(Psi{1});
aPsi2 = [];
elseif par.probe_modes == 1 && ~isempty(self.background) && self.diff_pattern_blur == 0 ...
&& ~par.background_detection && strcmpi(par.likelihood, 'l1') && par.upsampling_data_factor == 0
% second simplest option, abs + background
aPsi = Gfun(@modulus_with_background,Psi{1}, self.background , cache.background_profile);
else
% apply corrected model and sum up all coherence modes
aPsi2 = sumsq_cell(Psi);
% assume that the data were upsampled by the utils.unbinning_2D function
if par.upsampling_data_factor
aPsi2 = utils.binning_2D(aPsi2, 2^par.upsampling_data_factor);
end
%%%%%%%%%%%%%%%% linear correction model %%%%%%%%%%%%%%%%%%%%%%%
[aPsi2,cache, self] = get_linear_correction_model(self,par,cache,aPsi2,modF,mask,iter, g_ind );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
aPsi = sqrt(aPsi2);
end
end
function aPsi = modulus_with_background(Psi, background_value, background_shape)
rPsi = real(Psi);
iPsi = imag(Psi);
aPsi2 = rPsi.^2 + iPsi.^2;
aPsi2 = aPsi2 + background_value .* background_shape;
% sqrt is very slow ...
aPsi = sqrt(aPsi2);
% aPsi = exp(0.5*log(aPsi2)); % log identity has exactly the same calculation time
end
function y = sumsq_cell(x)
% Description: sum incoherently cells x, make it inplace and fast
N = length(x);
if N <= 15 && builtin( 'isa', x{1}, 'gpuArray' )
switch N
case 1, fun = @sum_1;
case 2, fun = @sum_2;
case 3, fun = @sum_3;
case 4, fun = @sum_4;
case 5, fun = @sum_5;
case 6, fun = @sum_6;
case 7, fun = @sum_7;
case 8, fun = @sum_8;
case 9, fun = @sum_9;
case 10, fun = @sum_10;
case 11, fun = @sum_11;
case 12, fun = @sum_12;
case 13, fun = @sum_13;
case 14, fun = @sum_14;
case 15, fun = @sum_15;
end
y = arrayfun(fun, x{:});
else
y = 0;
for i = 1:N
y = y + abs(x{i}).^2;
end
end
end
% !! using sqrt(imag(x)^2 + real(x)^2) is much slower !!!
% merged GPU kernels
function y = sum_1(x)
y = abs(x).^2;
end
function y = sum_2(x1,x2)
y = abs(x1).^2+abs(x2).^2;
end
function y = sum_3(x1,x2,x3)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2;
end
function y = sum_4(x1,x2,x3,x4)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2;
end
function y = sum_5(x1,x2,x3,x4,x5)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2;
end
function y = sum_6(x1,x2,x3,x4,x5,x6)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2;
end
function y = sum_7(x1,x2,x3,x4,x5,x6,x7)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2;
end
function y = sum_8(x1,x2,x3,x4,x5,x6,x7,x8)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2;
end
function y = sum_9(x1,x2,x3,x4,x5,x6,x7,x8,x9)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2;
end
function y = sum_10(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2;
end
function y = sum_11(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2+abs(x11).^2;
end
function y = sum_12(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2+abs(x11).^2+abs(x12).^2;
end
function y = sum_13(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2+abs(x11).^2+abs(x12).^2+abs(x13).^2;
end
function y = sum_14(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2+abs(x11).^2+abs(x12).^2+abs(x13).^2+abs(x14).^2;
end
function y = sum_15(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15)
y = abs(x1).^2+abs(x2).^2+abs(x3).^2+abs(x4).^2+abs(x5).^2+abs(x6).^2+abs(x7).^2+abs(x8).^2+abs(x9).^2+abs(x10).^2+abs(x11).^2+abs(x12).^2+abs(x13).^2+abs(x14).^2+abs(x15).^2;
end
function [aPsi2, cache, self] = get_linear_correction_model(self,par,cache,aPsi2,modF,mask, iter, ii )
import engines.GPU_MS.GPU_wrapper.*
if isempty(self.background) && self.diff_pattern_blur == 0 && strcmp(par.background_detection, 'none')
return % nothing to be done, return
end
%% add background
if ~isempty(self.background)
if ~isfield(cache, 'background_profile') || isscalar(cache.background_profile)
aPsi2 = aPsi2 + self.background;
else
aPsi2 = Gfun(@add_background, aPsi2, self.background, cache.background_profile,modF);
end
end
if self.diff_pattern_blur > 0
%%%%%%%%%%%%%%%%%%%%%%% LINEAR MODEL CORRECTIONS START %%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%
if isempty(self.modes{1}.ASM_factor) % is not nearfield
aPsi2 = aPsi2(cache.fftshift_idx{:},:);
end
% apply blur correction change to Gaussian by ZC
if self.diff_pattern_blur > 0
blur_kernel = fspecial('gaussian',round(self.diff_pattern_blur) *10+1,self.diff_pattern_blur);
aPsi2 = convn(aPsi2, blur_kernel, 'same');
end
% apply blur correction
% if self.diff_pattern_blur > 0
% % generate blurring kernel
% x = [-1, 0,-1]/self.diff_pattern_blur;
% [X,Y] = meshgrid(x,x);
% blur_kernel = exp( -(X.^2 + Y.^2) );
% blur_kernel = blur_kernel / math.sum2( blur_kernel );
% aPsi2 = convn(aPsi2, blur_kernel, 'same');
% end
if isempty(self.modes{1}.ASM_factor) % is not nearfield
aPsi2 = aPsi2(cache.fftshift_idx{:},:);
end
end
% simple estimation of background
if par.background_detection && iter > par.background_detection
if isempty(mask); mask = false; end
% calculate the most optimal background update
[nom,denom] = Gfun(@get_background_estimate,modF, aPsi2, mask, cache.background_profile_weight, cache.background_profile );
update = sum2(nom)./sum2(denom);
if any(ii == 1)
fprintf('Background update: %3.3g curr value:%3.3g \n ', mean(update), self.background);
end
self.background = posit(self.background + (par.grouping/self.Npos)*mean(update));
% %% Check if background is fitted well
% X = (-self.Np_p(1)/2:self.Np_p(1)/2-1);
% Y = (-self.Np_p(2)/2:self.Np_p(2)/2-1);
% [X,Y] = meshgrid(X,Y);
%
% R = (sqrt(X.^2 + Y.^2));
% D = fftshift(single(modF.^2) - aPsi2);
% for i = 1:mean(self.Np_p)/2
% progressbar(i, mean(self.Np_p)/2);
% mask = (R==i);
% mask = mask / sum2(mask);
% B(i) = median(sum2(bsxfun(@times, D, mask)));
% end
% plot(B)
% ylim([-5,5])
% drawnow
%
end
end
function aPsi2 = add_background(aPsi2, background,background_profile,modF)
aPsi2 = aPsi2 + background .* background_profile .* (modF > 0); % leave empty pixels empty
end
function [nom, denom] = get_background_estimate(modF, aPsi2, mask, distribution, background )
W = ~mask .* distribution;
nom = W.* (modF.^2 - aPsi2).*background;
denom = W.*background.^2;
end
@@ -0,0 +1,149 @@
% GRADIENT_NF_PROPAGATION_SOLVER update estimate of the nearfield propagation distance based on the
% current update of the wavefront
%
% self = gradient_NF_propagation_solver(self,psi,chi, cache, ind)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** psi exitwave (psi = P*O)
% ** chi [Nx,Ny,N] array, difference between original and updated exit-wave
% ** cache precalculated values
% ** ind indices containg corresponding probe id for each processed position
%
% returns:
% ++ self updated self structure with optimized propagation distance
% ++ cache precalculated values with updated velocity for momentum method
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [self, cache] = gradient_NF_propagation_solver(self,psi,chi, cache, ind, layer_id)
% update estimate of the nearfield propagation distance based on the
% current update of the wavefront
import engines.GPU_MS.GPU_wrapper.*
import engines.GPU_MS.shared.*
import math.*
import utils.*
dH = cache.ASM_difference;
% propagate to farfield
Psi_0 = fft2_safe(psi{1});
Psi_1 = fft2_safe(psi{1}+chi{1});
% get phase diffence
Psi_diff = sum(Psi_0 .* conj(Psi_1),3);
aPsi = abs(Psi_diff);
Psi_diff = Psi_diff ./ (aPsi+mean2(aPsi).*1e-6);
% calculate distance that best explains the differences
dz = -Ggather(sum2(real(conj(dH) .* Psi_diff)) ./ sum2(abs(dH).^2));
% %% USE MOMENTUM ACCELERATION TO MAKE THE CONVERGENCE FASTER
% try
% momentum_memory = 10; % compare 10 iterations
% if length(self.modes{1}.distances) > momentum_memory
% if ~isfield(cache, 'velocity_NF_propagation')
% cache.velocity_NF_propagation = 0;
% end
%
% updates = diff(self.modes{1}.distances(end-momentum_memory:end));
% corr_level = corr(updates(1:end-1)', updates(2:end)');
%
% corr_level
%
%
% if all(corr_level > 0 )
% %%%%%%%%%%%% very empritical model %%%
% gain = 1; % smaller -> lower relative speed (less momentum)
% friction = 0.1*max(0, 0.5-corr_level); % smaller -> longer memory, more momentum
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% else
% gain = 0; friction = 0.5;
% end
%
% cache.velocity_NF_propagation = cache.velocity_NF_propagation*(1-friction) + dz;
% %% apply the velocity to the refined positions , if the postition updated are sufficiently small
%
% cache.velocity_NF_propagation
%
% dz = dz + gain*cache.velocity_NF_propagation;
%
%
% end
% catch
% keyboard
% end
% update propagation values
if any(ind==1)
% add new field only when new iteration is started
self.modes{1}.distances = [self.modes{1}.distances,self.modes{1}.distances(end) + dz];
verbose(1, 'Propagation distance: %3.5gum ', self.modes{1}.distances(end)*1e6)
else
self.modes{1}.distances = self.modes{1}.distances + dz;
end
% use the same distance for all positions
for i = 1:length(self.modes)
self.modes{i}.distances = self.modes{1}.distances;
end
end
@@ -0,0 +1,117 @@
% GRADIENT_DESCENT_XI_SOLVER solve optimal update step for the Poisson noise method
%
% [beta_chi, find_xi_step] = gradient_descent_xi_solver(self,modF, aPsi2, R,mask, ind, beta_xi, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** modF pre-fftshifted and sqrt-ed data
% ** aPsi2 reciprocal intensity model
% ** R 1- modF / aPsi
% ** mask masked values on detector
% ** ind processed indices
% ** beta_xi previous steps, needed for calculation as initial guess
% ** cache precalculated values
%
% returns:
% ++ beta_xi optimal probe step
% ++ find_xi_step optimal object step
%
% see also: engines.GPU_MS.LSQML, engines.GPU_MS.PIE
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [beta_xi, find_xi_step] = gradient_descent_xi_solver(self,modF, aPsi2, R,mask, ind, beta_xi, cache)
import utils.*
import math.*
import engines.GPU_MS.GPU_wrapper.*
find_xi_step = true;
%% !! use precached values if the change is small
if ~find_xi_step
beta_xi = cache.beta_xi_all(ind,end);
else
if isempty(mask)
mask = 0;
end
for i = 1:2
[nom, denom] = Gfun(@get_coefs, aPsi2,modF,R,mask,beta_xi);
% avoid oscilations of the solution
beta_xi = beta_xi*0.5 + 0.5* Ggather(sum2(nom) ./ sum2(denom));
beta_xi = abs(max(min(beta_xi,1),0));
end
beta_xi = beta_xi + randn(size(beta_xi)) * 1e-2;
end
if any(ind ==1)
verbose(1,'Average xi_alpha %3.2g find_step %i ', mean(beta_xi(:)), find_xi_step)
end
end
function [nom, denom,W] = get_coefs(aPsi2,modF,R,mask,alpha)
modF2 = modF.^2;
chi = 1-R;
W = 1-mask;
nom = -W.*chi.* (modF2./ (1-alpha.*chi) - aPsi2);
denom = W.*aPsi2.*chi.^2;
end
@@ -0,0 +1,110 @@
% FUNCTION mode = gradient_fourier_position_solver(chi,O,P,mode, ind)
% Description: solve position errors in the Fourier space
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** chi [Nx,Ny,N] array, difference between original and updated exit-wave
% ** O [Nx,Ny,N] array, object views
% ** P [Nx,Ny,1] or [Nx,Ny,N] array, single or variable probe
% ** mode structure with information about each incoherent mode
% ** ind processed indices
%
% returns:
% ++ mode updated mode structure
%
% see also: engines.GPU_MS.LSQML, engines.GPU_MS.PIE
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 mode = gradient_fourier_position_solver(chi,O,P,mode, ind)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
% use gradinent solver for position correction in fourier space
Np = size(P);
X = linspace(-0.5, 0.5,Np(2)) ;
Y = linspace(-0.5, 0.5,Np(1))' ;
[nom_dx, denom_dx, nom_dy, denom_dy] = Gfun(@get_coefs,chi, P,O,X, Y);
dx = - sum2(nom_dx)./ sum2(denom_dx);
dy = - sum2(nom_dy)./ sum2(denom_dy);
shift = squeeze(Ggather(cat(4,dx, dy)));
shift = min(abs(shift), 0.2) .* sign(shift); % avoid too fast jumps, <0.5px/iter is enough
if any(ind==1)
verbose(1,'Grad fourier pos correction -- AVG step %3.2g px', mean(abs(shift(:))))
end
mode.probe_fourier_shift(ind,:)=mode.probe_fourier_shift(ind,:)+reshape(shift,[],2);
end
function [nom1, denom1, nom2, denom2] = get_coefs(chi, P, O, dX, dY)
dPx = 2i*pi.*dX.*P.*O;
dPy = 2i*pi.*dY.*P.*O;
nom1 = real(conj(dPx) .* chi);
denom1 = abs(dPx).^2;
nom2 = real(conj(dPy) .* chi);
denom2 = abs(dPy).^2;
end
@@ -0,0 +1,196 @@
% GRADIENT_POSITION_SOLVER solve position errors in the real space
%
% [pos_update, cache] = gradient_position_solver(self,xi,O,P,ind, iter, cache)
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** xi exit wave update vector
% ** O object views
% ** P probe or probes
% ** ind indices of of processed position
% ++ iter current iteration
% ** cache structure with precalculated values to avoid unnecessary overhead
%
% returns:
% ++ pos_update position updates for each of the indices
% ++ cache updated structure with precalculated values
%
% see also: engines.GPU_MS.LSQML, engines.GPU_MS.PIE
function [pos_update, probe_rotation,probe_scale,cache] = gradient_position_solver(self,xi,O,P,ind, iter, cache, par)
import engines.GPU_MS.GPU_wrapper.*
import math.*
import utils.*
% use gradinent solver for position correction
low_mem_errs = {'id:parallel:gpu:array:OOMForOperation',...
'id:MATLAB:LowGPUMem','MATLAB:LowGPUMem',...
'parallel:gpu:array:OOM',...
'parallel:gpu:device:UnknownCUDAError', ...
'parallel:gpu:array:OOMForOperation', ...
'parallel:gpu:array:FFTInternalError'};
% wrapper around get_img_grad, in case of low memory it will try to repeat
% Ntimes before giving up
pos_update = 0; probe_rotation = 0; probe_scale = 0;
N = 5;
for ii = 1:N
try
% reuse dx_O, dy_O to save memory !!
[dx_O,dy_O]=get_img_grad(O);
if iter >= par.detector_rotation_search
%% estimate detector rotation
xgrid = Garray(linspace(-1,1,self.Np_p(1))');
ygrid = Garray(-linspace(-1,1,self.Np_p(2)));
[nom, denom] = Gfun(@get_coefs_mixed,xi, P, dx_O, dy_O, xgrid, ygrid);
probe_rotation = gather(sum2(nom)./ sum2(denom));
end
if iter >= par.detector_scale_search
%% estimate detector scale (ie pixel scale error in farfield mode)
xgrid = Garray(-linspace(-1,1,self.Np_p(2)) .* tukeywin(self.Np_p(2), 0.1)');
ygrid = Garray(-linspace(-1,1,self.Np_p(1))'.* tukeywin(self.Np_p(1), 0.1));
[nom, denom] = Gfun(@get_coefs_mixed,xi, P, dx_O, dy_O, xgrid, ygrid);
probe_scale = gather(sum2(nom)./ sum2(denom));
probe_scale = 0.5*mean(probe_scale) / mean(self.Np_p);
end
if iter >= par.probe_position_search
%% estimate sample shift
[dx_O, denom_dx, dy_O, denom_dy] = Gfun(@get_coefs_shift,xi,P,dx_O, dy_O);
dx = sum2(dx_O)./ sum2(denom_dx);
dy = sum2(dy_O)./ sum2(denom_dy);
end
break
catch ME
warning('Low memory')
if ~any(strcmpi(ME.identifier, low_mem_errs))
rethrow(ME)
end
pause(1)
end
end
if ii == 5
rethrow(ME)
end
if iter < par.probe_position_search
return
end
shift = squeeze(Ggather(cat(4,dx, dy)));
% modified by YJ. allow user to spcify maximum position update
max_shift = min(par.max_pos_update_shift, 10*mad(shift)); %why a factor of 10??
% prevent outliers and too rapid shifts
%max_shift = min(0.1, 10*mad(shift));
shift = min(abs(shift), max_shift) .* sign(shift); % avoid too fast jumps, <0.5px/iter is enough
%old code
%shift = min(abs(shift), 0.2) .* sign(shift); % avoid too fast jumps, <0.5px/iter is enough
pos_update = reshape(shift,[],2);
if ~isfield(cache, 'velocity_map_positions')
cache.velocity_map_positions = zeros(self.Npos,2,'single');
end
if ~isfield(cache, 'position_update_memory')
cache.position_update_memory = {};
end
cache.position_update_memory{iter}(ind,:) = pos_update;
%% USE MOMENTUM ACCELERATION TO MAKE THE CONVERGENCE FASTER
ACC = 0;
if par.probe_position_search_momentum >0
momentum_memory = par.probe_position_search_momentum; %
% only in case far field ptychography
if isinf(self.z_distance) && sum(cellfun(@length, cache.position_update_memory) > 0) > momentum_memory
%corr_level = zeros(momentum_memory,1);
for ii = 1:momentum_memory
corr_level(ii) = mean(diag(corr(cache.position_update_memory{end}(ind,:), cache.position_update_memory{end-ii}(ind,:))));
end
if all(corr_level > 0 )
%estimate optimal friction from previous steps
poly_fit = polyfit(0:momentum_memory,log([1,corr_level]),1);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
gain = 0.5; % smaller -> lower relative speed (less momentum)
friction = 0.1*max(-poly_fit(1),0); % smaller -> longer memory, more momentum
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
gain = 0; friction = 0.5;
end
cache.velocity_map_positions(ind,:) = cache.velocity_map_positions(ind,:)*(1-friction) + pos_update;
% apply the velocity to the refined positions , if the postition updated are sufficiently small
if max(abs(pos_update)) < 0.1
ACC = norm2(pos_update + gain*cache.velocity_map_positions(ind,:)) / norm2(pos_update);
pos_update = pos_update + gain*cache.velocity_map_positions(ind,:);
end
end
end
% try
%ACC = 0;
% momentum_memory = 5; % remember 5 iterations
%
% % only in case far field ptychography
% if isinf(self.z_distance) && sum(cellfun(@length, cache.position_update_memory) > 0) > momentum_memory
% for ii = 1:momentum_memory
% corr_level(ii) = mean(diag(corr(cache.position_update_memory{end}(ind,:), cache.position_update_memory{end-ii}(ind,:))));
% end
% if all(corr_level > 0 )
% %estimate optimal friction from previous steps
% poly_fit = polyfit(0:momentum_memory,log([1,corr_level]),1);
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% gain = 0.5; % smaller -> lower relative speed (less momentum)
% friction = 0.1*max(-poly_fit(1),0); % smaller -> longer memory, more momentum
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% else
% gain = 0; friction = 0.5;
% end
%
% cache.velocity_map_positions(ind,:) = cache.velocity_map_positions(ind,:)*(1-friction) + pos_update;
% % apply the velocity to the refined positions , if the postition updated are sufficiently small
%
% if max(abs(pos_update)) < 0.1
% ACC = norm2(pos_update + gain*cache.velocity_map_positions(ind,:)) / norm2(pos_update);
% pos_update = pos_update + gain*cache.velocity_map_positions(ind,:);
% end
%
% end
% catch
% keyboard
% end
if any(ind==1)
verbose(1,'Grad pos corr -- AVG step %3.3g px , acceleration = %4.1f', max(abs(pos_update(:))), ACC)
end
end
function [nom1, denom1, nom2, denom2] = get_coefs_shift(xi, P, dx_O, dy_O)
dx_OP = dx_O.*P;
nom1 = real(conj(dx_OP) .* xi);
denom1 = abs(dx_OP).^2;
dy_OP = dy_O.*P;
nom2 = real(conj(dy_OP) .* xi);
denom2 = abs(dy_OP).^2;
end
function [nom, denom] = get_coefs_mixed(xi, P, dx_O, dy_O, xgrid, ygrid)
dm_O = dx_O .* xgrid + dy_O .* ygrid;
dm_OP = dm_O.*P;
nom = real(conj(dm_OP) .* xi);
denom = abs(dm_OP).^2;
end
@@ -0,0 +1,153 @@
% GRADIENT_PROJECTION_SOLVER: 1D search for the optimal step in the gradients descent-like methods
% used in ML and PIE functions
%
% [beta_p, beta_o] = gradient_projection_solver(self,xi,O,P,dO,dP,p_ind,par, cache)
%
%
% ** self structure containing inputs: e.g. current reconstruction results, data, mask, positions, pixel size, ..
% ** chi [Nx,Ny,N] array, difference between original and updated exit-wave
% ** dO [Nx,Ny,N] array, object update direction
% ** dP [Nx,Ny,N] array, probe update direction
% ** O [Nx,Ny,N] array, object views
% ** P [Nx,Ny,1] or [Nx,Ny,N] array, single or variable probe
% ** p_ind indices containg corresponding probe id for each processed position
% ** par structure containing parameters for the engines
% ** cache precalculated values
%
% returns:
% ++ beta_p optimal probe step
% ++ beta_o optimal object step
%
% see also: engines.GPU_MS.LSQML, engines.GPU_MS.PIE
% 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 cSAXS
% ptychography MATLAB package 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
% LICENSEEs 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 cSAXS ptychography MATLAB 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 for difference map:
% P. Thibault, M. Dierolf, A. Menzel, O. Bunk, C. David, F. Pfeiffer, High-resolution scanning X-ray diffraction microscopy, Science 321, 379382 (2008).
% (doi: 10.1126/science.1158573),
% for mixed coherent modes:
% P. Thibault and A. Menzel, Reconstructing state mixtures from diffraction measurements, Nature 494, 6871 (2013). (doi: 10.1038/nature11806),
% for LSQ-ML method
% M. Odstrcil, A. Menzel, M.G. Sicairos, Iterative least-squares solver for generalized maximum-likelihood ptychography, Optics Express, 2018
% for OPRP method
% 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." Optics express 24.8 (2016): 8360-8369
% 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, 2908929108 (2016).
% 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 [beta_p, beta_o] = gradient_projection_solver(self,xi,O,P,dO,dP,p_ind,par, cache)
import engines.GPU_MS.GPU_wrapper.*
import utils.*
import math.*
%% initial setting
beta_o = 0;
beta_p = 0;
find_object_step = numel(dO) > 1;
find_probe_step = numel(dP) > 1;
if ~( par.share_probe || length(unique(p_ind)) == 1 ) && find_probe_step
% in case of multiple scans !!
% replicate the update back to the original probe_update size
dP = dP(:,:,p_ind);
end
dP = mean(dP,3);
% !! reusing dP,dO variables to same GPU memory
if find_probe_step && find_object_step
[dP, denom_p, dO, denom_o] = Gfun(@get_coefs,xi, P,O, dP, dO);
elseif find_probe_step
[dP, denom_p] = Gfun(@get_coef,xi, O, dP);
elseif find_object_step
[dO, denom_o] = Gfun(@get_coef,xi, P, dO);
end
if find_probe_step
beta_p = sum2(dP)./ sum2(denom_p) ; % half of the corrections goes to probe , half to object
end
if find_object_step
beta_o = sum2(dO)./ sum2(denom_o) ; % half of the corrections goes to probe , half to object
end
%% allow matlab GPU paralelization to process the sums while positions corrections are calculated
beta_p = Ggather(beta_p).*par.beta_LSQ * par.beta_probe ;
beta_o = Ggather(beta_o).*par.beta_LSQ * par.beta_object;
beta_o(isnan(beta_o)) = 0;
beta_p(isnan(beta_p)) = 0;
if any(isnan(beta_o)) || any(isnan(beta_p))
%keyboard
error('Convergence failed')
end
end
function [nom1, denom1, nom2, denom2] = get_coefs(xi, P,O, dP, dO)
% projection of dPO in direction of xi, to avoid issues caused by
% correlation between dPO and dOP, the step is divided by 2
dPO = dP.*O;
nom1 = 0.5* real(conj(dPO) .* xi);
denom1 = abs(dPO).^2;
PdO = P.*dO;
nom2 = 0.5* real(conj(PdO) .* xi);
denom2 = abs(PdO).^2;
end
function [nom1, denom1] = get_coef(xi, X, dX)
XdX = X.*dX;
nom1 = 0.5*real(conj(XdX) .* xi);
denom1 = abs(XdX).^2;
end
@@ -0,0 +1,67 @@
% function f = local_TV2D_chambolle(f, lambda, niter)
% apply local total variation usiniter matlab functions, it uses chambolle
% solver -> faster but more memory demanding
% Inputs: x - 2D array to be regularized
% lambda - constant to be tuned
% niter - number of iterations
% Modified from PSI's tomo code. Written by Jonathan Schwartz at U. Mich
function x = local_TV2D_chambolle(x,lambda, niter)
[M,N] = size(x);
if lambda == 0
return
end
x0 = x;
xi = zeros(M,N,2, class(x));
tau=1/8;
%%% INNER LOOP
for iinner = 1:niter
% chambolle step
gdv = grad( div(xi) - x/lambda );
%% isotropic
d = sqrt(sum(gdv.^2,3));
xi = bsxfun(@times, xi + tau*gdv, 1 ./ ( 1+tau*d ));
% reconstruct
x = x - lambda*div( xi );
end
% prevent pushing values to zero by the TV regularization
x = sum(x0(:).* x(:)) / sum(x(:).^2) * x;
end
function fd = div(P)
% div - divergence (backward difference)
%
% fd = div(P);
Px = P(:,:,1);
Py = P(:,:,2);
fx = Px-Px([1 1:end-1],:);
fy = Py-Py(:,[1 1:end-1]);
fd = fx+fy;
end
function f = grad(M)
% grad - gradient, forward differences
% g = grad(M);
fx = M([2:end end],:)-M;
fy = M(:,[2:end end])-M;
f = cat(3,fx,fy);
end

Some files were not shown because too many files have changed in this diff Show More