简体   繁体   中英

scipy.optimize.curve_fit run in Java using Runtime.getRuntime().exec()

I am running a curve fitting Python script in Java using Process p = Runtime.getRuntime().exec and Java is not getting output after one of my calls to scipy.optimize.curve_fit. I am fitting to a piecewise error function as shown below.

I created a "dummy" version of the function to check that the curve fitting would work on that, and it did. But when I apply the curve fit to actual data, Java does not get any of the output below that statement.

I ran the Python code in the terminal and it worked fine.

Java code:

String[] command = new String[2];
command[0] = "python";
command[1] = "python/curvefit.py";


try {
    Process p = Runtime.getRuntime().exec(command);
    BufferedReader stdInput = new BufferedReader(new 
    InputStreamReader(p.getInputStream()));
    while ((s1 = stdInput.readLine()) != null) {
        System.out.println(s1);
    }
}catch (IOException e) {
    e.printStackTrace();
}

Python code

## modifiable error function
def easyerf(x,a,b,c, d):
    return a*scipy.special.erf((b * x) - c) + d

## piecewise error function
def pwerf(x, a1, a2, b, c1, c2, h, e):
    returnarray = []
    for i in x:
        if i < e:
            returnarray.append(easyerf(i, a1, b, c1, h - a1))
        else:
            returnarray.append(easyerf(i, a2, b, c2, h + a2))
    return returnarray


array = np.linspace(-5, 10, 15)
array2 = np.linspace(10, 25, 15)
array3 = [easyerf(i, 4, 1, 1, 1) for i in array]
array4 = [easyerf(i, -4, 1, 15, 1) for i in array2]
array5 = np.concatenate((array,array2)) # x values
array6 = np.concatenate((array3,array4)) # y values
pguess7 = [10, -4, 17, 1, 15, 1, 10]

par3, con3 = scipy.optimize.curve_fit(pwerf, array5, array6, p0 = pguess7)

print("got here")

indexes = [1, 2, 3, 4, 5, 6, 7, 8,... ## long array
intensities = [0.050938000000000004, 0.049611, 0.054938, 0.047958,... ## long array

## fit piecewise error function and find ends/length
par4, con4 = scipy.optimize.curve_fit(pwerf, indexes, intensities, [10, 1, 1, 5, 17, 25,120])
print("now got here")

Java prints only the first "got here" but not the "now got here" that should follow after the second curve fit. Again, it works perfectly fine in the terminal. I need to get the output from the second curve fit. What's happening?

UPDATE: Figured it out. Java is running an older version of Python and requires the arguments of curve_fit to be numpy arrays, not lists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM