Skip to content

Commit

Permalink
ruff and black formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienWehrle committed Dec 2, 2024
1 parent f8011f5 commit df21188
Show file tree
Hide file tree
Showing 13 changed files with 781 additions and 654 deletions.
20 changes: 9 additions & 11 deletions app/Intro.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
st.markdown(
"""
Snowlaps is an open-source python package to study the albedo reducing
effect of red snow algae, mineral dust and black carbon on melting snow
effect of red snow algae, mineral dust and black carbon on melting snow
surfaces. The package is built on a deep-learning emulator of the radiative
transfer model [biosnicar](https://biosnicar.vercel.app/) and can be used
in forward and inverse mode.
In forward mode, the albedo of a snow surface is predicted from the solar
zenith angle, snow grain size, liquid water content, and abundance of
algae, mineral dust and black carbon. In inverse mode, the surface
zenith angle, snow grain size, liquid water content, and abundance of
algae, mineral dust and black carbon. In inverse mode, the surface
properties are retrieved from prescribed spectral measurements. Snowlaps
also directly calculates the albedo-reduction caused by each type of
surface particles.
also directly calculates the albedo-reduction caused by each type of
surface particles.
More details and performance evaluation of the model were presented in a
[recent scientific publication](https://doi.org/10.5194/egusphere-2024-2583).
**👈 Select a mode from the sidebar** to start working with Snowlaps!
"""
)


74 changes: 46 additions & 28 deletions app/pages/1_Snowlaps_forward.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import streamlit as st
import pandas as pd
import plotly.express as px
import streamlit as st

from snowlaps.snowlaps import SnowlapsEmulator

# st.set_page_config(
Expand All @@ -15,22 +16,21 @@
# )



st.markdown(
f"""
"""
### Try snowlaps as a fast two-stream radiative transfer model for snow! :zap:
:point_left: Feed the sidebar with your desired inputs and check the corresponding albedo
spectrum on the graph below, which you can also directly download locally.
:hourglass_flowing_sand: We are working on directly displaying the BBA
reduction associated with each light absorbing particle on the graph!
*Note that impurities are assumed to exist in the upper 2 cm of the snow
:point_left: Feed the sidebar with your desired inputs and check the corresponding albedo
spectrum on the graph below, which you can also directly download locally.
:hourglass_flowing_sand: We are working on directly displaying the BBA
reduction associated with each light absorbing particle on the graph!
*Note that impurities are assumed to exist in the upper 2 cm of the snow
only, and that the snow grain shape is set to spherical.*
"""
)
Expand All @@ -50,10 +50,17 @@
placeholder_button = st.sidebar.empty()


default_values = {"SZA": 42.0, "SOR": 500.0, "AC": 110000.0, "BCC": 800.0, "MDC": 78000.0, "LWC": 0.015}
default_values = {
"SZA": 42.0,
"SOR": 500.0,
"AC": 110000.0,
"BCC": 800.0,
"MDC": 78000.0,
"LWC": 0.015,
}


if placeholder_button.button('Reset'):
if placeholder_button.button("Reset"):
st.session_state.SZA = default_values["SZA"]
st.session_state.SOR = default_values["SOR"]
st.session_state.AC = default_values["AC"]
Expand All @@ -63,42 +70,51 @@


with st.sidebar:

placeholder_title_1.header("Solar geometry")


SZA = placeholder_num_1.number_input(
"Solar Zenith Angle (SZA; degrees)", 0.0, 90.0, value=default_values["SZA"], key="SZA"
"Solar Zenith Angle (SZA; degrees)",
0.0,
90.0,
value=default_values["SZA"],
key="SZA",
)

placeholder_title_2.header("Snow structure ")

optical_radius = placeholder_num_2.number_input(
"Snow optical radius (µm)", 0.0, 1000.0, value=default_values["SOR"], key="SOR"
)

liquid_water_content = placeholder_num_3.number_input(
"Liquid water content (%)", 0.0, 0.1, value=default_values["LWC"], key="LWC"
)

placeholder_title_3.header("Light Absorbing Particles (LAPs)")


algae_concentration = placeholder_num_4.number_input(
"Algae concentration (cells/mL)", 0.0, 1000000.0, value=default_values["AC"], key="AC"
"Algae concentration (cells/mL)",
0.0,
1000000.0,
value=default_values["AC"],
key="AC",
)
black_carbon_concentration = placeholder_num_5.number_input(
"Black carbon concentration (ppb)", 0.0, 10000.0, value=default_values["BCC"], key="BCC"
"Black carbon concentration (ppb)",
0.0,
10000.0,
value=default_values["BCC"],
key="BCC",
)
mineral_dust_concentration = placeholder_num_6.number_input(
"Mineral dust concentration (ppb)", 0.0, 780000.0, value=default_values["MDC"], key="MDC"
"Mineral dust concentration (ppb)",
0.0,
780000.0,
value=default_values["MDC"],
key="MDC",
)






def run_snowlaps(
SZA,
optical_radius,
Expand Down Expand Up @@ -142,6 +158,7 @@ def run_snowlaps(
),
}


def plot_albedo(albedo: pd.Series):
fig = px.line(
result["albedo"],
Expand All @@ -152,6 +169,7 @@ def plot_albedo(albedo: pd.Series):
fig.update_layout(showlegend=False)
return fig


result = run_snowlaps(
SZA,
optical_radius,
Expand Down
81 changes: 33 additions & 48 deletions app/pages/2_Snowlaps_inverse.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,33 @@
import streamlit as st
import pandas as pd
import plotly.express as px
import plotly.graph_objs as go
from snowlaps.snowlaps import SnowlapsEmulator
from importlib import reload
import matplotlib.pyplot as plt
import numpy as np
import copy
import streamlit as st

from snowlaps.snowlaps import SnowlapsEmulator

st.markdown(
f"""
"""
### Try snowlaps to retrieve snow properties from observations! :zap:
#### 1 - Load your files and run the inversion :boom:
First, you must give snowlaps your spectral observations as a csv file
with the spectral observations in the range 350-2500nm (typically measured with
a field spectrometer or a hyperspectral satellite). The unique IDs of your
spectra are given as columns, and the wavelengths are given as index. Then,
a field spectrometer or a hyperspectral satellite). The unique IDs of your
spectra are given as columns, and the wavelengths are given as index. Then,
the metadata associated with the spectra must be prescribed, with the spectral
IDs as index and at minimum 1) the latitude, longitude and time for each of the
measurements or 2) the SZA corresponding to each measurement.
measurements or 2) the SZA corresponding to each measurement.
Once you are done, you can click on 'run inversion' and snowlaps will look for
Once you are done, you can click on 'run inversion' and snowlaps will look for
the surface properties that best explain your measurements.
:bulb: Check [the example files](https://github.com/openosmia/snowlaps-emulator/tree/main/data/spectra)
:bulb: Check [the example files](https://github.com/openosmia/snowlaps-emulator/tree/main/data/spectra)
to see the formatting required.
:hourglass_flowing_sand: We are working on making the back-end code more
:hourglass_flowing_sand: We are working on making the back-end code more
flexible with the input file formatting. Feel free to [share your ideas](https://github.com/openosmia/snowlaps-emulator/discussions)
with us!
Expand All @@ -42,7 +37,6 @@
)



my_emulator = SnowlapsEmulator()


Expand Down Expand Up @@ -91,13 +85,11 @@ def plot_inversion(spectrum):
emulator = st.session_state.best_emulator_spectra[spectrum]
df_m = pd.DataFrame({"measures": st.session_state.albedo_spectra[spectrum]})
df = pd.concat([df_m, emulator], axis=1)
fig = go.Figure(layout=go.Layout(
xaxis=dict(
title="Wavelengths (nm)"
),
yaxis=dict(
title="Albedo"
) ) )
fig = go.Figure(
layout=go.Layout(
xaxis=dict(title="Wavelengths (nm)"), yaxis=dict(title="Albedo")
)
)
fig.add_trace(
go.Scatter(
x=df.index,
Expand All @@ -124,13 +116,11 @@ def plot_inversion(spectrum):

def plot_forward(spectrum, parameters):
df_m = pd.DataFrame({"measures": st.session_state.albedo_spectra[spectrum]})
fig1 = go.Figure(layout=go.Layout(
xaxis=dict(
title="Wavelengths (nm)"
),
yaxis=dict(
title="Albedo"
) ) )
fig1 = go.Figure(
layout=go.Layout(
xaxis=dict(title="Wavelengths (nm)"), yaxis=dict(title="Albedo")
)
)
emulator_results = run_snowlaps(parameters)
df = pd.concat([df_m, emulator_results], axis=1)
df = df.rename(columns={df.columns[1]: "emulator"})
Expand All @@ -157,7 +147,6 @@ def plot_forward(spectrum, parameters):
return fig1



placeholder_title_solar_geometry = st.sidebar.empty()
placeholder_SZA = st.sidebar.empty()

Expand All @@ -184,25 +173,24 @@ def change_input():
with st.spinner("Please wait..."):
best_optimization_results = run_model()
st.session_state.inv = True


st.markdown(
f"""
"""
#### 2 - Visually inspect the inversion :female-detective: and download the results
:point_down: The first graph below displays your measurement with the best fit
found by snowlaps, for any selected measurement.
found by snowlaps, for any selected measurement.
"""
)



