use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class DisplayTimeSeriesOp method getDataSetControllerProvider.
private DataSetControllerProvider getDataSetControllerProvider(final ImageTimeSeries<? extends Image> imageTimeSeries, final PDEDataViewer pdeDataViewer) throws ImageException, IOException {
ISize size = imageTimeSeries.getISize();
int dimension = (size.getZ() > 0) ? (3) : (2);
Extent extent = imageTimeSeries.getExtent();
Origin origin = imageTimeSeries.getAllImages()[0].getOrigin();
// don't care ... no surfaces
double filterCutoffFrequency = 0.5;
VCImage vcImage = new VCImageUncompressed(null, new byte[size.getXYZ()], extent, size.getX(), size.getY(), size.getZ());
RegionImage regionImage = new RegionImage(vcImage, dimension, extent, origin, filterCutoffFrequency);
final CartesianMesh mesh = CartesianMesh.createSimpleCartesianMesh(origin, extent, size, regionImage);
final DataIdentifier dataIdentifier = new DataIdentifier("var", VariableType.VOLUME, new Domain("domain"), false, "var");
final DataSetController dataSetController = new DataSetController() {
@Override
public ExportEvent makeRemoteFile(OutputContext outputContext, ExportSpecs exportSpecs) throws DataAccessException, RemoteProxyException {
throw new RuntimeException("not yet implemented");
}
@Override
public TimeSeriesJobResults getTimeSeriesValues(OutputContext outputContext, VCDataIdentifier vcdataID, TimeSeriesJobSpec timeSeriesJobSpec) throws RemoteProxyException, DataAccessException {
pdeDataViewer.dataJobMessage(new DataJobEvent(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_START, vcdataID.getDataKey(), vcdataID.getID(), new Double(0)));
if (!timeSeriesJobSpec.isCalcSpaceStats() && !timeSeriesJobSpec.isCalcTimeStats()) {
int[][] indices = timeSeriesJobSpec.getIndices();
double[] timeStamps = imageTimeSeries.getImageTimeStamps();
// [var][dataindex+1][timeindex]
double[][][] dataValues = new double[1][indices[0].length + 1][timeStamps.length];
for (int timeIndex = 0; timeIndex < timeStamps.length; timeIndex++) {
// index 0 is time
dataValues[0][0][timeIndex] = timeStamps[timeIndex];
}
for (int timeIndex = 0; timeIndex < timeStamps.length; timeIndex++) {
float[] pixelValues = imageTimeSeries.getAllImages()[timeIndex].getFloatPixels();
for (int samplePointIndex = 0; samplePointIndex < indices[0].length; samplePointIndex++) {
int pixelIndex = indices[0][samplePointIndex];
dataValues[0][samplePointIndex + 1][timeIndex] = pixelValues[pixelIndex];
}
}
TSJobResultsNoStats timeSeriesJobResults = new TSJobResultsNoStats(new String[] { "var" }, indices, timeStamps, dataValues);
pdeDataViewer.dataJobMessage(new DataJobEvent(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_COMPLETE, vcdataID.getDataKey(), vcdataID.getID(), new Double(0)));
return timeSeriesJobResults;
}
return null;
}
@Override
public SimDataBlock getSimDataBlock(OutputContext outputContext, VCDataIdentifier vcdataID, String varName, double time) throws RemoteProxyException, DataAccessException {
double timePoint = time;
double[] timePoints = getDataSetTimes(vcdataID);
int index = -1;
for (int i = 0; i < timePoints.length; i++) {
if (timePoint == timePoints[i]) {
index = i;
break;
}
}
double[] data = imageTimeSeries.getAllImages()[index].getDoublePixels();
PDEDataInfo pdeDataInfo = new PDEDataInfo(null, null, varName, time, 0);
VariableType varType = VariableType.VOLUME;
return new SimDataBlock(pdeDataInfo, data, varType);
}
@Override
public boolean getParticleDataExists(VCDataIdentifier vcdataID) throws DataAccessException, RemoteProxyException {
return false;
}
@Override
public ParticleDataBlock getParticleDataBlock(VCDataIdentifier vcdataID, double time) throws DataAccessException, RemoteProxyException {
return null;
}
@Override
public ODESimData getODEData(VCDataIdentifier vcdataID) throws DataAccessException, RemoteProxyException {
return null;
}
@Override
public CartesianMesh getMesh(VCDataIdentifier vcdataID) throws RemoteProxyException, DataAccessException {
return mesh;
}
@Override
public PlotData getLineScan(OutputContext outputContext, VCDataIdentifier vcdataID, String variable, double time, SpatialSelection spatialSelection) throws RemoteProxyException, DataAccessException {
throw new RuntimeException("not yet implemented");
}
@Override
public AnnotatedFunction[] getFunctions(OutputContext outputContext, VCDataIdentifier vcdataID) throws DataAccessException, RemoteProxyException {
return new AnnotatedFunction[0];
}
@Override
public double[] getDataSetTimes(VCDataIdentifier vcdataID) throws RemoteProxyException, DataAccessException {
return imageTimeSeries.getImageTimeStamps();
}
@Override
public DataSetTimeSeries getDataSetTimeSeries(VCDataIdentifier vcdataID, String[] variableNames) throws DataAccessException, RemoteProxyException {
throw new RuntimeException("not yet implemented");
}
@Override
public DataSetMetadata getDataSetMetadata(VCDataIdentifier vcdataID) throws DataAccessException, RemoteProxyException {
throw new RuntimeException("not yet implemented");
}
@Override
public DataIdentifier[] getDataIdentifiers(OutputContext outputContext, VCDataIdentifier vcdataID) throws RemoteProxyException, DataAccessException {
return new DataIdentifier[] { dataIdentifier };
}
@Override
public FieldDataFileOperationResults fieldDataFileOperation(FieldDataFileOperationSpec fieldDataFileOperationSpec) throws RemoteProxyException, DataAccessException {
throw new RuntimeException("not yet implemented");
}
@Override
public DataOperationResults doDataOperation(DataOperation dataOperation) throws DataAccessException, RemoteProxyException {
throw new RuntimeException("not yet implemented");
}
@Override
public VtuFileContainer getEmptyVtuMeshFiles(VCDataIdentifier vcdataID, int timeIndex) throws RemoteProxyException, DataAccessException {
throw new RuntimeException("not yet implemented");
}
@Override
public double[] getVtuTimes(VCDataIdentifier vcdataID) throws RemoteProxyException, DataAccessException {
throw new RuntimeException("not yet implemented");
}
@Override
public double[] getVtuMeshData(OutputContext outputContext, VCDataIdentifier vcdataID, VtuVarInfo var, double time) throws RemoteProxyException, DataAccessException {
// TODO Auto-generated method stub
return null;
}
@Override
public VtuVarInfo[] getVtuVarInfos(OutputContext outputContext, VCDataIdentifier vcDataIdentifier) throws DataAccessException, RemoteProxyException {
// TODO Auto-generated method stub
return null;
}
@Override
public NFSimMolecularConfigurations getNFSimMolecularConfigurations(VCDataIdentifier vcdataID) throws RemoteProxyException, DataAccessException {
// TODO Auto-generated method stub
return null;
}
};
DataSetControllerProvider dataSetControllerProvider = new DataSetControllerProvider() {
@Override
public DataSetController getDataSetController() throws DataAccessException {
return dataSetController;
}
};
return dataSetControllerProvider;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class VFrapXmlHelper method LoadVFrapSpecialImages.
// // load and compute prebleach average and first postbleach images
// public void LoadVFrapSpecialImages(AnnotatedImageDataset annotatedImages, int startingIndexRecovery)
// {
// // unnormalized prebleach average
// prebleachAvg = new double[annotatedImages.getImageDataset().getImage(0, 0, startingIndexRecovery).getNumXYZ()];
// for(int j = 0; j < prebleachAvg.length; j++)
// {
// double pixelTotal = 0;
// for(int i = 0 ; i < startingIndexRecovery; i++)
// {
// pixelTotal = pixelTotal + (annotatedImages.getImageDataset().getImage(0, 0, i).getPixels()[j] & 0x0000FFFF);
// }
// prebleachAvg[j] = pixelTotal/startingIndexRecovery;
// }
//
// // unnormalized first post bleach
// firstPostBleach = new double[annotatedImages.getImageDataset().getImage(0, 0, startingIndexRecovery).getNumXYZ()];
// short[] pixels = annotatedImages.getImageDataset().getImage(0, 0, startingIndexRecovery).getPixels();
// for(int i = 0; i< pixels.length; i++)
// {
// firstPostBleach[i] = pixels[i] & 0x0000FFFF;
// }
// }
//
// Locate the special images within the vFrap files and load them in memory
//
public static boolean LoadVFrapSpecialImages(Hashtable<String, Object> hashTable, Element vFrapRoot) throws IOException, DataAccessException, MathException, ImageException {
// ------ parse the vfrap file and the log/zip files referred within -----
// many channels of 1 timepoint each
int NumTimePoints = 1;
// the channels: prebleach, postbleach, roi1, roi2 ... roiN
int NumChannels = tokenNames.length;
String[] channelNames = new String[NumChannels];
VariableType[] channelTypes = new VariableType[NumChannels];
DataSymbolType[] channelVFrapImageType = new DataSymbolType[NumChannels];
double[][][] pixData = new double[NumTimePoints][NumChannels][];
// get the path of the file tagged with "ROIExternalDataInfoTag" and open it
Element roiExternalDataInfoElement = vFrapRoot.getChild(MicroscopyXMLTags.ROIExternalDataInfoTag);
if (roiExternalDataInfoElement == null) {
// can't load FieldData for some reason, fall back to importing the biomodel only
return false;
}
// <ROIExternalDataInfo Filename="c:\vFrap\VirtualMicroscopy\SimulationData\SimID_1282941232246_0_.log">
// <ExternalDataIdentifier Name="timeData" KeyValue="1282941232246" OwnerName="SimulationData" OwnerKey="0" />
// </ImageDatasetExternalDataInfo>
// c:\VirtualMicroscopy\SimulationData\SimID_1284149203811_0_.log
String filename = (roiExternalDataInfoElement).getAttributeValue("Filename");
Element childElement = (roiExternalDataInfoElement).getChild("ExternalDataIdentifier");
if (childElement == null) {
// can't load FieldData for some reason, fall back to importing the biomodel only
return false;
}
StringTokenizer tokens = new StringTokenizer(filename, "/\\.");
final ArrayList<String> tokenArray = new ArrayList<String>();
while (tokens.hasMoreElements()) {
tokenArray.add(tokens.nextToken());
}
final String dataID = tokenArray.get(tokenArray.size() - 2);
final String userName = tokenArray.get(tokenArray.size() - 3);
VCDataIdentifier vcDataIdentifier = new VCDataIdentifier() {
public String getID() {
return dataID;
}
public KeyValue getDataKey() {
return null;
}
public User getOwner() {
return new User(userName, new KeyValue("123345432334"));
}
};
// ------- recover simulation data for this user name, load the images in memory ------------
// ex c:\\VirtualMicroscopy\\SimulationData
String userDirName = filename.substring(0, filename.indexOf(dataID) - 1);
File userDir = new File(userDirName);
SimulationData.SimDataAmplistorInfo simDataAmplistorInfo = AmplistorUtils.getSimDataAmplistorInfoFromPropertyLoader();
SimulationData simData = new SimulationData(vcDataIdentifier, userDir, null, simDataAmplistorInfo);
// build a valid mesh in 2 steps, what we have in simData is incomplete
CartesianMesh incompleteMesh = simData.getMesh();
Extent extent = incompleteMesh.getExtent();
ISize isize = new ISize(incompleteMesh.getSizeX(), incompleteMesh.getSizeY(), incompleteMesh.getSizeZ());
Origin origin = new Origin(0, 0, 0);
CartesianMesh mesh = CartesianMesh.createSimpleCartesianMesh(origin, extent, isize, new RegionImage(new VCImageUncompressed(null, new byte[isize.getXYZ()], extent, isize.getX(), isize.getY(), isize.getZ()), 0, null, null, RegionImage.NO_SMOOTHING));
DataIdentifier[] dataIdentifiers = simData.getVarAndFunctionDataIdentifiers(null);
double[] times = simData.getDataTimes();
for (int i = 0; i < dataIdentifiers.length; i++) {
// ex: prebleach_avg, postbleach_first, postbleach_last, bleached_mask, cell_mask, ring1_mask,... ring8_mask
System.out.println(dataIdentifiers[i].getName());
for (double time : times) {
// this loops only once, we have just 1 timepoint for each "special" image
SimDataBlock simDataBlock = simData.getSimDataBlock(null, dataIdentifiers[i].getName(), time);
channelNames[i] = dataIdentifiers[i].getName();
channelTypes[i] = VariableType.VOLUME;
channelVFrapImageType[i] = SymbolEquivalence.typeFromToken(dataIdentifiers[i].getName());
pixData[0][i] = simDataBlock.getData();
// var = prebleach_avg, time = 0.0, data = { 1.0832530361887216 1.0832530361887216 1.0832530361887216 1.0 .... }
System.out.print("var = " + dataIdentifiers[i].getName() + ", time = " + time + ", data = { ");
// show a few
for (int j = 0; j < 5; j++) {
System.out.print(pixData[0][i][j] + " ");
}
// show a few
;
// show a few
System.out.println(" ... ");
}
}
hashTable.put("mesh", mesh);
hashTable.put("pixData", pixData);
hashTable.put("channelNames", channelNames);
hashTable.put("channelTypes", channelTypes);
hashTable.put("channelVFrapImageType", channelVFrapImageType);
return true;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class ASCIIExporter method getSlice.
/**
* This method was created in VisualAge.
* @return java.io.File
* @param variable java.lang.String
* @param time double
* @throws IOException
*/
private FileDataContainerID getSlice(OutputContext outputContext, User user, DataServerImpl dataServerImpl, VCDataIdentifier vcdID, String variable, int timeIndex, String slicePlane, int sliceNumber, boolean switchRowsColumns, FileDataContainerManager fileDataContainerManager) throws DataAccessException, IOException {
double[] allTimes = dataServerImpl.getDataSetTimes(user, vcdID);
double timepoint = allTimes[timeIndex];
SimDataBlock simDataBlock = dataServerImpl.getSimDataBlock(outputContext, user, vcdID, variable, timepoint);
double[] data = simDataBlock.getData();
cbit.vcell.solvers.CartesianMesh mesh = dataServerImpl.getMesh(user, vcdID);
int[] sizeXYZ = { mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ() };
FileDataContainerID fileDataContainerID = fileDataContainerManager.getNewFileDataContainerID();
if (simDataBlock.getVariableType().equals(VariableType.VOLUME) || simDataBlock.getVariableType().equals(VariableType.POSTPROCESSING)) {
//
// put data in csv format
//
fileDataContainerManager.append(fileDataContainerID, "2D Slice for variable " + variable + " at time " + timepoint);
if (slicePlane.equals(Coordinate.getNormalAxisPlaneName(Coordinate.Z_AXIS))) {
fileDataContainerManager.append(fileDataContainerID, " in plane XY at Z = " + sliceNumber + "\n\n");
int start = sliceNumber * sizeXYZ[0] * sizeXYZ[1];
if (switchRowsColumns) {
fileDataContainerManager.append(fileDataContainerID, "X in rows, Y in columns\n");
for (int j = 0; j < sizeXYZ[0]; j++) {
for (int i = 0; i < sizeXYZ[1]; i++) {
fileDataContainerManager.append(fileDataContainerID, data[start + i * sizeXYZ[0] + j] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
} else {
fileDataContainerManager.append(fileDataContainerID, "X in columns, Y in rows\n");
for (int i = 0; i < sizeXYZ[1]; i++) {
for (int j = 0; j < sizeXYZ[0]; j++) {
fileDataContainerManager.append(fileDataContainerID, data[start + i * sizeXYZ[0] + j] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
}
}
if (slicePlane.equals(Coordinate.getNormalAxisPlaneName(Coordinate.Y_AXIS))) {
fileDataContainerManager.append(fileDataContainerID, " in plane XZ at Y = " + sliceNumber + "\n\n");
int start = sliceNumber * sizeXYZ[0];
if (switchRowsColumns) {
fileDataContainerManager.append(fileDataContainerID, "X in rows, Z in columns\n");
for (int i = 0; i < sizeXYZ[0]; i++) {
for (int j = 0; j < sizeXYZ[2]; j++) {
fileDataContainerManager.append(fileDataContainerID, data[start + j * sizeXYZ[0] * sizeXYZ[1] + i] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
} else {
fileDataContainerManager.append(fileDataContainerID, "X in columns, Z in rows\n");
for (int j = 0; j < sizeXYZ[2]; j++) {
for (int i = 0; i < sizeXYZ[0]; i++) {
fileDataContainerManager.append(fileDataContainerID, data[start + j * sizeXYZ[0] * sizeXYZ[1] + i] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
}
}
if (slicePlane.equals(Coordinate.getNormalAxisPlaneName(Coordinate.X_AXIS))) {
fileDataContainerManager.append(fileDataContainerID, " in plane YZ at X = " + sliceNumber + "\n\n");
int start = sliceNumber;
if (switchRowsColumns) {
fileDataContainerManager.append(fileDataContainerID, "Y in rows, Z in columns\n");
for (int j = 0; j < sizeXYZ[1]; j++) {
for (int i = 0; i < sizeXYZ[2]; i++) {
fileDataContainerManager.append(fileDataContainerID, data[start + i * sizeXYZ[0] * sizeXYZ[1] + j * sizeXYZ[0]] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
} else {
fileDataContainerManager.append(fileDataContainerID, "Y in columns, Z in rows\n");
for (int i = 0; i < sizeXYZ[2]; i++) {
for (int j = 0; j < sizeXYZ[1]; j++) {
fileDataContainerManager.append(fileDataContainerID, data[start + i * sizeXYZ[0] * sizeXYZ[1] + j * sizeXYZ[0]] + ",");
}
fileDataContainerManager.append(fileDataContainerID, "\n");
}
}
}
// } else if (mesh.getGeometryDimension() < 3) {
} else {
// membrane variable; we export the data by index
// for 3D one gets the whole dataset for now... warning at the client level... will get more sophisticated later...
fileDataContainerManager.append(fileDataContainerID, "Data for membrane variable " + variable + " at time " + timepoint + "\nEntire datablock by index\n\n");
for (int i = 0; i < data.length; i++) {
fileDataContainerManager.append(fileDataContainerID, data[i] + "\n");
}
// buffer.append("\n");
// } else {
// throw new RuntimeException("3D export for membrane or region variables not supported yet");
}
return fileDataContainerID;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class MathTestingUtilities method comparePDEResults.
/**
* Insert the method's description here.
* Creation date: (8/20/2003 12:58:10 PM)
*/
public static SimulationComparisonSummary comparePDEResults(SimulationSymbolTable testSimSymbolTable, PDEDataManager testDataManager, SimulationSymbolTable refSimSymbolTable, PDEDataManager refDataManager, String[] varsToCompare, double absErrorThreshold, double relErrorThreshold, VCDocument refDocument, DataInfoProvider refDataInfoProvider, VCDocument testDocument, DataInfoProvider testDataInfoProvider) throws DataAccessException, ExpressionException {
java.util.Hashtable<String, DataErrorSummary> tempVarHash = new java.util.Hashtable<String, DataErrorSummary>();
boolean bTimesEqual = true;
double[] testTimeArray = testDataManager.getDataSetTimes();
double[] refTimeArray = refDataManager.getDataSetTimes();
if (testTimeArray.length != refTimeArray.length) {
bTimesEqual = false;
// throw new RuntimeException("Data times for reference and test simulations don't match, cannot compare the two simulations!");
} else {
for (int i = 0; i < testTimeArray.length; i++) {
if (testTimeArray[i] != refTimeArray[i]) {
bTimesEqual = false;
}
}
}
// if (!checkSimVars(testSim, refSim)) {
// //String errorS = "TestVars - ";
// Variable[] testVars = testSim.getVariables();
// //for(int i =0;i<vars.length;i+= 1){
// //errorS+= vars[i].getName()+" ";
// //}
// //errorS+=" <<>> RefVars - ";
// Variable[] refVars = refSim.getVariables();
// //for(int i =0;i<vars.length;i+= 1){
// //errorS+= vars[i].getName()+" ";
// //}
// throw new RuntimeException(
// "VarNotMatch testLength="+(testVars != null?testVars.length+"":"null")+" refLength="+(refVars != null?refVars.length+"":"null"));
// }
CartesianMesh testMesh = testDataManager.getMesh();
MathDescription testMathDesc = testSimSymbolTable.getSimulation().getMathDescription();
// Variable[] refVars = refSim.getVariables();
MathDescription refMathDesc = refSimSymbolTable.getSimulation().getMathDescription();
CartesianMesh refMesh = refDataManager.getMesh();
int[] membraneIndexMapping = null;
// Get volumeSubdomains from mathDesc/mesh and store in lookupTable for testSimulation
int testNumVol = testMesh.getSizeX() * testMesh.getSizeY() * testMesh.getSizeZ();
CompartmentSubDomain[] testVolSubDomainLookup = new CompartmentSubDomain[testNumVol];
for (int i = 0; i < testNumVol; i++) {
int subVolumeIndex = testMesh.getSubVolumeFromVolumeIndex(i);
SubVolume subVolume = testMathDesc.getGeometry().getGeometrySpec().getSubVolume(subVolumeIndex);
CompartmentSubDomain compSubDomain = testMathDesc.getCompartmentSubDomain(subVolume.getName());
testVolSubDomainLookup[i] = compSubDomain;
}
// Get membraneSubdomains from mathDesc/mesh and store in lookupTable for testSimulation
int testNumMem = testMesh.getMembraneElements().length;
MembraneSubDomain[] testMemSubDomainLookup = new MembraneSubDomain[testNumMem];
for (int i = 0; i < testNumMem; i++) {
int insideVolIndex = testMesh.getMembraneElements()[i].getInsideVolumeIndex();
int outsideVolIndex = testMesh.getMembraneElements()[i].getOutsideVolumeIndex();
MembraneSubDomain memSubDomain = testMathDesc.getMembraneSubDomain(testVolSubDomainLookup[insideVolIndex], testVolSubDomainLookup[outsideVolIndex]);
testMemSubDomainLookup[i] = memSubDomain;
}
// Get volumeSubdomains from mathDesc/mesh and store in lookupTable for refSimulation
int refNumVol = refMesh.getSizeX() * refMesh.getSizeY() * refMesh.getSizeZ();
CompartmentSubDomain[] refVolSubDomainLookup = new CompartmentSubDomain[refNumVol];
for (int i = 0; i < refNumVol; i++) {
int subVolumeIndex = refMesh.getSubVolumeFromVolumeIndex(i);
SubVolume subVolume = refMathDesc.getGeometry().getGeometrySpec().getSubVolume(subVolumeIndex);
CompartmentSubDomain compSubDomain = refMathDesc.getCompartmentSubDomain(subVolume.getName());
refVolSubDomainLookup[i] = compSubDomain;
}
// Get membraneSubdomains from mathDesc/mesh and store in lookupTable for refSimulation
int refNumMem = refMesh.getMembraneElements().length;
MembraneSubDomain[] refMemSubDomainLookup = new MembraneSubDomain[refNumMem];
for (int i = 0; i < refNumMem; i++) {
int insideVolIndex = refMesh.getMembraneElements()[i].getInsideVolumeIndex();
int outsideVolIndex = refMesh.getMembraneElements()[i].getOutsideVolumeIndex();
MembraneSubDomain memSubDomain = refMathDesc.getMembraneSubDomain(refVolSubDomainLookup[insideVolIndex], refVolSubDomainLookup[outsideVolIndex]);
refMemSubDomainLookup[i] = memSubDomain;
}
SimulationComparisonSummary simComparisonSummary = new SimulationComparisonSummary();
String hashKey = new String("");
DataErrorSummary tempVar = null;
DataIdentifier[] refDataIDs = refDataManager.getDataIdentifiers();
// for each var, do the following :
for (int i = 0; i < varsToCompare.length; i++) {
DataIdentifier refDataID = null;
for (int j = 0; j < refDataIDs.length; j++) {
if (refDataIDs[j].getName().equals(varsToCompare[i])) {
refDataID = refDataIDs[j];
break;
}
}
//
// Find REFERENCE variable
//
Variable refVar = getSimVar(refSimSymbolTable, varsToCompare[i]);
if (refVar == null) {
// Should only happen if TEST sims were generated 'post-domains' and REFERENCE sims were generated 'pre-domains'
if (refDataID != null) {
throw new RuntimeException("Unexpected reference condition: '" + varsToCompare[i] + "' not found in symboltable but was found in dataidentifiers");
}
if (testDocument instanceof BioModel) {
// Only BioModels need to be checked
// Look in TEST for a speciescontext with matching species name
System.out.println("ReferenceVariable: using alternate method to find '" + varsToCompare[i] + "'");
BioModel refBioModel = (BioModel) refDocument;
BioModel testBioModel = (BioModel) testDocument;
SpeciesContext testSpeciesContext = testBioModel.getModel().getSpeciesContext(varsToCompare[i]);
if (testSpeciesContext != null) {
refVar = refSimSymbolTable.getVariable(testSpeciesContext.getSpecies().getCommonName());
for (int j = 0; j < refDataIDs.length; j++) {
if (refDataIDs[j].getName().equals(testSpeciesContext.getSpecies().getCommonName())) {
refDataID = refDataIDs[j];
break;
}
}
}
}
if (refVar == null || refDataID == null) {
Simulation refSim = refSimSymbolTable.getSimulation();
throw new RuntimeException("The variable " + varsToCompare[i] + " was not found in Simulation (" + refSim.getName() + " " + refSim.getVersion().getDate() + ")\n");
}
}
//
// Find TEST variable (assumed to have sims generated with a software version later than REFERENCE)
//
Variable testVar = getSimVar(testSimSymbolTable, varsToCompare[i]);
if (testVar == null) {
// Should only happen if TEST sims were generated 'post-domains' and REFERENCE sims were generated 'pre-domains'
System.out.println("TestVariable: using alternate method to find '" + varsToCompare[i] + "'");
BioModel testBioModel = (BioModel) testDocument;
SpeciesContext[] speciesContexts = testBioModel.getModel().getSpeciesContexts();
boolean bSkip = false;
for (int j = 0; j < speciesContexts.length; j++) {
if (speciesContexts[j].getSpecies().getCommonName().equals(varsToCompare[i])) {
testVar = testSimSymbolTable.getVariable(speciesContexts[j].getName());
if (testVar == null) {
throw new RuntimeException("Speciescontext name '" + speciesContexts[j].getName() + "' not found in testsimsymboltable");
}
// If we got here it means at least one matching speciescontext was found in TEST with
// a species name matching varsToCompare[i]. We can skip because the matching speciesconetext
// will be used to do a comparison at some point.
bSkip = true;
break;
}
}
if (bSkip) {
// these are tested already using full simcontext names
System.out.println("Skipping '" + varsToCompare[i] + "' as lookup in testSimSymbolTable");
continue;
}
Simulation refSim = refSimSymbolTable.getSimulation();
throw new RuntimeException("The variable " + varsToCompare[i] + " was not found in Simulation (" + refSim.getName() + " " + refSim.getVersion().getDate() + ")\n");
}
// for each time in timeArray. ('t' is used to index the testTimeArray, for interpolation purposes.)
int t = 0;
for (int j = 0; j < refTimeArray.length; j++) {
// get data block from varName, data from datablock
SimDataBlock refSimDataBlock = refDataManager.getSimDataBlock(refVar.getName(), refTimeArray[j]);
double[] refData = refSimDataBlock.getData();
double[] resampledTestData = null;
if (bTimesEqual) {
// If time arrays for both sims are equal, no need to resample/interpolate, just obtain the datablock from dataManager
SimDataBlock testSimDataBlock = testDataManager.getSimDataBlock(testVar.getName(), testTimeArray[j]);
resampledTestData = testSimDataBlock.getData();
} else {
// Time resampling (interpolation) needed.
while ((t < testTimeArray.length - 2) && (refTimeArray[j] >= testTimeArray[t + 1])) {
t++;
}
SimDataBlock testSimDataBlock_1 = testDataManager.getSimDataBlock(testVar.getName(), testTimeArray[t]);
double[] testData_1 = testSimDataBlock_1.getData();
SimDataBlock testSimDataBlock_2 = testDataManager.getSimDataBlock(testVar.getName(), testTimeArray[t + 1]);
double[] testData_2 = testSimDataBlock_2.getData();
resampledTestData = new double[testData_1.length];
//
for (int m = 0; m < testData_1.length; m++) {
resampledTestData[m] = testData_1[m] + (testData_2[m] - testData_1[m]) * (refTimeArray[j] - testTimeArray[t]) / (testTimeArray[t + 1] - testTimeArray[t]);
}
}
// Spatial resampling (interpolation) ...
double[] spaceResampledData = new double[refData.length];
if (!testMathDesc.getGeometry().getExtent().compareEqual(refMathDesc.getGeometry().getExtent()) || !testMathDesc.getGeometry().getOrigin().compareEqual(refMathDesc.getGeometry().getOrigin())) {
throw new RuntimeException("Different origins and/or extents for the 2 geometries. Cannot compare the 2 simulations");
}
if (testMesh.getSizeX() != refMesh.getSizeX() || testMesh.getSizeY() != refMesh.getSizeY() || testMesh.getSizeZ() != refMesh.getSizeZ()) {
if (testVar instanceof VolVariable) {
if (testMathDesc.getGeometry().getDimension() == 1 && refMathDesc.getGeometry().getDimension() == 1) {
spaceResampledData = resample1DSpatial(resampledTestData, testMesh, refMesh);
} else if (testMathDesc.getGeometry().getDimension() == 2 && refMathDesc.getGeometry().getDimension() == 2) {
spaceResampledData = resample2DSpatial(resampledTestData, testMesh, refMesh);
} else if (testMathDesc.getGeometry().getDimension() == 3 && refMathDesc.getGeometry().getDimension() == 3) {
spaceResampledData = resample3DSpatial(resampledTestData, testMesh, refMesh);
} else {
throw new RuntimeException("Comparison of 2 simulations with different geometry dimensions are not handled at this time!");
}
} else {
throw new RuntimeException("spatial resampling for variable type: " + testVar.getClass().getName() + " not supported");
}
} else {
// no space resampling required
if (testVar instanceof MemVariable) {
//
if (membraneIndexMapping == null) {
membraneIndexMapping = testMesh.getMembraneIndexMapping(refMesh);
}
spaceResampledData = new double[resampledTestData.length];
for (int k = 0; k < resampledTestData.length; k++) {
spaceResampledData[k] = resampledTestData[membraneIndexMapping[k]];
}
} else {
//
// no reordering needed for other variable types.
//
spaceResampledData = resampledTestData;
}
}
// for each point in data block ...
testDataInfoProvider.getPDEDataContext().setVariableName(testVar.getName());
for (int k = 0; k < refData.length; k++) {
if (!testDataInfoProvider.isDefined(k)) {
continue;
}
// Determine maxRef, minRef, maxAbsErr for variable
// SubDomain testSubDomain = null;
String sn = null;
VariableType refVarType = refDataID.getVariableType();
if (refVarType.equals(VariableType.VOLUME)) {
// testSubDomain = refVolSubDomainLookup[k];
sn = refVolSubDomainLookup[k].getName();
} else if (refVarType.equals(VariableType.MEMBRANE)) {
// testSubDomain = refMemSubDomainLookup[k];
sn = refMemSubDomainLookup[k].getName();
} else if (refVarType.equals(VariableType.MEMBRANE_REGION)) {
sn = "MRV_" + i;
} else if (refVarType.equals(VariableType.VOLUME_REGION)) {
sn = "VRV_" + i;
} else {
throw new RuntimeException("Var " + refVar.getName() + " not supported yet!");
}
// hashKey = refVar.getName()+":"+testSubDomain.getName();
hashKey = refVar.getName() + ":" + sn;
tempVar = tempVarHash.get(hashKey);
if (tempVar == null) {
tempVar = new DataErrorSummary(null);
tempVarHash.put(hashKey, tempVar);
}
tempVar.addDataValues(refData[k], spaceResampledData[k], refTimeArray[j], k, absErrorThreshold, relErrorThreshold);
}
// end for (k)
}
// end for (j)
}
// end for (i)
Enumeration<String> enumKeys = tempVarHash.keys();
while (enumKeys.hasMoreElements()) {
String key = enumKeys.nextElement();
DataErrorSummary tempVarSummary = tempVarHash.get(key);
simComparisonSummary.addVariableComparisonSummary(new VariableComparisonSummary(key, tempVarSummary.getMinRef(), tempVarSummary.getMaxRef(), tempVarSummary.getMaxAbsoluteError(), tempVarSummary.getMaxRelativeError(), tempVarSummary.getL2Norm(), tempVarSummary.getTimeAtMaxAbsoluteError(), tempVarSummary.getIndexAtMaxAbsoluteError(), tempVarSummary.getTimeAtMaxRelativeError(), tempVarSummary.getIndexAtMaxRelativeError()));
}
return simComparisonSummary;
}
use of cbit.vcell.simdata.SimDataBlock in project vcell by virtualcell.
the class HybridSolverTester method makeAltCSV.
private static void makeAltCSV(AltArgsHelper altArgsHelper, FileWriter fw, int runIndex, File userSimDataDir) throws Exception {
VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(new KeyValue(altArgsHelper.simID), altArgsHelper.user);
boolean bInit = runIndex == 0;
ArrayList<Double> simTimes = new ArrayList<Double>();
StringTokenizer st = null;
if (altArgsHelper.times.equals("all")) {
} else {
st = new StringTokenizer(altArgsHelper.times, ":");
while (st.hasMoreTokens()) {
double timePoint = Double.parseDouble(st.nextToken());
simTimes.add(timePoint);
}
}
SimLocHelper simLocHelper0 = null;
ArrayList<String> simVars = new ArrayList<String>();
st = new StringTokenizer(altArgsHelper.varnames, ":");
while (st.hasMoreTokens()) {
String var = st.nextToken();
simVars.add(var);
}
int jobCounter = 0;
final int TIME_SPACE_EXTRA = 0;
double[][][] trialData = null;
while (true) {
VCSimulationDataIdentifier vcSimulationDataIdentifier = new VCSimulationDataIdentifier(vcSimID, jobCounter);
SimulationData simData = null;
DataOperationResults.DataProcessingOutputInfo dataProcessingOutputInfo = null;
try {
simData = new SimulationData(vcSimulationDataIdentifier, userSimDataDir, null, null);
dataProcessingOutputInfo = (DataOperationResults.DataProcessingOutputInfo) DataSetControllerImpl.getDataProcessingOutput(new DataOperation.DataProcessingOutputInfoOP(vcSimulationDataIdentifier, true, null), new File(userSimDataDir, SimulationData.createCanonicalPostProcessFileName(vcSimulationDataIdentifier)));
} catch (FileNotFoundException e) {
if (jobCounter == 0) {
System.out.println("found no trials matching SimID=" + altArgsHelper.simID + " in user dir " + userSimDataDir.getAbsolutePath());
} else {
System.out.println("found " + jobCounter + " trials in dir " + userSimDataDir.getAbsolutePath() + " matching SimID=" + altArgsHelper.simID);
}
break;
} catch (Exception e) {
e.printStackTrace();
}
if (dataProcessingOutputInfo == null) {
System.out.println("No postprocessing found for " + jobCounter + " trials in dir " + userSimDataDir.getAbsolutePath() + " matching SimID=" + altArgsHelper.simID);
jobCounter++;
continue;
}
if (simLocHelper0 == null && !altArgsHelper.dataIndexes.equals(POSTPROC)) {
simLocHelper0 = calcSimLocs(altArgsHelper.dataIndexes, simData.getMesh());
}
if (jobCounter == 0) {
double[] allDatasetTimes = simData.getDataTimes();
if (altArgsHelper.times.equals("all")) {
for (double thisTime : allDatasetTimes) {
simTimes.add(thisTime);
}
} else {
// Convert user input times to actual data times
for (int times = 0; times < simTimes.size(); times++) {
double masterDelta = Double.POSITIVE_INFINITY;
double timePoint = -1;
for (int j = 0; j < allDatasetTimes.length; j++) {
double tempDelta = Math.abs(simTimes.get(times) - allDatasetTimes[j]);
if (tempDelta < masterDelta) {
masterDelta = tempDelta;
timePoint = allDatasetTimes[j];
if (tempDelta == 0) {
break;
}
}
}
System.out.println("User time=" + simTimes.get(times) + " converted to dataset time=" + timePoint);
simTimes.set(times, timePoint);
}
}
trialData = new double[simTimes.size()][(simLocHelper0 == null ? 1 : simLocHelper0.boxToLocs.size())][simVars.size()];
}
if (bInit && jobCounter == 0) {
// print state vars
DataIdentifier[] dataIdentifiers = simData.getVarAndFunctionDataIdentifiers(null);
for (int j = 0; j < dataIdentifiers.length; j++) {
System.out.println(dataIdentifiers[j]);
}
printheader(altArgsHelper, simTimes, simLocHelper0, simVars, fw, TIME_SPACE_EXTRA);
}
for (int times = 0; times < simTimes.size(); times++) {
double timePoint = simTimes.get(times);
for (int vars = 0; vars < simVars.size(); vars++) {
double[] data = null;
if (altArgsHelper.dataIndexes.equals(POSTPROC)) {
data = dataProcessingOutputInfo.getVariableStatValues().get(simVars.get(vars) + "_average");
} else {
SimDataBlock simDataBlock = simData.getSimDataBlock(null, simVars.get(vars), timePoint);
data = simDataBlock.getData();
}
for (int locs = 0; locs < trialData[times].length; locs++) {
double val;
if (simLocHelper0 != null && simLocHelper0.boxToLocs.get(locs).size() == 1) {
// point
// System.out.println("pointIndex="+simLocHelper.boxToLocs.get(locs).get(0));
val = data[simLocHelper0.boxToLocs.get(locs).get(0)];
} else if (simLocHelper0 != null) {
// box, calculate the average, could be concentration or counts
double accum = 0;
for (Integer locIndex : simLocHelper0.boxToLocs.get(locs)) {
// System.out.println("boxIndex="+locIndex);
accum += data[locIndex];
}
val = accum / simLocHelper0.boxToLocs.get(locs).size();
} else {
// PostProcess
if (times < data.length) {
val = data[times];
} else {
val = Double.NaN;
}
}
trialData[times][locs][vars] = val;
}
}
}
fw.write("r=" + runIndex + " s=" + jobCounter + ",");
for (int times = 0; times < simTimes.size(); times++) {
for (int locs = 0; locs < trialData[times].length; locs++) {
for (int vars = 0; vars < simVars.size(); vars++) {
// System.out.println("job="+jobCounter+" time="+simTimes.get(times)+" loc="+simLocHelper.boxToID.get(locs)+" var="+simVars.get(vars)+" data="+trialData[times][locs][vars]);
boolean isNan = Double.isNaN(trialData[times][locs][vars]);
fw.write((isNan ? "" : trialData[times][locs][vars]) + ",");
}
fw.write(",");
}
for (int timeSpace = 0; timeSpace < TIME_SPACE_EXTRA; timeSpace++) {
fw.write(",");
}
}
fw.write("\n");
jobCounter++;
}
fw.flush();
}
Aggregations