New and improved Species Stats

This commit is contained in:
CaiusX
2022-05-18 14:35:34 +02:00
parent f59762229f
commit c859c2250b
+62 -9
View File
@@ -60,7 +60,8 @@ df2=df2.set_index('DateTime')
# Date as slider
Start_Date = pd.to_datetime(df2.index.min()).date()
End_Date = pd.to_datetime(df2.index.max()).date()
Date_Slider = st.slider('Date Range',
cols1,cols2= st.columns((1,1))
Date_Slider = cols1.slider('Date Range',
min_value = Start_Date-timedelta(days=1),
max_value = End_Date,
value=(Start_Date,
@@ -72,14 +73,29 @@ Date_Slider = st.slider('Date Range',
filt = (df2.index >= pd.Timestamp(Date_Slider[0])) & (df2.index <= pd.Timestamp(Date_Slider[1]+timedelta(days=1)))
df2 = df2[filt]
st.write('<style>div.row-widget.stRadio > div{flex-direction:row;justify-content: left;} </style>', unsafe_allow_html=True)
st.write('<style>div.st-bf{flex-direction:column;} div.st-ag{font-weight:bold;padding-left:2px;}</style>', unsafe_allow_html=True)
resample_sel=cols2.radio("Select Resample Resolution - To downsample and make run faster select longer period, Daily provides a view on detections at 15 min intervals through the day", ('1 minute', '5 minutes', '10 minutes', 'Hourly', 'Daily'))
resample_times = {'1 minute':'1min',
'5 minutes':'5min',
'10 minutes':'10min',
'Hourly':'1H',
'Daily':'1D'
}
resample_time = resample_times[resample_sel]
df5=df2.resample(resample_time)['Com_Name'].aggregate('unique').explode()
#Create species count for selected date range
Specie_Count=df2['Com_Name'].value_counts()
Specie_Count=df5.value_counts()
#Create species treemap
# Create Hourly Crosstab
hourly=pd.crosstab(df2['Com_Name'],df2.index.hour, dropna=False)
hourly=pd.crosstab(df5,df5.index.hour, dropna=False)
# Filter on species
species = list(hourly.index)
@@ -91,7 +107,7 @@ top_N = cols1.slider(
value=min(10,len(Specie_Count))
)
top_N_species = (df2['Com_Name'].value_counts()[:top_N])
top_N_species = (df5.value_counts()[:top_N])
specie = cols2.selectbox('Which bird would you like to explore for the dates '+str(Date_Slider[0])+' to '+str(Date_Slider[1])+'?', species,
@@ -104,13 +120,13 @@ font_size=15
#specie filter
filt=df2['Com_Name']==specie
df_counts=df2[filt].resample('D').count()
df_counts=sum(df5==specie)
fig = make_subplots(
rows=3, cols =2,
specs= [[{"type":"xy","rowspan":3}, {"type":"polar","rowspan":2}], [{"rowspan":1}, {"rowspan":1} ], [None, {"type":"xy","rowspan":1}]],
subplot_titles=('<b>Top '+ str(top_N) + ' Species in Date Range '+str(Date_Slider[0])+' to '+str(Date_Slider[1])+'</b>',
'Total Detect:'+str('{:,}'.format(sum(df_counts.Time)))+
'Total Detect:'+str('{:,}'.format(df_counts))+
' Confidence Max:'+str('{:.2f}%'.format(max(df2[df2['Com_Name']==specie]['Confidence'])*100))+
' '+' Median:'+str('{:.2f}%'.format(np.median(df2[df2['Com_Name']==specie]['Confidence'])*100))
)
@@ -123,14 +139,26 @@ fig.add_trace(go.Bar(y=top_N_species.index, x=top_N_species, orientation='h'), r
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)
specie_filt= df5==specie
df3=df5[specie_filt]
detections2= pd.crosstab(df3, df3.index.hour)
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), row=1, col=2)
if resample_time != '1D':
fig.add_trace(go.Barpolar(r = detections, theta=theta), row=1, col=2)
fig.update_layout(
autosize=False,
width = 1000,
@@ -139,7 +167,7 @@ fig.update_layout(
polar = dict(
radialaxis = dict(
tickfont_size = font_size,
showticklabels = True,
showticklabels = False,
hoverformat = "#%{theta}: <br>Popularity: %{percent} </br> %{r}"
),
angularaxis = dict(
@@ -156,10 +184,35 @@ fig.update_layout(
daily=pd.crosstab(df2['Com_Name'],df2.index.date, dropna=False)
daily=pd.crosstab(df5,df5.index.date, dropna=False)
fig.add_trace(go.Bar(x=daily.columns, y=daily.loc[specie]), row=3, col=2)
else:
fig = make_subplots(
rows=1, cols =2,
# specs= [[{"type":"xy","rowspan":1}],
# [{"rowspan":1}],
# ],
# subplot_titles=('<b>Top '+ str(top_N) + ' Species in Date Range '+str(Date_Slider[0])+' to '+str(Date_Slider[1])+'</b>',
# 'Total Detect:'+str('{:,}'.format(df_counts))+
# ' Confidence Max:'+str('{:.2f}%'.format(max(df2[df2['Com_Name']==specie]['Confidence'])*100))+
# ' '+' Median:'+str('{:.2f}%'.format(np.median(df2[df2['Com_Name']==specie]['Confidence'])*100))
# )
)
df4=df2['Com_Name'][df2['Com_Name']==specie].resample('15min').count()
df4.index=[df4.index.date, df4.index.time]
day_hour_freq=df4.unstack().fillna(0)
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()]
fig_z = day_hour_freq.values.transpose()
fig_heatmap = go.Figure(data=go.Heatmap(x=fig_x,y=fig_y,z=fig_z))
fig.add_trace(go.Bar(y=top_N_species.index, x=top_N_species, orientation='h'), row=1,col=1)
fig.update_layout(
margin=dict(l=0, r=0, t=50, b=0),
yaxis={'categoryorder':'total ascending'})
fig.add_trace(go.Heatmap(x=fig_x,y=fig_y,z=fig_z, autocolorscale = False, colorscale = 'blackbody'), row=1, col=2)
# container=st.container()
# config={'displayModelBar': False}
st.plotly_chart(fig, use_container_width=True) #, config=config)