Skip to content

Commit

Permalink
add warning & minor updates (#13)
Browse files Browse the repository at this point in the history
* add warning & minor updates

* fix workflows & linting

* update actions versions

* also the hidden ones

* warn for user input

* black

* incorporate feedback

* update CHANGELOG comment
  • Loading branch information
fwfichtner authored Jan 30, 2023
1 parent 0d46e7f commit 4096aa1
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 32 deletions.
15 changes: 5 additions & 10 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,8 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-python@v2
- name: black lint
run: |
python -m pip install black
black . --check --line-length 120
# - uses: psf/black@stable
# with:
# args: ". --check --line-length 120"
- uses: actions/checkout@v3
- uses: psf/black@stable
with:
options: "--check --line-length 120"
src: "."
4 changes: 2 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: '3.x'
- name: Install dependencies
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/pythonapp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7, 3.8, 3.9]
python-version: ['3.7', '3.8', '3.9', '3.10']

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
Expand All @@ -32,7 +32,7 @@ jobs:
coverage run -m pytest
coverage xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
file: coverage.xml
flags: pytest
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
Changelog
=========


[0.1.8] (2023-01-30)
--------------------
Added
*******
- warn user when smaller tiling than 256x256 is used

[0.1.7] (2022-12-09)
--------------------
Added
Expand Down
28 changes: 21 additions & 7 deletions tests/test_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
@pytest.mark.parametrize(
"img, band_order, nodata_value",
[
(np.empty((20, 20, 6), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((20, 20, 8), np.float32), ["Green", "Red", "Blue", "NIR", "SWIR1", "SWIR2", "NIR2", "ETC"], None),
(np.empty((20, 20, 6), np.float32), ["Green", "Red", "Blue", "NIR", "SWIR1", "SWIR2"], -666),
(np.empty((256, 256, 6), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((256, 256, 8), np.float32), ["Green", "Red", "Blue", "NIR", "SWIR1", "SWIR2", "NIR2", "ETC"], None),
(np.empty((256, 256, 6), np.float32), ["Green", "Red", "Blue", "NIR", "SWIR1", "SWIR2"], -666),
],
)
def test_csmask_init(img, band_order, nodata_value):
Expand All @@ -27,17 +27,30 @@ def test_csmask_init(img, band_order, nodata_value):
"img, band_order, nodata_value",
[
(3, ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((20, 20), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((20, 20, 3), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((20, 20, 6), np.uint8), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((20, 20, 6), np.float32), ["Blue", "Green", "Yellow", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((256, 256), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((256, 256, 3), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((256, 256, 6), np.uint8), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((256, 256, 6), np.float32), ["Blue", "Green", "Yellow", "NIR", "SWIR1", "SWIR2"], None),
],
)
def test_csmask_init_raises(img, band_order, nodata_value):
with pytest.raises(TypeError):
CSmask(img=img, band_order=band_order, nodata_value=nodata_value)


@pytest.mark.parametrize(
"img, band_order, nodata_value",
[
(np.empty((128, 128, 6), np.float32), ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"], None),
(np.empty((64, 64, 6), np.float32), ["Green", "Red", "Blue", "NIR", "SWIR1", "SWIR2"], -666),
],
)
def test_csmask_init_warns(img, band_order, nodata_value):
with pytest.warns(UserWarning):
CSmask(img=img, band_order=band_order, nodata_value=nodata_value)


@pytest.mark.filterwarnings("ignore::UserWarning")
@pytest.mark.parametrize(
"data",
[
Expand All @@ -57,6 +70,7 @@ def test_csmask_csm(data):
assert kappa >= 0.75


@pytest.mark.filterwarnings("ignore::UserWarning")
@pytest.mark.parametrize(
"data",
[
Expand Down
2 changes: 1 addition & 1 deletion ukis_csmask/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.1.7"
__version__ = "0.1.8"
9 changes: 9 additions & 0 deletions ukis_csmask/mask.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import warnings

import numpy as np

from pathlib import Path
Expand Down Expand Up @@ -52,6 +54,13 @@ def __init__(
if img.dtype != np.float32:
raise TypeError("img must be in top of atmosphere reflectance with dtype float32")

if img.shape[0] < 256 or img.shape[1] < 256:
warnings.warn(
message=f"Your input image is smaller than the internal tiling size of 256x256 pixels. This may result "
f"in suboptimal performance. Consider using a larger image size.",
category=UserWarning,
)

# consistency checks on band_order
target_band_order = ["Blue", "Green", "Red", "NIR", "SWIR1", "SWIR2"]
if band_order != target_band_order:
Expand Down
16 changes: 8 additions & 8 deletions ukis_csmask/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import numpy as np
import scipy.signal
from scipy.signal.windows import tukey


def reclassify(array, class_dict):
Expand Down Expand Up @@ -27,9 +27,9 @@ def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toen
:param array: Array to which the rolling window is applied (array_like).
:param window: Either a single integer to create a window of only the last axis or a
tuple to create it for the last len(window) axes. 0 can be used as a to ignore a
dimension in the window (int or tuple).
:param asteps: Aligned at the last axis, new steps for the original array, ie. for
tuple to create it for the last len(window) axes. 0 can be used as a placeholder
to ignore a dimension in the window (int or tuple).
:param asteps: Aligned at the last axis, new steps for the original array, i.e. for
creation of non-overlapping windows (tuple).
:param wsteps: Steps for the added window dimensions. These can be 0 to repeat values
along the axis (int or tuple (same size as window)).
Expand All @@ -41,7 +41,7 @@ def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toen
end makes it easier to get the neighborhood, however toend=False will give
a more intuitive result if you view the whole array (bool).
:returns: A view on `array` which is smaller to fit the windows and has windows added
dimensions (0s not counting), ie. every point of `array` is an array of size
dimensions (0s not counting), i.e. every point of `array` is an array of size
window. (ndarray).
"""
array = np.asarray(array)
Expand Down Expand Up @@ -89,7 +89,7 @@ def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toen
_wsteps[window == 0] = 1
wsteps = _wsteps

# Check that the window would not be larger then the original:
# Check that the window would not be larger than the original:
if np.any(orig_shape[-len(window) :] < window * wsteps):
raise ValueError("`window` * `wsteps` larger then `array` in at least one dimension.")

Expand All @@ -101,7 +101,7 @@ def rolling_window(array, window=(0,), asteps=None, wsteps=None, axes=None, toen

new_shape[-len(window) :] += wsteps - _window * wsteps
new_shape = (new_shape + asteps - 1) // asteps
# make sure the new_shape is at least 1 in any "old" dimension (ie. steps
# make sure the new_shape is at least 1 in any "old" dimension (i.e. steps
# is (too) large, but we do not care.
new_shape[new_shape < 1] = 1
shape = new_shape
Expand Down Expand Up @@ -227,7 +227,7 @@ def untile_array(array_tiled, target_shape, overlap=0.1, smooth_blending=False):
raise ValueError("overlap needs to be <=0.5 when using smooth blending.")

# define tapered cosine function (tukey) to be used for smooth blending
window1d = scipy.signal.tukey(M=xsize, alpha=overlap * 2)
window1d = tukey(M=xsize, alpha=overlap * 2)
window2d = np.expand_dims(np.expand_dims(window1d, axis=1), axis=2)
window2d = window2d * window2d.transpose(1, 0, 2)

Expand Down

0 comments on commit 4096aa1

Please sign in to comment.