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

Added logging to SpectralResampler. Added first not working codes for...

Added logging to SpectralResampler. Added first not working codes for KMeansRSImage. Added test_kmeans. Updated minimal versions of in-house-libs.


Former-commit-id: 212d1793
parent e7188b72
......@@ -7,6 +7,10 @@ import numpy as np
from scipy.interpolate import interp1d
import scipy as sp
import matplotlib.pyplot as plt
from logging import Logger
from sklearn.cluster import KMeans
from geoarray import GeoArray # noqa F401 # flake8 issue
from ..config import GMS_config as CFG
from ..io.input_reader import SRF # noqa F401 # flake8 issue
......@@ -63,7 +67,7 @@ class L2B_object(L2A_object):
class SpectralResampler(object):
"""Class for spectral resampling of a single spectral signature (1D-array) or an image (3D-array)."""
def __init__(self, wvl_src, srf_tgt, wvl_unit='nanometers'):
def __init__(self, wvl_src, srf_tgt, wvl_unit='nanometers', logger=Logger(__name__)):
# type: (np.ndarray, SRF, str) -> None
"""Get an instance of the SpectralResampler1D class.
......@@ -82,6 +86,7 @@ class SpectralResampler(object):
self.wvl_src_nm = wvl if wvl_unit == 'nanometers' else wvl * 1000
self.srf_tgt = srf_tgt
self.wvl_unit = wvl_unit
self.logger = logger
def resample_signature(self, spectrum, scale_factor=10000, v=False):
# type: (np.ndarray, int, bool) -> np.ndarray
......@@ -149,6 +154,8 @@ class SpectralResampler(object):
image_rsp = np.zeros((R, C, B), dtype=image_cube.dtype)
for band_idx, (band, wvl_center) in enumerate(zip(self.srf_tgt.bands, self.srf_tgt.wvl)):
self.logger.info('Applying spectral resampling to band %s...' % band_idx)
# resample srf to 1 nm
srf_1nm = sp.interpolate.interp1d(self.srf_tgt.srfs_wvl, self.srf_tgt.srfs[band],
bounds_error=False, fill_value=0, kind='linear')(wvl_1nm)
......@@ -157,3 +164,24 @@ class SpectralResampler(object):
image_rsp[:, :, band_idx] = np.average(image_1nm, weights=srf_1nm, axis=2)
return image_rsp
class KMeansRSImage(object):
def __init__(self, im, n_clusters):
# type: (GeoArray, int) -> None
self.im = im
self.n_clusters = n_clusters
def compute_clusters(self):
# implement like this: https://www.pyimagesearch.com/2014/05/26/opencv-python-k-means-color-clustering/
pixels2d = self.im.reshape(self.im.rows * self.im.cols, 3)
kmeans = KMeans(n_clusters=self.n_clusters, random_state=0)
out = kmeans.fit(pixels2d)
print(out)
py_tools_ds>=0.9.1
geoarray>=0.6.12
arosics>=0.6.2
py_tools_ds>=0.9.3
geoarray>=0.6.15
arosics>=0.6.4
git+https://gitext.gfz-potsdam.de/EnMAP/sicor.git
matplotlib
numpy
......
......@@ -14,7 +14,7 @@ with open('HISTORY.rst') as history_file:
requirements = [
'matplotlib', 'numpy', 'scikit-learn', 'scipy', 'gdal', 'pyproj', 'shapely', 'ephem', 'pyorbital', 'dill', 'pytz',
'pandas', 'numba', 'spectral>=0.16', 'geopandas', 'iso8601', 'pyinstrument', 'geoalchemy2', 'sqlalchemy',
'psycopg2', 'py_tools_ds>=0.9.1', 'geoarray>=0.6.12', 'arosics>0.6.2', 'six'
'psycopg2', 'py_tools_ds>=0.9.3', 'geoarray>=0.6.15', 'arosics>0.6.4', 'six'
# spectral<0.16 has some problems with writing signed integer 8bit data
# fmask # conda install -c conda-forge python-fmask
# 'pyhdf', # conda install --yes -c conda-forge pyhdf
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_kmeans
-----------
Tests for gms_preprocessing.algorithms.L2B_P.KMeansRSImage
"""
import unittest
import numpy as np
import os
from geoarray import GeoArray
from gms_preprocessing import __file__
from gms_preprocessing.config import set_config
from gms_preprocessing.algorithms.L2B_P import KMeansRSImage
testdata = os.path.join(os.path.dirname(__file__),
'../tests/data/hy_spec_data/Bavaria_farmland_LMU_Hyspex_subset.bsq')
class Test_KMeansRSImage(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.SpectralResampler1D"""
@classmethod
def setUpClass(cls):
# Testjob Landsat-8
set_config(call_type='webapp', exec_mode='Python', job_ID=26186196, db_host='geoms', reset=True)
cls.geoArr = GeoArray(testdata)
cls.geoArr.to_mem()
cls.kmeans = KMeansRSImage(cls.geoArr, n_clusters=10)
def test_compute_clusters(self):
self.kmeans.compute_clusters()
......@@ -5,7 +5,7 @@
test_spectral_resampler
-----------------------
Tests for gms_preprocessing.algorithms.L2B_P.SpectralResampler1D
Tests for gms_preprocessing.algorithms.L2B_P.SpectralResampler
"""
import unittest
......
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