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,371 @@
%libDetXR prepares and exports data for libDetXR/ptyhon data
% preparation.
%
% ** p p structure
%
% returns:
% ++ p updated p structure
% ++ fdb feedback structure
%
% see also: core.run_data_preparator
% 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 [p, fdb] = libDetXR( p )
import utils.verbose
import io.HDF.save2hdf5
import io.mat2json
import beamline.identify_eaccount
verbose(1, 'Preparing data using json/python data preparation.')
if ~isfield(p, 'delete_temp_json_data')
p.delete_temp_json_data = true;
end
% Parameters for reconstruction with external C code
if ~isfield(p, 'initial_conditions_file') || isempty(p.initial_conditions_file)%&&p.external_reconstruction
if ~isempty(p.suffix)
suffix = ['_' p.suffix];
else
suffix = '';
end
p.initial_conditions_file = [core.generate_scan_name(p) sprintf('_initial_conditions_%03dx%03d%s.h5', p.asize(1), p.asize(2), suffix)];
verbose(3, 'C-code initial_conditions_file = %s', p.initial_conditions_file);
end
% path for saving temp data for C code
if isfield(p, 'initial_conditions_path') && ~isempty(p.initial_conditions_path)
if p.prepare_data_path(1) == '~'
p.initial_conditions_path = ['/sls/X12SA/Data10/' identify_eaccount p.prepare_data_path(9:end)];
end
else
p.initial_conditions_path = p.prepare_data_path;
end
if ~isfield(p, 'initial_probe_temp_file')
p.initial_probe_temp_file{1} = [];
end
if ~isfield(p, 'initial_object_temp_file')
p.initial_object_temp_file{1} = [];
end
if isempty(p.initial_probe_temp_file{1})
for ii=1:p.numscans
p.initial_probe_temp_file{ii} = [core.generate_scan_name(p) sprintf('_temp_probe_S%05d_%03dx%03d.mat', p.scan_number(ii), p.asize(1), p.asize(2))];
end
end
if isempty(p.initial_object_temp_file{1})
for ii=1:p.numscans
p.initial_object_temp_file{ii} = [core.generate_scan_name(p) sprintf('_temp_object_S%05d_%03dx%03d.mat', p.scan_number(ii), p.asize(1), p.asize(2))];
end
end
s=struct;
s.glob = struct;
s.detector = struct;
s.measurement = struct;
s.initialCondition = struct;
s.initialCondition.probe = struct;
s.initialCondition.object = struct;
s.initialCondition.param = struct;
for ii = 1:p.numscans
p.scanID = ii;
cid = sprintf('id%d',ii-1);
% Structure begins
s.glob.dst = {'hdf', fullfile(p.prepare_data_path, p.prepare_data_filename)};
s.glob.energy = p.energy;
s.glob.z = p.z;
s.glob.ds = p.ds;
s.detector.(cid) = struct; % id0 means "detector":{"0":
s.detector.(cid).validMsk = p.detectors(ii).params.mask; % Besides a matlab binary valid mask file
% it accepts a definition based on
% module and/or bad pixels
s.detector.(cid).trfMsk = [0,0]; % Transformation for mask, [rot90,fliplr]
% Mask for pilatus frames apparently not needed. Just datapath.
% raw_data_filenamemask =fullfile(p.raw_data_path_full{1},sprintf('%s%05d_*.%s',p.detector.data_prefix,p.scan_number(ii),p.data_extension));
s.measurement.(cid) = struct;
s.measurement.(cid).detector = ii-1; % NEEDS ADAPTATION FOR SHARING
s.measurement.(cid).probe = p.share_probe_ID(ii)-1;
s.measurement.(cid).object = p.share_object_ID(ii)-1;
% extract h5location and reformat it
h5loc = [];
for det_extraargs = 1:length(p.detectors(ii).params.image_read_extraargs)
if strcmpi(p.detectors(ii).params.image_read_extraargs{det_extraargs}, 'H5Location')
h5loc = p.detectors(ii).params.image_read_extraargs{det_extraargs+1}(2:end-1);
end
end
% libDetXR uses 2 different readers: hdf for single h5 file and
% oldEigerH5 for multiple files
p = p.detectors(ii).params.get_filename(p);
if strcmpi(p.detectors(ii).params.file_extension, 'h5')
if numel(p.detectors(ii).detStorage.files)==1
reader = 'hdf';
else
reader = 'oldEigerH5';
end
else
reader = p.detectors(ii).params.file_extension;
end
% bug fix for reading cbfs
if strcmpi(p.detectors(ii).params.file_extension, 'cbf')
h5loc = 'eh5/images';
end
if numel(p.detectors(ii).detStorage.files)==1 && strcmpi(p.detectors(ii).params.file_extension, 'h5')
fname = dir([p.raw_data_path_full{ii} '*.h5']);
file_list = fullfile(fname.folder, fname.name);
else
file_list = p.raw_data_path_full{ii};
end
s.measurement.(cid).src = {reader, file_list, h5loc};
% calculate transformation
trfData = [0 0];
if p.detectors(ii).params.orientation(1) == 1
trfData = trfData+1;
end
if p.detectors(ii).params.orientation(2) == 1
trfData(2) = trfData(2)+1;
end
if p.detectors(ii).params.orientation(3) == 1
trfData = trfData+1;
trfData(1) = trfData(1)+1;
end
% additional transpose for python data prep
trfData = trfData - 1;
% reduce number of fliplr
trfData(2) = mod(trfData(2),2);
s.measurement.(cid).trfData = trfData;
assert(size(p.ctr,1) >= p.numscans, 'one line of p.ctrl is needed for each scan')
s.measurement.(cid).roi = [p.ctr(ii,2)-1, p.ctr(ii,1)-1, p.asize(1), p.asize(1)];
s.measurement.(cid).pos = round(p.positions(p.scanindexrange(ii,1):p.scanindexrange(ii,2),:));
% Besides a list of points the positions
% can be given as omni_pos dat file and in
% that case it allows a transformation
% matrix.
s.initialCondition.dst = {'hdf', fullfile(p.initial_conditions_path,p.initial_conditions_file)};
%
% Also supports hdf "0": ["hdf", "$INDIR/cSAXS_sxdm_2014_07_omny_commissioning/analysis/S00033/S00033_initial_conditions_400x400.h5","probes/probe_0"],
% focusMdl and pinholeMdl are also
% supported but not tested.
end
for ii=unique(p.share_probe_ID)
cid = sprintf('id%d',ii-1);
% Probe may be propagated or interpolated from initial guess, here we
% have to save it to disk in order to be used by the Json ptycho
% preparator
% The probe is adapted in this code, interpolated, propagated or
% created from a model. In order for the Python data preparer to access
% these changes we save a temporary probe file to disk.
verbose(3, 'Temporary probe file for python data prep = %s', p.initial_probe_temp_file{ii});
temp_probe_full_file = fullfile(p.initial_conditions_path, p.initial_probe_temp_file{ii});
%probe = squeeze(p.probes(:,:,1,:));
probe = p.probes(:,:,ii,:);
probe = permute(probe,[4 1 2 3]); % C-code expects probe mode index first.
save(temp_probe_full_file,'probe');
s.initialCondition.probe.(cid) = {'mat', temp_probe_full_file, 'probe'};
end
for ii=unique(p.share_object_ID)
cid = sprintf('id%d',ii-1);
verbose(3, 'Temporary object file for python data prep = %s', p.initial_object_temp_file{ii});
temp_object_full_file = fullfile(p.initial_conditions_path,p.initial_object_temp_file{ii});
object = p.object{ii};
object = squeeze(permute(object,[3 1 2])); % C-code expects probe mode index first.
save(temp_object_full_file,'object', '-v6');
s.initialCondition.object.(cid) = {'mat', temp_object_full_file, 'object'};
end
% set parameters for c_solver engine; if only data preparation is needed,
% use dummy values
if p.external_engine0
s.initialCondition.param.pfft_relaxation = p.engines{1}.pfft_relaxation;
s.initialCondition.param.probe_regularization = p.engines{1}.probe_regularization;
s.initialCondition.param.probe_radius = p.engines{1}.probe_support_radius;
s.initialCondition.param.diffmap_iterations = p.engines{1}.number_iterations;
s.initialCondition.param.max_mlh_iterations = p.engines{1}.opt_iter;
else
s.initialCondition.param.pfft_relaxation = 0.05;
s.initialCondition.param.probe_regularization = 0.1;
s.initialCondition.param.probe_radius = 0.8;
s.initialCondition.param.diffmap_iterations = 200;
s.initialCondition.param.max_mlh_iterations = 100;
end
% Get a JSON string
t=mat2json(s);
% Write the file
if ~isfield(p, 'json_filename') || isempty(p.json_filename{1})
p.json_filename{1} = [core.generate_scan_name(p) sprintf('_json_template_%03dx%03d.json', p.asize(1), p.asize(2))];
end
verbose(3, 'JSON template filename = %s', p.json_filename{1});
json_fullpath_filename = fullfile(p.initial_conditions_path,p.json_filename{1});
verbose(3,sprintf('Writting to file %s',json_fullpath_filename));
h_json = fopen(json_fullpath_filename,'w');
fprintf(h_json,t);
fclose(h_json);
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Python prepare data %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if ~isfield(p, 'ptychoPrep') || isempty(p.ptychoPrep)
[status hostname] = system('hostname');
fdb.status = core.engine_status(status);
hostname = strsplit(hostname, '-');
switch hostname{1}
case 'ra'
verbose(3, 'Loading python environment for the DaaS cluster.');
p.ptychoPrep = fullfile(p.cSAXS_matlab_path, 'cSAXS_python_env/daas/lib/python2.7/site-packages/libDetXR/procPtycho.py');
case 'x12sa'
verbose(3, 'Loading python environment for the cSAXS beamline.');
p.ptychoPrep = fullfile(p.cSAXS_matlab_path, 'cSAXS_python_env/x12sa/lib/python2.7/site-packages/libDetXR/procPtycho.py');
otherwise
error('Unknown host. Please specify your ptychoPrep or run it on the DaaS / beamline nodes.');
end
end
if verbose > 4
verb_ptychoPrep = ' -v1';
else
verb_ptychoPrep = ' -v0';
end
python_call = ['python ' p.ptychoPrep ' --meta ' json_fullpath_filename verb_ptychoPrep ' --multiproc --cmpr s-zlib4'];
verbose(3,'Calling python prepare data:\n%s', python_call);
for ii = 1:3
try
[status, result] = system(python_call, '-echo');
break
catch ME
warning('Data loading failed with error: %s\n Trying again ', ME.message)
end
end
if status
error('Data loading failed')
end
fdb.status = core.engine_status(status);
if p.delete_temp_json_data
verbose(3,'Removing temporary probe and object files')
for jj=1:length(p.initial_object_temp_file)
if exist([p.initial_conditions_path p.initial_object_temp_file{jj}], 'file')
[status, result] = system(['rm ' p.initial_conditions_path p.initial_object_temp_file{jj}], '-echo');
end
end
for jj=1:length(p.initial_probe_temp_file)
if exist([p.initial_conditions_path p.initial_probe_temp_file{jj}], 'file')
[status, result] = system(['rm ' p.initial_conditions_path p.initial_probe_temp_file{jj}], '-echo');
end
end
else
verbose(2,'Keeping H5 prepared data')
end
verbose(2,'Prepared data: %s', s.glob.dst{2});
verbose(2,'Initial conditions: %s', s.initialCondition.dst{2})
% bug fix for object sharing: procPtycho does not support object sharing,
% thus we have to overwrite the object size in the h5 data file.
if any(p.share_object)
h5_struc = [];
for ii=1:size(p.object_size,1)
h5_struc.objects(:,ii) = uint64(p.object_size(ii,:));
end
save2hdf5(fullfile(p.prepare_data_path, p.prepare_data_filename), h5_struc)
end
end
@@ -0,0 +1,21 @@
%APPLY_CIRCULAR_MASK applies a circular mask to fmask, which will be
%applied to data later
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_aps
function [ p ] = apply_circular_mask( p )
if isfield(p.detector,'circ_mask') && p.detector.circ_mask>0
utils.verbose(2, 'Apply a circular mask (radius=%d pixels) to diffraction patterns',p.detector.circ_mask)
fmask = p.detectors(p.scanID).detStorage.fmask;
circ_mask = utils.make_circular_mask([size(fmask,1),size(fmask,1)], p.detector.circ_mask);
p.detectors(p.scanID).detStorage.fmask = fmask .*circ_mask;
end
end
@@ -0,0 +1,47 @@
%BINNING postprocess functions
% apply binning or upsampling on the measured data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_aps
function [ p ] = binning( p )
data = p.detectors(p.scanID).detStorage.data;
fmask = p.detectors(p.scanID).detStorage.fmask;
%% apply binning or upsampling on the measured data
if isfield(p.detector,'binning')&& p.detector.binning
binning = 2^p.detector.binning;
data = utils.binning_2D(data, binning) * binning^2;
fmask =utils.binning_2D(fmask,binning) == 1; % remove all binned pixel where at least one was masked
end
if isfield(p.detector,'upsampling')&& p.detector.upsampling
upsample = 2^p.detector.upsampling;
utils.verbose(2, 'Upsample diffraction patterns by %d',upsample)
if isfield(p.detector,'upsampling_method') && (strcmp(p.detector.upsampling_method,'bilinear') || strcmp(p.detector.upsampling_method,'bicubic'))
%added by YJ
%disp(p.detector.upsampling_method)
utils.verbose(2, 'Upsample method: imresize with %s interpolation',p.detector.upsampling_method)
data = imresize(data, upsample, p.detector.upsampling_method) / upsample^2;
else
utils.verbose(2, 'Upsample method: utils.unbinning_2D')
data = utils.unbinning_2D(data, upsample) / upsample^2; %PSI method
end
fmask = utils.unbinning_2D(fmask,upsample) == 1; % remove all binned pixel where at least one was masked
end
p.detectors(p.scanID).detStorage.data = data;
p.detectors(p.scanID).detStorage.fmask = fmask;
end
@@ -0,0 +1,84 @@
%CHECK_BURST check first file for burst 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 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 [ p ] = check_burst( p )
import io.image_read
import utils.compile_x12sa_filename
import utils.get_hdr_val
filefirst = compile_x12sa_filename(p.scan_number(1),0,'BasePath',[p.raw_data_path{1} p.(p.detector.name).basepath_dir],'BaseName',p.detector.data_prefix(1:end-2));
x = image_read(filefirst,'IsFmask',1);
if isempty(x.data)
error(['File not found ' filefirst])
end
cutoff = get_hdr_val(x.header{1}, 'Count_cutoff', ' %d', true);
if p.scan.is_cont||~isempty(p.positions_file)
p.(p.detector.name).mask_saturated_value = cutoff;
else
underscore = strfind(filefirst,'_');
point = strfind(filefirst,'.');
x = image_read([filefirst(1:underscore(end)) '*' filefirst(point(end):end)],'IsFmask',1);
if isempty(x.data)
error(['File not found ' [filefirst(1:underscore(end)) '*' filefirst(point(end):end)] ])
end
burst = size(x.data,3);
display(['Detected burst at each point = ' num2str(burst)])
p.(p.detector.name).mask_saturated_value = cutoff*burst;
end
end
@@ -0,0 +1,71 @@
%GET_CENTER Estimate the center of the diffraction pattern
function [ p ] = get_center( p )
import utils.verbose
import io.*
detStorage = p.detectors(p.scanID).detStorage;
ctr = detStorage.ctr;
%{
if verbose>2
verbose(3, 'Loading sample image.')
% read sample image
if p.detectors(p.scanID).params.data_stored % if the data should be loaded from disk
if verbose > 2
[p] = p.detectors(p.scanID).params.get_filename(p);
files = detStorage.files;
if numel(files) == 0
error('Did not find any files.')
end
det.params = p.detectors(p.scanID).params;
sample_file = image_read(files{1}, det.params.image_read_extraargs);
end
end
sz = size(sample_file.data(:,:,1));
f = double(sample_file.data(:,:,1));
% Look for center
[~, cy] = max(sum(f.*detStorage.mask,2));
[~, cx] = max(sum(f.*detStorage.mask,1));
ctr_auto = [cy, cx];
if strcmp(detStorage.check_ctr, 'auto')
ctr = ctr_auto;
verbose(3, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
elseif strcmp(detStorage.check_ctr, 'inter')
imagesc(log(detStorage.f));
[cx,cy] = getpts;
close(gcf);
ctr = round([cy, cx]);
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
else
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
end
else
verbose(2, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
end
%}
verbose(2, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
detStorage.ctr = ctr;
if ~p.prealign_FP
detStorage.lim_inf = ctr-p.asize/2;
detStorage.lim_sup = ctr+p.asize/2-1;
else
detStorage.lim_inf = ctr-p.prealign.asize/2;
detStorage.lim_sup = ctr+p.prealign.asize/2-1;
end
verbose(2, sprintf('Selected region: (''RowFrom'', %d, ''RowTo'', %d, ''ColumnFrom'', %d, ''ColumnTo'', %d)', detStorage.lim_inf(1), detStorage.lim_sup(1), detStorage.lim_inf(2), detStorage.lim_sup(2)));
%{
if verbose>2
if any(detStorage.lim_inf < 1) || any(detStorage.lim_sup > sz)
error('Array size exceeds limit (according to position of center, should be < %d)', max([1-detStorage.lim_inf, detStorage.lim_sup-sz]));
end
end
%}
end
@@ -0,0 +1,96 @@
%GET_MASK Mask saturated values as defined in the detector template
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = get_mask( p )
%% Mask saturated values, create 3D fmask if detector mask is not empty
% get pointers and local references
detStorage = p.detectors(p.scanID).detStorage;
detParams = p.detectors(p.scanID).params;
if ~isempty(detParams.mask_saturated_value) || ~isempty(detParams.mask_below_value)
if size(detStorage.mask,3)==1
detStorage.fmask = repmat(detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2)),[1 1 size(detStorage.data,3)]);
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),1:size(detStorage.data,3));
end
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),:);
end
if ~isempty(detParams.mask_saturated_value)
detStorage.fmask = (detStorage.data < detParams.mask_saturated_value) .* detStorage.fmask;
end
if ~isempty(detParams.mask_below_value)
detStorage.fmask = (detStorage.data > detParams.mask_below_value) .* detStorage.fmask;
end
%% (Fourier ptycho specific option)
if isfield(detParams, 'filt_pinhole') && ~isempty(detParams.filt_pinhole)
detStorage.fmask = detStorage.fmask.*utils.filt2d_pad(800,round(detParams.filt_pinhole*0.9),round(detParams.filt_pinhole*0.9), 'circ');
end
end
@@ -0,0 +1,177 @@
%LOAD_DATA prepare filenames and load data
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
% written by YJ
function [ p ] = load_data( p )
import utils.find_files
import io.image_read
det.params = p.detectors(p.scanID).params;
detStorage = p.detectors(p.scanID).detStorage;
%% prepare filenames
if isempty(detStorage.files)
[p] = det.params.get_filename(p);
if numel(detStorage.files) == 0
error('Did not find any files.')
end
end
utils.verbose(2, 'Loading raw data of scan %05d.', p.scan_number(p.scanID))
files = detStorage.files;
utils.verbose(2, strcat('HDF5 file name:', files{p.scanID}))
%disp(files{p.scanID})
try
data = h5read(files{p.scanID},'/dp');
catch
error('Failed to load dp from %s', files{p.scanID});
end
data = squeeze(data);
utils.verbose(2, strcat('Loaded data from:', files{p.scanID}))
if det.params.orientation(1)
utils.verbose(2, 'Transposing diffraction patterns')
data = permute(data, [2 1 3]);
end
if det.params.orientation(2) && det.params.orientation(3)
utils.verbose(2, 'Flipping diffraction patterns')
data = rot90(data,2); % merge the fliplr and flipup operations
else
if det.params.orientation(2)
data = fliplr(data);
end
if det.params.orientation(3)
data = flipud(data);
end
end
%{
%% select data loading routine
current_version = version;
ver_str = strsplit(current_version, '.');
ver_num = str2double([ver_str{1} '.' ver_str{2} ver_str{3}]);
if ver_num >= 9.4
c_reader = true;
else
utils.verbose(3, 'Fast data reader is not available for Matlab version %d. Switching to image_read.', ver_num)
c_reader = false;
end
if c_reader && ~ismember(det.params.file_extension, {'h5', 'cbf', 'tiff', 'tif'})
utils.verbose(3, 'Fast data reader is not available for selected file format %s. Switching to image_read.', det.params.file_extension)
c_reader = false;
end
%% load data
if c_reader
% use fast data reader
if strcmpi(det.params.file_extension, 'tif')
det.params.file_extension = 'tiff';
end
if strcmpi(det.params.file_extension, 'tiff')
% keep results consistent with matlab's imread and io.image_read for tiff files
det.params.orientation(1) = ~det.params.orientation(1);
end
% convert PtychoShelves center to raw data center
arg.ctr = detStorage.ctr-1;
sz = det.params.geometry.sz;
if det.params.orientation(3)
arg.ctr(1) = round(sz(1) - arg.ctr(1));
end
if det.params.orientation(2)
arg.ctr(2) = round(sz(2) - arg.ctr(2));
end
if det.params.orientation(1)
arg.ctr = fliplr(arg.ctr);
end
if iscolumn(arg.ctr)
arg.ctr = arg.ctr';
end
% flip XY
arg.ctr = fliplr(arg.ctr);
% create structure for c_reader
arg.data_path = files;
arg.nthreads = p.io.data_nthreads;
arg.precision = p.io.data_precision;
arg.extension = det.params.file_extension;
arg.asize = detStorage.read_size;
arg.data_location = detStorage.h5_group;
assert(all(arg.ctr>0),'Raw data center position has to be positive.')
% load data and permute
utils.verbose(2, 'Loading raw data of scan S%05d.', p.scan_number(p.scanID))
data = io.read_measurement(arg);
data = squeeze(data);
size(data)
if det.params.orientation(1)
data = permute(data, [2 1 3]);
end
if det.params.orientation(2) && det.params.orientation(3)
data = rot90(data,2); % merge the fliplr and flipup operations
else
if det.params.orientation(2)
data = fliplr(data);
end
if det.params.orientation(3)
data = flipud(data);
end
end
else
if isempty(detStorage.h5_group)
if numel(files)==1
% one directory contains single file
dataaux = image_read(files{1}, det.params.image_read_extraargs);
data = dataaux.data;
else
% multiple files per directory
dataaux = image_read(files, det.params.image_read_extraargs);
data = dataaux.data;
end
else
data = zeros([p.asize numel(detStorage.h5_group)]);
if numel(files)==1
if numel(detStorage.h5_group)==1
numel(detStorage.h5_group)
% one hdf5 file; one group
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% one hdf5 file; multiple groups
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
else
if numel(detStorage.h5_group)==1
% multiple files, single H5 group
dataaux = image_read(files, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% multiple files, but different H5 group
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{ii}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
end
end
end
%}
detStorage.data = data;
end
@@ -0,0 +1,21 @@
%MASK_GEOMETRY crop mask to fit defined readout geometries
% if no geometry is specified, the full mask is used
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
%
function [ p ] = mask_geometry( p )
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if isfield(p.detectors(scanID).params, 'geometry') && ~isempty(p.detectors(scanID).params.geometry.mask)
detStorage.mask = detStorage.mask(p.detectors(ii).params.geometry.mask{1},p.detectors(ii).params.geometries.mask{2},:);
end
end
@@ -0,0 +1,46 @@
%MATLAB_APS - Matlab data preparator for APS hdf5 data
% Writtend by YJ
function p = matlab_aps(p)
import utils.*
%% call prepare data function
for ii=1:length(p.scan_number)
p.scanID = ii;
[p] = detector.prep_data.matlab_aps.prepare_data(p);
end
%% combine data structures
data = single([]);
fmask = logical([]);
for ii=1:length(p.scan_number)
data = cat(3,data,p.detectors(ii).detStorage.data);
if size(p.detectors(ii).detStorage.fmask,3) > 1
fmask = cat(3,fmask,logical(round(p.detectors(ii).detStorage.fmask)));
else
fmask = cat(3,fmask,repmat(p.detectors(ii).detStorage.fmask,[1 1 size(p.detectors(ii).detStorage.data,3)]));
end
p.detectors(ii).detStorage.fmask = []; % save memory
p.detectors(ii).detStorage.data = [];
end
for ii = 1:p.numobjs
p.object_size(ii,:) = [size(p.object{ii},1),size(p.object{ii},2)];
end
if (isfield(p.detector,'binning')&& p.detector.binning) || (isfield(p.detector,'upsampling')&& p.detector.upsampling)
p = core.apply_binning(p, 2^(p.detector.binning - p.detector.upsampling)); % modify the p structure after binning
end
p = detector.prep_data.matlab_ps.prep_data_matlab(p, data, fmask);
% Compare number of points and diffraction patterns
num_difpat = size(p.fmag,3);
verbose(2, 'Number of probe positions: %d', sum(p.numpts));
verbose(2, 'Number of diffraction patterns : %d', num_difpat);
if num_difpat ~= sum(p.numpts)
error('Number of probe positions (%d) inconsistent with number of diffraction patterns (%d)', sum(p.numpts), num_difpat);
end
end
@@ -0,0 +1,20 @@
%POSTPROCESS postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
function [ p ] = postprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
%% apply some custom correction such as background subtraction on detStorage.data
end
@@ -0,0 +1,165 @@
%PREPARE_DATA_MATLAB prepares and normalizes data for matlab engines
function [ p ] = prep_data_matlab( p, data, fmask )
import utils.pshift
import utils.verbose
import utils.get_option
import math.fftshift_2D
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
% Region of interest [xmin xmax ymin ymax]
% Warning Not implemented for shared scans
% Need to update p.numpts, p.positions, data, fmask, indices
if isfield(p, 'scan') && isfield(p.scan, 'roi') && ~isempty(p.scan.roi)
% Convert to p.positions centered on the object
if p.share_object
positions_centered = p.positions - p.object_size(1,:)/2 + p.asize(:)/2;
else
for ii = 1:p.numscans
positions_centered(p.scanidxs{ii},:) = p.positions(p.scanidxs{ii},:) - p.object_size(ii,:)/2;
end
end
xmin = p.scan.roi(1)/p.dx_spec(2);
xmax = p.scan.roi(2)/p.dx_spec(2);
ymin = p.scan.roi(3)/p.dx_spec(1);
ymax = p.scan.roi(4)/p.dx_spec(1);
% Quick check
if (xmin>xmax)
error('ROI is empty, xmax<xmin')
elseif (ymin>ymax)
error('ROI is empty, ymax<ymin')
end
% Do the comparison and update accordingly [xmin xmax ymin ymax]
whichtokeep = find( (positions_centered(:,1) > ymin) & ...
(positions_centered(:,1)< ymax) & ...
(positions_centered(:,2) > xmin) & ...
(positions_centered(:,2) < xmax) );
% update p.numpts, positions, data, fmask, indices
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Redo a convenient offset
p.positions = p.positions - min(p.positions) + 1;
% Recompute object sizes
p = update_object_size(p);
end
% Skip some data point (for testing reduced dose)
if isfield(p, 'skip_points')
if ~isempty(p.skip_points) && p.skip_points>1
offset = mod(p.scan_number, p.skip_points);
%offset = mod([0 1 2], p.skip_points);
whichtokeep = [1+offset(1):p.skip_points:p.numpts(1)];
for idx = 2:length(p.numpts)
last_idx = sum(p.numpts(1:idx-1));
whichtokeep = [whichtokeep , (last_idx+1+offset(idx)):p.skip_points:(last_idx+p.numpts(idx))];
end
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Recompute object sizes
if p.share_object
p.object_size = p.asize + max(p.positions,[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(1), p.object_size(2));
else
for ii = 1:p.numscans
p.object_size(ii,:) = p.asize + max(p.positions(p.scanindexrange(ii,1):p.scanindexrange(ii,2),:),[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(ii,1), p.object_size(ii,2));
end
end
end
end
% % padding
% if any(datasize ~= p.asize)
% newdata = zeros([p.asize, num_difpat]);
% offset = floor(.5*(p.asize-datasize));
% newdata(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = data;
% data = newdata;
% clear newdata
% newfmask = ones([p.asize, size(fmask,3)]);
% newfmask(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = fmask;
% fmask = newfmask;
% clear newfmask
% end
% 'auto_center_data' option is useful if detector shifts provided in
% template cannot be trusted, ie for more than 2 joined scans
if get_option(p,'auto_center_data') && ~get_option(p,'get_artificial_data')
for i = 1:p.numscans
ind = p.scanidxs{i};
[x,y] = math.center(mean(data(:,:,ind) .* fmask(:,:,ind),3));
verbose(2,'Auto-shifting diffraction patterns by %i %i px', round(x), round(y))
data(:,:,ind) = utils.imshift_fast(data(:,:,ind), x, y, [], 'nearest');
if size(fmask,3) == size(data,3)
fmask(:,:,ind) = utils.imshift_fast(fmask(:,:,ind), x, y, [], 'nearest');
elseif p.numscans == 1
fmask = utils.imshift_fast(fmask, x, y, [], 'nearest');
else
error('Not implemented mask shifting option')
end
end
end
p.fmask_per_scan = ndims(fmask) == 3;
% Prepare Fourier projections
% normalization ignores valid mask - should modify
for ii=1:p.numscans
p.max_sum(ii) = max(sum(sum(data(:,:,p.scanidxs{ii}),1),2),[],3);
end
max_power = max(p.max_sum) / prod(p.asize);
p.renorm = sqrt(1/max_power);
p.Nphot = sum(data(:).*fmask(:)); % Number of photons for regularization normalization (ML optimization)
% store mask pre-fftshifted
p.fmask = fftshift_2D(fmask);
clear fmask
% precalculate modulus of data, normalize and fftshift
p.fmag = fftshift_2D(sqrt(data)) * p.renorm;
end
@@ -0,0 +1,96 @@
%PREPARE_DATA prepare the raw data for the ptychographic reconstruction
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% *Note:*
% All (default) functions are located in +detector/+prep_data/+matlab_ps. If You want
% to add a new detector, please create a new package directory
% (+detector_name) with a parameter file detector_name.m. Functions in a
% package detectory directory will overload similar functions in the default directory.
%
% see also: detector.prep_data.matlab_ps.matlab_ps
%
%
function [ p ] = prepare_data( p )
import utils.*
import io.image_read
verbose(1, 'Preparing data using matlab APS data preparation.')
%% initialize detector
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if size(p.ctr,1) == 1
center = p.ctr; % save centers for all diffraction patterns
else
center = p.ctr(scanID,:); % different centers for different diffraction patterns
end
check_ctr = 'auto';
if ~isempty(center)
check_ctr = 'inter';
if ~strcmp(center, 'inter')
detStorage.ctr = center;
verbose(2,['Using supplied center: ', num2str(detStorage.ctr)]);
check_ctr = 'no';
end
end
detStorage.check_ctr = check_ctr;
%% load mask
if isfield(p.detectors(scanID).params, 'mask') && ~isempty(p.detectors(scanID).params.mask)
load(p.detectors(scanID).params.mask);
detStorage.mask = logical(mask);
elseif p.detectors(scanID).params.data_stored
detStorage.mask = ones(p.detectors(scanID).params.geometry.sz, 'logical');
else
detStorage.mask = logical([]);
end
%% crop mask to readout geometry (if necessary)
[p] = p.detectors(scanID).funcs.mask_geometry(p);
%% get center and readout size
[p] = p.detectors(scanID).funcs.get_center(p);
%% preprocess
[p] = p.detectors(scanID).funcs.preprocess(p);
%% load data
[p] = p.detectors(scanID).funcs.load_data(p);
%% process raw data
[p] = p.detectors(scanID).funcs.process_raw_data(p);
%% mask saturated values
[p] = p.detectors(scanID).funcs.get_mask(p);
%% apply a circular mask to diffraction patterns. Added by YJ.
[p] = p.detectors(scanID).funcs.apply_circular_mask(p);
%% apply binning on the measured data / mask
[p] = p.detectors(scanID).funcs.binning(p);
%% final step postprocessing, e.g. background subtraction
[p] = p.detectors(scanID).funcs.postprocess(p);
end
@@ -0,0 +1,34 @@
%PREPROCESS preprocess functions
% e.g. select area before reading from disk
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
%
function [ p ] = preprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(2);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(2);
if ~p.prealign_FP
detStorage.read_size = p.asize;
else
detStorage.read_size = p.prealign.asize;
end
end
@@ -0,0 +1,31 @@
%PROCESS_RAW_DATA postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
function [ p ] = process_raw_data( p )
detStorage = p.detectors(p.scanID).detStorage;
if p.prealign_FP
p = core.FPM.FP_prealign(p);
detStorage.lim_inf = detStorage.ctr-p.asize/2;
detStorage.lim_sup = detStorage.ctr+p.asize/2-1;
end
% sum up data from burst scans
if p.scan.is_cont
detStorage.data = sum(detStorage.data,4);
else
sz = size(detStorage.data);
detStorage.data = reshape(detStorage.data, sz(1), sz(2), []);
end
end
@@ -0,0 +1,21 @@
%APPLY_CIRCULAR_MASK applies a circular mask to fmask, which will be
%applied to data later
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_aps
function [ p ] = apply_circular_mask( p )
if isfield(p.detector,'circ_mask') && p.detector.circ_mask>0
utils.verbose(2, 'Apply a circular mask (radius=%d pixels) to diffraction patterns',p.detector.circ_mask)
fmask = p.detectors(p.scanID).detStorage.fmask;
circ_mask = utils.make_circular_mask([size(fmask,1),size(fmask,1)], p.detector.circ_mask);
p.detectors(p.scanID).detStorage.fmask = fmask .*circ_mask;
end
end
@@ -0,0 +1,48 @@
%BINNING postprocess functions
% apply binning or upsampling on the measured data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_aps
function [ p ] = binning( p )
data = p.detectors(p.scanID).detStorage.data;
fmask = p.detectors(p.scanID).detStorage.fmask;
%% apply binning or upsampling on the measured data
if isfield(p.detector,'binning')&& p.detector.binning
binning = 2^p.detector.binning;
data = utils.binning_2D(data, binning) * binning^2;
fmask =utils.binning_2D(fmask,binning) == 1; % remove all binned pixel where at least one was masked
end
if isfield(p.detector,'upsampling')&& p.detector.upsampling
upsample = 2^p.detector.upsampling;
utils.verbose(2, 'Upsample diffraction patterns by %d',upsample)
if isfield(p.detector,'upsampling_method') && (strcmp(p.detector.upsampling_method,'bilinear') || strcmp(p.detector.upsampling_method,'bicubic'))
%added by YJ
%disp(p.detector.upsampling_method)
utils.verbose(2, 'Upsample method: imresize with %s interpolation',p.detector.upsampling_method)
data = imresize(data, upsample, p.detector.upsampling_method) / upsample^2;
else
utils.verbose(2, 'Upsample method: utils.unbinning_2D')
data = utils.unbinning_2D(data, upsample) / upsample^2; %PSI method
end
fmask = utils.unbinning_2D(fmask,upsample) == 1; % remove all binned pixel where at least one was masked
end
p.detectors(p.scanID).detStorage.data = data;
p.detectors(p.scanID).detStorage.fmask = fmask;
end
@@ -0,0 +1,84 @@
%CHECK_BURST check first file for burst 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 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 [ p ] = check_burst( p )
import io.image_read
import utils.compile_x12sa_filename
import utils.get_hdr_val
filefirst = compile_x12sa_filename(p.scan_number(1),0,'BasePath',[p.raw_data_path{1} p.(p.detector.name).basepath_dir],'BaseName',p.detector.data_prefix(1:end-2));
x = image_read(filefirst,'IsFmask',1);
if isempty(x.data)
error(['File not found ' filefirst])
end
cutoff = get_hdr_val(x.header{1}, 'Count_cutoff', ' %d', true);
if p.scan.is_cont||~isempty(p.positions_file)
p.(p.detector.name).mask_saturated_value = cutoff;
else
underscore = strfind(filefirst,'_');
point = strfind(filefirst,'.');
x = image_read([filefirst(1:underscore(end)) '*' filefirst(point(end):end)],'IsFmask',1);
if isempty(x.data)
error(['File not found ' [filefirst(1:underscore(end)) '*' filefirst(point(end):end)] ])
end
burst = size(x.data,3);
display(['Detected burst at each point = ' num2str(burst)])
p.(p.detector.name).mask_saturated_value = cutoff*burst;
end
end
@@ -0,0 +1,71 @@
%GET_CENTER Estimate the center of the diffraction pattern
function [ p ] = get_center( p )
import utils.verbose
import io.*
detStorage = p.detectors(p.scanID).detStorage;
ctr = detStorage.ctr;
%{
if verbose>2
verbose(3, 'Loading sample image.')
% read sample image
if p.detectors(p.scanID).params.data_stored % if the data should be loaded from disk
if verbose > 2
[p] = p.detectors(p.scanID).params.get_filename(p);
files = detStorage.files;
if numel(files) == 0
error('Did not find any files.')
end
det.params = p.detectors(p.scanID).params;
sample_file = image_read(files{1}, det.params.image_read_extraargs);
end
end
sz = size(sample_file.data(:,:,1));
f = double(sample_file.data(:,:,1));
% Look for center
[~, cy] = max(sum(f.*detStorage.mask,2));
[~, cx] = max(sum(f.*detStorage.mask,1));
ctr_auto = [cy, cx];
if strcmp(detStorage.check_ctr, 'auto')
ctr = ctr_auto;
verbose(3, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
elseif strcmp(detStorage.check_ctr, 'inter')
imagesc(log(detStorage.f));
[cx,cy] = getpts;
close(gcf);
ctr = round([cy, cx]);
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
else
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
end
else
verbose(2, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
end
%}
verbose(2, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
detStorage.ctr = ctr;
if ~p.prealign_FP
detStorage.lim_inf = ctr-p.asize/2;
detStorage.lim_sup = ctr+p.asize/2-1;
else
detStorage.lim_inf = ctr-p.prealign.asize/2;
detStorage.lim_sup = ctr+p.prealign.asize/2-1;
end
verbose(2, sprintf('Selected region: (''RowFrom'', %d, ''RowTo'', %d, ''ColumnFrom'', %d, ''ColumnTo'', %d)', detStorage.lim_inf(1), detStorage.lim_sup(1), detStorage.lim_inf(2), detStorage.lim_sup(2)));
%{
if verbose>2
if any(detStorage.lim_inf < 1) || any(detStorage.lim_sup > sz)
error('Array size exceeds limit (according to position of center, should be < %d)', max([1-detStorage.lim_inf, detStorage.lim_sup-sz]));
end
end
%}
end
@@ -0,0 +1,96 @@
%GET_MASK Mask saturated values as defined in the detector template
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = get_mask( p )
%% Mask saturated values, create 3D fmask if detector mask is not empty
% get pointers and local references
detStorage = p.detectors(p.scanID).detStorage;
detParams = p.detectors(p.scanID).params;
if ~isempty(detParams.mask_saturated_value) || ~isempty(detParams.mask_below_value)
if size(detStorage.mask,3)==1
detStorage.fmask = repmat(detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2)),[1 1 size(detStorage.data,3)]);
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),1:size(detStorage.data,3));
end
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),:);
end
if ~isempty(detParams.mask_saturated_value)
detStorage.fmask = (detStorage.data < detParams.mask_saturated_value) .* detStorage.fmask;
end
if ~isempty(detParams.mask_below_value)
detStorage.fmask = (detStorage.data > detParams.mask_below_value) .* detStorage.fmask;
end
%% (Fourier ptycho specific option)
if isfield(detParams, 'filt_pinhole') && ~isempty(detParams.filt_pinhole)
detStorage.fmask = detStorage.fmask.*utils.filt2d_pad(800,round(detParams.filt_pinhole*0.9),round(detParams.filt_pinhole*0.9), 'circ');
end
end
@@ -0,0 +1,185 @@
%LOAD_DATA prepare filenames and load data
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
% written by YJ
function [ p ] = load_data( p )
import utils.find_files
import io.image_read
det.params = p.detectors(p.scanID).params;
detStorage = p.detectors(p.scanID).detStorage;
%% prepare filenames
if isempty(detStorage.files)
[p] = det.params.get_filename(p);
if numel(detStorage.files) == 0
error('Did not find any files.')
end
end
utils.verbose(2, 'Loading raw data of scan %05d.', p.scan_number(p.scanID))
files = detStorage.files;
utils.verbose(2, strcat('HDF5 file name:', files{p.scanID}))
%disp(files{p.scanID})
try
data = h5read(files{p.scanID},'/entry/data/eiger_4');
%data = h5read(files{p.scanID},'/entry/data/data');
catch
error('Failed to load dp from %s', files{p.scanID});
end
data = single(squeeze(data));
data = data(detStorage.lim_inf(2):detStorage.lim_sup(2),detStorage.lim_inf(1):detStorage.lim_sup(1),:);
%quick fix for bad detector readouts
data(data<0) = 0;
data(data>1e9) = 0;
utils.verbose(2, strcat('Loaded data from:', files{p.scanID}))
if det.params.orientation(1)
utils.verbose(2, 'Transposing diffraction patterns')
data = permute(data, [2 1 3]);
end
if det.params.orientation(2) && det.params.orientation(3)
utils.verbose(2, 'Flipping diffraction patterns')
data = rot90(data,2); % merge the fliplr and flipup operations
else
if det.params.orientation(2)
data = fliplr(data);
end
if det.params.orientation(3)
data = flipud(data);
end
end
%{
%% select data loading routine
current_version = version;
ver_str = strsplit(current_version, '.');
ver_num = str2double([ver_str{1} '.' ver_str{2} ver_str{3}]);
if ver_num >= 9.4
c_reader = true;
else
utils.verbose(3, 'Fast data reader is not available for Matlab version %d. Switching to image_read.', ver_num)
c_reader = false;
end
if c_reader && ~ismember(det.params.file_extension, {'h5', 'cbf', 'tiff', 'tif'})
utils.verbose(3, 'Fast data reader is not available for selected file format %s. Switching to image_read.', det.params.file_extension)
c_reader = false;
end
%% load data
if c_reader
% use fast data reader
if strcmpi(det.params.file_extension, 'tif')
det.params.file_extension = 'tiff';
end
if strcmpi(det.params.file_extension, 'tiff')
% keep results consistent with matlab's imread and io.image_read for tiff files
det.params.orientation(1) = ~det.params.orientation(1);
end
% convert PtychoShelves center to raw data center
arg.ctr = detStorage.ctr-1;
sz = det.params.geometry.sz;
if det.params.orientation(3)
arg.ctr(1) = round(sz(1) - arg.ctr(1));
end
if det.params.orientation(2)
arg.ctr(2) = round(sz(2) - arg.ctr(2));
end
if det.params.orientation(1)
arg.ctr = fliplr(arg.ctr);
end
if iscolumn(arg.ctr)
arg.ctr = arg.ctr';
end
% flip XY
arg.ctr = fliplr(arg.ctr);
% create structure for c_reader
arg.data_path = files;
arg.nthreads = p.io.data_nthreads;
arg.precision = p.io.data_precision;
arg.extension = det.params.file_extension;
arg.asize = detStorage.read_size;
arg.data_location = detStorage.h5_group;
assert(all(arg.ctr>0),'Raw data center position has to be positive.')
% load data and permute
utils.verbose(2, 'Loading raw data of scan S%05d.', p.scan_number(p.scanID))
data = io.read_measurement(arg);
data = squeeze(data);
size(data)
if det.params.orientation(1)
data = permute(data, [2 1 3]);
end
if det.params.orientation(2) && det.params.orientation(3)
data = rot90(data,2); % merge the fliplr and flipup operations
else
if det.params.orientation(2)
data = fliplr(data);
end
if det.params.orientation(3)
data = flipud(data);
end
end
else
if isempty(detStorage.h5_group)
if numel(files)==1
% one directory contains single file
dataaux = image_read(files{1}, det.params.image_read_extraargs);
data = dataaux.data;
else
% multiple files per directory
dataaux = image_read(files, det.params.image_read_extraargs);
data = dataaux.data;
end
else
data = zeros([p.asize numel(detStorage.h5_group)]);
if numel(files)==1
if numel(detStorage.h5_group)==1
numel(detStorage.h5_group)
% one hdf5 file; one group
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% one hdf5 file; multiple groups
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
else
if numel(detStorage.h5_group)==1
% multiple files, single H5 group
dataaux = image_read(files, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% multiple files, but different H5 group
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{ii}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
end
end
end
%}
detStorage.data = data;
end
@@ -0,0 +1,21 @@
%MASK_GEOMETRY crop mask to fit defined readout geometries
% if no geometry is specified, the full mask is used
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
%
function [ p ] = mask_geometry( p )
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if isfield(p.detectors(scanID).params, 'geometry') && ~isempty(p.detectors(scanID).params.geometry.mask)
detStorage.mask = detStorage.mask(p.detectors(ii).params.geometry.mask{1},p.detectors(ii).params.geometries.mask{2},:);
end
end
@@ -0,0 +1,50 @@
%MATLAB_APS - Matlab data preparator for APS hdf5 data
% Writtend by YJ
function p = matlab_aps_lynx(p)
import utils.*
%% call prepare data function
for ii=1:length(p.scan_number)
p.scanID = ii;
[p] = detector.prep_data.matlab_aps_lynx.prepare_data(p);
end
%% combine data structures
data = single([]);
fmask = logical([]);
for ii=1:length(p.scan_number)
data = cat(3,data,p.detectors(ii).detStorage.data);
if size(p.detectors(ii).detStorage.fmask,3) > 1
fmask = cat(3,fmask,logical(round(p.detectors(ii).detStorage.fmask)));
else
fmask = cat(3,fmask,repmat(p.detectors(ii).detStorage.fmask,[1 1 size(p.detectors(ii).detStorage.data,3)]));
end
p.detectors(ii).detStorage.fmask = []; % save memory
p.detectors(ii).detStorage.data = [];
end
for ii = 1:p.numobjs
p.object_size(ii,:) = [size(p.object{ii},1),size(p.object{ii},2)];
end
if (isfield(p.detector,'binning') && p.detector.binning) || (isfield(p.detector,'upsampling')&& p.detector.upsampling)
p = core.apply_binning(p, 2^(p.detector.binning - p.detector.upsampling)); % modify the p structure after binning
end
p = detector.prep_data.matlab_aps_lynx.prep_data_matlab(p, data, fmask);
% Compare number of points and diffraction patterns
num_difpat = size(p.fmag,3);
verbose(2, 'Number of probe positions: %d', sum(p.numpts));
verbose(2, 'Number of diffraction patterns : %d', num_difpat);
%quick fix for fly-scan data
%only works if # of diffraction patterns >= # of scan points
%{
if num_difpat ~= sum(p.numpts)
error('Number of probe positions (%d) inconsistent with number of diffraction patterns (%d)', sum(p.numpts), num_difpat);
end
%}
end
@@ -0,0 +1,20 @@
%POSTPROCESS postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
function [ p ] = postprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
%% apply some custom correction such as background subtraction on detStorage.data
end
@@ -0,0 +1,163 @@
%PREPARE_DATA_MATLAB prepares and normalizes data for matlab engines
function [ p ] = prep_data_matlab( p, data, fmask )
import utils.pshift
import utils.verbose
import utils.get_option
import math.fftshift_2D
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
% Region of interest [xmin xmax ymin ymax]
% Warning Not implemented for shared scans
% Need to update p.numpts, p.positions, data, fmask, indices
if isfield(p, 'scan') && isfield(p.scan, 'roi') && ~isempty(p.scan.roi)
% Convert to p.positions centered on the object
if p.share_object
positions_centered = p.positions - p.object_size(1,:)/2 + p.asize(:)/2;
else
for ii = 1:p.numscans
positions_centered(p.scanidxs{ii},:) = p.positions(p.scanidxs{ii},:) - p.object_size(ii,:)/2;
end
end
xmin = p.scan.roi(1)/p.dx_spec(2);
xmax = p.scan.roi(2)/p.dx_spec(2);
ymin = p.scan.roi(3)/p.dx_spec(1);
ymax = p.scan.roi(4)/p.dx_spec(1);
% Quick check
if (xmin>xmax)
error('ROI is empty, xmax<xmin')
elseif (ymin>ymax)
error('ROI is empty, ymax<ymin')
end
% Do the comparison and update accordingly [xmin xmax ymin ymax]
whichtokeep = find( (positions_centered(:,1) > ymin) & ...
(positions_centered(:,1)< ymax) & ...
(positions_centered(:,2) > xmin) & ...
(positions_centered(:,2) < xmax) );
% update p.numpts, positions, data, fmask, indices
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Redo a convenient offset
p.positions = p.positions - min(p.positions) + 1;
% Recompute object sizes
p = update_object_size(p);
end
% Skip some data point (for testing reduced dose)
if isfield(p, 'skip_points')
if ~isempty(p.skip_points) && p.skip_points>1
offset = mod(p.scan_number, p.skip_points);
%offset = mod([0 1 2], p.skip_points);
whichtokeep = [1+offset(1):p.skip_points:p.numpts(1)];
for idx = 2:length(p.numpts)
last_idx = sum(p.numpts(1:idx-1));
whichtokeep = [whichtokeep , (last_idx+1+offset(idx)):p.skip_points:(last_idx+p.numpts(idx))];
end
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Recompute object sizes
if p.share_object
p.object_size = p.asize + max(p.positions,[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(1), p.object_size(2));
else
for ii = 1:p.numscans
p.object_size(ii,:) = p.asize + max(p.positions(p.scanindexrange(ii,1):p.scanindexrange(ii,2),:),[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(ii,1), p.object_size(ii,2));
end
end
end
end
% % padding
% if any(datasize ~= p.asize)
% newdata = zeros([p.asize, num_difpat]);
% offset = floor(.5*(p.asize-datasize));
% newdata(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = data;
% data = newdata;
% clear newdata
% newfmask = ones([p.asize, size(fmask,3)]);
% newfmask(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = fmask;
% fmask = newfmask;
% clear newfmask
% end
% 'auto_center_data' option is useful if detector shifts provided in
% template cannot be trusted, ie for more than 2 joined scans
if get_option(p,'auto_center_data') && ~get_option(p,'get_artificial_data')
for i = 1:p.numscans
ind = p.scanidxs{i};
[x,y] = math.center(mean(data(:,:,ind) .* fmask(:,:,ind),3));
verbose(2,'Auto-shifting diffraction patterns by %i %i px', round(x), round(y))
data(:,:,ind) = utils.imshift_fast(data(:,:,ind), x, y, [], 'nearest');
if size(fmask,3) == size(data,3)
fmask(:,:,ind) = utils.imshift_fast(fmask(:,:,ind), x, y, [], 'nearest');
elseif p.numscans == 1
fmask = utils.imshift_fast(fmask, x, y, [], 'nearest');
else
error('Not implemented mask shifting option')
end
end
end
p.fmask_per_scan = ndims(fmask) == 3;
% Prepare Fourier projections
% normalization ignores valid mask - should modify
for ii=1:p.numscans
p.max_sum(ii) = max(sum(sum(data(:,:,p.scanidxs{ii}),1),2),[],3);
end
max_power = max(p.max_sum) / prod(p.asize);
p.renorm = sqrt(1/max_power);
p.Nphot = sum(data(:).*fmask(:)); % Number of photons for regularization normalization (ML optimization)
% store mask pre-fftshifted
p.fmask = fftshift_2D(fmask);
clear fmask
% precalculate modulus of data, normalize and fftshift
p.fmag = fftshift_2D(sqrt(data)) * p.renorm;
end
@@ -0,0 +1,95 @@
%PREPARE_DATA prepare the raw data for the ptychographic reconstruction
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% *Note:*
% All (default) functions are located in +detector/+prep_data/+matlab_ps. If You want
% to add a new detector, please create a new package directory
% (+detector_name) with a parameter file detector_name.m. Functions in a
% package detectory directory will overload similar functions in the default directory.
%
% see also: detector.prep_data.matlab_ps.matlab_ps
%
%
function [ p ] = prepare_data( p )
import utils.*
import io.image_read
verbose(1, 'Preparing data using matlab APS-LYNX data preparation.')
%% initialize detector
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if size(p.ctr,1) == 1
center = p.ctr; % save centers for all diffraction patterns
else
center = p.ctr(scanID,:); % different centers for different diffraction patterns
end
check_ctr = 'auto';
if ~isempty(center)
check_ctr = 'inter';
if ~strcmp(center, 'inter')
detStorage.ctr = center;
verbose(2,['Using supplied center: ', num2str(detStorage.ctr)]);
check_ctr = 'no';
end
end
detStorage.check_ctr = check_ctr;
%% load mask
if isfield(p.detectors(scanID).params, 'mask') && ~isempty(p.detectors(scanID).params.mask)
load(p.detectors(scanID).params.mask);
detStorage.mask = logical(mask);
elseif p.detectors(scanID).params.data_stored
detStorage.mask = ones(p.detectors(scanID).params.geometry.sz, 'logical');
else
detStorage.mask = logical([]);
end
%% crop mask to readout geometry (if necessary)
[p] = p.detectors(scanID).funcs.mask_geometry(p);
%% get center and readout size
[p] = p.detectors(scanID).funcs.get_center(p);
%% preprocess
[p] = p.detectors(scanID).funcs.preprocess(p);
%% load data
[p] = p.detectors(scanID).funcs.load_data(p);
%% process raw data
[p] = p.detectors(scanID).funcs.process_raw_data(p);
%% mask saturated values
[p] = p.detectors(scanID).funcs.get_mask(p);
%% apply a circular mask to diffraction patterns. Added by YJ.
[p] = p.detectors(scanID).funcs.apply_circular_mask(p);
%% apply binning on the measured data / mask
[p] = p.detectors(scanID).funcs.binning(p);
%% final step postprocessing, e.g. background subtraction
[p] = p.detectors(scanID).funcs.postprocess(p);
end
@@ -0,0 +1,34 @@
%PREPROCESS preprocess functions
% e.g. select area before reading from disk
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_data
%
function [ p ] = preprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(2);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(2);
if ~p.prealign_FP
detStorage.read_size = p.asize;
else
detStorage.read_size = p.prealign.asize;
end
end
@@ -0,0 +1,31 @@
%PROCESS_RAW_DATA postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
function [ p ] = process_raw_data( p )
detStorage = p.detectors(p.scanID).detStorage;
if p.prealign_FP
p = core.FPM.FP_prealign(p);
detStorage.lim_inf = detStorage.ctr-p.asize/2;
detStorage.lim_sup = detStorage.ctr+p.asize/2-1;
end
% sum up data from burst scans
if p.scan.is_cont
detStorage.data = sum(detStorage.data,4);
else
sz = size(detStorage.data);
detStorage.data = reshape(detStorage.data, sz(1), sz(2), []);
end
end
@@ -0,0 +1,91 @@
%BINNING postprocess functions
% apply binning or upsampling on the measured data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
% 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 [ p ] = binning( p )
data = p.detectors(p.scanID).detStorage.data;
fmask = p.detectors(p.scanID).detStorage.fmask;
%% apply binning or upsampling on the measured data
if isfield(p.detector,'binning')&& p.detector.binning
binning = 2^p.detector.binning;
data = utils.binning_2D(data, binning) * binning^2;
fmask =utils.binning_2D(fmask,binning) == 1; % remove all binned pixel where at least one was masked
end
if isfield(p.detector,'upsampling')&& p.detector.upsampling
upsample = 2^p.detector.upsampling;
data = utils.unbinning_2D(data, upsample) / upsample^2;
fmask =utils.unbinning_2D(fmask,upsample) == 1; % remove all binned pixel where at least one was masked
end
p.detectors(p.scanID).detStorage.data = data;
p.detectors(p.scanID).detStorage.fmask = fmask;
end
@@ -0,0 +1,84 @@
%CHECK_BURST check first file for burst 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 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 [ p ] = check_burst( p )
import io.image_read
import utils.compile_x12sa_filename
import utils.get_hdr_val
filefirst = compile_x12sa_filename(p.scan_number(1),0,'BasePath',[p.raw_data_path{1} p.(p.detector.name).basepath_dir],'BaseName',p.detector.data_prefix(1:end-2));
x = image_read(filefirst,'IsFmask',1);
if isempty(x.data)
error(['File not found ' filefirst])
end
cutoff = get_hdr_val(x.header{1}, 'Count_cutoff', ' %d', true);
if p.scan.is_cont||~isempty(p.positions_file)
p.(p.detector.name).mask_saturated_value = cutoff;
else
underscore = strfind(filefirst,'_');
point = strfind(filefirst,'.');
x = image_read([filefirst(1:underscore(end)) '*' filefirst(point(end):end)],'IsFmask',1);
if isempty(x.data)
error(['File not found ' [filefirst(1:underscore(end)) '*' filefirst(point(end):end)] ])
end
burst = size(x.data,3);
display(['Detected burst at each point = ' num2str(burst)])
p.(p.detector.name).mask_saturated_value = cutoff*burst;
end
end
@@ -0,0 +1,123 @@
%GET_CENTER Estimate the center of the diffraction pattern
% 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 [ p ] = get_center( p )
import utils.verbose
import io.*
detStorage = p.detectors(p.scanID).detStorage;
ctr = detStorage.ctr;
if verbose>2
verbose(3, 'Loading sample image.')
% read sample image
if p.detectors(p.scanID).params.data_stored % if the data should be loaded from disk
if verbose > 2
[p] = p.detectors(p.scanID).params.get_filename(p);
files = detStorage.files;
if numel(files) == 0
error('Did not find any files.')
end
det.params = p.detectors(p.scanID).params;
sample_file = image_read(files{1}, det.params.image_read_extraargs);
end
end
sz = size(sample_file.data(:,:,1));
f = double(sample_file.data(:,:,1));
% Look for center
[~, cy] = max(sum(f.*detStorage.mask,2));
[~, cx] = max(sum(f.*detStorage.mask,1));
ctr_auto = [cy, cx];
if strcmp(detStorage.check_ctr, 'auto')
ctr = ctr_auto;
verbose(3, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
elseif strcmp(detStorage.check_ctr, 'inter')
imagesc(log(detStorage.f));
[cx,cy] = getpts;
close(gcf);
ctr = round([cy, cx]);
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
else
verbose(3, sprintf('Using center: (%d, %d) - I would have guessed it is (%d, %d)', ctr(1), ctr(2), ctr_auto(1), ctr_auto(2)));
end
else
verbose(2, sprintf('Using center: (%d, %d)', ctr(1), ctr(2)));
end
detStorage.ctr = ctr;
if ~p.prealign_FP
detStorage.lim_inf = ctr-p.asize/2;
detStorage.lim_sup = ctr+p.asize/2-1;
else
detStorage.lim_inf = ctr-p.prealign.asize/2;
detStorage.lim_sup = ctr+p.prealign.asize/2-1;
end
verbose(2, sprintf('Selected region: (''RowFrom'', %d, ''RowTo'', %d, ''ColumnFrom'', %d, ''ColumnTo'', %d)', detStorage.lim_inf(1), detStorage.lim_sup(1), detStorage.lim_inf(2), detStorage.lim_sup(2)));
if verbose>2
if any(detStorage.lim_inf < 1) || any(detStorage.lim_sup > sz)
error('Array size exceeds limit (according to position of center, should be < %d)', max([1-detStorage.lim_inf, detStorage.lim_sup-sz]));
end
end
end
@@ -0,0 +1,96 @@
%GET_MASK Mask saturated values as defined in the detector template
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = get_mask( p )
%% Mask saturated values, create 3D fmask if detector mask is not empty
% get pointers and local references
detStorage = p.detectors(p.scanID).detStorage;
detParams = p.detectors(p.scanID).params;
if ~isempty(detParams.mask_saturated_value) || ~isempty(detParams.mask_below_value)
if size(detStorage.mask,3)==1
detStorage.fmask = repmat(detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2)),[1 1 size(detStorage.data,3)]);
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),1:size(detStorage.data,3));
end
else
detStorage.fmask = detStorage.mask(detStorage.lim_inf(1):detStorage.lim_sup(1), detStorage.lim_inf(2):detStorage.lim_sup(2),:);
end
if ~isempty(detParams.mask_saturated_value)
detStorage.fmask = (detStorage.data < detParams.mask_saturated_value) .* detStorage.fmask;
end
if ~isempty(detParams.mask_below_value)
detStorage.fmask = (detStorage.data > detParams.mask_below_value) .* detStorage.fmask;
end
%% (Fourier ptycho specific option)
if isfield(detParams, 'filt_pinhole') && ~isempty(detParams.filt_pinhole)
detStorage.fmask = detStorage.fmask.*utils.filt2d_pad(800,round(detParams.filt_pinhole*0.9),round(detParams.filt_pinhole*0.9), 'circ');
end
end
@@ -0,0 +1,196 @@
%LOAD_DATA prepare filenames and load data
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = load_data( p )
import utils.find_files
import io.image_read
det.params = p.detectors(p.scanID).params;
detStorage = p.detectors(p.scanID).detStorage;
%% prepare filenames
if isempty(detStorage.files)
[p] = det.params.get_filename(p);
if numel(detStorage.files) == 0
error('Did not find any files.')
end
end
files = detStorage.files;
%% select data loading routine
current_version = version;
ver_str = strsplit(current_version, '.');
ver_num = str2double([ver_str{1} '.' ver_str{2} ver_str{3}]);
if ver_num >= 9.4
c_reader = true;
else
utils.verbose(3, 'Fast data reader is not available for Matlab version %d. Switching to image_read.', ver_num)
c_reader = false;
end
if c_reader && ~ismember(det.params.file_extension, {'h5', 'cbf', 'tiff', 'tif'})
utils.verbose(3, 'Fast data reader is not available for selected file format %s. Switching to image_read.', det.params.file_extension)
c_reader = false;
end
%% load data
if c_reader
% use fast data reader
if strcmpi(det.params.file_extension, 'tif')
det.params.file_extension = 'tiff';
end
if strcmpi(det.params.file_extension, 'tiff')
% keep results consistent with matlab's imread and io.image_read for tiff files
det.params.orientation(1) = ~det.params.orientation(1);
end
% convert PtychoShelves center to raw data center
arg.ctr = detStorage.ctr-1;
sz = det.params.geometry.sz;
if det.params.orientation(3)
arg.ctr(1) = round(sz(1) - arg.ctr(1));
end
if det.params.orientation(2)
arg.ctr(2) = round(sz(2) - arg.ctr(2));
end
if det.params.orientation(1)
arg.ctr = fliplr(arg.ctr);
end
if iscolumn(arg.ctr)
arg.ctr = arg.ctr';
end
% flip XY
arg.ctr = fliplr(arg.ctr);
% create structure for c_reader
arg.data_path = files;
arg.nthreads = p.io.data_nthreads;
arg.precision = p.io.data_precision;
arg.extension = det.params.file_extension;
arg.asize = detStorage.read_size;
arg.data_location = detStorage.h5_group;
assert(all(arg.ctr>0),'Raw data center position has to be positive.')
% load data and permute
utils.verbose(2, 'Loading raw data of scan S%05d.', p.scan_number(p.scanID))
data = io.read_measurement(arg);
data = squeeze(data);
if det.params.orientation(1)
data = permute(data, [2 1 3]);
end
if det.params.orientation(2) && det.params.orientation(3)
data = rot90(data,2); % merge the fliplr and flipup operations
else
if det.params.orientation(2)
data = fliplr(data);
end
if det.params.orientation(3)
data = flipud(data);
end
end
else
if isempty(detStorage.h5_group)
if numel(files)==1
% one directory contains single file
dataaux = image_read(files{1}, det.params.image_read_extraargs);
data = dataaux.data;
else
% multiple files per directory
dataaux = image_read(files, det.params.image_read_extraargs);
data = dataaux.data;
end
else
data = zeros([p.asize numel(detStorage.h5_group)]);
if numel(files)==1
if numel(detStorage.h5_group)==1
% one hdf5 file; one group
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% one hdf5 file; multiple groups
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{1}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
else
if numel(detStorage.h5_group)==1
% multiple files, single H5 group
dataaux = image_read(files, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{1});
data = dataaux.data;
else
% multiple files, but different H5 group
for ii=1:numel(detStorage.h5_group)
dataaux = image_read(files{ii}, det.params.image_read_extraargs{:}, 'H5Location', detStorage.h5_group{ii});
data(:,:,ii) = dataaux.data;
end
end
end
end
end
detStorage.data = data;
end
@@ -0,0 +1,75 @@
%MASK_GEOMETRY crop mask to fit defined readout geometries
% if no geometry is specified, the full mask is used
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = mask_geometry( p )
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if isfield(p.detectors(scanID).params, 'geometry') && ~isempty(p.detectors(scanID).params.geometry.mask)
detStorage.mask = detStorage.mask(p.detectors(ii).params.geometry.mask{1},p.detectors(ii).params.geometries.mask{2},:);
end
end
@@ -0,0 +1,101 @@
%MATLAB_PS - Matlab data preparator
% 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 p = matlab_ps(p)
import utils.*
%% call prepare data function
for ii=1:length(p.scan_number)
p.scanID = ii;
[p] = detector.prep_data.matlab_ps.prepare_data(p);
end
%% combine data structures
data = single([]);
fmask = logical([]);
for ii=1:length(p.scan_number)
data = cat(3,data,p.detectors(ii).detStorage.data);
if size(p.detectors(ii).detStorage.fmask,3) > 1
fmask = cat(3,fmask,logical(round(p.detectors(ii).detStorage.fmask)));
else
fmask = cat(3,fmask,repmat(p.detectors(ii).detStorage.fmask,[1 1 size(p.detectors(ii).detStorage.data,3)]));
end
p.detectors(ii).detStorage.fmask = []; % save memory
p.detectors(ii).detStorage.data = [];
end
for ii = 1:p.numobjs
p.object_size(ii,:) = [size(p.object{ii},1),size(p.object{ii},2)];
end
if (isfield(p.detector,'binning')&& p.detector.binning) || (isfield(p.detector,'upsampling')&& p.detector.upsampling)
p = core.apply_binning(p, 2^(p.detector.binning - p.detector.upsampling)); % modify the p structure after binning
end
p = detector.prep_data.matlab_ps.prep_data_matlab(p, data, fmask);
% Compare number of points and diffraction patterns
num_difpat = size(p.fmag,3);
verbose(3, 'Number of probe positions: %d', sum(p.numpts));
verbose(3, 'Number of diffraction patterns : %d', num_difpat);
if num_difpat ~= sum(p.numpts)
error('Number of probe positions (%d) inconsistent with number of diffraction patterns (%d)', sum(p.numpts), num_difpat);
end
end
@@ -0,0 +1,73 @@
%POSTPROCESS postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
% 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 [ p ] = postprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
%% apply some custom correction such as background subtraction on detStorage.data
end
@@ -0,0 +1,220 @@
%PREPARE_DATA_MATLAB prepares and normalizes data for matlab 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 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 [ p ] = prep_data_matlab( p, data, fmask )
import utils.pshift
import utils.verbose
import utils.get_option
import math.fftshift_2D
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
% Region of interest [xmin xmax ymin ymax]
% Warning Not implemented for shared scans
% Need to update p.numpts, p.positions, data, fmask, indices
if isfield(p, 'scan') && isfield(p.scan, 'roi') && ~isempty(p.scan.roi)
% Convert to p.positions centered on the object
if p.share_object
positions_centered = p.positions - p.object_size(1,:)/2 + p.asize(:)/2;
else
for ii = 1:p.numscans
positions_centered(p.scanidxs{ii},:) = p.positions(p.scanidxs{ii},:) - p.object_size(ii,:)/2;
end
end
xmin = p.scan.roi(1)/p.dx_spec(2);
xmax = p.scan.roi(2)/p.dx_spec(2);
ymin = p.scan.roi(3)/p.dx_spec(1);
ymax = p.scan.roi(4)/p.dx_spec(1);
% Quick check
if (xmin>xmax)
error('ROI is empty, xmax<xmin')
elseif (ymin>ymax)
error('ROI is empty, ymax<ymin')
end
% Do the comparison and update accordingly [xmin xmax ymin ymax]
whichtokeep = find( (positions_centered(:,1) > ymin) & ...
(positions_centered(:,1)< ymax) & ...
(positions_centered(:,2) > xmin) & ...
(positions_centered(:,2) < xmax) );
% update p.numpts, positions, data, fmask, indices
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Redo a convenient offset
p.positions = p.positions - min(p.positions) + 1;
% Recompute object sizes
p = update_object_size(p);
end
% Skip some data point (for testing reduced dose)
if isfield(p, 'skip_points')
if ~isempty(p.skip_points) && p.skip_points>1
offset = mod(p.scan_number, p.skip_points);
%offset = mod([0 1 2], p.skip_points);
whichtokeep = [1+offset(1):p.skip_points:p.numpts(1)];
for idx = 2:length(p.numpts)
last_idx = sum(p.numpts(1:idx-1));
whichtokeep = [whichtokeep , (last_idx+1+offset(idx)):p.skip_points:(last_idx+p.numpts(idx))];
end
data = data(:,:,whichtokeep);
fmask = fmask(:,:,whichtokeep);
p.positions = p.positions(whichtokeep,:);
for ii = 1:p.numscans
% indaux = find(whichtokeep<scanfirstindex(ii+1),'last'); % Finds the last index of scan ii in the new variables
indaux = find((whichtokeep>=scanfirstindex(ii))&(whichtokeep<scanfirstindex(ii+1))); % Finds indices of scan ii in the new variables
if isempty(indaux)
error('ROI: Scan %s has no points in the ROI. Change the ROI or remove this scan from the scan list',p.scan_str{ii})
end
p.numpts(ii) = numel(indaux);
end
scanfirstindex = [1 cumsum(p.numpts)+1]; % First index for scan number
for ii = 1:p.numscans
p.scanindexrange(ii,:) = [scanfirstindex(ii) scanfirstindex(ii+1)-1];
end
verbose(2, 'Region of interest reduced number of points from %d to %d', sum(p.numpts_orig), sum(p.numpts));
% Recompute object sizes
if p.share_object
p.object_size = p.asize + max(p.positions,[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(1), p.object_size(2));
else
for ii = 1:p.numscans
p.object_size(ii,:) = p.asize + max(p.positions(p.scanindexrange(ii,1):p.scanindexrange(ii,2),:),[],1);
verbose(3, 'Computed object size: %d x %d', p.object_size(ii,1), p.object_size(ii,2));
end
end
end
end
% % padding
% if any(datasize ~= p.asize)
% newdata = zeros([p.asize, num_difpat]);
% offset = floor(.5*(p.asize-datasize));
% newdata(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = data;
% data = newdata;
% clear newdata
% newfmask = ones([p.asize, size(fmask,3)]);
% newfmask(offset(1) + (1:datasize(1)), offset(2) + (1:datasize(2)), :) = fmask;
% fmask = newfmask;
% clear newfmask
% end
% 'auto_center_data' option is useful if detector shifts provided in
% template cannot be trusted, ie for more than 2 joined scans
if get_option(p,'auto_center_data') && ~get_option(p,'get_artificial_data')
for i = 1:p.numscans
ind = p.scanidxs{i};
[x,y] = math.center(mean(data(:,:,ind) .* fmask(:,:,ind),3));
verbose(2,'Auto-shifting diffraction patterns by %i %i px', round(x), round(y))
data(:,:,ind) = utils.imshift_fast(data(:,:,ind), x, y, [], 'nearest');
if size(fmask,3) == size(data,3)
fmask(:,:,ind) = utils.imshift_fast(fmask(:,:,ind), x, y, [], 'nearest');
elseif p.numscans == 1
fmask = utils.imshift_fast(fmask, x, y, [], 'nearest');
else
error('Not implemented mask shifting option')
end
end
end
p.fmask_per_scan = ndims(fmask) == 3;
% Prepare Fourier projections
% normalization ignores valid mask - should modify
for ii=1:p.numscans
p.max_sum(ii) = max(sum(sum(data(:,:,p.scanidxs{ii}),1),2),[],3);
end
max_power = max(p.max_sum) / prod(p.asize);
p.renorm = sqrt(1/max_power);
p.Nphot = sum(data(:).*fmask(:)); % Number of photons for regularization normalization (ML optimization)
% store mask pre-fftshifted
p.fmask = fftshift_2D(fmask);
clear fmask
% precalculate modulus of data, normalize and fftshift
p.fmag = fftshift_2D(sqrt(data)) * p.renorm;
end
@@ -0,0 +1,149 @@
%PREPARE_DATA prepare the raw data for the ptychographic reconstruction
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% *Note:*
% All (default) functions are located in +detector/+prep_data/+matlab_ps. If You want
% to add a new detector, please create a new package directory
% (+detector_name) with a parameter file detector_name.m. Functions in a
% package detectory directory will overload similar functions in the default directory.
%
% see also: detector.prep_data.matlab_ps.matlab_ps
%
%
% 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 [ p ] = prepare_data( p )
import utils.*
import io.image_read
verbose(1, 'Preparing data using matlab data preparation.')
%% initialize detector
scanID = p.scanID;
detStorage = p.detectors(scanID).detStorage;
if size(p.ctr,1) == 1
center = p.ctr;
else
center = p.ctr(scanID,:);
end
check_ctr = 'auto';
if ~isempty(center)
check_ctr = 'inter';
if ~strcmp(center, 'inter')
detStorage.ctr = center;
verbose(2,['Using supplied center: ', num2str(detStorage.ctr)]);
check_ctr = 'no';
end
end
detStorage.check_ctr = check_ctr;
%% load mask
if isfield(p.detectors(scanID).params, 'mask') && ~isempty(p.detectors(scanID).params.mask)
load(p.detectors(scanID).params.mask);
detStorage.mask = logical(mask);
elseif p.detectors(scanID).params.data_stored
detStorage.mask = ones(p.detectors(scanID).params.geometry.sz, 'logical');
else
detStorage.mask = logical([]);
end
%% crop mask to readout geometry (if necessary)
[p] = p.detectors(scanID).funcs.mask_geometry(p);
%% get center and readout size
[p] = p.detectors(scanID).funcs.get_center(p);
%% preprocess
[p] = p.detectors(scanID).funcs.preprocess(p);
%% load data
[p] = p.detectors(scanID).funcs.load_data(p);
%% process raw data
[p] = p.detectors(scanID).funcs.process_raw_data(p);
%% mask saturated values
[p] = p.detectors(scanID).funcs.get_mask(p);
%% apply binning on the measured data / mask
[p] = p.detectors(scanID).funcs.binning(p);
%% final step postprocessing, e.g. background subtraction
[p] = p.detectors(scanID).funcs.postprocess(p);
end
@@ -0,0 +1,88 @@
%PREPROCESS preprocess functions
% e.g. select area before reading from disk
%
% ** p p structure
%
% returns:
% ++ p p structure
%
% see also: detector.prep_data.matlab_ps.prepare_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 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 [ p ] = preprocess( p )
detStorage = p.detectors(p.scanID).detStorage;
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'RowTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(1);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnFrom';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_inf(2);
p.detectors(p.scanID).params.image_read_extraargs{end+1} = 'ColumnTo';
p.detectors(p.scanID).params.image_read_extraargs{end+1} = detStorage.lim_sup(2);
if ~p.prealign_FP
detStorage.read_size = p.asize;
else
detStorage.read_size = p.prealign.asize;
end
end
@@ -0,0 +1,84 @@
%PROCESS_RAW_DATA postprocess functions
% applied to the 4D raw data
%
% ** p p structure
% returns:
% ++ p updated p structure
%
% see also: detector.prep_data.matlab_ps.matlab_ps
% 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 [ p ] = process_raw_data( p )
detStorage = p.detectors(p.scanID).detStorage;
if p.prealign_FP
p = core.FPM.FP_prealign(p);
detStorage.lim_inf = detStorage.ctr-p.asize/2;
detStorage.lim_sup = detStorage.ctr+p.asize/2-1;
end
% sum up data from burst scans
if p.scan.is_cont
detStorage.data = sum(detStorage.data,4);
else
sz = size(detStorage.data);
detStorage.data = reshape(detStorage.data, sz(1), sz(2), []);
end
end
@@ -0,0 +1,65 @@
%GET_DIR_PATH define directory structure.
% default scheme for data (scan 68) taken at cSAXS: S00000-00999/S00068/
% 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 data_path = get_dir_path(p, ii)
data_path = [p.raw_data_path{ii} p.detectors(ii).params.basepath_dir p.detectors(ii).params.read_path(p.scan_number(ii))];
end
@@ -0,0 +1,141 @@
%UPDATE_DEFAULTS update parameters with default values that are crucial for
% a further processing of the data and look for overloaded functions
% 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 [ p ] = update_defaults( p )
import utils.verbose
%% update fields
p = check_fields(p, 'mask_saturated_value', []);
p = check_fields(p, 'mask_below_value', []);
p = check_fields(p, 'read_path_format', 'S%05d-%05d/S%05d/');
%% fill the image_read_extraargs list with default parameters for image_read function
for ii = 1:length(p.detectors)
p.detectors(ii).params.image_read_extraargs(end+(1:6)) =...
{'IsFmask', false, ...
'Orientation', p.detectors(ii).params.orientation, ...
'OrientByExtension', 0};
end
% check if a different detector mask has been specified in template_ptycho
if ~isempty(p.io.default_mask_file)
p.detectors(p.detID).params.mask = p.io.default_mask_file;
end
%% update all default functions
func_path = fullfile(p.ptycho_matlab_path, '+detector', '+prep_data');
if ~exist(func_path, 'dir')
error('Could not find detector functions in %s', func_path)
end
priv = dir([func_path]);
for ii=1:size(priv,1)
if priv(ii).name(1)=='.'
continue
end
if priv(ii).isdir
%subdir search
if strcmpi(priv(ii).name, ['+' p.prepare.data_preparator])
func_path_sub = fullfile(p.ptycho_matlab_path, '+detector', '+prep_data', ['+' p.prepare.data_preparator]);
priv_sub = dir([func_path_sub]);
for jj=1:size(priv_sub,1)
[~, fname, ext] = fileparts(fullfile(priv_sub(jj).folder, priv_sub(jj).name));
if strcmpi(ext, '.m')
p = detector.prep_data.update_funcs(p, fname, ext);
end
end
else
end
else
[~, fname, ext] = fileparts(fullfile(priv(ii).folder, priv(ii).name));
if strcmpi(ext, '.m')
p = detector.prep_data.update_funcs(p, fname, ext);
end
end
end
end
function [p] = check_fields(p, field, val, varargin)
import utils.verbose
if ~isfield(p.detectors(p.detID).params,field)
verbose(3, [field ' not defined for ' p.detectors(p.detID).name '. Using default value.'])
p.detectors(p.detID).params.(field) = val;
end
if nargin > 3
is_func = varargin{1};
else
is_func = 0;
end
if is_func
p = update_funcs(p, field);
end
end
@@ -0,0 +1,72 @@
%UPDATE_FUNCS Update prepare data functions with overloaded functions from
%the detector directory
% 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 p = update_funcs( p, func, ext )
if exist(fullfile(p.ptycho_matlab_path, '+detector', ['+' p.detector.name], [func ext]), 'file')
p.detectors(p.detID).funcs.(func) = str2func(['detector.' p.detector.name '.' func]);
else
if exist(fullfile(p.ptycho_matlab_path, '+detector', '+prep_data', [func ext]), 'file')
p.detectors(p.detID).funcs.(func) = str2func(['detector.prep_data.' func]);
else
p.detectors(p.detID).funcs.(func) = str2func(['detector.prep_data.' p.prepare.data_preparator '.' func]);
end
end
end