Unable to start python scripts from java code - java

I've got java code to call a python script
Java
private void visualizeData() {
try{
Runtime.getRuntime().exec(“python pyScripts/visualize.py”)
} catch (IOException e){
e.printStackTrace();
}
}
And this is my code for the visualize.py:
visualize.py
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pylot as plt
f = open(“ecoli_data_transformed.txt”,”r”)
fig = plt.figure()
ax = fig.add_subplot(111, projection=‘3d’)
for line in f:
(a, b, c) = line.split(“\t”)
a = float(a)
b = float(b)
c = float(c)
ax.scatter(a,b,c)
ax.setxlabel(‘PCA1’)
ax.setylabel(‘PCA2’)
ax.setzlabel(‘PCA3’)
plt.show()
But it doesn't plot the data.
If I call the test.py script from the java code (test.py is in the same directory as visualize.py), it works:
test.py
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pylot as plt
import numpy as np
np.random.seed(19680801)
def randrange(n, min, vmcx):
return (vmax - vein)*np.random.rand(n) + vmin
fig = plt.figure()
ax = fig.add_subplot(111, projection=‘3d’)
n = 100
for c, m, slow, high in [(‘r’, ‘o’, -50, -25), (‘b’, ‘^’, -30, -5)]:
xs = randrange(n, 23, 32)
ys = randrange(n, 0, 100)
zs = randrange(n, zlow, zhigh)
ax.scatter(xs, ys, zs, c=c, marker=m)
ax.set_xlabel(‘X Label’)
ax.set_ylabel(‘Y Label’)
ax.set_zlabel(‘Z Label’)
plt.show()
What could be the problem?
*NOTE: Calling the visualize.py script from console by 'python visualize.py' works totally fine.

