Search is not available for this dataset
text
stringlengths
75
104k
def reduce(self, func, axis=(0,), keepdims=False): """ Reduce an array along an axis. Applies a commutative/associative function of two arguments cumulatively to all arrays along an axis. Array will be aligned so that the desired set of axes are in the keys, which may incur a swap. Parameters ---------- func : function Function of two arrays that returns a single array axis : tuple or int, optional, default=(0,) Axis or multiple axes to reduce along. Returns ------- BoltArraySpark """ from bolt.local.array import BoltArrayLocal from numpy import ndarray axis = tupleize(axis) swapped = self._align(axis) arr = swapped._rdd.values().treeReduce(func, depth=3) if keepdims: for i in axis: arr = expand_dims(arr, axis=i) if not isinstance(arr, ndarray): # the result of a reduce can also be a scalar return arr elif arr.shape == (1,): # ndarrays with single values in them should be converted into scalars return arr[0] return BoltArrayLocal(arr)
def _stat(self, axis=None, func=None, name=None, keepdims=False): """ Compute a statistic over an axis. Can provide either a function (for use in a reduce) or a name (for use by a stat counter). Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes func : function, optional, default=None Function for reduce, see BoltArraySpark.reduce name : str A named statistic, see StatCounter keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ if axis is None: axis = list(range(len(self.shape))) axis = tupleize(axis) if func and not name: return self.reduce(func, axis, keepdims) if name and not func: from bolt.local.array import BoltArrayLocal swapped = self._align(axis) def reducer(left, right): return left.combine(right) counter = swapped._rdd.values()\ .mapPartitions(lambda i: [StatCounter(values=i, stats=name)])\ .treeReduce(reducer, depth=3) arr = getattr(counter, name) if keepdims: for i in axis: arr = expand_dims(arr, axis=i) return BoltArrayLocal(arr).toscalar() else: raise ValueError('Must specify either a function or a statistic name.')
def mean(self, axis=None, keepdims=False): """ Return the mean of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ return self._stat(axis, name='mean', keepdims=keepdims)
def var(self, axis=None, keepdims=False): """ Return the variance of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ return self._stat(axis, name='variance', keepdims=keepdims)
def std(self, axis=None, keepdims=False): """ Return the standard deviation of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ return self._stat(axis, name='stdev', keepdims=keepdims)
def sum(self, axis=None, keepdims=False): """ Return the sum of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ from operator import add return self._stat(axis, func=add, keepdims=keepdims)
def max(self, axis=None, keepdims=False): """ Return the maximum of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ from numpy import maximum return self._stat(axis, func=maximum, keepdims=keepdims)
def min(self, axis=None, keepdims=False): """ Return the minimum of the array over the given axis. Parameters ---------- axis : tuple or int, optional, default=None Axis to compute statistic over, if None will compute over all axes keepdims : boolean, optional, default=False Keep axis remaining after operation with size 1. """ from numpy import minimum return self._stat(axis, func=minimum, keepdims=keepdims)
def concatenate(self, arry, axis=0): """ Join this array with another array. Paramters --------- arry : ndarray, BoltArrayLocal, or BoltArraySpark Another array to concatenate with axis : int, optional, default=0 The axis along which arrays will be joined. Returns ------- BoltArraySpark """ if isinstance(arry, ndarray): from bolt.spark.construct import ConstructSpark arry = ConstructSpark.array(arry, self._rdd.context, axis=range(0, self.split)) else: if not isinstance(arry, BoltArraySpark): raise ValueError("other must be local array or spark array, got %s" % type(arry)) if not all([x == y if not i == axis else True for i, (x, y) in enumerate(zip(self.shape, arry.shape))]): raise ValueError("all the input array dimensions except for " "the concatenation axis must match exactly") if not self.split == arry.split: raise NotImplementedError("two arrays must have the same split ") if axis < self.split: shape = self.keys.shape def key_func(key): key = list(key) key[axis] += shape[axis] return tuple(key) rdd = self._rdd.union(arry._rdd.map(lambda kv: (key_func(kv[0]), kv[1]))) else: from numpy import concatenate as npconcatenate shift = axis - self.split rdd = self._rdd.join(arry._rdd).map(lambda kv: (kv[0], npconcatenate(kv[1], axis=shift))) shape = tuple([x + y if i == axis else x for i, (x, y) in enumerate(zip(self.shape, arry.shape))]) return self._constructor(rdd, shape=shape, ordered=False).__finalize__(self)
def _getbasic(self, index): """ Basic indexing (for slices or ints). """ key_slices = index[0:self.split] value_slices = index[self.split:] def key_check(key): def inrange(k, s): if s.step > 0: return s.start <= k < s.stop else: return s.stop < k <= s.start def check(k, s): return inrange(k, s) and mod(k - s.start, s.step) == 0 out = [check(k, s) for k, s in zip(key, key_slices)] return all(out) def key_func(key): return tuple([(k - s.start)/s.step for k, s in zip(key, key_slices)]) filtered = self._rdd.filter(lambda kv: key_check(kv[0])) if self._split == self.ndim: rdd = filtered.map(lambda kv: (key_func(kv[0]), kv[1])) else: # handle use of use slice.stop = -1 for a special case (see utils.slicify) value_slices = [s if s.stop != -1 else slice(s.start, None, s.step) for s in value_slices] rdd = filtered.map(lambda kv: (key_func(kv[0]), kv[1][value_slices])) shape = tuple([int(ceil((s.stop - s.start) / float(s.step))) for s in index]) split = self.split return rdd, shape, split
def _getadvanced(self, index): """ Advanced indexing (for sets, lists, or ndarrays). """ index = [asarray(i) for i in index] shape = index[0].shape if not all([i.shape == shape for i in index]): raise ValueError("shape mismatch: indexing arrays could not be broadcast " "together with shapes " + ("%s " * self.ndim) % tuple([i.shape for i in index])) index = tuple([listify(i, d) for (i, d) in zip(index, self.shape)]) # build tuples with target indices key_tuples = list(zip(*index[0:self.split])) value_tuples = list(zip(*index[self.split:])) # build dictionary to look up targets in values d = {} for k, g in groupby(zip(value_tuples, key_tuples), lambda x: x[1]): d[k] = map(lambda x: x[0], list(g)) def key_check(key): return key in key_tuples def key_func(key): return unravel_index(key, shape) # filter records based on key targets filtered = self._rdd.filter(lambda kv: key_check(kv[0])) # subselect and flatten records based on value targets (if they exist) if len(value_tuples) > 0: flattened = filtered.flatMap(lambda kv: [(kv[0], kv[1][i]) for i in d[kv[0]]]) else: flattened = filtered # reindex indexed = flattened.zipWithIndex() rdd = indexed.map(lambda kkv: (key_func(kkv[1]), kkv[0][1])) split = len(shape) return rdd, shape, split
def _getmixed(self, index): """ Mixed indexing (combines basic and advanced indexes) Assumes that only a single advanced index is used, due to the complicated behavior needed to be compatible with NumPy otherwise. """ # find the single advanced index loc = where([isinstance(i, (tuple, list, ndarray)) for i in index])[0][0] idx = list(index[loc]) if isinstance(idx[0], (tuple, list, ndarray)): raise ValueError("When mixing basic and advanced indexing, " "advanced index must be one-dimensional") # single advanced index is on a key -- filter and update key if loc < self.split: def newkey(key): newkey = list(key) newkey[loc] = idx.index(key[loc]) return tuple(newkey) rdd = self._rdd.filter(lambda kv: kv[0][loc] in idx).map(lambda kv: (newkey(kv[0]), kv[1])) # single advanced index is on a value -- use NumPy indexing else: slices = [slice(0, None, None) for _ in self.values.shape] slices[loc - self.split] = idx rdd = self._rdd.map(lambda kv: (kv[0], kv[1][slices])) newshape = list(self.shape) newshape[loc] = len(idx) barray = self._constructor(rdd, shape=tuple(newshape)).__finalize__(self) # apply the rest of the simple indices new_index = index[:] new_index[loc] = slice(0, None, None) barray = barray[tuple(new_index)] return barray._rdd, barray.shape, barray.split
def chunk(self, size="150", axis=None, padding=None): """ Chunks records of a distributed array. Chunking breaks arrays into subarrays, using an specified size of chunks along each value dimension. Can alternatively specify an average chunk byte size (in kilobytes) and the size of chunks (as ints) will be computed automatically. Parameters ---------- size : tuple, int, or str, optional, default = "150" A string giving the size in kilobytes, or a tuple with the size of chunks along each dimension. axis : int or tuple, optional, default = None One or more axis to chunk array along, if None will use all axes, padding: tuple or int, default = None Number of elements per dimension that will overlap with the adjacent chunk. If a tuple, specifies padding along each chunked dimension; if a int, same padding will be applied to all chunked dimensions. Returns ------- ChunkedArray """ if type(size) is not str: size = tupleize((size)) axis = tupleize((axis)) padding = tupleize((padding)) from bolt.spark.chunk import ChunkedArray chnk = ChunkedArray(rdd=self._rdd, shape=self._shape, split=self._split, dtype=self._dtype) return chnk._chunk(size, axis, padding)
def swap(self, kaxes, vaxes, size="150"): """ Swap axes from keys to values. This is the core operation underlying shape manipulation on the Spark bolt array. It exchanges an arbitrary set of axes between the keys and the valeus. If either is None, will only move axes in one direction (from keys to values, or values to keys). Keys moved to values will be placed immediately after the split; values moved to keys will be placed immediately before the split. Parameters ---------- kaxes : tuple Axes from keys to move to values vaxes : tuple Axes from values to move to keys size : tuple or int, optional, default = "150" Can either provide a string giving the size in kilobytes, or a tuple with the number of chunks along each value dimension being moved Returns ------- BoltArraySpark """ kaxes = asarray(tupleize(kaxes), 'int') vaxes = asarray(tupleize(vaxes), 'int') if type(size) is not str: size = tupleize(size) if len(kaxes) == self.keys.ndim and len(vaxes) == 0: raise ValueError('Cannot perform a swap that would ' 'end up with all data on a single key') if len(kaxes) == 0 and len(vaxes) == 0: return self from bolt.spark.chunk import ChunkedArray chunks = self.chunk(size) swapped = chunks.keys_to_values(kaxes).values_to_keys([v+len(kaxes) for v in vaxes]) barray = swapped.unchunk() return barray
def transpose(self, *axes): """ Return an array with the axes transposed. This operation will incur a swap unless the desiured permutation can be obtained only by transpoing the keys or the values. Parameters ---------- axes : None, tuple of ints, or n ints If None, will reverse axis order. """ if len(axes) == 0: p = arange(self.ndim-1, -1, -1) else: p = asarray(argpack(axes)) istransposeable(p, range(self.ndim)) split = self.split # compute the keys/value axes that need to be swapped new_keys, new_values = p[:split], p[split:] swapping_keys = sort(new_values[new_values < split]) swapping_values = sort(new_keys[new_keys >= split]) stationary_keys = sort(new_keys[new_keys < split]) stationary_values = sort(new_values[new_values >= split]) # compute the permutation that the swap causes p_swap = r_[stationary_keys, swapping_values, swapping_keys, stationary_values] # compute the extra permutation (p_x) on top of this that # needs to happen to get the full permutation desired p_swap_inv = argsort(p_swap) p_x = p_swap_inv[p] p_keys, p_values = p_x[:split], p_x[split:]-split # perform the swap and the the within key/value permutations arr = self.swap(swapping_keys, swapping_values-split) arr = arr.keys.transpose(tuple(p_keys.tolist())) arr = arr.values.transpose(tuple(p_values.tolist())) return arr
def swapaxes(self, axis1, axis2): """ Return the array with two axes interchanged. Parameters ---------- axis1 : int The first axis to swap axis2 : int The second axis to swap """ p = list(range(self.ndim)) p[axis1] = axis2 p[axis2] = axis1 return self.transpose(p)
def reshape(self, *shape): """ Return an array with the same data but a new shape. Currently only supports reshaping that independently reshapes the keys, or the values, or both. Parameters ---------- shape : tuple of ints, or n ints New shape """ new = argpack(shape) isreshapeable(new, self.shape) if new == self.shape: return self i = self._reshapebasic(new) if i == -1: raise NotImplementedError("Currently no support for reshaping between " "keys and values for BoltArraySpark") else: new_key_shape, new_value_shape = new[:i], new[i:] return self.keys.reshape(new_key_shape).values.reshape(new_value_shape)
def _reshapebasic(self, shape): """ Check if the requested reshape can be broken into independant reshapes on the keys and values. If it can, returns the index in the new shape separating keys from values, otherwise returns -1 """ new = tupleize(shape) old_key_size = prod(self.keys.shape) old_value_size = prod(self.values.shape) for i in range(len(new)): new_key_size = prod(new[:i]) new_value_size = prod(new[i:]) if new_key_size == old_key_size and new_value_size == old_value_size: return i return -1
def squeeze(self, axis=None): """ Remove one or more single-dimensional axes from the array. Parameters ---------- axis : tuple or int One or more singleton axes to remove. """ if not any([d == 1 for d in self.shape]): return self if axis is None: drop = where(asarray(self.shape) == 1)[0] elif isinstance(axis, int): drop = asarray((axis,)) elif isinstance(axis, tuple): drop = asarray(axis) else: raise ValueError("an integer or tuple is required for the axis") if any([self.shape[i] > 1 for i in drop]): raise ValueError("cannot select an axis to squeeze out which has size greater than one") if any(asarray(drop) < self.split): kmask = set([d for d in drop if d < self.split]) kfunc = lambda k: tuple([kk for ii, kk in enumerate(k) if ii not in kmask]) else: kfunc = lambda k: k if any(asarray(drop) >= self.split): vmask = tuple([d - self.split for d in drop if d >= self.split]) vfunc = lambda v: v.squeeze(vmask) else: vfunc = lambda v: v rdd = self._rdd.map(lambda kv: (kfunc(kv[0]), vfunc(kv[1]))) shape = tuple([ss for ii, ss in enumerate(self.shape) if ii not in drop]) split = len([d for d in range(self.keys.ndim) if d not in drop]) return self._constructor(rdd, shape=shape, split=split).__finalize__(self)
def astype(self, dtype, casting='unsafe'): """ Cast the array to a specified type. Parameters ---------- dtype : str or dtype Typecode or data-type to cast the array to (see numpy) """ rdd = self._rdd.mapValues(lambda v: v.astype(dtype, 'K', casting)) return self._constructor(rdd, dtype=dtype).__finalize__(self)
def clip(self, min=None, max=None): """ Clip values above and below. Parameters ---------- min : scalar or array-like Minimum value. If array, will be broadcasted max : scalar or array-like Maximum value. If array, will be broadcasted. """ rdd = self._rdd.mapValues(lambda v: v.clip(min=min, max=max)) return self._constructor(rdd).__finalize__(self)
def toarray(self): """ Returns the contents as a local array. Will likely cause memory problems for large objects. """ rdd = self._rdd if self._ordered else self._rdd.sortByKey() x = rdd.values().collect() return asarray(x).reshape(self.shape)
def tupleize(arg): """ Coerce singletons and lists and ndarrays to tuples. Parameters ---------- arg : tuple, list, ndarray, or singleton Item to coerce """ if arg is None: return None if not isinstance(arg, (tuple, list, ndarray, Iterable)): return tuple((arg,)) elif isinstance(arg, (list, ndarray)): return tuple(arg) elif isinstance(arg, Iterable) and not isinstance(arg, str): return tuple(arg) else: return arg
def argpack(args): """ Coerce a list of arguments to a tuple. Parameters ---------- args : tuple or nested tuple Pack arguments into a tuple, converting ((,...),) or (,) -> (,) """ if isinstance(args[0], (tuple, list, ndarray)): return tupleize(args[0]) elif isinstance(args[0], Iterable) and not isinstance(args[0], str): # coerce any iterable into a list before calling tupleize (Python 3 compatibility) return tupleize(list(args[0])) else: return tuple(args)
def inshape(shape, axes): """ Checks to see if a list of axes are contained within an array shape. Parameters ---------- shape : tuple[int] the shape of a BoltArray axes : tuple[int] the axes to check against shape """ valid = all([(axis < len(shape)) and (axis >= 0) for axis in axes]) if not valid: raise ValueError("axes not valid for an ndarray of shape: %s" % str(shape))
def allclose(a, b): """ Test that a and b are close and match in shape. Parameters ---------- a : ndarray First array to check b : ndarray First array to check """ from numpy import allclose return (a.shape == b.shape) and allclose(a, b)
def listify(lst, dim): """ Flatten lists of indices and ensure bounded by a known dim. Parameters ---------- lst : list List of integer indices dim : tuple Bounds for indices """ if not all([l.dtype == int for l in lst]): raise ValueError("indices must be integers") if npany(asarray(lst) >= dim): raise ValueError("indices out of bounds for axis with size %s" % dim) return lst.flatten()
def slicify(slc, dim): """ Force a slice to have defined start, stop, and step from a known dim. Start and stop will always be positive. Step may be negative. There is an exception where a negative step overflows the stop needs to have the default value set to -1. This is the only case of a negative start/stop value. Parameters ---------- slc : slice or int The slice to modify, or int to convert to a slice dim : tuple Bound for slice """ if isinstance(slc, slice): # default limits start = 0 if slc.start is None else slc.start stop = dim if slc.stop is None else slc.stop step = 1 if slc.step is None else slc.step # account for negative indices if start < 0: start += dim if stop < 0: stop += dim # account for over-flowing the bounds if step > 0: if start < 0: start = 0 if stop > dim: stop = dim else: if stop < 0: stop = -1 if start > dim: start = dim-1 return slice(start, stop, step) elif isinstance(slc, int): if slc < 0: slc += dim return slice(slc, slc+1, 1) else: raise ValueError("Type for slice %s not recongized" % type(slc))
def istransposeable(new, old): """ Check to see if a proposed tuple of axes is a valid permutation of an old set of axes. Checks length, axis repetion, and bounds. Parameters ---------- new : tuple tuple of proposed axes old : tuple tuple of old axes """ new, old = tupleize(new), tupleize(old) if not len(new) == len(old): raise ValueError("Axes do not match axes of keys") if not len(set(new)) == len(set(old)): raise ValueError("Repeated axes") if any(n < 0 for n in new) or max(new) > len(old) - 1: raise ValueError("Invalid axes")
def isreshapeable(new, old): """ Check to see if a proposed tuple of axes is a valid reshaping of the old axes by ensuring that they can be factored. Parameters ---------- new : tuple tuple of proposed axes old : tuple tuple of old axes """ new, old = tupleize(new), tupleize(old) if not prod(new) == prod(old): raise ValueError("Total size of new keys must remain unchanged")
def allstack(vals, depth=0): """ If an ndarray has been split into multiple chunks by splitting it along each axis at a number of locations, this function rebuilds the original array from chunks. Parameters ---------- vals : nested lists of ndarrays each level of nesting of the lists representing a dimension of the original array. """ if type(vals[0]) is ndarray: return concatenate(vals, axis=depth) else: return concatenate([allstack(x, depth+1) for x in vals], axis=depth)
def iterexpand(arry, extra): """ Expand dimensions by iteratively append empty axes. Parameters ---------- arry : ndarray The original array extra : int The number of empty axes to append """ for d in range(arry.ndim, arry.ndim+extra): arry = expand_dims(arry, axis=d) return arry
def zip_with_index(rdd): """ Alternate version of Spark's zipWithIndex that eagerly returns count. """ starts = [0] if rdd.getNumPartitions() > 1: nums = rdd.mapPartitions(lambda it: [sum(1 for _ in it)]).collect() count = sum(nums) for i in range(len(nums) - 1): starts.append(starts[-1] + nums[i]) else: count = rdd.count() def func(k, it): for i, v in enumerate(it, starts[k]): yield v, i return count, rdd.mapPartitionsWithIndex(func)
def wrapped(f): """ Decorator to append routed docstrings """ import inspect def extract(func): append = "" args = inspect.getargspec(func) for i, a in enumerate(args.args): if i < (len(args) - len(args.defaults)): append += str(a) + ", " else: default = args.defaults[i-len(args.defaults)] if hasattr(default, "__name__"): default = default.__name__ else: default = str(default) append += str(a) + "=" + default + ", " append = append[:-2] + ")" return append doc = f.__doc__ + "\n" doc += " local -> array(" + extract(getattr(ConstructLocal, f.__name__)) + "\n" doc += " spark -> array(" + extract(getattr(ConstructSpark, f.__name__)) + "\n" f.__doc__ = doc return f
def lookup(*args, **kwargs): """ Use arguments to route constructor. Applies a series of checks on arguments to identify constructor, starting with known keyword arguments, and then applying constructor-specific checks """ if 'mode' in kwargs: mode = kwargs['mode'] if mode not in constructors: raise ValueError('Mode %s not supported' % mode) del kwargs['mode'] return constructors[mode] else: for mode, constructor in constructors: if constructor._argcheck(*args, **kwargs): return constructor return ConstructLocal
def reshape(self, *shape): """ Reshape just the keys of a BoltArraySpark, returning a new BoltArraySpark. Parameters ---------- shape : tuple New proposed axes. """ new = argpack(shape) old = self.shape isreshapeable(new, old) if new == old: return self._barray def f(k): return unravel_index(ravel_multi_index(k, old), new) newrdd = self._barray._rdd.map(lambda kv: (f(kv[0]), kv[1])) newsplit = len(new) newshape = new + self._barray.values.shape return BoltArraySpark(newrdd, shape=newshape, split=newsplit).__finalize__(self._barray)
def transpose(self, *axes): """ Transpose just the keys of a BoltArraySpark, returning a new BoltArraySpark. Parameters ---------- axes : tuple New proposed axes. """ new = argpack(axes) old = range(self.ndim) istransposeable(new, old) if new == old: return self._barray def f(k): return tuple(k[i] for i in new) newrdd = self._barray._rdd.map(lambda kv: (f(kv[0]), kv[1])) newshape = tuple(self.shape[i] for i in new) + self._barray.values.shape return BoltArraySpark(newrdd, shape=newshape, ordered=False).__finalize__(self._barray)
def reshape(self, *shape): """ Reshape just the values of a BoltArraySpark, returning a new BoltArraySpark. Parameters ---------- shape : tuple New proposed axes. """ new = argpack(shape) old = self.shape isreshapeable(new, old) if new == old: return self._barray def f(v): return v.reshape(new) newrdd = self._barray._rdd.mapValues(f) newshape = self._barray.keys.shape + new return BoltArraySpark(newrdd, shape=newshape).__finalize__(self._barray)
def transpose(self, *axes): """ Transpose just the values of a BoltArraySpark, returning a new BoltArraySpark. Parameters ---------- axes : tuple New proposed axes. """ new = argpack(axes) old = range(self.ndim) istransposeable(new, old) if new == old: return self._barray def f(v): return v.transpose(new) newrdd = self._barray._rdd.mapValues(f) newshape = self._barray.keys.shape + tuple(self.shape[i] for i in new) return BoltArraySpark(newrdd, shape=newshape).__finalize__(self._barray)
def ones(shape, dtype=float64, order='C'): """ Create a local bolt array of ones. Parameters ---------- shape : tuple Dimensions of the desired array dtype : data-type, optional, default=float64 The desired data-type for the array. (see numpy) order : {'C', 'F', 'A'}, optional, default='C' The order of the array. (see numpy) Returns ------- BoltArrayLocal """ from numpy import ones return ConstructLocal._wrap(ones, shape, dtype, order)
def zeros(shape, dtype=float64, order='C'): """ Create a local bolt array of zeros. Parameters ---------- shape : tuple Dimensions of the desired array. dtype : data-type, optional, default=float64 The desired data-type for the array. (see numpy) order : {'C', 'F', 'A'}, optional, default='C' The order of the array. (see numpy) Returns ------- BoltArrayLocal """ from numpy import zeros return ConstructLocal._wrap(zeros, shape, dtype, order)
def concatenate(arrays, axis=0): """ Join a sequence of arrays together. Parameters ---------- arrays : tuple A sequence of array-like e.g. (a1, a2, ...) axis : int, optional, default=0 The axis along which the arrays will be joined. Returns ------- BoltArrayLocal """ if not isinstance(arrays, tuple): raise ValueError("data type not understood") arrays = tuple([asarray(a) for a in arrays]) from numpy import concatenate return BoltArrayLocal(concatenate(arrays, axis))
def plfit_lsq(x,y): """ Returns A and B in y=Ax^B http://mathworld.wolfram.com/LeastSquaresFittingPowerLaw.html """ n = len(x) btop = n * (log(x)*log(y)).sum() - (log(x)).sum()*(log(y)).sum() bbottom = n*(log(x)**2).sum() - (log(x).sum())**2 b = btop / bbottom a = ( log(y).sum() - b * log(x).sum() ) / n A = exp(a) return A,b
def plfit(x,nosmall=False,finite=False): """ A Python implementation of the Matlab code http://www.santafe.edu/~aaronc/powerlaws/plfit.m from http://www.santafe.edu/~aaronc/powerlaws/ See A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions in empirical data" SIAM Review, to appear (2009). (arXiv:0706.1062) http://arxiv.org/abs/0706.1062 """ xmins = unique(x) xmins = xmins[1:-1] dat = xmins * 0 z = sort(x) for xm in arange(len(xmins)): xmin = xmins[xm] z = z[z>=xmin] n = float(len(z)) # estimate alpha using direct MLE a = n / sum( log(z/xmin) ) if nosmall: # 4. For continuous data, PLFIT can return erroneously large estimates of # alpha when xmin is so large that the number of obs x >= xmin is very # small. To prevent this, we can truncate the search over xmin values # before the finite-size bias becomes significant by calling PLFIT as if (a-1)/sqrt(n) > 0.1: #dat(xm:end) = []; dat = dat[:xm] xm = len(xmins)+1 break # compute KS statistic cx = arange(n)/float(n) #data cf = 1-(xmin/z)**a # fitted dat[xm] = max( abs(cf-cx) ) D = min(dat); #xmin = xmins(find(dat<=D,1,'first')); xmin = xmins[argmin(dat)] z = x[x>=xmin] n = len(z) alpha = 1 + n / sum( log(z/xmin) ) if finite: alpha = alpha*(n-1)/n+1/n if n < 50 and ~finite: print '(PLFIT) Warning: finite-size bias may be present.' L = n*log((alpha-1)/xmin) - alpha*sum(log(z/xmin)); return xmin,alpha,L,dat
def plotcdf(x,xmin,alpha): """ Plots CDF and powerlaw """ x=sort(x) n=len(x) xcdf = arange(n,0,-1,dtype='float')/float(n) q = x[x>=xmin] fcdf = (q/xmin)**(1-alpha) nc = xcdf[argmax(x>=xmin)] fcdf_norm = nc*fcdf loglog(x,xcdf) loglog(q,fcdf_norm)
def plotpdf(x,xmin,alpha,nbins=30,dolog=False): """ Plots PDF and powerlaw.... """ x=sort(x) n=len(x) if dolog: hb = hist(x,bins=logspace(log10(min(x)),log10(max(x)),nbins),log=True) alpha += 1 else: hb = hist(x,bins=linspace((min(x)),(max(x)),nbins)) h,b=hb[0],hb[1] b = b[1:] q = x[x>=xmin] px = (alpha-1)/xmin * (q/xmin)**(-alpha) arg = argmin(abs(b-xmin)) norm = mean( h[b>xmin] / ((alpha-1)/xmin * (b[b>xmin]/xmin)**(-alpha)) ) px = px*norm loglog(q,px) gca().set_xlim(min(x),max(x))
def plexp(x,xm=1,a=2.5): """ CDF(x) for the piecewise distribution exponential x<xmin, powerlaw x>=xmin This is the CDF version of the distributions drawn in fig 3.4a of Clauset et al. """ C = 1/(-xm/(1 - a) - xm/a + math.exp(a)*xm/a) Ppl = lambda X: 1+C*(xm/(1-a)*(X/xm)**(1-a)) Pexp = lambda X: C*xm/a*math.exp(a)-C*(xm/a)*math.exp(-a*(X/xm-1)) d=Ppl(x) d[x<xm]=Pexp(x) return d
def plexp_inv(P,xm,a): """ Inverse CDF for a piecewise PDF as defined in eqn. 3.10 of Clauset et al. """ C = 1/(-xm/(1 - a) - xm/a + math.exp(a)*xm/a) Pxm = 1+C*(xm/(1-a)) pp = P x = xm*(pp-1)*(1-a)/(C*xm)**(1/(1-a)) if pp >= Pxm else (math.log( ((C*xm/a)*math.exp(a)-pp)/(C*xm/a)) - a) * (-xm/a) #x[P>=Pxm] = xm*( (P[P>=Pxm]-1) * (1-a)/(C*xm) )**(1/(1-a)) # powerlaw #x[P<Pxm] = (math.log( (C*xm/a*math.exp(a)-P[P<Pxm])/(C*xm/a) ) - a) * (-xm/a) # exp return x
def alpha_(self,x): """ Create a mappable function alpha to apply to each xmin in a list of xmins. This is essentially the slow version of fplfit/cplfit, though I bet it could be speeded up with a clever use of parellel_map. Not intended to be used by users.""" def alpha(xmin,x=x): """ given a sorted data set and a minimum, returns power law MLE fit data is passed as a keyword parameter so that it can be vectorized """ x = [i for i in x if i>=xmin] n = sum(x) divsum = sum([math.log(i/xmin) for i in x]) if divsum == 0: return float('inf') # the "1+" here is unimportant because alpha_ is only used for minimization a = 1 + float(n) / divsum return a return alpha
def plfit(self,nosmall=True,finite=False,quiet=False,silent=False, xmin=None, verbose=False): """ A pure-Python implementation of the Matlab code http://www.santafe.edu/~aaronc/powerlaws/plfit.m from http://www.santafe.edu/~aaronc/powerlaws/ See A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions in empirical data" SIAM Review, 51, 661-703 (2009). (arXiv:0706.1062) http://arxiv.org/abs/0706.1062 nosmall is on by default; it rejects low s/n points can specify xmin to skip xmin estimation This is only for continuous distributions; I have not implemented a pure-python discrete distribution fitter """ x = self.data z = sorted(x) t = time.time() possible_xmins = sorted(set(z)) argxmins = [z.index(i) for i in possible_xmins] self._nunique = len(possible_xmins) if xmin is None: av = map(self.alpha_(z),possible_xmins) dat = map(self.kstest_(z),possible_xmins) sigma = [(a-1)/math.sqrt(len(z)-i+1) for a,i in zip(av,argxmins)] if nosmall: # test to make sure the number of data points is high enough # to provide a reasonable s/n on the computed alpha goodvals = [s<0.1 for s in sigma] if False in goodvals: nmax = goodvals.index(False) dat = dat[:nmax] possible_xmins = possible_xmins[:nmax] av = av[:nmax] else: print("Not enough data left after flagging - using all positive data.") if not quiet: print("PYTHON plfit executed in %f seconds" % (time.time()-t)) self._av = av self._xmin_kstest = dat self._sigma = sigma # [:-1] to weed out the very last data point; it cannot be correct # (can't have a power law with 1 data point). # However, this should only be done if the ends have not previously # been excluded with nosmall if nosmall: xmin = possible_xmins[dat.index(min(dat))] else: xmin = possible_xmins[dat.index(min(dat[:-1]))] z = [i for i in z if i >= xmin] n = len(z) alpha = 1 + n / sum([math.log(a/xmin) for a in z]) if finite: alpha = alpha*(n-1.)/n+1./n if n == 1 and not silent: print("Failure: only 1 point kept. Probably not a power-law distribution.") self._alpha = 0 self._alphaerr = 0 self._likelihood = 0 self._ks = 0 self._ks_prob = 0 self._xmin = xmin return xmin,0 if n < 50 and not finite and not silent: print('(PLFIT) Warning: finite-size bias may be present. n=%i' % n) # ks = max(abs( numpy.arange(n)/float(n) - (1-(xmin/z)**(alpha-1)) )) ks = max( [abs( i/float(n) - (1-(xmin/b)**(alpha-1))) for i,b in zip(range(n),z)] ) # Parallels Eqn 3.5 in Clauset et al 2009, but zeta(alpha, xmin) = (alpha-1)/xmin. Really is Eqn B3 in paper. #L = n*log((alpha-1)/xmin) - alpha*sum(log(z/xmin)) sl = sum([math.log(a/xmin) for a in z]) L = (n*math.log((alpha-1)/xmin) - alpha*sl) #requires another map... Larr = arange(len(unique(x))) * log((av-1)/unique(x)) - av*sum self._likelihood = L self._xmin = xmin self._xmins = possible_xmins self._alpha= alpha self._alphaerr = (alpha-1)/math.sqrt(n) self._ks = ks # this ks statistic may not have the same value as min(dat) because of unique() #if scipyOK: self._ks_prob = scipy.stats.kstwobign.sf(ks*numpy.sqrt(n)) self._ngtx = n if math.isnan(L) or math.isnan(xmin) or math.isnan(alpha): raise ValueError("plfit failed; returned a nan") if not quiet: if verbose: print("The lowest value included in the power-law fit, ", end=' ') print("xmin: %g" % xmin, end=' ') if verbose: print("\nThe number of values above xmin, ", end=' ') print("n(>xmin): %i" % n, end=' ') if verbose: print("\nThe derived power-law alpha (p(x)~x^-alpha) with MLE-derived error, ", end=' ') print("alpha: %g +/- %g " % (alpha,self._alphaerr), end=' ') if verbose: print("\nThe log of the Likelihood (the maximized parameter), ", end=' ') print("Log-Likelihood: %g " % L, end=' ') if verbose: print("\nThe KS-test statistic between the best-fit power-law and the data, ", end=' ') print("ks: %g" % (ks)) return xmin,alpha
def alpha_gen(x): """ Create a mappable function alpha to apply to each xmin in a list of xmins. This is essentially the slow version of fplfit/cplfit, though I bet it could be speeded up with a clever use of parellel_map. Not intended to be used by users. Docstring for the generated alpha function:: Given a sorted data set and a minimum, returns power law MLE fit data is passed as a keyword parameter so that it can be vectorized If there is only one element, return alpha=0 """ def alpha_(xmin,x=x): """ Given a sorted data set and a minimum, returns power law MLE fit data is passed as a keyword parameter so that it can be vectorized If there is only one element, return alpha=0 """ gexmin = x>=xmin n = np.count_nonzero(gexmin) if n < 2: return 0 x = x[gexmin] a = 1 + float(n) / sum(log(x/xmin)) return a return alpha_
def plexp_cdf(x,xmin=1,alpha=2.5, pl_only=False, exp_only=False): """ CDF(x) for the piecewise distribution exponential x<xmin, powerlaw x>=xmin This is the CDF version of the distributions drawn in fig 3.4a of Clauset et al. The constant "C" normalizes the PDF """ x = np.array(x) C = 1/(-xmin/(1 - alpha) - xmin/alpha + exp(alpha)*xmin/alpha) Ppl = lambda X: 1+C*(xmin/(1-alpha)*(X/xmin)**(1-alpha)) Pexp = lambda X: C*xmin/alpha*exp(alpha)-C*(xmin/alpha)*exp(-alpha*(X/xmin-1)) if exp_only: return Pexp(x) elif pl_only: return Ppl(x) d=Ppl(x) d[x<xmin]=Pexp(x)[x<xmin] return d
def plexp_inv(P, xmin, alpha, guess=1.): """ Inverse CDF for a piecewise PDF as defined in eqn. 3.10 of Clauset et al. (previous version was incorrect and lead to weird discontinuities in the distribution function) """ def equation(x,prob): return plexp_cdf(x, xmin, alpha)-prob # http://stackoverflow.com/questions/19840425/scipy-optimize-faster-root-finding-over-2d-grid def solver(y, x0=guess): return scipy.optimize.fsolve(equation, guess, args=(y,)) f = np.vectorize(solver) return f(P)
def discrete_likelihood(data, xmin, alpha): """ Equation B.8 in Clauset Given a data set, an xmin value, and an alpha "scaling parameter", computes the log-likelihood (the value to be maximized) """ if not scipyOK: raise ImportError("Can't import scipy. Need scipy for zeta function.") from scipy.special import zeta as zeta zz = data[data>=xmin] nn = len(zz) sum_log_data = np.log(zz).sum() zeta = zeta(alpha, xmin) L_of_alpha = -1*nn*log(zeta) - alpha * sum_log_data return L_of_alpha
def discrete_likelihood_vector(data, xmin, alpharange=(1.5,3.5), n_alpha=201): """ Compute the likelihood for all "scaling parameters" in the range (alpharange) for a given xmin. This is only part of the discrete value likelihood maximization problem as described in Clauset et al (Equation B.8) *alpharange* [ 2-tuple ] Two floats specifying the upper and lower limits of the power law alpha to test """ from scipy.special import zeta as zeta zz = data[data>=xmin] nn = len(zz) alpha_vector = np.linspace(alpharange[0],alpharange[1],n_alpha) sum_log_data = np.log(zz).sum() # alpha_vector is a vector, xmin is a scalar zeta_vector = zeta(alpha_vector, xmin) #xminvec = np.arange(1.0,xmin) #xminalphasum = np.sum([xm**(-alpha_vector) for xm in xminvec]) #L = -1*alpha_vector*sum_log_data - nn*log(zeta_vector) - xminalphasum L_of_alpha = -1*nn*log(zeta_vector) - alpha_vector * sum_log_data return L_of_alpha
def discrete_max_likelihood_arg(data, xmin, alpharange=(1.5,3.5), n_alpha=201): """ Returns the *argument* of the max of the likelihood of the data given an input xmin """ likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha) Largmax = np.argmax(likelihoods) return Largmax
def discrete_max_likelihood(data, xmin, alpharange=(1.5,3.5), n_alpha=201): """ Returns the *argument* of the max of the likelihood of the data given an input xmin """ likelihoods = discrete_likelihood_vector(data, xmin, alpharange=alpharange, n_alpha=n_alpha) Lmax = np.max(likelihoods) return Lmax
def most_likely_alpha(data, xmin, alpharange=(1.5,3.5), n_alpha=201): """ Return the most likely alpha for the data given an xmin """ alpha_vector = np.linspace(alpharange[0],alpharange[1],n_alpha) return alpha_vector[discrete_max_likelihood_arg(data, xmin, alpharange=alpharange, n_alpha=n_alpha)]
def discrete_alpha_mle(data, xmin): """ Equation B.17 of Clauset et al 2009 The Maximum Likelihood Estimator of the "scaling parameter" alpha in the discrete case is similar to that in the continuous case """ # boolean indices of positive data gexmin = (data>=xmin) nn = gexmin.sum() if nn < 2: return 0 xx = data[gexmin] alpha = 1.0 + float(nn) * (sum(log(xx/(float(xmin)-0.5))))**-1 return alpha
def discrete_best_alpha(data, alpharangemults=(0.9,1.1), n_alpha=201, approximate=True, verbose=True): """ Use the maximum L to determine the most likely value of alpha *alpharangemults* [ 2-tuple ] Pair of values indicating multiplicative factors above and below the approximate alpha from the MLE alpha to use when determining the "exact" alpha (by directly maximizing the likelihood function) """ xmins = np.unique(data) if approximate: alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ] else: alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ] alpharanges = [(0.9*a,1.1*a) for a in alpha_approx] alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha) for xmin,ar in zip(xmins,alpharanges) ] ksvalues = [ discrete_ksD(data, xmin, alpha) for xmin,alpha in zip(xmins,alpha_of_xmin) ] best_index = argmin(ksvalues) best_alpha = alpha_of_xmin[best_index] best_xmin = xmins[best_index] best_ks = ksvalues[best_index] best_likelihood = discrete_likelihood(data, best_xmin, best_alpha) if verbose: print("alpha = %f xmin = %f ksD = %f L = %f (n<x) = %i (n>=x) = %i" % ( best_alpha, best_xmin, best_ks, best_likelihood, (data<best_xmin).sum(), (data>=best_xmin).sum())) return best_alpha,best_xmin,best_ks,best_likelihood
def discrete_ksD(data, xmin, alpha): """ given a sorted data set, a minimum, and an alpha, returns the power law ks-test D value w/data The returned value is the "D" parameter in the ks test (this is implemented differently from the continuous version because there are potentially multiple identical points that need comparison to the power law) """ zz = np.sort(data[data>=xmin]) nn = float(len(zz)) if nn < 2: return np.inf #cx = np.arange(nn,dtype='float')/float(nn) #cf = 1.0-(zz/xmin)**(1.0-alpha) model_cdf = 1.0-(zz.astype('float')/float(xmin))**(1.0-alpha) data_cdf = np.searchsorted(zz,zz,side='left')/(float(nn)) ks = max(abs(model_cdf-data_cdf)) return ks
def plfit(self, nosmall=True, finite=False, quiet=False, silent=False, usefortran=False, usecy=False, xmin=None, verbose=False, discrete=None, discrete_approx=True, discrete_n_alpha=1000, skip_consistency_check=False): """ A Python implementation of the Matlab code http://www.santafe.edu/~aaronc/powerlaws/plfit.m from http://www.santafe.edu/~aaronc/powerlaws/ See A. Clauset, C.R. Shalizi, and M.E.J. Newman, "Power-law distributions in empirical data" SIAM Review, 51, 661-703 (2009). (arXiv:0706.1062) http://arxiv.org/abs/0706.1062 There are 3 implementations of xmin estimation. The fortran version is fastest, the C (cython) version is ~10% slower, and the python version is ~3x slower than the fortran version. Also, the cython code suffers ~2% numerical error relative to the fortran and python for unknown reasons. There is also a discrete version implemented in python - it is different from the continous version! Parameters ---------- discrete : bool or None If *discrete* is None, the code will try to determine whether the data set is discrete or continous based on the uniqueness of the data; if your data set is continuous but you have any non-unique data points (e.g., flagged "bad" data), the "automatic" determination will fail. If *discrete* is True or False, the discrete or continuous fitter will be used, respectively. xmin : float or int If you specify xmin, the fitter will only determine alpha assuming the given xmin; the rest of the code (and most of the complexity) is determining an estimate for xmin and alpha. nosmall : bool When on, the code rejects low s/n points. WARNING: This option, which is on by default, may result in different answers than the original Matlab code and the "powerlaw" python package finite : bool There is a 'finite-size bias' to the estimator. The "alpha" the code measures is "alpha-hat" s.t. ᾶ = (nα-1)/(n-1), or α = (1 + ᾶ (n-1)) / n quiet : bool If False, delivers messages about what fitter is used and the fit results verbose : bool Deliver descriptive messages about the fit parameters (only if `quiet==False`) silent : bool If True, will print NO messages skip_consistency_check : bool The code will normally perform a consistency check to make sure the alpha value computed by the fitter matches the alpha value computed directly in python. It is possible for numerical differences to creep in, usually at the 10^-6 or less level. If you see an exception reporting this type of error, skipping the check can be the appropriate next step. Returns ------- (xmin, alpha) The best-fit xmin and alpha values """ x = self.data if any(x < 0): raise ValueError("Power law distributions are only valid for " "positive data. Remove negative values before " "fitting.") z = np.sort(x) # xmins = the unique values of x that can be used as the threshold for # the power law fit # argxmins = the index of each of these possible thresholds xmins,argxmins = np.unique(z,return_index=True) self._nunique = len(xmins) if self._nunique == len(x) and discrete is None: if verbose: print("Using CONTINUOUS fitter because there are no repeated " "values.") discrete = False elif self._nunique < len(x) and discrete is None: if verbose: print("Using DISCRETE fitter because there are repeated " "values.") discrete = True t = time.time() if xmin is None: if discrete: self.discrete_best_alpha(approximate=discrete_approx, n_alpha=discrete_n_alpha, verbose=verbose, finite=finite) return self._xmin,self._alpha elif usefortran and fortranOK: kstest_values,alpha_values = fplfit.plfit(z, 0) if not quiet: print(("FORTRAN plfit executed in %f seconds" % (time.time()-t))) elif usecy and cyOK: kstest_values,alpha_values = cplfit.plfit_loop(z, nosmall=False, zunique=xmins, argunique=argxmins) if not quiet: print(("CYTHON plfit executed in %f seconds" % (time.time()-t))) else: # python (numpy) version f_alpha = alpha_gen(z) f_kstest = kstest_gen(z) alpha_values = np.asarray(list(map(f_alpha,xmins)), dtype='float') kstest_values = np.asarray(list(map(f_kstest,xmins)), dtype='float') if not quiet: print(("PYTHON plfit executed in %f seconds" % (time.time()-t))) if not quiet: if usefortran and not fortranOK: raise ImportError("fortran fplfit did not load") if usecy and not cyOK: raise ImportError("cython cplfit did not load") # For each alpha, the number of included data points is # total data length - first index of xmin # No +1 is needed: xmin is included. sigma = (alpha_values-1)/np.sqrt(len(z)-argxmins) # I had changed it to this, but I think this is wrong. # sigma = (alpha_values-1)/np.sqrt(len(z)-np.arange(len(z))) if nosmall: # test to make sure the number of data points is high enough # to provide a reasonable s/n on the computed alpha goodvals = sigma<0.1 nmax = argmin(goodvals) if nmax <= 0: nmax = len(xmins) - 1 if not silent: print("Not enough data left after flagging " "low S/N points. " "Using all data.") else: # -1 to weed out the very last data point; it cannot be correct # (can't have a power law with 1 data point). nmax = len(xmins)-1 best_ks_index = argmin(kstest_values[:nmax]) xmin = xmins[best_ks_index] self._alpha_values = alpha_values self._xmin_kstest = kstest_values if scipyOK: # CHECK THIS self._ks_prob_all = np.array([scipy.stats.ksone.sf(D_stat, len(kstest_values)-ii) for ii,D_stat in enumerate(kstest_values)]) self._sigma = sigma # sanity check n = np.count_nonzero(z>=xmin) alpha = 1. + float(n)/sum(log(z[z>=xmin]/xmin)) try: if not skip_consistency_check: np.testing.assert_almost_equal(alpha, alpha_values[best_ks_index], decimal=4) except AssertionError: raise AssertionError("The alpha value computed was not self-" "consistent. This should not happen. " "However, it is possible that this is " "a numerical uncertainty issue; the " "values being compared are {0} and {1}." "If they are close enough, set " "skip_consistency_check=True." .format(alpha, alpha_values[best_ks_index])) z = z[z>=xmin] n = len(z) alpha = 1. + float(n) / sum(log(z/xmin)) if finite: alpha = alpha*(n-1.)/n+1./n if n < 50 and not finite and not silent: print(('(PLFIT) Warning: finite-size bias may be present. n=%i' % n)) ks = max(abs( np.arange(n)/float(n) - (1-(xmin/z)**(alpha-1)) )) # Parallels Eqn 3.5 in Clauset et al 2009, but zeta(alpha, xmin) = # (alpha-1)/xmin. Really is Eqn B3 in paper. L = n*log((alpha-1)/xmin) - alpha*sum(log(z/xmin)) #requires another map... Larr = arange(len(unique(x))) * log((alpha_values-1)/unique(x)) - alpha_values*sum self._likelihood = L self._xmin = xmin self._xmins = xmins self._alpha= alpha self._alphaerr = (alpha-1)/np.sqrt(n) # this ks statistic may not have the same value as min(dat) because of unique() self._ks = ks if scipyOK: self._ks_prob = scipy.stats.ksone.sf(ks, n) self._ngtx = n if n == 1: if not silent: print("Failure: only 1 point kept. Probably not a power-law distribution.") self._alpha = alpha = 0 self._alphaerr = 0 self._likelihood = L = 0 self._ks = 0 self._ks_prob = 0 self._xmin = xmin return xmin,0 if np.isnan(L) or np.isnan(xmin) or np.isnan(alpha): raise ValueError("plfit failed; returned a nan") if not quiet: if verbose: print("The lowest value included in the power-law fit, ", end=' ') print("xmin: %g" % xmin, end=' ') if verbose: print("\nThe number of values above xmin, ", end=' ') print("n(>xmin): %i" % n, end=' ') if verbose: print("\nThe derived power-law alpha (p(x)~x^-alpha) with MLE-derived error, ", end=' ') print("alpha: %g +/- %g " % (alpha,self._alphaerr), end=' ') if verbose: print("\nThe log of the Likelihood (the maximized parameter; you minimized the negative log likelihood), ", end=' ') print("Log-Likelihood: %g " % L, end=' ') if verbose: print("\nThe KS-test statistic between the best-fit power-law and the data, ", end=' ') print("ks: %g" % (ks), end=' ') if scipyOK: if verbose: print(" occurs with probability ", end=' ') print("p(ks): %g" % (self._ks_prob)) else: print() return xmin,alpha
def discrete_best_alpha(self, alpharangemults=(0.9,1.1), n_alpha=201, approximate=True, verbose=True, finite=True): """ Use the maximum likelihood to determine the most likely value of alpha *alpharangemults* [ 2-tuple ] Pair of values indicating multiplicative factors above and below the approximate alpha from the MLE alpha to use when determining the "exact" alpha (by directly maximizing the likelihood function) *n_alpha* [ int ] Number of alpha values to use when measuring. Larger number is more accurate. *approximate* [ bool ] If False, try to "zoom-in" around the MLE alpha and get the exact best alpha value within some range around the approximate best *vebose* [ bool ] *finite* [ bool ] Correction for finite data? """ data = self.data self._xmins = xmins = np.unique(data) if approximate: alpha_of_xmin = [ discrete_alpha_mle(data,xmin) for xmin in xmins ] else: alpha_approx = [ discrete_alpha_mle(data,xmin) for xmin in xmins ] alpharanges = [(0.9*a,1.1*a) for a in alpha_approx] alpha_of_xmin = [ most_likely_alpha(data,xmin,alpharange=ar,n_alpha=n_alpha) for xmin,ar in zip(xmins,alpharanges) ] ksvalues = np.array([discrete_ksD(data, xmin, alpha) for xmin,alpha in zip(xmins,alpha_of_xmin) ]) self._alpha_values = np.array(alpha_of_xmin) self._xmin_kstest = ksvalues ksvalues[np.isnan(ksvalues)] = np.inf best_index = argmin(ksvalues) self._alpha = best_alpha = alpha_of_xmin[best_index] self._xmin = best_xmin = xmins[best_index] self._ks = best_ks = ksvalues[best_index] self._likelihood = best_likelihood = discrete_likelihood(data, best_xmin, best_alpha) if finite: self._alpha = self._alpha*(n-1.)/n+1./n if verbose: print("alpha = %f xmin = %f ksD = %f L = %f (n<x) = %i (n>=x) = %i" % ( best_alpha, best_xmin, best_ks, best_likelihood, (data<best_xmin).sum(), (data>=best_xmin).sum())) self._ngtx = n = (self.data>=self._xmin).sum() self._alphaerr = (self._alpha-1.0)/np.sqrt(n) if scipyOK: self._ks_prob = scipy.stats.ksone.sf(self._ks, n) return best_alpha,best_xmin,best_ks,best_likelihood
def xminvsks(self, **kwargs): """ Plot xmin versus the ks value for derived alpha. This plot can be used as a diagnostic of whether you have derived the 'best' fit: if there are multiple local minima, your data set may be well suited to a broken powerlaw or a different function. """ pylab.plot(self._xmins,self._xmin_kstest,'.') pylab.plot(self._xmin,self._ks,'s') #pylab.errorbar([self._ks],self._alpha,yerr=self._alphaerr,fmt='+') ax=pylab.gca() ax.set_ylabel("KS statistic") ax.set_xlabel("min(x)") pylab.draw() return ax
def alphavsks(self,autozoom=True,**kwargs): """ Plot alpha versus the ks value for derived alpha. This plot can be used as a diagnostic of whether you have derived the 'best' fit: if there are multiple local minima, your data set may be well suited to a broken powerlaw or a different function. """ pylab.plot(self._alpha_values, self._xmin_kstest, '.') pylab.errorbar(self._alpha, self._ks, xerr=self._alphaerr, fmt='+') ax=pylab.gca() if autozoom: ax.set_ylim(0.8*(self._ks),3*(self._ks)) ax.set_xlim((self._alpha)-5*self._alphaerr,(self._alpha)+5*self._alphaerr) ax.set_ylabel("KS statistic") ax.set_xlabel(r'$\alpha$') pylab.draw() return ax
def plotcdf(self, x=None, xmin=None, alpha=None, pointcolor='k', dolog=True, zoom=True, pointmarker='+', **kwargs): """ Plots CDF and powerlaw """ if x is None: x=self.data if xmin is None: xmin=self._xmin if alpha is None: alpha=self._alpha x=np.sort(x) n=len(x) xcdf = np.arange(n,0,-1,dtype='float')/float(n) q = x[x>=xmin] fcdf = (q/xmin)**(1-alpha) nc = xcdf[argmax(x>=xmin)] fcdf_norm = nc*fcdf D_location = argmax(xcdf[x>=xmin]-fcdf_norm) pylab.vlines(q[D_location], xcdf[x>=xmin][D_location], fcdf_norm[D_location], color='m', linewidth=2, zorder=2) pylab.plot([q[D_location]]*2, [xcdf[x>=xmin][D_location], fcdf_norm[D_location]], color='m', marker='s', zorder=3) #plotx = pylab.linspace(q.min(),q.max(),1000) #ploty = (plotx/xmin)**(1-alpha) * nc if dolog: pylab.loglog(x,xcdf,marker=pointmarker,color=pointcolor,**kwargs) pylab.loglog(q,fcdf_norm,'r',**kwargs) else: pylab.semilogx(x,xcdf,marker=pointmarker,color=pointcolor,**kwargs) pylab.semilogx(q,fcdf_norm,'r',**kwargs) if zoom: pylab.axis([xmin, x.max(), xcdf.min(), nc])
def plotpdf(self, x=None, xmin=None, alpha=None, nbins=50, dolog=True, dnds=False, drawstyle='steps-post', histcolor='k', plcolor='r', fill=False, dohist=True, **kwargs): """ Plots PDF and powerlaw. kwargs is passed to pylab.hist and pylab.plot """ if x is None: x=self.data if xmin is None: xmin=self._xmin if alpha is None: alpha=self._alpha x=np.sort(x) #n=len(x) pylab.gca().set_xscale('log') pylab.gca().set_yscale('log') if dnds: hb = pylab.histogram(x,bins=np.logspace(log10(min(x)),log10(max(x)),nbins)) h = hb[0] b = hb[1] db = hb[1][1:]-hb[1][:-1] h = h/db if dohist: pylab.plot(b[:-1],h,drawstyle=drawstyle,color=histcolor,**kwargs) #alpha -= 1 elif dolog: hb = pylab.hist(x, bins=np.logspace(log10(min(x)), log10(max(x)), nbins), log=True, fill=fill, edgecolor=histcolor, **kwargs) alpha -= 1 h,b=hb[0],hb[1] if not dohist: for rect in hb[2]: rect.set_visible(False) else: hb = pylab.hist(x, bins=np.linspace((min(x)), (max(x)), nbins), fill=fill, edgecolor=histcolor, **kwargs) h,b=hb[0],hb[1] if not dohist: for rect in hb[2]: rect.set_visible(False) # plotting points are at the center of each bin b = (b[1:]+b[:-1])/2.0 q = x[x>=xmin] px = (alpha-1)/xmin * (q/xmin)**(-alpha) # Normalize by the median ratio between the histogram and the power-law # The normalization is semi-arbitrary; an average is probably just as valid plotloc = (b>xmin)*(h>0) norm = np.median(h[plotloc] / ((alpha-1)/xmin * (b[plotloc]/xmin)**(-alpha))) px = px*norm plotx = pylab.linspace(q.min(),q.max(),1000) ploty = (alpha-1)/xmin * (plotx/xmin)**(-alpha) * norm #pylab.loglog(q,px,'r',**kwargs) pylab.plot(plotx, ploty, color=plcolor, **kwargs) axlims = pylab.axis() pylab.vlines(xmin, axlims[2], max(px), colors=plcolor, linestyle='dashed') if dolog and min(x) <= 0: lolim = 0.1 else: lolim = min(x) pylab.gca().set_xlim(lolim, max(x))
def plotppf(self,x=None,xmin=None,alpha=None,dolog=True,**kwargs): """ Plots the power-law-predicted value on the Y-axis against the real values along the X-axis. Can be used as a diagnostic of the fit quality. """ if not(xmin): xmin=self._xmin if not(alpha): alpha=self._alpha if not(x): x=np.sort(self.data[self.data>xmin]) else: x=np.sort(x[x>xmin]) # N = M^(-alpha+1) # M = N^(1/(-alpha+1)) m0 = min(x) N = (1.0+np.arange(len(x)))[::-1] xmodel = m0 * N**(1/(1-alpha)) / max(N)**(1/(1-alpha)) if dolog: pylab.loglog(x,xmodel,'.',**kwargs) pylab.gca().set_xlim(min(x),max(x)) pylab.gca().set_ylim(min(x),max(x)) else: pylab.plot(x,xmodel,'.',**kwargs) pylab.plot([min(x),max(x)],[min(x),max(x)],'k--') pylab.xlabel("Real Value") pylab.ylabel("Power-Law Model Value")
def lognormal(self,doprint=True): """ Use the maximum likelihood estimator for a lognormal distribution to produce the best-fit lognormal parameters """ # N = float(self.data.shape[0]) # mu = log(self.data).sum() / N # sigmasquared = ( ( log(self.data) - mu )**2 ).sum() / N # self.lognormal_mu = mu # self.lognormal_sigma = np.sqrt(sigmasquared) # self.lognormal_likelihood = -N/2. * log(np.pi*2) - N/2. * log(sigmasquared) - 1/(2*sigmasquared) * (( self.data - mu )**2).sum() # if doprint: # print "Best fit lognormal is exp( -(x-%g)^2 / (2*%g^2)" % (mu,np.sqrt(sigmasquared)) # print "Likelihood: %g" % (self.lognormal_likelihood) if scipyOK: fitpars = scipy.stats.lognorm.fit(self.data) self.lognormal_dist = scipy.stats.lognorm(*fitpars) self.lognormal_ksD,self.lognormal_ksP = scipy.stats.kstest(self.data,self.lognormal_dist.cdf) # nnlf = NEGATIVE log likelihood self.lognormal_likelihood = -1*scipy.stats.lognorm.nnlf(fitpars,self.data) # Is this the right likelihood ratio? # Definition of L from eqn. B3 of Clauset et al 2009: # L = log(p(x|alpha)) # _nnlf from scipy.stats.distributions: # -sum(log(self._pdf(x, *args)),axis=0) # Assuming the pdf and p(x|alpha) are both non-inverted, it looks # like the _nnlf and L have opposite signs, which would explain the # likelihood ratio I've used here: self.power_lognorm_likelihood = (self._likelihood + self.lognormal_likelihood) # a previous version had 2*(above). That is the correct form if you want the likelihood ratio # statistic "D": http://en.wikipedia.org/wiki/Likelihood-ratio_test # The above explanation makes sense, since nnlf is the *negative* log likelihood function: ## nnlf -- negative log likelihood function (to minimize) # # Assuming we want the ratio between the POSITIVE likelihoods, the D statistic is: # D = -2 log( L_power / L_lognormal ) self.likelihood_ratio_D = -2 * (log(self._likelihood/self.lognormal_likelihood)) if doprint: print("Lognormal KS D: %g p(D): %g" % (self.lognormal_ksD,self.lognormal_ksP), end=' ') print(" Likelihood Ratio Statistic (powerlaw/lognormal): %g" % self.likelihood_ratio_D) print("At this point, have a look at Clauset et al 2009 Appendix C: determining sigma(likelihood_ratio)")
def plot_lognormal_pdf(self,**kwargs): """ Plot the fitted lognormal distribution """ if not hasattr(self,'lognormal_dist'): return normalized_pdf = self.lognormal_dist.pdf(self.data)/self.lognormal_dist.pdf(self.data).max() minY,maxY = pylab.gca().get_ylim() pylab.plot(self.data,normalized_pdf*maxY,'.',**kwargs)
def plot_lognormal_cdf(self,**kwargs): """ Plot the fitted lognormal distribution """ if not hasattr(self,'lognormal_dist'): return x=np.sort(self.data) n=len(x) xcdf = np.arange(n,0,-1,dtype='float')/float(n) lcdf = self.lognormal_dist.sf(x) D_location = argmax(xcdf-lcdf) pylab.vlines(x[D_location],xcdf[D_location],lcdf[D_location],color='m',linewidth=2) pylab.plot(x, lcdf,',',**kwargs)
def sanitize_turbo(html, allowed_tags=TURBO_ALLOWED_TAGS, allowed_attrs=TURBO_ALLOWED_ATTRS): """Sanitizes HTML, removing not allowed tags and attributes. :param str|unicode html: :param list allowed_tags: List of allowed tags. :param dict allowed_attrs: Dictionary with attributes allowed for tags. :rtype: unicode """ return clean(html, tags=allowed_tags, attributes=allowed_attrs, strip=True)
def configure_analytics_yandex(self, ident, params=None): """Configure Yandex Metrika analytics counter. :param str|unicode ident: Metrika counter ID. :param dict params: Additional params. """ params = params or {} data = { 'type': 'Yandex', 'id': ident, } if params: data['params'] = '%s' % params self.analytics.append(data)
def tag_list(self, tags): """ Generates a list of tags identifying those previously selected. Returns a list of tuples of the form (<tag name>, <CSS class name>). Uses the string names rather than the tags themselves in order to work with tag lists built from forms not fully submitted. """ return [ (tag.name, "selected taggit-tag" if tag.name in tags else "taggit-tag") for tag in self.model.objects.all() ]
def gcd(self, lon1, lat1, lon2, lat2): """ Calculate the great circle distance between two points on the earth (specified in decimal degrees) """ # convert decimal degrees to radians lon1, lat1, lon2, lat2 = map(math.radians, [lon1, lat1, lon2, lat2]) # haversine formula dlon = lon2 - lon1 dlat = lat2 - lat1 a = math.sin(dlat / 2) ** 2 + math.cos(lat1) * math.cos(lat2) * math.sin(dlon / 2) ** 2 c = 2 * math.asin(math.sqrt(a)) dis = E.R * c return dis
def hash_md5(self): """Calculate md5 fingerprint. Shamelessly copied from http://stackoverflow.com/questions/6682815/deriving-an-ssh-fingerprint-from-a-public-key-in-python For specification, see RFC4716, section 4.""" fp_plain = hashlib.md5(self._decoded_key).hexdigest() return "MD5:" + ':'.join(a + b for a, b in zip(fp_plain[::2], fp_plain[1::2]))
def hash_sha256(self): """Calculate sha256 fingerprint.""" fp_plain = hashlib.sha256(self._decoded_key).digest() return (b"SHA256:" + base64.b64encode(fp_plain).replace(b"=", b"")).decode("utf-8")
def hash_sha512(self): """Calculates sha512 fingerprint.""" fp_plain = hashlib.sha512(self._decoded_key).digest() return (b"SHA512:" + base64.b64encode(fp_plain).replace(b"=", b"")).decode("utf-8")
def _unpack_by_int(self, data, current_position): """Returns a tuple with (location of next data field, contents of requested data field).""" # Unpack length of data field try: requested_data_length = struct.unpack('>I', data[current_position:current_position + self.INT_LEN])[0] except struct.error: raise MalformedDataError("Unable to unpack %s bytes from the data" % self.INT_LEN) # Move pointer to the beginning of the data field current_position += self.INT_LEN remaining_data_length = len(data[current_position:]) if remaining_data_length < requested_data_length: raise MalformedDataError( "Requested %s bytes, but only %s bytes available." % (requested_data_length, remaining_data_length) ) next_data = data[current_position:current_position + requested_data_length] # Move pointer to the end of the data field current_position += requested_data_length return current_position, next_data
def _parse_long(cls, data): """Calculate two's complement.""" if sys.version < '3': # this does not exist in python 3 - undefined-variable disabled to make pylint happier. ret = long(0) # pylint:disable=undefined-variable for byte in data: ret = (ret << 8) + ord(byte) else: ret = 0 for byte in data: ret = (ret << 8) + byte return ret
def decode_key(cls, pubkey_content): """Decode base64 coded part of the key.""" try: decoded_key = base64.b64decode(pubkey_content.encode("ascii")) except (TypeError, binascii.Error): raise MalformedDataError("Unable to decode the key") return decoded_key
def parse_options(self, options): """Parses ssh options string.""" quote_open = False parsed_options = {} def parse_add_single_option(opt): """Parses and validates a single option, and adds it to parsed_options field.""" if "=" in opt: opt_name, opt_value = opt.split("=", 1) opt_value = opt_value.replace('"', '') else: opt_name = opt opt_value = True if " " in opt_name or not self.OPTION_NAME_RE.match(opt_name): raise InvalidOptionNameError("%s is not valid option name." % opt_name) if self.strict_mode: for valid_opt_name, value_required in self.OPTIONS_SPEC: if opt_name.lower() == valid_opt_name: if value_required and opt_value is True: raise MissingMandatoryOptionValueError("%s is missing mandatory value." % opt_name) break else: raise UnknownOptionNameError("%s is unrecognized option name." % opt_name) if opt_name not in parsed_options: parsed_options[opt_name] = [] parsed_options[opt_name].append(opt_value) start_of_current_opt = 0 i = 1 # Need to be set for empty options strings for i, character in enumerate(options): if character == '"': # only double quotes are allowed, no need to care about single quotes quote_open = not quote_open if quote_open: continue if character == ",": opt = options[start_of_current_opt:i] parse_add_single_option(opt) start_of_current_opt = i + 1 # Data begins after the first space if start_of_current_opt + 1 != i: opt = options[start_of_current_opt:] parse_add_single_option(opt) if quote_open: raise InvalidOptionsError("Unbalanced quotes.") return parsed_options
def _process_ssh_rsa(self, data): """Parses ssh-rsa public keys.""" current_position, raw_e = self._unpack_by_int(data, 0) current_position, raw_n = self._unpack_by_int(data, current_position) unpacked_e = self._parse_long(raw_e) unpacked_n = self._parse_long(raw_n) self.rsa = RSAPublicNumbers(unpacked_e, unpacked_n).public_key(default_backend()) self.bits = self.rsa.key_size if self.strict_mode: min_length = self.RSA_MIN_LENGTH_STRICT max_length = self.RSA_MAX_LENGTH_STRICT else: min_length = self.RSA_MIN_LENGTH_LOOSE max_length = self.RSA_MAX_LENGTH_LOOSE if self.bits < min_length: raise TooShortKeyError( "%s key data can not be shorter than %s bits (was %s)" % (self.key_type, min_length, self.bits) ) if self.bits > max_length: raise TooLongKeyError( "%s key data can not be longer than %s bits (was %s)" % (self.key_type, max_length, self.bits) ) return current_position
def _process_ssh_dss(self, data): """Parses ssh-dsa public keys.""" data_fields = {} current_position = 0 for item in ("p", "q", "g", "y"): current_position, value = self._unpack_by_int(data, current_position) data_fields[item] = self._parse_long(value) q_bits = self._bits_in_number(data_fields["q"]) p_bits = self._bits_in_number(data_fields["p"]) if q_bits != self.DSA_N_LENGTH: raise InvalidKeyError("Incorrect DSA key parameters: bits(p)=%s, q=%s" % (self.bits, q_bits)) if self.strict_mode: min_length = self.DSA_MIN_LENGTH_STRICT max_length = self.DSA_MAX_LENGTH_STRICT else: min_length = self.DSA_MIN_LENGTH_LOOSE max_length = self.DSA_MAX_LENGTH_LOOSE if p_bits < min_length: raise TooShortKeyError("%s key can not be shorter than %s bits (was %s)" % (self.key_type, min_length, p_bits)) if p_bits > max_length: raise TooLongKeyError( "%s key data can not be longer than %s bits (was %s)" % (self.key_type, max_length, p_bits) ) dsa_parameters = DSAParameterNumbers(data_fields["p"], data_fields["q"], data_fields["g"]) self.dsa = DSAPublicNumbers(data_fields["y"], dsa_parameters).public_key(default_backend()) self.bits = self.dsa.key_size return current_position
def _process_ecdsa_sha(self, data): """Parses ecdsa-sha public keys.""" current_position, curve_information = self._unpack_by_int(data, 0) if curve_information not in self.ECDSA_CURVE_DATA: raise NotImplementedError("Invalid curve type: %s" % curve_information) curve, hash_algorithm = self.ECDSA_CURVE_DATA[curve_information] current_position, key_data = self._unpack_by_int(data, current_position) try: # data starts with \x04, which should be discarded. ecdsa_key = ecdsa.VerifyingKey.from_string(key_data[1:], curve, hash_algorithm) except AssertionError: raise InvalidKeyError("Invalid ecdsa key") self.bits = int(curve_information.replace(b"nistp", b"")) self.ecdsa = ecdsa_key return current_position
def _process_ed25516(self, data): """Parses ed25516 keys. There is no (apparent) way to validate ed25519 keys. This only checks data length (256 bits), but does not try to validate the key in any way.""" current_position, verifying_key = self._unpack_by_int(data, 0) verifying_key_length = len(verifying_key) * 8 verifying_key = self._parse_long(verifying_key) if verifying_key < 0: raise InvalidKeyError("ed25519 verifying key must be >0.") self.bits = verifying_key_length if self.bits != 256: raise InvalidKeyLengthError("ed25519 keys must be 256 bits (was %s bits)" % self.bits) return current_position
def parse(self, keydata=None): """Validates SSH public key. Throws exception for invalid keys. Otherwise returns None. Populates key_type, bits and bits fields. For rsa keys, see field "rsa" for raw public key data. For dsa keys, see field "dsa". For ecdsa keys, see field "ecdsa".""" if keydata is None: if self.keydata is None: raise ValueError("Key data must be supplied either in constructor or to parse()") keydata = self.keydata else: self.reset() self.keydata = keydata if keydata.startswith("---- BEGIN SSH2 PUBLIC KEY ----"): # SSH2 key format key_type = None # There is no redundant key-type field - skip comparing plain-text and encoded data. pubkey_content = "".join([line for line in keydata.split("\n") if ":" not in line and "----" not in line]) else: key_parts = self._split_key(keydata) key_type = key_parts[0] pubkey_content = key_parts[1] self._decoded_key = self.decode_key(pubkey_content) # Check key type current_position, unpacked_key_type = self._unpack_by_int(self._decoded_key, 0) if key_type is not None and key_type != unpacked_key_type.decode(): raise InvalidTypeError("Keytype mismatch: %s != %s" % (key_type, unpacked_key_type)) self.key_type = unpacked_key_type key_data_length = self._process_key(self._decoded_key[current_position:]) current_position = current_position + key_data_length if current_position != len(self._decoded_key): raise MalformedDataError("Leftover data: %s bytes" % (len(self._decoded_key) - current_position)) if self.disallow_options and self.options: raise InvalidOptionsError("Options are disallowed.")
def status_list(maj_status, min_status, status_type=C.GSS_C_GSS_CODE, mech_type=C.GSS_C_NO_OID): """ Creates a "friendly" error message from a GSS status code. This is used to create the :attr:`GSSCException.message` of a :class:`GSSCException`. :param maj_status: The major status reported by the C GSSAPI. :type maj_status: int :param min_status: The minor status reported by the C GSSAPI. :type min_status: int :param status_type: Whether the status is a general GSSAPI status or a mechanism status. :type status_type: ``GSS_C_GSS_CODE`` or ``GSS_C_MECH_CODE`` :param mech_type: Optional mechanism type, if the status is a mechanism status. :type mech_type: :class:`~gssapi.oids.OID` :returns: a list of strings describing the error. :rtype: list of strings """ from .oids import OID statuses = [] message_context = ffi.new('OM_uint32[1]') minor_status = ffi.new('OM_uint32[1]') if isinstance(mech_type, OID): mech_type = ffi.addressof(mech_type._oid) # OID._oid is type "struct gss_OID_desc" elif mech_type == C.GSS_C_NO_OID: mech_type = ffi.cast('gss_OID', C.GSS_C_NO_OID) elif not isinstance(mech_type, ffi.CData) or ffi.typeof(mech_type) != ffi.typeof('gss_OID'): raise TypeError( "Expected mech_type to be a gssapi.oids.OID or gss_OID, got {0}".format(type(mech_type)) ) while True: status_buf = ffi.new('gss_buffer_desc[1]') try: retval = C.gss_display_status( minor_status, maj_status, status_type, mech_type, message_context, status_buf ) if retval == C.GSS_S_COMPLETE: statuses.append("({0}) {1}.".format( maj_status, _buf_to_str(status_buf[0]).decode("utf-8", errors="replace") )) elif retval == C.GSS_S_BAD_MECH: statuses.append("Unsupported mechanism type passed to GSSException") break elif retval == C.GSS_S_BAD_STATUS: statuses.append("Unrecognized status value passed to GSSException") break finally: C.gss_release_buffer(minor_status, status_buf) if message_context[0] == 0: break if min_status: minor_status_msgs = status_list(min_status, 0, C.GSS_C_MECH_CODE, mech_type) if minor_status_msgs: statuses.append("Minor code:") statuses.extend(minor_status_msgs) return statuses
def canonicalize(self, mech): """ Create a canonical mechanism name (MechName) from an arbitrary internal name. The canonical MechName would be set as the :attr:`~gssapi.ctx.AcceptContext.peer_name` property on an acceptor's :class:`~gssapi.ctx.AcceptContext` if an initiator performed a successful authentication to the acceptor using the given mechanism, using a :class:`~gssapi.creds.Credential` obtained using this :class:`Name`. :param mech: The mechanism to canonicalize this name for :type mech: :class:`~gssapi.oids.OID` :returns: a canonical mechanism name based on this internal name. :rtype: :class:`MechName` """ if isinstance(mech, OID): oid = mech._oid else: raise TypeError("Expected an OID, got " + str(type(mech))) minor_status = ffi.new('OM_uint32[1]') out_name = ffi.new('gss_name_t[1]') try: retval = C.gss_canonicalize_name( minor_status, self._name[0], ffi.addressof(oid), out_name ) if GSS_ERROR(retval): raise _exception_for_status(retval, minor_status[0]) return MechName(out_name, mech) except: C.gss_release_name(minor_status, out_name)
def export(self): """ Returns a representation of the Mechanism Name which is suitable for direct string comparison against other exported Mechanism Names. Its form is defined in the GSSAPI specification (RFC 2743). It can also be re-imported by constructing a :class:`Name` with the `name_type` param set to :const:`gssapi.C_NT_EXPORT_NAME`. :returns: an exported bytestring representation of this mechanism name :rtype: bytes """ minor_status = ffi.new('OM_uint32[1]') output_buffer = ffi.new('gss_buffer_desc[1]') retval = C.gss_export_name( minor_status, self._name[0], output_buffer ) try: if GSS_ERROR(retval): if minor_status[0] and self._mech_type: raise _exception_for_status(retval, minor_status[0], self._mech_type) else: raise _exception_for_status(retval, minor_status[0]) return _buf_to_str(output_buffer[0]) finally: if output_buffer[0].length != 0: C.gss_release_buffer(minor_status, output_buffer)
def integrity_negotiated(self): """ After :meth:`step` has been called, this property will be set to True if integrity protection (signing) has been negotiated in this context, False otherwise. If this property is True, you can use :meth:`get_mic` to sign messages with a message integrity code (MIC), which the peer application can verify. """ return ( self.flags & C.GSS_C_INTEG_FLAG ) and ( self.established or (self.flags & C.GSS_C_PROT_READY_FLAG) )
def confidentiality_negotiated(self): """ After :meth:`step` has been called, this property will be set to True if confidentiality (encryption) has been negotiated in this context, False otherwise. If this property is True, you can use :meth:`wrap` with the `conf_req` param set to True to encrypt messages sent to the peer application. """ return ( self.flags & C.GSS_C_CONF_FLAG ) and ( self.established or (self.flags & C.GSS_C_PROT_READY_FLAG) )
def replay_detection_negotiated(self): """ After :meth:`step` has been called, this property will be set to True if the security context can use replay detection for messages protected by :meth:`get_mic` and :meth:`wrap`. False if replay detection cannot be used. """ return ( self.flags & C.GSS_C_REPLAY_FLAG ) and ( self.established or (self.flags & C.GSS_C_PROT_READY_FLAG) )
def sequence_detection_negotiated(self): """ After :meth:`step` has been called, this property will be set to True if the security context can use out-of-sequence message detection for messages protected by :meth:`get_mic` and :meth:`wrap`. False if OOS detection cannot be used. """ return ( self.flags & C.GSS_C_SEQUENCE_FLAG ) and ( self.established or (self.flags & C.GSS_C_PROT_READY_FLAG) )
def get_mic(self, message, qop_req=C.GSS_C_QOP_DEFAULT): """ Calculates a cryptographic message integrity code (MIC) over an application message, and returns that MIC in a token. This is in contrast to :meth:`wrap` which calculates a MIC over a message, optionally encrypts it and returns the original message and the MIC packed into a single token. The peer application can then verify the MIC to ensure the associated message has not been changed in transit. :param message: The message to calculate a MIC for :type message: bytes :param qop_req: The quality of protection required. It is recommended to not change this from the default as most GSSAPI implementations do not support it. :returns: A MIC for the message calculated using this security context's cryptographic keys :rtype: bytes """ if not (self.flags & C.GSS_C_INTEG_FLAG): raise GSSException("No integrity protection negotiated.") if not (self.established or (self.flags & C.GSS_C_PROT_READY_FLAG)): raise GSSException("Protection not yet ready.") minor_status = ffi.new('OM_uint32[1]') output_token_buffer = ffi.new('gss_buffer_desc[1]') message_buffer = ffi.new('gss_buffer_desc[1]') message_buffer[0].length = len(message) c_str_message = ffi.new('char[]', message) message_buffer[0].value = c_str_message retval = C.gss_get_mic( minor_status, self._ctx[0], ffi.cast('gss_qop_t', qop_req), message_buffer, output_token_buffer ) try: if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0]) output_token = _buf_to_str(output_token_buffer[0]) return output_token finally: if output_token_buffer[0].length != 0: C.gss_release_buffer(minor_status, output_token_buffer)
def verify_mic(self, message, mic, supplementary=False): """ Takes a message integrity code (MIC) that has been generated by the peer application for a given message, and verifies it against a message, using this security context's cryptographic keys. The `supplementary` parameter determines how this method deals with replayed, unsequential, too-old or missing tokens, as follows: If the `supplementary` parameter is False (the default), and if a replayed or otherwise out-of-sequence token is detected, this method raises a :exc:`~gssapi.error.GSSCException`. If no replay or out-of-sequence token is detected, this method does not raise an exception and returns the ``qop_state`` only. If `supplementary` is True, instead of raising an exception when a replayed or out-of-sequence token is detected, this method returns a tuple ``(qop_state, supplementary_info)`` where ``supplementary_info`` is a tuple containing zero or more of the constants :const:`~gssapi.S_DUPLICATE_TOKEN`, :const:`~gssapi.S_OLD_TOKEN`, :const:`~gssapi.S_UNSEQ_TOKEN` and :const:`~gssapi.S_GAP_TOKEN`. The supplementary info tells the caller whether a replayed or out-of-sequence message was detected. The caller must check this and decide how to handle the message if any of the flags are set. For a reference to the meaning of the flags, check `RFC 2744 Section 3.9.1 <http://tools.ietf.org/html/rfc2744#section-3.9.1>` for the corresponding GSS_S_OLD_TOKEN, etc, constants. :param message: The message the MIC was calculated for :type message: bytes :param mic: The MIC calculated by the peer :type mic: bytes :param supplementary: Whether to also return supplementary info. :type supplementary: bool :returns: ``qop_state`` if `supplementary` is False, or ``(qop_state, supplementary_info)`` if `supplementary` is True. :raises: :exc:`~gssapi.error.GSSException` if :attr:`integrity_negotiated` is false, or :exc:`~gssapi.error.GSSCException` if the verification fails indicating the message was modified, replayed or out-of-sequence. """ if not (self.flags & C.GSS_C_INTEG_FLAG): raise GSSException("No integrity protection negotiated.") if not (self.established or (self.flags & C.GSS_C_PROT_READY_FLAG)): raise GSSException("Protection not yet ready.") minor_status = ffi.new('OM_uint32[1]') message_buffer = ffi.new('gss_buffer_desc[1]') message_buffer[0].length = len(message) c_str_message = ffi.new('char[]', message) message_buffer[0].value = c_str_message mic_buffer = ffi.new('gss_buffer_desc[1]') mic_buffer[0].length = len(mic) c_str_mic = ffi.new('char[]', mic) mic_buffer[0].value = c_str_mic qop_state = ffi.new('gss_qop_t[1]') retval = C.gss_verify_mic( minor_status, self._ctx[0], message_buffer, mic_buffer, qop_state ) if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0]) supp_bits = _status_bits(retval) if supplementary: return qop_state[0], supp_bits elif len(supp_bits) > 0: # Raise if unseq/replayed token detected raise _exception_for_status(retval, minor_status[0]) else: return qop_state[0]
def wrap(self, message, conf_req=True, qop_req=C.GSS_C_QOP_DEFAULT): """ Wraps a message with a message integrity code, and if `conf_req` is True, encrypts the message. The message can be decrypted and the MIC verified by the peer by passing the token returned from this method to :meth:`unwrap` on the peer's side. :param message: The message to wrap :type message: bytes :param conf_req: Whether to require confidentiality (encryption) :type conf_req: bool :param qop_req: The quality of protection required. It is recommended to not change this from the default as most GSSAPI implementations do not support it. :returns: the wrapped message in a token suitable for passing to :meth:`unwrap` :rtype: bytes :raises: GSSException if integrity protection is not available (:attr:`integrity_negotiated` is False), or if the `conf_req` parameter is True and confidentiality protection is not available (:attr:`confidentiality_negotiated` is False) """ if not (self.flags & C.GSS_C_INTEG_FLAG): raise GSSException("No integrity protection negotiated.") if (conf_req and not (self.flags & C.GSS_C_CONF_FLAG)): raise GSSException("No confidentiality protection negotiated.") if not (self.established or (self.flags & C.GSS_C_PROT_READY_FLAG)): raise GSSException("Protection not yet ready.") minor_status = ffi.new('OM_uint32[1]') output_token_buffer = ffi.new('gss_buffer_desc[1]') message_buffer = ffi.new('gss_buffer_desc[1]') message_buffer[0].length = len(message) c_str_message = ffi.new('char[]', message) message_buffer[0].value = c_str_message conf_state = ffi.new('int[1]') retval = C.gss_wrap( minor_status, self._ctx[0], ffi.cast('int', conf_req), ffi.cast('gss_qop_t', qop_req), message_buffer, conf_state, output_token_buffer ) try: if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0]) output_token = _buf_to_str(output_token_buffer[0]) if conf_req and not conf_state[0]: raise GSSException("No confidentiality protection.") return output_token finally: if output_token_buffer[0].length != 0: C.gss_release_buffer(minor_status, output_token_buffer)
def unwrap(self, message, conf_req=True, qop_req=None, supplementary=False): """ Takes a token that has been generated by the peer application with :meth:`wrap`, verifies and optionally decrypts it, using this security context's cryptographic keys. The `supplementary` parameter determines how this method deals with replayed, unsequential, too-old or missing tokens, as follows: If the `supplementary` parameter is False (the default), and if a replayed or otherwise out-of-sequence token is detected, this method raises a :exc:`~gssapi.error.GSSCException`. If no replay or out-of-sequence token is detected, this method returns the unwrapped message only. If `supplementary` is True, instead of raising an exception when a replayed or out-of-sequence token is detected, this method returns a tuple ``(unwrapped_message, supplementary_info)`` where ``supplementary_info`` is a tuple containing zero or more of the constants :const:`~gssapi.S_DUPLICATE_TOKEN`, :const:`~gssapi.S_OLD_TOKEN`, :const:`~gssapi.S_UNSEQ_TOKEN` and :const:`~gssapi.S_GAP_TOKEN`. The supplementary info tells the caller whether a replayed or out-of-sequence message was detected. The caller must check this and decide how to handle the message if any of the flags are set. For a reference to the meaning of the flags, check `RFC 2744 Section 3.9.1 <http://tools.ietf.org/html/rfc2744#section-3.9.1>` for the corresponding GSS_S_OLD_TOKEN, etc, constants. :param message: The wrapped message token :type message: bytes :param conf_req: Whether to require confidentiality (encryption) :type conf_req: bool :param qop_req: The quality of protection required. It is recommended to not change this from the default None as most GSSAPI implementations do not support it. :param supplementary: Whether to also return supplementary info. :type supplementary: bool :returns: the verified and decrypted message if `supplementary` is False, or a tuple ``(unwrapped_message, supplementary_info)`` if `supplementary` is True. :raises: :exc:`~gssapi.error.GSSException` if :attr:`integrity_negotiated` is false, or if the verification or decryption fails, if the message was modified, or if confidentiality was required (`conf_req` was True) but the message did not have confidentiality protection applied (was not encrypted), or if the `qop_req` parameter was set and it did not match the QOP applied to the message, or if a replayed or out-of-sequence message was detected. """ if not (self.flags & C.GSS_C_INTEG_FLAG): raise GSSException("No integrity protection negotiated.") if not (self.established or (self.flags & C.GSS_C_PROT_READY_FLAG)): raise GSSException("Protection not yet ready.") minor_status = ffi.new('OM_uint32[1]') output_buffer = ffi.new('gss_buffer_desc[1]') message_buffer = ffi.new('gss_buffer_desc[1]') message_buffer[0].length = len(message) c_str_message = ffi.new('char[]', message) message_buffer[0].value = c_str_message conf_state = ffi.new('int[1]') qop_state = ffi.new('gss_qop_t[1]') retval = C.gss_unwrap( minor_status, self._ctx[0], message_buffer, output_buffer, conf_state, qop_state ) try: if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0]) output = _buf_to_str(output_buffer[0]) if conf_req and not conf_state[0]: raise GSSException("No confidentiality protection.") if qop_req is not None and qop_req != qop_state[0]: raise GSSException("QOP {0} does not match required value {1}.".format(qop_state[0], qop_req)) supp_bits = _status_bits(retval) if supplementary: return output, supp_bits elif len(supp_bits) > 0: # Raise if unseq/replayed token detected raise _exception_for_status(retval, minor_status[0], token=output) else: return output finally: if output_buffer[0].length != 0: C.gss_release_buffer(minor_status, output_buffer)
def get_wrap_size_limit(self, output_size, conf_req=True, qop_req=C.GSS_C_QOP_DEFAULT): """ Calculates the maximum size of message that can be fed to :meth:`wrap` so that the size of the resulting wrapped token (message plus wrapping overhead) is no more than a given maximum output size. :param output_size: The maximum output size (in bytes) of a wrapped token :type output_size: int :param conf_req: Whether to calculate the wrapping overhead for confidentiality protection (if True) or just integrity protection (if False). :type conf_req: bool :returns: The maximum input size (in bytes) of message that can be passed to :meth:`wrap` :rtype: int """ minor_status = ffi.new('OM_uint32[1]') max_input_size = ffi.new('OM_uint32[1]') retval = C.gss_wrap_size_limit( minor_status, self._ctx[0], ffi.cast('int', conf_req), ffi.cast('gss_qop_t', qop_req), ffi.cast('OM_uint32', output_size), max_input_size ) if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0]) return max_input_size[0]
def process_context_token(self, context_token): """ Provides a way to pass an asynchronous token to the security context, outside of the normal context-establishment token passing flow. This method is not normally used, but some example uses are: * when the initiator's context is established successfully but the acceptor's context isn't and the acceptor needs to signal to the initiator that the context shouldn't be used. * if :meth:`delete` on one peer's context returns a final token that can be passed to the other peer to indicate the other peer's context should be torn down as well (though it's recommended that :meth:`delete` should return nothing, i.e. this method should not be used by GSSAPI mechanisms). :param context_token: The context token to pass to the security context :type context_token: bytes :raises: :exc:`~gssapi.error.DefectiveToken` if consistency checks on the token failed. :exc:`~gssapi.error.NoContext` if this context is invalid. :exc:`~gssapi.error.GSSException` for any other GSSAPI errors. """ minor_status = ffi.new('OM_uint32[1]') context_token_buffer = ffi.new('gss_buffer_desc[1]') context_token_buffer[0].length = len(context_token) c_str_context_token = ffi.new('char[]', context_token) context_token_buffer[0].value = c_str_context_token retval = C.gss_process_context_token( minor_status, self._ctx[0], context_token_buffer ) if GSS_ERROR(retval): if minor_status[0] and self.mech_type: raise _exception_for_status(retval, minor_status[0], self.mech_type) else: raise _exception_for_status(retval, minor_status[0])