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 an optional kwarg to CloudCompositor for RGBA output #2788

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions satpy/composites/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2015-2023 Satpy developers

Check notice on line 1 in satpy/composites/__init__.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1276 to 1284, improve code health by reducing it to 600. The number of Lines of Code in a single file. More Lines of Code lowers the code health.
#
# This file is part of satpy.
#
Expand Down Expand Up @@ -1033,24 +1033,31 @@
class CloudCompositor(GenericCompositor):
"""Detect clouds based on thresholding and use it as a mask for compositing."""

_supported_modes = ["LA", "RGBA"]

def __init__(self, name, transition_min=258.15, transition_max=298.15, # noqa: D417
transition_gamma=3.0, invert_alpha=False, **kwargs):
transition_gamma=3.0, invert_alpha=False, mode="LA", **kwargs):
"""Collect custom configuration values.

Args:
transition_min (float): Values below or equal to this are
clouds -> opaque white
transition_max (float): Values above this are
cloud free -> transparent
transition_gamma (float): Gamma correction to apply at the end
invert_alpha (bool): Invert the alpha channel to make low data values transparent
and high data values opaque.
mode (str, optional): Image mode to return.
This shall be "LA" (default) or "RGBA".

"""
self.transition_min = transition_min
self.transition_max = transition_max
self.transition_gamma = transition_gamma
self.invert_alpha = invert_alpha
if mode not in self._supported_modes:
raise ValueError(f"Invalid mode {mode!s}. Supported modes: " + ", ".join(self._supported_modes))
self.mode = mode

Check notice on line 1060 in satpy/composites/__init__.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Excess Number of Function Arguments

CloudCompositor.__init__ increases from 6 to 7 arguments, threshold = 4. This function has too many arguments, indicating a lack of encapsulation. Avoid adding more arguments.
super(CloudCompositor, self).__init__(name, **kwargs)

def __call__(self, projectables, **kwargs):
Expand All @@ -1077,7 +1084,10 @@

# gamma adjustment
alpha **= gamma
res = super(CloudCompositor, self).__call__((data, alpha), **kwargs)
if self.mode == "LA":
res = super(CloudCompositor, self).__call__((data, alpha), **kwargs)
else:
res = super(CloudCompositor, self).__call__((data, data,data, alpha), **kwargs)
return res


Expand Down
29 changes: 29 additions & 0 deletions satpy/tests/test_composites.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Lines of Code in a Single File

The lines of code increases from 1480 to 1495, improve code health by reducing it to 600. The number of Lines of Code in a single file. More Lines of Code lowers the code health.

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Number of Functions in a Single Module

The number of functions increases from 122 to 125, threshold = 75. This file contains too many functions. Beyond a certain threshold, more functions lower the code health.

Check notice on line 1 in satpy/tests/test_composites.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

ℹ Getting worse: Low Cohesion

The number of different responsibilities increases from 75 to 76, threshold = 4. Cohesion is calculated using the LCOM4 metric. Low cohesion means that the module/class has multiple unrelated responsibilities, doing too many things and breaking the Single Responsibility Principle.
# -*- coding: utf-8 -*-
# Copyright (c) 2018-2020 Satpy developers
#
Expand Down Expand Up @@ -952,6 +952,35 @@
np.testing.assert_raises(ValueError, self.colormap_composite, [data])


class TestCloudCompositor:
"""Test CloudCompositor."""

def setup_method(self):
"""Create test data."""
self.data = xr.DataArray(da.from_array(np.array([[260, 290, 300], [250, 280, 300]])), dims=("y", "x"))

@pytest.mark.parametrize(
("invert_alpha", "mode", "expected_alpha"),
[
(False, "LA", np.array([[0.8675683, 0.0084585, 0.0], [1.0, 0.0934222, 0.0]])),
(True, "RGBA", np.array([[0.0000989, 0.5048337, 1.0], [0.0, 0.162995, 1.0]]))
]
)
def test_cloud_compositor(self, invert_alpha, mode, expected_alpha):
"""Test general default functionality of compositor."""
from satpy.composites import CloudCompositor
comp = CloudCompositor(name="test", invert_alpha=invert_alpha, mode=mode)
res = comp([self.data])
assert res.attrs["mode"] == mode
np.testing.assert_almost_equal(res.values[len(mode) - 1], expected_alpha)

def test_cloud_compositor_validity_checks(self):
"""Test that errors are raised for invalid settings."""
from satpy.composites import CloudCompositor
with pytest.raises(ValueError, match="Invalid mode RGB. Supported modes: LA, RGBA"):
_ = CloudCompositor("test", mode="RGB")


class TestPrecipCloudsCompositor(unittest.TestCase):
"""Test the PrecipClouds compositor."""

Expand Down