Ejemplo n.º 1
0
  /**
   * Appends the given argument frameblock 'that' to this frameblock by creating a deep copy to
   * prevent side effects. For cbind, the frames are appended column-wise (same number of rows),
   * while for rbind the frames are appended row-wise (same number of columns).
   *
   * @param that
   * @param ret
   * @param cbind
   * @return
   */
  public FrameBlock appendOperations(FrameBlock that, FrameBlock ret, boolean cbind)
      throws DMLRuntimeException {
    if (cbind) // COLUMN APPEND
    {
      // sanity check row dimension mismatch
      if (getNumRows() != that.getNumRows()) {
        throw new DMLRuntimeException(
            "Incompatible number of rows for cbind: "
                + that.getNumRows()
                + " (expected: "
                + getNumRows()
                + ")");
      }

      // allocate output frame
      if (ret == null) ret = new FrameBlock();
      ret._numRows = _numRows;

      // concatenate schemas (w/ deep copy to prevent side effects)
      ret._schema = new ArrayList<ValueType>(_schema);
      ret._schema.addAll(that._schema);
      ret._colnames = new ArrayList<String>(_colnames);
      ret._colnames.addAll(that._colnames);

      // concatenate column data (w/ deep copy to prevent side effects)
      for (Array tmp : _coldata) ret._coldata.add(tmp.clone());
      for (Array tmp : that._coldata) ret._coldata.add(tmp.clone());
    } else // ROW APPEND
    {
      // sanity check column dimension mismatch
      if (getNumColumns() != that.getNumColumns()) {
        throw new DMLRuntimeException(
            "Incompatible number of columns for rbind: "
                + that.getNumColumns()
                + " (expected: "
                + getNumColumns()
                + ")");
      }

      // allocate output frame (incl deep copy schema)
      if (ret == null) ret = new FrameBlock();
      ret._numRows = _numRows;
      ret._schema = new ArrayList<ValueType>(_schema);
      ret._colnames = new ArrayList<String>(_colnames);

      // concatenate data (deep copy first, append second)
      for (Array tmp : _coldata) ret._coldata.add(tmp.clone());
      Iterator<Object[]> iter = that.getObjectRowIterator();
      while (iter.hasNext()) ret.appendRow(iter.next());
    }

    return ret;
  }
Ejemplo n.º 2
0
  /**
   * Right indexing operations to slice a subframe out of this frame block. Note that the existing
   * column value types are preserved.
   *
   * @param rl row lower index, inclusive, 0-based
   * @param ru row upper index, inclusive, 0-based
   * @param cl column lower index, inclusive, 0-based
   * @param cu column upper index, inclusive, 0-based
   * @param ret
   * @return
   */
  public FrameBlock sliceOperations(int rl, int ru, int cl, int cu, FrameBlock ret)
      throws DMLRuntimeException {
    // check the validity of bounds
    if (rl < 0
        || rl >= getNumRows()
        || ru < rl
        || ru >= getNumRows()
        || cl < 0
        || cu >= getNumColumns()
        || cu < cl
        || cu >= getNumColumns()) {
      throw new DMLRuntimeException(
          "Invalid values for frame indexing: ["
              + (rl + 1)
              + ":"
              + (ru + 1)
              + ","
              + (cl + 1)
              + ":"
              + (cu + 1)
              + "] "
              + "must be within frame dimensions ["
              + getNumRows()
              + ","
              + getNumColumns()
              + "]");
    }

    // allocate output frame
    if (ret == null) ret = new FrameBlock();
    ret._numRows = ru - rl + 1;

    // copy output schema and colnames
    for (int j = cl; j <= cu; j++) {
      ret._schema.add(_schema.get(j));
      ret._colnames.add(_colnames.get(j));
    }

    // copy output data
    for (int j = cl; j <= cu; j++) ret._coldata.add(_coldata.get(j).slice(rl, ru));

    return ret;
  }
Ejemplo n.º 3
0
  /**
   * @param rhsFrame
   * @param rl
   * @param ru
   * @param cl
   * @param cu
   * @param ret
   * @return
   */
  public FrameBlock leftIndexingOperations(
      FrameBlock rhsFrame, int rl, int ru, int cl, int cu, FrameBlock ret)
      throws DMLRuntimeException {
    // check the validity of bounds
    if (rl < 0
        || rl >= getNumRows()
        || ru < rl
        || ru >= getNumRows()
        || cl < 0
        || cu >= getNumColumns()
        || cu < cl
        || cu >= getNumColumns()) {
      throw new DMLRuntimeException(
          "Invalid values for frame indexing: ["
              + (rl + 1)
              + ":"
              + (ru + 1)
              + ","
              + (cl + 1)
              + ":"
              + (cu + 1)
              + "] "
              + "must be within frame dimensions ["
              + getNumRows()
              + ","
              + getNumColumns()
              + "].");
    }
    if ((ru - rl + 1) < rhsFrame.getNumRows() || (cu - cl + 1) < rhsFrame.getNumColumns()) {
      throw new DMLRuntimeException(
          "Invalid values for frame indexing: "
              + "dimensions of the source frame ["
              + rhsFrame.getNumRows()
              + "x"
              + rhsFrame.getNumColumns()
              + "] "
              + "do not match the shape of the frame specified by indices ["
              + (rl + 1)
              + ":"
              + (ru + 1)
              + ", "
              + (cl + 1)
              + ":"
              + (cu + 1)
              + "].");
    }

    // allocate output frame (incl deep copy schema)
    if (ret == null) ret = new FrameBlock();
    ret._numRows = _numRows;
    ret._schema = new ArrayList<ValueType>(_schema);
    ret._colnames = new ArrayList<String>(_colnames);

    // copy data to output and partial overwrite w/ rhs
    for (int j = 0; j < getNumColumns(); j++) {
      Array tmp = _coldata.get(j).clone();
      if (j >= cl && j <= cu) tmp.set(rl, ru, rhsFrame._coldata.get(j - cl));
      ret._coldata.add(tmp);
    }

    return ret;
  }