Skip to content

Commit

Permalink
Merge branch 'master' into validate
Browse files Browse the repository at this point in the history
  • Loading branch information
gmarupilla committed Apr 8, 2021
2 parents 374316a + 273bbad commit 380b4ad
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 39 deletions.
125 changes: 96 additions & 29 deletions vcell-client/src/main/java/cbit/vcell/client/ClientSimManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
package cbit.vcell.client;

import java.awt.Dimension;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -23,6 +25,7 @@
import java.util.EventObject;
import java.util.Hashtable;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Vector;

import javax.swing.SwingUtilities;
Expand Down Expand Up @@ -54,6 +57,7 @@
import cbit.vcell.client.desktop.simulation.SimulationWindow.LocalState;
import cbit.vcell.client.desktop.simulation.SimulationWorkspace;
import cbit.vcell.client.pyvcellproxy.SimulationDataSetRef;
import cbit.vcell.client.server.ClientServerManager;
import cbit.vcell.client.server.UserPreferences;
import cbit.vcell.client.task.AsynchClientTask;
import cbit.vcell.client.task.AsynchClientTaskFunction;
Expand All @@ -63,10 +67,13 @@
import cbit.vcell.field.FieldDataIdentifierSpec;
import cbit.vcell.mapping.SimulationContext;
import cbit.vcell.mapping.SimulationContext.NetworkGenerationRequirements;
import cbit.vcell.message.server.bootstrap.client.RemoteProxyVCellConnectionFactory.RemoteProxyException;
import cbit.vcell.messaging.server.SimulationTask;
import cbit.vcell.parser.ExpressionException;
import cbit.vcell.resource.PropertyLoader;
import cbit.vcell.resource.ResourceUtil;
import cbit.vcell.resource.VCellConfiguration;
import cbit.vcell.server.DataSetController;
import cbit.vcell.server.SimulationStatus;
import cbit.vcell.simdata.DataManager;
import cbit.vcell.simdata.DataSetControllerImpl;
Expand All @@ -88,12 +95,15 @@
import cbit.vcell.solver.TempSimulation;
import cbit.vcell.solver.VCSimulationDataIdentifier;
import cbit.vcell.solver.VCSimulationIdentifier;
import cbit.vcell.solver.ode.ODESimData;
import cbit.vcell.solver.ode.ODESolverResultSet;
import cbit.vcell.solver.server.SimulationMessage;
import cbit.vcell.solver.server.Solver;
import cbit.vcell.solver.server.SolverEvent;
import cbit.vcell.solver.server.SolverFactory;
import cbit.vcell.solver.server.SolverListener;
import cbit.vcell.solver.server.SolverStatus;
import cbit.vcell.util.ColumnDescription;

