Beispiel #1
0
  def __init__ (self, var, indices):
  # {{{
    from pygeode.var import Var
    import numpy as np
    from pygeode.tools import combine_axes, common_dtype
    # Are we given a list of variables to work on in parallel?
    if isinstance(var,(tuple,list)):
      axes = combine_axes(var)
      dtype = common_dtype(var)
    else:
      axes = var.axes
      dtype = var.dtype

#    if not isinstance(indices,(list,tuple)): indices = [indices]
    indices = np.sort([var.whichaxis(i) for i in indices])
    assert len(indices) > 0, "no reduction axes specified"

    N = [len(axes[i]) for i in indices]
    # Check for degenerate reductions (ill-defined)
    for i,n in enumerate(N):
      if n == 0:  raise ValueError("Can't do a reduction over axis '%s' - length is 0."%axes[i].name)
    N = int(np.product(N))
    self.N =  N # number of values to reduce over
    self.var = var
    self.indices = indices

    self.in_axes = axes

    # Remove the reduction axis from the output variable
    axes = [a for i,a in enumerate(axes) if i not in indices]

    Var.__init__(self, axes, dtype=dtype, name=var.name, atts=var.atts, plotatts=var.plotatts)
Beispiel #2
0
  def __init__ (self, var, indices):
  # {{{
    from pygeode.var import Var
    import numpy as np
    from pygeode.tools import combine_axes, common_dtype
    # Are we given a list of variables to work on in parallel?
    if isinstance(var,(tuple,list)):
      axes = combine_axes(var)
      dtype = common_dtype(var)
    else:
      axes = var.axes
      dtype = var.dtype

#    if not isinstance(indices,(list,tuple)): indices = [indices]
    indices = np.sort([var.whichaxis(i) for i in indices])
    assert len(indices) > 0, "no reduction axes specified"

    N = [len(axes[i]) for i in indices]
    # Check for degenerate reductions (ill-defined)
    for i,n in enumerate(N):
      if n == 0:  raise ValueError("Can't do a reduction over axis '%s' - length is 0."%axes[i].name)
    N = int(np.product(N))
    self.N =  N # number of values to reduce over
    self.var = var
    self.indices = indices

    self.in_axes = axes

    # Remove the reduction axis from the output variable
    axes = [a for i,a in enumerate(axes) if i not in indices]

    Var.__init__(self, axes, dtype=dtype, name=var.name, atts=var.atts, plotatts=var.plotatts)
Beispiel #3
0
  def __init__(self, vars, iaxis=None):
    import pygeode.axis
    from pygeode.tools import common_dtype
    from pygeode.var import combine_meta
    import numpy as np

    # Use first var segment for the axes
    axes = list(vars[0].axes)
    naxes = len(axes)
    # For now, assume all segments have the same order of axes
    assert all(v.naxes == naxes for v in vars)
    for i in range(naxes):
      assert all(axes[i].isparentof(v.axes[i]) for v in vars)

    if iaxis is None:
      iaxis = set(i for v in vars for i in range(naxes) if v.axes[i] not in axes)
  
      assert len(iaxis) <= 1, "more than one varying axis id=%s for %s; can't concatenate"%(iaxis,repr(vars[0]))
  
      # Degenerate case: all segments have identical axes
      if len(iaxis) == 0:
        from warnings import warn
        warn ('all axes are identical.  Creating a fake "concat" axis', stacklevel=2)
        iaxis = naxes
        axes.append(pygeode.axis.NamedAxis(len(vars), name='concat'))
  
      # Standard case: exactly one concatenation axis
      else:
        iaxis = iaxis.pop()

    if not iaxis is naxes:
      # Get a numerical dimension number
      iaxis = vars[0].whichaxis(iaxis)

      # Update the list of axes with the concatenated axis included
      axes[iaxis] = pygeode.axis.concat([v.axes[iaxis] for v in vars])

    # Get the data type
    dtype = common_dtype(vars)

    Var.__init__(self, axes, dtype=dtype)

    # Grab metadata from the input variables
    combine_meta (vars, self)

#    # Assign a name (and other attributes??) to the var
#    name = set(v.name for v in vars if v.name != '')
#    if len(name) == 1: self.name = name.pop()
#
#    # Combine the attributes (if applicable)
#    atts = common_dict([v.atts for v in vars])
#    self.atts = atts
#    # Combine the plot attributes (if applicable)
#    plotatts = common_dict([v.plotatts for v in vars])
#    self.plotatts = plotatts

    # Other stuff
    self.vars = vars
    self.iaxis = iaxis
Beispiel #4
0
    def __init__(self, vars, iaxis=None):
        import pygeode.axis
        from pygeode.tools import common_dtype
        from pygeode.var import combine_meta
        import numpy as np

        # Use first var segment for the axes
        axes = list(vars[0].axes)
        naxes = len(axes)
        # For now, assume all segments have the same order of axes
        assert all(v.naxes == naxes for v in vars)
        for i in range(naxes):
            assert all(axes[i].isparentof(v.axes[i]) for v in vars)

        if iaxis is None:
            iaxis = set(i for v in vars for i in range(naxes)
                        if v.axes[i] not in axes)

            assert len(
                iaxis
            ) <= 1, "more than one varying axis id=%s for %s; can't concatenate" % (
                iaxis, repr(vars[0]))

            # Degenerate case: all segments have identical axes
            if len(iaxis) == 0:
                from warnings import warn
                warn('all axes are identical.  Creating a fake "concat" axis',
                     stacklevel=2)
                iaxis = naxes
                axes.append(pygeode.axis.NamedAxis(len(vars), name='concat'))

            # Standard case: exactly one concatenation axis
            else:
                iaxis = iaxis.pop()

        if not iaxis is naxes:
            # Get a numerical dimension number
            iaxis = vars[0].whichaxis(iaxis)

            # Update the list of axes with the concatenated axis included
            axes[iaxis] = pygeode.axis.concat([v.axes[iaxis] for v in vars])

        # Get the data type
        dtype = common_dtype(vars)

        Var.__init__(self, axes, dtype=dtype)

        # Grab metadata from the input variables
        combine_meta(vars, self)

        #    # Assign a name (and other attributes??) to the var
        #    name = set(v.name for v in vars if v.name != '')
        #    if len(name) == 1: self.name = name.pop()
        #
        #    # Combine the attributes (if applicable)
        #    atts = common_dict([v.atts for v in vars])
        #    self.atts = atts
        #    # Combine the plot attributes (if applicable)
        #    plotatts = common_dict([v.plotatts for v in vars])
        #    self.plotatts = plotatts

        # Other stuff
        self.vars = vars
        self.iaxis = iaxis