Source code for cellcomplex.property_topomesh.utils.array_tools
# -*- coding: utf-8 -*-
# -*- python -*-
#
# PropertyTopomesh
#
# Copyright 2015-2016 INRIA - CIRAD - INRA
#
# File author(s): Guillaume Cerutti <guillaume.cerutti@inria.fr>
#
# File contributor(s): Guillaume Cerutti <guillaume.cerutti@inria.fr>
#
# Distributed under the Cecill-C License.
# See accompanying file LICENSE.txt or copy at
# http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.html
#
# OpenaleaLab Website : http://virtualplants.github.io/
#
###############################################################################
import numpy as np
from scipy import ndimage as nd
[docs]def array_unique(array,return_index=False):
_,unique_rows = np.unique(np.ascontiguousarray(array).view(np.dtype((np.void,array.dtype.itemsize * array.shape[1]))),return_index=True)
if return_index:
return array[unique_rows],unique_rows
else:
return array[unique_rows]
[docs]def where_list(array,values):
mask = nd.sum(np.ones_like(values),values,index=array)
return np.where(mask>0)
[docs]def array_difference(array,subarray):
import numpy as np
return np.array(list(set(array).difference(set(subarray))))
[docs]def weighted_percentile(values, percentiles, sample_weight=None, values_sorted=False):
values = np.array(values)
percentiles = np.array(percentiles)
if sample_weight is None:
sample_weight = np.ones(len(values))
sample_weight = np.array(sample_weight)
assert np.all(percentiles >= 0) and np.all(percentiles <= 100), 'percentiles should be in [0, 1]'
if not values_sorted:
sorter = np.argsort(values)
values = values[sorter]
sample_weight = sample_weight[sorter]
weighted_percentiles = 100.*(np.cumsum(sample_weight) - 0.5*sample_weight)/np.sum(sample_weight)
return np.interp(percentiles, weighted_percentiles, values)