Updating script/daily_plot.py to satisfy pep8 style guide

Used 'autopep8 --in-place --aggressive scripts/daily_plot.py' as initial style fixes.
This commit is contained in:
Jake Herbst
2022-05-11 07:56:41 -04:00
parent 26079e5696
commit 3e20ee732e
+92 -25
View File
@@ -12,7 +12,8 @@ userDir = os.path.expanduser('~')
conn = sqlite3.connect(userDir + '/BirdNET-Pi/scripts/birds.db')
df = pd.read_sql_query("SELECT * from detections", conn)
cursor = conn.cursor()
cursor.execute('SELECT * FROM detections WHERE Date = DATE(\'now\', \'localtime\')')
cursor.execute(
'SELECT * FROM detections WHERE Date = DATE(\'now\', \'localtime\')')
table_rows = cursor.fetchall()
@@ -37,17 +38,29 @@ df_plt_today = df_plt[df_plt['Date']==now.strftime("%Y-%m-%d")]
readings = 10
plt_top10_today = (df_plt_today['Com_Name'].value_counts()[:readings])
df_plt_top10_today = df_plt_today[df_plt_today.Com_Name.isin(plt_top10_today.index)]
df_plt_top10_today = df_plt_today[df_plt_today.Com_Name.isin(
plt_top10_today.index)]
# Set Palette for graphics
pal = "Greens"
# Set up plot axes and titles
f, axs = plt.subplots(1, 2, figsize = (10, 4), gridspec_kw=dict(width_ratios=[3, 6]), facecolor='#77C487')
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0, hspace=0)
f, axs = plt.subplots(
1, 2, figsize=(
10, 4), gridspec_kw=dict(
width_ratios=[
3, 6]), facecolor='#77C487')
plt.subplots_adjust(
left=None,
bottom=None,
right=None,
top=None,
wspace=0,
hspace=0)
# generate y-axis order for all figures based on frequency
freq_order = pd.value_counts(df_plt_top10_today['Com_Name']).iloc[:readings].index
freq_order = pd.value_counts(
df_plt_top10_today['Com_Name']).iloc[:readings].index
# make color for max confidence --> this groups by name and calculates max conf
confmax = df_plt_top10_today.groupby('Com_Name')['Confidence'].max()
@@ -59,21 +72,28 @@ norm = plt.Normalize(confmax.values.min(), confmax.values.max())
colors = plt.cm.Greens(norm(confmax))
# Generate frequency plot
plot=sns.countplot(y='Com_Name', data = df_plt_top10_today, palette = colors, order=freq_order, ax=axs[0])
plot = sns.countplot(
y='Com_Name',
data=df_plt_top10_today,
palette=colors,
order=freq_order,
ax=axs[0])
#Try plot grid lines between bars - problem at the moment plots grid lines on bars - want between bars
# Try plot grid lines between bars - problem at the moment plots grid
# lines on bars - want between bars
z = plot.get_ymajorticklabels()
plot.set_yticklabels(['\n'.join(textwrap.wrap(ticklabel.get_text(),15)) for ticklabel in plot.get_yticklabels()], fontsize = 10)
plot.set_yticklabels(['\n'.join(textwrap.wrap(ticklabel.get_text(), 15))
for ticklabel in plot.get_yticklabels()], fontsize=10)
plot.set(ylabel=None)
plot.set(xlabel="Detections")
# Generate crosstab matrix for heatmap plot
heat = pd.crosstab(df_plt_top10_today['Com_Name'],df_plt_top10_today['Hour of Day'])
heat = pd.crosstab(
df_plt_top10_today['Com_Name'],
df_plt_top10_today['Hour of Day'])
# Order heatmap Birds by frequency of occurrance
heat.index = pd.CategoricalIndex(heat.index, categories=freq_order)
heat.sort_index(level=0, inplace=True)
@@ -84,7 +104,20 @@ heat_frame = pd.DataFrame(data=0, index=heat.index, columns = hours_in_day)
heat = (heat + heat_frame).fillna(0)
# Generatie heatmap plot
plot = sns.heatmap(heat, norm=LogNorm(), annot=True, annot_kws={"fontsize":7}, fmt="g", cmap = pal , square = False, cbar=False, linewidths = 0.5, linecolor = "Grey", ax=axs[1], yticklabels = False)
plot = sns.heatmap(
heat,
norm=LogNorm(),
annot=True,
annot_kws={
"fontsize": 7},
fmt="g",
cmap=pal,
square=False,
cbar=False,
linewidths=0.5,
linecolor="Grey",
ax=axs[1],
yticklabels=False)
plot.set_xticklabels(plot.get_xticklabels(), rotation=0, size=7)
# Set heatmap border
@@ -99,7 +132,8 @@ plt.suptitle("Top 10 Last Updated: "+ str(now.strftime("%Y-%m-%d %H:%M")))
# Save combined plot
userDir = os.path.expanduser('~')
savename=userDir + '/BirdSongs/Extracted/Charts/Combo-'+str(now.strftime("%Y-%m-%d"))+'.png'
savename = userDir + '/BirdSongs/Extracted/Charts/Combo-' + \
str(now.strftime("%Y-%m-%d")) + '.png'
plt.savefig(savename)
plt.show()
plt.close()
@@ -107,18 +141,30 @@ plt.close()
# Get Bottom detection frequency
plt_Bot10_today = (df_plt_today['Com_Name'].value_counts()[-readings:])
df_plt_Bot10_today = df_plt_today[df_plt_today.Com_Name.isin(plt_Bot10_today.index)]
df_plt_Bot10_today = df_plt_today[df_plt_today.Com_Name.isin(
plt_Bot10_today.index)]
# Set Palette for graphics
pal = "Reds"
# Set up plot axes and titles
f, axs = plt.subplots(1, 2, figsize = (10, 4), gridspec_kw=dict(width_ratios=[3, 6]), facecolor='#77C487')
plt.subplots_adjust(left=None, bottom=None, right=None, top=None, wspace=0, hspace=0)
f, axs = plt.subplots(
1, 2, figsize=(
10, 4), gridspec_kw=dict(
width_ratios=[
3, 6]), facecolor='#77C487')
plt.subplots_adjust(
left=None,
bottom=None,
right=None,
top=None,
wspace=0,
hspace=0)
# generate y-axis order for all figures based on frequency
freq_order = pd.value_counts(df_plt_Bot10_today['Com_Name']).iloc[-readings:].index
freq_order = pd.value_counts(
df_plt_Bot10_today['Com_Name']).iloc[-readings:].index
# make color for max confidence --> this groups by name and calculates max conf
confmax = df_plt_Bot10_today.groupby('Com_Name')['Confidence'].max()
@@ -129,20 +175,27 @@ norm = plt.Normalize(confmax.values.min(), confmax.values.max())
colors = plt.cm.Reds(norm(confmax))
# Generate frequency plot
plot=sns.countplot(y='Com_Name', data = df_plt_Bot10_today, palette = colors, order=freq_order, ax=axs[0])
plot = sns.countplot(
y='Com_Name',
data=df_plt_Bot10_today,
palette=colors,
order=freq_order,
ax=axs[0])
#Try plot grid lines between bars - problem at the moment plots grid lines on bars - want between bars
# Try plot grid lines between bars - problem at the moment plots grid
# lines on bars - want between bars
z = plot.get_ymajorticklabels()
plot.set_yticklabels(['\n'.join(textwrap.wrap(ticklabel.get_text(),15)) for ticklabel in plot.get_yticklabels()], fontsize = 10)
plot.set_yticklabels(['\n'.join(textwrap.wrap(ticklabel.get_text(), 15))
for ticklabel in plot.get_yticklabels()], fontsize=10)
plot.set(ylabel=None)
plot.set(xlabel="Detections")
# Generate crosstab matrix for heatmap plot
heat = pd.crosstab(df_plt_Bot10_today['Com_Name'],df_plt_Bot10_today['Hour of Day'])
heat = pd.crosstab(
df_plt_Bot10_today['Com_Name'],
df_plt_Bot10_today['Hour of Day'])
# Order heatmap Birds by frequency of occurrance
heat.index = pd.CategoricalIndex(heat.index, categories=freq_order)
heat.sort_index(level=0, inplace=True)
@@ -153,7 +206,20 @@ heat_frame = pd.DataFrame(data=0, index=heat.index, columns = hours_in_day)
heat = (heat + heat_frame).fillna(0)
# Generatie heatmap plot
plot = sns.heatmap(heat, norm=LogNorm(), annot=True, fmt="g", annot_kws={"fontsize":7}, cmap = pal , square = False, cbar=False, linewidths = 0.5, linecolor = "Grey", ax=axs[1], yticklabels = False)
plot = sns.heatmap(
heat,
norm=LogNorm(),
annot=True,
fmt="g",
annot_kws={
"fontsize": 7},
cmap=pal,
square=False,
cbar=False,
linewidths=0.5,
linecolor="Grey",
ax=axs[1],
yticklabels=False)
plot.set_xticklabels(plot.get_xticklabels(), rotation=0, size=7)
# Set heatmap border
@@ -167,7 +233,8 @@ f.subplots_adjust(top=0.9)
plt.suptitle("Bottom 10 Last Updated: " + str(now.strftime("%Y-%m-%d %H:%M")))
# Save combined plot
savename=userDir + '/BirdSongs/Extracted/Charts/Combo2-'+str(now.strftime("%Y-%m-%d"))+'.png'
savename = userDir + '/BirdSongs/Extracted/Charts/Combo2-' + \
str(now.strftime("%Y-%m-%d")) + '.png'
plt.savefig(savename)
plt.show()
plt.close()