7. Chlorophyll estimate trends
This notebook is optimized to run in Google Colab.
In [ ]:
Copied!
!pip install fast-dash
!pip install fast-dash
In [ ]:
Copied!
import datetime
import pandas as pd
import plotly.express as px
from fast_dash import fastdash, Graph
import datetime
import pandas as pd
import plotly.express as px
from fast_dash import fastdash, Graph
In [ ]:
Copied!
# If you get a ConnectionError, please retry.
data = pd.read_csv("https://raw.githubusercontent.com/dkedar7/fast_dash/docs/docs/Examples/assets/chla_subset.csv")
data['date_acquired'] = pd.to_datetime(data["date_acquired"])
data = data.sort_values(["date_acquired", "gnis_name"])
data
# If you get a ConnectionError, please retry.
data = pd.read_csv("https://raw.githubusercontent.com/dkedar7/fast_dash/docs/docs/Examples/assets/chla_subset.csv")
data['date_acquired'] = pd.to_datetime(data["date_acquired"])
data = data.sort_values(["date_acquired", "gnis_name"])
data
In [ ]:
Copied!
Let's build a simple function to display trends¶
In [ ]:
Copied!
#@fastdash(mode="external", port=5000)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist()) -> Graph:
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[data["gnis_name"] == lake_name]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
return trend
#@fastdash(mode="external", port=5000)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist()) -> Graph:
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[data["gnis_name"] == lake_name]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
return trend
In [ ]:
Copied!
plot_chlorophyll_estimates("Cayuta Lake")
plot_chlorophyll_estimates("Cayuta Lake")
In [ ]:
Copied!
Display mean and median values¶
In [ ]:
Copied!
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
AB
CC
CC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist()) -> (str, str, Graph):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[data["gnis_name"] == lake_name]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
AB
CC
CC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist()) -> (str, str, Graph):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[data["gnis_name"] == lake_name]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend
In [ ]:
Copied!
Add date filters¶
In [ ]:
Copied!
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
AB
CC
CC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist(),
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2023, 12, 31)) -> (str, str, Graph):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[(data["gnis_name"] == lake_name) & (data["date_acquired"].between(start_date, end_date))]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
AB
CC
CC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist(),
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2023, 12, 31)) -> (str, str, Graph):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[(data["gnis_name"] == lake_name) & (data["date_acquired"].between(start_date, end_date))]
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend
In [ ]:
Copied!
How do we draw a map?¶
In [ ]:
Copied!
!pip install folium
!pip install folium
In [ ]:
Copied!
import folium
from fast_dash import html
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
DDAB
CCCC
CCCC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist(),
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2023, 12, 31)) -> (str, str, Graph, html.Iframe()):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[(data["gnis_name"] == lake_name) & (data["date_acquired"].between(start_date, end_date))]
centroid_latitude = filtered_data["centroid_latitude"].dropna().iloc[0]
centroid_longitude = filtered_data["centroid_longitude"].dropna().iloc[0]
map_ = folium.Map(location=(centroid_latitude, centroid_longitude), zoom_start=13)._repr_html_()
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend, map_
import folium
from fast_dash import html
# Define the layout of the app. Alphabets denote the placement of each output
# In this layout, we are telling the app to display the first two outputs (A and B) next to each other,
# and let the third output occupy double the width in the second row.
output_layout = """
DDAB
CCCC
CCCC
"""
@fastdash(mode="external", port=5000, mosaic=output_layout)
def plot_chlorophyll_estimates(lake_name: str = data.gnis_name.unique().tolist(),
start_date=datetime.date(2015, 1, 1),
end_date=datetime.date(2023, 12, 31)) -> (str, str, Graph, html.Iframe()):
"""
Plot Landsat-driven estimation of Chlorophyll-a values in island waters of NY state.
"""
filtered_data = data[(data["gnis_name"] == lake_name) & (data["date_acquired"].between(start_date, end_date))]
centroid_latitude = filtered_data["centroid_latitude"].dropna().iloc[0]
centroid_longitude = filtered_data["centroid_longitude"].dropna().iloc[0]
map_ = folium.Map(location=(centroid_latitude, centroid_longitude), zoom_start=13)._repr_html_()
trend = px.line(data_frame=filtered_data, x="date_acquired", y="predictions", template="simple_white")
mean = f"{round(filtered_data['predictions'].mean(), 2)} ug/L"
median = f"{round(filtered_data['predictions'].median(), 2)} ug/L"
return mean, median, trend, map_
In [ ]:
Copied!