spectra-gui/utils.py
2021-03-09 21:16:54 +01:00

42 lines
1.6 KiB
Python

import numpy as np
def ui8_clip(x, vmin=None, vmax=None):
vmin = vmin if vmin else np.nanmin(x)
vmax = vmax if vmax else np.nanmax(x)
ui8 = ((np.clip(x, vmin, vmax) - vmin) / (vmax - vmin) * 255).astype(np.uint8)
return np.ma.array(ui8, mask=np.isnan(x))
def hillshades(x):
hs = np.zeros_like(x)
hs[:-1,:-1] = x[:-1,:-1] - x[1:,1:]
return hs
from sap.spectra import get_bins
def spectrum2d(tree, x_attribute, y_attribute, x_count=100, y_count=100,
x_log=False, y_log=False, weighted=True, normalized=True,
node_mask=None):
x = tree.get_attribute(x_attribute)
y = tree.get_attribute(y_attribute)
bins = (get_bins(x, x_count, 'geo' if x_log else 'lin'),
get_bins(y, y_count, 'geo' if y_log else 'lin'))
weights = tree.get_attribute('area') if weighted else None
weights = weights / tree._image.size if normalized and weighted else weights
s, xedges, yedges = np.histogram2d(x[node_mask].ravel(), y[node_mask].ravel(),
bins=bins, density=None, weights=weights[node_mask].ravel())
return s, xedges, yedges, x_log, y_log
def pixel_to_node(tree, mask):
"""Compute the node mask from the pixel mask."""
node_mask = hg.accumulate_and_min_sequential(tree._tree,
np.ones(tree._tree.num_vertices(), dtype=np.uint8),
mask.ravel(),
hg.Accumulators.min).astype(np.bool)
return node_mask