org.python.core.PyFunction Java Examples

The following examples show how to use org.python.core.PyFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GBMLRDataFlow.java    From ytk-learn with MIT License 6 votes vote down vote up
public GBMLRCoreData(ThreadCommSlave comm,
                     CoreParams coreParams,
                     IFeatureMap featureMap,
                     PyFunction pyTransformFunc,
                     boolean needPyTransform,
                     FeatureHash featureHash,
                     float uniformBaseScore,
                     boolean sampleDepdtBaseScore) {
    super(comm, coreParams, featureMap, pyTransformFunc, needPyTransform, featureHash);
    this.z = new float[MAX_2D_LEN][];
    this.randMask = new BitSet[MAX_2D_LEN];
    this.uniformBaseScore = uniformBaseScore;
    this.sampleDepdtBaseScore = sampleDepdtBaseScore;


}
 
Example #2
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
private boolean checkTransactions(final PyFunction condition, final PyFunction nodeCondition, final int direction) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> transactions = getTransactions(context.current(), direction);
    for (final Map.Entry<Integer, Integer> entry : transactions.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.TRANSACTION);
        boolean conditionMatches = isTrueValue(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            return true;
        }
    }
    return false;
}
 
Example #3
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public boolean has_edge(final PyFunction condition, final PyFunction nodeCondition) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> edges = getEdges(context.current());
    for (final Map.Entry<Integer, Integer> entry : edges.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.EDGE);
        boolean conditionMatches = isTrueAndContainsNonNulls(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            return true;
        }
    }
    return false;
}
 
Example #4
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public boolean has_link(final PyFunction condition, final PyFunction nodeCondition) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> links = getLinks(context.current());
    for (final Map.Entry<Integer, Integer> entry : links.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.LINK);
        boolean conditionMatches = isTrueAndContainsNonNulls(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            return true;
        }
    }
    return false;
}
 
Example #5
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
private int countTransactions(final PyFunction condition, final PyFunction nodeCondition, final int direction) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> transactions = getTransactions(context.current(), direction);
    int count = 0;
    for (final Map.Entry<Integer, Integer> entry : transactions.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.TRANSACTION);
        boolean conditionMatches = isTrueValue(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            count++;
        }
    }
    return count;
}
 
Example #6
Source File: XunfengInner.java    From TrackRay with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void process() {

    try {
        PythonInterpreter interpreter = python.interpreter();
        interpreter.execfile(filename);
        PyFunction check = interpreter.get("check", PyFunction.class);

        PyObject check_call = check.__call__(new PyString(ip), new PyInteger(port), new PyInteger(timeout));

        String result = check_call.toString();
        if (result!=null &&
                !StringUtils.contains(result,"None")
                && !StringUtils.contains(result,"False")) {

            //PyObject get_plugin_info = interpreter.get("get_plugin_info").__call__();
            //Map map = (Map) get_plugin_info.getDict().__tojava__(Map.class);
            this.result = result;
            return;
        }
    }catch (Exception e){
        log.error(e.toString());
    }

    result="";
}
 
Example #7
Source File: GBDTCoreData.java    From ytk-learn with MIT License 6 votes vote down vote up
public GBDTCoreData(ThreadCommSlave comm,
                    DataFlow.CoreParams coreParams,
                    IFeatureMap featureMap,
                    PyFunction pyTransformFunc,
                    boolean needPyTransfor,
                    int maxFeatureDim,
                    int numTreeInGroup,
                    ILossFunction obj,
                    float baseScore,
                    boolean sampleDepdtBasePrediction) {
    super(comm, coreParams, featureMap, pyTransformFunc, needPyTransfor);
    this.initScore = new float[MAX_2D_LEN][];
    this.score = new float[MAX_2D_LEN][];

    this.DENSE_MAX_1D_SAMPLE_CNT = MAX_1D_LEN / maxFeatureDim;
    this.DENSE_MAX_1D_LEN = DENSE_MAX_1D_SAMPLE_CNT * maxFeatureDim;
    this.lastPredRound = 0;

    this.maxFeatureDim = maxFeatureDim;
    this.numTreeInGroup = numTreeInGroup;
    this.obj = obj;
    this.baseScore = baseScore;
    this.sampleDepdtBasePrediction = sampleDepdtBasePrediction;
}
 
