Decorator to check the distribution of return values of a
    function with dimensions.
170430
def hist_calls_with_dims(**dims):
    
    def hist_wrapper(fn):
        @functools.wraps(fn)
        def fn_wrapper(*args, **kwargs):
            _histogram = histogram(
                "%s_calls" % pyformance.registry.get_qualname(fn), **dims)
            rtn = fn(*args, **kwargs)
            if type(rtn) in (int, float):
                _histogram.add(rtn)
            return rtn
        return fn_wrapper
    return hist_wrapper
1070057
def _all_dims(x, default_dims=None):
  """"""
  if x.get_shape().ndims is not None:
    return list(xrange(x.get_shape().ndims))
  else:
    return default_dims
357930
def _ndefs(f):
    '''
        
    '''
    if isinstance(f, Function):
        return f.ndefs
    spec = inspect.getfullargspec(f)
    if spec.defaults is None:
        return 0
    return len(spec.defaults)
313449
def _check_var(var, dtype, ndmin, name, shape=None, shape2=None):
    """
    if var is None:
        raise ValueError
    var = np.array(var, dtype=dtype, copy=True, ndmin=ndmin)
    if shape:
        _check_shape(var, name, shape, shape2)
    return var
1129318
def std(self) -> Optional[float]:  #, ddof=0):
        
        # TODO: Add DOF
        if self._stats:
            return np.sqrt(self.variance())
        else:
            return None
20067
def _check_decorator(fct):
        """"""
        def wrapper(self, *args, **kw):
            if self.is_enable():
                ret = fct(self, *args, **kw)
            else:
                ret = self.stats
            return ret
        return wrapper
1211773
def scalarcoords(self):
        """"""
        return {k: v.values for k, v in self.coords.items() if v.dims==()}
1282811
def invcdf(x):
    """"""
    x_flat = np.ravel(x)
    x_trans = np.array([flib.ppnd16(y, 1) for y in x_flat])
    return np.reshape(x_trans, np.shape(x))
270774
def validate(**vkargs):
    
    def decorator(func):
        def wrapper(**kargs):
            for key, value in six.iteritems(vkargs):
                if key not in kargs:
                    abort(403, 'Missing parameter: %s' % key)
                try:
                    kargs[key] = value(kargs[key])
                except ValueError:
                    abort(403, 'Wrong parameter format for: %s' % key)
            return func(**kargs)
        return wrapper
    return decorator
985750
def _F_measure(self, alpha, TP, FP, FN, return_num_den=False):
        """"""
        num = np.float64(TP)
        den = np.float64(alpha * (TP + FP) + (1 - alpha) * (TP + FN))
        with np.errstate(divide='ignore', invalid='ignore'):
            F_measure = num/den
        #F_measure = num/den

        if return_num_den:
            return F_measure, num, den
        else:
            return F_measure
744532
def _random_helper(random, sampler, params, shape, dtype, kwargs):
    """"""
    if isinstance(params[0], Symbol):
        for i in params[1:]:
            assert isinstance(i, Symbol), \
                "Distribution parameters must all have the same type, but got " \
                "both %s and %s."%(type(params[0]), type(i))
        return sampler(*params, shape=shape, dtype=dtype, **kwargs)
    elif isinstance(params[0], numeric_types):
        for i in params[1:]:
            assert isinstance(i, numeric_types), \
                "Distribution parameters must all have the same type, but got " \
                "both %s and %s."%(type(params[0]), type(i))
        return random(*params, shape=shape, dtype=dtype, **kwargs)

    raise ValueError("Distribution parameters must be either Symbol or numbers, "
                     "but got %s."%type(params[0]))
1003662
def SpiceUDFUNS(f):
    

    @functools.wraps(f)
    def wrapping_udfuns(x, value):
        result = f(x)
        value[0] = c_double(result)

    return UDFUNS(wrapping_udfuns)
80825
def _wrapper(func, *vect_args, **vect_kwargs):
        """"""
        if not hasattr(func, '__name__'):
            # Set name if not available. Happens if func is actually a function
            func.__name__ = '{}.__call__'.format(func.__class__.__name__)

        return wraps(func)(_NumpyVectorizeWrapper(func, *vect_args,
                                                  **vect_kwargs))
81649
def _getFuncArgs(func):
  
  """
  code = func.func_code
  Defaults = func.func_defaults

  nargs = code.co_argcount
  ArgNames = code.co_varnames[:nargs]

  Args = OrderedDict()
  argCount = len(ArgNames)
  defCount = len(Defaults) if Defaults else 0
  diff = argCount - defCount

  for i in range(0, diff):
    Args[ArgNames[i]] = {}

  for i in range(diff, argCount):
    Args[ArgNames[i]] = {'default': Defaults[i - diff]}

  return Args
457364
def somenan(self, x, fun, p=0.1):
        """"""
        if np.random.rand(1) < p:
            return np.NaN
        else:
            return fun(x)
1309357
def cache_return(func):
    """"""

    _cache = []

    def wrap():
        if not _cache:
            _cache.append(func())
        return _cache[0]
    return wrap
840656
def _valid_dimensions(self, dimensions):
        """"""
        if dimensions is None:
            dimensions = self.kdims
        elif not isinstance(dimensions, list):
            dimensions = [dimensions]

        valid_dimensions = []
        for dim in dimensions:
            if isinstance(dim, Dimension): dim = dim.name
            if dim not in self.kdims:
                raise Exception("Supplied dimensions %s not found." % dim)
            valid_dimensions.append(dim)
        return valid_dimensions