Esempio n. 1
0
    def compute(
        self,
        verbose: bool = False,
        lineage: bool = False
    ) -> Union[float, np.array, Tuple[Union[float, np.array], str]]:
        if self.result_var is None:
            self.script = DMLScript()
            self.script.build_code(self)
            if lineage:
                result_variables, ltrace = self.script.execute(lineage)
            else:
                result_variables = self.script.execute(lineage)
            if self.output_type == OutputType.DOUBLE:
                self.result_var = result_variables.getDouble(
                    self.script.out_var_name)
            elif self.output_type == OutputType.MATRIX:
                self.result_var = matrix_block_to_numpy(
                    get_gateway().jvm,
                    result_variables.getMatrixBlock(self.script.out_var_name))
        if verbose:
            print(self.script.dml_script)
            # TODO further info

        if lineage:
            return self.result_var, ltrace
        else:
            return self.result_var
Esempio n. 2
0
    def execute(
            self,
            lineage: bool = False
    ) -> Union[JavaObject, Tuple[JavaObject, str]]:
        """If not already created, create a preparedScript from our DMLCode, pass python local data to our prepared
        script, then execute our script and return the resultVariables

        :return: resultVariables of our execution
        """
        # we could use the gateway directly, non defined functions will be automatically
        # sent to the entry_point, but this is safer
        gateway = get_gateway()
        entry_point = gateway.entry_point
        if self.prepared_script is None:
            input_names = self.inputs.keys()
            connection = entry_point.getConnection()
            self.prepared_script = connection.prepareScript(
                self.dml_script, _list_to_java_array(input_names),
                _list_to_java_array([self.out_var_name]))
            for (name, input_node) in self.inputs.items():
                input_node.pass_python_data_to_prepared_script(
                    gateway.jvm, name, self.prepared_script)

            if lineage:
                connection.setLineage(True)

        ret = self.prepared_script.executeScript()
        if lineage:
            return ret, self.prepared_script.getLineageTrace(self.out_var_name)

        return ret
Esempio n. 3
0
def _list_to_java_array(
        py_list: Union[Collection[str], KeysView[str]]) -> JavaArray:
    """Convert python collection to java array.

    :param py_list: python collection
    :return: java array
    """
    gateway = get_gateway()
    array = gateway.new_array(gateway.jvm.java.lang.String, len(py_list))
    for (i, e) in enumerate(py_list):
        array[i] = e
    return array
Esempio n. 4
0
    def getlineage(self) -> str:
        gateway = get_gateway()
        entry_point = gateway.entry_point
        if self.prepared_script is None:
            input_names = self.inputs.keys()
            connection = entry_point.getConnection()
            self.prepared_script = connection.prepareScript(
                self.dml_script, _list_to_java_array(input_names),
                _list_to_java_array([self.out_var_name]))
            for (name, input_node) in self.inputs.items():
                input_node.pass_python_data_to_prepared_script(
                    gateway.jvm, name, self.prepared_script)

            connection.setLineage(True)

        ret = self.prepared_script.executeScript()
        lineage = self.prepared_script.getLineageTrace(self.out_var_name)
        return lineage