Commit 4f9923b9 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Moved spectral homogenization to new separate library 'spechomo'.

parent ab9e52ca
Pipeline #3779 failed with stage
in 2 minutes and 8 seconds
This diff is collapsed.
......@@ -323,8 +323,6 @@ def classify_image(image, train_spectra, train_labels, classif_alg, in_nodataVal
def normalize_endmembers_image(endmembers, image):
# type: (np.ndarray, np.ndarray) -> Tuple[np.ndarray, np.ndarray]
from .L2B_P import im2spectra, spectra2im # avoid circular import
em = endmembers.astype(np.float)
im = image.astype(np.float)
......@@ -340,3 +338,21 @@ def normalize_endmembers_image(endmembers, image):
else:
return em, im
def im2spectra(geoArr):
# type: (Union[GeoArray, np.ndarray]) -> np.ndarray
"""Convert 3D images to array of spectra samples (rows: samples; cols: spectral information)."""
return geoArr.reshape((geoArr.shape[0] * geoArr.shape[1], geoArr.shape[2]))
def spectra2im(spectra, tgt_rows, tgt_cols):
# type: (Union[GeoArray, np.ndarray], int, int) -> np.ndarray
"""Convert array of spectra samples (rows: samples; cols: spectral information) to a 3D image.
:param spectra: 2D array with rows: spectral samples / columns: spectral information (bands)
:param tgt_rows: number of target image rows
:param tgt_cols: number of target image rows
:return: 3D array (rows x columns x spectral bands)
"""
return spectra.reshape(tgt_rows, tgt_cols, spectra.shape[1])
......@@ -37,26 +37,3 @@ class FmaskError(RuntimeError):
class ACNotSupportedError(RuntimeError):
"""An error raised if there is currently no AC supported for the current sensor."""
####################################
# SPECTRAL HOMOGENIZATION EXCEPTIONS
####################################
class ClassifierNotAvailableError(RuntimeError):
def __init__(self, spechomo_method, src_sat, src_sen, src_LBA, tgt_sat, tgt_sen, tgt_LBA, n_clusters):
self.spechomo_method = spechomo_method
self.src_sat = src_sat
self.src_sen = src_sen
self.src_LBA = src_LBA
self.tgt_sat = tgt_sat
self.tgt_sen = tgt_sen
self.tgt_LBA = tgt_LBA
self.n_clusters = n_clusters
RuntimeError.__init__(self)
def __str__(self):
return 'No %s classifier available for predicting %s %s %s from %s %s %s (%d clusters).'\
% (self.spechomo_method, self.tgt_sat, self.tgt_sen, self.tgt_LBA,
self.src_sat, self.src_sen, self.src_LBA, self.n_clusters)
......@@ -9,6 +9,7 @@ import os
from ..options.config import GMS_config as CFG
# try:
# # noinspection PyCompatibility
# from StringIO import StringIO # Python 2
# except ImportError:
# from io import StringIO # Python 3
......@@ -32,6 +33,7 @@ class GMS_logger(logging.Logger):
# private attributes
self._captured_stream = ''
# attributes that need to be present in order to unpickle the logger via __setstate_
self.name_logfile = name_logfile
self.fmt_suffix = fmt_suffix
self.path_logfile = path_logfile
......
ENVI
description = {
File Resize Result, x resize factor: 1.000000, y resize factor: 1.000000.
[Thu Jan 5 13:15:35 2017]}
samples = 50
lines = 50
bands = 325
header offset = 0
file type = ENVI Standard
data type = 4
interleave = bsq
sensor type = Unknown
byte order = 0
map info = { UTM , 1.000 , 1.000 , 340998.025 , 5396656.747 , 4.0000000000e+00 , 4.0000000000e+00 , 33 , North , WGS-84 , units=Meters }
y start = 87
coordinate system string = { PROJCS["UTM_Zone_33N" , GEOGCS["GCS_WGS_1984" , DATUM["D_WGS_1984" , SPHEROID["WGS_1984" , 6378137.0 , 298.257223563]] , PRIMEM["Greenwich" , 0.0] , UNIT["Degree" , 0.0174532925199433]] , PROJECTION["Transverse_Mercator"] , PARAMETER["False_Easting" , 500000.0] , PARAMETER["False_Northing" , 0.0] , PARAMETER["Central_Meridian" , 15.0] , PARAMETER["Scale_Factor" , 0.9996] , PARAMETER["Latitude_Of_Origin" , 0.0] , UNIT["Meter" , 1.0]] }
wavelength units = Nanometers
band names = { Resize (band 1:LMU_Hyspex_2) , Resize (band 2:LMU_Hyspex_2) , Resize (band 3:LMU_Hyspex_2) , Resize (band 4:LMU_Hyspex_2) , Resize (band 5:LMU_Hyspex_2) , Resize (band 6:LMU_Hyspex_2) , Resize (band 7:LMU_Hyspex_2) , Resize (band 8:LMU_Hyspex_2) , Resize (band 9:LMU_Hyspex_2) , Resize (band 10:LMU_Hyspex_2) , Resize (band 11:LMU_Hyspex_2) , Resize (band 12:LMU_Hyspex_2) , Resize (band 13:LMU_Hyspex_2) , Resize (band 14:LMU_Hyspex_2) , Resize (band 15:LMU_Hyspex_2) , Resize (band 16:LMU_Hyspex_2) , Resize (band 17:LMU_Hyspex_2) , Resize (band 18:LMU_Hyspex_2) , Resize (band 19:LMU_Hyspex_2) , Resize (band 20:LMU_Hyspex_2) , Resize (band 21:LMU_Hyspex_2) , Resize (band 22:LMU_Hyspex_2) , Resize (band 23:LMU_Hyspex_2) , Resize (band 24:LMU_Hyspex_2) , Resize (band 25:LMU_Hyspex_2) , Resize (band 26:LMU_Hyspex_2) , Resize (band 27:LMU_Hyspex_2) , Resize (band 28:LMU_Hyspex_2) , Resize (band 29:LMU_Hyspex_2) , Resize (band 30:LMU_Hyspex_2) , Resize (band 31:LMU_Hyspex_2) , Resize (band 32:LMU_Hyspex_2) , Resize (band 33:LMU_Hyspex_2) , Resize (band 34:LMU_Hyspex_2) , Resize (band 35:LMU_Hyspex_2) , Resize (band 36:LMU_Hyspex_2) , Resize (band 37:LMU_Hyspex_2) , Resize (band 38:LMU_Hyspex_2) , Resize (band 39:LMU_Hyspex_2) , Resize (band 40:LMU_Hyspex_2) , Resize (band 41:LMU_Hyspex_2) , Resize (band 42:LMU_Hyspex_2) , Resize (band 43:LMU_Hyspex_2) , Resize (band 44:LMU_Hyspex_2) , Resize (band 45:LMU_Hyspex_2) , Resize (band 46:LMU_Hyspex_2) , Resize (band 47:LMU_Hyspex_2) , Resize (band 48:LMU_Hyspex_2) , Resize (band 49:LMU_Hyspex_2) , Resize (band 50:LMU_Hyspex_2) , Resize (band 51:LMU_Hyspex_2) , Resize (band 52:LMU_Hyspex_2) , Resize (band 53:LMU_Hyspex_2) , Resize (band 54:LMU_Hyspex_2) , Resize (band 55:LMU_Hyspex_2) , Resize (band 56:LMU_Hyspex_2) , Resize (band 57:LMU_Hyspex_2) , Resize (band 58:LMU_Hyspex_2) , Resize (band 59:LMU_Hyspex_2) , Resize (band 60:LMU_Hyspex_2) , Resize (band 61:LMU_Hyspex_2) , Resize (band 62:LMU_Hyspex_2) , Resize (band 63:LMU_Hyspex_2) , Resize (band 64:LMU_Hyspex_2) , Resize (band 65:LMU_Hyspex_2) , Resize (band 66:LMU_Hyspex_2) , Resize (band 67:LMU_Hyspex_2) , Resize (band 68:LMU_Hyspex_2) , Resize (band 69:LMU_Hyspex_2) , Resize (band 70:LMU_Hyspex_2) , Resize (band 71:LMU_Hyspex_2) , Resize (band 72:LMU_Hyspex_2) , Resize (band 73:LMU_Hyspex_2) , Resize (band 74:LMU_Hyspex_2) , Resize (band 75:LMU_Hyspex_2) , Resize (band 76:LMU_Hyspex_2) , Resize (band 77:LMU_Hyspex_2) , Resize (band 78:LMU_Hyspex_2) , Resize (band 79:LMU_Hyspex_2) , Resize (band 80:LMU_Hyspex_2) , Resize (band 81:LMU_Hyspex_2) , Resize (band 82:LMU_Hyspex_2) , Resize (band 83:LMU_Hyspex_2) , Resize (band 84:LMU_Hyspex_2) , Resize (band 85:LMU_Hyspex_2) , Resize (band 86:LMU_Hyspex_2) , Resize (band 87:LMU_Hyspex_2) , Resize (band 88:LMU_Hyspex_2) , Resize (band 89:LMU_Hyspex_2) , Resize (band 90:LMU_Hyspex_2) , Resize (band 91:LMU_Hyspex_2) , Resize (band 92:LMU_Hyspex_2) , Resize (band 93:LMU_Hyspex_2) , Resize (band 94:LMU_Hyspex_2) , Resize (band 95:LMU_Hyspex_2) , Resize (band 96:LMU_Hyspex_2) , Resize (band 97:LMU_Hyspex_2) , Resize (band 98:LMU_Hyspex_2) , Resize (band 99:LMU_Hyspex_2) , Resize (band 100:LMU_Hyspex_2) , Resize (band 101:LMU_Hyspex_2) , Resize (band 102:LMU_Hyspex_2) , Resize (band 103:LMU_Hyspex_2) , Resize (band 104:LMU_Hyspex_2) , Resize (band 105:LMU_Hyspex_2) , Resize (band 106:LMU_Hyspex_2) , Resize (band 107:LMU_Hyspex_2) , Resize (band 108:LMU_Hyspex_2) , Resize (band 109:LMU_Hyspex_2) , Resize (band 110:LMU_Hyspex_2) , Resize (band 111:LMU_Hyspex_2) , Resize (band 112:LMU_Hyspex_2) , Resize (band 113:LMU_Hyspex_2) , Resize (band 114:LMU_Hyspex_2) , Resize (band 115:LMU_Hyspex_2) , Resize (band 116:LMU_Hyspex_2) , Resize (band 117:LMU_Hyspex_2) , Resize (band 118:LMU_Hyspex_2) , Resize (band 119:LMU_Hyspex_2) , Resize (band 120:LMU_Hyspex_2) , Resize (band 121:LMU_Hyspex_2) , Resize (band 122:LMU_Hyspex_2) , Resize (band 123:LMU_Hyspex_2) , Resize (band 124:LMU_Hyspex_2) , Resize (band 125:LMU_Hyspex_2) , Resize (band 126:LMU_Hyspex_2) , Resize (band 127:LMU_Hyspex_2) , Resize (band 128:LMU_Hyspex_2) , Resize (band 129:LMU_Hyspex_2) , Resize (band 130:LMU_Hyspex_2) , Resize (band 131:LMU_Hyspex_2) , Resize (band 132:LMU_Hyspex_2) , Resize (band 133:LMU_Hyspex_2) , Resize (band 134:LMU_Hyspex_2) , Resize (band 135:LMU_Hyspex_2) , Resize (band 136:LMU_Hyspex_2) , Resize (band 137:LMU_Hyspex_2) , Resize (band 138:LMU_Hyspex_2) , Resize (band 139:LMU_Hyspex_2) , Resize (band 140:LMU_Hyspex_2) , Resize (band 141:LMU_Hyspex_2) , Resize (band 142:LMU_Hyspex_2) , Resize (band 143:LMU_Hyspex_2) , Resize (band 144:LMU_Hyspex_2) , Resize (band 145:LMU_Hyspex_2) , Resize (band 146:LMU_Hyspex_2) , Resize (band 147:LMU_Hyspex_2) , Resize (band 148:LMU_Hyspex_2) , Resize (band 149:LMU_Hyspex_2) , Resize (band 150:LMU_Hyspex_2) , Resize (band 151:LMU_Hyspex_2) , Resize (band 152:LMU_Hyspex_2) , Resize (band 153:LMU_Hyspex_2) , Resize (band 154:LMU_Hyspex_2) , Resize (band 155:LMU_Hyspex_2) , Resize (band 156:LMU_Hyspex_2) , Resize (band 157:LMU_Hyspex_2) , Resize (band 158:LMU_Hyspex_2) , Resize (band 159:LMU_Hyspex_2) , Resize (band 160:LMU_Hyspex_2) , Resize (band 161:LMU_Hyspex_2) , Resize (band 162:LMU_Hyspex_2) , Resize (band 163:LMU_Hyspex_2) , Resize (band 164:LMU_Hyspex_2) , Resize (band 165:LMU_Hyspex_2) , Resize (band 166:LMU_Hyspex_2) , Resize (band 167:LMU_Hyspex_2) , Resize (band 168:LMU_Hyspex_2) , Resize (band 169:LMU_Hyspex_2) , Resize (band 170:LMU_Hyspex_2) , Resize (band 171:LMU_Hyspex_2) , Resize (band 172:LMU_Hyspex_2) , Resize (band 173:LMU_Hyspex_2) , Resize (band 174:LMU_Hyspex_2) , Resize (band 175:LMU_Hyspex_2) , Resize (band 176:LMU_Hyspex_2) , Resize (band 177:LMU_Hyspex_2) , Resize (band 178:LMU_Hyspex_2) , Resize (band 179:LMU_Hyspex_2) , Resize (band 180:LMU_Hyspex_2) , Resize (band 181:LMU_Hyspex_2) , Resize (band 182:LMU_Hyspex_2) , Resize (band 183:LMU_Hyspex_2) , Resize (band 184:LMU_Hyspex_2) , Resize (band 185:LMU_Hyspex_2) , Resize (band 186:LMU_Hyspex_2) , Resize (band 187:LMU_Hyspex_2) , Resize (band 188:LMU_Hyspex_2) , Resize (band 189:LMU_Hyspex_2) , Resize (band 190:LMU_Hyspex_2) , Resize (band 191:LMU_Hyspex_2) , Resize (band 192:LMU_Hyspex_2) , Resize (band 193:LMU_Hyspex_2) , Resize (band 194:LMU_Hyspex_2) , Resize (band 195:LMU_Hyspex_2) , Resize (band 196:LMU_Hyspex_2) , Resize (band 197:LMU_Hyspex_2) , Resize (band 198:LMU_Hyspex_2) , Resize (band 199:LMU_Hyspex_2) , Resize (band 200:LMU_Hyspex_2) , Resize (band 201:LMU_Hyspex_2) , Resize (band 202:LMU_Hyspex_2) , Resize (band 203:LMU_Hyspex_2) , Resize (band 204:LMU_Hyspex_2) , Resize (band 205:LMU_Hyspex_2) , Resize (band 206:LMU_Hyspex_2) , Resize (band 207:LMU_Hyspex_2) , Resize (band 208:LMU_Hyspex_2) , Resize (band 209:LMU_Hyspex_2) , Resize (band 210:LMU_Hyspex_2) , Resize (band 211:LMU_Hyspex_2) , Resize (band 212:LMU_Hyspex_2) , Resize (band 213:LMU_Hyspex_2) , Resize (band 214:LMU_Hyspex_2) , Resize (band 215:LMU_Hyspex_2) , Resize (band 216:LMU_Hyspex_2) , Resize (band 217:LMU_Hyspex_2) , Resize (band 218:LMU_Hyspex_2) , Resize (band 219:LMU_Hyspex_2) , Resize (band 220:LMU_Hyspex_2) , Resize (band 221:LMU_Hyspex_2) , Resize (band 222:LMU_Hyspex_2) , Resize (band 223:LMU_Hyspex_2) , Resize (band 224:LMU_Hyspex_2) , Resize (band 225:LMU_Hyspex_2) , Resize (band 226:LMU_Hyspex_2) , Resize (band 227:LMU_Hyspex_2) , Resize (band 228:LMU_Hyspex_2) , Resize (band 229:LMU_Hyspex_2) , Resize (band 230:LMU_Hyspex_2) , Resize (band 231:LMU_Hyspex_2) , Resize (band 232:LMU_Hyspex_2) , Resize (band 233:LMU_Hyspex_2) , Resize (band 234:LMU_Hyspex_2) , Resize (band 235:LMU_Hyspex_2) , Resize (band 236:LMU_Hyspex_2) , Resize (band 237:LMU_Hyspex_2) , Resize (band 238:LMU_Hyspex_2) , Resize (band 239:LMU_Hyspex_2) , Resize (band 240:LMU_Hyspex_2) , Resize (band 241:LMU_Hyspex_2) , Resize (band 242:LMU_Hyspex_2) , Resize (band 243:LMU_Hyspex_2) , Resize (band 244:LMU_Hyspex_2) , Resize (band 245:LMU_Hyspex_2) , Resize (band 246:LMU_Hyspex_2) , Resize (band 247:LMU_Hyspex_2) , Resize (band 248:LMU_Hyspex_2) , Resize (band 249:LMU_Hyspex_2) , Resize (band 250:LMU_Hyspex_2) , Resize (band 251:LMU_Hyspex_2) , Resize (band 252:LMU_Hyspex_2) , Resize (band 253:LMU_Hyspex_2) , Resize (band 254:LMU_Hyspex_2) , Resize (band 255:LMU_Hyspex_2) , Resize (band 256:LMU_Hyspex_2) , Resize (band 257:LMU_Hyspex_2) , Resize (band 258:LMU_Hyspex_2) , Resize (band 259:LMU_Hyspex_2) , Resize (band 260:LMU_Hyspex_2) , Resize (band 261:LMU_Hyspex_2) , Resize (band 262:LMU_Hyspex_2) , Resize (band 263:LMU_Hyspex_2) , Resize (band 264:LMU_Hyspex_2) , Resize (band 265:LMU_Hyspex_2) , Resize (band 266:LMU_Hyspex_2) , Resize (band 267:LMU_Hyspex_2) , Resize (band 268:LMU_Hyspex_2) , Resize (band 269:LMU_Hyspex_2) , Resize (band 270:LMU_Hyspex_2) , Resize (band 271:LMU_Hyspex_2) , Resize (band 272:LMU_Hyspex_2) , Resize (band 273:LMU_Hyspex_2) , Resize (band 274:LMU_Hyspex_2) , Resize (band 275:LMU_Hyspex_2) , Resize (band 276:LMU_Hyspex_2) , Resize (band 277:LMU_Hyspex_2) , Resize (band 278:LMU_Hyspex_2) , Resize (band 279:LMU_Hyspex_2) , Resize (band 280:LMU_Hyspex_2) , Resize (band 281:LMU_Hyspex_2) , Resize (band 282:LMU_Hyspex_2) , Resize (band 283:LMU_Hyspex_2) , Resize (band 284:LMU_Hyspex_2) , Resize (band 285:LMU_Hyspex_2) , Resize (band 286:LMU_Hyspex_2) , Resize (band 287:LMU_Hyspex_2) , Resize (band 288:LMU_Hyspex_2) , Resize (band 289:LMU_Hyspex_2) , Resize (band 290:LMU_Hyspex_2) , Resize (band 291:LMU_Hyspex_2) , Resize (band 292:LMU_Hyspex_2) , Resize (band 293:LMU_Hyspex_2) , Resize (band 294:LMU_Hyspex_2) , Resize (band 295:LMU_Hyspex_2) , Resize (band 296:LMU_Hyspex_2) , Resize (band 297:LMU_Hyspex_2) , Resize (band 298:LMU_Hyspex_2) , Resize (band 299:LMU_Hyspex_2) , Resize (band 300:LMU_Hyspex_2) , Resize (band 301:LMU_Hyspex_2) , Resize (band 302:LMU_Hyspex_2) , Resize (band 303:LMU_Hyspex_2) , Resize (band 304:LMU_Hyspex_2) , Resize (band 305:LMU_Hyspex_2) , Resize (band 306:LMU_Hyspex_2) , Resize (band 307:LMU_Hyspex_2) , Resize (band 308:LMU_Hyspex_2) , Resize (band 309:LMU_Hyspex_2) , Resize (band 310:LMU_Hyspex_2) , Resize (band 311:LMU_Hyspex_2) , Resize (band 312:LMU_Hyspex_2) , Resize (band 313:LMU_Hyspex_2) , Resize (band 314:LMU_Hyspex_2) , Resize (band 315:LMU_Hyspex_2) , Resize (band 316:LMU_Hyspex_2) , Resize (band 317:LMU_Hyspex_2) , Resize (band 318:LMU_Hyspex_2) , Resize (band 319:LMU_Hyspex_2) , Resize (band 320:LMU_Hyspex_2) , Resize (band 321:LMU_Hyspex_2) , Resize (band 322:LMU_Hyspex_2) , Resize (band 323:LMU_Hyspex_2) , Resize (band 324:LMU_Hyspex_2) , Resize (band 325:LMU_Hyspex_2) }
wavelength = { 417.459991 , 424.709991 , 431.959991 , 439.200012 , 446.450012 , 453.690002 , 460.940002 , 468.190002 , 475.429993 , 482.679993 , 489.929993 , 497.179993 , 504.420013 , 511.670013 , 518.909973 , 526.159973 , 533.409973 , 540.650024 , 547.900024 , 555.150024 , 562.390015 , 569.640015 , 576.890015 , 584.130005 , 591.380005 , 598.630005 , 605.869995 , 613.119995 , 620.369995 , 627.609985 , 634.859985 , 642.109985 , 649.349976 , 656.599976 , 663.849976 , 671.090027 , 678.340027 , 685.590027 , 692.830017 , 700.080017 , 707.330017 , 714.570007 , 721.820007 , 729.070007 , 736.309998 , 743.559998 , 750.809998 , 758.049988 , 765.299988 , 772.549988 , 779.789978 , 787.039978 , 794.289978 , 801.530029 , 808.780029 , 816.030029 , 823.270020 , 830.520020 , 837.770020 , 845.010010 , 852.260010 , 859.510010 , 866.750000 , 874.000000 , 881.250000 , 888.489990 , 895.739990 , 902.989990 , 910.229980 , 917.479980 , 924.729980 , 931.969971 , 939.219971 , 946.469971 , 953.710022 , 960.960022 , 968.210022 , 975.450012 , 982.700012 , 989.950012 , 991.770020 , 997.770020 , 1003.760010 , 1009.760010 , 1015.750000 , 1021.750000 , 1027.739990 , 1033.739990 , 1039.729980 , 1045.729980 , 1051.719971 , 1057.719971 , 1063.709961 , 1069.709961 , 1075.699951 , 1081.699951 , 1087.689941 , 1093.689941 , 1099.680054 , 1105.680054 , 1111.670044 , 1117.670044 , 1123.660034 , 1129.660034 , 1135.650024 , 1141.650024 , 1147.640015 , 1153.640015 , 1159.640015 , 1165.630005 , 1171.630005 , 1177.619995 , 1183.619995 , 1189.609985 , 1195.609985 , 1201.599976 , 1207.599976 , 1213.589966 , 1219.589966 , 1225.579956 , 1231.579956 , 1237.569946 , 1243.569946 , 1249.560059 , 1255.560059 , 1261.550049 , 1267.550049 , 1273.540039 , 1279.540039 , 1285.530029 , 1291.530029 , 1297.520020 , 1303.520020 , 1309.510010 , 1315.510010 , 1321.500000 , 1327.500000 , 1333.489990 , 1339.489990 , 1345.479980 , 1351.479980 , 1357.469971 , 1363.469971 , 1369.459961 , 1375.459961 , 1381.449951 , 1387.449951 , 1393.439941 , 1399.439941 , 1405.430054 , 1411.430054 , 1417.420044 , 1423.420044 , 1429.410034 , 1435.410034 , 1441.400024 , 1447.400024 , 1453.390015 , 1459.390015 , 1465.380005 , 1471.380005 , 1477.369995 , 1483.369995 , 1489.359985 , 1495.359985 , 1501.349976 , 1507.349976 , 1513.339966 , 1519.339966 , 1525.329956 , 1531.329956 , 1537.319946 , 1543.319946 , 1549.310059 , 1555.310059 , 1561.300049 , 1567.300049 , 1573.290039 , 1579.290039 , 1585.280029 , 1591.280029 , 1597.270020 , 1603.270020 , 1609.260010 , 1615.260010 , 1621.250000 , 1627.250000 , 1633.239990 , 1639.239990 , 1645.229980 , 1651.229980 , 1657.219971 , 1663.219971 , 1669.209961 , 1675.209961 , 1681.199951 , 1687.199951 , 1693.189941 , 1699.189941 , 1705.180054 , 1711.180054 , 1717.170044 , 1723.170044 , 1729.160034 , 1735.160034 , 1741.150024 , 1747.150024 , 1753.150024 , 1759.140015 , 1765.140015 , 1771.130005 , 1777.130005 , 1783.119995 , 1789.119995 , 1795.109985 , 1801.109985 , 1807.099976 , 1813.099976 , 1819.089966 , 1825.089966 , 1831.079956 , 1837.079956 , 1843.069946 , 1849.069946 , 1855.060059 , 1861.060059 , 1867.050049 , 1873.050049 , 1879.040039 , 1885.040039 , 1891.030029 , 1897.030029 , 1903.020020 , 1909.020020 , 1915.010010 , 1921.010010 , 1927.000000 , 1933.000000 , 1938.989990 , 1944.989990 , 1950.979980 , 1956.979980 , 1962.969971 , 1968.969971 , 1974.959961 , 1980.959961 , 1986.949951 , 1992.949951 , 1998.939941 , 2004.939941 , 2010.930054 , 2016.930054 , 2022.920044 , 2028.920044 , 2034.910034 , 2040.910034 , 2046.900024 , 2052.899902 , 2058.889893 , 2064.889893 , 2070.879883 , 2076.879883 , 2082.870117 , 2088.870117 , 2094.860107 , 2100.860107 , 2106.850098 , 2112.850098 , 2118.840088 , 2124.840088 , 2130.830078 , 2136.830078 , 2142.820068 , 2148.820068 , 2154.810059 , 2160.810059 , 2166.800049 , 2172.800049 , 2178.790039 , 2184.790039 , 2190.780029 , 2196.780029 , 2202.770020 , 2208.770020 , 2214.760010 , 2220.760010 , 2226.750000 , 2232.750000 , 2238.739990 , 2244.739990 , 2250.729980 , 2256.729980 , 2262.719971 , 2268.719971 , 2274.709961 , 2280.709961 , 2286.699951 , 2292.699951 , 2298.689941 , 2304.689941 , 2310.679932 , 2316.679932 , 2322.669922 , 2328.669922 , 2334.659912 , 2340.659912 , 2346.659912 , 2352.649902 , 2358.649902 , 2364.639893 , 2370.639893 , 2376.629883 , 2382.629883 , 2388.620117 , 2394.620117 , 2400.610107 , 2406.610107 , 2412.600098 , 2418.600098 , 2424.590088 , 2430.590088 , 2436.580078 , 2442.580078 , 2448.570068 , 2454.570068 }
ENVI
description = {
/home/gfz-fe/scheffler/python/gms_preprocessing/tests/data/spechomo_inputs/refcube__Landsat-5__TM__nclust50__nsamp100.bsq}
samples = 16
lines = 100
bands = 6
header offset = 0
file type = ENVI Standard
data type = 4
interleave = bsq
byte order = 0
map info = {Arbitrary, 1, 1, 0, 0, 1, 1, 0, North}
band names = {
Band 1,
Band 2,
Band 3,
Band 4,
Band 5,
Band 6}
{
"satellite": "Landsat-5",
"sensor": "TM",
"filepath": "/home/gfz-fe/scheffler/temp/SPECHOM_py/CUBE/refcube__Landsat-5__TM__nclust50__nsamp20000.bsq",
"n_signatures": 100,
"n_images": 16,
"col_imName_dict": {
"0": "Alps_snow_rocks_Apex",
"1": "Balaton_lake_clouds_rural_Apex",
"2": "Balaton_lake_clouds_rural_Apex_CLOUDfree",
"3": "Bavaria_farmland_LMU_Hyspex",
"4": "Bruessels_urban_forest",
"5": "Coral_French_Frigate_Shoals_Hawaii_Aviris",
"6": "Coral_French_Frigate_Shoals_Hawaii_Aviris_CLOUDfree",
"7": "Costa_rica_Hymap",
"8": "Costa_rica_Hymap_CLOUDfree",
"9": "Goodsprings_nevada_desert_Aviris",
"10": "Mullewa_Hymap",
"11": "Plumas_national_forest_CA_Aviris",
"12": "SouthAfrica_Arundale_bbr3_selsmo_Hymap",
"13": "canada_arctic_derek_Hymap",
"14": "isabena_spain_Hyspex",
"15": "karlsruhe_forest_urban_Hymap"
},
"LayerBandsAssignment": [
"1",
"2",
"3",
"4",
"5",
"7"
]
}
\ No newline at end of file
ENVI
description = {
/home/gfz-fe/scheffler/python/gms_preprocessing/tests/data/spechomo_inputs/refcube__Landsat-8__OLI_TIRS__nclust50__nsamp100.bsq}
samples = 16
lines = 100
bands = 9
header offset = 0
file type = ENVI Standard
data type = 4
interleave = bsq
byte order = 0
map info = {Arbitrary, 1, 1, 0, 0, 1, 1, 0, North}
band names = {
Band 1,
Band 2,
Band 3,
Band 4,
Band 5,
Band 6,
Band 7,
Band 8,
Band 9}
{
"satellite": "Landsat-8",
"sensor": "OLI_TIRS",
"filepath": "/home/gfz-fe/scheffler/temp/SPECHOM_py/CUBE/refcube__Landsat-8__OLI_TIRS__nclust50__nsamp20000.bsq",
"n_signatures": 100,
"n_images": 16,
"col_imName_dict": {
"0": "Alps_snow_rocks_Apex",
"1": "Balaton_lake_clouds_rural_Apex",
"2": "Balaton_lake_clouds_rural_Apex_CLOUDfree",
"3": "Bavaria_farmland_LMU_Hyspex",
"4": "Bruessels_urban_forest",
"5": "Coral_French_Frigate_Shoals_Hawaii_Aviris",
"6": "Coral_French_Frigate_Shoals_Hawaii_Aviris_CLOUDfree",
"7": "Costa_rica_Hymap",
"8": "Costa_rica_Hymap_CLOUDfree",
"9": "Goodsprings_nevada_desert_Aviris",
"10": "Mullewa_Hymap",
"11": "Plumas_national_forest_CA_Aviris",
"12": "SouthAfrica_Arundale_bbr3_selsmo_Hymap",
"13": "canada_arctic_derek_Hymap",
"14": "isabena_spain_Hyspex",
"15": "karlsruhe_forest_urban_Hymap"
},
"LayerBandsAssignment": [
"1",
"2",
"3",
"4",
"5",
"9",
"6",
"7",
"8"
]
}
\ No newline at end of file
......@@ -12,10 +12,13 @@ import unittest
import os
from geoarray import GeoArray
from gms_preprocessing import __file__
from gms_preprocessing import __file__, set_config
from gms_preprocessing.algorithms.cloud_masking import FMASK_Runner_Landsat, FMASK_Runner_Sentinel2, FmaskWarning
from gms_preprocessing.misc.exception_handler import ignore_warning
from . import db_host
rootpath_testdata = os.path.join(os.path.dirname(__file__), '../tests/data/')
testdata = dict(
Landsat5_collections_data=os.path.join(rootpath_testdata, 'archive_data', 'Landsat-5', 'TM',
......@@ -39,6 +42,9 @@ testdata = dict(
class Test_FMASK_Runner_Landsat(unittest.TestCase):
def setUp(self):
set_config(job_ID=26186196, db_host=db_host, reset_status=True)
@ignore_warning(FmaskWarning)
def test_Landsat5_collections_data(self):
# os.environ['RIOS_DFLT_DRIVER'] = 'VRT'
......@@ -77,6 +83,9 @@ class Test_FMASK_Runner_Landsat(unittest.TestCase):
class Test_FMASK_Runner_Sentinel2(unittest.TestCase):
def setUp(self):
set_config(job_ID=26186196, db_host=db_host, reset_status=True)
@ignore_warning(FmaskWarning)
def test_Sentinel2A_old_style(self):
# FIXME scene ID or granule ID must be provided in case of multiple files within source archive
......
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_kmeans
-----------
Tests for gms_preprocessing.algorithms.L2B_P.KMeansRSImage
"""
import unittest
import os
import numpy as np
from sklearn.cluster import k_means_
from geoarray import GeoArray # noqa E402 module level import not at top of file
from gms_preprocessing import __file__ # noqa E402 module level import not at top of file
from gms_preprocessing.options.config import set_config # noqa E402 module level import not at top of file
from gms_preprocessing.algorithms.L2B_P import KMeansRSImage # noqa E402 module level import not at top of file
from . import db_host
testdata = os.path.join(os.path.dirname(__file__),
'../tests/data/spechomo_inputs/Bavaria_farmland_LMU_Hyspex_subset.bsq')
class Test_KMeansRSImage(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.SpectralResampler"""
@classmethod
def setUpClass(cls):
# Testjob Landsat-8
cls.config = set_config(job_ID=26186196, db_host=db_host, reset_status=True)
cls.geoArr = GeoArray(testdata)
cls.geoArr.to_mem()
cls.kmeans = KMeansRSImage(cls.geoArr, n_clusters=10)
os.environ['MPLBACKEND'] = 'Template' # disables matplotlib figure popups # NOTE: import geoarray sets 'Agg'
def test_compute_clusters(self):
self.kmeans.compute_clusters()
self.assertIsInstance(self.kmeans.clusters, k_means_.KMeans)
def test_apply_clusters(self):
labels = self.kmeans.apply_clusters(self.geoArr)
self.assertIsInstance(labels, np.ndarray)
self.assertTrue(labels.size == self.geoArr.rows * self.geoArr.cols)
def test_get_random_spectra_from_each_cluster(self):
random_samples = self.kmeans.get_random_spectra_from_each_cluster()
self.assertIsInstance(random_samples, dict)
for cluster_label in range(self.kmeans.n_clusters):
self.assertIn(cluster_label, random_samples)
def test_plot_cluster_centers(self):
self.kmeans.plot_cluster_centers()
def test_plot_cluster_histogram(self):
self.kmeans.plot_cluster_histogram()
def test_plot_clustered_image(self):
self.kmeans.plot_clustered_image()
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
test_spechomo_classifier
------------------------
Tests for gms_preprocessing.algorithms.L2B_P.SpecHomo_Classifier
"""
import unittest
import os
import tempfile
import numpy as np
import dill
from geoarray import GeoArray
import gms_preprocessing
from gms_preprocessing import set_config
from gms_preprocessing.algorithms.L2B_P import ReferenceCube_Generator
from gms_preprocessing.algorithms.L2B_P import RefCube
from gms_preprocessing.algorithms.L2B_P import SpectralHomogenizer
from gms_preprocessing.algorithms.L2B_P import ClusterClassifier_Generator
from gms_preprocessing.model.gms_object import GMS_identifier
from . import db_host
hyspec_data = os.path.join(gms_preprocessing.__path__[0],
'../tests/data/spechomo_inputs/Bavaria_farmland_LMU_Hyspex_subset.bsq')
refcube_l8 = os.path.join(gms_preprocessing.__path__[0],
'../tests/data/spechomo_inputs/refcube__Landsat-8__OLI_TIRS__nclust50__nsamp100.bsq')
refcube_l5 = os.path.join(gms_preprocessing.__path__[0],
'../tests/data/spechomo_inputs/refcube__Landsat-5__TM__nclust50__nsamp100.bsq')
class Test_ReferenceCube_Generator(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.SpecHomo_Classifier"""
@classmethod
def setUpClass(cls):
# Testjob Landsat-8
cls.config = set_config(job_ID=26186196, db_host=db_host, reset_status=True, is_test=True)
cls.tmpOutdir = tempfile.TemporaryDirectory()
cls.testIms = [hyspec_data, hyspec_data, ]
cls.tgt_sat_sen_list = [
('Landsat-8', 'OLI_TIRS'),
('Landsat-7', 'ETM+'),
('Landsat-5', 'TM'),
('Sentinel-2A', 'MSI'),
# ('Terra', 'ASTER'), # currently does not work
('SPOT-4', 'HRVIR1'),
('SPOT-4', 'HRVIR2'),
('SPOT-5', 'HRG1'),
('SPOT-5', 'HRG2'),
('RapidEye-5', 'MSI')
]
cls.n_clusters = 5
cls.tgt_n_samples = 500
cls.SHC = ReferenceCube_Generator(cls.testIms, dir_refcubes=cls.tmpOutdir.name,
tgt_sat_sen_list=cls.tgt_sat_sen_list,
n_clusters=cls.n_clusters, tgt_n_samples=cls.tgt_n_samples, v=False)
@classmethod
def tearDownClass(cls):
cls.tmpOutdir.cleanup()
def test_cluster_image_and_get_uniform_samples(self):
src_im = self.SHC.ims_ref[0]
unif_random_spectra = self.SHC.cluster_image_and_get_uniform_spectra(src_im)
self.assertIsInstance(unif_random_spectra, np.ndarray)
self.assertEqual(unif_random_spectra.shape, (self.tgt_n_samples, GeoArray(src_im).bands))
def test_resample_spectra(self):
src_im = GeoArray(self.SHC.ims_ref[0])
unif_random_spectra = self.SHC.cluster_image_and_get_uniform_spectra(src_im)
from gms_preprocessing.io.input_reader import SRF
tgt_srf = SRF(GMS_identifier(satellite='Sentinel-2A', sensor='MSI', subsystem='', image_type='RSD',
dataset_ID=-9999, proc_level='L1A', logger=None))
unif_random_spectra_rsp = \
self.SHC.resample_spectra(unif_random_spectra,
src_cwl=np.array(src_im.meta.band_meta['wavelength'], dtype=np.float).flatten(),
tgt_srf=tgt_srf)
self.assertIsInstance(unif_random_spectra_rsp, np.ndarray)
self.assertEqual(unif_random_spectra_rsp.shape, (self.tgt_n_samples, len(tgt_srf.bands)))
def test_generate_reference_cube(self):
refcubes = self.SHC.generate_reference_cubes()
self.assertIsInstance(refcubes, dict)
self.assertIsInstance(refcubes[('Landsat-8', 'OLI_TIRS')], RefCube)
self.assertEqual(refcubes[('Landsat-8', 'OLI_TIRS')].data.shape, (self.tgt_n_samples, len(self.testIms), 9))
self.assertNotEqual(len(os.listdir(self.tmpOutdir.name)), 0)
# @unittest.SkipTest
# def test_multiprocessing(self):
# SHC = ReferenceCube_Generator_OLD([testdata, testdata, ], v=False, CPUs=1)
# ref_cube_sp = SHC.generate_reference_cube('Landsat-8', 'OLI_TIRS', n_clusters=10, tgt_n_samples=1000)
#
# SHC = ReferenceCube_Generator_OLD([testdata, testdata, ], v=False, CPUs=None)
# ref_cube_mp = SHC.generate_reference_cube('Landsat-8', 'OLI_TIRS', n_clusters=10, tgt_n_samples=1000)
#
# self.assertTrue(np.any(ref_cube_sp), msg='Singleprocessing result is empty.')
# self.assertTrue(np.any(ref_cube_mp), msg='Multiprocessing result is empty.')
class Test_ClusterClassifier_Generator(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.Classifier_Generator"""
@classmethod
def setUpClass(cls):
cls.config = set_config(job_ID=26186196, db_host=db_host, reset_status=True, is_test=True)
cls.tmpOutdir = tempfile.TemporaryDirectory()
@classmethod
def tearDownClass(cls):
cls.tmpOutdir.cleanup()
def test_init_from_path_strings(self):
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
self.assertIsInstance(CCG, ClusterClassifier_Generator)
def test_init_from_RefCubes(self):
RC = RefCube(refcube_l8)
CCG = ClusterClassifier_Generator([RC, RC])
self.assertIsInstance(CCG, ClusterClassifier_Generator)
def test_cluster_refcube_spectra(self):
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
CCG.cluster_refcube_spectra(cube2cluster=refcube_l8, n_clusters=5)
def test_create_classifiers_LR(self):
"""Test creation of linear regression classifiers."""
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
CCG.create_classifiers(outDir=self.tmpOutdir.name, method='LR', n_clusters=5)
outpath_cls = os.path.join(self.tmpOutdir.name, 'LR_clust5__Landsat-8__OLI_TIRS.dill')
self.assertTrue(os.path.exists(outpath_cls))
with open(outpath_cls, 'rb') as inF:
undilled = dill.load(inF)
self.assertIsInstance(undilled, dict)
self.assertTrue(bool(undilled), msg='Generated classifier collection is empty.')
def test_create_classifiers_RR(self):
"""Test creation of ridge regression classifiers."""
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
CCG.create_classifiers(outDir=self.tmpOutdir.name, method='RR', n_clusters=5)
outpath_cls = os.path.join(self.tmpOutdir.name, 'RR_alpha1.0_clust5__Landsat-8__OLI_TIRS.dill')
self.assertTrue(os.path.exists(outpath_cls))
with open(outpath_cls, 'rb') as inF:
undilled = dill.load(inF)
self.assertIsInstance(undilled, dict)
self.assertTrue(bool(undilled), msg='Generated classifier collection is empty.')
def test_create_classifiers_QR(self):
"""Test creation of quadratic regression classifiers."""
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
CCG.create_classifiers(outDir=self.tmpOutdir.name, method='QR', n_clusters=5)
outpath_cls = os.path.join(self.tmpOutdir.name, 'QR_clust5__Landsat-8__OLI_TIRS.dill')
self.assertTrue(os.path.exists(outpath_cls))
with open(outpath_cls, 'rb') as inF:
undilled = dill.load(inF)
self.assertIsInstance(undilled, dict)
self.assertTrue(bool(undilled), msg='Generated classifier collection is empty.')
def test_create_classifiers_RFR(self):
"""Test creation of random forest regression classifiers."""
CCG = ClusterClassifier_Generator([refcube_l8, refcube_l5])
CCG.create_classifiers(outDir=self.tmpOutdir.name, method='RFR', n_clusters=1,
**dict(n_jobs=-1, n_estimators=20, max_depth=10))
outpath_cls = os.path.join(self.tmpOutdir.name,
'RFR_trees%d_clust1__Landsat-8__OLI_TIRS.dill' % self.config.spechomo_rfr_n_trees)
self.assertTrue(os.path.exists(outpath_cls))
with open(outpath_cls, 'rb') as inF:
undilled = dill.load(inF)
self.assertIsInstance(undilled, dict)
self.assertTrue(bool(undilled), msg='Generated classifier collection is empty.')
class Test_SpectralHomogenizer(unittest.TestCase):
"""Tests class for gms_preprocessing.algorithms.L2B_P.Test_SpectralHomogenizer"""
@classmethod
def setUpClass(cls):
# Testjob Landsat-8
cfg = set_config(job_ID=26186196, db_host=db_host, reset_status=True, is_test=True)
cls.SpH = SpectralHomogenizer(classifier_rootDir=cfg.path_spechomo_classif)
cls.testArr_L8 = GeoArray(np.random.randint(1, 10000, (50, 50, 7), dtype=np.int16)) # no band 9, no pan
# cls.testArr_L8 = GeoArray('/home/gfz-fe/scheffler/temp/'
# 'Landsat-8__OLI_TIRS__LC81940242014072LGN00_L2B__250x250.bsq') # no pan
# cls.testArr_L8 = GeoArray('/home/gfz-fe/scheffler/temp/'
# 'Landsat-8__OLI_TIRS__LC81940242014072LGN00_L2B.bsq') # no pan
# cls.testArr_L8 = GeoArray('/home/gfz-fe/scheffler/temp/'
# 'clusterhomo_sourceL8_full_withoutB9.bsq') # no pan, no cirrus
cls.cwl_L8 = [442.98, 482.59, 561.33, 654.61, 864.57, 1609.09, 2201.25]
# cls.cwl_L8 = [442.98, 482.59, 561.33, 654.61, 864.57, 1373.48, 1609.09, 2201.25]
def test_interpolate_cube_linear(self):
outarr = self.SpH.interpolate_cube(self.testArr_L8, self.cwl_L8, [500., 700., 1300.], kind='linear')
self.assertIsInstance(outarr, np.ndarray)
self.assertEqual(outarr.shape, (50, 50, 3))
self.assertEqual(outarr.dtype, np.int16)
def test_interpolate_cube_quadratic(self):
outarr = self.SpH.interpolate_cube(self.testArr_L8, self.cwl_L8, [500., 700., 1300.], kind='quadratic')
self.assertIsInstance(outarr, np.ndarray)
self.assertEqual(outarr.shape, (50, 50, 3))
self.assertEqual(outarr.dtype, np.int16)
def test_predict_by_machine_learner__LR_L8_S2(self):
"""Test linear regression from Landsat-8 to Sentinel-2A."""
predarr, errors = self.SpH.predict_by_machine_learner(
self.testArr_L8,
method='LR', n_clusters=1,
src_satellite='Landsat-8', src_sensor='OLI_TIRS',
src_LBA=['1', '2', '3', '4', '5', '6', '7'],
tgt_satellite='Sentinel-2A', tgt_sensor='MSI',
tgt_LBA=['1', '2', '3', '4', '5', '6', '7', '8', '8A', '9', '10', '11', '12'],
compute_errors=True
)
self.assertIsInstance(predarr, GeoArray)
self.assertEqual(predarr.shape, (50, 50, 13))
self.assertEqual(predarr.dtype, np.int16)
self.assertIsInstance(errors, np.ndarray)
self.assertEqual(errors.shape, (50, 50, 13))
self.assertEqual(errors.dtype, np.int16)
def test_predict_by_machine_learner__RR_L8_S2(self):
"""Test ridge regression from Landsat-8 to Sentinel-2A."""
predarr, errors = self.SpH.predict_by_machine_learner(
self.testArr_L8,
method='RR', n_clusters=1,
src_satellite='Landsat-8', src_sensor='OLI_TIRS',
src_LBA=['1', '2', '3', '4', '5', '6', '7'],
tgt_satellite='Sentinel-2A', tgt_sensor='MSI',
tgt_LBA=['1', '2', '3', '4', '5', '6', '7', '8', '8A', '9', '10', '11', '12'],
compute_errors=True)
self.assertIsInstance(predarr, GeoArray)
self.assertEqual(predarr.shape, (50, 50, 13))
self.assertEqual(predarr.dtype, np.int16)
self.assertIsInstance(errors, np.ndarray)
self.assertEqual(errors.shape, (50, 50, 13))
self.assertEqual(errors.dtype, np.int16)
def test_predict_by_machine_learner__QR_L8_S2(self):
"""Test quadratic regression from Landsat-8 to Sentinel-2A."""
predarr, errors = self.SpH.predict_by_machine_learner(
self.testArr_L8,
method='QR', n_clusters=1,
src_satellite='Landsat-8', src_sensor='OLI_TIRS',