Commit 5c2516f0 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

The initialization of NoDataMask and BadDataMask is now much faster in case a...


The initialization of NoDataMask and BadDataMask is now much faster in case a boolean array is passed.
Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent 353f7e4d
......@@ -2,6 +2,12 @@
History
=======
0.11.2 (2021-05-07)
-------------------
* The initialization of NoDataMask and BadDataMask is now much faster in case a boolean array is passed.
0.11.1 (2021-05-07)
-------------------
......
......@@ -29,7 +29,28 @@ from .baseclasses import GeoArray
__author__ = 'Daniel Scheffler'
class BadDataMask(GeoArray):
class _Mask(GeoArray):
_CLASSNAME = 'Mask baseclass'
def _validate_array_values(self, array):
if not array.dtype == np.bool:
if np.issubdtype(array.dtype, np.integer):
if array.min() < 0 or array.max() > 1:
pixelvals = sorted(list(np.unique(array)))
assert len(pixelvals) <= 2, 'The %s must have only two pixel values (boolean) - 0 and 1 or ' \
'False and True! The given mask for %s contains the values %s.' \
% (self._CLASSNAME, self.basename, pixelvals)
assert pixelvals in [[0, 1], [0], [1], [False, True], [False], [True]], \
'Found unsupported pixel values in the given %s for %s: %s. ' \
'Only the values True, False, 0 and 1 are supported. ' \
% (self._CLASSNAME, self.basename, pixelvals)
else:
raise TypeError('Boolean or integer array expected.')
class BadDataMask(_Mask):
_CLASSNAME = 'bad data mask'
def __init__(self, path_or_array, geotransform=None, projection=None, bandnames=None, nodata=False, progress=True,
q=False):
super(BadDataMask, self).__init__(path_or_array, geotransform=geotransform, projection=projection,
......@@ -52,17 +73,10 @@ class BadDataMask(GeoArray):
self._validate_array_values(ndarray)
self._arr = ndarray.astype(bool)
def _validate_array_values(self, maskarray):
pixelVals_in_mask = sorted(list(np.unique(maskarray)))
assert len(pixelVals_in_mask) <= 2, 'Bad data mask must have only two pixel values (boolean) - 0 and 1 or ' \
'False and True! The given mask for %s contains the values %s.' \
% (self.basename, pixelVals_in_mask)
assert pixelVals_in_mask in [[0, 1], [0], [1], [False, True], [False], [True]], \
'Found unsupported pixel values in the given bad data mask for %s: %s. Only the values True, False, 0 ' \
'and 1 are supported. ' % (self.basename, pixelVals_in_mask)
class NoDataMask(_Mask):
_CLASSNAME = 'no data mask'
class NoDataMask(GeoArray):
def __init__(self, path_or_array, geotransform=None, projection=None, bandnames=None, nodata=False, progress=True,
q=False):
super(NoDataMask, self).__init__(path_or_array, geotransform=geotransform, projection=projection,
......@@ -86,17 +100,10 @@ class NoDataMask(GeoArray):
self._validate_array_values(ndarray)
self._arr = ndarray.astype(bool)
def _validate_array_values(self, maskarray):
pixelVals_in_mask = sorted(list(np.unique(maskarray)))
assert len(pixelVals_in_mask) <= 2, 'Nodata mask must have only two pixel values (boolean) - 0 and 1 or ' \
'False and True! The given mask for %s contains the values %s.' % (
self.basename, pixelVals_in_mask)
assert pixelVals_in_mask in [[0, 1], [0], [1], [False, True], [False], [True]], \
'Found unsupported pixel values in the given Nodata mask for %s: %s. Only the values True, False, 0 ' \
'and 1 are supported. ' % (self.basename, pixelVals_in_mask)
class CloudMask(_Mask):
_CLASSNAME = 'cloud mask'
class CloudMask(GeoArray):
def __init__(self, path_or_array, geotransform=None, projection=None, bandnames=None, nodata=None, progress=True,
q=False):
# TODO implement class definitions and specific metadata
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# geoarray, A fast Python interface for image geodata - either on disk or in memory.
#
# Copyright (C) 2019 Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
#
# This software was developed within the context of the GeoMultiSens project funded
# by the German Federal Ministry of Education and Research
# (project grant code: 01 IS 14 010 A-C).
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Lesser General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
# details.
#
# You should have received a copy of the GNU Lesser General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
"""
test_masks
----------
Tests for the geoarray.masks module.
"""
from unittest import TestCase
import numpy as np
from geoarray import NoDataMask, BadDataMask
class Test_NoDataMask(TestCase):
def test_init_from_bool(self):
NoDataMask(np.array([[True, False], [False, False]]))
def test_init_from_int(self):
NoDataMask(np.array([[1, 0], [0, 0]]))
def test_init_out_of_range(self):
with self.assertRaises(AssertionError):
NoDataMask(np.array([[2, 0], [5, 0]]))
with self.assertRaises(AssertionError):
NoDataMask(np.array([[5, 2], [2, 5]]))
class Test_BadDataMask(TestCase):
def test_init_from_bool(self):
BadDataMask(np.array([[True, False], [False, False]]))
def test_init_from_int(self):
BadDataMask(np.array([[1, 0], [0, 0]]))
def test_init_out_of_range(self):
with self.assertRaises(AssertionError):
BadDataMask(np.array([[2, 0], [5, 0]]))
with self.assertRaises(AssertionError):
BadDataMask(np.array([[5, 2], [2, 5]]))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment