Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 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 @@ -362,7 +365,7 @@ def init_storage(storage, pars, energy=None, **kwargs):

if p.model is None:
model = np.ones(s.shape, s.dtype)
if p.photons is not None:
if (type(p.photons) is int) or (type(p.photons) is float):
model *= np.sqrt(p.photons) / np.prod(s.shape)
elif type(p.model) is np.ndarray:
model = p.model
Expand All @@ -377,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 @@ -475,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 @@ -1129,18 +1129,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
Loading