if "inv" in st.session_state:
spectrum = st.selectbox(
"Choose spectrum",
Expand All @@ -221,7 +209,7 @@ def change_input():
90.0,
value=st.session_state.best_optimization_results.loc[spectrum]["sza"],
)

placeholder_title_snow_structure.header("Snow structure")

optical_radius = placeholder_optical_radius.number_input(
Expand All @@ -232,7 +220,7 @@ def change_input():
"grain_size"
],
)

liquid_water_content = placeholder_lwc.number_input(
"Liquid water content (%)",
0.0,
Expand Down Expand Up @@ -261,9 +249,6 @@ def change_input():
value=st.session_state.best_optimization_results.loc[spectrum]["dust"],
)




parameters = [
SZA,
optical_radius,
Expand All @@ -272,13 +257,13 @@ def change_input():
black_carbon_concentration,
mineral_dust_concentration,
]

st.markdown(
f"""
"""
:point_left: A side bar appeared! The second gaph lets you play with the
:point_left: A side bar appeared! The second gaph lets you play with the
surface parameters retrieved by snowlaps and check how it changes the albedo.
You can for example check what the albedo would look like if there was no algal
bloom, or a much bigger grain size? :snowman:
Expand Down
14 changes: 6 additions & 8 deletions app/pages/3_Get_support.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@authors: Adrien Wehrlé (University of Zurich), Lou-Anne Chevrollier (Aarhus University)
Expand All @@ -10,16 +9,15 @@

st.markdown(
"""
If you run into an issue using the app and need support, feel free to use
the [discussion forum](https://github.com/openosmia/snowlaps-emulator/discussions)
If you run into an issue using the app and need support, feel free to use
the [discussion forum](https://github.com/openosmia/snowlaps-emulator/discussions)
of the snowlaps repository so we can work out the problem :female-detective:
If a new feature would be handy for you or that you identify a bug, please
[report it](https://github.com/openosmia/snowlaps-emulator/issues) so we
[report it](https://github.com/openosmia/snowlaps-emulator/issues) so we
can fix it! :comet:
"""
)

Loading

0 comments on commit df21188

Please sign in to comment.