correlation_length_fit.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import numpy as np
  2. from scripts.spatial_network.perlin_map.run_simulation_perlin_map import DATA_FOLDER
  3. from scipy.optimize import curve_fit
  4. import pingouin as pg
  5. def filter_run_names_by_par_dict(traj, par_dict):
  6. run_name_list = []
  7. for run_idx, run_name in enumerate(traj.f_get_run_names()):
  8. traj.f_set_crun(run_name)
  9. paramters_equal = True
  10. for key, val in par_dict.items():
  11. if (traj.par[key] != val):
  12. paramters_equal = False
  13. if paramters_equal:
  14. run_name_list.append(run_name)
  15. traj.f_restore_default()
  16. return run_name_list
  17. def correlation_length_fit_dict(traj, map_type='perlin_map', load=True):
  18. def phi_diff(d_phi):
  19. return (d_phi + np.pi) % (2 * np.pi) - np.pi
  20. def exp_fit_func(x, corr_len):
  21. return np.pi / 2. * np.exp(-corr_len * x)
  22. scale_expl = traj.f_get('scale').f_get_range()
  23. scale_list = np.unique(scale_expl)
  24. seed_expl = traj.f_get('seed').f_get_range()
  25. seed_list = np.unique(seed_expl)
  26. dim = 60
  27. size = 900
  28. traj_name = traj.name
  29. if load:
  30. try:
  31. fit_correlation_lengths_dict = np.load(DATA_FOLDER + map_type + traj_name + '_fit_correlation_lengths_dict.npy')
  32. all_values_there = [scale in fit_correlation_lengths_dict.item().keys() for scale in scale_list]
  33. if all(all_values_there):
  34. return fit_correlation_lengths_dict.item()
  35. print('Some scale values missing in corr. len. fit dictionary, new one will be generated')
  36. except:
  37. print('No correlation length fit dictionary found, will be generated now')
  38. fit_correlation_lengths_dict = {}
  39. for scale in scale_list:
  40. fit_corr_lens_different_seeds = []
  41. for map_seed in seed_list:
  42. par_dict = {'seed': map_seed, 'scale': scale, 'long_axis': 100.0}
  43. run_name = filter_run_names_by_par_dict(traj, par_dict)
  44. ex_tunings = traj.results.runs[run_name].ex_tunings
  45. ex_tun_array = np.array(ex_tunings).reshape((dim, dim))
  46. x_displacements = range(-dim + 1, dim)
  47. y_displacements = range(-dim + 1, dim)
  48. r_c_array = np.ndarray((len(x_displacements), len(y_displacements)))
  49. dist_array = np.ndarray((len(x_displacements), len(y_displacements)))
  50. for x_displacement in x_displacements:
  51. for y_displacement in y_displacements:
  52. a_1 = ex_tun_array[max(0, x_displacement): min(dim, dim + x_displacement),
  53. max(0, y_displacement): min(dim, dim + y_displacement)]
  54. a_2 = ex_tun_array[max(0, -x_displacement): min(dim, dim - x_displacement),
  55. max(0, -y_displacement): min(dim, dim - y_displacement)]
  56. if a_1.shape == (1, 1):
  57. # TODO: Still not sure which value to put here.
  58. r_c = np.nan
  59. else:
  60. # TODO: For some reason, some values come out as NaN or +/-inf
  61. r_c, p_c = pg.circ_corrcc(a_1.flatten(), a_2.flatten(), correction_uniform=True)
  62. r_c_array[x_displacement + dim - 1, y_displacement + dim - 1] = r_c
  63. dist_array[x_displacement + dim - 1, y_displacement + dim - 1] = np.sqrt(
  64. x_displacement ** 2 + y_displacement ** 2)
  65. # correlations over distances
  66. dist_array *= size / dim
  67. distances = dist_array.flatten()
  68. correlations = r_c_array.flatten()
  69. distances = distances[~np.isnan(correlations)]
  70. correlations = correlations[~np.isnan(correlations)]
  71. # Binned mean and exponential fit
  72. bins = np.linspace(0.0, size, 301)
  73. binned_ids = np.digitize(distances, bins)
  74. binned_correlations = [[] for _ in range(len(bins))]
  75. for id, bin_id in enumerate(binned_ids):
  76. bin_corr = correlations[id]
  77. binned_correlations[bin_id - 1].append(bin_corr)
  78. binned_mean_correlations = np.array([np.mean(bin) for bin in binned_correlations])
  79. bins = bins[np.isfinite(binned_mean_correlations)]
  80. binned_mean_correlations = binned_mean_correlations[np.isfinite(binned_mean_correlations)]
  81. # TODO: bins is wrong here, as it is the bin borders
  82. params, _ = curve_fit(exp_fit_func, bins, binned_mean_correlations, [0.5])
  83. fit_corr_len = 1 / params[0]
  84. fit_corr_lens_different_seeds.append(fit_corr_len)
  85. fit_correlation_lengths_dict[scale] = np.mean(fit_corr_lens_different_seeds)
  86. np.save(DATA_FOLDER + map_type + traj_name + '_fit_correlation_lengths_dict.npy', fit_correlation_lengths_dict)
  87. return fit_correlation_lengths_dict