/**
* Insert the type's description here.
Expand Down Expand Up @@ -287,8 +297,9 @@ private static void saveFailure(Hashtable<String, Object>hashTable,Simulation si
failures.put(sim, throwable);
}

public void importBatchSimulations(OutputContext outputContext, Simulation simulation) throws java.beans.PropertyVetoException {
public void getBatchSimulationsResults(OutputContext outputContext, Simulation simulation) throws java.beans.PropertyVetoException {



// simulation should be a template simulation
if(simulation.getName().contains(SimulationContext.ReservedBatchExtensionString)) {
Expand All @@ -305,35 +316,82 @@ public void importBatchSimulations(OutputContext outputContext, Simulation simul
throw new RuntimeException("Cannot add simulation, bioModel not set");
}

File batchResultsDir = ResourceUtil.getLocalBatchDir();
// File localSimDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());

ArrayList<AsynchClientTask> taskList = new ArrayList<AsynchClientTask>();
AsynchClientTask retrieveResultsTask = new AsynchClientTask("Retrieving results", AsynchClientTask.TASKTYPE_NONSWING_BLOCKING) {
public void run(Hashtable<String, Object> hashTable) throws Exception {

// recover the list of batch simulations that belong to this template
Simulation allSims[] = bioModel.getSimulations();
LinkedHashMap<String, String> importsMap = new LinkedHashMap<>();
LinkedHashMap <String, Boolean> successMap = new LinkedHashMap<>();
String namePrefix = simulation.getName() + SimulationContext.ReservedBatchExtensionString;

for(Simulation simCandidate : allSims) {
if(simCandidate.getName().startsWith(namePrefix) && simCandidate.getName().contains("_bat_")) {

int pos = simCandidate.getName().lastIndexOf("_");
String indexName = simCandidate.getName().substring(pos+1);
File currentSimulation = new File(batchResultsDir, indexName + ".txt");

ODESimData simData = null;
importsMap.put(simCandidate.getName(), simCandidate.getSimulationID());
successMap.put(simCandidate.getName(), true); // on failure we'll change to false

// recover the list of batch simulations that belong to this template
Simulation allSims[] = bioModel.getSimulations();
LinkedHashMap<String, String> importsMap = new LinkedHashMap<>();
String namePrefix = simulation.getName() + SimulationContext.ReservedBatchExtensionString;
for(Simulation simCandidate : allSims) {
if(simCandidate.getName().startsWith(namePrefix)) {
importsMap.put(simCandidate.getName(), simCandidate.getSimulationID());
System.out.println(simCandidate.getName() + ": " + simCandidate.getSimulationID());
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// importBatchSimulation(outputContext, simCandidate);
try {
simData = importBatchSimulation(outputContext, simCandidate);
} catch(Exception e) { // whatever fails, we keep going, this is a batch run
System.out.println(simCandidate.getName() + ": failed to recover simulation results");

// TODO: also make a report with detailed exception text
// e.printStackTrace();
successMap.put(simCandidate.getName(), false);
}
if(simData != null) { // write the file
// double[] res = osrs.extractColumn(4);

StringBuilder sb = new StringBuilder();
for(ColumnDescription cd : simData.getDataColumnDescriptions()) {
sb.append(cd.getName() + " ");
}
sb.append("\r\n");

for(double[] row : simData.getRows()) {
for(double entry : row) {
sb.append(entry);
sb.append(" ");
}
sb.append("\r\n");
}
PrintWriter out = new PrintWriter(currentSimulation);
out.print(sb.toString());
out.flush();
out.close();
}
}
});
}
}
// for(String name : importsMap.keySet()) {
// String value = importsMap.get(name);
// SwingUtilities.invokeLater(new Runnable() {
// public void run() {
// // write manifest
}

// for(String name : importsMap.keySet()) {
// String value = importsMap.get(name);
// write some manifest?
// // at least some basic info like the name of the simulation / biomodel / dat file
// }
// });
// }
System.out.println("Done !!!");
} // --- end run()
};
taskList.add(retrieveResultsTask);
AsynchClientTask[] taskArray = new AsynchClientTask[taskList.size()];
taskList.toArray(taskArray);
// knowProgress, cancelable, progressDialogListener
ClientTaskDispatcher.dispatch(documentWindowManager.getComponent(), new Hashtable<String, Object>(), taskArray, false, false, null);
}
private void importBatchSimulation(OutputContext outputContext, Simulation sim) {

File localBatchDir = ResourceUtil.getLocalBatchDir();
File localSimDir = ResourceUtil.getLocalSimDir(User.tempUser.getName());
private ODESimData importBatchSimulation(OutputContext outputContext, Simulation sim) throws DataAccessException, RemoteProxyException, ExpressionException {

boolean success = false;
ODESimData simData = null;

if(sim.getVersion() == null) {
throw new RuntimeException("Missing Version.");
Expand All @@ -346,10 +404,18 @@ private void importBatchSimulation(OutputContext outputContext, Simulation sim)
User usr = sim.getVersion().getOwner();


// VCSimulationIdentifier authoritativeVCSimulationIdentifier = simInfo.getAuthoritativeVCSimulationIdentifier();
// // sim.getScanCount() number of jobs - is 0
// VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(authoritativeVCSimulationIdentifier, 0);
// VCellClientTest.getVCellClient().getClientServerManager().getDataSetController().getODEData(vcSimulationDataIdentifier);
VCSimulationIdentifier asi = simInfo.getAuthoritativeVCSimulationIdentifier();
// sim.getScanCount() number of jobs must be one, so job index is 0
VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(asi, 0);

ClientServerManager csm = VCellClientTest.getVCellClient().getClientServerManager();
DataSetController dsc = csm.getDataSetController();
simData = dsc.getODEData(vcSimulationDataIdentifier);

System.out.println(sim.getName() + ": simulation results recovered");
return simData;



/*
Expand Down Expand Up @@ -393,6 +459,7 @@ private void importBatchSimulation(OutputContext outputContext, Simulation sim)
}
*/

}

