def apply(t: Table) -> Table:
     return t.update("f = a + b")
Example #2
0
def learn(table: Table = None,
          model_func: Callable = None,
          inputs: List[Input] = [],
          outputs: List[Output] = [],
          batch_size: int = None) -> Table:
    """ Learn gathers data from multiple rows of the input table, performs a calculation, and scatters values from the
    calculation into an output table. This is a common computing paradigm for artificial intelligence, machine learning,
    and deep learning.

    Args:
        table (Table): the Deephaven table to perform computations on.
        model_func (Callable): function that performs computations on the table.
        inputs (List[Input]): list of Input objects that determine how data gets extracted from the table.
        outputs (List[Output]): list of Output objects that determine how data gets scattered back into the results table.
        batch_size (int): maximum number of rows for which model_func is evaluated at once.

    Returns:
        a Table with added columns containing the results of evaluating model_func.

    Raises:
        DHError
    """

    try:
        _validate(inputs, outputs, table)

        if batch_size is None:
            raise ValueError(
                "Batch size cannot be inferred. Please specify a batch size.")

        # TODO: When ticket #1072 is resolved, the following code should be replaced with
        # Globals["__computer"] = _Computer_(table, model_func, [input.input for input in inputs], batch_size)
        # and remove from globals at the end of function
        (jpy.get_type("io.deephaven.engine.table.lang.QueryScope").addParam(
            "__computer",
            _JLearnComputer(table.j_table, model_func,
                            [input_.input for input_ in inputs], batch_size)))

        future_offset = _create_non_conflicting_col_name(
            table, "__FutureOffset")
        clean = _create_non_conflicting_col_name(table, "__CleanComputer")

        if outputs is not None:
            __scatterer = _JLearnScatterer(
                [output.output for output in outputs])
            # TODO: Similarly at resolution of #1072, replace the following code with
            # Globals["__scatterer"] = __scatterer
            # and remove from Globals at end of function
            jpy.get_type("io.deephaven.engine.table.lang.QueryScope").addParam(
                "__scatterer", __scatterer)

            return (table.update(formulas=[
                f"{future_offset} = __computer.compute(k)",
            ]).update(formulas=[
                __scatterer.generateQueryStrings(f"{future_offset}"),
            ]).update(formulas=[
                f"{clean} = __computer.clear()",
            ]).drop_columns(cols=[
                f"{future_offset}",
                f"{clean}",
            ]))

        result = _create_non_conflicting_col_name(table, "__Result")

        return (table.update(formulas=[
            f"{future_offset} = __computer.compute(k)",
            f"{result} = {future_offset}.getFuture().get()",
            f"{clean} = __computer.clear()",
        ]).drop_columns(cols=[
            f"{future_offset}",
            f"{clean}",
            f"{result}",
        ]))
    except Exception as e:
        raise DHError(e, "failed to complete the learn function.") from e
def transform_func(t: Table) -> Table:
    return t.update("f = a + b")