The relative path that you are passing to the open() command in your python script is probably the problem.
Your Java program is located in a different directory than the one your Python program is located in. When it starts the python script, the current path is still the path to the Java program.
This means that Python is unable to find the relative path to the file you are trying to open, ecoli_data_transformed.txt.
A work around would be to include the complete path to your .txt file:
f = open(“C:\\path\\to\\your\\file\\ecoli_data_transformed.txt”,”r”)
A better solution would be to determine it programatically:
import os
file_path = os.path.dirname(__file__)
f = open(file_path + "\\ecoli_data_transformed.txt”,”r”)

Related

Build webassembly sorting functions from C++ and Java source code

I have a problem that I was going to work on arrays in WebAssembly and I wanted to use Java and C ++, and trying to do this, I ran into the following problems and I would like to ask for help:
Java: I'm using JWebAssembly
And we have a class that works on tables
import de.inetsoftware.jwebassembly.api.annotation.Export;
public class Add {
#Export
public static int[] add( int a[], int b ) {
for(int i = 0;i<b-1;i++){
a[i] += b ;
}
return a;
}
}
we convert it to wasm
import de.inetsoftware.jwebassembly.JWebAssembly;
import java.io.File;
import java.net.URL;
public class Wasm {
public static void main(String[] args) {
File wasmFile = new File("testTable.wasm");
JWebAssembly wasm = new JWebAssembly();
Class clazz = Add.class;
URL url = clazz.getResource(
'/' +
clazz.getName().replace('.', '/') +
".class");
wasm.addFile(url);
String txt = wasm.compileToText();
System.out.println(txt);
wasm.compileToBinary(wasmFile);
}
}
and such an error comes out
Exception in thread "main" de.inetsoftware.jwebassembly.WasmException: Unimplemented Java byte code operation: 42 at Add.java:11
https://en.wikipedia.org/wiki/List_of_Java_bytecode_instructions
And I don't understand why because in this guy's presentation https://www.youtube.com/watch?v=93z9SaLQVVw (40 min+) you can see that it works and compiles
Now C++
I use emscripten, I wanted to do a bubble sort but for the sake of simplicity an example showing the problem
#include <emscripten.h>
using namespace std;
EMSCRIPTEN_KEEPALIVE
int* mainFunction(int table[], int length)
{
int* outTable = new int[length];
for(int i = 0;i<length; i++){
outTable[i] = table[i];
}
return table;
}
By running it with this command test.cpp -s WASM=1 -o test.html
after compiling it, files appear to me from which I extract the appropriate data and in javascript I set and import everything
let wasmExports = null;
let asmLibraryArg = {
abort: () => {},
emscripten_resize_heap: () => {},
};
let info = {
env: asmLibraryArg,
wasi_snapshot_preview1: asmLibraryArg,
};
async function loadWasm() {
let response = await fetch("test.wasm");
let bytes = await response.arrayBuffer();
let wasmObj = await WebAssembly.instantiate(bytes, info);
wasmExports = wasmObj.instance.exports;
}
loadWasm();
and later in the code I use
console.log(wasmExports);
console.log(wasmExports._Z12mainFunctionPii(table, table.length));
results
and when I throw in some array of integers it only throws me the number 0 and I have no idea how to get out of it. Or maybe someone knows another language in which it is possible to compile for wasm and then run it on the website?

Nothing happening when a R file is called from my Java code

I have a java code which has R programming steps in it which is running perfectly fine. But the same R programming steps are extracted into a R file (MyScript.r) and I tried to call this file from my java code. When I run my java code nothing seems to be happening. I may look dumb with what I'm trying to achieve, may be it's true as I don't have knowledge on R. So required your help on this.
My Java code with R programming steps inside it.
package com.rtest;
import java.io.IOException;
import org.rosuda.JRI.Rengine;
public class RWithJavaTest {
public static void main(String a[]) {
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// Start Rengine.
Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
// The vector that was created in JAVA context is stored in 'rVector'
// which is a variable in R context.
engine.eval("rVector=" + javaVector);
// Calculate MEAN of vector using R syntax.
engine.eval("meanVal=mean(rVector)");
// Retrieve MEAN value
double mean = engine.eval("meanVal").asDouble();
// Print output values
System.out.println("Mean of given vector is=" + mean);
}
}
The above program when run, successfully giving me the output: 3.0
Java code with R programming steps included in R file and calling the R file from the java code.
package com.rtest;
import java.io.IOException;
public class RWithJavaTest {
public static void main(String a[]) {
try {
System.out.println("before..");
Runtime.getRuntime().exec("Rscript D:\\MyScript.R");
System.out.println("after..");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
R script:
// Create an R vector in the form of a string.
String javaVector = "c(1,2,3,4,5)";
// System.out.println("System.getProperty(\"java.library.path\")>>"+System.getProperty("java.library.path")); //Prints path in env var
// Start Rengine.
Rengine engine = new Rengine(new String[] { "--no-save" }, false, null);
// The vector that was created in JAVA context is stored in 'rVector'
// which is a variable in R context.
engine.eval("rVector=" + javaVector);
// Calculate MEAN of vector using R syntax.
engine.eval("meanVal=mean(rVector)");
// Retrieve MEAN value
double mean = engine.eval("meanVal").asDouble();
// Print output values
System.out.println("Mean of given vector is=" + mean);
I know what I did here is something completely wrong but seeking help on 2 things here.
1) How to correct my R Script so that it runs with out any issues
2) Java code to call the R Script, so I can see the output 3.0 once I run the code.
Below lines of code obviously worked for me and it calls the R file successfully.
ProcessBuilder pb = new ProcessBuilder("C:/Program Files/R/R-3.4.3/bin/Rscript.exe" ,"D:/RTest/MyScript.R");
pb.start();

Basic Java Runtime program cannot find python

Hello working on a small program that just needs to run a python script I have. This python script will play a given .wav file, and draw a shape on the turtle screen. As such, I'm not looking for an output to be returned to java. Here is my java code:
public class Driver {
public static void main(String[] args){
try {
Process p = Runtime.getRuntime().exec("python " +
" D:/Coding Files/Python/MusicColors.py" +" teenagers.wav");
}
catch (Exception e){
System.out.println(e);
}
}
}
The exception I get is:
java.io.IOException: Cannot run program "python":
CreateProcess error=2, The system cannot find the file specified
I probably am making a very stupid mistake as I have limited knowledge in the subject of processes and such. I added python to my system path, so whenever I put "python" into command line, it returns with
Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:01:18) [MSC v.1900 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
And makes it the python shell.
Here is the exact line I added to my environment path:
C:\Users\Joe\AppData\Local\Programs\Python\Python35-32
If anyone can figure out where I went wrong I'd really appreciate it!
The $PATH variable you've set is not inherited in Java's execution context. Try passing the Python's bin path to exec()'s execution environment.
To do this, the code below first retrieve all the environment variables and create an array of ENV_KEY=ENV_VALUE pairs.
Then, the path to your Python's bin is appended to the PATH value.
Finally, we pass the array of all environment variables to exec() (via the second parameter).
import java.util.HashMap;
import java.util.Map;
public class Driver {
public static void main(String[] args){
try {
String[] commands = {"python D:/Coding Files/Python/MusicColors.py teenagers.wav"};
// Get a list of all environment variables
final Map<String, String> envMap = new HashMap<String, String>(System.getenv());
// Append Python bin path to Path
envMap.put("Path", envMap.get("Path") + ";C:/Users/Joe/AppData/Local/Programs/Python/Python35-32");
// Convert to an array of ENV_KEY=ENV_VALUE format strings
final String[] envs = new String[envMap.size()];
int i = 0;
for (Map.Entry<String, String> e : envMap.entrySet()) {
envs[i] = e.getKey() + '=' + e.getValue();
i++;
}
// Exec with the environment variables
Process p = Runtime.getRuntime().exec(commands, envs);
}
catch (Exception e){
System.out.println(e);
}
}
}

How to export data from R script within Java using Rserve?

I am using Rserve to access an R script through my Java project. The java code asks for a user input to enter the file location and stores in a String variable. This variable is then passes through to the R function which should read the file location perform some processes and then create a new folder and write the processed data in individual files and then print out on the console that all the files have been generated. I initially checked the R connection with a smaller version of the program and it worked. But, when I include the steps to write data to files, it shows the following error:
Enter the file path:
/home/workspace/TestR/test_file
Exception in thread "main" org.rosuda.REngine.Rserve.RserveException: eval failed, request status: error code: 127
at org.rosuda.REngine.Rserve.RConnection.eval(RConnection.java:234)
at testMain.main(testMain.java:23)
Moreover, the code also does not print any statements on the console which have to be printed via R from the Rscript. Here is the Java code:
import java.util.Scanner;
import org.rosuda.REngine.REXP;
import org.rosuda.REngine.REXPMismatchException;
import org.rosuda.REngine.REngineException;
import org.rosuda.REngine.Rserve.RConnection;
import org.rosuda.REngine.Rserve.RserveException;
public class testMain {
static String dirPath;
public static void main(String[] args) throws REXPMismatchException, REngineException {
// For user input
Scanner scanner = new Scanner(System.in );
System.out.println("Enter the file path: ");
dirPath = scanner.nextLine();
RConnection c = new RConnection();
// source the Palindrome function
c.eval("source('/home/workspace/TestR/Main.R')");
REXP valueReturned = c.eval("Main(\""+dirPath+"\")");
//c.eval("Main(\""+dirPath+"\")");
System.out.println(valueReturned.length());
}
}
And, here is the R script:
Main <- function(FILE_PATH)
{
## load libraries
library(MALDIquant)
library(MALDIquantForeign)
library(dcemriS4)
require(gridExtra) # also loads grid
library(lattice)
library(fields)
library(matlab)
library(rJava)
#Call the source files of the function which this script will use
source('/home/workspace/TestR/importAnalyzeFormat.R', echo=TRUE)
source('/home/workspace/TestR/exportFile.R', echo=TRUE)
source('/home/workspace/TestR/readRecalibratedSpectra.R', echo=TRUE)
spectralDataObjects <- importAnalyzeFormat(FILE_PATH)
p <- detectPeaks(spectralDataObjects, method="MAD", halfWindowSize=1, SNR=1)
# Assign the p to preprocessedDataObjects
preprocessedDataObjects<-p
dir.create("PreprocessedSpectra", showWarnings = FALSE)
setwd("PreprocessedSpectra")
for(i in 1:length(preprocessedDataObjects))
{
coordinateValue<-metaData(preprocessedDataObjects[[i]])
coordinates<-coordinateValue$imaging$pos
mzValues<-mass(preprocessedDataObjects[[i]])
intensityValues<-intensity(preprocessedDataObjects[[i]])
exportFile(coordinates,mzValues,intensityValues)
}
print("Files exported. Program will now terminate")
print("############################################################")
return(preprocessedDataObjects)
}
Can someone please help me?
You have an error in your script, a 127 means that there is a parse exception.
If you use something like this it will print out the error in the script.
c is the rserve connection in this case.
c.assign(".tmp.", myCode);
REXP r = c.parseAndEval("try(eval(parse(text=.tmp.)),silent=TRUE)");
if (r.inherits("try-error")) System.err.println("Error: "+r.toString())
else { // success .. }
Error code 127 means parsing exception.
Change your line:
c.eval("source('/home/workspace/TestR/Main.R')");
to
c.eval("source(\"/home/workspace/TestR/Main.R\")");
Now it is suppose to work.

How To Use Rcaller with Java Servlet and read CSV file

I'm using R programing to analysis FFT . now I want to make Java web application/ java servlet and calling R to use Rcaller/Rcode for it . I have some reference about Calling Rcode in java application. http://code.google.com/p/rcaller/wiki/Examples
I have CSV File
for example A.csv
time Amplitude
1 0.00000 -0.021
2 0.00001 -0.024
3 0.00003 -0.013
4 0.00004 -0.023
5 0.00005 0.019
6 0.00007 -0.002
7 0.00008 -0.013
then I want to upload this file and use R Code for analysis FFT and Plot it.
Help is much appreciated! Thanks in advance, Maria
You start creating an instance of RCaller and set the current location of install Rscript.exe file. You can start with
RCaller caller = new RCaller();
Globals.detect_current_rscript();
caller.setRscriptExecutable(Globals.Rscript_current);
RCode code = new RCode();
or you can give the exact location
RCaller caller = new RCaller();
caller.setRscriptExecutable("c:\\path\\to\\Rscript.exe");
RCode code = new RCode();
Suppose your data is saved in a file mydata.csv.
code.addRCode("dat <- read.cvs(\"mydata.csv\", header=T, sep=\",\"");
then we are plotting the Amplitude
File file = code.startPlot();
code.addRCode("plot.ts(dat$Amplitude)");
code.endPlot();
and sending our code to R:
caller.setRCode(code);
caller.runOnly();
And now, the file variable holds the image data. It can be shown on screen using the code
code.showPlot(file);
For further reading, follow the blog entries on stdioe blog
When I execute this code is running but didn't show anything !!!!!!!
package test2;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import javax.swing.ImageIcon;
import rcaller.RCaller;
import rcaller.RCode;
import rcaller.exception.RCallerExecutionException;
import rcaller.exception.RCallerParseException;
public class Test2 {
public static void main(String[] args) {
Test2 test2=new Test2();
}
private int span;
#SuppressWarnings("empty-statement")
public void test2()throws IOException{
try {
RCaller caller = new RCaller();
caller.setRscriptExecutable("C:\\Program Files\\R\\R-3.0.3\\bin\\Rscript.exe");
RCode code = new RCode();
code.addRCode("dat<-read.csv(\"NetBeansProjects\"test2\"A.csv\",header=T,sep=\",\"");
File file=code.startPlot();
code.addRCode("plot.ts(dat$Amplitude)");
code.endPlot();
caller.setRCode(code);
caller.runOnly();
ImageIcon i=code.getPlot(file);
code.showPlot(file);
} catch (RCallerExecutionException | RCallerParseException e) {
System.out.println(e.toString());
}
}
}

Categories

Resources