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

Fix : compatibility with plotly 6 #295 #301

Merged
merged 7 commits into from
Mar 8, 2025
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions scripts/plotly_streamlit.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@

@st.cache_resource()
def get_connection(path: str):
return sqlite3.connect(path, check_same_thread=False)
uri = f"file:{path}?mode=ro&cache=shared"
return sqlite3.connect(uri, uri=True, check_same_thread=False)


def get_data(_conn: Connection):
Expand Down Expand Up @@ -254,14 +255,14 @@ def hms_to_str(t):

# Plot seen species for selected date range and number of species

fig.add_trace(go.Bar(y=top_N_species.index, x=top_N_species, orientation='h', marker_color='seagreen'), row=1, col=1)
fig.add_trace(go.Bar(y=top_N_species.index.tolist(), x=top_N_species.values.tolist(), orientation='h', marker_color='seagreen'), row=1, col=1)

fig.update_layout(
margin=dict(l=0, r=0, t=50, b=0),
yaxis={'categoryorder': 'total ascending'})

# Set 360 degrees, 24 hours for polar plot
theta = np.linspace(0.0, 360, 24, endpoint=False)
theta = np.linspace(0.0, 360, 24, endpoint=False).tolist()

specie_filt = df5 == specie
df3 = df5[specie_filt]
Expand All @@ -271,7 +272,7 @@ def hms_to_str(t):
d = pd.DataFrame(np.zeros((23, 1))).squeeze()
detections = hourly.loc[specie]
detections = (d + detections).fillna(0)
fig.add_trace(go.Barpolar(r=detections, theta=theta, marker_color='seagreen'), row=1, col=2)
fig.add_trace(go.Barpolar(r=detections.tolist(), theta=theta, marker_color='seagreen'), row=1, col=2)
fig.update_layout(
autosize=False,
width=1000,
Expand Down Expand Up @@ -299,7 +300,7 @@ def hms_to_str(t):
)

daily = pd.crosstab(df5, df5.index.date, dropna=True, margins=True)
fig.add_trace(go.Bar(x=daily.columns[:-1], y=daily.loc[specie][:-1], marker_color='seagreen'), row=3, col=2)
fig.add_trace(go.Bar(x=daily.columns[:-1].tolist(), y=daily.loc[specie][:-1].tolist(), marker_color='seagreen'), row=3, col=2)
st.plotly_chart(fig, use_container_width=True) # , config=config)

else:
Expand All @@ -310,7 +311,7 @@ def hms_to_str(t):
specs=[[{"type": "polar", "rowspan": 2}], [{"rowspan": 1}], [{"type": "xy", "rowspan": 1}]]
)
# Set 360 degrees, 24 hours for polar plot
theta = np.linspace(0.0, 360, 24, endpoint=False)
theta = np.linspace(0.0, 360, 24, endpoint=False).tolist()

specie_filt = df5 == specie
df3 = df5[specie_filt]
Expand All @@ -320,7 +321,7 @@ def hms_to_str(t):
d = pd.DataFrame(np.zeros((23, 1))).squeeze()
detections = hourly.loc[specie]
detections = (d + detections).fillna(0)
fig.add_trace(go.Barpolar(r=detections, theta=theta, marker_color='seagreen'), row=1, col=1)
fig.add_trace(go.Barpolar(r=detections.tolist(), theta=theta, marker_color='seagreen'), row=1, col=1)
fig.update_layout(
autosize=False,
width=1000,
Expand Down Expand Up @@ -348,7 +349,7 @@ def hms_to_str(t):
)

daily = pd.crosstab(df5, df5.index.date, dropna=True, margins=True)
fig.add_trace(go.Bar(x=daily.columns[:-1], y=daily.loc[specie][:-1], marker_color='seagreen'), row=3, col=1)
fig.add_trace(go.Bar(x=daily.columns[:-1].tolist(), y=daily.loc[specie][:-1].tolist(), marker_color='seagreen'), row=3, col=1)
st.plotly_chart(fig, use_container_width=True) # , config=config)
df_counts = int(hourly[hourly.index == specie]['All'].iloc[0])
st.subheader('Total Detect:' + str('{:,}'.format(df_counts))
Expand Down Expand Up @@ -377,8 +378,8 @@ def hms_to_str(t):
species[1:],
index=0)

df_counts = int(hourly[hourly.index == specie]['All'])
fig = st.container()
df_counts = int(hourly.loc[hourly.index == specie, 'All'].iloc[0])

fig = make_subplots(rows=1, cols=1)

df4 = df2['Com_Name'][df2['Com_Name'] == specie].resample('15min').count()
Expand All @@ -390,14 +391,14 @@ def hms_to_str(t):
fig_x = [d.strftime('%d-%m-%Y') for d in day_hour_freq.index.tolist()]
fig_y = [h.strftime('%H:%M') for h in day_hour_freq.columns.tolist()]
day_hour_freq.columns = fig_dec_y
fig_z = day_hour_freq.values.transpose()
fig_z = day_hour_freq.values.transpose().tolist()

color_pals = px.colors.named_colorscales()
selected_pal = st.sidebar.selectbox('Select Color Pallet for Daily Detections', color_pals)

heatmap = go.Heatmap(
x=fig_x,
y=day_hour_freq.columns,
y=day_hour_freq.columns.tolist(),
z=fig_z, # heat.values,
showscale=False,
texttemplate="%{text}", autocolorscale=False, colorscale=selected_pal
Expand Down Expand Up @@ -438,7 +439,7 @@ def hms_to_str(t):

plt_topN_today = (df6['Com_Name'].value_counts()[:readings])
freq_order = pd.value_counts(df6['Com_Name']).iloc[:readings].index
fig.add_trace(go.Bar(y=plt_topN_today.index, x=plt_topN_today, marker_color='seagreen', orientation='h'), row=1,
fig.add_trace(go.Bar(y=plt_topN_today.index.tolist(), x=plt_topN_today.values.tolist(), marker_color='seagreen', orientation='h'), row=1,
col=1)

df6['Hour of Day'] = [r.hour for r in df6.index.time]
Expand All @@ -447,6 +448,7 @@ def hms_to_str(t):
heat.index = pd.CategoricalIndex(heat.index, categories=freq_order)
heat.sort_index(level=0, inplace=True)

heat.index = heat.index.astype(str)
heat_plot_values = ma.log(heat.values).filled(0)

hours_in_day = pd.Series(data=range(0, 24))
Expand All @@ -457,7 +459,7 @@ def hms_to_str(t):

labels = heat.values.astype(int).astype('str')
labels[labels == '0'] = ""
fig.add_trace(go.Heatmap(x=heat.columns, y=heat.index, z=heat_values_normalized, # heat.values,
fig.add_trace(go.Heatmap(x=heat.columns.tolist(), y=heat.index.tolist(), z=heat_values_normalized, # heat.values,
showscale=False,
text=labels, texttemplate="%{text}", colorscale='Blugrn'
), row=1, col=2)
Expand Down