File size: 1,424 Bytes
c0d813b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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()
    )