You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

912 lines
34 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. import numpy as np
  2. from uncertainties import ufloat
  3. import lmfit
  4. from lmfit.models import (ConstantModel, ComplexConstantModel, LinearModel, QuadraticModel,
  5. PolynomialModel, SineModel, GaussianModel, Gaussian2dModel, LorentzianModel,
  6. SplitLorentzianModel, VoigtModel, PseudoVoigtModel, MoffatModel,
  7. Pearson7Model, StudentsTModel, BreitWignerModel, LognormalModel,
  8. DampedOscillatorModel, ExponentialGaussianModel, SkewedGaussianModel,
  9. SkewedVoigtModel, ThermalDistributionModel, DoniachModel, PowerLawModel,
  10. ExponentialModel, StepModel, RectangleModel, ExpressionModel, DampedHarmonicOscillatorModel)
  11. from lmfit.models import (guess_from_peak, guess_from_peak2d, fwhm_expr, height_expr,
  12. update_param_vals)
  13. from lmfit.lineshapes import (not_zero, breit_wigner, damped_oscillator, dho, doniach,
  14. expgaussian, exponential, gaussian, gaussian2d,
  15. linear, lognormal, lorentzian, moffat, parabolic,
  16. pearson7, powerlaw, pvoigt, rectangle, sine,
  17. skewed_gaussian, skewed_voigt, split_lorentzian, step,
  18. students_t, thermal_distribution, tiny, voigt)
  19. from lmfit import Model
  20. import numpy as np
  21. from numpy import (arctan, copysign, cos, exp, isclose, isnan, log, pi, real,
  22. sin, sqrt, where)
  23. from scipy.special import erf, erfc
  24. from scipy.special import gamma as gamfcn
  25. from scipy.special import wofz
  26. from scipy.optimize import curve_fit
  27. import xarray as xr
  28. log2 = log(2)
  29. s2pi = sqrt(2*pi)
  30. s2 = sqrt(2.0)
  31. def gaussianWithOffset(x, amplitude=1.0, center=0.0, sigma=1.0, offset=0.0):
  32. """Return a 1-dimensional Gaussian function with an offset.
  33. gaussian(x, amplitude, center, sigma) =
  34. (amplitude/(s2pi*sigma)) * exp(-(1.0*x-center)**2 / (2*sigma**2))
  35. """
  36. return ((amplitude/(max(tiny, s2pi*sigma)))
  37. * exp(-(1.0*x-center)**2 / max(tiny, (2*sigma**2))) + offset)
  38. def lorentzianWithOffset(x, amplitude=1.0, center=0.0, sigma=1.0, offset=0.0):
  39. return ((amplitude/(1 + ((1.0*x-center)/max(tiny, sigma))**2))
  40. / max(tiny, (pi*sigma)) + offset)
  41. def exponentialWithOffset(x, amplitude=1.0, decay=1.0, offset=0.0):
  42. decay = not_zero(decay)
  43. return amplitude * exp(-x/decay) + offset
  44. def expansion(x, amplitude=1.0, offset=0.0):
  45. return np.sqrt(amplitude*x*x + offset)
  46. def dampingOscillation(x, center=0, amplitude=1.0, frequency=1.0, decay=1.0, offset=0.0):
  47. return amplitude * np.exp(-decay*x)*np.sin(2*np.pi*frequency*(x-center)) + offset
  48. def two_gaussian2d(x, y=0.0, A_amplitude=1.0, A_centerx=0.0, A_centery=0.0, A_sigmax=1.0,
  49. A_sigmay=1.0, B_amplitude=1.0, B_centerx=0.0, B_centery=0.0, B_sigmax=1.0,
  50. B_sigmay=1.0):
  51. """Return a 2-dimensional Gaussian function.
  52. gaussian2d(x, y, amplitude, centerx, centery, sigmax, sigmay) =
  53. amplitude/(2*pi*sigmax*sigmay) * exp(-(x-centerx)**2/(2*sigmax**2)
  54. -(y-centery)**2/(2*sigmay**2))
  55. """
  56. z = A_amplitude*(gaussian(x, amplitude=1, center=A_centerx, sigma=A_sigmax) *
  57. gaussian(y, amplitude=1, center=A_centery, sigma=A_sigmay))
  58. z += B_amplitude*(gaussian(x, amplitude=1, center=B_centerx, sigma=B_sigmax) *
  59. gaussian(y, amplitude=1, center=B_centery, sigma=B_sigmay))
  60. return z
  61. def ThomasFermi_2d(x, y=0.0, centerx=0.0, centery=0.0, amplitude=1.0, sigmax=1.0, sigmay=1.0):
  62. res = (1- ((x-centerx)/(sigmax))**2 - ((y-centery)/(sigmay))**2)**(3 / 2)
  63. return amplitude * 5 / 2 / np.pi / max(tiny, sigmax * sigmay) * np.where(res > 0, res, 0)
  64. def polylog(power, numerator):
  65. dataShape = numerator.shape
  66. numerator = np.tile(numerator, (20, 1))
  67. denominator = np.arange(1, 21)
  68. denominator = np.tile(denominator, (dataShape[0], 1))
  69. denominator = denominator.T
  70. data = numerator / denominator
  71. return np.sum(np.power(data, power), axis=0)
  72. def polylog2_2d(x, y=0.0, centerx=0.0, centery=0.0, amplitude=1.0, sigmax=1.0, sigmay=1.0):
  73. ## Approximation of the polylog function with 2D gaussian as argument. -> discribes the thermal part of the cloud
  74. return amplitude / np.pi / 1.59843 / max(tiny, sigmax * sigmay) * polylog(2, np.exp( -((x-centerx)**2/(2 * (sigmax)**2))-((y-centery)**2/( 2 * (sigmay)**2)) ))
  75. def density_profile_BEC_2d(x, y=0.0, BEC_amplitude=1.0, thermal_amplitude=1.0, BEC_centerx=0.0, BEC_centery=0.0, thermal_centerx=0.0, thermal_centery=0.0,
  76. BEC_sigmax=1.0, BEC_sigmay=1.0, thermal_sigmax=1.0, thermal_sigmay=1.0):
  77. return ThomasFermi_2d(x=x, y=y, centerx=BEC_centerx, centery=BEC_centery,
  78. amplitude=BEC_amplitude, sigmax=BEC_sigmax, sigmay=BEC_sigmay
  79. ) + polylog2_2d(x=x, y=y, centerx=thermal_centerx, centery=thermal_centery,
  80. amplitude=thermal_amplitude, sigmax=thermal_sigmax, sigmay=thermal_sigmay)
  81. class GaussianWithOffsetModel(Model):
  82. fwhm_factor = 2*np.sqrt(2*np.log(2))
  83. height_factor = 1./np.sqrt(2*np.pi)
  84. def __init__(self, independent_vars=['x'], nan_policy='raise', prefix='', name=None, **kwargs):
  85. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  86. 'independent_vars': independent_vars})
  87. super().__init__(gaussianWithOffset, **kwargs)
  88. self._set_paramhints_prefix()
  89. def _set_paramhints_prefix(self):
  90. self.set_param_hint('sigma', min=0)
  91. self.set_param_hint('fwhm', expr=fwhm_expr(self))
  92. self.set_param_hint('height', expr=height_expr(self))
  93. def guess(self, data, x, negative=False, **kwargs):
  94. offset = np.min(data)
  95. data = data - offset
  96. pars = guess_from_peak(self, data, x, negative)
  97. pars.add('offset', value=offset)
  98. return update_param_vals(pars, self.prefix, **kwargs)
  99. class LorentzianWithOffsetModel(Model):
  100. fwhm_factor = 2.0
  101. height_factor = 1./np.pi
  102. def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
  103. **kwargs):
  104. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  105. 'independent_vars': independent_vars})
  106. super().__init__(lorentzianWithOffset, **kwargs)
  107. self._set_paramhints_prefix()
  108. def _set_paramhints_prefix(self):
  109. self.set_param_hint('sigma', min=0)
  110. self.set_param_hint('fwhm', expr=fwhm_expr(self))
  111. self.set_param_hint('height', expr=height_expr(self))
  112. def guess(self, data, x, negative=False, **kwargs):
  113. """Estimate initial model parameter values from data."""
  114. offset = np.min(data)
  115. data = data - offset
  116. pars = guess_from_peak(self, data, x, negative, ampscale=1.25)
  117. pars.add('offset', value=offset)
  118. return update_param_vals(pars, self.prefix, **kwargs)
  119. class ExponentialWithOffsetModel(Model):
  120. def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
  121. **kwargs):
  122. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  123. 'independent_vars': independent_vars})
  124. super().__init__(exponentialWithOffset, **kwargs)
  125. def guess(self, data, x, **kwargs):
  126. """Estimate initial model parameter values from data."""
  127. offset = np.min(data)
  128. data = data - offset
  129. try:
  130. sval, oval = np.polyfit(x, np.log(abs(data)+1.e-15), 1)
  131. except TypeError:
  132. sval, oval = 1., np.log(abs(max(data)+1.e-9))
  133. pars = self.make_params(amplitude=np.exp(oval), decay=-1.0/sval)
  134. pars.add('offset', value=offset)
  135. return update_param_vals(pars, self.prefix, **kwargs)
  136. class ExpansionModel(Model):
  137. def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
  138. **kwargs):
  139. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  140. 'independent_vars': independent_vars})
  141. super().__init__(expansion, **kwargs)
  142. def guess(self, data, x, **kwargs):
  143. """Estimate initial model parameter values from data."""
  144. popt1, pcov1 = curve_fit(expansion, x, data)
  145. pars = self.make_params(amplitude=popt1[0], offset=popt1[1])
  146. return update_param_vals(pars, self.prefix, **kwargs)
  147. class DampingOscillationModel(Model):
  148. def __init__(self, independent_vars=['x'], prefix='', nan_policy='raise',
  149. **kwargs):
  150. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  151. 'independent_vars': independent_vars})
  152. super().__init__(dampingOscillation, **kwargs)
  153. def guess(self, data, x, **kwargs):
  154. """Estimate initial model parameter values from data."""
  155. try:
  156. popt1, pcov1 = curve_fit(dampingOscillation, x, data, np.array(0, 5, 5e2, 1e3, 16))
  157. pars = self.make_params(center=popt1[0], amplitude=popt1[1], frequency=popt1[2], decay=popt1[3], offset=popt1[4])
  158. except:
  159. pars = self.make_params(center=0, amplitude=5.0, frequency=5e2, decay=1.0e3, offset=16.0)
  160. return update_param_vals(pars, self.prefix, **kwargs)
  161. class TwoGaussian2dModel(Model):
  162. fwhm_factor = 2*np.sqrt(2*np.log(2))
  163. height_factor = 1./2*np.pi
  164. def __init__(self, independent_vars=['x', 'y'], prefix='', nan_policy='raise',
  165. **kwargs):
  166. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  167. 'independent_vars': independent_vars})
  168. self.helperModel = Gaussian2dModel()
  169. super().__init__(two_gaussian2d, **kwargs)
  170. self._set_paramhints_prefix()
  171. def _set_paramhints_prefix(self):
  172. self.set_param_hint('delta', value=-1, max=0)
  173. self.set_param_hint('A_sigmax', expr=f'{self.prefix}delta + {self.prefix}B_sigmax')
  174. def guess(self, data, x, y, negative=False, **kwargs):
  175. pars_guess = guess_from_peak2d(self.helperModel, data, x, y, negative)
  176. pars = self.make_params(A_amplitude=pars_guess['amplitude'].value, A_centerx=pars_guess['centerx'].value, A_centery=pars_guess['centery'].value,
  177. A_sigmax=pars_guess['sigmax'].value, A_sigmay=pars_guess['sigmay'].value,
  178. B_amplitude=pars_guess['amplitude'].value, B_centerx=pars_guess['centerx'].value, B_centery=pars_guess['centery'].value,
  179. B_sigmax=pars_guess['sigmax'].value, B_sigmay=pars_guess['sigmay'].value)
  180. pars[f'{self.prefix}A_sigmax'].set(expr=f'delta + {self.prefix}B_sigmax')
  181. pars.add(f'{self.prefix}delta', value=-1, max=0, min=-np.inf, vary=True)
  182. pars[f'{self.prefix}A_sigmay'].set(min=0.0)
  183. pars[f'{self.prefix}B_sigmax'].set(min=0.0)
  184. pars[f'{self.prefix}B_sigmay'].set(min=0.0)
  185. return pars
  186. class Polylog22dModel(Model):
  187. fwhm_factor = 2*np.sqrt(2*np.log(2))
  188. height_factor = 1./2*np.pi
  189. def __init__(self, independent_vars=['x', 'y'], prefix='', nan_policy='raise',
  190. **kwargs):
  191. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  192. 'independent_vars': independent_vars})
  193. super().__init__(polylog2_2d, **kwargs)
  194. self._set_paramhints_prefix()
  195. def _set_paramhints_prefix(self):
  196. self.set_param_hint('Rx', min=0)
  197. self.set_param_hint('Ry', min=0)
  198. def guess(self, data, x, y, negative=False, **kwargs):
  199. """Estimate initial model parameter values from data."""
  200. pars = guess_from_peak2d(self, data, x, y, negative)
  201. return update_param_vals(pars, self.prefix, **kwargs)
  202. class ThomasFermi2dModel(Model):
  203. fwhm_factor = 1
  204. height_factor = 0.5
  205. def __init__(self, independent_vars=['x', 'y'], prefix='', nan_policy='raise',
  206. **kwargs):
  207. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  208. 'independent_vars': independent_vars})
  209. super().__init__(ThomasFermi_2d, **kwargs)
  210. self._set_paramhints_prefix()
  211. def _set_paramhints_prefix(self):
  212. self.set_param_hint('Rx', min=0)
  213. self.set_param_hint('Ry', min=0)
  214. def guess(self, data, x, y, negative=False, **kwargs):
  215. """Estimate initial model parameter values from data."""
  216. pars = guess_from_peak2d(self, data, x, y, negative)
  217. # amplitude = pars['amplitude'].value
  218. # simgax = pars['sigmax'].value
  219. # sigmay = pars['sigmay'].value
  220. # pars['amplitude'].set(value=amplitude/s2pi/simgax/sigmay)
  221. simgax = pars['sigmax'].value
  222. sigmay = pars['sigmay'].value
  223. pars['simgax'].set(value=simgax / 2.355)
  224. pars['sigmay'].set(value=sigmay / 2.355)
  225. return update_param_vals(pars, self.prefix, **kwargs)
  226. class DensityProfileBEC2dModel(Model):
  227. fwhm_factor = 2*np.sqrt(2*np.log(2))
  228. height_factor = 1./2*np.pi
  229. def __init__(self, independent_vars=['x', 'y'], prefix='', nan_policy='raise',
  230. **kwargs):
  231. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  232. 'independent_vars': independent_vars})
  233. super().__init__(density_profile_BEC_2d, **kwargs)
  234. self._set_paramhints_prefix()
  235. def _set_paramhints_prefix(self):
  236. self.set_param_hint('BEC_sigmax', min=0)
  237. self.set_param_hint('BEC_sigmay', min=0)
  238. self.set_param_hint('thermal_sigmax', min=0)
  239. # self.set_param_hint('thermal_sigmay', min=0)
  240. self.set_param_hint('BEC_amplitude', min=0)
  241. self.set_param_hint('thermal_amplitude', min=0)
  242. self.set_param_hint('thermalAspectRatio', min=0.8, max=1.2)
  243. self.set_param_hint('thermal_sigmay', expr=f'{self.prefix}thermalAspectRatio * {self.prefix}thermal_sigmax')
  244. self.set_param_hint('condensate_fraction', expr=f'{self.prefix}BEC_amplitude / ({self.prefix}BEC_amplitude + {self.prefix}thermal_amplitude)')
  245. def guess(self, data, x, y, negative=False, pureBECThreshold=0.5, noBECThThreshold=0.0, **kwargs):
  246. """Estimate initial model parameter values from data."""
  247. fitModel = TwoGaussian2dModel()
  248. pars = fitModel.guess(data, x=x, y=y, negative=negative)
  249. fitResult = fitModel.fit(data, x=x, y=y, params=pars, **kwargs)
  250. pars_guess = fitResult.params
  251. BEC_amplitude = pars_guess['A_amplitude'].value
  252. thermal_amplitude = pars_guess['B_amplitude'].value
  253. pars = self.make_params(BEC_amplitude=BEC_amplitude,
  254. thermal_amplitude=thermal_amplitude,
  255. BEC_centerx=pars_guess['A_centerx'].value, BEC_centery=pars_guess['A_centery'].value,
  256. BEC_sigmax=(pars_guess['A_sigmax'].value / 2.355), BEC_sigmay=(pars_guess['A_sigmay'].value / 2.355),
  257. thermal_centerx=pars_guess['B_centerx'].value, thermal_centery=pars_guess['B_centery'].value,
  258. thermal_sigmax=(pars_guess['B_sigmax'].value * s2),
  259. thermalAspectRatio=(pars_guess['B_sigmax'].value * s2) / (pars_guess['B_sigmay'].value * s2)
  260. # thermal_sigmay=(pars_guess['B_sigmay'].value * s2)
  261. )
  262. if BEC_amplitude / (thermal_amplitude + BEC_amplitude) > pureBECThreshold:
  263. if np.abs(1 - pars_guess['A_sigmax'].value / pars_guess['A_sigmay'].value) < 0.1:
  264. pars[f'{self.prefix}BEC_amplitude'].set(value=0)
  265. pars[f'{self.prefix}thermal_amplitude'].set(value=(thermal_amplitude + BEC_amplitude))
  266. else:
  267. pars[f'{self.prefix}thermal_amplitude'].set(value=0)
  268. pars[f'{self.prefix}BEC_amplitude'].set(value=(thermal_amplitude + BEC_amplitude))
  269. if BEC_amplitude / (thermal_amplitude + BEC_amplitude) < noBECThThreshold:
  270. pars[f'{self.prefix}BEC_amplitude'].set(value=0)
  271. pars[f'{self.prefix}thermal_amplitude'].set(value=(thermal_amplitude + BEC_amplitude))
  272. return update_param_vals(pars, self.prefix, **kwargs)
  273. class NewFitModel(Model):
  274. def __init__(self, func, independent_vars=['x'], prefix='', nan_policy='raise',
  275. **kwargs):
  276. kwargs.update({'prefix': prefix, 'nan_policy': nan_policy,
  277. 'independent_vars': independent_vars})
  278. super().__init__(func, **kwargs)
  279. def guess(self, *args, **kwargs):
  280. return self.make_params()
  281. lmfit_models = {'Constant': ConstantModel,
  282. 'Complex Constant': ComplexConstantModel,
  283. 'Linear': LinearModel,
  284. 'Quadratic': QuadraticModel,
  285. 'Polynomial': PolynomialModel,
  286. 'Gaussian': GaussianModel,
  287. 'Gaussian-2D': Gaussian2dModel,
  288. 'Lorentzian': LorentzianModel,
  289. 'Split-Lorentzian': SplitLorentzianModel,
  290. 'Voigt': VoigtModel,
  291. 'PseudoVoigt': PseudoVoigtModel,
  292. 'Moffat': MoffatModel,
  293. 'Pearson7': Pearson7Model,
  294. 'StudentsT': StudentsTModel,
  295. 'Breit-Wigner': BreitWignerModel,
  296. 'Log-Normal': LognormalModel,
  297. 'Damped Oscillator': DampedOscillatorModel,
  298. 'Damped Harmonic Oscillator': DampedHarmonicOscillatorModel,
  299. 'Exponential Gaussian': ExponentialGaussianModel,
  300. 'Skewed Gaussian': SkewedGaussianModel,
  301. 'Skewed Voigt': SkewedVoigtModel,
  302. 'Thermal Distribution': ThermalDistributionModel,
  303. 'Doniach': DoniachModel,
  304. 'Power Law': PowerLawModel,
  305. 'Exponential': ExponentialModel,
  306. 'Step': StepModel,
  307. 'Rectangle': RectangleModel,
  308. 'Expression': ExpressionModel,
  309. 'Gaussian With Offset':GaussianWithOffsetModel,
  310. 'Lorentzian With Offset':LorentzianWithOffsetModel,
  311. 'Expansion':ExpansionModel,
  312. 'Damping Oscillation Model':DampingOscillationModel,
  313. 'Two Gaussian-2D':TwoGaussian2dModel,
  314. }
  315. class FitAnalyser():
  316. def __init__(self, fitModel, fitDim=1, **kwargs) -> None:
  317. if isinstance(fitModel, str):
  318. self.fitModel = lmfit_models[fitModel](**kwargs)
  319. else:
  320. self.fitModel = fitModel
  321. self.fitDim = fitDim
  322. def print_params_set_template(self, params=None):
  323. if params is None:
  324. params = self.fitModel.make_params()
  325. for key in params:
  326. res = "params.add("
  327. res += "name=\"" + key + "\", "
  328. if not params[key].expr is None:
  329. res += "expr=" + params[key].expr +")"
  330. else:
  331. res += "value=" + f'{params[key].value:3g}' + ", "
  332. if str(params[key].max)=="inf":
  333. res += "max=np.inf, "
  334. else:
  335. res += "max=" + f'{params[key].max:3g}' + ", "
  336. if str(params[key].min)=="-inf":
  337. res += "min=-np.inf, "
  338. else:
  339. res += "min=" + f'{params[key].min:3g}' + ", "
  340. res += "vary=" + str(params[key].vary) + ")"
  341. print(res)
  342. def _guess_1D(self, data, x, **kwargs):
  343. return self.fitModel.guess(data=data, x=x, **kwargs)
  344. def _guess_2D(self, data, x, y, **kwargs):
  345. data = data.flatten(order='F')
  346. return self.fitModel.guess(data=data, x=x, y=y, **kwargs)
  347. def guess(self, dataArray, x=None, y=None, guess_kwargs={}, input_core_dims=None, dask='parallelized', vectorize=True, keep_attrs=True, daskKwargs=None, **kwargs):
  348. kwargs.update(
  349. {
  350. "dask": dask,
  351. "vectorize": vectorize,
  352. "input_core_dims": input_core_dims,
  353. 'keep_attrs': keep_attrs,
  354. }
  355. )
  356. if not daskKwargs is None:
  357. kwargs.update({"dask_gufunc_kwargs": daskKwargs})
  358. if input_core_dims is None:
  359. kwargs.update(
  360. {
  361. "input_core_dims": [['x']],
  362. }
  363. )
  364. if x is None:
  365. if 'x' in dataArray.dims:
  366. x = dataArray['x'].to_numpy()
  367. else:
  368. if isinstance(x, str):
  369. if input_core_dims is None:
  370. kwargs.update(
  371. {
  372. "input_core_dims": [[x]],
  373. }
  374. )
  375. x = dataArray[x].to_numpy()
  376. if self.fitDim == 1:
  377. guess_kwargs.update(
  378. {
  379. 'x':x,
  380. }
  381. )
  382. return xr.apply_ufunc(self._guess_1D, dataArray, kwargs=guess_kwargs,
  383. output_dtypes=[type(self.fitModel.make_params())],
  384. **kwargs
  385. )
  386. if self.fitDim == 2:
  387. if y is None:
  388. if 'y' in dataArray.dims:
  389. y = dataArray['y'].to_numpy()
  390. if input_core_dims is None:
  391. kwargs.update(
  392. {
  393. "input_core_dims": [['x', 'y']],
  394. }
  395. )
  396. else:
  397. if isinstance(y, str):
  398. kwargs["input_core_dims"][0] = np.append(kwargs["input_core_dims"][0], y)
  399. y = dataArray[y].to_numpy()
  400. elif input_core_dims is None:
  401. kwargs.update(
  402. {
  403. "input_core_dims": [['x', 'y']],
  404. }
  405. )
  406. _x, _y = np.meshgrid(x, y)
  407. _x = _x.flatten()
  408. _y = _y.flatten()
  409. # dataArray = dataArray.stack(_z=(kwargs["input_core_dims"][0][0], kwargs["input_core_dims"][0][1]))
  410. # kwargs["input_core_dims"][0] = ['_z']
  411. guess_kwargs.update(
  412. {
  413. 'x':_x,
  414. 'y':_y,
  415. }
  416. )
  417. return xr.apply_ufunc(self._guess_2D, dataArray, kwargs=guess_kwargs,
  418. output_dtypes=[type(self.fitModel.make_params())],
  419. **kwargs
  420. )
  421. def _fit_1D(self, data, params, x):
  422. return self.fitModel.fit(data=data, x=x, params=params, nan_policy='omit')
  423. def _fit_2D(self, data, params, x, y):
  424. data = data.flatten(order='F')
  425. return self.fitModel.fit(data=data, x=x, y=y, params=params, nan_policy='omit')
  426. def fit(self, dataArray, paramsArray, x=None, y=None, input_core_dims=None, dask='parallelized', vectorize=True, keep_attrs=True, daskKwargs=None, **kwargs):
  427. kwargs.update(
  428. {
  429. "dask": dask,
  430. "vectorize": vectorize,
  431. "input_core_dims": input_core_dims,
  432. 'keep_attrs': keep_attrs,
  433. }
  434. )
  435. if not daskKwargs is None:
  436. kwargs.update({"dask_gufunc_kwargs": daskKwargs})
  437. if isinstance(paramsArray, type(self.fitModel.make_params())):
  438. if input_core_dims is None:
  439. kwargs.update(
  440. {
  441. "input_core_dims": [['x']],
  442. }
  443. )
  444. if x is None:
  445. if 'x' in dataArray.dims:
  446. x = dataArray['x'].to_numpy()
  447. else:
  448. if isinstance(x, str):
  449. if input_core_dims is None:
  450. kwargs.update(
  451. {
  452. "input_core_dims": [[x]],
  453. }
  454. )
  455. x = dataArray[x].to_numpy()
  456. if self.fitDim == 1:
  457. return xr.apply_ufunc(self._fit_1D, dataArray, kwargs={'params':paramsArray,'x':x},
  458. output_dtypes=[type(lmfit.model.ModelResult(self.fitModel, self.fitModel.make_params()))],
  459. **kwargs)
  460. if self.fitDim == 2:
  461. if y is None:
  462. if 'y' in dataArray.dims:
  463. y = dataArray['y'].to_numpy()
  464. if input_core_dims is None:
  465. kwargs.update(
  466. {
  467. "input_core_dims": [['x', 'y']],
  468. }
  469. )
  470. else:
  471. if isinstance(y, str):
  472. kwargs["input_core_dims"][0] = np.append(kwargs["input_core_dims"][0], y)
  473. y = dataArray[y].to_numpy()
  474. elif input_core_dims is None:
  475. kwargs.update(
  476. {
  477. "input_core_dims": [['x', 'y']],
  478. }
  479. )
  480. _x, _y = np.meshgrid(x, y)
  481. _x = _x.flatten()
  482. _y = _y.flatten()
  483. # dataArray = dataArray.stack(_z=(kwargs["input_core_dims"][0][0], kwargs["input_core_dims"][0][1]))
  484. # kwargs["input_core_dims"][0] = ['_z']
  485. return xr.apply_ufunc(self._fit_2D, dataArray, kwargs={'params':paramsArray,'x':_x, 'y':_y},
  486. output_dtypes=[type(lmfit.model.ModelResult(self.fitModel, self.fitModel.make_params()))],
  487. **kwargs)
  488. else:
  489. if input_core_dims is None:
  490. kwargs.update(
  491. {
  492. "input_core_dims": [['x'], []],
  493. }
  494. )
  495. if x is None:
  496. if 'x' in dataArray.dims:
  497. x = dataArray['x'].to_numpy()
  498. else:
  499. if isinstance(x, str):
  500. if input_core_dims is None:
  501. kwargs.update(
  502. {
  503. "input_core_dims": [[x], []],
  504. }
  505. )
  506. x = dataArray[x].to_numpy()
  507. if self.fitDim == 1:
  508. return xr.apply_ufunc(self._fit_1D, dataArray, paramsArray, kwargs={'x':x},
  509. output_dtypes=[type(lmfit.model.ModelResult(self.fitModel, self.fitModel.make_params()))],
  510. **kwargs)
  511. if self.fitDim == 2:
  512. if input_core_dims is None:
  513. kwargs.update(
  514. {
  515. "input_core_dims": [['x', 'y'], []],
  516. }
  517. )
  518. if y is None:
  519. if 'y' in dataArray.dims:
  520. y = dataArray['y'].to_numpy()
  521. else:
  522. if isinstance(y, str):
  523. y = dataArray[y].to_numpy()
  524. kwargs["input_core_dims"][0] = np.append(kwargs["input_core_dims"][0], y)
  525. _x, _y = np.meshgrid(x, y)
  526. _x = _x.flatten()
  527. _y = _y.flatten()
  528. # dataArray = dataArray.stack(_z=(kwargs["input_core_dims"][0][0], kwargs["input_core_dims"][0][1]))
  529. # kwargs["input_core_dims"][0] = ['_z']
  530. return xr.apply_ufunc(self._fit_2D, dataArray, paramsArray, kwargs={'x':_x, 'y':_y},
  531. output_dtypes=[type(lmfit.model.ModelResult(self.fitModel, self.fitModel.make_params()))],
  532. **kwargs)
  533. def _eval_1D(self, fitResult, x):
  534. return self.fitModel.eval(x=x, **fitResult.best_values)
  535. def _eval_2D(self, fitResult, x, y, shape):
  536. res = self.fitModel.eval(x=x, y=y, **fitResult.best_values)
  537. return res.reshape(shape, order='F')
  538. def eval(self, fitResultArray, x=None, y=None, output_core_dims=None, prefix="", dask='parallelized', vectorize=True, daskKwargs=None, **kwargs):
  539. kwargs.update(
  540. {
  541. "dask": dask,
  542. "vectorize": vectorize,
  543. "output_core_dims": output_core_dims,
  544. }
  545. )
  546. if daskKwargs is None:
  547. daskKwargs = {}
  548. if self.fitDim == 1:
  549. if output_core_dims is None:
  550. kwargs.update(
  551. {
  552. "output_core_dims": prefix+'x',
  553. }
  554. )
  555. output_core_dims = [prefix+'x']
  556. daskKwargs.update(
  557. {
  558. 'output_sizes': {
  559. output_core_dims[0]: np.size(x),
  560. },
  561. 'meta': np.ndarray((0,0), dtype=float)
  562. }
  563. )
  564. kwargs.update(
  565. {
  566. "dask_gufunc_kwargs": daskKwargs,
  567. }
  568. )
  569. res = xr.apply_ufunc(self._eval_1D, fitResultArray, kwargs={"x":x}, **kwargs)
  570. return res.assign_coords({prefix+'x':np.array(x)})
  571. if self.fitDim == 2:
  572. if output_core_dims is None:
  573. kwargs.update(
  574. {
  575. "output_core_dims": [[prefix+'x', prefix+'y']],
  576. }
  577. )
  578. output_core_dims = [prefix+'x', prefix+'y']
  579. daskKwargs.update(
  580. {
  581. 'output_sizes': {
  582. output_core_dims[0]: np.size(x),
  583. output_core_dims[1]: np.size(y),
  584. },
  585. 'meta': np.ndarray((0,0), dtype=float)
  586. },
  587. )
  588. kwargs.update(
  589. {
  590. "dask_gufunc_kwargs": daskKwargs,
  591. }
  592. )
  593. _x, _y = np.meshgrid(x, y)
  594. _x = _x.flatten()
  595. _y = _y.flatten()
  596. res = xr.apply_ufunc(self._eval_2D, fitResultArray, kwargs={"x":_x, "y":_y, "shape":(len(x), len(y))}, **kwargs)
  597. return res.assign_coords({prefix+'x':np.array(x), prefix+'y':np.array(y)})
  598. def _get_fit_value_single(self, fitResult, key):
  599. return fitResult.params[key].value
  600. def _get_fit_value(self, fitResult, params):
  601. func = np.vectorize(self._get_fit_value_single)
  602. res = tuple(
  603. func(fitResult, key)
  604. for key in params
  605. )
  606. return res
  607. def get_fit_value(self, fitResult, dask='parallelized', **kwargs):
  608. firstIndex = {
  609. key: fitResult[key][0]
  610. for key in fitResult.dims
  611. }
  612. firstFitResult = fitResult.sel(firstIndex).item()
  613. params = list(firstFitResult.params.keys())
  614. output_core_dims=[ [] for _ in range(len(params))]
  615. kwargs.update(
  616. {
  617. "dask": dask,
  618. "output_core_dims": output_core_dims,
  619. }
  620. )
  621. value = xr.apply_ufunc(self._get_fit_value, fitResult, kwargs=dict(params=params), **kwargs)
  622. value = xr.Dataset(
  623. data_vars={
  624. params[i]: value[i]
  625. for i in range(len(params))
  626. },
  627. attrs=fitResult.attrs
  628. )
  629. return value
  630. def _get_fit_std_single(self, fitResult, key):
  631. return fitResult.params[key].stderr
  632. def _get_fit_std(self, fitResult, params):
  633. func = np.vectorize(self._get_fit_std_single)
  634. res = tuple(
  635. func(fitResult, key)
  636. for key in params
  637. )
  638. return res
  639. def get_fit_std(self, fitResult, dask='parallelized', **kwargs):
  640. firstIndex = {
  641. key: fitResult[key][0]
  642. for key in fitResult.dims
  643. }
  644. firstFitResult = fitResult.sel(firstIndex).item()
  645. params = list(firstFitResult.params.keys())
  646. output_core_dims=[ [] for _ in range(len(params))]
  647. kwargs.update(
  648. {
  649. "dask": dask,
  650. "output_core_dims": output_core_dims,
  651. }
  652. )
  653. value = xr.apply_ufunc(self._get_fit_std, fitResult, kwargs=dict(params=params), **kwargs)
  654. value = xr.Dataset(
  655. data_vars={
  656. params[i]: value[i]
  657. for i in range(len(params))
  658. },
  659. attrs=fitResult.attrs
  660. )
  661. return value
  662. def _get_fit_full_result_single(self, fitResult, key):
  663. if not fitResult.params[key].value is None:
  664. value = fitResult.params[key].value
  665. else:
  666. value = np.nan
  667. if not fitResult.params[key].stderr is None:
  668. std = fitResult.params[key].stderr
  669. else:
  670. std = np.nan
  671. return ufloat(value, std)
  672. def _get_fit_full_result(self, fitResult, params):
  673. func = np.vectorize(self._get_fit_full_result_single)
  674. res = tuple(
  675. func(fitResult, key)
  676. for key in params
  677. )
  678. return res
  679. def get_fit_full_result(self, fitResult, dask='parallelized', **kwargs):
  680. firstIndex = {
  681. key: fitResult[key][0]
  682. for key in fitResult.dims
  683. }
  684. firstFitResult = fitResult.sel(firstIndex).item()
  685. params = list(firstFitResult.params.keys())
  686. output_core_dims=[ [] for _ in range(len(params))]
  687. kwargs.update(
  688. {
  689. "dask": dask,
  690. "output_core_dims": output_core_dims,
  691. }
  692. )
  693. value = xr.apply_ufunc(self._get_fit_full_result, fitResult, kwargs=dict(params=params), **kwargs)
  694. value = xr.Dataset(
  695. data_vars={
  696. params[i]: value[i]
  697. for i in range(len(params))
  698. },
  699. attrs=fitResult.attrs
  700. )
  701. return value