Skip to content

Commit

Permalink
unify illumination.photons functionality for model, ptyr and np.array…
Browse files Browse the repository at this point in the history
… probe inits
  • Loading branch information
kahntm committed Jan 8, 2025
1 parent 9a270a0 commit fa2a7fa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
17 changes: 9 additions & 8 deletions ptypy/core/illumination.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,13 @@
userlevel = 0
[photons]
type = int, float, None
default = None
type = int, float, str, None
default = 'maxdiff'
help = Number of photons in the incident illumination
doc = A value specified here will take precedence over calculated statistics from the loaded data.
lowlim = 0
doc = A value specified here will take precedence over calculated statistics from the loaded data. Choices:
- ``None`` : modeled or loaded probe remains unscaled in intensity
- ``int`` or ``float`` : modeled or loaded probe intensity is scaled to that number of photons
- ``'maxdiff'`` : modeled or loaded probe intensity is scaled to match the brightest diffraction pattern
userlevel = 2
[propagation]
Expand Down Expand Up @@ -311,7 +313,8 @@ def init_storage(storage, pars, energy=None, **kwargs):

p = DEFAULT.copy(depth=3)
model = None
if hasattr(pars, 'items') or hasattr(pars, 'items'):

if hasattr(pars, 'items'):
# This is a dict
p.update(pars, in_place_depth=3)

Expand Down Expand Up @@ -366,7 +369,6 @@ def init_storage(storage, pars, energy=None, **kwargs):
model *= np.sqrt(p.photons) / np.prod(s.shape)
elif type(p.model) is np.ndarray:
model = p.model
p.photons = None
elif p.model in resources.probes:
model = resources.probes[p.model](s.shape)
elif str(p.model) == 'recon':
Expand All @@ -378,7 +380,6 @@ def init_storage(storage, pars, energy=None, **kwargs):
'Attempt to load layer `%s` of probe storage with ID `%s` from `%s`'
% (str(layer), str(ID), p.recon.rfile))
model = u.load_from_ptyr(p.recon.rfile, 'probe', ID, layer)
p.photons = None
# This could be more sophisticated,
# i.e. matching the real space grids etc.
elif str(p.model) == 'stxm':
Expand Down Expand Up @@ -476,7 +477,7 @@ def _process(model, aperture_pars=None, prop_pars=None, photons=1e7,
model = prop(model)

# apply photon count
if photons is not None:
if (type(photons) is int) or (type(photons) is float):
model *= np.sqrt(photons / u.norm2(model))

return model
Expand Down
24 changes: 16 additions & 8 deletions ptypy/core/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,26 @@ def _initialize_probe(self, probe_ids):
# Bypass additional tests if input is a string (previous reconstruction)
if illu_pars != str(illu_pars):

# if photon count is None, assign a number from the stats.
phot = illu_pars.get('photons')
phot_max = self.diff.max_power

if phot is None:
if phot == 'maxdiff':
# probe intensity to be scaled to the brightest diffraction pattern
logger.info(
'Found no photon count for probe in parameters.\nUsing photon count %.2e from photon report' % phot_max)
'Probe intensity is being rescaled to match the brightest diffraction pattern.\nUsing photon count %.2e from photon report' % phot_max)
illu_pars['photons'] = phot_max
elif np.abs(np.log10(phot) - np.log10(phot_max)) > 1:
logger.warning(
'Photon count from input parameters (%.2e) differs from statistics (%.2e) by more than a magnitude' % (
phot, phot_max))
elif phot is None:
# probe intensity to remain untouched
pass
elif (type(phot) is int) or (type(phot) is float):
# probe intensity to be scaled to a specific value
if phot < 0:
logger.warning(
f'Given photon count is negative. Using the absolute of the given value: {-1 * phot:.2e}')
phot = -1 * phot
if np.abs(np.log10(phot) - np.log10(phot_max)) > 1:
logger.warning(
'Photon count from input parameters (%.2e) differs from statistics (%.2e) by more than a magnitude' % (
phot, phot_max))

if (self.p.coherence.num_probe_modes > 1) and (type(illu_pars) is not np.ndarray):

Expand Down

0 comments on commit fa2a7fa

Please sign in to comment.