public class PythonSession
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static class |
PythonSession.PythonVariableType |
Modifier and Type | Method and Description |
---|---|
static PythonSession |
acquireSession(java.lang.Object requester)
Acquire the default session for the requester
|
static PythonSession |
acquireSession(java.lang.String pythonCommand,
java.lang.String ownerID,
java.lang.Object requester) |
boolean |
checkIfPythonVariableIsSet(java.lang.String varName,
boolean debug)
Check if a named variable is set/exists in the python environment
|
java.util.List<java.lang.String> |
executeScript(java.lang.String pyScript,
boolean debug)
Execute an arbitrary script in python
|
weka.core.Instances |
getDataFrameAsInstances(java.lang.String frameName,
boolean debug)
Retrieve a pandas data frame from Python and convert it to a set of
instances.
|
java.awt.image.BufferedImage |
getImageFromPython(java.lang.String varName,
boolean debug)
Retrieve an image from python.
|
java.util.List<java.lang.String> |
getPythonDebugBuffer(boolean debug)
Grab the contents of the debug buffer from the python server.
|
static java.lang.String |
getPythonEnvCheckResults()
Gets the result of running the checks in python
|
static java.lang.String |
getPythonEnvCheckResults(java.lang.String pythonCommand,
java.lang.String ownerID)
Gets the result of running the checks in python for the given python path +
optional ownerID.
|
PythonSession.PythonVariableType |
getPythonVariableType(java.lang.String varName,
boolean debug)
Get the type of a variable in python
|
java.util.List<java.lang.String[]> |
getVariableListFromPython(boolean debug)
Get a list of variables that are set in python.
|
java.lang.Object |
getVariableValueFromPythonAsJson(java.lang.String varName,
boolean debug)
Attempt to retrieve the value of a variable in python using serialization
to Json.
|
java.lang.String |
getVariableValueFromPythonAsPickledObject(java.lang.String varName,
boolean debug)
Attempt to retrieve the value of a variable in python using pickle
serialization.
|
java.lang.String |
getVariableValueFromPythonAsPlainString(java.lang.String varName,
boolean debug)
Attempt to retrieve the value of a variable in python as a plain string
(i.e.
|
static boolean |
initSession(java.lang.String pythonCommand,
boolean debug)
Initialize the default session.
|
static boolean |
initSession(java.lang.String pythonCommand,
java.lang.String ownerID,
java.lang.String pathEntries,
boolean debug)
Initialize a server/session for a user-supplied python path and (optional)
ownerID.
|
void |
instancesToPython(weka.core.Instances instances,
java.lang.String pythonFrameName,
boolean debug)
Transfer Weka instances into python as a named pandas data frame
|
void |
instancesToPythonAsScikitLearn(weka.core.Instances instances,
java.lang.String pythonFrameName,
boolean debug)
Transfer Weka instances into python as a pandas data frame and then extract
out numpy arrays of input and target features/columns.
|
static void |
main(java.lang.String[] args)
Some quick tests...
|
static boolean |
pythonAvailable()
Returns true if (at least) the default python environment/server is
available
|
static boolean |
pythonAvailable(java.lang.String pythonCommand,
java.lang.String ownerID)
Returns true if the user-specified python environment/server (as specified
by pythonCommand (and optional ownerID) is available.
|
static void |
releaseSession(java.lang.Object requester)
Release the default session so that other clients can obtain it.
|
static void |
releaseSession(java.lang.String pythonCommand,
java.lang.String ownerID,
java.lang.Object requester)
Release the user-specified python session.
|
void |
setLog(weka.gui.Logger log)
Set a log
|
void |
setPythonPickledVariableValue(java.lang.String varName,
java.lang.String varValue,
boolean debug)
Push a pickled python variable value back into python.
|
public static PythonSession acquireSession(java.lang.Object requester) throws weka.core.WekaException
requester
- the object requesting the sessionweka.core.WekaException
- if python is not availablepublic static PythonSession acquireSession(java.lang.String pythonCommand, java.lang.String ownerID, java.lang.Object requester) throws weka.core.WekaException
pythonCommand
- command (either fully qualified path or that which is
in the PATH). This, plus the optional ownerID is used to lookup
and return a session/server.ownerID
- an optional ownerID string for acquiring the session. This
can be used to restrict the session/server to one (or more)
clients (i.e. those with the ID "ownerID").requester
- the object requesting the session/serverweka.core.WekaException
- if the requested session/server is not available (or
does not exist).public static void releaseSession(java.lang.Object requester)
requester
- the session holderpublic static void releaseSession(java.lang.String pythonCommand, java.lang.String ownerID, java.lang.Object requester) throws weka.core.WekaException
pythonCommand
- command (either fully qualified path or that which is
in the PATH). This, plus the optional ownerID is used to lookup a
session/server.ownerID
- an optional ownerID string for identifying the session. This
can be used to restrict the session/server to one (or more)
clients (i.e. those with the ID "ownerID").requester
- the object requesting the session/serverweka.core.WekaException
- if the requested session/server is not available (or
does not exist).public static boolean pythonAvailable()
public static boolean pythonAvailable(java.lang.String pythonCommand, java.lang.String ownerID)
pythonCommand
- command (either fully qualified path or that which is
in the PATH). This, plus the optional ownerID is used to lookup a
session/server.ownerID
- an optional ownerID string for identifying the session. This
can be used to restrict the session/server to one (or more)
clients (i.e. those with the ID "ownerID").public void setLog(weka.gui.Logger log)
log
- the log to usepublic PythonSession.PythonVariableType getPythonVariableType(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable to get the type fordebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic void instancesToPython(weka.core.Instances instances, java.lang.String pythonFrameName, boolean debug) throws weka.core.WekaException
instances
- the instances to transferpythonFrameName
- the name of the data frame to use in pythondebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic void instancesToPythonAsScikitLearn(weka.core.Instances instances, java.lang.String pythonFrameName, boolean debug) throws weka.core.WekaException
instances
- the instances to transferpythonFrameName
- the name of the pandas data frame to use in pythondebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic weka.core.Instances getDataFrameAsInstances(java.lang.String frameName, boolean debug) throws weka.core.WekaException
frameName
- the name of the pandas data frame to extract and convert
to instancesdebug
- true for debugging outputweka.core.WekaException
- if the named data frame does not exist in python or
is not a pandas data framepublic java.util.List<java.lang.String> executeScript(java.lang.String pyScript, boolean debug) throws weka.core.WekaException
pyScript
- the script to executedebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic boolean checkIfPythonVariableIsSet(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable to checkdebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic java.lang.Object getVariableValueFromPythonAsJson(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable to retrievedebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic java.lang.String getVariableValueFromPythonAsPickledObject(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable to retrievedebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic java.lang.String getVariableValueFromPythonAsPlainString(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable to retrievedebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic java.util.List<java.lang.String[]> getVariableListFromPython(boolean debug) throws weka.core.WekaException
debug
- true if debugging info is to be outputweka.core.WekaException
- if a problem occurspublic void setPythonPickledVariableValue(java.lang.String varName, java.lang.String varValue, boolean debug) throws weka.core.WekaException
varName
- the name of the variable in python that will hold the
deserialized valuevarValue
- the pickled string value of the variabledebug
- true for debugging outputweka.core.WekaException
- if a problem occurspublic java.util.List<java.lang.String> getPythonDebugBuffer(boolean debug) throws weka.core.WekaException
debug
- true for debugging output (from the execution of this specific
command)weka.core.WekaException
- if a problem occurspublic java.awt.image.BufferedImage getImageFromPython(java.lang.String varName, boolean debug) throws weka.core.WekaException
varName
- the name of the variable in python that contains the imagedebug
- true to output debugging infoweka.core.WekaException
- if the variable doesn't exist, doesn't contain a
Figure object or there is a comms error.public static boolean initSession(java.lang.String pythonCommand, boolean debug) throws weka.core.WekaException
pythonCommand
- the python commanddebug
- true for debugging outputweka.core.WekaException
- if there was a problem - missing packages in python,
or python could not be started for some reasonpublic static boolean initSession(java.lang.String pythonCommand, java.lang.String ownerID, java.lang.String pathEntries, boolean debug) throws weka.core.WekaException
pythonCommand
- command (either fully qualified path or that which is
in the PATH). This, plus the optional ownerID is used to lookup
and return a session/server.ownerID
- an optional ownerID string for acquiring the session. This
can be used to restrict the session/server to one (or more)
clients (i.e. those with the ID "ownerID").pathEntries
- optional entries that need to be in the PATH in order
for the python environment to work correctlydebug
- true for debugging infoweka.core.WekaException
- if the requested session/server is not available (or
does not exist).public static java.lang.String getPythonEnvCheckResults()
public static java.lang.String getPythonEnvCheckResults(java.lang.String pythonCommand, java.lang.String ownerID) throws weka.core.WekaException
pythonCommand
- command (either fully qualified path or that which is
in the PATH). This, plus the optional ownerID is used to lookup
and return a session/server.ownerID
- an optional ownerID string for acquiring the session. This
can be used to restrict the session/server to one (or more)
clients (i.e. those with the ID "ownerID").weka.core.WekaException
- if the requested server does not existpublic static void main(java.lang.String[] args)
args
-