diff --git a/ptypy/core/illumination.py b/ptypy/core/illumination.py index 9ec99d6fc..cfc3f9e1b 100644 --- a/ptypy/core/illumination.py +++ b/ptypy/core/illumination.py @@ -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] @@ -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) @@ -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 @@ -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': @@ -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 diff --git a/ptypy/core/manager.py b/ptypy/core/manager.py index 55250eeca..831cbd63e 100644 --- a/ptypy/core/manager.py +++ b/ptypy/core/manager.py @@ -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):