Skip to content

Commit

Permalink
Merge pull request #149 from siapy/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
janezlapajne authored Sep 3, 2024
2 parents b5eefba + d617b37 commit 4ecee8f
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
6 changes: 5 additions & 1 deletion .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
github: [janezlapajne]
github:
- janezlapajne
- siapy
ko_fi: janezlapajne
custom: buymeacoffee.com/janezlapajne
File renamed without changes.
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ nav:
- Plots: api/utils/plots.md
- Contributing: contributing.md
- Release Notes: changelog.md
- License: license.md
- License: permit.md

markdown_extensions:
# Python Markdown
Expand Down
17 changes: 17 additions & 0 deletions siapy/entities/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ def to_signatures(self, pixels: "Pixels") -> Signatures:
signatures = Signatures.from_array_and_pixels(image_arr, pixels)
return signatures

def to_subarray(self, pixels: "Pixels") -> np.ndarray:
image_arr = self.to_numpy()
u_max = pixels.u().max()
u_min = pixels.u().min()
v_max = pixels.v().max()
v_min = pixels.v().min()
# create new image
image_arr_area = np.nan * np.ones(
(v_max - v_min + 1, u_max - u_min + 1, self.bands)
)
# convert original coordinates to coordinates for new image
v_norm = pixels.v() - v_min
u_norm = pixels.u() - u_min
# write values from original image to new image
image_arr_area[v_norm, u_norm, :] = image_arr[pixels.v(), pixels.u(), :]
return image_arr_area

def mean(self, axis: int | tuple[int] | None = None) -> float | np.ndarray:
image_arr = self.to_numpy()
return np.nanmean(image_arr, axis=axis)
Expand Down
2 changes: 1 addition & 1 deletion siapy/entities/signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def _create(cls, pixels: Pixels, signals: Signals) -> "Signatures":
def from_array_and_pixels(cls, image: np.ndarray, pixels: Pixels) -> "Signatures":
u = pixels.u()
v = pixels.v()
signals_list = list(image[v, u, :])
signals_list = image[v, u, :]
signals = Signals(pd.DataFrame(signals_list))
return cls._create(pixels, signals)

Expand Down
24 changes: 24 additions & 0 deletions tests/entities/test_entities_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from siapy.entities import Pixels, Shape, SpectralImage
from siapy.entities.images import GeometricShapes, _parse_description
from siapy.utils.plots import pixels_select_lasso


def test_envi_open(configs):
Expand Down Expand Up @@ -180,6 +181,29 @@ def test_to_signatures(spectral_images):
assert np.array_equal(signatures.pixels.df.iloc[2].to_numpy(), iterable[2])


@pytest.mark.manual
def test_to_signatures_perf(spectral_images):
spectral_image_vnir = spectral_images.vnir
selected_areas_vnir = pixels_select_lasso(spectral_image_vnir)
spectral_image_vnir.to_signatures(selected_areas_vnir[0]).signals.to_numpy()
pass


def test_to_subarray(spectral_images):
spectral_image_vnir = spectral_images.vnir
iterable = [(1, 2), (3, 4), (2, 4)]
pixels = Pixels.from_iterable(iterable)
subarray = spectral_image_vnir.to_subarray(pixels)
expected_subarray = np.full((3, 3, spectral_image_vnir.bands), np.nan)
image_array = spectral_image_vnir.to_numpy()
expected_subarray[0, 0, :] = image_array[2, 1, :]
expected_subarray[2, 2, :] = image_array[4, 3, :]
expected_subarray[2, 1, :] = image_array[4, 2, :]

assert expected_subarray.shape == (3, 3, spectral_image_vnir.bands)
assert np.array_equal(subarray, expected_subarray, equal_nan=True)


def test_mean(spectral_images):
spectral_image_vnir = spectral_images.vnir

Expand Down

0 comments on commit 4ecee8f

Please sign in to comment.