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 Scene function to use Hvplot backend visualization #2106

Merged
merged 42 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
fa7942d
to_hvplot function
May 6, 2022
88d4002
Add to_hvplot function
May 6, 2022
93c14ef
Merge remote-tracking branch 'origin/main' into main
May 6, 2022
2fa9b87
trying to follow and correct stickler-ci messages
May 6, 2022
fcfd481
correction of whitespaces
May 6, 2022
dfd93ec
correction whitespaces
May 6, 2022
c0022f3
correction whitespaces
May 6, 2022
249c420
function correction for pull request
May 6, 2022
2f2e8a8
Add to_hvplot functon
May 6, 2022
ac8a361
add hvplot in extras require
May 6, 2022
d2c80fb
add hvplot in test require
May 6, 2022
09b8f6e
Answer to #issuecomment-1120099909
May 7, 2022
d0299dd
Update satpy/scene.py
bornagain1981 May 7, 2022
aed6a91
Update satpy/scene.py
bornagain1981 May 7, 2022
4687ba4
Update satpy/scene.py
bornagain1981 May 7, 2022
1aff451
Update
May 7, 2022
546bb5f
Update setup.py
bornagain1981 Aug 2, 2022
4455ac2
Merge remote-tracking branch 'pytroll/main' into main
Feb 3, 2023
d574eeb
Merge tag 'v0.40.0' into main
Feb 9, 2023
03b8056
Merge remote-tracking branch 'pytroll/main' into main
Feb 21, 2023
590d374
add holoviews to continuous_integration
Feb 24, 2023
d319a9b
Merge remote-tracking branch 'pytroll/main' into main
Mar 20, 2023
09ad15d
Merge remote-tracking branch 'pytroll/main' into main
Mar 24, 2023
3aba218
Merge tag 'v0.42.1' into main
May 5, 2023
9541b70
Merge tag 'v0.42.2' into main
May 11, 2023
3369c93
Merge remote-tracking branch 'pytroll/main' into main
Jul 24, 2023
5beb117
Merge remote-tracking branch 'pytroll/main' into main
Dec 11, 2023
874f8d8
Merge remote-tracking branch 'pytroll/main' into main
Dec 12, 2023
2da489b
hvplot tests
Dec 14, 2023
07b313b
Merge remote-tracking branch 'pytroll/main' into main
Dec 14, 2023
27041a4
add control for swath data
Dec 14, 2023
fb8ff3b
import hvplot directly inside method
Dec 14, 2023
91f96af
Merge remote-tracking branch 'pytroll/main' into main
Dec 14, 2023
807357a
Add holoviews required library
Dec 14, 2023
aad6ea8
Add holoviews required library
Dec 14, 2023
fca35cd
Add holoviews required library
Dec 14, 2023
7918f37
Revert "Add holoviews required library"
Dec 14, 2023
258fd17
Add holoviews library
Dec 14, 2023
f45e225
Merge remote-tracking branch 'origin/main' into main
Dec 14, 2023
d16728b
Add holoviews in documentation
bornagain1981 Dec 15, 2023
4d3153a
Merge remote-tracking branch 'pytroll/main'
bornagain1981 Dec 15, 2023
9b0bcae
Holoviews inside to_hvplot method
bornagain1981 Dec 15, 2023
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
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ The following people have made contributions to this project:
- [Lu Liu (yukaribbba)](/~https://github.com/yukaribbba)
- [Andrea Meraner (ameraner)](/~https://github.com/ameraner)
- [Aronne Merrelli (aronnem)](/~https://github.com/aronnem)
- [Luca Merucci (lmeru)](/~https://github.com/lmeru)
- [Lucas Meyer (LTMeyer)](/~https://github.com/LTMeyer)
- [Zifeng Mo (Isotr0py)](/~https://github.com/Isotr0py)
- [Ondrej Nedelcev (nedelceo)](/~https://github.com/nedelceo)
Expand Down
3 changes: 2 additions & 1 deletion continuous_integration/environment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ dependencies:
- mock
- libtiff
- geoviews
- holoviews
- hvplot
- zarr
- python-eccodes
# 2.19.1 seems to cause library linking issues
- eccodes>=2.20
- geoviews
- pytest
- pytest-cov
- pytest-lazy-fixture
Expand Down
2 changes: 1 addition & 1 deletion doc/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def __getattr__(cls, name):
for mod_name in MOCK_MODULES:
sys.modules[mod_name] = Mock() # type: ignore

autodoc_mock_imports = ["cf", "glymur", "h5netcdf", "imageio", "mipp", "netCDF4",
autodoc_mock_imports = ["cf", "glymur", "h5netcdf", "holoviews", "imageio", "mipp", "netCDF4",
"pygac", "pygrib", "pyhdf", "pyninjotiff",
"pyorbital", "pyspectral", "rasterio", "trollimage",
"zarr"]
Expand Down
76 changes: 75 additions & 1 deletion satpy/scene.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/scene.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 1287 to 1343, 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/scene.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 80 to 81, threshold = 75. This file contains too many functions. Beyond a certain threshold, more functions lower the code health.
# -*- coding: utf-8 -*-
# Copyright (c) 2010-2022 Satpy developers
#
Expand Down Expand Up @@ -28,7 +28,7 @@
from pyresample.geometry import AreaDefinition, BaseDefinition, SwathDefinition
from xarray import DataArray

from satpy.composites import IncompatibleAreas
from satpy.composites import IncompatibleAreas, enhance2dataset
from satpy.composites.config_loader import load_compositor_configs_for_sensors
from satpy.dataset import DataID, DataQuery, DatasetDict, combine_metadata, dataset_walker, replace_anc
from satpy.dependency_tree import DependencyTree
Expand Down Expand Up @@ -1066,6 +1066,80 @@

return gview

def to_hvplot(self, datasets=None, *args, **kwargs):
"""Convert satpy Scene to Hvplot. The method could not be used with composites of swath data.

Args:
datasets (list): Limit included products to these datasets.
args: Arguments coming from hvplot
kwargs: hvplot options dictionary.

Returns: hvplot object that contains within it the plots of datasets list.
As default it contains all Scene datasets plots and a plot title is shown.

Example usage::

scene_list = ['ash','IR_108']
scn = Scene()
scn.load(scene_list)
scn = scn.resample('eurol')
plot = scn.to_hvplot(datasets=scene_list)
plot.ash+plot.IR_108
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't enough to show the plot, what else needs to be done? I was testing this in IPython command line prompt.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Schermata del 2023-03-20 21-05-08

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pnuu does it work in your Jupyter Notebook?

"""

def _get_crs(xarray_ds):
return xarray_ds.area.to_cartopy_crs()

def _get_timestamp(xarray_ds):
time = xarray_ds.attrs["start_time"]
return time.strftime("%Y %m %d -- %H:%M UTC")

def _get_units(xarray_ds, variable):
return xarray_ds[variable].attrs["units"]

def _plot_rgb(xarray_ds, variable, **defaults):
img = enhance2dataset(xarray_ds[variable])
return img.hvplot.rgb(bands="bands", title=title,

Check warning on line 1102 in satpy/scene.py

View check run for this annotation

Codecov / codecov/patch

satpy/scene.py#L1101-L1102

Added lines #L1101 - L1102 were not covered by tests
clabel="", **defaults)

def _plot_quadmesh(xarray_ds, variable, **defaults):
return xarray_ds[variable].hvplot.quadmesh(
clabel=f"[{_get_units(xarray_ds,variable)}]", title=title,
**defaults)

import hvplot.xarray as hvplot_xarray # noqa
from holoviews import Overlay

plot = Overlay()
xarray_ds = self.to_xarray_dataset(datasets)

if hasattr(xarray_ds, "area") and hasattr(xarray_ds.area, "to_cartopy_crs"):
ccrs = _get_crs(xarray_ds)
defaults={"x":"x","y":"y"}
else:
ccrs = None
defaults={"x":"longitude","y":"latitude"}

if datasets is None:
datasets = list(xarray_ds.keys())

defaults.update(data_aspect=1, project=True, geo=True,
crs=ccrs, projection=ccrs, rasterize=True, coastline="110m",
cmap="Plasma", responsive=True, dynamic=False, framewise=True,
colorbar=False, global_extent=False, xlabel="Longitude",
ylabel="Latitude")

defaults.update(kwargs)

for element in datasets:
title = f"{element} @ {_get_timestamp(xarray_ds)}"
if xarray_ds[element].shape[0] == 3:
plot[element] = _plot_rgb(xarray_ds, element, **defaults)

Check warning on line 1137 in satpy/scene.py

View check run for this annotation

Codecov / codecov/patch

satpy/scene.py#L1137

Added line #L1137 was not covered by tests
else:
plot[element] = _plot_quadmesh(xarray_ds, element, **defaults)

return plot

def to_xarray_dataset(self, datasets=None):
"""Merge all xr.DataArrays of a scene to a xr.DataSet.

Expand Down
47 changes: 47 additions & 0 deletions satpy/tests/scene_tests/test_conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,53 @@
# we assume that if we got something back, geoviews can use it
assert gv_obj is not None

def test_hvplot_basic_with_area(self):
"""Test converting a Scene to hvplot with a AreaDefinition."""
from pyresample.geometry import AreaDefinition
scn = Scene()
area = AreaDefinition("test", "test", "test",
{"proj": "geos", "lon_0": -95.5, "h": 35786023.0},
2, 2, [-200, -200, 200, 200])
scn["ds1"] = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
attrs={"start_time": datetime(2018, 1, 1),
"area": area, "units": "m"})
hv_obj = scn.to_hvplot()
# we assume that if we got something back, hvplot can use it
assert hv_obj is not None

Check warning on line 96 in satpy/tests/scene_tests/test_conversions.py

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (main)

❌ New issue: Code Duplication

The module contains 4 functions with similar structure: TestSceneConversions.test_geoviews_basic_with_area,TestSceneConversions.test_geoviews_basic_with_swath,TestSceneConversions.test_hvplot_basic_with_area,TestSceneConversions.test_hvplot_basic_with_swath. Avoid duplicated, aka copy-pasted, code inside the module. More duplication lowers the code health.

def test_hvplot_rgb_with_area(self):
"""Test converting a Scene to hvplot with a AreaDefinition."""
from pyresample.geometry import AreaDefinition
scn = Scene()
area = AreaDefinition("test", "test", "test",
{"proj": "geos", "lon_0": -95.5, "h": 35786023.0},
2, 2, [-200, -200, 200, 200])
scn["ds1"] = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
attrs={"start_time": datetime(2018, 1, 1),
"area": area, "units": "m"})
scn["ds2"] = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
attrs={"start_time": datetime(2018, 1, 1),
"area": area, "units": "m"})
scn["ds3"] = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
attrs={"start_time": datetime(2018, 1, 1),
"area": area, "units": "m"})
hv_obj = scn.to_hvplot()
# we assume that if we got something back, hvplot can use it
assert hv_obj is not None

def test_hvplot_basic_with_swath(self):
"""Test converting a Scene to hvplot with a SwathDefinition."""
from pyresample.geometry import SwathDefinition
scn = Scene()
longitude = xr.DataArray(da.zeros((2, 2)))
latitude = xr.DataArray(da.zeros((2, 2)))
area = SwathDefinition(longitude, latitude)
scn["ds1"] = xr.DataArray(da.zeros((2, 2), chunks=-1), dims=("y", "x"),
attrs={"start_time": datetime(2018, 1, 1),
"area": area, "units": "m"})
hv_obj = scn.to_hvplot()
# we assume that if we got something back, hvplot can use it
assert hv_obj is not None

class TestToXarrayConversion:
"""Test Scene.to_xarray() conversion."""
Expand Down Expand Up @@ -161,6 +208,6 @@

def test_to_xarray_with_multiple_area_scene(self, multi_area_scn):
"""Test converting muiltple area Scene to xarray."""
# TODO: in future adapt for DataTree implementation

Check notice on line 211 in satpy/tests/scene_tests/test_conversions.py

View check run for this annotation

codefactor.io / CodeFactor

satpy/tests/scene_tests/test_conversions.py#L211

unresolved comment '# TODO: in future adapt for DataTree implementation' (C100)
with pytest.raises(ValueError, match="Datasets to be saved .* must have identical projection coordinates."):
_ = multi_area_scn.to_xarray()
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"doc": ["sphinx", "sphinx_rtd_theme", "sphinxcontrib-apidoc"],
# Other
"geoviews": ["geoviews"],
"holoviews": ["holoviews"],
"hvplot": ["hvplot", "geoviews", "cartopy", "holoviews"],
"overlays": ["pycoast", "pydecorate"],
"satpos_from_tle": ["skyfield", "astropy"],
"tests": test_requires,
Expand Down
Loading