manoharan-lab / holopy

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

Issue with Fitting Models to Data: Size and Refractive Index #442

Open BnM-BnM opened 1 month ago

BnM-BnM commented 1 month ago

Hello,

Thank you for providing this great code! I am encountering an issue with the "Fitting Models to Data" part. When I implement my experiment image in the code and ask it to find the size and refractive index, it only returns the midpoint of the range I defined for these parameters. I know that the size of the particle is 10 micrometres (diameter).

Could you please help me identify what might be causing this issue and how I can correct it?

Thank you in advance for your assistance. here is the code I am using:

import holopy as hp import numpy as np from holopy.core.process import normalize from holopy.scattering import Sphere, calc_holo from holopy.scattering.theory import Mie from holopy.inference import prior, EmceeStrategy, ExactModel import multiprocessing import matplotlib.pyplot as plt import corner import time import logging

Configure logging

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(message)s')

def process_image(imagepath, pix_size, med_n, wl, pol, noise_sd, result_filename): logging.info("Loading and normalizing image...") data_holo = normalize(hp.load_image(imagepath, pix_size, med_n, wl, pol, channel=0)) logging.info("Image loaded and normalized.") hp.show(data_holo)

logging.info("Defining parameterized sphere to fit...")
r = prior.BoundedGaussian(6.5, 0.01, 4, 9)  # Restricting r to be between 9 and 10
n = prior.ComplexPrior(real=prior.BoundedGaussian(1.6, 0.02, 1.5, 1.7), imag=1e-4)  # Restricting n.real to be between 1.55 and 1.6

par_sphere = Sphere(n=n, r=r, center=[25, 22, 2500])
theory = Mie(False, False)  # Use far-field Mie theory
model = ExactModel(scatterer=par_sphere, calc_func=calc_holo, noise_sd=noise_sd, theory=theory)

logging.info("Calculating initial hologram guess...")
initial_guess_holo = calc_holo(data_holo, par_sphere, medium_index=med_n, illum_wavelen=wl, illum_polarization=pol, theory=theory)
hp.show(initial_guess_holo)
logging.info("Initial hologram guess calculated.")

logging.info("Performing model inference...")
strategy = EmceeStrategy(nwalkers=150)
strategy.steps = 5000  # Number of steps
strategy.burn_in = 4500

sampling_result = hp.inference.sample(data_holo, model, strategy)
logging.info("Model inference completed.")

samples = sampling_result.samples
n_samples = samples.sel(parameter='n.real').values
r_samples = samples.sel(parameter='r').values

logging.info("Sampled parameters:")
logging.info(f"n.real samples: {n_samples}")
logging.info(f"r samples: {r_samples}")

logging.info("Visualizing and saving results...")
inferred_parameters = sampling_result.parameters
fit_sphere = par_sphere.from_parameters(inferred_parameters)
fit_holo = calc_holo(data_holo, fit_sphere, medium_index=med_n, illum_wavelen=wl, illum_polarization=pol, theory=theory)
hp.show(fit_holo)

hp.save(result_filename, sampling_result)
logging.info(f"Results saved to {result_filename}.")

def main(): start_time = time.time() logging.info("Setting up parameters...") pix_size = 7 # Image pixel spacing noise_sd = 0.5 # Image noise level med_n = 1.33 # Medium Refractive index wl = 0.460 # Wavelength pol = (1, 0) # Polarization

imagepath = r'10um_7umsens.png'

logging.info("Starting image processing with multiprocessing...")
result_filename = 'inference_result_Beads5.h5'
with multiprocessing.Pool(4) as pool:  # Adjust the number of processes if needed
    pool.apply(process_image, (imagepath, pix_size, med_n, wl, pol, noise_sd, result_filename))
logging.info("Image processing completed.")

end_time = time.time()
logging.info(f"Processing time: {end_time - start_time} seconds")

if name == 'main': multiprocessing.freeze_support() main()

Picture1