Commit 212d1793 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.
parent df54bc8b
Pipeline #1368 failed with stage
in 7 minutes and 39 seconds
......@@ -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 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)):'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 = im
self.n_clusters = n_clusters
def compute_clusters(self):
# implement like this:
pixels2d = *, 3)
kmeans = KMeans(n_clusters=self.n_clusters, random_state=0)
out =
......@@ -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 -*-
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__),
class Test_KMeansRSImage(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.SpectralResampler1D"""
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.kmeans = KMeansRSImage(cls.geoArr, n_clusters=10)
def test_compute_clusters(self):
......@@ -5,7 +5,7 @@
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