Commit d3a79944 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Moved Test_SAM_Classifier to new module test_image_classifier. Added...

Moved Test_SAM_Classifier to new module test_image_classifier. Added Test_MinimumDistance_Classifier and Test_kNN_Classifier.
parent 7d756443
#!/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 gms_preprocessing import set_config # noqa E402 module level import not at top of file
from gms_preprocessing.algorithms.classification import MinimumDistance_Classifier
from gms_preprocessing.algorithms.classification import kNN_Classifier
from gms_preprocessing.algorithms.classification import SAM_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.randint(0, 10000, (1010, 1010, 6), np.int16))
# 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())
class Test_MinimumDistance_Classifier(unittest.TestCase):
def test_classify(self):
MDC = MinimumDistance_Classifier(cluster_centers, cluster_labels)
cmap = MDC.classify(test_gA, nodataVal=-9999)
self.assertIsInstance(cmap, np.ndarray)
self.assertEqual(cmap.shape, (1010, 1010))
class Test_kNN_Classifier(unittest.TestCase):
def test_classify(self):
kNNC = kNN_Classifier(cluster_centers, cluster_labels)
cmap = kNNC.classify(test_gA, nodataVal=-9999)
self.assertIsInstance(cmap, np.ndarray)
self.assertEqual(cmap.shape, (1010, 1010))
class Test_SAM_Classifier(unittest.TestCase):
def test_classify(self):
SC = SAM_Classifier(cluster_centers)
cmap = SC.classify(test_gA, nodataVal=-9999)
self.assertIsInstance(cmap, np.ndarray)
self.assertEqual(cmap.shape, (1010, 1010))
......@@ -280,21 +280,3 @@ class Test_SpectralHomogenizer(unittest.TestCase):
self.assertIsInstance(errors, np.ndarray)
self.assertEqual(errors.shape, (50, 50, 13))
self.assertEqual(errors.dtype, np.int16)
class Test_SAM_Classifier(unittest.TestCase):
def test_classify(self):
from gms_preprocessing.algorithms.classification import SAM_Classifier
gA = GeoArray('/home/gfz-fe/scheffler/temp/Landsat-7__ETM+__LE71920242016104NSG00_image_data_L1A.bsq')
with open('/home/gfz-fe/scheffler/temp/SPECHOM_py/QR_clust50__Landsat-7__ETM+.dill', 'rb') as inF:
undilled = dill.load(inF)
cluster_centers = \
np.array([cc.cluster_center
for cc in undilled['1__2__3__4__5__7'][('Landsat-5', 'TM')]['1__2__3__4__5__7'].values()])
sub = gA[4465:4470, 4495:4500].astype(np.int16)
# sub[sub==-9999] = 10000
SC = SAM_Classifier(cluster_centers)
cmap = SC.classify(sub, nodataVal=-9999)
cmap
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