def hexbin_plot(self, var1, var2): print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig("{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() plt.close()
def hexbin_plot(self, var1, var2, force=False): fig_name = "{}/hexbin_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return if var1 == "customer_extra_view_choices" and var2 == "delta_position": print("Doing hexbin plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) hb = ax.hexbin(x=x, y=y, gridsize=20, cmap='inferno') ax.set_facecolor('black') cb = fig.colorbar(hb, ax=ax) cb.set_label('counts') plt.savefig(fig_name) if self.display: plt.show() plt.close()
def make_a_selection(self, n_select, extrema_only): n_eco = len(self.stats.data["idx"]) for key in self.stats.data.keys(): self.stats.data[key] = np.asarray(self.stats.data[key]) all_str_cost = sorted(np.unique( self.stats.data["transportation_cost"])) if extrema_only: tr_cost_selected = [all_str_cost[0], all_str_cost[-1]] else: tr_cost_selected = all_str_cost idx_selected = [] for tr_cost in tr_cost_selected: idx_selected += \ list(np.random.choice(np.arange(n_eco)[self.stats.data["transportation_cost"] == tr_cost], size=n_select, replace=False)) idx_selected = np.asarray(idx_selected) for key in self.stats.data.keys(): self.stats.data[key] = self.stats.data[key][idx_selected] print("Selection done.")
def scatter_plot(self, var1, var2, range_var, linear_regression, force=False): fig_name = "{}/scatterplot_{}_{}.pdf".format(self.fig_folder, var1, var2) if path.exists(fig_name) and not force: return print("Doing scatter plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) plt.scatter(x=x, y=y, c=self.stats.data["transportation_cost"], s=10, cmap=cm.plasma) plt.xlim(range_var[var1]) plt.ylim(range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) if linear_regression: slope, intercept, r_value, p_value, std_err = linregress(x, y) plt.plot(x, intercept + x * slope, c="black", lw=2) with open("{}/stats.txt".format(self.fig_folder), "a", encoding='utf-8') as f: to_write = "*****\n" + \ "{} against {}\n".format(self.format_label(var2), self.format_label(var1)) + \ "p value: {}\n".format(p_value) + \ "intercept: {}\n".format(intercept) + \ "slope: {}\n".format(slope) + \ "r value: {}\n".format(r_value) + \ "\n" f.write(to_write) plt.savefig(fig_name) if self.display: plt.show() plt.close()
def short_analysis(data, analysis_file_path, fig_root_name): # Suppose there are two idx for rt for rt_idx in [1, 2]: # Convert your data in array for easier manipulation rt_column_name = "RT {}".format(rt_idx) rt = np.asarray(data[rt_column_name]) rt_mt_column_name = "RT-MT {}".format(rt_idx) rt_mt = np.asarray(data[rt_mt_column_name]) # Look where 'rt' and 'rt_mt' are different to zero cond0 = rt[:] != 0 cond1 = rt_mt[:] != 0 # Combine the two conditions idx = cond0 * cond1 # Use the booleans as index and make a cut in your data rt = rt[idx] rt_mt = rt_mt[idx] # Compute 'mt' mt = rt_mt - rt print("Short analysis.") print("'mt {}' is: \n".format(rt_idx), mt) # Save this in a new 'xlsx' file new_data = dict() new_data["RT{}".format(rt_idx)] = rt new_data["MT{}".format(rt_idx)] = mt write_a_new_file(file_path=analysis_file_path, data=new_data) # Do some plots plt.scatter(mt, rt) plt.xlabel("mt") plt.ylabel("rt") plt.savefig("{}_scatter_rt{}.pdf".format(fig_root_name, rt_idx)) plt.close() plt.hist(mt) plt.xlabel("mt") plt.savefig("{}_hist_mt{}.pdf".format(fig_root_name, rt_idx)) plt.close() plt.hist(rt) plt.xlabel("rt") plt.savefig("{}_hist_rt{}.pdf".format(fig_root_name, rt_idx)) plt.close()
def get_influenced_nonlinear(self, arguments_idx, arguments_strength): """ Modify the own convictions of an agent based on the 'attacker's argument strength and the agent's own suggestibility. The formula is non-linear and is dependent on the self convictions. Now the influence is considered to be non-linear : - if self convictions are between -0.5 and +0.5, then the formula is the same than the "linear" version (i.e. get_influenced()). - if self convictions are below are above this range, if the attackers arguments are or same sign, the formulu then the self suggestibility gets weaker the more the convictions gets to the extremes (-1 or +1). suggestibility could be 2-fold depending if the attacker has arguments that goes in the same 'direction'.""" # Apply influence formula for (i, s) in zip(arguments_idx, arguments_strength): if self.culture.convictions[i] >= -0.1 or self.culture.convictions[ i] <= 0.1: self.apply_linear_ind_influence(np.asarray([i]), np.asarray([s])) else: # If the self convitions are in the opposite direction than the attacker's if self.culture.convictions * s < 0: # The influence is decreased the more the self convictions are closed to the extremes {-1 ; 1} self.culture.convictions[i] += 2 * (1 - abs(self.culture.convictions[i])) \ * self.suggestibility * s # For abs(self.culture.convictions[i]) equal to 0.5 this is identical # to the classical formula because the new factor equals 1: 2 * (1 - 0.5) = 1 elif self.culture.convictions * s > 0: # The influence is increased the more the self convictions are closed to the extremes {-1 ; 1} if 1 - abs(self.culture.convictions[i]) < 10**-3: self.culture.convictions[ i] += 10**3 * self.suggestibility * s else: self.culture.convictions[i] += 1 / (2 * (1 - abs(self.culture.convictions[i]))) \ * self.suggestibility * s else: # Nothing happens in case it is equal to 0, because the product would be 0 anyway. pass # Apply threshold self.apply_threshold_influence()
def scatter_plot(self, var1, var2, range_var, linear_regression): print("Doing scatter plot '{}' against '{}'.".format(var2, var1)) x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) plt.scatter(x=x, y=y, color="black", s=10) plt.xlim(range_var[var1]) plt.ylim(range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) if linear_regression: slope, intercept, r_value, p_value, std_err = linregress(x, y) plt.plot(x, intercept + x * slope, c="black", lw=2) with open("{}/stats.txt".format(self.fig_folder), "a", encoding='utf-8') as f: to_write = "*****\n" + \ "{} against {}\n".format(self.format_label(var2), self.format_label(var1)) + \ "p value: {}\n".format(p_value) + \ "intercept: {}\n".format(intercept) + \ "slope: {}\n".format(slope) + \ "r value: {}\n".format(r_value) + \ "\n" f.write(to_write) plt.savefig("{}/scatterplot_{}_{}.pdf".format(self.fig_folder, var1, var2)) if self.display: plt.show() plt.close()
def my_curve_fitting(xdata, ydata): xdata = np.asarray(xdata) popt, pcov = curve_fit( func, xdata, ydata ) # bounds=(0, [3., 1., 0.5])) # https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html print('a1,a2,a3 =', popt) plt.figure() plt.plot(xdata, ydata, 'x', label='original data') x = np.arange(min(xdata) * 3, max(xdata) * 3, 0.1) plt.plot(x, func(x, *popt), 'r--', label='fit: a1=%5.3f, a2=%5.3f, a3=%5.3f' % tuple(popt)) plt.plot(x, func(x, 0.0, popt[1], popt[2]), 'b--', label='fit: a1=%5.3f, a2=%5.3f, a3=%5.3f' % tuple(popt))
def extract_single_dimension(self, variable, t_max=5000): print("Extracting variable '{}'.".format(variable.name)) if self.folders is None: self.get_folders() variable.data = [[] for i in range(t_max)] for i in tqdm(self.stats.data["idx"]): results = Results(economy_folder=self.folders[i]) for t in range(t_max): variable.data[t].append(results.data[variable.name][t]) print("Convert in array.") variable.data = np.asarray(variable.data) print("Write in pickle.") variable.write() print("Done.")
def get_most_robust_convictions(self, n=1): # np.argsort() returns the indices that would sort the array # [::-1] reverse the sorting from increasing to decreasing order # [:n] selects the n first elements unique_convictions_values = np.unique(self.convictions) if len(unique_convictions_values) == len(self.convictions): return np.argsort(np.absolute(self.convictions))[::-1][:n] else: # Class in decreasing order the unique different values of convictions sorted_unique_values = sorted(unique_convictions_values, reverse=True) to_return = np.zeros(len(self.convictions), dtype=int) i = 0 for value in sorted_unique_values: list_of_idx = np.where(self.convictions == value)[0] to_return[i:i + len(list_of_idx)] = np.random.permutation( list_of_idx) i += len(list_of_idx) return np.asarray(to_return)
def find_nearest(array, value): array = np.asarray(array) idx = (np.abs(array - value)).argmin() return idx, array[idx]
def histbin(self, var1, var2): if var1 == "customer_extra_view_choices" and var2 == "delta_position": x = np.asarray(self.stats.data[var1]) y = np.asarray(self.stats.data[var2]) n_bin = 10 a = np.linspace(0, 10, n_bin + 1) b = np.zeros(n_bin) for i in range(n_bin): yo = list() for idx, xi in enumerate(x): if a[i] <= xi < a[i + 1]: yo.append(y[idx]) b[i] = np.median(yo) if len(yo) else 0 # ----- # fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) ax.bar(a[:-1] + (a[1] - a[0]) / 2, b, a[1] - a[0], color='grey') plt.savefig("{}/hist_median_{}_{}.pdf".format( self.fig_folder, var1, var2)) # --- # if self.display: plt.show() plt.close() # ---- # b = np.zeros(n_bin) c = np.zeros(n_bin) for i in range(n_bin): yo = list() for idx, xi in enumerate(x): if a[i] <= xi < a[i + 1]: yo.append(y[idx]) b[i] = np.mean(yo) if len(yo) else 0 c[i] = np.std(yo) if len(yo) else 0 # ----- # fig = plt.figure() ax = fig.add_subplot(1, 1, 1) plt.xlim(self.range_var[var1]) plt.ylim(self.range_var[var2]) plt.xlabel(self.format_label(var1)) plt.ylabel(self.format_label(var2)) ax.bar(a[:-1] + (a[1] - a[0]) / 2, b, a[1] - a[0], color='grey', yerr=c) plt.savefig("{}/hist_mean_{}_{}.pdf".format( self.fig_folder, var1, var2)) # --- # if self.display: plt.show() plt.close()
def get_bounds(self): global ad print('get_bounds:', ad.bounds_denorm) min_b, max_b = np.asarray(ad.bounds_denorm).T return ( min_b.tolist(), max_b.tolist() )