private AsynchClientTask[] showSimulationResults0(final boolean isLocal, final ViewerType viewerType) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public void actionPerformed(java.awt.event.ActionEvent e) {
stopSimulations();
} else if (e.getSource() == exportBatchButton) {
// DialogUtils.showInfoDialog(SimulationListPanel.this, "Under Construction");
importBatchSimulations();
getBatchSimulationsResults();
} else if (e.getSource() == importBatchButton) {
createBatchSimulations();
} else if (e.getSource() == getNativeResultsButton()) {
Expand Down Expand Up @@ -278,7 +278,7 @@ private void createBatchSimulations() {
getScrollPaneTable().scrollRectToVisible(getScrollPaneTable().getCellRect(index, 0, true));
}

private void importBatchSimulations() {
private void getBatchSimulationsResults() {
int[] selections = getScrollPaneTable().getSelectedRows();
if(selections.length != 1) {
throw new RuntimeException("Exactly one template Simulation is required for Batch results Import");
Expand All @@ -289,8 +289,17 @@ private void importBatchSimulations() {
Simulation[] toImport = (Simulation[])BeanUtils.getArray(v, Simulation.class);
int index = -1;

// one way to choose dir for results
// other way in ClientSimManager.getBatchSimulationResults()
// UserPreferences up = getSimulationWorkspace().getClientSimManager().getUserPreferences();
// File batchOutputDir = chooseBatchOutputDirectory(up);
// if(batchOutputDir == null) {
// System.out.println("Failed to create batch output directory or user canceled");
// return;
// }

try {
index = getSimulationWorkspace().importBatchSimulations(toImport, this);
index = getSimulationWorkspace().getBatchSimulationsResults(toImport, this);
} catch (Throwable exc) {
exc.printStackTrace(System.out);
PopupGenerator.showErrorDialog(this, exc.getMessage(), exc);
Expand All @@ -300,6 +309,11 @@ private void importBatchSimulations() {
getScrollPaneTable().scrollRectToVisible(getScrollPaneTable().getCellRect(index, 0, true));
}

private File chooseBatchOutputDirectory(UserPreferences userPreferences) {
File batchOutputDirectory = null;

return batchOutputDirectory;
}
private File parseBatchInputFile(UserPreferences userPreferences, Map<Integer, Map<String, String>> batchInputDataMap) {

StringBuffer stringBuffer = new StringBuffer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,15 +389,15 @@ int createBatchSimulations(Simulation[] sims, Map<Integer, Map<String, String>>
}
return -1;
}
int importBatchSimulations(Simulation[] sims, Component requester) throws java.beans.PropertyVetoException {
int getBatchSimulationsResults(Simulation[] sims, Component requester) throws java.beans.PropertyVetoException {
if (sims == null || sims.length == 0) {
return -1;
}

// sims contains exactly one template simulation
ArrayList<AnnotatedFunction> outputFunctionsList = getSimulationOwner().getOutputFunctionContext().getOutputFunctionsList();
OutputContext outputContext = new OutputContext(outputFunctionsList.toArray(new AnnotatedFunction[outputFunctionsList.size()]));
getClientSimManager().importBatchSimulations(outputContext, sims[0]);
getClientSimManager().getBatchSimulationsResults(outputContext, sims[0]);
// getSimulationOwner().importBatchSimulations(sims[0]); // was in simContext initially
return -1;
}
Expand Down
25 changes: 20 additions & 5 deletions vcell-core/src/main/java/cbit/vcell/resource/ResourceUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,30 @@ public static File getLocalRootDir()
return localRootDir;
}

private static void deleteRecursively(File f) throws IOException {
if (f.isDirectory()) {
for (File c : f.listFiles()) {
deleteRecursively(c);
}
}
if (!f.delete()) {
throw new FileNotFoundException("Failed to delete file: " + f);
}
}
public static File getLocalBatchDir()
{
if(localBatchDir == null)
{
localBatchDir = new File(getVcellHome(), "batchdata");
if (!localBatchDir.exists()) {
localBatchDir.mkdirs();
File adir = new File(getVcellHome(), "batchResults");
if(adir.exists()) {
try {
deleteRecursively(adir); // delete the output directory and all its content recursively
} catch (IOException e) {
System.err.println("Failed to empty " + adir.getName());
}
}
if(!adir.exists()) {
adir.mkdirs();
}
localBatchDir = adir;
return localBatchDir;
}

Expand Down

0 comments on commit 380b4ad

Please sign in to comment.