Skip to content

Commit

Permalink
Added Flake8 to github actions and cleaned code to pass it
Browse files Browse the repository at this point in the history
  • Loading branch information
danyoungday committed Jul 23, 2024
1 parent 523c3e8 commit 982ae00
Show file tree
Hide file tree
Showing 49 changed files with 168 additions and 111 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/eluc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ jobs:
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with PyLint
run: pylint ./*
- name: Lint with Flake8
run: flake8
- name: Run unit tests
run: python -m unittest

2 changes: 2 additions & 0 deletions use_cases/eluc/.flake8
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[flake8]
max-line-length = 120
6 changes: 3 additions & 3 deletions use_cases/eluc/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
For a given context cell representing a portion of the earth,
identified by its latitude and longitude coordinates, and a given year:
* What changes can we make to the land usage
* In order to minimize the resulting estimated CO2 emissions? (Emissions from Land Use Change, ELUC,
in tons of carbon per hectare)
* In order to minimize the resulting estimated CO2 emissions? (Emissions from Land Use Change, ELUC,
in tons of carbon per hectare)
'''),
dcc.Markdown('''## Context'''),
Expand All @@ -99,8 +99,8 @@
dcc.Graph(id='presc-fig',
figure=chart_component.create_treemap(type_context=False),
style={'grid-clumn': '4'})
# This can't be set to auto because the lines will overflow!
], style={'display': 'grid', 'grid-template-columns': '4.5% 40% 1fr 1fr', "width": "100%"}),
# The above line can't be set to auto because the lines will overflow!
html.Div([
sliders_component.get_frozen_div(),
html.Button("Sum to 100%", id='sum-button', n_clicks=0),
Expand Down
61 changes: 31 additions & 30 deletions use_cases/eluc/app/components/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from app import utils
from data import constants


class ChartComponent:
"""
Component in charge of handling the context and prescription charts.
Expand Down Expand Up @@ -161,24 +162,24 @@ def create_treemap(self, data=pd.Series, type_context=True, year=2021) -> go.Fig
fields = data[app_constants.FIELDS].sum()

labels = [title, "Nonland",
"Crops",
"Primary Vegetation", "primf", "primn",
"Secondary Vegetation", "secdf", "secdn",
"Urban",
"Fields", "pastr", "range"]
"Crops",
"Primary Vegetation", "primf", "primn",
"Secondary Vegetation", "secdf", "secdn",
"Urban",
"Fields", "pastr", "range"]
parents = ["", title,
title,
title, "Primary Vegetation", "Primary Vegetation",
title, "Secondary Vegetation", "Secondary Vegetation",
title,
title, "Fields", "Fields"]

values = [total + data["nonland"], data["nonland"],
data["crop"],
primary, data["primf"], data["primn"],
secondary, data["secdf"], data["secdn"],
data["urban"],
fields, data["pastr"], data["range"]]
title,
title, "Primary Vegetation", "Primary Vegetation",
title, "Secondary Vegetation", "Secondary Vegetation",
title,
title, "Fields", "Fields"]

values = [total + data["nonland"], data["nonland"],
data["crop"],
primary, data["primf"], data["primn"],
secondary, data["secdf"], data["secdn"],
data["urban"],
fields, data["pastr"], data["range"]]

tree_params["customdata"] = self._create_hovertext(labels, parents, values, title)
tree_params["hovertemplate"] = "%{customdata}<extra></extra>"
Expand All @@ -188,15 +189,15 @@ def create_treemap(self, data=pd.Series, type_context=True, year=2021) -> go.Fig

fig = go.Figure(
go.Treemap(
labels = labels,
parents = parents,
values = values,
labels=labels,
parents=parents,
values=values,
**tree_params
)
)
colors = px.colors.qualitative.Plotly
fig.update_layout(
treemapcolorway = [colors[1], colors[4], colors[2], colors[7], colors[3], colors[0]],
treemapcolorway=[colors[1], colors[4], colors[2], colors[7], colors[3], colors[0]],
margin={"t": 0, "b": 0, "l": 10, "r": 10}
)
return fig
Expand All @@ -219,24 +220,24 @@ def create_pie(self, data=pd.Series, type_context=True, year=2021) -> go.Figure:
else:
values = data[app_constants.CHART_COLS].tolist()

assert(len(values) == len(app_constants.CHART_COLS))
assert len(values) == len(app_constants.CHART_COLS)

title = f"Context in {year}" if type_context else f"Prescribed for {year+1}"

# Attempt to match the colors from the treemap
plo = px.colors.qualitative.Plotly
dar = px.colors.qualitative.Dark24
#['crop', 'pastr', 'primf', 'primn', 'range', 'secdf', 'secdn', 'urban', 'nonland]
# ['crop', 'pastr', 'primf', 'primn', 'range', 'secdf', 'secdn', 'urban', 'nonland]
colors = [plo[4], plo[0], plo[2], dar[14], plo[5], plo[7], dar[2], plo[3], plo[1]]
fig = go.Figure(
go.Pie(
values = values,
labels = app_constants.CHART_COLS,
textposition = "inside",
sort = False,
marker_colors = colors,
hovertemplate = "%{label}<br>%{value}<br>%{percent}<extra></extra>",
title = title
values=values,
labels=app_constants.CHART_COLS,
textposition="inside",
sort=False,
marker_colors=colors,
hovertemplate="%{label}<br>%{value}<br>%{percent}<extra></extra>",
title=title
)
)

Expand Down
7 changes: 4 additions & 3 deletions use_cases/eluc/app/components/legend.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dash import dcc
from dash import html


# pylint: disable=too-few-public-methods
class LegendComponent:
"""
Expand All @@ -15,8 +16,8 @@ def get_legend_div(self):
"""
# Legend examples come from https://hess.copernicus.org/preprints/hess-2021-247/hess-2021-247-ATC3.pdf
legend_div = html.Div(
style={"margin-bottom": "100px"}, # Because we removed some crops, we extend this so the map doesn't shrink.
children = [
style={"margin-bottom": "100px"}, # Because we removed some crops, we extend this so the map doesn't shrink
children=[
dcc.Markdown('''
### Land Use Types
Expand All @@ -25,7 +26,7 @@ def get_legend_div(self):
- primf: Primary forest
- primn: Primary nonforest vegetation
Secondary: Vegetation that has been touched by humans
- secdf: Secondary forest
Expand Down
3 changes: 2 additions & 1 deletion use_cases/eluc/app/components/lock.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from data import constants


class LockComponent:
"""
Component that creates lock div based on reco columns.
Expand All @@ -20,7 +21,7 @@ def create_check_options(self, values: list) -> list:
for val in values:
options.append(
{"label": [html.I(className="bi bi-lock"), html.Span(val)],
"value": val})
"value": val})
return options

def get_checklist_div(self):
Expand Down
7 changes: 4 additions & 3 deletions use_cases/eluc/app/components/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from app import constants as app_constants


class MapComponent:
"""
Component handling the map. Keeps track of the latitudes and longitudes a user can select as well as the countries.
Expand Down Expand Up @@ -49,8 +50,8 @@ def get_context_div(self):
select_style = {'width': '75%', 'justify-self': 'left', 'margin-top': '-3px'}
context_div = html.Div(
style={'display': 'grid',
'grid-template-columns': 'auto 1fr', 'grid-template-rows': 'auto auto auto auto',
'position': 'absolute', 'bottom': '0'},
'grid-template-columns': 'auto 1fr', 'grid-template-rows': 'auto auto auto auto',
'position': 'absolute', 'bottom': '0'},
children=[
html.P("Region", style={'grid-column': '1', 'grid-row': '1', 'padding-right': '10px'}),
dcc.Dropdown(
Expand Down Expand Up @@ -164,7 +165,7 @@ def update_map(year, lat, lon, location):

return self.create_map(data, 10, idx)

def create_map(self, df: pd.DataFrame, zoom=10, color_idx = None) -> go.Figure:
def create_map(self, df: pd.DataFrame, zoom=10, color_idx=None) -> go.Figure:
"""
Creates map figure with data centered and zoomed in with appropriate point marked.
:param df: DataFrame of data to plot. This dataframe has its index reset.
Expand Down
9 changes: 5 additions & 4 deletions use_cases/eluc/app/components/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from predictors.predictor import Predictor
from predictors.percent_change.percent_change_predictor import PercentChangePredictor


class PredictionComponent:
"""
Component in charge of handling predictor selection and predict button callback.
Expand All @@ -41,11 +42,11 @@ def load_predictors(self) -> dict[str, Predictor]:
rf_path = "danyoung/eluc-global-rf"
rf_local_dir = app_constants.PREDICTOR_PATH / rf_path.replace("/", "--")
global_nn = nn_persistor.from_pretrained(nn_path,
local_dir=nn_local_dir)
local_dir=nn_local_dir)
global_linreg = sklearn_persistor.from_pretrained(linreg_path,
local_dir=linreg_local_dir)
local_dir=linreg_local_dir)
global_rf = sklearn_persistor.from_pretrained(rf_path,
local_dir=rf_local_dir)
local_dir=rf_local_dir)

predictors["Global Neural Network"] = global_nn
predictors["Global Linear Regression"] = global_linreg
Expand Down Expand Up @@ -177,5 +178,5 @@ def context_presc_to_df(self, context: pd.Series, presc: pd.Series) -> pd.DataFr
diff = diff.rename({col: f"{col}_diff" for col in diff.index})
context_actions = diff.combine_first(context[constants.CAO_MAPPING["context"]])
context_actions_df = pd.DataFrame([context_actions])
context_actions_df[constants.NO_CHANGE_COLS] = 0 # TODO: I'm not entirely sure why this line is necessary
context_actions_df[constants.NO_CHANGE_COLS] = 0 # TODO: I'm not entirely sure why this line is necessary
return context_actions_df
16 changes: 8 additions & 8 deletions use_cases/eluc/app/components/prescription.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from persistence.serializers.prescriptor_serializer import PrescriptorSerializer
from prescriptors.prescriptor_manager import PrescriptorManager


class PrescriptionComponent():
"""
Component in charge of handling prescriptor selection and prescribe button.
Expand Down Expand Up @@ -59,10 +60,10 @@ def get_presc_select_div(self):
html.P("Minimize change", style={"grid-column": "1"}),
html.Div([
dcc.Slider(id='presc-select',
min=0, max=len(self.prescriptor_list)-1, step=1,
value=app_constants.DEFAULT_PRESCRIPTOR_IDX,
included=False,
marks={i : "" for i in range(len(self.prescriptor_list))})
min=0, max=len(self.prescriptor_list)-1, step=1,
value=app_constants.DEFAULT_PRESCRIPTOR_IDX,
included=False,
marks={i: "" for i in range(len(self.prescriptor_list))})
], style={"grid-column": "2", "width": "100%", "margin-top": "8px"}),
html.P("Minimize ELUC", style={"grid-column": "3", "padding-right": "10px"}),
html.Button("Prescribe", id='presc-button', n_clicks=0, style={"grid-column": "4", **margin_style}),
Expand Down Expand Up @@ -168,10 +169,9 @@ def create_pareto(self, pareto_df: pd.DataFrame, presc_id: int) -> go.Figure:
})
# Name axes and hide legend
fig.update_layout(xaxis_title={"text": "Change (%)"},
yaxis_title={"text": 'ELUC (tC/ha)'},
showlegend=False,
title="Prescriptors",
)
yaxis_title={"text": 'ELUC (tC/ha)'},
showlegend=False,
title="Prescriptors")
fig.update_traces(hovertemplate="Average Change: %{x} <span>&#37;</span>"
"<br>"
" Average ELUC: %{y} tC/ha<extra></extra>")
Expand Down
19 changes: 10 additions & 9 deletions use_cases/eluc/app/components/references.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
from dash import html


# pylint: disable=too-few-public-methods
class ReferencesComponent:
"""
Expand All @@ -16,36 +17,36 @@ def get_references_div(self):
references_div = html.Div([
html.Div(className="parent", children=[
html.P("Code for this project can be found here: ",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(Project Resilience MVP repo)",
href="https://github.com/Project-Resilience/mvp/tree/main/use_cases/eluc\n"),
href="https://github.com/Project-Resilience/mvp/tree/main/use_cases/eluc\n"),
]),
html.Div(className="parent", children=[
html.P("The paper for this project can be found here: ",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(arXiv link)", href="https://arxiv.org/abs/2311.12304\n"),
]),
html.Div(className="parent", children=[
html.P("ELUC data provided by the BLUE model ",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(BLUE: Bookkeeping of land use emissions)",
href="https://agupubs.onlinelibrary.wiley.com/doi/10.1002/2014GB004997\n"),
href="https://agupubs.onlinelibrary.wiley.com/doi/10.1002/2014GB004997\n"),
]),
html.Div(className="parent", children=[
html.P("Land use change data provided by the LUH2 project",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(LUH2: Land Use Harmonization 2)", href="https://luh.umd.edu/\n"),
]),
html.Div(className="parent", children=[
html.P("Setup is described in Appendix C2.1 of the GCB 2022 report",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(Global Carbon Budget 2022 report)",
href="https://essd.copernicus.org/articles/14/4811/2022/#section10/\n"),
href="https://essd.copernicus.org/articles/14/4811/2022/#section10/\n"),
]),
html.Div(className="parent", children=[
html.P("The Global Carbon Budget report assesses the global CO2 budget \
for the Intergovernmental Panel on Climate Change",
className="child", style=inline_block),
className="child", style=inline_block),
html.A("(IPCC)", href="https://www.ipcc.ch/\n"),
]),
])
Expand Down
3 changes: 2 additions & 1 deletion use_cases/eluc/app/components/sliders.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from app import utils
from data import constants


class SlidersComponent:
"""
Component that displays the sliders, shows their values in frozen inputs, resets the sliders when context changes,
Expand Down Expand Up @@ -56,7 +57,7 @@ def get_frozen_div(self):
type="text",
disabled=True,
id={"type": "frozen-input", "index": f"{col}-frozen"})
for col in app_constants.NO_CHANGE_COLS + ["nonland"]
for col in app_constants.NO_CHANGE_COLS + ["nonland"]
])
return frozen_div

Expand Down
Loading

0 comments on commit 982ae00

Please sign in to comment.