Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add label_type param #34

Merged
merged 1 commit into from
Nov 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions sankee/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def sankify(
scale: Union[int, None] = None,
seed: int = 0,
exclude: None = None,
label_type: str = "class",
) -> go.Figure:
"""
Generate an interactive Sankey plot showing land cover change over time from a series of
Expand Down Expand Up @@ -142,6 +143,10 @@ def sankify(
The seed value used to generate repeatable results during random sampling.
exclude : None
Unused parameter that will be removed in a future release.
label_type : str, default "class"
The type of label to display for each link, one of "class", "percent", or "count". Selecting
"class" will use the class label, "percent" will use the proportion of sampled pixels in each
class, and "count" will use the number of sampled pixels in each class.

Returns
-------
Expand Down Expand Up @@ -174,6 +179,7 @@ def sankify(
title=title,
scale=scale,
seed=seed,
label_type=label_type,
)


Expand Down
29 changes: 28 additions & 1 deletion sankee/plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def sankify(
title: Union[None, str] = None,
scale: Union[None, int] = None,
seed: int = 0,
label_type: str = "class",
) -> go.Figure:
"""
Generate an interactive Sankey plot showing land cover change over time from a series of images.
Expand Down Expand Up @@ -73,6 +74,10 @@ def sankify(
use the image's nominal scale, which may cause errors depending on the image projection.
seed : int, default 0
The seed value used to generate repeatable results during random sampling.
label_type : str, default "class"
The type of label to display for each link, one of "class", "percent", or "count". Selecting
"class" will use the class label, "percent" will use the proportion of sampled pixels in each
class, and "count" will use the number of sampled pixels in each class.

Returns
-------
Expand Down Expand Up @@ -107,6 +112,7 @@ def sankify(
palette=palette,
title=title,
samples=samples,
label_type=label_type,
)


Expand All @@ -118,12 +124,14 @@ def __init__(
palette: Dict[int, str],
title: str,
samples: ee.FeatureCollection,
label_type: str,
):
self.data = data
self.labels = labels
self.palette = palette
self.title = title
self.samples = samples
self.label_type = label_type

self.hide = []
self.plot = self.generate_plot()
Expand Down Expand Up @@ -167,7 +175,6 @@ def generate_plot_parameters(self) -> SankeyParameters:
all_classes = pd.concat([source_df, target_df])

all_classes = all_classes.drop_duplicates().reset_index(drop=True)
all_classes["label"] = all_classes["class"].apply(lambda k: self.labels[k]).tolist()
all_classes["color"] = all_classes["class"].apply(lambda k: self.palette[k]).tolist()
all_classes["id"] = all_classes.groupby(["year", "class"], sort=False).ngroup()

Expand All @@ -187,6 +194,25 @@ def generate_plot_parameters(self) -> SankeyParameters:
right_on=["year", "class"],
)["id"]

# Calculate the proportion of each class in each year
melted = self.data.melt(var_name="year")
melted = melted.groupby(["year", "value"]).size().reset_index(name="count")
melted["proportion_of_total"] = (melted
.groupby("year")["count"]
.transform(lambda x: x / x.sum())
.apply(lambda x: f"{x:.0%}")
)
all_classes = all_classes.merge(melted, left_on=["year", "class"], right_on=["year", "value"])

if self.label_type == "class":
all_classes["label"] = all_classes["class"].apply(lambda k: self.labels[k])
elif self.label_type == "percent":
all_classes["label"] = all_classes["proportion_of_total"]
elif self.label_type == "count":
all_classes["label"] = all_classes["count"]
else:
raise ValueError("Invalid label_type. Choose from 'class', 'percent', or 'count'.")

return SankeyParameters(
node_labels=all_classes.year,
link_labels=df.link_label,
Expand Down Expand Up @@ -360,6 +386,7 @@ def generate_plot(self) -> go.Figure:
fig = go.FigureWidget(
data=[
go.Sankey(
arrangement="snap",
node=dict(
pad=30,
thickness=10,
Expand Down
1 change: 1 addition & 0 deletions tests/test_plotting.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def sankey():
palette=TEST_DATASET.palette,
title="",
samples=None,
label_type="class",
)


Expand Down