Skip to content

Commit

Permalink
Merge pull request #55 from siapy/feature
Browse files Browse the repository at this point in the history
Feature
  • Loading branch information
janezlapajne authored Jul 5, 2024
2 parents 6a65658 + 3059dc0 commit b4841cb
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 77 deletions.
10 changes: 1 addition & 9 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,13 @@ end_of_line = lf
charset = utf-8
insert_final_newline = true
trim_trailing_whitespace = true
indent_style = tab
indent_style = space
indent_size = 4

[{*.md}]
indent_style = unset
indent_size = unset

[**/Dockerfile]
indent_style = space
indent_size = 2

[*.{yml,yaml,js,json,sh}]
indent_style = space
indent_size = 2

[*.{py}]
indent_style = space
indent_size = 4
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.2.4](https://github.com/siapy/siapy-lib/compare/v0.2.3...v0.2.4) (2024-07-04)


### Documentation

* moved files from .github -> repo root ([b1a3989](https://github.com/siapy/siapy-lib/commit/b1a3989e07d20825414713dddcbe859837f6e281))
* security put in place ([8585538](https://github.com/siapy/siapy-lib/commit/8585538064f0e3eee1c2a76e156a1400d94abbcc))

## [0.2.3](https://github.com/siapy/siapy-lib/compare/v0.2.2...v0.2.3) (2024-07-01)


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "siapy"
version = "0.2.3"
version = "0.2.4"
description = "A python library for efficient processing of spectral images."
authors = [{ name = "janezlapajne", email = "[email protected]" }]

Expand Down
11 changes: 11 additions & 0 deletions siapy/entities/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ def __repr__(self) -> str:
def __str__(self) -> str:
return str(self._sp_file)

def __lt__(self, other: "SpectralImage") -> bool:
return self.filepath.name < other.filepath.name

def __eq__(self, other: Any) -> bool:
if not isinstance(other, SpectralImage):
return NotImplemented
return (
self.filepath.name == other.filepath.name
and self._sp_file == other._sp_file
)

@classmethod
def envi_open(
cls, *, header_path: str | Path, image_path: str | Path | None = None
Expand Down
15 changes: 14 additions & 1 deletion siapy/entities/imagesets.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from dataclasses import dataclass
from pathlib import Path
from typing import Iterator
from typing import Any, Iterator

import numpy as np
from rich.progress import track

from siapy.core import logger
Expand Down Expand Up @@ -57,3 +58,15 @@ def from_paths(
@property
def images(self) -> list[SpectralImage]:
return self._images

@property
def cameras_id(self) -> list[str]:
return list({image.camera_id for image in self.images})

def images_by_camera_id(self, camera_id: str):
ids = np.array([image.camera_id for image in self.images])
indices = np.nonzero(ids == camera_id)[0]
return [image for idx, image in enumerate(self.images) if idx in indices]

def sort(self, key: Any = None, reverse: bool = False):
self.images.sort(key=key, reverse=reverse)
6 changes: 0 additions & 6 deletions tests/configs.py

This file was deleted.

35 changes: 22 additions & 13 deletions tests/fixtures.py → tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@
import numpy as np
import pytest

from siapy.core.configs import TEST_DATA_DIR
from siapy.entities import Pixels, SpectralImage
from tests.configs import (
image_swir_hdr_path,
image_swir_img_path,
image_vnir_hdr_path,
image_vnir_img_path,
)


class PytestConfigs(SimpleNamespace):
image_vnir_hdr_path = TEST_DATA_DIR / "vnir.hdr"
image_vnir_img_path = TEST_DATA_DIR / "vnir.hyspex"
image_swir_hdr_path = TEST_DATA_DIR / "swir.hdr"
image_swir_img_path = TEST_DATA_DIR / "swir.hyspex"
image_vnir_name = "VNIR_1600_SN0034"
image_swir_name = "SWIR_384me_SN3109"


@pytest.fixture(scope="session")
def configs():
return PytestConfigs()


class SpectralImages(SimpleNamespace):
Expand All @@ -19,15 +28,15 @@ class SpectralImages(SimpleNamespace):
swir_np: np.ndarray


@pytest.fixture
def spectral_images() -> SpectralImages:
@pytest.fixture(scope="module")
def spectral_images(configs) -> SpectralImages:
spectral_image_vnir = SpectralImage.envi_open(
header_path=image_vnir_hdr_path,
image_path=image_vnir_img_path,
header_path=configs.image_vnir_hdr_path,
image_path=configs.image_vnir_img_path,
)
spectral_image_swir = SpectralImage.envi_open(
header_path=image_swir_hdr_path,
image_path=image_swir_img_path,
header_path=configs.image_swir_hdr_path,
image_path=configs.image_swir_img_path,
)
spectral_image_vnir_np = spectral_image_vnir.to_numpy()
spectral_image_swir_np = spectral_image_swir.to_numpy()
Expand All @@ -44,7 +53,7 @@ class CorrespondingPixels(SimpleNamespace):
swir: Pixels


@pytest.fixture
@pytest.fixture(scope="module")
def corresponding_pixels() -> CorrespondingPixels:
pixels_vnir = np.array(
[
Expand Down
44 changes: 26 additions & 18 deletions tests/entities/test_entities_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

from siapy.entities import Pixels, Shape, SpectralImage
from siapy.entities.images import GeometricShapes, _parse_description
from tests.configs import (
image_swir_hdr_path,
image_swir_img_path,
image_vnir_hdr_path,
image_vnir_img_path,
)
from tests.fixtures import corresponding_pixels, spectral_images # noqa: F401


def test_envi_open():
def test_envi_open(configs):
spectral_image_vnir = SpectralImage.envi_open(
header_path=image_vnir_hdr_path,
image_path=image_vnir_img_path,
header_path=configs.image_vnir_hdr_path,
image_path=configs.image_vnir_img_path,
)
assert isinstance(spectral_image_vnir, SpectralImage)

spectral_image_swir = SpectralImage.envi_open(
header_path=image_swir_hdr_path,
image_path=image_swir_img_path,
header_path=configs.image_swir_hdr_path,
image_path=configs.image_swir_img_path,
)
assert isinstance(spectral_image_swir, SpectralImage)

Expand All @@ -45,6 +38,15 @@ def test_str(spectral_images):
assert isinstance(str(spectral_images.swir), str)


def test_lt(spectral_images):
assert spectral_images.vnir > spectral_images.swir


def test_eq(spectral_images):
assert spectral_images.vnir == spectral_images.vnir
assert spectral_images.swir != spectral_images.vnir


def test_file(spectral_images):
assert isinstance(
spectral_images.vnir.file,
Expand Down Expand Up @@ -95,11 +97,11 @@ def test_default_bands(spectral_images):
assert np.array_equal(swir_db, [20, 117, 57])


def test_filename(spectral_images):
def test_filename(spectral_images, configs):
assert isinstance(spectral_images.vnir.filepath, Path)
assert isinstance(spectral_images.swir.filepath, Path)
assert spectral_images.vnir.filepath.name == image_vnir_img_path.name
assert spectral_images.swir.filepath.name == image_swir_img_path.name
assert spectral_images.vnir.filepath.name == configs.image_vnir_img_path.name
assert spectral_images.swir.filepath.name == configs.image_swir_img_path.name


def test_wavelengths(spectral_images):
Expand All @@ -123,11 +125,11 @@ def test_description(spectral_images):
assert all(key in swir_desc.keys() for key in required_keys)


def test_camera_id(spectral_images):
def test_camera_id(spectral_images, configs):
vnir_cam_id = spectral_images.vnir.camera_id
swir_cam_id = spectral_images.swir.camera_id
assert vnir_cam_id == "VNIR_1600_SN0034"
assert swir_cam_id == "SWIR_384me_SN3109"
assert vnir_cam_id == configs.image_vnir_name
assert swir_cam_id == configs.image_swir_name


def test_to_numpy(spectral_images):
Expand Down Expand Up @@ -308,6 +310,7 @@ def test_geometric_shapes_append_valid(spectral_images, corresponding_pixels):
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.append(rect)
assert rect in spectral_images.vnir.geometric_shapes
assert len(spectral_images.vnir.geometric_shapes) == 1
Expand All @@ -323,6 +326,7 @@ def test_geometric_shapes_extend_valid(spectral_images, corresponding_pixels):
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.extend([rect, rect])
assert len(spectral_images.vnir.geometric_shapes) == 2

Expand Down Expand Up @@ -351,6 +355,7 @@ def test_geometric_shapes_remove_valid(spectral_images, corresponding_pixels):
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.append(rect)
spectral_images.vnir.geometric_shapes.remove(rect)
assert rect not in spectral_images.vnir.geometric_shapes
Expand All @@ -366,6 +371,7 @@ def test_geometric_shapes_pop(spectral_images, corresponding_pixels):
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.append(rect)
popped_shape = spectral_images.vnir.geometric_shapes.pop()
assert popped_shape == rect
Expand Down Expand Up @@ -404,6 +410,7 @@ def test_geometric_shapes_count(spectral_images, corresponding_pixels):
rect = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.append(rect)
spectral_images.vnir.geometric_shapes.append(rect)
count = spectral_images.vnir.geometric_shapes.count(rect)
Expand Down Expand Up @@ -431,6 +438,7 @@ def test_geometric_shapes_sort(spectral_images, corresponding_pixels):
rect2 = Shape.from_shape_type(
shape_type="rectangle", pixels=corresponding_pixels.vnir, label="A"
)
spectral_images.vnir.geometric_shapes.clear()
spectral_images.vnir.geometric_shapes.append(rect1)
spectral_images.vnir.geometric_shapes.append(rect2)
spectral_images.vnir.geometric_shapes.sort(key=lambda shape: shape.label)
Expand Down
Loading

0 comments on commit b4841cb

Please sign in to comment.