From eec4684e67ffe62103243e7d86578e13c8ce270c Mon Sep 17 00:00:00 2001 From: Karamaz0V1 Date: Sun, 31 May 2020 12:38:31 +0200 Subject: [PATCH] Add feature profiles --- minigrida/descriptors/fps.py | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 minigrida/descriptors/fps.py diff --git a/minigrida/descriptors/fps.py b/minigrida/descriptors/fps.py new file mode 100644 index 0000000..c2e7207 --- /dev/null +++ b/minigrida/descriptors/fps.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# file fps.py +# author Florent Guiotte +# version 0.0 +# date 31 mai 2020 +"""Abstract + +doc. +""" + +import numpy as np +import sap +from joblib import Memory + +memory = Memory(location='cache/', verbose=0) + + +@memory.cache +def _feature_profiles(*kwargs): + return sap.feature_profiles(*kwargs) + + +def run(gt, rasters, coords, remove, attributes, adjacency='4', + feature='same', filtering='direct'): + X = [] + y = [] + groups = [] + Xn = None + + for i, (gti, rastersi, coordsi) in enumerate(zip(gt, rasters, coords)): + # Compute EAP + eap = [] + for name, raster in rastersi.items(): + eap += [_feature_profiles(image=raster, + attribute=attributes, + adjacency=adjacency, + image_name=name, + out_feature=feature, + filtering_rule=filtering)] + eap = sap.concatenate(eap) + + Xn = [' '.join((a['tree']['image_name'], + a['attribute'], + 'feature', + a['out feature'], + *[str(v) for v in p.values()])) + for a in eap.description for p in a['profiles']] if not Xn else Xn + + # Create vectors + X_raw = np.moveaxis(np.array(list(eap.vectorize())), 0, -1) + y_raw = gti + + # Remove unwanted label X, y + lbl = np.ones_like(y_raw, dtype=np.bool) + for l in remove if remove else []: + lbl &= y_raw != l + + X += [X_raw[lbl]] + y += [y_raw[lbl]] + groups += [np.repeat(coordsi, lbl.sum())] + + X = np.concatenate(X) + y = np.concatenate(y) + groups = np.concatenate(groups) + + return X, y, groups, Xn