use of org.vcell.vcellij.api.VariableInfo in project vcell by virtualcell.
the class SimulationServiceImpl method getVariableList.
public List<VariableInfo> getVariableList(SimulationInfo simInfo) throws ThriftDataAccessException, TException {
SimulationServiceContext simServiceContext = sims.get(simInfo.id);
if (simServiceContext == null) {
throw new ThriftDataAccessException("simulation results not found");
}
try {
DataSetControllerImpl datasetController = getDataSetController(simServiceContext);
OutputContext outputContext = new OutputContext(new AnnotatedFunction[0]);
final DataIdentifier[] dataIdentifiers;
try {
dataIdentifiers = datasetController.getDataIdentifiers(outputContext, simServiceContext.vcDataIdentifier);
} catch (IOException | DataAccessException e) {
e.printStackTrace();
throw new RuntimeException("failed to retrieve variable information: " + e.getMessage(), e);
}
ArrayList<VariableInfo> varInfos = new ArrayList<VariableInfo>();
for (DataIdentifier dataIdentifier : dataIdentifiers) {
final DomainType domainType;
if (dataIdentifier.getVariableType().equals(VariableType.VOLUME)) {
domainType = DomainType.VOLUME;
} else if (dataIdentifier.getVariableType().equals(VariableType.MEMBRANE)) {
domainType = DomainType.MEMBRANE;
} else {
continue;
}
String domainName = "";
if (dataIdentifier.getDomain() != null) {
domainName = dataIdentifier.getDomain().getName();
}
VariableInfo varInfo = new VariableInfo(dataIdentifier.getName(), dataIdentifier.getDisplayName(), domainName, domainType);
varInfos.add(varInfo);
}
return varInfos;
} catch (Exception e) {
e.printStackTrace();
throw new ThriftDataAccessException("failed to retrieve variable list: " + e.getMessage());
}
}
use of org.vcell.vcellij.api.VariableInfo in project vcell by virtualcell.
the class VCellService method runSimulation.
private static Task<List<Dataset>, SimulationState> runSimulation(final SimulationServiceImpl client, final VCellModel vCellModel, final SimulationSpec simSpec, final List<Species> outputSpecies, final boolean shouldCreateIndividualDatasets, final OpService opService, final DatasetService datasetService) throws IOException, XMLStreamException {
final Task<List<Dataset>, SimulationState> task = new Task<List<Dataset>, SimulationState>() {
@Override
protected List<Dataset> doInBackground() throws Exception {
setSubtask(SimulationState.notRun);
final File sbmlSpatialFile = new File(vCellModel.getName() + ".xml");
new SBMLWriter().write(vCellModel.getSbmlDocument(), sbmlSpatialFile);
final SBMLModel model = new SBMLModel(sbmlSpatialFile);
final SimulationInfo simulationInfo = client.computeModel(model, simSpec);
try {
Thread.sleep(500);
} catch (final InterruptedException e) {
e.printStackTrace();
}
setSubtask(SimulationState.running);
while (client.getStatus(simulationInfo).getSimState() == SimulationState.running) {
System.out.println("waiting for simulation results");
try {
Thread.sleep(500);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
if (client.getStatus(simulationInfo).getSimState() == SimulationState.failed) {
setSubtask(SimulationState.failed);
return null;
}
final List<Dataset> results = new ArrayList<>();
final List<VariableInfo> vars = client.getVariableList(simulationInfo);
final List<Double> times = client.getTimePoints(simulationInfo);
for (final VariableInfo var : vars) {
if (outputSpecies.stream().anyMatch(species -> species.getId().equals(var.getVariableVtuName()))) {
// Get data for first time point and determine dimensions
List<Double> data = client.getData(simulationInfo, var, 0);
final int[] dimensions = getDimensions(data, times);
final Img<DoubleType> img = opService.create().img(dimensions);
final RandomAccess<DoubleType> imgRA = img.randomAccess();
// Copy data to the ImgLib2 Img
for (int t = 0; t < times.size(); t++) {
data = client.getData(simulationInfo, var, t);
for (int d = 0; d < data.size(); d++) {
imgRA.setPosition(new int[] { d, t });
imgRA.get().set(data.get(d));
}
}
// Create ImageJ Dataset and add to results
final Dataset dataset = datasetService.create(img);
dataset.setName(var.getVariableVtuName());
results.add(dataset);
}
}
// If desired, add all datasets with the same dimensions
if (!shouldCreateIndividualDatasets && !results.isEmpty()) {
// First, group datasets according to dimensions
final List<List<Dataset>> datasetGroups = new ArrayList<>();
final List<Dataset> initialGroup = new ArrayList<>();
initialGroup.add(results.get(0));
datasetGroups.add(initialGroup);
for (int i = 1; i < results.size(); i++) {
final Dataset result = results.get(i);
for (final List<Dataset> datasetGroup : datasetGroups) {
final Dataset[] datasets = new Dataset[] { datasetGroup.get(0), result };
if (Datasets.areSameSize(datasets, 0, 1)) {
datasetGroup.add(result);
} else {
final List<Dataset> newGroup = new ArrayList<>();
newGroup.add(result);
datasetGroups.add(newGroup);
}
}
}
final List<Dataset> summedResults = new ArrayList<>();
for (final List<Dataset> datasetGroup : datasetGroups) {
final Img<DoubleType> sum = opService.create().img(datasetGroup.get(0));
for (final Dataset dataset : datasetGroup) {
@SuppressWarnings("unchecked") final RandomAccessibleInterval<DoubleType> current = (Img<DoubleType>) dataset.getImgPlus().getImg();
opService.math().add(sum, sum, current);
}
final Dataset result = datasetService.create(sum);
result.setName(datasetGroup.stream().map(d -> d.getName()).collect(Collectors.joining("+")));
summedResults.add(result);
}
return summedResults;
}
setSubtask(SimulationState.done);
return results;
}
};
return task;
}
Aggregations