Skip to content

Commit

Permalink
DAS-NONE: Random small fixes. (#38)
Browse files Browse the repository at this point in the history
No functional differences. Code cleanup and refactor
  • Loading branch information
flamingbear authored Dec 12, 2024
1 parent 2775c15 commit 2f47607
Show file tree
Hide file tree
Showing 23 changed files with 183 additions and 205 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/run_lib_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ['3.10', '3.11']
python-version: ['3.10', '3.11', '3.12']

steps:
- name: Checkout harmony-browse-image-generator repository
Expand Down
12 changes: 3 additions & 9 deletions .github/workflows/run_service_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ jobs:
- name: Run test image
run: ./bin/run-test

- name: Archive test results
- name: Archive test results and coverage
uses: actions/upload-artifact@v4
with:
name: Test results
path: test-reports/

- name: Archive coverage report
uses: actions/upload-artifact@v4
with:
name: Coverage report
path: coverage/*
name: reports
path: reports/**/*
11 changes: 4 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ repos:
rev: v0.8.2
hooks:
- id: ruff
args: ["--fix", "--show-fixes", "--extend-select", "I"]
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.10.0
hooks:
- id: black-jupyter
args: ["--skip-string-normalization"]
language_version: python3.11
args: ["--fix", "--show-fixes"]
types_or: [python, jupyter]
- id: ruff-format
types_or: [python, jupyter]
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ HyBIG follows semantic versioning. All notable changes to this project will be
documented in this file. The format is based on [Keep a
Changelog](http://keepachangelog.com/en/1.0.0/).

## [unreleased] - 2024-12-10

### Changed

* Changed pre-commit configuration to remove `black-jupyter` dependency [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
* Updates service image's python to 3.12 [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
* Simplifies test scripts to run with pytest and pytest plugins [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)

### Removed

* Removes `test_code_format.py` in favor of `ruff` pre-commit configuration [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)


## [v2.0.2] - 2024-10-15

### Fixed
Expand Down
8 changes: 4 additions & 4 deletions bin/run-test
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ set -ex
find . | grep -E "(__pycache__|\.pyc|\.pyo$)" | xargs rm -rf

# Make the directory into which XML format test reports will be saved
mkdir -p test-reports
mkdir -p reports/test-reports

# Make the directory into which coverage reports will be saved
mkdir -p coverage
mkdir -p reports/coverage

# Run the tests in a Docker container with mounted volumes for XML report
# output and test coverage reporting
docker run --platform linux/amd64 --rm \
-v $(pwd)/test-reports:/home/tests/reports \
-v $(pwd)/coverage:/home/tests/coverage \
-v $(pwd)/reports/test-reports:/home/reports/test-reports \
-v $(pwd)/reports/coverage:/home/reports/coverage \
ghcr.io/nasa/harmony-browse-image-generator-test "$@"
2 changes: 1 addition & 1 deletion docker/service.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# and updates the entrypoint of the new service container
#
###############################################################################
FROM python:3.11
FROM python:3.12

WORKDIR "/home"

Expand Down
6 changes: 3 additions & 3 deletions docs/HyBIG-Example-Usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@
},
"outputs": [],
"source": [
"# if gdalinfo is installed you can see the palette associated with the PNG image.\n",
"# if gdalinfo is installed view the palette associated with the PNG image.\n",
"!gdalinfo hybig-output/example1/VCF5KYR_1991001_001_2018224205008.png"
]
},
Expand Down Expand Up @@ -318,7 +318,7 @@
"metadata": {},
"outputs": [],
"source": [
"# If gdal is installed this will show you the corner points associated with the output files.\n",
"# If gdal is installed, show the corner points associated with the output files.\n",
"!gdalinfo hybig-output/example2/ASTGTMV003_N00E022_dem.jpg | grep -E \"Left|Right\""
]
},
Expand Down Expand Up @@ -690,7 +690,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.12.6"
},
"name": "HyBIG-Example-Usage.ipynb"
},
Expand Down
115 changes: 56 additions & 59 deletions harmony_service/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@
"""

from os.path import basename
from pathlib import Path
from shutil import rmtree
from tempfile import mkdtemp
from tempfile import TemporaryDirectory

from harmony_service_lib import BaseHarmonyAdapter
from harmony_service_lib.message import Source as HarmonySource
Expand Down Expand Up @@ -73,7 +71,8 @@ def get_asset_from_item(self, item: Item) -> Asset:
This is used to select which asset is used by HyBIG to generate
the browse image following these steps:
1. If found, return the first asset with 'visual' in any of the item's values' roles.
1. If found, return the first asset with 'visual' in any of the item's
values' roles.
2. If found, return the first asset that has 'data' in its item's values' roles.
3. Raise a StopIteration error.
Expand All @@ -93,61 +92,59 @@ def get_asset_from_item(self, item: Item) -> Asset:

def process_item(self, item: Item, source: HarmonySource) -> Item:
"""Processes a single input STAC item."""
try:
working_directory = mkdtemp()
results = item.clone()
results.assets = {}

asset = self.get_asset_from_item(item)

color_palette = get_color_palette_from_item(item)

# Download the input:
input_data_filename = download(
asset.href,
working_directory,
logger=self.logger,
cfg=self.config,
access_token=self.message.accessToken,
)
with TemporaryDirectory() as working_directory:
try:
results = item.clone()
results.assets = {}

asset = self.get_asset_from_item(item)

color_palette = get_color_palette_from_item(item)

# Download the input:
input_data_filename = download(
asset.href,
working_directory,
logger=self.logger,
cfg=self.config,
access_token=self.message.accessToken,
)

# Create browse images.
image_file_list = create_browse_imagery(
self.message,
input_data_filename,
source,
color_palette,
logger=self.logger,
)
# Create browse images.
image_file_list = create_browse_imagery(
self.message,
input_data_filename,
source,
color_palette,
logger=self.logger,
)

# image_file_list is a list of tuples (image, world, auxiliary)
# we need to stage them each individually, and then add their final
# locations to a list before creating the stac item.
item_assets = []

for (
browse_image_name,
world_file_name,
aux_xml_file_name,
) in image_file_list:
# Stage the images:
browse_image_url = self.stage_output(browse_image_name, asset.href)
browse_aux_url = self.stage_output(aux_xml_file_name, asset.href)
world_file_url = self.stage_output(world_file_name, asset.href)
item_assets.append(('data', browse_image_url, 'data'))
item_assets.append(('metadata', world_file_url, 'metadata'))
item_assets.append(('auxiliary', browse_aux_url, 'metadata'))

manifest_url = self.stage_manifest(image_file_list, asset.href)
item_assets.insert(0, ('data', manifest_url, 'metadata'))

return self.create_output_stac_item(item, item_assets)

except Exception as exception:
self.logger.exception(exception)
raise HyBIGServiceError from exception
finally:
rmtree(working_directory)
# image_file_list is a list of tuples (image, world, auxiliary)
# we need to stage them each individually, and then add their final
# locations to a list before creating the stac item.
item_assets = []

for (
browse_image_name,
world_file_name,
aux_xml_file_name,
) in image_file_list:
# Stage the images:
browse_image_url = self.stage_output(browse_image_name, asset.href)
browse_aux_url = self.stage_output(aux_xml_file_name, asset.href)
world_file_url = self.stage_output(world_file_name, asset.href)
item_assets.append(('data', browse_image_url, 'data'))
item_assets.append(('metadata', world_file_url, 'metadata'))
item_assets.append(('auxiliary', browse_aux_url, 'metadata'))

manifest_url = self.stage_manifest(image_file_list, asset.href)
item_assets.insert(0, ('data', manifest_url, 'metadata'))

return self.create_output_stac_item(item, item_assets)

except Exception as exception:
self.logger.exception(exception)
raise HyBIGServiceError(str(exception)) from exception

def stage_output(self, transformed_file: Path, input_file: str) -> str:
"""Generate an output file name based on the input asset URL and the
Expand All @@ -174,7 +171,7 @@ def create_output_stac_item(
"""Create an output STAC item used to access the browse imagery and
ESRI world file as staged in S3.
asset_items are an array of tuples where the tuples should be: (name,
item_assets is an array of tuples where the tuples should be: (name,
url, role)
"""
Expand All @@ -191,7 +188,7 @@ def create_output_stac_item(

output_stac_item.assets[asset_name] = Asset(
url,
title=basename(url),
title=Path(url).name,
media_type=get_file_mime_type(url),
roles=[role],
)
Expand Down
4 changes: 2 additions & 2 deletions harmony_service/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def get_tiled_file_extension(file_name: Path) -> str:
generate the correct one to pass in.
"""
ext_pattern = r"(\.r\d+c\d+)?\.(png|jpg|pgw|jgw|txt)(.aux.xml)?"
ext_pattern = r'(\.r\d+c\d+)?\.(png|jpg|pgw|jgw|txt)(.aux.xml)?'
match = re.search(ext_pattern, file_name.name)
return match.group()

Expand All @@ -34,7 +34,7 @@ def get_asset_name(name: str, url: str) -> str:
dictionary.
"""
tiled_pattern = r"\.(r\d+c\d+)\."
tiled_pattern = r'\.(r\d+c\d+)\.'
tile_id = re.search(tiled_pattern, url)
if tile_id is not None:
name = f'{name}_{tile_id.groups()[0]}'
Expand Down
16 changes: 8 additions & 8 deletions hybig/browse_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def get_harmony_message_from_params(params: dict | None) -> HarmonyMessage:

return HarmonyMessage(
{
"format": {
"mime": mime,
"crs": crs,
"srs": crs,
"scaleExtent": scale_extent,
"scaleSize": scale_size,
"height": height,
"width": width,
'format': {
'mime': mime,
'crs': crs,
'srs': crs,
'scaleExtent': scale_extent,
'scaleSize': scale_size,
'height': height,
'width': width,
},
}
)
4 changes: 2 additions & 2 deletions hybig/crs.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ def choose_crs_from_srs(srs: SRS):
"""
try:
if srs.epsg is not None and srs.epsg != "":
if srs.epsg is not None and srs.epsg != '':
return CRS.from_string(srs.epsg)
if srs.wkt is not None and srs.wkt != "":
if srs.wkt is not None and srs.wkt != '':
return CRS.from_string(srs.wkt)
return CRS.from_string(srs.proj4)
except Exception as exception:
Expand Down
2 changes: 1 addition & 1 deletion hybig/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


class HyBIGError(Exception):
"""Base error class for exceptions rasied by HyBIG library."""
"""Base error class for exceptions raised by HyBIG library."""

def __init__(self, message=None):
"""All HyBIG errors have a message field."""
Expand Down
32 changes: 16 additions & 16 deletions hybig/sizes.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,29 +79,29 @@ class Dimensions(TypedDict):

# This is Table 4.1.8-1 WGS84 (EPSG: 4326 Resolutions)
epsg_4326_resolutions = [
ResolutionInfo("2km", 0.017578125, 10240, 20480, 6, 2),
ResolutionInfo("1km", 0.0087890625, 20480, 40960, 7, 2),
ResolutionInfo("500m", 0.00439453125, 40960, 81920, 8, 2),
ResolutionInfo("250m", 0.002197265625, 81920, 163840, 9, 2),
ResolutionInfo("125m", 0.0010986328125, 163840, 327680, 10, 2),
ResolutionInfo("62.5m", 0.00054931640625, 327680, 655360, 11, 2),
ResolutionInfo("31.25m", 0.000274658203125, 655360, 1310720, 12, 2),
ResolutionInfo("15.625m", 0.0001373291015625, 1310720, 2521440, 13, 2),
ResolutionInfo('2km', 0.017578125, 10240, 20480, 6, 2),
ResolutionInfo('1km', 0.0087890625, 20480, 40960, 7, 2),
ResolutionInfo('500m', 0.00439453125, 40960, 81920, 8, 2),
ResolutionInfo('250m', 0.002197265625, 81920, 163840, 9, 2),
ResolutionInfo('125m', 0.0010986328125, 163840, 327680, 10, 2),
ResolutionInfo('62.5m', 0.00054931640625, 327680, 655360, 11, 2),
ResolutionInfo('31.25m', 0.000274658203125, 655360, 1310720, 12, 2),
ResolutionInfo('15.625m', 0.0001373291015625, 1310720, 2521440, 13, 2),
]

# Conversion used above: resolution / pixel_size
METERS_PER_DEGREE = 113777.77777777778

# This is Table 4.1.8-2 NSIDC Sea Ice Polar Stereographic Extent (EPSG:3413) Resolutions
epsg_3413_resolutions = [
ResolutionInfo("2km", 2048, 4096, 4096, 3, 2),
ResolutionInfo("1km", 1024, 8192, 8192, 4, 2),
ResolutionInfo("500m", 512, 16384, 16384, 5, 2),
ResolutionInfo("250m", 256, 32768, 32768, 6, 2),
ResolutionInfo("125m", 128, 65536, 65536, 7, 2),
ResolutionInfo("62.5m", 64, 131072, 131072, 8, 2),
ResolutionInfo("31.25m", 32, 252144, 252144, 9, 2),
ResolutionInfo("15.625m", 16, 524288, 524288, 10, 2),
ResolutionInfo('2km', 2048, 4096, 4096, 3, 2),
ResolutionInfo('1km', 1024, 8192, 8192, 4, 2),
ResolutionInfo('500m', 512, 16384, 16384, 5, 2),
ResolutionInfo('250m', 256, 32768, 32768, 6, 2),
ResolutionInfo('125m', 128, 65536, 65536, 7, 2),
ResolutionInfo('62.5m', 64, 131072, 131072, 8, 2),
ResolutionInfo('31.25m', 32, 252144, 252144, 9, 2),
ResolutionInfo('15.625m', 16, 524288, 524288, 10, 2),
]

# The Antarctic resolutions match the northern resolutions precisely.
Expand Down
14 changes: 14 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,17 @@ exclude = [

[tool.hatch.build.targets.wheel]
packages=["hybig"]

[tool.ruff]
lint.select = [
"E", # pycodestyle
"F", # pyflakes
"UP", # pyupgrade
"I", # organize imports
]

[tool.ruff.format]
quote-style = "single"

[tool.ruff.lint.pydocstyle]
convention = "google"
Loading

0 comments on commit 2f47607

Please sign in to comment.