我想把这段代码嵌套到函数中。我想让它更通用,更自动化,更参数化。
以下是当前工作的代码:
# Generate trial data set
data = {'date_time':['2017-03-22 10:07','2017-03-22 10:08','2017-03-22 10:09','2017-03-22 10:10','2017-03-22 10:11','2017-03-22 10:12','2017-03-22 10:13',
'2017-03-22 10:14','2017-03-22 10:15','2017-03-22 10:16','2017-03-22 10:17','2017-03-22 10:18','2017-03-22 10:19', '2017-03-22 10:20',
'2017-03-22 10:21','2017-03-22 10:22','2017-03-22 10:23','2017-03-22 10:24','2017-03-22 10:25','2017-03-22 10:26','2017-03-22 10:27',
'2017-03-22 10:28','2017-03-22 10:29','2017-03-22 10:30','2017-03-22 10:31','2017-03-22 10:32','2017-03-22 10:33','2017-03-22 10:34',
'2017-03-22 10:35','2017-03-22 10:36','2017-03-22 10:37','2017-03-22 10:38','2017-03-22 10:39','2017-03-22 10:40','2017-03-22 10:41',
'2017-03-22 10:42','2017-03-22 10:43','2017-03-22 10:44','2017-03-22 10:45','2017-03-22 10:46','2017-03-22 10:47','2017-03-22 10:48',
'2017-03-22 10:49','2017-03-22 10:50','2017-03-22 10:51','2017-03-22 10:52','2017-03-22 10:53','2017-03-22 10:54'],
'prod_line_no':[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1],
'Signal1':[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0],
'Signal2':[0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0 , 0, 0, 1, 1, 0, 0, 0],
'var1':[20, 2, 2, 2, 20, 21, 2, 18, 20, 21, 1, 18, 10, 6, 9, 8, 10, 3, 9, 8, 7, 18, 20, 21, 20, 2, 2, 2, 20, 21, 2, 18, 20, 21, 1, 18, 10, 6, 9, 8, 10, 3, 9, 8, 7, 18, 20, 21],
'var2':[20, 21, 19, 18, 20, 21, 19, 18, 20, 21, 19, 18, 10, 11, 9, 8, 10, 11, 9, 8, 19, 18, 20, 21, 20, 21, 19, 18, 20, 21, 19, 18, 20, 21, 19, 18, 10, 11, 9, 8, 10, 11, 9, 8, 19, 18, 20, 21],
'running':[0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
'SKU':['NA','RO','RO','RO', 'NA','NA','NA','NA','RA','RA','RA','RA', 'NA','NA','NA','NA','RO','RO','RO','RO','RO','RO','RO','RO', 'NA','RO','RO','RO', 'NA','NA','NA','NA','RA','RA','RA','RA', 'NA','NA','NA','NA','RO','RO','RO','RO','RO','RO','RO','RO']
}
df=pd.DataFrame(data)
# Build the phase signal based on two signals
# To be reworked to become a function
df2=df.copy()
FrontUpDict={1.0:1.0, 0.0:0.0, -1.0:0.0} # keep only signal front up
FrontDownDict={1.0:0.0, 0.0:0.0, -1.0:-1.0} # keep only signal front down
df2['Phase']=(df2['Signal1'].diff().map(FrontUpDict)+df2['Signal2'].diff().map(FrontDownDict)).cumsum()
df2[['date_time', 'Signal1', 'Signal2','Phase']].head(30)
以下是它的表现:
在初始信号子块上打标并建立新的信号
或者是代码:
from plotly.subplots import make_subplots
import plotly.graph_objects as go
row=3
fig = make_subplots(rows=row, cols=1)
fig.append_trace(go.Scatter(
x=df2.date_time,
y=df2.Signal1
), row=1, col=1)
fig.append_trace(go.Scatter(
x=df2.date_time,
y=df2.Signal2,
), row=2, col=1)
fig.append_trace(go.Scatter(
x=df2.date_time,
y=df2.Phase,
), row=3, col=1)
fig.update_layout(height=500, width=1100, title_text="Stacked Subplots")
fig.show()
现在我想把它嵌套在一个函数中,使我的代码更干净、更易操作,但我失败了。
这里是我当前的代码(对不起,我重命名了一些变量):
# Function to build a boolean signal between to events
import numpy as np
def signalBuilder(df,s1,s2,T1,T2):
# df as 'DataFrame' = DataFrame
# s1 as 'Series'= signal1 or event1, -> 'df.sigal1'
# s2 as 'Series'= signal2 or event2, -> 'df.sigal2'
# T1 as 'str'= trigger for signal1 or event1, -> EdgeUp or EdgeDown
# T2 as 'str'= trigger for signal2 or event2, -> EdgeUp or EdgeDown
EdgeUp={1.0:1.0, 0.0:0.0, -1.0:0.0} # keep only signal front up
EdgeDown={1.0:0.0, 0.0:0.0, -1.0:-1.0} # keep only signal front down
df['Phase']=(df[s1].diff().map(T1)+df[s2].diff().map(T2)).cumsum()
以下是错误消息:
错误消息
我可以修改什么?如何修改它,使新的信号名称包括初始信号名称,例如“phase\u signal1\u signal2”
暂无答案!
目前还没有任何答案,快来回答吧!