def fit_full_spheroid_model(img, msk, diameter_a, diameter_c, phi, intensity, wavelength, pixel_size, detector_distance, full_output=False, x0=0, y0=0, detector_adu_photon=1., detector_quantum_efficiency=1., material='water', rmax=None, downsampling=1, maxfev=1000, deltab=0.2, do_photon_counting=False,n=1): diameter_a = max(diameter_a, 1.E-9) diameter_c = max(diameter_c, 1.E-9) #intensity = max(intensity,1.) #x0 = min(x0, img.shape[1]) #y0 = min(y0, img.shape[0]) for i in range(n): Xm, Ym, img, msk = _prepare_for_fitting(img, msk, x0, y0, rmax, downsampling, detector_adu_photon, do_photon_counting, pixel_size, detector_distance) size = lambda d: sphere_model_convert_diameter_to_size(d, wavelength, pixel_size, detector_distance) d = lambda da,dc: (da**2*dc)**(1/3.) scaling = lambda i,da,dc: sphere_model_convert_intensity_to_scaling(i, d(da,dc), wavelength, pixel_size, detector_distance, detector_quantum_efficiency, 1, material) I_fit_m = lambda dx,dy,da,dc,p,i: I_spheroid_diffraction(scaling(i,da,dc), Xm-dx, Ym-dy, size(da), size(dc), 0., p) E_fit_m = lambda p: I_fit_m(p[0],p[1],p[2],p[3],p[4],p[5]) - img[msk] p0 = numpy.array([0,0,diameter_a,diameter_c,phi,intensity]) x0_bound = (None, None) y0_bound = (None, None) d_a_bound = ((1-deltab)*diameter_a, (1+deltab)*diameter_a) d_c_bound = ((1-deltab)*diameter_c, (1+deltab)*diameter_c) p_bound = (None, None) i_bound = (None, None) bounds = numpy.array([x0_bound, y0_bound , d_a_bound, d_c_bound, p_bound, i_bound]) p, cov, info, mesg, ier = spimage.leastsqbound(E_fit_m, p0, maxfev=maxfev, xtol=1e-5, full_output=True, bounds=bounds) [dx0, dy0, diameter_a, diameter_c, phi, intensity] = p diameter_a = abs(diameter_a) diameter_c = abs(diameter_c) x0 += dx0 y0 += dy0 phi = phi % numpy.pi if (numpy.isfinite(p) == False).sum() > 0: break # Reduced Chi-squared and standard errors chisquared = (E_fit_m(p)**2).sum()/(img.shape[0]*img.shape[1] - len(p)) #print(cov) if cov is not None: pcov = numpy.diag(cov)*chisquared else: pcov = numpy.array(len(p)*[None]) if full_output: info["error"] = chisquared info["pcov"] = pcov return x0, y0, diameter_a, diameter_c, phi, intensity, info else: return x0, y0, diameter_a, diameter_c, phi, intensity
def fit_full_sphere_model_bg(img, msk, diameter, intensity, wavelength, pixel_size, detector_distance, full_output=False, x0=0, y0=0, detector_adu_photon=1., detector_quantum_efficiency=1., material='water', rmax=None, downsampling=1, maxfev=1000, deltab=0.2, do_photon_counting=False, n=1): diameter = max(diameter, 1.E-9) for i in range(n): Xm, Ym, _img, _msk = _prepare_for_fitting(img, msk, x0, y0, rmax, downsampling, detector_adu_photon, do_photon_counting, pixel_size, detector_distance) Rmc = lambda dx,dy: np.sqrt((Xm - dx)**2 + (Ym - dy)**2) size = lambda d: sphere_model_convert_diameter_to_size(d, wavelength, pixel_size, detector_distance) scaling = lambda i,d: sphere_model_convert_intensity_to_scaling(i, d, wavelength, pixel_size, detector_distance, detector_quantum_efficiency, 1, material) I_fit_m = lambda dx,dy,d,i,b: I_sphere_diffraction(scaling(i,d), Rmc(dx,dy), size(d)) + b E_fit_m = lambda p: I_fit_m(p[0],p[1],p[2],p[3],p[4]) - _img[_msk] p0 = np.array([0.,0.,diameter,intensity]) x0_bound = (-img.shape[1]/2., img.shape[1]/2.) y0_bound = (-img.shape[0]/2., img.shape[1]/2.) d_bound = (diameter-deltab*diameter, diameter+deltab*diameter) i_bound = (None, None) b_bound = (None, None) bounds = np.array([x0_bound, y0_bound , d_bound, i_bound,b_bound]) p, cov, infodict, mesg, ier = spimage.leastsqbound(E_fit_m, np.array([0,0,diameter,intensity,0]), maxfev=maxfev, xtol=1e-5, full_output=True, bounds=bounds) [dx0, dy0, diameter, intensity, bg] = p diameter = abs(diameter) x0 += dx0 y0 += dy0 if (np.isfinite(p) == False).sum() > 0: break # Reduced Chi-squared and standard errors chisquared = (E_fit_m(p)**2).sum()/(_img.shape[0]*_img.shape[1] - len(p)) nmerr = abs( E_fit_m(p) ).sum() / (img.size - len(p)) / abs(img[msk]).sum() if cov is not None: pcov = np.diag(cov)*chisquared else: pcov = np.array(4*[np.nan]) if full_output: infodict["chisquared"] = chisquared infodict["error"] = nmerr infodict["pcov"] = pcov infodict["background"] = bg return x0, y0, diameter, intensity, infodict else: return x0, y0, diameter, intensity
def fit_full_sphere_model(img, msk, diameter, intensity, wavelength, pixel_size, detector_distance, full_output=False, x0=0, y0=0, detector_adu_photon=1., detector_quantum_efficiency=1., material='water', rmax=None, downsampling=1, maxfev=1000, deltab=0.2, do_photon_counting=False, n=1): diameter = max(diameter, 1.E-9) for i in range(n): Xm, Ym, _img, _msk = _prepare_for_fitting(img, msk, x0, y0, rmax, downsampling, detector_adu_photon, do_photon_counting, pixel_size, detector_distance) Rmc = lambda dx,dy: numpy.sqrt((Xm - dx)**2 + (Ym - dy)**2) size = lambda d: sphere_model_convert_diameter_to_size(d, wavelength, pixel_size, detector_distance) scaling = lambda i,d: sphere_model_convert_intensity_to_scaling(i, d, wavelength, pixel_size, detector_distance, detector_quantum_efficiency, 1, material) I_fit_m = lambda dx,dy,d,i: I_sphere_diffraction(scaling(i,d), Rmc(dx,dy), size(d)) E_fit_m = lambda p: I_fit_m(p[0],p[1],p[2],p[3]) - _img[_msk] p0 = numpy.array([0.,0.,diameter,intensity]) #print E_fit_m(p0) x0_bound = (None, None) y0_bound = (None, None) d_bound = (diameter-deltab*diameter, diameter+deltab*diameter) i_bound = (None, None) bounds = numpy.array([x0_bound, y0_bound , d_bound, i_bound]) p, cov, infodict, mesg, ier = spimage.leastsqbound(E_fit_m, numpy.array([x0,y0,diameter,intensity]), maxfev=maxfev, xtol=1e-5, full_output=True, bounds=bounds) [dx0, dy0, diameter, intensity] = p diameter = abs(diameter) x0 += dx0 y0 += dy0 if (numpy.isfinite(p) == False).sum() > 0: break # Reduced Chi-squared and standard errors chisquared = (E_fit_m(p)**2).sum()/(_img.shape[0]*_img.shape[1] - len(p)) if cov is not None: pcov = numpy.diag(cov)*chisquared else: pcov = numpy.array(4*[numpy.nan]) if full_output: infodict["error"] = chisquared infodict["pcov"] = pcov return x0, y0, diameter, intensity, infodict else: return x0, y0, diameter, intensity
def fit_full_spheroid_model(img, msk, diameter_a, diameter_c, phi, intensity, wavelength, pixel_size, detector_distance, full_output=False, x0=0, y0=0, detector_adu_photon=1., detector_quantum_efficiency=1., material='water', rmax=None, downsampling=1, maxfev=1000, deltab=0.2, do_photon_counting=False, n=1): diameter_a = max(diameter_a, 1.E-9) diameter_c = max(diameter_c, 1.E-9) #intensity = max(intensity,1.) #x0 = min(x0, img.shape[1]) #y0 = min(y0, img.shape[0]) for i in range(n): Xm, Ym, img, msk = _prepare_for_fitting(img, msk, x0, y0, rmax, downsampling, detector_adu_photon, do_photon_counting, pixel_size, detector_distance) size = lambda d: sphere_model_convert_diameter_to_size( d, wavelength, pixel_size, detector_distance) d = lambda da, dc: (da**2 * dc)**(1 / 3.) scaling = lambda i, da, dc: sphere_model_convert_intensity_to_scaling( i, d(da, dc), wavelength, pixel_size, detector_distance, detector_quantum_efficiency, 1, material) I_fit_m = lambda dx, dy, da, dc, p, i: I_spheroid_diffraction( scaling(i, da, dc), Xm - dx, Ym - dy, size(da), size(dc), 0., p) E_fit_m = lambda p: I_fit_m(p[0], p[1], p[2], p[3], p[4], p[5]) - img[ msk] p0 = numpy.array([0, 0, diameter_a, diameter_c, phi, intensity]) x0_bound = (None, None) y0_bound = (None, None) d_a_bound = ((1 - deltab) * diameter_a, (1 + deltab) * diameter_a) d_c_bound = ((1 - deltab) * diameter_c, (1 + deltab) * diameter_c) p_bound = (None, None) i_bound = (None, None) bounds = numpy.array( [x0_bound, y0_bound, d_a_bound, d_c_bound, p_bound, i_bound]) p, cov, info, mesg, ier = spimage.leastsqbound(E_fit_m, p0, maxfev=maxfev, xtol=1e-5, full_output=True, bounds=bounds) [dx0, dy0, diameter_a, diameter_c, phi, intensity] = p diameter_a = abs(diameter_a) diameter_c = abs(diameter_c) x0 += dx0 y0 += dy0 phi = phi % numpy.pi if (numpy.isfinite(p) == False).sum() > 0: break # Reduced Chi-squared and standard errors chisquared = (E_fit_m(p)**2).sum() / (img.shape[0] * img.shape[1] - len(p)) #print(cov) if cov is not None: pcov = numpy.diag(cov) * chisquared else: pcov = numpy.array(len(p) * [None]) if full_output: info["error"] = chisquared info["pcov"] = pcov return x0, y0, diameter_a, diameter_c, phi, intensity, info else: return x0, y0, diameter_a, diameter_c, phi, intensity