Skip to content

Commit

Permalink
Added minimal version of geoarray.
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Scheffler <[email protected]>
  • Loading branch information
danschef committed Nov 2, 2020
1 parent 27a1613 commit 10ca3f5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ test_spechomo:
script:
- source /root/miniconda3/bin/activate ci_env
- conda install -y -c conda-forge "scikit-learn=0.23.2" # FIXME remove as soon as docker container is rebuilt
- pip install 'geoarray>=0.10.4' # FIXME remove as soon as docker container is rebuilt

# run nosetests
- make nosetests # test are called here
Expand Down
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ History
(causes faulty predictions).
* Bugfix for only choosing 25 spectra in classifier creation in case the maximum angle threshold is automatically
set to 0 because there are many well matching spectra.
* Added minimal version of geoarray.


0.8.2 (2020-10-12)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

req = [
'dill',
'geoarray',
'geoarray>=0.10.4',
'matplotlib',
'natsort',
'nested_dict',
Expand Down
29 changes: 23 additions & 6 deletions spechomo/prediction.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,11 @@ def interpolate_cube(self, arrcube, source_CWLs, target_CWLs, kind='linear'):
np.max(outarr) <= np.iinfo(np.int16).max:

outarr = outarr.astype(np.int16)

elif np.min(outarr) >= np.iinfo(np.int32).min and np.max(outarr) <= np.iinfo(np.int32).max:

outarr = outarr.astype(np.int32)

else:
raise TypeError('The interpolated data cube cannot be cast into a 16- or 32-bit integer array.')

Expand Down Expand Up @@ -405,7 +408,8 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap
if self.classif_alg == 'RF':
train_spectra = np.vstack([classifier.MLdict[clust].cluster_sample_spectra
for clust in range(classifier.n_clusters)])
train_labels = list(np.hstack([[i] * 100 for i in range(classifier.n_clusters)]))
train_labels = list(np.hstack([[i] * 100
for i in range(classifier.n_clusters)]))
else:
train_spectra = classifier.cluster_centers
train_labels = classifier.cluster_pixVals
Expand All @@ -424,15 +428,19 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap
% time.strftime("%H:%M:%S", time.gmtime(time.time() - t0)))

else:
self.classif_map = GeoArray(np.full((image.rows, image.cols), classifier.cluster_pixVals[0], np.int16),
self.classif_map = GeoArray(np.full((image.rows,
image.cols),
classifier.cluster_pixVals[0],
np.int16),
nodata=cmap_nodataVal)

# overwrite all pixels where the input image contains nodata in ANY band
# (would lead to faulty predictions due to multivariate prediction algorithms)
if in_nodataVal is not None and cmap_nodataVal is not None:
self.classif_map[np.any(image[:] == image.nodata, axis=2)] = cmap_nodataVal

self.distance_metrics = np.zeros_like(self.classif_map, np.float32)
self.distance_metrics = np.zeros_like(self.classif_map,
np.float32)

####################
# apply prediction #
Expand All @@ -446,9 +454,17 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap
# NOTE: prediction is applied in 1000 x 1000 tiles to save memory (because classifier.predict returns float32)
t0 = time.time()
out_nodataVal = out_nodataVal if out_nodataVal is not None else image.nodata
image_predicted = GeoArray(np.empty((image.rows, image.cols, classifier.tgt_n_bands), dtype=image.dtype),
geotransform=image.gt, projection=image.prj, nodata=out_nodataVal,
bandnames=['B%s' % i if len(i) == 2 else 'B0%s' % i for i in classifier.tgt_LBA])
image_predicted = GeoArray(np.empty((image.rows,
image.cols,
classifier.tgt_n_bands),
dtype=image.dtype),
geotransform=image.gt,
projection=image.prj,
nodata=out_nodataVal,
bandnames=['B%s' % i
if len(i) == 2
else 'B0%s' % i
for i in classifier.tgt_LBA])

if classifier.n_clusters > 1 and\
self.classif_map.ndim > 2:
Expand All @@ -458,6 +474,7 @@ def predict(self, image, classifier, in_nodataVal=None, out_nodataVal=None, cmap
dist_norm = (self.distance_metrics - dist_min) /\
(dist_max - dist_min)
weights = 1 - dist_norm

else:
weights = None

Expand Down
2 changes: 1 addition & 1 deletion tests/gitlab_CI_docker/context/environment_spechomo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ channels: &id1
dependencies:
- python>=3.7.*
- pip
- geoarray>=0.9.0
- geoarray>=0.10.4
- matplotlib
- numpy
- pandas
Expand Down

0 comments on commit 10ca3f5

Please sign in to comment.