Class PythonUtil

java.lang.Object
com.eco.bio7.pybridge.PythonUtil

public final class PythonUtil extends Object
The API of the embedded Python of Bio7 for Java, Groovy and BeanShell scripts (as REval for R; Python scripts use the module bio7). The variables are created in the namespace of the Python console and of the scripts (__main__).
PythonUtil.toPython("x", 1.0, 2.0, 3.0);           // numpy array
PyValue mean = PythonUtil.fromPython("float(x.mean())");
PythonUtil.imageToPython("img");                   // the current ImageJ image
PythonUtil.pythonToR("x", "xr");                   // Python -> R
PythonUtil.rToPython("summary(cars)", "s");        // R -> Python
  • Method Summary

    Modifier and Type
    Method
    Description
    static PyValue
    eval(String expression)
    Evaluates a Python expression and returns its value.
    static void
    exec(String code)
    Executes Python statements (an error raises an exception with the traceback).
    static PyValue
    fromPython(String expression)
    Evaluates a Python expression and returns its value.
    static double[]
    Returns the numbers of an expression (e.g., a numpy array) as double[].
    static void
    fromR(String rExpression, String pythonName)
    static ij.ImagePlus
    imageFromPython(String expression, String title)
    Creates and shows an ImageJ image from a numpy array (2-d, stack or RGB).
    static void
    Transfers the current ImageJ image as numpy array (see PythonConvert.image(ImagePlus)).
    static void
    imageToPython(String name, ij.ImagePlus imp)
    Transfers an image as numpy array.
    static boolean
    Returns true if the embedded Python runs.
    static void
    pythonToR(String pythonExpression, String rName)
    Transfers the value of a Python expression to R (embedded R): arrays keep their shape (matrices, arrays), DataFrames become data frames, dicts named lists.
    static void
    Transfers the ImageJ results table as pandas DataFrame (a dict without pandas).
    static void
    rToPython(String rExpression, String pythonName)
    Transfers the value of an R expression to Python: vectors and matrices become numpy arrays, data frames pandas DataFrames, named lists dicts.
    static void
    run(String code)
    Executes Python code like a script: the output and errors in the console, new plots in the view.
    static void
    Starts the embedded Python with the Python of the preferences (if not running).
    static void
    toPython(String name, boolean... values)
    Assigns booleans (a numpy bool array).
    static void
    toPython(String name, double... values)
    Assigns numbers (a numpy array).
    static void
    toPython(String name, double[][] matrix)
    Assigns a matrix (rows) as 2-d numpy array.
    static void
    toPython(String name, int... values)
    Assigns integers (a numpy int32 array).
    static void
    toPython(String name, PyValue value)
    Assigns any value (arrays with shape, lists, dicts, DataFrames).
    static void
    toPython(String name, String... values)
    Assigns strings (a str for one string, else a list).
    static void
    toR(String pythonExpression, String rName)

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • isAlive

      public static boolean isAlive()
      Returns true if the embedded Python runs.
    • start

      public static void start() throws com.eco.bio7.pyffm.PyException
      Starts the embedded Python with the Python of the preferences (if not running).
      Throws:
      com.eco.bio7.pyffm.PyException - with the reason if it can't be started
    • toPython

      public static void toPython(String name, double... values) throws com.eco.bio7.pyffm.PyException
      Assigns numbers (a numpy array).
      Throws:
      com.eco.bio7.pyffm.PyException
    • toPython

      public static void toPython(String name, int... values) throws com.eco.bio7.pyffm.PyException
      Assigns integers (a numpy int32 array).
      Throws:
      com.eco.bio7.pyffm.PyException
    • toPython

      public static void toPython(String name, String... values) throws com.eco.bio7.pyffm.PyException
      Assigns strings (a str for one string, else a list).
      Throws:
      com.eco.bio7.pyffm.PyException
    • toPython

      public static void toPython(String name, boolean... values) throws com.eco.bio7.pyffm.PyException
      Assigns booleans (a numpy bool array).
      Throws:
      com.eco.bio7.pyffm.PyException
    • toPython

      public static void toPython(String name, double[][] matrix) throws com.eco.bio7.pyffm.PyException
      Assigns a matrix (rows) as 2-d numpy array.
      Throws:
      com.eco.bio7.pyffm.PyException
    • toPython

      public static void toPython(String name, PyValue value) throws com.eco.bio7.pyffm.PyException
      Assigns any value (arrays with shape, lists, dicts, DataFrames).
      Throws:
      com.eco.bio7.pyffm.PyException
    • fromPython

      public static PyValue fromPython(String expression) throws com.eco.bio7.pyffm.PyException
      Evaluates a Python expression and returns its value.
      Throws:
      com.eco.bio7.pyffm.PyException
    • eval

      public static PyValue eval(String expression) throws com.eco.bio7.pyffm.PyException
      Evaluates a Python expression and returns its value.
      Throws:
      com.eco.bio7.pyffm.PyException
    • exec

      public static void exec(String code) throws com.eco.bio7.pyffm.PyException
      Executes Python statements (an error raises an exception with the traceback).
      Throws:
      com.eco.bio7.pyffm.PyException
    • run

      public static void run(String code) throws com.eco.bio7.pyffm.PyException
      Executes Python code like a script: the output and errors in the console, new plots in the view.
      Throws:
      com.eco.bio7.pyffm.PyException
    • fromPythonDoubles

      public static double[] fromPythonDoubles(String expression) throws com.eco.bio7.pyffm.PyException
      Returns the numbers of an expression (e.g., a numpy array) as double[].
      Throws:
      com.eco.bio7.pyffm.PyException
    • imageToPython

      public static void imageToPython(String name) throws com.eco.bio7.pyffm.PyException
      Transfers the current ImageJ image as numpy array (see PythonConvert.image(ImagePlus)).
      Throws:
      com.eco.bio7.pyffm.PyException
    • imageToPython

      public static void imageToPython(String name, ij.ImagePlus imp) throws com.eco.bio7.pyffm.PyException
      Transfers an image as numpy array.
      Throws:
      com.eco.bio7.pyffm.PyException
    • imageFromPython

      public static ij.ImagePlus imageFromPython(String expression, String title) throws com.eco.bio7.pyffm.PyException
      Creates and shows an ImageJ image from a numpy array (2-d, stack or RGB).
      Throws:
      com.eco.bio7.pyffm.PyException
    • resultsTableToPython

      public static void resultsTableToPython(String name) throws com.eco.bio7.pyffm.PyException
      Transfers the ImageJ results table as pandas DataFrame (a dict without pandas).
      Throws:
      com.eco.bio7.pyffm.PyException
    • pythonToR

      public static void pythonToR(String pythonExpression, String rName) throws Exception
      Transfers the value of a Python expression to R (embedded R): arrays keep their shape (matrices, arrays), DataFrames become data frames, dicts named lists.
      Throws:
      Exception
    • rToPython

      public static void rToPython(String rExpression, String pythonName) throws Exception
      Transfers the value of an R expression to Python: vectors and matrices become numpy arrays, data frames pandas DataFrames, named lists dicts.
      Throws:
      Exception
    • toR

      public static void toR(String pythonExpression, String rName) throws Exception
      Throws:
      Exception
    • fromR

      public static void fromR(String rExpression, String pythonName) throws Exception
      Throws:
      Exception