def array_convert_function(sshape_one, sshape_two, variables): """ Return a function defining the conversion process between two NumPy arrays of different shapes """ if not isinstance(sshape_one, tuple): sshape_one = (sshape_one,) if not isinstance(sshape_two, tuple): sshape_two = (sshape_two,) s_one = flatten([eval_expr_names_and_nrs(d) if isinstance(d,str) else d for d in sshape_one]) s_two = flatten([eval_expr_names_and_nrs(d) if isinstance(d,str) else d for d in sshape_two]) if len(s_one) != len(s_two): raise ValueError, ('Flattened shapes %s and %s '\ 'do not have the same length. ' 'Original shapes were %s and %s') % \ (s_one, s_two, sshape_one, sshape_two) # Reason about the transpose t_idx = tuple([s_one.index(v) for v in s_two]) # Figure out the actual numeric shape values to use n_one = shape_from_str_tuple(s_one, variables) n_two = [eval_expr(d,variables) if isinstance(d,str) else d for d in sshape_two] def f(ary): return np.reshape(ary, n_one).transpose(t_idx).reshape(n_two) return f
def array_convert_function(sshape_one, sshape_two, variables): """ Return a function defining the conversion process between two NumPy arrays of different shapes """ if not isinstance(sshape_one, tuple): sshape_one = (sshape_one, ) if not isinstance(sshape_two, tuple): sshape_two = (sshape_two, ) s_one = flatten([ eval_expr_names_and_nrs(d) if isinstance(d, str) else d for d in sshape_one ]) s_two = flatten([ eval_expr_names_and_nrs(d) if isinstance(d, str) else d for d in sshape_two ]) if len(s_one) != len(s_two): raise ValueError, ('Flattened shapes %s and %s '\ 'do not have the same length. ' 'Original shapes were %s and %s') % \ (s_one, s_two, sshape_one, sshape_two) # Reason about the transpose t_idx = tuple([s_one.index(v) for v in s_two]) # Figure out the actual numeric shape values to use n_one = shape_from_str_tuple(s_one, variables) n_two = [ eval_expr(d, variables) if isinstance(d, str) else d for d in sshape_two ] def f(ary): return np.reshape(ary, n_one).transpose(t_idx).reshape(n_two) return f
def shape_from_str_tuple(sshape, variables, ignore=None): """ Substitutes string values in the supplied shape parameter with integer variables stored in a dictionary Parameters ---------- sshape : tuple/string composed of integers and strings. The strings should related to integral properties registered with this Solver object variables : dictionary Keys with associated integer values. Used to replace string values within the tuple ignore : list A list of tuple strings to ignore >>> print self.shape_from_str_tuple((4,'na','ntime'),ignore=['ntime']) (4, 3) """ if ignore is None: ignore = [] if not isinstance(sshape, tuple) and not isinstance(sshape, list): raise TypeError, 'sshape argument must be a tuple or list' if not isinstance(ignore, list): raise TypeError, 'ignore argument must be a list' return tuple([int(eval_expr(v,variables)) if isinstance(v,str) else int(v) for v in sshape if v not in ignore])
def shape_from_str_tuple(sshape, variables, ignore=None): """ Substitutes string values in the supplied shape parameter with integer variables stored in a dictionary Parameters ---------- sshape : tuple/string composed of integers and strings. The strings should related to integral properties registered with this Solver object variables : dictionary Keys with associated integer values. Used to replace string values within the tuple ignore : list A list of tuple strings to ignore >>> print self.shape_from_str_tuple((4,'na','ntime'),ignore=['ntime']) (4, 3) """ if ignore is None: ignore = [] if not isinstance(sshape, tuple) and not isinstance(sshape, list): raise TypeError, 'sshape argument must be a tuple or list' if not isinstance(ignore, list): raise TypeError, 'ignore argument must be a list' return tuple([ int(eval_expr(v, variables)) if isinstance(v, str) else int(v) for v in sshape if v not in ignore ])