ptycho / ptypy

Ptypy - main repository
Other
34 stars 14 forks source link

How do I properly load multimode initial probe from file? #98

Open daurer opened 7 years ago

daurer commented 7 years ago

Hi,

I am trying to load an existing multi-probe from a .ptyr file using this code:

# Create parameter object
p.verbose_level = verbose
p.io = u.Param()
p.io.home = base
p.autosave = None

# Create scan object (based on exisiting .ptyd file)
p.scans = u.Param()
p.scans.MF = u.Param()
p.scans.MF.data= u.Param()
p.scans.MF.data.source = base + data % runnr
p.scans.MF.data.dfile = base + './pulse/out.ptyd'

# Create illumination object (based on existing .ptyr file)
p.scans.MF.illumination = u.Param()
p.scans.MF.illumination.model = 'recon'
p.scans.MF.illumination.photons = None
p.scans.MF.illumination.recon = u.Param()
p.scans.MF.illumination.recon.rfile = result.ptyr
p.scans.MF.illumination.recon.layer = None
p.scans.MF.illumination.aperture = u.Param()
p.scans.MF.illumination.aperture.form = "rect"
p.scans.MF.illumination.aperture.diffuser = None
p.scans.MF.illumination.aperture.size = 1e-3
p.scans.MF.illumination.aperture.edge = 2

# Create sample object (based on exisitng .ptyr file) 
p.scans.MF.sample = u.Param()
p.scans.MF.sample.model = 'recon'
p.scans.MF.sample.recon = u.Param()
p.scans.MF.sample.recon.rfile = base + result

# Define illumination properties (number of modes)
p.scans.MF.coherence = u.Param()
p.scans.MF.coherence.num_probe_modes = nmodes
p.scans.MF.coherence.num_object_modes = 1
p.scans.MF.coherence.spectrum = None
p.scans.MF.coherence.object_dispersion = None
p.scans.MF.coherence.probe_dispersion = None

# Create Ptycho object 
P = Ptycho(p,level=2)

What I was expecting is that P.probe.S['S00G00'].data would be the same as the multi-probe stored in f['content']['probe']['S00G00']['data'] of result.ptyr, which is not the case because of some rescaling during the loading procedure:

1) Since I specified p.scans.MF.illumination.photons = None, the parameter photons is set to scan.diff.max_power (line 647 of core/manager.py) which leads to a rescaling by the factor np.sqrt(photons / u.norm2(model)) (line 399 of core/illumination.py) where u.norm2(model) is the full power of all the multi-probes.

2) Even though I did not specify anything about diversity, p.diversity (line 361 in core/illumination.py) is not None, instead it has the default parameters (power=1.0). But since it goes inside u.diversify(...), it leads to a rescaling of (1/sqrt("nr of probe modes")) which happens in line 67 of utils/scripts.py where power=1.0 has been expanded to an array of length "nr of probe modes". Each element of this array is rescaled by power.sum() (which equals "nr of probe modes") and then the multi-probe illumination is rescaled by this array.

I am not sure if what I am trying to do is supposed to work, but it would certainly be nice if I could load a multiprobe reconstruction and have it properly scaled (as it is stored in the .ptyr file). Maybe there is another way of doing this?

Thanks, Benedikt

bjoernenders commented 7 years ago

Hi Benedikt,

thanks for filing this well documented question.

Hope it helps, Bjoern

daurer commented 7 years ago

Hi Bjoern,

thanks for your answer.

However, both solutions are not very intuitive, so you might wanna improve the probe loading or document this particular case.

Thanks again, Benedikt

pierrethibault commented 7 years ago

I'm reopening this to make sure that we don't forget to make the improvements when our model rewrite it complete.

Thanks for pointing that out.

Pierre

pierrethibault commented 6 years ago

(with @bjoernenders and @aaron-parsons) Decision:

bjoernenders commented 6 years ago

We will make side loading objects and probe from previous reconstructions a proper method of Ptycho. Bumped to next release

daurer commented 1 year ago

The first problem raised in the initial issue has been addressed in #417. We should also fix the issues with diversy implementing what has been suggested above.