Example #8
Source File: AbstractCalculatorUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public Set<Integer> getFunctionalArgumentIndices(String methodName, int numArgs) {
    final Set<Integer> functionalArgumentIndices = new HashSet<>();
    Method[] methods = getClass().getDeclaredMethods();
    Method candidate = null;
    for (Method m : methods) {
        if (m.getName().equals(methodName) && m.getParameterCount() == numArgs) {
            candidate = m;
            break;
        }
    }
    if (candidate != null) {
        Class<?>[] types = candidate.getParameterTypes();
        for (int i = 0; i < types.length; i++) {
            if (types[i].equals(PyFunction.class)) {
                functionalArgumentIndices.add(i);
            }
        }
    }
    return functionalArgumentIndices;
}
 
Example #9
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_edges(final PyFunction condition, PyFunction nodeCondition, PyFunction computation) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> edges = getEdges(context.current());
    final List<Object> results = new ArrayList<>();
    for (final Map.Entry<Integer, Integer> entry : edges.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.EDGE);
        if (condition == null || isTrueAndContainsNonNulls(condition.__call__())) {
            context.enter(entry.getValue(), GraphElementType.VERTEX);
            boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
            context.exit();
            if (otherNodeMatches) {
                final Object result = computation.__call__();
                if (!nullCheck(result)) {
                    results.add(result);
                }
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #10
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public PyList for_parallel_transactions(final PyFunction condition, final PyFunction computation) throws ScriptException {
    context.require(GraphElementType.TRANSACTION);
    final Set<Integer> transactions = getParallelTransactions(context.current());
    final List<Object> results = new ArrayList<>();
    for (int transId : transactions) {
        context.enter(transId, GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return new PyList(results);
}
 
Example #11
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public int count_edges(final PyFunction condition, final PyFunction nodeCondition) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> edges = getEdges(context.current());
    int count = 0;
    for (final Map.Entry<Integer, Integer> entry : edges.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.EDGE);
        boolean conditionMatches = isTrueAndContainsNonNulls(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            count++;
        }
    }
    return count;
}
 
Example #12
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 6 votes vote down vote up
public int count_links(final PyFunction condition, final PyFunction nodeCondition) {
    context.require(GraphElementType.VERTEX);
    final Map<Integer, Integer> links = getLinks(context.current());
    int count = 0;
    for (final Map.Entry<Integer, Integer> entry : links.entrySet()) {
        context.enter(entry.getKey(), GraphElementType.LINK);
        boolean conditionMatches = isTrueAndContainsNonNulls(condition.__call__());
        context.enter(entry.getValue(), GraphElementType.VERTEX);
        boolean otherNodeMatches = nodeCondition == null || isTrueValue(nodeCondition.__call__());
        context.exit();
        context.exit();
        if (conditionMatches && otherNodeMatches) {
            count++;
        }
    }
    return count;
}
 
Example #13
Source File: GraphAnalysisUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public List<Object> graph_for_transactions(final PyFunction condition, final PyFunction computation) throws ScriptException {
    final List<Object> results = new ArrayList<>();
    for (int i = 0; i < context.graph.getTransactionCount(); i++) {
        final int elementId = context.graph.getTransaction(i);
        context.enter(elementId, GraphElementType.TRANSACTION);
        if (condition == null || isTrueValue(condition.__call__())) {
            final Object result = computation.__call__();
            if (!nullCheck(result)) {
                results.add(result);
            }
        }
        context.exit();
    }
    return results;
}
 
Example #14
Source File: VertexNeighbourUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public int count_nodes_at_distance(final PyFunction condition, final int distance) throws ScriptException {
    context.require(GraphElementType.VERTEX);
    final List<Integer> elementsAtDistance = new LinkedList<>();
    getAtDistance(distance, context.current(), elementsAtDistance, new BitSet(context.graph.getVertexCount()));
    int count = 0;
    for (int elementId : elementsAtDistance) {
        context.enter(elementId);
        boolean conditionMatches = isTrueValue(condition.__call__());
        context.exit();
        if (conditionMatches) {
            count++;
        }
    }
    return count;
}
 
Example #15
Source File: PythonExecutor.java    From score with Apache License 2.0 5 votes vote down vote up
private boolean keyIsExcluded(String key, PyObject value) {
    return (key.startsWith("__") && key.endsWith("__")) ||
            value instanceof PyFile ||
            value instanceof PyModule ||
            value instanceof PyFunction ||
            value instanceof PySystemState ||
            value instanceof PyClass ||
            value instanceof PyType ||
            value instanceof PyReflectedFunction;
}
 
Example #16
Source File: VertexTransactionUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public int count_parallel_transactions(final PyFunction condition) throws ScriptException {
    context.require(GraphElementType.TRANSACTION);
    final Set<Integer> transactions = getParallelTransactions(context.current());
    int count = 0;
    for (int transId : transactions) {
        context.enter(transId, GraphElementType.TRANSACTION);
        boolean conditionMatches = isTrueAndContainsNonNulls(condition.__call__());
        context.exit();
        if (conditionMatches) {
            count++;
        }
    }
    return count;
}
 
Example #17
Source File: VertexNeighbourUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public boolean has_node_at_distance(final PyFunction condition, final int distance) throws ScriptException {
    context.require(GraphElementType.VERTEX);
    final List<Integer> elementsAtDistance = new LinkedList<>();
    getAtDistance(distance, context.current(), elementsAtDistance, new BitSet(context.graph.getVertexCount()));
    for (int elementId : elementsAtDistance) {
        context.enter(elementId);
        boolean conditionMatches = isTrueValue(condition.__call__());
        context.exit();
        if (conditionMatches) {
            return true;
        }
    }
    return false;
}
 
Example #18
Source File: JythonFunction.java    From spork with Apache License 2.0 5 votes vote down vote up
@Override
public Schema outputSchema(Schema input) {
    if(schema != null) {
        return schema;
    } else {
        if(outputSchemaFunc != null) {
            PyFunction pf;
            try {
                pf = JythonScriptEngine.getFunction(scriptFilePath, outputSchemaFunc);
                // this should be a schema function
                PyObject schemaFunctionDef = pf.__findattr__("schemaFunction".intern());
                if(schemaFunctionDef == null) {
                    throw new IllegalStateException("Function: "
                            + outputSchemaFunc + " is not a schema function");
                }
                Object newSchema = ((pf.__call__(Py.java2py(input))).__tojava__(Object.class));
                if (newSchema instanceof ResourceSchema) {
                    return(Schema.getPigSchema((ResourceSchema) newSchema));
                }
                else if (newSchema instanceof Schema) {
                    return (Schema) newSchema;
                }
                else {
                    return Utils.getSchemaFromString(newSchema.toString());
                }
            } catch (IOException ioe) {
                throw new IllegalStateException("Could not find function: "
                    + outputSchemaFunc + "()", ioe);
            }
        } else {
            return new Schema(new Schema.FieldSchema(null, DataType.BYTEARRAY));
        }
    }
}
 
Example #19
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject params){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(params.__getitem__(0));
	return pyobj;
}
 
Example #20
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function, PyObject param0, PyObject param1){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__(param0, param1);
	return pyobj;
}
 
