Search in sources :

Example 1 with OptRun

use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.

the class OptServerImplTest method main.

public static void main(String[] args) {
    try {
        System.setProperty(PropertyLoader.installationRoot, "/Users/schaff/Documents/workspace-modular/vcell");
        System.setProperty(PropertyLoader.pythonExe, "/Users/schaff/anaconda/bin/python");
        PythonSupport.verifyInstallation(new PythonPackage[] { PythonPackage.COPASI, PythonPackage.LIBSBML, PythonPackage.THRIFT });
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
        }
        File optProbFile = new File(ResourceUtil.getVCellOptPythonDir(), "optprob.bin");
        System.out.println("using optProblem: " + optProbFile.getAbsolutePath());
        OptServerImpl optServerImpl = new OptServerImpl();
        OptProblem optProblem = CopasiServicePython.readOptProblem(optProbFile);
        ArrayList<String> jobIDs = new ArrayList<String>();
        jobIDs.add(optServerImpl.submit(optProblem));
        jobIDs.add(optServerImpl.submit(optProblem));
        optServerImpl.start();
        jobIDs.add(optServerImpl.submit(optProblem));
        jobIDs.add(optServerImpl.submit(optProblem));
        boolean done = false;
        while (!done) {
            done = true;
            for (String jobID : jobIDs) {
                OptRunContext optRunContext = optServerImpl.getOptRunContextByOptimizationId(jobID);
                if (optRunContext == null) {
                    throw new RuntimeException("optRunContext was null for id = " + jobID);
                }
                OptRunStatus status = optRunContext.getStatus();
                if (status != OptRunStatus.Complete && status != OptRunStatus.Failed) {
                    done = false;
                }
                if (status == OptRunStatus.Complete) {
                    OptRun optRun = CopasiServicePython.readOptRun(optRunContext.getOptRunBinaryFile());
                    System.out.println("job " + jobID + ": status " + status + " " + optRun.getOptResultSet().toString());
                } else {
                    System.out.println("job " + jobID + ": status " + status);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with all jobs");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : OptProblem(org.vcell.optimization.thrift.OptProblem) ArrayList(java.util.ArrayList) OptRunContext(org.vcell.optimization.OptServerImpl.OptRunContext) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) File(java.io.File)

Example 2 with OptRun

use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.

the class CopasiServicePython method readOptRun.

public static OptRun readOptRun(File optRunFile) throws IOException {
    TDeserializer deserializer = new TDeserializer(new TBinaryProtocol.Factory());
    try {
        OptRun optJob = new OptRun();
        byte[] bytes = FileUtils.readFileToByteArray(optRunFile);
        deserializer.deserialize(optJob, bytes);
        return optJob;
    } catch (TException e) {
        e.printStackTrace();
        throw new IOException("error reading optRun from file " + optRunFile.getPath() + ": " + e.getMessage(), e);
    }
}
Also used : TException(org.apache.thrift.TException) TDeserializer(org.apache.thrift.TDeserializer) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) OptRun(org.vcell.optimization.thrift.OptRun) IOException(java.io.IOException)

Example 3 with OptRun

use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.

the class OptimizationApiTest method main.

public static void main(String[] args) {
    try {
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        File optProbFile = new File("../pythonScripts/VCell_Opt/optprob.bin");
        System.out.println("using optProblem: " + optProbFile.getAbsolutePath());
        OptProblem optProblem = readOptProblem(optProbFile);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        ArrayList<String> jobIDs = new ArrayList<String>();
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        jobIDs.add(apiClient.submitOptimization(optProblemJson));
        boolean done = false;
        while (!done) {
            done = true;
            for (String jobID : jobIDs) {
                String optRunJson = apiClient.getOptRunJson(jobID);
                TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
                OptRun optRun = new OptRun();
                deserializer.deserialize(optRun, optRunJson.getBytes());
                OptRunStatus status = optRun.status;
                if (status != OptRunStatus.Complete && status != OptRunStatus.Failed) {
                    done = false;
                }
                if (status == OptRunStatus.Complete) {
                    System.out.println("job " + jobID + ": status " + status + " " + optRun.getOptResultSet().toString());
                } else {
                    System.out.println("job " + jobID + ": status " + status);
                }
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with all jobs");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : TDeserializer(org.apache.thrift.TDeserializer) OptProblem(org.vcell.optimization.thrift.OptProblem) ArrayList(java.util.ArrayList) VCellApiClient(org.vcell.api.client.VCellApiClient) TException(org.apache.thrift.TException) IOException(java.io.IOException) TSerializer(org.apache.thrift.TSerializer) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) File(java.io.File)

Example 4 with OptRun

use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.

the class CopasiOptimizationSolver method solveRemoteApi.

public static OptimizationResultSet solveRemoteApi(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback) throws IOException, ExpressionException, OptimizationException {
    try {
        OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
        boolean bIgnoreCertProblems = true;
        boolean bIgnoreHostMismatch = true;
        // e.g. vcell.serverhost=vcellapi.cam.uchc.edu:8080
        String serverHost = PropertyLoader.getRequiredProperty(PropertyLoader.vcellServerHost);
        String[] parts = serverHost.split(":");
        String host = parts[0];
        int port = Integer.parseInt(parts[1]);
        VCellApiClient apiClient = new VCellApiClient(host, port, bIgnoreCertProblems, bIgnoreHostMismatch);
        TSerializer serializer = new TSerializer(new TJSONProtocol.Factory());
        String optProblemJson = serializer.toString(optProblem);
        String optimizationId = apiClient.submitOptimization(optProblemJson);
        // 20 second minute timeout
        final long TIMEOUT_MS = 1000 * 20;
        long startTime = System.currentTimeMillis();
        OptRun optRun = null;
        while ((System.currentTimeMillis() - startTime) < TIMEOUT_MS) {
            if (optSolverCallbacks.getStopRequested()) {
                throw new RuntimeException(STOP_REQUESTED);
            }
            String optRunJson = apiClient.getOptRunJson(optimizationId);
            TDeserializer deserializer = new TDeserializer(new TJSONProtocol.Factory());
            optRun = new OptRun();
            deserializer.deserialize(optRun, optRunJson.getBytes());
            OptRunStatus status = optRun.status;
            if (status == OptRunStatus.Complete) {
                System.out.println("job " + optimizationId + ": status " + status + " " + optRun.getOptResultSet().toString());
                break;
            }
            if (status == OptRunStatus.Failed) {
                throw new RuntimeException("optimization failed, message=" + optRun.statusMessage);
            }
            System.out.println("job " + optimizationId + ": status " + status);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
        }
        System.out.println("done with optimization");
        OptResultSet optResultSet = optRun.getOptResultSet();
        int numFittedParameters = optResultSet.getOptParameterValues().size();
        String[] paramNames = new String[numFittedParameters];
        double[] paramValues = new double[numFittedParameters];
        for (int pIndex = 0; pIndex < numFittedParameters; pIndex++) {
            OptParameterValue optParamValue = optResultSet.getOptParameterValues().get(pIndex);
            paramNames[pIndex] = optParamValue.parameterName;
            paramValues[pIndex] = optParamValue.bestValue;
        }
        OptimizationStatus status = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, optRun.statusMessage);
        OptRunResultSet optRunResultSet = new OptRunResultSet(paramValues, optResultSet.objectiveFunction, optResultSet.numFunctionEvaluations, status);
        OptSolverResultSet copasiOptSolverResultSet = new OptSolverResultSet(paramNames, optRunResultSet);
        RowColumnResultSet copasiRcResultSet = parestSimulator.getRowColumnRestultSetByBestEstimations(parameterEstimationTask, paramNames, paramValues);
        OptimizationResultSet copasiOptimizationResultSet = new OptimizationResultSet(copasiOptSolverResultSet, copasiRcResultSet);
        System.out.println("-----------SOLUTION FROM VCellAPI---------------\n" + optResultSet.toString());
        return copasiOptimizationResultSet;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        if (e.getMessage() != null && e.getMessage().equals(STOP_REQUESTED)) {
            throw UserCancelException.CANCEL_GENERIC;
        }
        throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
    }
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) TDeserializer(org.apache.thrift.TDeserializer) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptParameterValue(org.vcell.optimization.thrift.OptParameterValue) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) TJSONProtocol(org.apache.thrift.protocol.TJSONProtocol) OptRunStatus(org.vcell.optimization.thrift.OptRunStatus) OptRun(org.vcell.optimization.thrift.OptRun) OptResultSet(org.vcell.optimization.thrift.OptResultSet) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) OptProblem(org.vcell.optimization.thrift.OptProblem) VCellApiClient(org.vcell.api.client.VCellApiClient) OptimizationException(cbit.vcell.opt.OptimizationException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException) MathException(cbit.vcell.math.MathException) UserCancelException(org.vcell.util.UserCancelException) TSerializer(org.apache.thrift.TSerializer) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)

Example 5 with OptRun

use of org.vcell.optimization.thrift.OptRun in project vcell by virtualcell.

the class CopasiOptimizationSolver method solveLocalPython.

public static OptimizationResultSet solveLocalPython(ParameterEstimationTaskSimulatorIDA parestSimulator, ParameterEstimationTask parameterEstimationTask, CopasiOptSolverCallbacks optSolverCallbacks, MathMappingCallback mathMappingCallback) throws IOException, ExpressionException, OptimizationException {
    File dir = Files.createTempDirectory("parest", new FileAttribute<?>[] {}).toFile();
    try {
        String prefix = "testing_" + Math.abs(new Random().nextInt(10000));
        File optProblemThriftFile = new File(dir, prefix + ".optprob.bin");
        File optRunFile = new File(dir, prefix + ".optrun.bin");
        // 
        // Setup Python COPASI opt problem and write to disk
        // 
        OptProblem optProblem = CopasiServicePython.makeOptProblem(parameterEstimationTask);
        CopasiServicePython.writeOptProblem(optProblemThriftFile, optProblem);
        // 
        // run Python COPASI opt problem
        // 
        CopasiServicePython.runCopasiPython(optProblemThriftFile, optRunFile);
        if (!optRunFile.exists()) {
            throw new RuntimeException("COPASI optimization output file not found:\n" + optRunFile.getAbsolutePath());
        }
        OptRun optRun = CopasiServicePython.readOptRun(optRunFile);
        OptResultSet optResultSet = optRun.getOptResultSet();
        int numFittedParameters = optResultSet.getOptParameterValues().size();
        String[] paramNames = new String[numFittedParameters];
        double[] paramValues = new double[numFittedParameters];
        for (int pIndex = 0; pIndex < numFittedParameters; pIndex++) {
            OptParameterValue optParamValue = optResultSet.getOptParameterValues().get(pIndex);
            paramNames[pIndex] = optParamValue.parameterName;
            paramValues[pIndex] = optParamValue.bestValue;
        }
        OptimizationStatus status = new OptimizationStatus(OptimizationStatus.NORMAL_TERMINATION, optRun.statusMessage);
        OptRunResultSet optRunResultSet = new OptRunResultSet(paramValues, optResultSet.objectiveFunction, optResultSet.numFunctionEvaluations, status);
        OptSolverResultSet copasiOptSolverResultSet = new OptSolverResultSet(paramNames, optRunResultSet);
        RowColumnResultSet copasiRcResultSet = parestSimulator.getRowColumnRestultSetByBestEstimations(parameterEstimationTask, paramNames, paramValues);
        OptimizationResultSet copasiOptimizationResultSet = new OptimizationResultSet(copasiOptSolverResultSet, copasiRcResultSet);
        System.out.println("-----------SOLUTION FROM PYTHON---------------\n" + optResultSet.toString());
        return copasiOptimizationResultSet;
    } catch (Throwable e) {
        e.printStackTrace(System.out);
        throw new OptimizationException(e.getCause() != null ? e.getCause().getMessage() : e.getMessage());
    } finally {
        if (dir != null && dir.exists()) {
            FileUtils.deleteDirectory(dir);
        }
    }
}
Also used : OptimizationException(cbit.vcell.opt.OptimizationException) OptProblem(org.vcell.optimization.thrift.OptProblem) OptimizationResultSet(cbit.vcell.opt.OptimizationResultSet) OptParameterValue(org.vcell.optimization.thrift.OptParameterValue) OptSolverResultSet(cbit.vcell.opt.OptSolverResultSet) Random(java.util.Random) OptRun(org.vcell.optimization.thrift.OptRun) OptResultSet(org.vcell.optimization.thrift.OptResultSet) OptimizationStatus(cbit.vcell.opt.OptimizationStatus) OptRunResultSet(cbit.vcell.opt.OptSolverResultSet.OptRunResultSet) File(java.io.File) FileAttribute(java.nio.file.attribute.FileAttribute) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet)

Aggregations

OptRun (org.vcell.optimization.thrift.OptRun)9 OptProblem (org.vcell.optimization.thrift.OptProblem)6 IOException (java.io.IOException)4 TSerializer (org.apache.thrift.TSerializer)4 TJSONProtocol (org.apache.thrift.protocol.TJSONProtocol)4 File (java.io.File)3 TDeserializer (org.apache.thrift.TDeserializer)3 ResourceException (org.restlet.resource.ResourceException)3 OptRunStatus (org.vcell.optimization.thrift.OptRunStatus)3 VCellApiApplication (org.vcell.rest.VCellApiApplication)3 ObjectNotFoundException (org.vcell.util.ObjectNotFoundException)3 PermissionException (org.vcell.util.PermissionException)3 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)2 OptSolverResultSet (cbit.vcell.opt.OptSolverResultSet)2 OptRunResultSet (cbit.vcell.opt.OptSolverResultSet.OptRunResultSet)2 OptimizationException (cbit.vcell.opt.OptimizationException)2 OptimizationResultSet (cbit.vcell.opt.OptimizationResultSet)2 OptimizationStatus (cbit.vcell.opt.OptimizationStatus)2 ArrayList (java.util.ArrayList)2 TException (org.apache.thrift.TException)2