Source code for pydiana.tools.math_tools

import numpy as np
#########################
#                       #
#    Data Manipulation  #
#                       #
#########################

[docs] def sortmultivec(*vecs,lead:int=0): """ Function for sorting multiple vectors with respect to one of the vectors provided Parameters ---------- *vecs : all the vectors to be sorted lead : Integer with the index of the vector with respect to which make the sorting (starting from 0). Returns ------- Matrix with the sorted vectors as rows """ vecs = [list(v) for v in vecs] lenvec = len(vecs[lead]) for v in vecs: if len(v)!=lenvec: print("Incompatible dimensions") return None inds = np.array(vecs[lead]).argsort() vecs = [[v[idx] for idx in inds]for v in vecs] return vecs
[docs] def moving_average(x, w:int,keep_shape:bool=False): """ Perform moving average Parameters ---------- x : 1D array or list of numbers that need to be averaged w : number of points to consider in the moving average keep_shape : if true result array is extended of "w-1" values in order to return an array of the same size as x Returns ------- array with the moving average of x """ res= np.convolve(x, np.ones(w), 'valid') / w if keep_shape: res = np.concatenate([res,[res[-1] for _ in range(w-1)]]) return res
###################################################################################################
[docs] def calc_Chi2Mean(values,err_values): """ Function that compute the Chi2 of mean : sum_i ( (values[i]-mean)/err_value[i] ) / (len(values)-1) Parameters ---------- values (array-like) : array of values err_values (array-like) : array of errors on values Returns ---------- result (float) : value of Chi2 of mean """ values = np.array(values) err_values = np.array(err_values) N = len(values) if N!=len(err_values): raise Exception("values and err_values have not the same length, aborting...") mean = np.mean(values) a=[] for i in range(N): a.append((values[i]-mean)*(values[i]-mean)/(err_values[i]*err_values[i])) s = np.sum(a) result = s/(N-1) return result