From eb2a4b022f023d36cdc6173172c7c53fa99b521a Mon Sep 17 00:00:00 2001 From: Karamaz0V1 Date: Sat, 30 May 2020 22:45:30 +0200 Subject: [PATCH] Add aps --- minigrida/descriptors/aps.py | 59 ++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 minigrida/descriptors/aps.py diff --git a/minigrida/descriptors/aps.py b/minigrida/descriptors/aps.py new file mode 100644 index 0000000..2c7aec3 --- /dev/null +++ b/minigrida/descriptors/aps.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python +# file aps.py +# author Florent Guiotte +# version 0.0 +# date 30 mai 2020 +"""Abstract + +doc. +""" + +import numpy as np +import sap +from sklearn.externals.joblib import Memory + +memory = Memory(location='cache/', verbose=0) + + +@memory.cache +def _attribute_profiles(*kwargs): + return sap.attribute_profiles(*kwargs) + + +def run(gt, rasters, coords, remove, attributes, adjacency='4', 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 += [_attribute_profiles(raster, attributes, adjacency, name, filtering)] + eap = sap.concatenate(eap) + + Xn = [' '.join((a['tree']['image_name'], + a['attribute'], + *[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) + Xn = rasters[0].keys() + + return X, y, groups, Xn