-
Notifications
You must be signed in to change notification settings - Fork 0
/
oil_analysis.py
193 lines (138 loc) · 6.62 KB
/
oil_analysis.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
###############################################################
# DAQ TA
#
# DAQ TA is a tool to automate the process of converting AiM
# files from Race Studio to generate statistics and graphs for
# certain tests.
#
# Copyright (c) 2023 Louis Cundari III. All rights reserved.
# Louis Cundari III
# louiscundari3@outlook.com
###############################################################
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import constants as c
import functions as f
pd.set_option('mode.chained_assignment', None)
def split_laps(df):
index_list = df.index[df[c.TIME_COL] == 0].tolist()
index_list.append(len(df))
df_list = []
n = 1
for i in index_list:
df_temp = df.loc[i:index_list[n]-1]
df_list.append(df_temp)
n+=1
if n == len(index_list):
break
return df_list
def round_limp_mode(df):
df[c.RPM_COL] = df[c.RPM_COL].apply(lambda x: f.custom_round(x, 100))
df[c.OIL_PRESS_COL] = df[c.OIL_PRESS_COL].apply(lambda x: f.custom_round(x, .25))
df[c.OIL_TEMP_COL] = df[c.OIL_TEMP_COL].apply(lambda x: f.custom_round(x, 1))
df[c.COOLANT_TEMP_COL] = df[c.COOLANT_TEMP_COL].apply(lambda x: f.custom_round(x, 1))
return df
def remove_rolling_outliers(df, col, window):
df['median']= df[col].rolling(window).median()
df['mean']= df[col].rolling(window).mean()
df['std'] = df[col].rolling(window).std()
df_new = df[(df[col] <= df['median'] + c.ROLLING_SIGMA*df['std']) & (df[col] >= df['median'] - c.ROLLING_SIGMA*df['std'])]
return df_new
def remove_quantile_outliers(df, low, high):
q_low = df[c.COOLANT_TEMP_COL].quantile(low)
q_hi = df[c.COOLANT_TEMP_COL].quantile(high)
df_new = df[(df[c.COOLANT_TEMP_COL] <= q_hi) & (df[c.COOLANT_TEMP_COL] >= q_low)]
return df_new
def hottest_avg_temp(df, temperature_col):
avg_temp = df[temperature_col].mean()
return avg_temp
def limp_mode_graph(df, x_col, y_col, plot_type, marker, session_name, color):
plt.style.use('ggplot')
plot_styles_dict = {
'color': color,
'marker': marker
}
title = f'{y_col} vs {x_col}'
df_plot = df[[x_col, y_col]]
# df_avg = df_plot # use this to demonstrate variation of rpm and press without average
df_avg = df_plot.groupby(x_col, group_keys=False)[y_col].mean().reset_index(name=y_col)
x = df_avg[x_col]
y = df_avg[y_col]
plt.xlabel(x_col)
plt.ylabel(y_col)
plt.title(title)
if plot_type.lower() == 'scatter':
plt.scatter(x, y, **plot_styles_dict)
elif plot_type.lower() == 'line':
plt.plot(x, y, **plot_styles_dict, label=f'Session: {session_name}')
plt.autoscale(enable=True, axis='both', tight=None)
plt.legend(loc='best')
return None
def init_oil_analysis(df_list: list, max_oil_temp_diff: int):
f.clear_plots()
df_sessions_list = []
for df in df_list:
df = round_limp_mode(df)
df_laps_list = split_laps(df)[1:-2] # ** automatically remove first and last two laps (total of 3 removed)
# loop laps to remove outliers
for index, lap in enumerate(df_laps_list):
lap = remove_rolling_outliers(lap, c.COOLANT_TEMP_COL, window=c.ROLLING_WINDOW)
lap = remove_quantile_outliers(lap, c.LO_QUANTILE, c.HI_QUANTILE)
df_laps_list[index] = lap
# loop laps to collect hottest average temperature
avg_temps_list = []
for lap in df_laps_list:
temperature = hottest_avg_temp(lap, c.COOLANT_TEMP_COL)
avg_temps_list.append(temperature)
hottest_lap_avg = max(avg_temps_list)
# print(f'# of Laps: {len(df_laps_list)}')
usable_laps_list = []
for lap in df_laps_list:
min_temp_diff = abs(hottest_lap_avg - lap[c.COOLANT_TEMP_COL].min())
max_temp_diff = abs(hottest_lap_avg - lap[c.COOLANT_TEMP_COL].max())
# print(f'Min: {lap[c.COOLANT_TEMP_COL].min()}')
# print(f'Min diff: {min_temp_diff}')
# print(f'Max: {lap[c.COOLANT_TEMP_COL].max()}')
# print(f'Max diff: {max_temp_diff}')
if (min_temp_diff < max_oil_temp_diff) and (max_temp_diff < max_oil_temp_diff):
usable_laps_list.append(lap)
df_good_laps = pd.concat(usable_laps_list)
df_sessions_list.append(df_good_laps)
# plt.cla() # Removes previous graphs. Shouldnt be needed when we move to Object Oriented
# Loop sessions and plot
plt.figure('Coolant Temp vs. Time')
k = 0
for df_session in df_sessions_list:
limp_mode_graph(df_session, c.TIME_COL, c.COOLANT_TEMP_COL, plot_type='line', marker=None, session_name=c.GRAPH_LABELS_LIST[k], color=c.COLORS_LIST[k])
k+=1
# loop sessions to set max and min ticks for graph
max_rpm = []
min_rpm = []
for df_session in df_sessions_list:
min_rpm.append(min(df_session[c.RPM_COL]))
max_rpm.append(max(df_session[c.RPM_COL]))
min_rpm = min(min_rpm)
max_rpm = max(max_rpm)
k = 0
# Loop sessions and plot
plt.figure('RPM vs. Oil Pressure')
for df_session in df_sessions_list:
limp_mode_graph(df_session, c.RPM_COL, c.OIL_PRESS_COL, plot_type='line', marker='none', session_name=c.GRAPH_LABELS_LIST[k], color=c.COLORS_LIST[k])
k+=1
plt.xticks(np.arange(f.custom_round(min_rpm-1000, 1000-500), f.custom_round(max_rpm+1000, 1000), 500))
# Loop sessions to group rpm values by average oil pressure
sessions_groupby_rpm_list = []
for df_session in df_sessions_list:
df_rpm_groupby = df_session.groupby(c.RPM_COL, group_keys=False)[c.OIL_PRESS_COL].mean().reset_index(name=c.OIL_PRESS_COL)
sessions_groupby_rpm_list.append(df_rpm_groupby)
df_pct_change = pd.merge(sessions_groupby_rpm_list[0], sessions_groupby_rpm_list[1], on=c.RPM_COL)
df_pct_change['% Change Initial'] = ((df_pct_change[f'{c.OIL_PRESS_COL}_x'] - df_pct_change[f'{c.OIL_PRESS_COL}_x']) / df_pct_change[f'{c.OIL_PRESS_COL}_x']) * 100
df_pct_change['% Change'] = ((df_pct_change[f'{c.OIL_PRESS_COL}_y'] - df_pct_change[f'{c.OIL_PRESS_COL}_x']) / df_pct_change[f'{c.OIL_PRESS_COL}_x']) * 100
print(df_pct_change)
# Graph df_pct_change. No loop needed.
plt.figure('RPM % Change')
limp_mode_graph(df_pct_change, c.RPM_COL, '% Change Initial', plot_type='line', marker=None, session_name=c.GRAPH_LABELS_LIST[0], color=c.COLORS_LIST[0])
limp_mode_graph(df_pct_change, c.RPM_COL, '% Change', plot_type='line', marker=None, session_name=c.GRAPH_LABELS_LIST[1], color=c.COLORS_LIST[1])
plt.show()
return None