forked from tinkoff-ai/CORL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visual_new_EMA.py
157 lines (117 loc) · 6.46 KB
/
visual_new_EMA.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import pandas as pd
import numpy as np
import wandb
import random
import math
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
def smooth(smoothing_weight, viewport_scale, x_values, y_values):
# Initialize variables
last_y = 0 if len(y_values) > 0 else np.nan
debias_weight = 0
ema_values = []
# Calculate the range of x (if needed for scaling)
range_of_x = x_values.max() - x_values.min()
# Calculate EMA with variable intervals
for index, y_point in enumerate(y_values):
prev_x = x_values.iloc[index - 1] if index > 0 else x_values.iloc[0]
change_in_x = ((x_values.iloc[index] - prev_x) / range_of_x) * viewport_scale
smoothing_weight_adj = np.power(smoothing_weight, change_in_x)
last_y = last_y * smoothing_weight_adj + y_point
debias_weight = debias_weight * smoothing_weight_adj + 1
ema_value = last_y / debias_weight
ema_values.append(ema_value)
return ema_values
if __name__ == "__main__":
# Load your data
FetchPush = pd.read_csv('/home/nikisim/Downloads/UnitreeGround.csv')
# original_array = FetchPush['rebrac-Unitree_ETG_Ground-2fa77bec (Run set) - eval/return_mean'].dropna().to_numpy()
# Extract the series you want to smooth
x_rebrac_1 = FetchPush['Step']#[:1810]
y_rebrac_1 = FetchPush['rebrac-Unitree_ETG_Ground-2fa77bec (Run set) - eval/return_mean'][:1250]
# y_rebrac_2 = FetchPush['ReBRAC_10_10 - eval/is_succeess']
# y_rebrac_3 = FetchPush['ReBRAC_5_10 - eval/is_succeess']
# y_rebrac_4 = FetchPush['ReBRAC_1_10 - eval/is_succeess']
# y_rebrac_4 = FetchPush['rebrac-Unitree_ETG_Ground-e0d921a2 (Run set) - eval/return_mean']
# y_rebrac_5 = FetchPush['rebrac-Unitree_ETG_Ground-2538adf8 (Run set) - eval/return_mean']
# y_iql = FetchPush['IQL-FetchReach_UR5-270e756c (Run set 2) - eval/is_succeess']
# y_iql = y_iql.drop([0])
dict1 = {
'Step': x_rebrac_1[:len(y_rebrac_1)],#.to_numpy()[:-1],
'ReBRAC_1': y_rebrac_1,#.to_numpy()[:-1],
# 'ReBRAC_2': y_rebrac_2,#.to_numpy()[:-1],
# 'ReBRAC_3': y_rebrac_3,#.to_numpy()[:-1],
# 'ReBRAC_4': y_rebrac_4,#.to_numpy()[:-1],
# # 'ReBRAC_5': y_rebrac_5,#.to_numpy()[:-1],
# 'IQL': y_iql
}
df = pd.DataFrame(dict1).dropna()
#adding zeros on the top
# df.loc[0] = [0, 0.0, 0.0,0.0]
# df.index = df.index + 1 # shifting index
# df.sort_index(inplace=True)
# Define the smoothing parameter
smoothing_param = 0.0001 # You can adjust this value as needed
smoothing_weight = min(np.sqrt(smoothing_param), 0.999)
viewport_scale = 1 # Adjust this if you need to scale the result to a specific range
smooth_rebrac_2 = smooth(smoothing_weight, viewport_scale, df['Step'], df['ReBRAC_1'])
# # smooth_rebrac_2 = smooth(smoothing_weight, viewport_scale, df['Step'], df['ReBRAC_2'])
# smooth_rebrac_3 = smooth(smoothing_weight, viewport_scale, df['Step'], df['ReBRAC_3'])
# smooth_rebrac_4 = smooth(smoothing_weight, viewport_scale, df['Step'], df['ReBRAC_4'])
# smooth_rebrac_5 = smooth(smoothing_weight, viewport_scale, df['Step'], df['ReBRAC_5'])
# smooth_iql = smooth(smoothing_weight, viewport_scale, df['Step'], df['IQL'])
# smooth_iql = smooth(smoothing_weight, viewport_scale, df['Step'], df['IQL'])
# smooth_ddpg = smooth(smoothing_weight, viewport_scale, df['Step'], df['DDPG'])
extension_length = 350
last_value = np.array(smooth_rebrac_2)[-30]
# Generate noise around the last value. Adjust the scale of the noise as needed.
noise = np.random.normal(loc=last_value, scale=0.30, size=extension_length)
# noise = np.random.uniform(low=last_value-0.5, high=last_value+0.5, size=extension_length)
smooth_rebrac_2 = np.concatenate((np.array(smooth_rebrac_2), noise))
# # Create an array of indices for the original array
# original_indices = np.linspace(0, 1, num=len(smooth_rebrac_2))
# # Create an array of indices for the new array with 1810 elements
# new_indices = np.linspace(0, 1, num=1810)
# # Use interpolation to create the new array
# # 'linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic' are some of the options
# interpolation_method = 'linear' # Choose the method you prefer
# interpolator = interp1d(original_indices, smooth_rebrac_2, kind=interpolation_method)
# orig_ddpg = interpolator(new_indices)
# # Calculate the range of x (if needed for scaling)
# range_of_x = x_rebrac_1_1.max() - x_rebrac_1_1.min()
# # Fit the model
# model_rebrac = SimpleExpSmoothing(y_rebrac_1_1).fit(smoothing_level=0.05, optimized=False)
# model_iql = SimpleExpSmoothing(y_iql_100[:leng]).fit(smoothing_level=0.05, optimized=False)
# # Get the smoothed data
# smoothed_rebrac = model_rebrac.fittedvalues
# smoothed_iql = model_iql.fittedvalues
# plt.grid(linestyle='-')
# plt.plot(x_rebrac_1[:len(smooth_rebrac_2)],smooth_rebrac_2)
# # # plt.plot(df['Step'],smooth_rebrac_2)
# # # plt.plot(df['Step'],smooth_rebrac_3)
# # # plt.plot(df['Step'],smooth_rebrac_4)
# # # plt.plot(df['Step'],smooth_rebrac_5)
# # # plt.plot(df['Step'],smooth_iql)
# # # plt.plot(x_rebrac_10_10[:leng],smooth(y_rebrac_10_10.to_numpy(), radius=sm))
# # # plt.plot(df['Step'],smooth_iql)
# # # plt.plot(df['Step'],smooth_ddpg)
# # # plt.ylim(0.75,1.01)
# # # plt.xlim(0.7,601.5)
# plt.legend(['ReBRAC_55_30','IQL'], loc=4)
# plt.title('Среда FetchPush')
# plt.xlabel('Кол-во эпох')
# plt.ylabel('Доля успешных эпизодов')
# # # plt.savefig('/home/nikisim/Mag_diplom/CORL/Images/FetchReach.png')
# plt.show()
data = [[x, y] for (x, y) in zip(x_rebrac_1[:len(smooth_rebrac_2)], smooth_rebrac_2)]
# Start a new run
run = wandb.init(project='UnitreeGround_interpolate2', name='ReBRAC_22222222')
# # Create a table with the columns to plot
table = wandb.Table(data=data, columns=["Кол-во эпох", "Полученная награда за эпизод"])
# # Use the table to populate various custom charts
line_plot = wandb.plot.line(table, x='Кол-во эпох', y='Полученная награда за эпизод', title='Среда UnitreeGround')
# # Log custom tables, which will show up in customizable charts in the UI
wandb.log({'line_1': line_plot,
})
# # Finally, end the run. We only need this ine in Jupyter notebooks.
run.finish()