manoharan-lab / holopy

Hologram processing and light scattering in python
GNU General Public License v3.0
134 stars 50 forks source link

Fitting a model to multiple particle hologram #384

Closed sid6155330 closed 3 years ago

sid6155330 commented 3 years ago

Hi,

I am having trouble with fitting a model to a two-particle hologram. The below code works well for a single particle system but gives me an error Parameter center.0 not found in scatterer parameters when I define multiple scatterers.

Thanks, Sid

import holopy as hp from holopy.core.io import get_example_data_path, load_average from holopy.core.process import bg_correct, subimage, normalize from holopy.scattering import Sphere, Spheres, calc_holo from holopy.inference import prior, ExactModel, CmaStrategy, EmceeStrategy, fit

medium_index = 1.50 illum_wavelen = 0.66 illum_polarization = (1,0) detector = hp.detector_grid(shape = 100, spacing = .1)

n1 = prior.Gaussian(1.58, 0.02) sphere_cluster = Spheres([ Sphere(n = n1, r = 0.5, center = [10., 10., 20.]), Sphere(n = n1, r = 0.5, center = [9., 11., 21.])])

data_holo = calc_holo(detector, sphere_cluster, medium_index, illum_wavelen, illum_polarization)

hp.show(data_holo)

now we fit

guess_sphere = Spheres([ Sphere(n = n1, r = 0.5, center = [11., 11., 21.]), Sphere(n = n1, r = 0.5, center = [8., 12., 20.])]) initial_guess = calc_holo(data_holo, guess_sphere)

hp.show(initial_guess)

fit_result = fit(data_holo, guess_sphere, parameters=['x', 'y', 'z', 'n', 'r']) best_fit_hologram = fit_result.hologram best_fit_lnprob = fit_result.max_lnprob best_fit_values = fit_result.parameters print(best_fit_values)

hp.show(best_fit_hologram.real) hp.show(data_holo)

hp.core.math.rsq(best_fit_hologram, data_holo)

barkls commented 3 years ago

Thanks for the issue!

There are a few things going on here: 1) The parameter names you passed in for fitting are ambiguous for a multiparticle system. You should prefix them with the index of the object they correspond to, e.g. parameters=['0:x', '0:y', '0:z', '1:n', '1:r'] would fit the center of the first particle and physical properties of the second. If you want to fit everything you can omit the parameters argument entirely. 2) The fitting landscapes for multiparticle holograms aren't usually as nice as for single particles and they tend to have more fitting parameters, so you usually need a closer initial guess than you use here to get good fits. 3) The most recent version of HoloPy (3.4) doesn't support mixing priors with direct scatterer fitting. To make your code compatible with the most recent version of HoloPy, you will need to either change n1 to a fixed value, or use a Model object to define all the parameters you wish to fit as Prior objects, which would also let you tie the two particles to have the same unknown value for refractive index, which it looks like you intend to do here.

sid6155330 commented 3 years ago

@barkls Thanks for the detailed response. Most importantly, thanks for your second point as I will now design my particle tracking experiments accordingly and will stick to single particle tracking in 3D. I have made the changes that you suggested and the code works well for the HoloPy v3.4. The fitting is pretty good (but the initial guess needs be close to the original values) and the rsq is close to 1.

Thanks, Sid

barkls commented 3 years ago

Glad I could be helpful! Closing this issue, but feel free to open another if something else comes up.