Utility function for processing arguments that are singletons or lists.

    Args:
      x: either a list of self.n elements, or not a list.

    Returns:
      a list of self.n elements.
965673
def _maybe_repeat(self, x):
    
    if isinstance(x, list):
      assert len(x) == self.n
      return x
    else:
      return [x] * self.n
148270
def n_elements(self):
        
        if self.is_array:
            return functools.reduce(operator.mul, self.array_dimensions)
        else:
            return 1
1308216
def _to_list(var):
    
    if isinstance(var, list):
        return var
    elif var is None:
        return []
    elif isinstance(var, str) or isinstance(var, dict):
        # We dont want to make a list out of those via the default constructor
        return [var]
    else:
        try:
            return list(var)
        except TypeError:
            return [var]
710678
def _get_instances(self, instance):
        

        return [
            x
            for x in self.tree.body
            if isinstance(x, instance)
        ]
198741
def _parse_var_array(self, X: np.ndarray) -> dict:
        
        arg_vars = {}
        # Get the shape and tensor rank
        shape = X.shape
        tensor_rank = len(shape)
        T = self._check_forward_mode_input_array(X)
        if tensor_rank == 0:
            #there is only 1 var name, so this will only run once
            for var_name in self.var_names:
                arg_vars[var_name] = X
        if tensor_rank == 1 and T == shape[0]:
            #there is only 1 var name, so this will only run once
            for var_name in self.var_names:
                arg_vars[var_name] = X.squeeze()
        elif tensor_rank == 1:
            for j, var_name in enumerate(sorted(self.var_names)):
                arg_vars[var_name] = X[j].squeeze()
        elif tensor_rank == 2:
            for j, var_name in enumerate(sorted(self.var_names)):
                arg_vars[var_name] = X[:,j].squeeze()
        return arg_vars
1220126
def list_of(cls):
    
    return lambda l: isinstance(l, list) and all(isinstance(x, cls) for x in l)
149752
def singleton(cls):
    

    # the only way to implement nonlocal closure variables in Python 2.X
    instances = {}

    def get_instance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]

    return get_instance
594024
def _X_selected(X, selected):
    """"""
    n_features = X.shape[1]
    ind = np.arange(n_features)
    sel = np.zeros(n_features, dtype=bool)
    sel[np.asarray(selected)] = True
    non_sel = np.logical_not(sel)
    n_selected = np.sum(sel)
    X_sel = X[:, ind[sel]]
    X_not_sel = X[:, ind[non_sel]]
    return X_sel, X_not_sel, n_selected, n_features
881254
def ProbGreater(self, x):
        
        t = [prob for (val, prob) in self.d.iteritems() if val > x]
        return sum(t)
74641
def Vaknin (x):
  
  # Preallocate array with 2 more rows than input array
  x_new = np.zeros((x.shape[0]+2, x.shape[1]))
  # print x_new.shape
  # Duplicate first and last row of x into first and last row of x_new
  x_new[0, :] = x[0, :]
  x_new[-1, :] = x[-1, :]
  # Duplicate all of x into middle rows of x_neww
  x_new[1:-1, :] = x
  return x_new
398825
def instances(cls):
        
        l = [i for i in cls.allinstances() if isinstance(i, cls)]
        return l
1061906
def index(self, value):
        

        for i, x in enumerate(self):
            if x == value:
                return i

        raise ValueError("{} is not in list".format(value))
329558
def singleton(cls):
    
    INSTANCES = {}

    def _singleton(*args, **kwargs):
        if cls not in INSTANCES:
            INSTANCES[cls] = cls(*args, **kwargs)
        return INSTANCES[cls]

    return _singleton
135447
def preprocess_xarray(func):
    
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        args = tuple(a.metpy.unit_array if isinstance(a, xr.DataArray) else a for a in args)
        kwargs = {name: (v.metpy.unit_array if isinstance(v, xr.DataArray) else v)
                  for name, v in kwargs.items()}
        return func(*args, **kwargs)
    return wrapper
1173910
def Iaxn(mt, x, n, *args):
    
    return (Sx(mt, x + 1) - Sx(mt, x + n + 1) - n * Nx(mt, x + n + 1)) / Dx(mt, x)
619633
def values(self, index):
        
        values = []
        for i in xrange(self.num_instances):
            inst = self.get_instance(i)
            values.append(inst.get_value(index))
        return numpy.array(values)
192686
def isinstance_(x, A_tuple):
    
    if is_union(A_tuple):
        return any(isinstance_(x, t) for t in A_tuple.__args__)
    elif getattr(A_tuple, '__origin__', None) is not None:
        return isinstance(x, A_tuple.__origin__)
    else:
        return isinstance(x, A_tuple)