Spaces:
Sleeping
Sleeping
| import pandas as pd | |
| import altair as alt | |
| """ | |
| app_plot_utils.py | |
| Description: This file contains utility functions for generating interactive plots | |
| using the Altair library. These functions are designed for visualizing fish count data | |
| obtained from processed videos and historical records. | |
| Author: Austin Powell | |
| """ | |
| def plot_count_date(dataframe): | |
| """Plots counts vs relative time for uploaded video.""" | |
| dataframe["seconds"] = dataframe["timestamps"] / 1000 | |
| dataframe["class"] = "Herring" # TBD: Hard-coded for now | |
| return ( | |
| alt.Chart(dataframe, title="Processed video detected fish") | |
| .mark_line() | |
| .encode(x="seconds", y="fish_count", color="class") | |
| .interactive() | |
| ) | |
| def plot_historical_data(dataframe): | |
| """Returns altair plot of historical counts to be rendered on main dashboard.""" | |
| dataframe["Date"] = pd.to_datetime(dataframe["Date"]) | |
| s = ( | |
| dataframe.resample(rule="D", on="Date")["Count"].sum().reset_index() | |
| ) # Resample on day | |
| return ( | |
| alt.Chart(s, title="Historical Video Counts of Herring") | |
| .mark_bar() | |
| .transform_window( | |
| # The field to average | |
| rolling_mean="mean(Count)", | |
| # The number of values before and after the current value to include. | |
| frame=[-9, 0], | |
| ) | |
| .encode(x="Date", y="Count", tooltip=["Count", "Date"]) | |
| .interactive() | |
| ) |