Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to add ruptures code #267

Closed
waad876 opened this issue Oct 3, 2022 · 7 comments
Closed

Trying to add ruptures code #267

waad876 opened this issue Oct 3, 2022 · 7 comments

Comments

@waad876
Copy link

waad876 commented Oct 3, 2022

I am trying to add the ruptures code but unfortunately i couldn't get any result.

here is the code :

@app.callback(
dash.dependencies.Output(component_id='chart_1', component_property="figure"),
[dash.dependencies.Input('btn-nclicks-1', 'n_clicks')],
[dash.dependencies.State('drop_down_1', 'value')],
[dash.dependencies.State('drop_down_2', 'value')],
[dash.dependencies.State('drop_down_3', 'value')],
[dash.dependencies.State('drop_down_4', 'value')],
prevent_initial_call=True
)
def update_values_app1(_, regions, indicators, start_date, end_date):
fig = go.Figure()

max_value_dict = dict()

if type(regions) == str:
    regions = [regions]

for region in regions:
    cd = global_df.loc[(global_df.loc[:, "country_region"] == region)]
    cd = cd.loc[((cd.loc[:, "date"] >= start_date) & (cd.loc[:, "date"] <= end_date))]
    x = cd["date"].values

    for iid, indicator in enumerate(indicators):
        y = cd[indicator].values
        notna_mask = ~np.isnan(y)

        if notna_mask.sum() == 0:
            max_value = 1
        else:
            max_value = max(abs(np.nanmax(y)), abs(np.nanmin(y)))

        if indicator not in max_value_dict:
            max_value_dict[indicator] = max_value
        else:
            max_value_dict[indicator] = max(max_value, max_value_dict[indicator])

        if iid == 0:
            fig = fig.add_trace(
                go.Scatter(x=x, y=y, mode='lines', name=region + " " + indicator,
                           line=dict(width=4)))
        else:
            fig = fig.add_trace(
                go.Scatter(x=x, y=y, mode='lines', name=region + " " + indicator, yaxis='y{}'.format(iid + 2),
                           line=dict(width=4)))

fig.update_layout(title="", xaxis_title="Date", yaxis_title="", legend_title="Indicators",
                  font=dict(family="Arial", size=20, color="dark blue"))

y_axis_label_width = 0.08

fig.update_layout(xaxis=dict(domain=[y_axis_label_width * (len(indicators) - 1), 1.0]))

for iid, indicator in enumerate(indicators):
    y_range = [-max_value_dict[indicator], max_value_dict[indicator]]
    if iid == 0:
        fig.update_layout({'yaxis': dict(title=indicator, constraintoward='center', position=0,
                                         range=y_range)})
    else:
        fig.update_layout({'yaxis{}'.format(iid + 2): dict(title=indicator, overlaying="y", side="left",
                                                           constraintoward='center', position=y_axis_label_width * iid,
                                                           range=y_range)})

fig.update_layout(legend=dict(font=dict(family="Arial", size=30, color="black")),
                  legend_title=dict(font=dict(family="Arial", size=35, color="blue")))
fig.update_layout(legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1))

fig.update_layout(margin=dict(t=250))
fig.update_layout(xaxis_tickangle=0)
fig.update_xaxes(showline=True, linewidth=2, linecolor='black')
fig.update_yaxes(showline=True, linewidth=2, linecolor='black')
fig.update_xaxes(zeroline=True, zerolinewidth=2, zerolinecolor='red')
fig.update_yaxes(zeroline=True, zerolinewidth=2, zerolinecolor='red')

return fig
@deepcharles
Copy link
Owner

Hi, please paste the error so that we can understand your problem. Also there is a lot of code not related to ruptures, can you please remove anything that is not related this library?

@waad876
Copy link
Author

waad876 commented Oct 5, 2022 via email

@deepcharles
Copy link
Owner

Ok. Can you give more details about what you are trying to achieve?

@waad876
Copy link
Author

waad876 commented Oct 5, 2022 via email

@deepcharles
Copy link
Owner

Please look at the examples in the documentation to help you start with the code. The most simple example would be

# `signal` contains your signal
algo = rpt.Pelt(model="rbf").fit(signal)
result = algo.predict(pen=10)  # penalty of 10
# `result` is the list of changes

@waad876
Copy link
Author

waad876 commented Oct 7, 2022 via email

@deepcharles
Copy link
Owner

Closing now. Feel free to reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants