-
-
Notifications
You must be signed in to change notification settings - Fork 839
Description
What is your suggestion?
Can someone help to correct this code which is generated wth copilot : I want to use a slider to change the interval (in seconds) on which he calculates the mean value of the 3th signal between the 2 vertical rulers. The first rule is fixed and the second ruler must vary an the mean value must adapt itself. The value if the slider must be added to the time of the first vertical ruler.
I have generated different variants of this code but none of them works
import pandas as pd
import numpy as np
import altair as alt
Sample data
data = {
'timestamp': pd.date_range(start='2023-01-01 00:00:00', periods=100, freq='s'),
'signal1': np.random.randn(100).cumsum(),
'signal2': np.random.randn(100).cumsum(),
'signal3': np.random.randn(100).cumsum()
}
df = pd.DataFrame(data)
Find the index where the difference between signal1 and signal2 is minimal
min_diff_index = (df['signal1'] - df['signal2']).abs().idxmin()
print(min_diff_index)
Create a slider for the interval
slider = alt.binding_range(min=1, max=20, step=1, name='Interv')
interval = alt.param(value=10, bind=slider)
Calculate the mean value of signal3 between the min_diff_index and the next interval seconds
def calculate_mean_signal3(df, min_diff_index, interval):
print(min_diff_index, interval, len(df))
end_index = min((min_diff_index + interval), len(df))
end_index = min_diff_index + 10
print('end_index :', end_index)
return df['signal3'][min_diff_index:end_index].mean()
mean_signal3 = calculate_mean_signal3(df, min_diff_index, interval.value)
print('mean :', mean_signal3)
Create the Altair chart with formatted tooltip
base = alt.Chart(df).encode(
x=alt.X('timestamp:T', title='Time')
)
line1 = base.mark_line(color='blue').encode(
y=alt.Y('signal1:Q', title='Signal 1'),
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S'), alt.Tooltip('signal1:Q', title='Signal 1')]
)
line2 = base.mark_line(color='red').encode(
y=alt.Y('signal2:Q', title='Signal 2'),
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S'), alt.Tooltip('signal2:Q', title='Signal 2')]
)
line3 = base.mark_line(color='green').encode(
y=alt.Y('signal3:Q', title='Signal 3'),
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S'), alt.Tooltip('signal3:Q', title='Signal 3')]
)
rule1 = alt.Chart(pd.DataFrame({'timestamp': [df['timestamp'][min_diff_index]]})).mark_rule(color='black').encode(
x='timestamp:T',
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S')]
)
rule2 = alt.Chart(pd.DataFrame({'timestamp': [df['timestamp'][min_diff_index + 10]]})).mark_rule(color='black').encode(
x='timestamp:T',
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S')]
)
mean_line = alt.Chart(pd.DataFrame({
'timestamp': [df['timestamp'][min_diff_index], df['timestamp'][min_diff_index + 10]],
'mean_signal3': [mean_signal3, mean_signal3]
})).mark_line(color='purple').encode(
x='timestamp:T',
y='mean_signal3:Q',
tooltip=[alt.Tooltip('timestamp:T', title='Time', format='%Y-%m-%d %H:%M:%S'), alt.Tooltip('mean_signal3:Q', title='Mean Signal 3')]
)
chart = alt.layer(line1, line2, line3, rule1, rule2, mean_line).properties(
width=800,
height=400,
title='Signals with Minimal Difference and Mean Value'
).add_params(interval)
chart.interactive()
Have you considered any alternative solutions?
No response