Example #21
Source File: VertexNeighbourUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public List<Integer> get_node_distances(final PyFunction condition) throws ScriptException {
    context.require(GraphElementType.VERTEX);
    final List<Integer> elementsAtDistance = new LinkedList<>();
    final List<Integer> results = new ArrayList<>();
    // Check the condition in the current context (for this node) and add distance 0
    if (isTrueValue(condition.__call__())) {
        results.add(0);
    }
    int currentDistance = 1;
    final BitSet visitedNodes = new BitSet(context.graph.getVertexCount());
    getAtDistance(currentDistance, context.current(), elementsAtDistance, visitedNodes);
    while (!elementsAtDistance.isEmpty()) {
        for (int elementId : elementsAtDistance) {
            context.enter(elementId);
            boolean conditionMatches = isTrueValue(condition.__call__());
            context.exit();
            if (conditionMatches) {
                results.add(currentDistance);
                break;
            }
        }
        currentDistance++;
        elementsAtDistance.clear();
        visitedNodes.clear();
        getAtDistance(currentDistance, context.current(), elementsAtDistance, visitedNodes);
    }
    // The node distances should be a list containing -1 if a node meeting the condition was never found.
    if (results.isEmpty()) {
        results.add(-1);
    }
    return results;
}
 
Example #22
Source File: PythonCode.java    From pycode-minecraft with MIT License 5 votes vote down vote up
public void invoke(String method) {
    PyObject obj = (PyObject) this.bindings.get(method);
    if (obj == null) {
        failz0r(world, pos, "Unknown function '%s'", method);
        return;
    }
    PyFunction func = (PyFunction)obj;
    try {
        func.__call__();
    } catch (RuntimeException e) {
        failz0r(world, pos, "Error running code: %s", e.toString());
    }
}
 
Example #23
Source File: GraphAnalysisUtilities.java    From constellation with Apache License 2.0 5 votes vote down vote up
public int graph_count_nodes(final PyFunction condition, final int distance) throws ScriptException {
    int count = 0;
    for (int i = 0; i < context.graph.getVertexCount(); i++) {
        final int elementId = context.graph.getVertex(i);
        context.enter(elementId, GraphElementType.VERTEX);
        boolean conditionMatches = isTrueValue(condition.__call__());
        context.exit();
        if (conditionMatches) {
            count++;
        }
    }
    return count;
}
 
