Skip to content

Commit

Permalink
Merge pull request #64 from sepal-contrib/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
dfguerrerom authored Aug 30, 2024
2 parents 709c952 + 8814404 commit acf9172
Show file tree
Hide file tree
Showing 13 changed files with 441 additions and 139 deletions.
3 changes: 3 additions & 0 deletions component/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from sepal_ui.scripts.utils import init_ee

init_ee()
2 changes: 1 addition & 1 deletion component/message/en/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"title" : "Reclassify table",
"get_classes" : "Get classes",
"no_data" : "No data",
"non_complete" : "You have classified {} classes. Empty classes will be classified as zero (0).",
"non_complete" : "You have classified {} classes. Empty classes will be masked out from the analysis.",
"description" : "Please reclassify the land cover classes of your custom land cover datasets to the reference land cover legend used for this indicator",
"tooltip": {
"save_matrix" : "Save matrix as .csv file",
Expand Down
13 changes: 9 additions & 4 deletions component/scripts/gee.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@
from component.parameter.module_parameter import transition_degradation_matrix
from component.scripts.gee_parse_reduce_regions import reduceGroups

NO_DATA_VALUE = 0
"""Union[int, None]: No data value for the remap process"""


def no_remap(image: ee.Image, remap_matrix: Optional[dict] = None):
"""return remapped or raw image if there's a matrix"""
print(f"NO_DATA_VALUE {NO_DATA_VALUE}")
print(f"NO_DATA_VALUE {remap_matrix}")

if remap_matrix:
from_, to_ = list(zip(*remap_matrix.items()))
return image.remap(from_, to_, 9999)
return image.remap(from_, to_, NO_DATA_VALUE).selfMask()

return image

Expand Down Expand Up @@ -192,13 +197,13 @@ def get_transition(
baseline_degradation = baseline_transition.remap(
transition_matrix["transition"].tolist(),
transition_matrix["impact_code"].tolist(),
9999,
NO_DATA_VALUE,
).rename("baseline_degradation")

report_degradation = report_transition.remap(
transition_matrix["transition"].tolist(),
transition_matrix["impact_code"].tolist(),
9999,
NO_DATA_VALUE,
).rename("report_degradation")

# Create transition between degradation images
Expand All @@ -211,7 +216,7 @@ def get_transition(
final_degradation = degraded_transition.remap(
transition_degradation_matrix["transition"].tolist(),
transition_degradation_matrix["impact_code"].tolist(),
9999,
NO_DATA_VALUE,
).rename("final_degradation")

return (
Expand Down
15 changes: 15 additions & 0 deletions component/scripts/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,18 @@ def validate_calc_params(calc_a: bool, calc_b: bool, sub_a_year, sub_b_year, sub

if not any([calc_a, calc_b]):
raise Exception("Please select at least one subindicator")


def validate_sankey_classes(df_sankey: pd.DataFrame, color_dict: dict) -> None:
"""Validate that classes in the df_sankey are all in the color_dict"""

# Combine the unique classes from both columns into a single set
unique_classes = set(df_sankey["from_lc"]).union(set(df_sankey["to_lc"]))

# Check if any class is not in the color dictionary
missing_classes = unique_classes - set(color_dict.keys())

if missing_classes:
raise ValueError(
f"You cannot create a Sankey diagram when using more classes than the default ones. Invalid classes: {missing_classes}."
)
51 changes: 51 additions & 0 deletions component/widget/buttons.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import ipyvuetify as v
import sepal_ui.sepalwidgets as sw

btn_color = "primary"


class IconBtn(sw.Btn):
def __init__(self, gliph: str, *args, **kwargs):

kwargs["icon"] = True
kwargs["small"] = True
kwargs["class_"] = "mr-2"

super().__init__(gliph=gliph, *args, **kwargs)

# to overwrite the default color
self.color = kwargs.get("color") or btn_color
self.v_icon.left = False


class TextBtn(sw.Btn):
def __init__(self, text: str, *args, **kwargs):

kwargs["small"] = True
kwargs["class_"] = "mr-2"

super().__init__(msg=text, *args, **kwargs)

# to overwrite the default color
self.color = btn_color


class DrawMenuBtn(v.Btn):

def __init__(self, *args, **kwargs):

self.v_on = "menuData.on"
self.small = True
self.children = [
v.Icon(children=["mdi-draw"], small=True, color="white"),
v.Icon(
children=["fa fa-caret-down"],
small=True,
right=True,
color="white",
),
]

super().__init__(*args, **kwargs)
self.class_ = "mr-2"
self.color = btn_color
12 changes: 7 additions & 5 deletions component/widget/calc_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def get_item(self, indicator):
children=[
v.Card(
min_height=225,
max_height=225,
children=[
v.CardTitle(
children=[
Expand Down Expand Up @@ -288,7 +287,7 @@ class EditionDialog(sw.Dialog):
def __init__(self, custom_list, indicator):
self.v_model = False
self.scrollable = True
self.max_width = 650
self.max_width = 750
self.style_ = "overflow-x: hidden;"
self.persistent = True

Expand All @@ -306,7 +305,7 @@ def __init__(self, custom_list, indicator):

self.children = [
sw.Card(
max_width=650,
max_width=750,
min_height=420,
style_="height: 100%;",
class_="pa-4",
Expand Down Expand Up @@ -474,7 +473,8 @@ def get_element(self):
actions, id_ = self.get_actions()

w_basep = v.Select(
class_="mr-2 max-width-200",
style_="max-width: 550px;",
class_="mr-2",
v_model=False,
attributes={"id": "selects", "unique_id": f"report_asset_{id_}"},
label=cm.calculation.year,
Expand Down Expand Up @@ -650,7 +650,8 @@ def __init__(self, items, actions, *args, **kwargs):
self.attributes = {"id": "custom_list_sub_b"}

self.w_basep = v.Select(
class_="mr-2 max-width-200",
style_="max-width: 485px;",
class_="mr-2",
v_model=False,
attributes={
"id": "selects",
Expand Down Expand Up @@ -678,6 +679,7 @@ def __init__(self, items, actions, *args, **kwargs):
)

self.w_reportp = v.Select(
style_="max-width: 485px;",
class_="mr-3 ",
v_model=False,
attributes={
Expand Down
2 changes: 1 addition & 1 deletion component/widget/custom_widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def set_state(self, state_color):

class AlertDialog(sw.Dialog):
def __init__(self, w_alert: sw.Alert):
self.max_width = 650
self.max_width = 750
self.persistent = True
self.style_ = "margin: 0 !important;"

Expand Down
8 changes: 7 additions & 1 deletion component/widget/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ def __init__(self, map_: "Map", model: MgciModel):

self._layers = []

self.w_layers = sw.Select(items=[], v_model=[], label="Layers", class_="mr-2")
self.w_layers = sw.Select(
style_="max-width: 362px",
items=[],
v_model=[],
label="Layers",
class_="mr-2",
)
self.btn = sw.Btn("", gliph="mdi-plus")
self.btn.v_icon.left = False

Expand Down
14 changes: 8 additions & 6 deletions component/widget/reclassify/reclassify_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,16 @@ def load_matrix_content(self, *_):
[str(v) for v in input_data.to_code.unique() if v not in classes]
)
raise Exception(
f"Some of the targed land cover classes ({missing_values}) are not present in the destination land cover classes."
f"Some of the target land cover classes ({missing_values}) are not present in the destination land cover classes."
)

# fill the data
for _, row in input_data.iterrows():
src_code, dst_code = row.from_code, row.to_code
if str(src_code) in self.reclassify_table.class_select_list:
self.reclassify_table.class_select_list[
str(src_code)
].v_model = dst_code
self.reclassify_table.class_select_list[str(src_code)].v_model = (
dst_code
)

self.import_dialog.close_dialog()

Expand Down Expand Up @@ -345,7 +345,6 @@ def on_validate_input(self, change):

def open_dialog(self):
"""show the dialog and set the matrix values"""

self.w_map_matrix_file.unobserve(self.on_validate_input, "v_model")

# Reset file name
Expand Down Expand Up @@ -664,13 +663,16 @@ def __init__(self, model: ReclassifyModel, **kwargs):
.hide()
)

self.message = sw.Html(tag="span", style_=f"color: {color.warning}")
self.message = sw.Html(
tag="span", style_=f"color: {color.warning}", class_="ml-2"
)

self.toolbar = v.Toolbar(
flat=True,
children=[
default_lc_dialog,
cm.reclass.title,
self.message,
v.Spacer(),
v.Divider(vertical=True, class_="mx-2"),
self.btn_info.with_tooltip,
Expand Down
Loading

0 comments on commit acf9172

Please sign in to comment.