diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1567443c1..50a890caa 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -51,7 +51,7 @@ jobs: miniforge-version: latest - name: Install ${{ matrix.conda-env }} dependencies run: | - sed -i 's/python/python=${{ matrix.python-version }}/' dependencies_${{ matrix.conda-env }}.yml + sed -i '/- python/d' dependencies_${{ matrix.conda-env }}.yml conda install -c conda-forge mpich conda env update --file dependencies_${{ matrix.conda-env }}.yml --name ptypy_env conda install -c conda-forge flake8 pytest pytest-cov diff --git a/ptypy/core/illumination.py b/ptypy/core/illumination.py index ddf1f98f7..9ec99d6fc 100644 --- a/ptypy/core/illumination.py +++ b/ptypy/core/illumination.py @@ -409,7 +409,7 @@ def init_storage(storage, pars, energy=None, **kwargs): # Ok this is nearfield curve = 1.0 - model = pod.bw(curve * np.sqrt(alldiff)) + model = pod.bw(curve * np.sqrt(alldiff).astype(np.complex128)) else: raise ValueError( prefix + 'Value to `model` key not understood in probe creation') diff --git a/ptypy/experiment/hdf5_loader.py b/ptypy/experiment/hdf5_loader.py index 72846e734..21f2c75b0 100644 --- a/ptypy/experiment/hdf5_loader.py +++ b/ptypy/experiment/hdf5_loader.py @@ -320,6 +320,12 @@ class Hdf5Loader(PtyScan): type = list, ndarray help = This is the array or list with the re-ordered indices. + [nearfield_defocus] + default = None + type = float + help = Distance from sample to focus (for nearfield only) + doc = If set, magnification will be calculated automatically and applied to detector distance and pixelsize + """ def __init__(self, pars=None, swmr=False, **kwargs): @@ -378,6 +384,10 @@ def __init__(self, pars=None, swmr=False, **kwargs): if self.p.electron_data: self.meta.energy = u.m2keV(u.electron_wavelength(self.meta.energy)) + # For nearfield data, manipulate distance and psize + if self.p.nearfield_defocus: + self._prepare_nearfield() + # it's much better to have this logic here than in load! if (self._ismapped and (self._scantype == 'arb')): log(3, "This scan looks to be a mapped arbitrary trajectory scan.") @@ -590,6 +600,20 @@ def _prepare_meta_info(self): assert self.pad.size == 4, "self.p.padding needs to of size 4" log(3, "Padding the detector frames by {}".format(self.p.padding)) + def _prepare_nearfield(self): + """ + Calculate magnification and modify distance and psize + """ + defocus = self.p.nearfield_defocus + mag = self.meta.distance / defocus + dist_eff = (self.meta.distance - defocus) / mag + psize_eff = self.info.psize / mag + log(3, f"Nearfield: With defocus {defocus} m the magmification is {mag}") + log(3, f"Nearfield: The effective detector distance is {dist_eff}") + log(3, f"Nearfield: The effective pixel size is {psize_eff}") + self.meta.distance = dist_eff + self.info.psize = psize_eff + def _prepare_center(self): """ define how data should be loaded (center, cropping) diff --git a/ptypy/utils/scripts.py b/ptypy/utils/scripts.py index f438bd226..1c6631725 100644 --- a/ptypy/utils/scripts.py +++ b/ptypy/utils/scripts.py @@ -621,7 +621,7 @@ def stxm_analysis(storage, probe=None): pod = list(v.pods.values())[0] if not pod.active: continue - t = pod.diff.sum() + t = (pod.diff * pod.mask).sum() if t > t2: t2=t ss = v.slice @@ -629,7 +629,7 @@ def stxm_analysis(storage, probe=None): # slice(v.roi[0,0], v.roi[1,0]), # slice(v.roi[0,1], v.roi[1,1])) # bufview = buf[ss] - m = mass_center(pod.diff) # + 1. + m = mass_center(pod.diff * pod.mask) # + 1. q = pod.di_view.storage._to_phys(m) dpc_row[ss] += q[0] * v.psize[0] * pr * 2 * np.pi / pod.geometry.lz dpc_col[ss] += q[1] * v.psize[1] * pr * 2 * np.pi / pod.geometry.lz @@ -723,7 +723,7 @@ def phase_from_dpc(dpc_row, dpc_col): """ py =- dpc_row - px =- dpc_col + px =+ dpc_col sh = px.shape sh = np.asarray(sh) fac = np.ones_like(sh)