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

library: Add imagery Python library module to grass.script #3756

Merged
merged 22 commits into from
Dec 11, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
add imagery module
ninsbl committed May 31, 2024
commit f78b813118a4a9dafb0b1208b70c4e84c743d7ec
166 changes: 101 additions & 65 deletions python/grass/script/testsuite/test_imagery.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,101 @@
from grass.gunittest.case import TestCase
from grass.gunittest.main import test

import grass.script as gs


class TestImageryGroupToDict(TestCase):
"""Tests function `group_to_dict` that returns raster maps
from an imagery group and their metadata."""

@classmethod
def setUpClass(cls):
cls.bands = [1, 2, 3]
cls.raster_maps = ",".join([f"lsat7_2000_{band}" for band in cls.bands])
cls.group = "L8_group"
cls.subgroup = "L8_group_subgroup"
for band in cls.bands:
cls.runModule("g.copy", raster=f"lsat7_2000_{band}0,lsat7_2000_{band}0")
cls.runModule(
"r.support", raster=f"lsat7_2000_{band}", semantic_label=f"L8_{band}"
)
cls.runModule("i.group", group="L8_group", input=cls.raster_maps)

@classmethod
def tearDownClass(cls):
cls.runModule("g.remove", type="raster", name=cls.raster_maps, flags="f")
cls.runModule("g.remove", type="group", name=cls.group, flags="f")

def test_basic_group_label_keys(self):
ref_dict = {f"L8_{band}": f"lsat7_2000_{band}" for band in self.bands}
group_info = gs.imagery.group_to_dict(self.group, full_info=False)
self.assertIsInstance(dict, group_info)
self.assertDictEqual(ref_dict, group_info)

def test_basic_group_map_keys(self):
ref_dict = {f"lsat7_2000_{band}": f"L8_{band}" for band in self.bands}
group_info = gs.imagery.group_to_dict(
self.group, dict_key=None, full_info=False
)
self.assertIsInstance(dict, group_info)
self.assertDictEqual(ref_dict, group_info)

def test_full_info_group_label_keys(self):
group_info = gs.imagery.group_to_dict(self.group, full_info=False)
self.assertIsInstance(dict, group_info)
self.assertListEqual(
[f"L8_{band}" for band in self.bands], list(group_info.keys())
)
for band in self.bands:
# Take some metadata keys from raster_info
for metadata_key in [
"north",
"nsres",
"cols",
"datatype",
"map",
"date",
"semantic_label",
"comments",
]:
self.assertIn(metadata_key, group_info[f"L8_{band}"])


if __name__ == "__main__":
test()
from grass.gunittest.case import TestCase
from grass.gunittest.main import test

import grass.script as gs


class TestImageryGroupToDict(TestCase):
"""Tests function `group_to_dict` that returns raster maps
from an imagery group and their metadata."""

@classmethod
def setUpClass(cls):
cls.bands = [1, 2, 3]
cls.raster_maps = [f"lsat7_2002_{band}0" for band in cls.bands]
cls.group = "L8_group"
cls.subgroup = "L8_group_subgroup"
# Create input maps with label and group
for band in cls.bands:
cls.runModule(
"g.copy", raster=[f"lsat7_2002_{band}0", f"lsat7_2002_{band}0"]
)
cls.runModule(
"r.support", map=f"lsat7_2002_{band}0", semantic_label=f"L8_{band}"
)
cls.runModule("i.group", group=cls.group, input=cls.raster_maps)

@classmethod
def tearDownClass(cls):
cls.runModule("g.remove", type="raster", name=cls.raster_maps, flags="f")
cls.runModule("g.remove", type="group", name=cls.group, flags="f")

def test_basic_group_label_keys(self):
ref_dict = {f"L8_{band}": f"lsat7_2002_{band}0" for band in self.bands}
group_info = gs.imagery.group_to_dict(self.group, full_info=False)
# Check that a dict is returned
self.assertIsInstance(group_info, dict)
self.assertListEqual(list(ref_dict.keys()), list(group_info.keys()))
self.assertListEqual(
list(ref_dict.values()), [val.split("@")[0] for val in group_info.values()]
)

def test_basic_group_map_keys(self):
ref_dict = {f"lsat7_2002_{band}0": f"L8_{band}" for band in self.bands}
group_info = gs.imagery.group_to_dict(
self.group, dict_key=None, full_info=False
)
# Check that a dict is returned
self.assertIsInstance(group_info, dict)
self.assertListEqual(
list(ref_dict.keys()), [key.split("@")[0] for key in group_info.keys()]
)
self.assertListEqual(list(ref_dict.values()), list(group_info.values()))

def test_full_info_group_label_keys(self):
group_info = gs.imagery.group_to_dict(self.group, full_info=True)
# Check that a dict is returned
self.assertIsInstance(group_info, dict)
self.assertListEqual(
[f"L8_{band}" for band in self.bands], list(group_info.keys())
)
for band in self.bands:
# Take some metadata keys from raster_info
for metadata_key in [
"north",
"nsres",
"cols",
"datatype",
"map",
"date",
"semantic_label",
"comments",
]:
self.assertIn(metadata_key, group_info[f"L8_{band}"])

def test_full_info_group_label_keys_subgroup(self):
self.runModule(
"i.group", group=self.group, subgroup=self.subgroup, input=self.raster_maps
)
group_info = gs.imagery.group_to_dict(self.group, full_info=True)
# Check that a dict is returned
self.assertIsInstance(group_info, dict)
self.assertListEqual(
[f"L8_{band}" for band in self.bands], list(group_info.keys())
)
for band in self.bands:
# Take some metadata keys from raster_info
for metadata_key in [
"north",
"nsres",
"cols",
"datatype",
"map",
"date",
"semantic_label",
"comments",
]:
self.assertIn(metadata_key, group_info[f"L8_{band}"])


if __name__ == "__main__":
test()

Unchanged files with check annotations Beta

run: |
mkdir $HOME/install
- name: Set number of cores for compilation

Check warning on line 116 in .github/workflows/python-code-quality.yml

GitHub Actions / Python Code Quality Checks (ubuntu-22.04, 3.10, 3.8, 24.4.0, 3.9.2, 2.12.2, 1.7.8)

Temporarily downgraded pytest-pylint and pytest to allow merging other PRs. The errors reported with a newer version seem legitimite and should be fixed (2023-10-18, see https://github.com/OSGeo/grass/pull/3205) (2024-01-28, see https://github.com/OSGeo/grass/issues/3380)
run: |
echo "MAKEFLAGS=-j$(nproc)" >> $GITHUB_ENV