Example #24
Source File: ContinuousCoreData.java    From ytk-learn with MIT License 5 votes vote down vote up
public ContinuousCoreData(ThreadCommSlave comm,
                          DataFlow.CoreParams coreParams,
                          IFeatureMap featureMap,
                          PyFunction pyTransformFunc,
                          boolean needPyTransform,
                          FeatureHash featureHash
                        ) {
    super(comm, coreParams, featureMap, pyTransformFunc, needPyTransform);
    this.featureHash = featureHash;
}
 
Example #25
Source File: AndroidDriver.java    From AndroidRobot with Apache License 2.0 5 votes vote down vote up
private PyObject executePython(String filePath, String function){
	PythonInterpreter interpreter = new PythonInterpreter();
	Vector<AndroidDriver> drivers = new Vector();
	drivers.add(this);
	interpreter.set("device", drivers);
	interpreter.execfile(filePath);
	PyFunction pyfunction = interpreter.get(function, PyFunction.class);
	PyObject pyobj = pyfunction.__call__();
	return pyobj;
}
 
Example #26
Source File: ContinuousOnlinePredictor.java    From ytk-learn with MIT License 5 votes vote down vote up
private static Iterator nextSamples(String line, boolean needPyTransform, PyFunction pyTransformFunc, List<String>lineList) throws UnsupportedEncodingException {
    Iterator iter;
    if (needPyTransform) {
        iter = transform(line, pyTransformFunc).iterator();
    } else {
        lineList.set(0, line);
        iter = lineList.iterator();
    }

    return iter;
}
 
Example #27
Source File: GBDTOnlinePredictor.java    From ytk-learn with MIT License 5 votes vote down vote up
private static Iterator nextSamples(String line, boolean needPyTransform, PyFunction pyTransformFunc, List<String> lineList) throws UnsupportedEncodingException {
    Iterator iter;
    if (needPyTransform) {
        iter = transform(line, pyTransformFunc).iterator();
    } else {
        lineList.set(0, line);
        iter = lineList.iterator();
    }

    return iter;
}
 
Example #28
Source File: MulticlassLinearModelDataFlow.java    From ytk-learn with MIT License 5 votes vote down vote up
public MulticlassLinearCoreData(ThreadCommSlave comm,
                                CoreParams coreParams,
                                IFeatureMap featureMap,
                                PyFunction pyTransformFunc,
                                boolean needPyTransform,
                                FeatureHash featureHash,
                                int K) {
    super(comm, coreParams, featureMap, pyTransformFunc, needPyTransform, featureHash);
    this.K = K;
}
 
Example #29
Source File: CoreData.java    From ytk-learn with MIT License 5 votes vote down vote up
public CoreData(ThreadCommSlave comm,
                DataFlow.CoreParams coreParams,
                IFeatureMap featureMap,
                PyFunction pyTransformFunc,
                boolean needPyTransform) {
    this.comm = comm;
    this.coreParams = coreParams;
    this.featureMap = featureMap;
    this.pyTransformFunc = pyTransformFunc;
    this.needPyTransform = needPyTransform;

    this.LOG_UTILS = new LogUtils(comm, coreParams.verbose);

    this.biasDelta = coreParams.need_bias ? 1 : 0;

    this.x = new int[MAX_2D_LEN][];
    this.xidx = new int[MAX_2D_LEN][];
    this.y = new float[MAX_2D_LEN][];
    this.weight = new float[MAX_2D_LEN][];

    this.realNum = new int[MAX_2D_LEN];
    this.weightNum = new double[MAX_2D_LEN];

    this.predict = new float[MAX_2D_LEN][];

    this.needFeatureTransform = coreParams.featureParams != null &&
            coreParams.featureParams.transform != null &&
            coreParams.featureParams.transform.switch_on;
}
 
Example #30
Source File: FFMModelDataFlow.java    From ytk-learn with MIT License 5 votes vote down vote up
public FFMCoreData(ThreadCommSlave comm,
                   DataFlow.CoreParams coreParams,
                   IFeatureMap featureMap,
                   PyFunction pyTransformFunc,
                   boolean needPyTransform,
                   FeatureHash featureHash,
                   int maxFeatureNum,
                   String fieldDelim,
                   Map<String, Integer> field2IndexMap) {
    super(comm, coreParams, featureMap, pyTransformFunc, needPyTransform, featureHash);
    this.maxFeatureNum = maxFeatureNum;
    this.fieldDelim = fieldDelim;
    this.field2IndexMap = field2IndexMap;
}