From 10ca3f5311e84280be346cbc405e77fe65e709e0 Mon Sep 17 00:00:00 2001 From: Daniel Scheffler Date: Tue, 3 Nov 2020 00:08:24 +0100 Subject: [PATCH] Added minimal version of geoarray. Signed-off-by: Daniel Scheffler --- .gitlab-ci.yml | 1 + HISTORY.rst | 1 + setup.py | 2 +- spechomo/prediction.py | 29 +++++++++++++++---- .../context/environment_spechomo.yml | 2 +- 5 files changed, 27 insertions(+), 8 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 7bbcceb..f3c3e98 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -12,6 +12,7 @@ test_spechomo: script: - source /root/miniconda3/bin/activate ci_env - conda install -y -c conda-forge "scikit-learn=0.23.2" # FIXME remove as soon as docker container is rebuilt + - pip install 'geoarray>=0.10.4' # FIXME remove as soon as docker container is rebuilt # run nosetests - make nosetests # test are called here diff --git a/HISTORY.rst b/HISTORY.rst index 3dd6e54..90d09ff 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -16,6 +16,7 @@ History (causes faulty predictions). * Bugfix for only choosing 25 spectra in classifier creation in case the maximum angle threshold is automatically set to 0 because there are many well matching spectra. +* Added minimal version of geoarray. 0.8.2 (2020-10-12) diff --git a/setup.py b/setup.py index 7550d9a..1294150 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ req = [ 'dill', - 'geoarray', + 'geoarray>=0.10.4', 'matplotlib', 'natsort', 'nested_dict', diff --git a/spechomo/prediction.py b/spechomo/prediction.py index 9546674..f8b2aca 100644 --- a/spechomo/prediction.py +++ b/spechomo/prediction.py @@ -96,8 +96,11 @@ def interpolate_cube(self, arrcube, source_CWLs, target_CWLs, kind='linear'): np.max(outarr) <= np.iinfo(np.int16).max: outarr = outarr.astype(np.int16) + elif np.min(outarr) >= np.iinfo(np.int32).min and np.max(outarr) <= np.iinfo(np.int32).max: + outarr = outarr.astype(np.int32) + else: raise TypeError('The interpolated data cube cannot be cast into a 16- or 32-bit integer array.') @@ -405,7 +408,8 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap if self.classif_alg == 'RF': train_spectra = np.vstack([classifier.MLdict[clust].cluster_sample_spectra for clust in range(classifier.n_clusters)]) - train_labels = list(np.hstack([[i] * 100 for i in range(classifier.n_clusters)])) + train_labels = list(np.hstack([[i] * 100 + for i in range(classifier.n_clusters)])) else: train_spectra = classifier.cluster_centers train_labels = classifier.cluster_pixVals @@ -424,7 +428,10 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap % time.strftime("%H:%M:%S", time.gmtime(time.time() - t0))) else: - self.classif_map = GeoArray(np.full((image.rows, image.cols), classifier.cluster_pixVals[0], np.int16), + self.classif_map = GeoArray(np.full((image.rows, + image.cols), + classifier.cluster_pixVals[0], + np.int16), nodata=cmap_nodataVal) # overwrite all pixels where the input image contains nodata in ANY band @@ -432,7 +439,8 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap if in_nodataVal is not None and cmap_nodataVal is not None: self.classif_map[np.any(image[:] == image.nodata, axis=2)] = cmap_nodataVal - self.distance_metrics = np.zeros_like(self.classif_map, np.float32) + self.distance_metrics = np.zeros_like(self.classif_map, + np.float32) #################### # apply prediction # @@ -446,9 +454,17 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap # NOTE: prediction is applied in 1000 x 1000 tiles to save memory (because classifier.predict returns float32) t0 = time.time() out_nodataVal = out_nodataVal if out_nodataVal is not None else image.nodata - image_predicted = GeoArray(np.empty((image.rows, image.cols, classifier.tgt_n_bands), dtype=image.dtype), - geotransform=image.gt, projection=image.prj, nodata=out_nodataVal, - bandnames=['B%s' % i if len(i) == 2 else 'B0%s' % i for i in classifier.tgt_LBA]) + image_predicted = GeoArray(np.empty((image.rows, + image.cols, + classifier.tgt_n_bands), + dtype=image.dtype), + geotransform=image.gt, + projection=image.prj, + nodata=out_nodataVal, + bandnames=['B%s' % i + if len(i) == 2 + else 'B0%s' % i + for i in classifier.tgt_LBA]) if classifier.n_clusters > 1 and\ self.classif_map.ndim > 2: @@ -458,6 +474,7 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap dist_norm = (self.distance_metrics - dist_min) /\ (dist_max - dist_min) weights = 1 - dist_norm + else: weights = None diff --git a/tests/gitlab_CI_docker/context/environment_spechomo.yml b/tests/gitlab_CI_docker/context/environment_spechomo.yml index 6d96eb3..176a076 100644 --- a/tests/gitlab_CI_docker/context/environment_spechomo.yml +++ b/tests/gitlab_CI_docker/context/environment_spechomo.yml @@ -6,7 +6,7 @@ channels: &id1 dependencies: - python>=3.7.* - pip - - geoarray>=0.9.0 + - geoarray>=0.10.4 - matplotlib - numpy - pandas