Commit 7fd96b0f authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Removed classification code (now included in external library 'specclassify').


Signed-off-by: Daniel Scheffler's avatarDaniel Scheffler <danschef@gfz-potsdam.de>
parent 11d3bed4
Pipeline #4488 failed with stage
in 2 minutes and 12 seconds
This diff is collapsed.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_image_classifier
------------------------
Tests for gms_preprocessing.algorithms.classifier module.
"""
import unittest
import dill
import os
import zipfile
import tempfile
import numpy as np
from geoarray import GeoArray
from time import time
from gms_preprocessing import set_config
from gms_preprocessing.algorithms.classification import \
MinimumDistance_Classifier, kNN_MinimumDistance_Classifier, kNN_Classifier, SAM_Classifier, kNN_SAM_Classifier, \
FEDSA_Classifier, kNN_FEDSA_Classifier, SID_Classifier, RF_Classifier
from . import db_host
cfg = set_config(job_ID=26186196, db_host=db_host, reset_status=True, is_test=True)
path_classifier_zip = os.path.join(cfg.path_spechomo_classif, 'LR_classifiers.zip')
fName_cls = 'LR_clust50__Landsat-7__ETM+.dill'
test_gA = GeoArray(np.random.RandomState(0).randint(0, 10000, (1010, 1010, 6), np.int16)) # 6 Landsat-5 bands
test_gA[:5, 0, :] = -9999
test_gA[:5, 1, 3] = -9999
# get cluster centers
with zipfile.ZipFile(path_classifier_zip, "r") as zf, tempfile.TemporaryDirectory() as td:
zf.extract(fName_cls, td)
with open(os.path.join(td, fName_cls), 'rb') as inF:
undilled = dill.load(inF)
CC = undilled['1__2__3__4__5__7'][('Landsat-5', 'TM')]['1__2__3__4__5__7']
cluster_centers = np.array([ml.cluster_center for ml in CC.values()])
cluster_labels = list(CC.keys())
test_gA_pure_endmembers = np.zeros((1, cluster_centers.shape[0], cluster_centers.shape[1]), dtype=cluster_centers.dtype)
test_gA_pure_endmembers[:, :, :] = cluster_centers
class Test_MinimumDistance_Classifier(unittest.TestCase):
def test_classify(self):
t0 = time()
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels, CPUs=1)
cmap_sp = MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
print(time()-t0)
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels, CPUs=None)
cmap_mp = MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels, CPUs=None)
cmap_mp = MDC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
def test_label_unclassified_pixels_absolute_th(self):
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels, CPUs=1)
MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
MDC.label_unclassified_pixels(label_unclassified=-1, threshold=6000)
def test_label_unclassified_pixels_relative_th(self):
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels, CPUs=1)
MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
MDC.label_unclassified_pixels(label_unclassified=-1, threshold='10%')
class Test_kNN_MinimumDistance_Classifier(unittest.TestCase):
def setUp(self) -> None:
self.n_neighbors = 5
def test_classify(self):
MDC = kNN_MinimumDistance_Classifier(cluster_centers, cluster_labels, n_neighbors=self.n_neighbors, CPUs=1)
cmap_sp = MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010, self.n_neighbors))
MDC = kNN_MinimumDistance_Classifier(cluster_centers, cluster_labels, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010, self.n_neighbors))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
MDC = kNN_MinimumDistance_Classifier(cluster_centers, cluster_labels, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = MDC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp[:, :, 0].flatten(), cluster_labels))
def test_label_unclassified_pixels_absolute_th(self):
MDC = kNN_MinimumDistance_Classifier(cluster_centers, cluster_labels, n_neighbors=self.n_neighbors, CPUs=None)
MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
MDC.label_unclassified_pixels(label_unclassified=-1, threshold=6000)
def test_label_unclassified_pixels_relative_th(self):
MDC = kNN_MinimumDistance_Classifier(cluster_centers, cluster_labels, self.n_neighbors, CPUs=None)
MDC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
MDC.label_unclassified_pixels(label_unclassified=-1, threshold='10%')
class Test_kNN_Classifier(unittest.TestCase):
def test_classify(self):
kNNC = kNN_Classifier(cluster_centers, cluster_labels, CPUs=1)
cmap_sp = kNNC.classify(test_gA, in_nodataVal=-9999)
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
kNNC = kNN_Classifier(cluster_centers, cluster_labels, CPUs=None)
cmap_mp = kNNC.classify(test_gA, in_nodataVal=-9999)
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
kNNC = kNN_Classifier(cluster_centers, cluster_labels, CPUs=None)
cmap_mp = kNNC.classify(test_gA_pure_endmembers, in_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
class Test_SAM_Classifier(unittest.TestCase):
def test_classify(self):
SC = SAM_Classifier(cluster_centers, CPUs=1)
cmap_sp = SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
SC = SAM_Classifier(cluster_centers, CPUs=None)
cmap_mp = SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
SC = SAM_Classifier(cluster_centers, CPUs=None)
cmap_mp = SC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
def test_label_unclassified_pixels_absolute_th(self):
SC = SAM_Classifier(cluster_centers, CPUs=None)
SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
SC.label_unclassified_pixels(label_unclassified=-1, threshold=10)
def test_label_unclassified_pixels_relative_th(self):
SC = SAM_Classifier(cluster_centers, CPUs=None)
SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
SC.label_unclassified_pixels(label_unclassified=-1, threshold='10%')
class Test_kNN_SAM_Classifier(unittest.TestCase):
def setUp(self) -> None:
self.n_neighbors = 5
def test_classify(self):
SC = kNN_SAM_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=1)
cmap_sp = SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010, self.n_neighbors))
SC = kNN_SAM_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010, self.n_neighbors))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
SC = kNN_SAM_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = SC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp[:, :, 0].flatten(), cluster_labels))
def test_label_unclassified_pixels_absolute_th(self):
SC = kNN_SAM_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
SC.label_unclassified_pixels(label_unclassified=-1, threshold=10)
def test_label_unclassified_pixels_relative_th(self):
SC = kNN_SAM_Classifier(cluster_centers, self.n_neighbors, CPUs=None)
SC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
SC.label_unclassified_pixels(label_unclassified=-1, threshold='10%')
class Test_FEDSA_Classifier(unittest.TestCase):
def test_classify(self):
FC = FEDSA_Classifier(cluster_centers, CPUs=1)
cmap_sp = FC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
FC = FEDSA_Classifier(cluster_centers, CPUs=None)
cmap_mp = FC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
FC = FEDSA_Classifier(cluster_centers, CPUs=None)
cmap_mp = FC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, tiledims=(400, 200))
FC.label_unclassified_pixels(-1, 0.2)
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
class Test_kNN_FEDSA_Classifier(unittest.TestCase):
def setUp(self) -> None:
self.n_neighbors = 5
def test_classify(self):
FC = kNN_FEDSA_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=1)
cmap_sp = FC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010, self.n_neighbors))
FC = kNN_FEDSA_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = FC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010, self.n_neighbors))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
FC = kNN_FEDSA_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
cmap_mp = FC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, cmap_nodataVal=-9999)
self.assertTrue(np.array_equal(cmap_mp[:, :, 0].flatten(), cluster_labels))
def test_label_unclassified_pixels_absolute_th(self):
FC = kNN_FEDSA_Classifier(cluster_centers, n_neighbors=self.n_neighbors, CPUs=None)
FC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
FC.label_unclassified_pixels(label_unclassified=-1, threshold=10)
def test_label_unclassified_pixels_relative_th(self):
FC = kNN_FEDSA_Classifier(cluster_centers, self.n_neighbors, CPUs=None)
FC.classify(test_gA, in_nodataVal=-9999, cmap_nodataVal=-9999, tiledims=(400, 200))
FC.label_unclassified_pixels(label_unclassified=-1, threshold='10%')
class Test_SID_Classifier(unittest.TestCase):
def test_classify(self):
SC = SID_Classifier(cluster_centers, CPUs=1)
cmap_sp = SC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
SC = SID_Classifier(cluster_centers, CPUs=None)
cmap_mp = SC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
SC = SID_Classifier(cluster_centers, CPUs=None)
cmap_mp = SC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, tiledims=(400, 200))
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
class Test_RF_Classifier(unittest.TestCase):
def test_classify(self):
RFC = RF_Classifier(cluster_centers, cluster_labels, CPUs=1, **dict(random_state=0))
cmap_sp = RFC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_sp, GeoArray)
self.assertEqual(cmap_sp.shape, (1010, 1010))
RFC = RF_Classifier(cluster_centers, cluster_labels, CPUs=None, **dict(random_state=0))
cmap_mp = RFC.classify(test_gA, in_nodataVal=-9999, tiledims=(400, 200))
self.assertIsInstance(cmap_mp, GeoArray)
self.assertEqual(cmap_mp.shape, (1010, 1010))
self.assertTrue(np.array_equal(cmap_sp, cmap_mp))
RFC = RF_Classifier(cluster_centers, cluster_labels, CPUs=None, **dict(random_state=0))
cmap_mp = RFC.classify(test_gA_pure_endmembers, in_nodataVal=-9999, tiledims=(400, 200))
self.assertTrue(np.array_equal(cmap_mp.flatten(), cluster_labels))
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