Exemple #1
0
def do_concat (shape1, shape2, iaxis, count=[0]):
  from pygeode.axis import XAxis, YAxis, ZAxis, TAxis
  from pygeode.var import Var
  from pygeode.concat import concat
  from var_test import varTest
  import numpy as np

  # Increment test counter (and generate a unique test name)
  count[0] += 1
  testname = "concattest%05d"%(count[0])

  # Create some test data
  np.random.seed(count[0])
  array1 = np.random.randn(*shape1)
  array2 = np.random.randn(*shape2)

  # Get the # of dimensions, and assign a unique axis class for each dim
  assert array1.ndim == array2.ndim
  ndim = array1.ndim
  axis_classes = (XAxis, YAxis, ZAxis, TAxis)[:ndim]

  # Construct the first var
  axes = [cls(n) for cls,n in zip(axis_classes,array1.shape)]
  var1 = Var(axes = axes, values = array1, name = "myvar", atts={'a':1, 'b':2, 'c':3})

  # The second var should have the same axes, except for the concatenation one
  n1 = array1.shape[iaxis]
  n2 = array2.shape[iaxis]
  axes[iaxis] = axis_classes[iaxis](np.arange(n1, n1+n2))
  var2 = Var(axes = axes, values = array2, name = "myvar", atts={'a':1, 'b':3, 'd':4})

  # Try concatenating
  var = concat(var1,var2)

  # The expected result
  expected = np.concatenate ( (array1, array2), iaxis)

  # Test this
  test = varTest(testname=testname, var=var, values=expected)

  # Store this test
  globals()[testname] = test
Exemple #2
0
def do_concat (shape1, shape2, iaxis, count=[0]):
  from pygeode.axis import XAxis, YAxis, ZAxis, TAxis
  from pygeode.var import Var
  from pygeode.concat import concat
  from var_test import varTest
  import numpy as np

  # Increment test counter (and generate a unique test name)
  count[0] += 1
  testname = "concattest%05d"%(count[0])

  # Create some test data
  np.random.seed(count[0])
  array1 = np.random.randn(*shape1)
  array2 = np.random.randn(*shape2)

  # Get the # of dimensions, and assign a unique axis class for each dim
  assert array1.ndim == array2.ndim
  ndim = array1.ndim
  axis_classes = (XAxis, YAxis, ZAxis, TAxis)[:ndim]

  # Construct the first var
  axes = [cls(n) for cls,n in zip(axis_classes,array1.shape)]
  var1 = Var(axes = axes, values = array1, name = "myvar", atts={'a':1, 'b':2, 'c':3})

  # The second var should have the same axes, except for the concatenation one
  n1 = array1.shape[iaxis]
  n2 = array2.shape[iaxis]
  axes[iaxis] = axis_classes[iaxis](np.arange(n1, n1+n2))
  var2 = Var(axes = axes, values = array2, name = "myvar", atts={'a':1, 'b':3, 'd':4})

  # Try concatenating
  var = concat(var1,var2)

  # The expected result
  expected = np.concatenate ( (array1, array2), iaxis)

  # Test this
  test = varTest(testname=testname, var=var, values=expected)

  # Store this test
  globals()[testname] = test
Exemple #3
0
def concat(*datasets):
    # {{{
    from pygeode.concat import concat
    from pygeode.tools import common_dict, islist

    # Did we get passed a list of datasets already?
    # (need to break out of the outer list)
    if len(datasets) == 1 and islist(datasets[0]): datasets = list(datasets[0])

    #  If we only have one dataset, then return it
    if len(datasets) == 1: return datasets[0]

    # Collect a list of variable names (in the order they're found in the datasets)
    # and, a corresponding dictionary mapping the names to vars

    varnames = []
    vardict = {}
    for dataset in datasets:
        for v in dataset.vars:
            if v.name not in vardict:
                vardict[v.name] = []
                varnames.append(v.name)
            vardict[v.name].append(v)

    # Merge the var segments together
    # If only one segment, just use the variable itself
    vars = [vardict[n] for n in varnames]
    vars = [concat(v) if len(v) > 1 else v[0] for v in vars]

    d = Dataset(vars)

    # Keep any common global attributes
    # Collect all attributes found in the datasets
    atts = common_dict(*[x.atts for x in datasets])
    if len(atts) > 0:
        d.atts = atts
    return d
Exemple #4
0
def concat(*datasets):
  from pygeode.concat import concat
  from pygeode.tools import common_dict, islist

  # Did we get passed a list of datasets already?
  # (need to break out of the outer list)
  if len(datasets) == 1 and islist(datasets[0]): datasets = list(datasets[0])

  #  If we only have one dataset, then return it
  if len(datasets) == 1: return datasets[0]

  # Collect a list of variable names (in the order they're found in the datasets)
  # and, a corresponding dictionary mapping the names to vars

  varnames = []
  vardict = {}
  for dataset in datasets:
    for v in dataset.vars:
      if v.name not in vardict:
        vardict[v.name] = []
        varnames.append(v.name)
      vardict[v.name].append(v)

  # Merge the var segments together
  # If only one segment, just use the variable itself
  vars = [vardict[n] for n in varnames]
  vars = [concat(v) if len(v)>1 else v[0] for v in vars]

  d = Dataset(vars)

  # Keep any common global attributes
  # Collect all attributes found in the datasets
  atts = common_dict(*[x.atts for x in datasets])
  if len(atts) > 0:
    d.atts = atts
  return d