fit_d_param.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. from functools import lru_cache
  4. from matplotlib import markers, pyplot as plt
  5. from scipy.optimize import curve_fit
  6. import numpy as np
  7. import cma
  8. # import pyfde
  9. from numba import njit, float64
  10. from eval_spectra import spectra
  11. from mealpy.physics_based.EO import AdaptiveEO
  12. from_disk = np.loadtxt('rs4-d_perp_interpolated.txt')
  13. step = 5
  14. omega_ratio = np.copy(from_disk[0, ::step])
  15. d_rs4 = from_disk[1, ::step] + 1j*from_disk[2, ::step]
  16. d_rms = d_rs4
  17. def rms(x0):
  18. d_fit = spectra(omega_ratio, x0)
  19. diff_re = np.real(d_rms - d_fit)
  20. rms = np.sqrt(np.sum(np.abs(diff_re)**2))
  21. diff_im = np.imag(d_rms - d_fit)
  22. rms += np.sqrt(np.sum(np.abs(diff_im)**2))
  23. return rms
  24. poles = 1
  25. dim = poles*4
  26. x0 = np.random.random(dim)
  27. d_rms = d_rs4
  28. # x, es = cma.fmin2(rms, x0, sigma0=0.2)
  29. x = np.array([0.13421489, 0.82250415, -0.50359304, -0.0591722])
  30. x1 = x
  31. x0 = np.random.random(dim)
  32. d_rms = d_rs4 - spectra(omega_ratio, x1)
  33. x, es = cma.fmin2(rms, x0, sigma0=2)
  34. # x = [0.00051888486267353, 0.9991499897780783,
  35. # 0.06926055304806265, -0.030608812209114836] # fitness = 4.7
  36. x2 = x
  37. d_fit = spectra(omega_ratio, x)
  38. plt.figure('rs4')
  39. # plt.title('rms = '+str(rms(x)/x.size))
  40. plt.plot(omega_ratio, np.real(d_rms), label='re d')
  41. plt.plot(omega_ratio, np.imag(d_rms), label='im d')
  42. # plt.plot(omega_ratio, np.real(
  43. # d_rs4 - spectra(omega_ratio, x1)), label='diff re d')
  44. # plt.plot(omega_ratio, np.imag(
  45. # d_rs4 - spectra(omega_ratio, x1)), label='diff im d')
  46. plt.plot(omega_ratio, np.real(d_fit), label='re d fit', alpha=0.2, lw=3)
  47. plt.plot(omega_ratio, np.imag(d_fit), label='im d fit', alpha=0.2, lw=3)
  48. # plt.plot(omega_ratio, func(xdata, *popt), 'r-',
  49. # label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
  50. plt.legend()
  51. plt.show()
  52. # from_disk = np.loadtxt('silver-d_perp_interpolated.txt')
  53. # plt.figure('silver')
  54. # plt.plot(from_disk[0, :], from_disk[1, :], label='re d perp')
  55. # plt.plot(from_disk[0, :], from_disk[2, :], label='im d perp')
  56. # from_disk = np.loadtxt('silver-d_parl_interpolated.txt')
  57. # plt.plot(from_disk[0, :], from_disk[1, :], label='re d parl')
  58. # plt.plot(from_disk[0, :], from_disk[2, :], label='im d parl')
  59. # plt.legend()
  60. # plt.show()