use of cbit.vcell.solvers.CartesianMesh in project vcell by virtualcell.
the class DataSetControllerImpl method getSpecialTimeSeriesValues.
private TimeSeriesJobResults getSpecialTimeSeriesValues(OutputContext outputContext, VCDataIdentifier vcdID, TimeSeriesJobSpec timeSeriesJobSpec, TimeInfo timeInfo) throws Exception {
String[] variableNames = timeSeriesJobSpec.getVariableNames();
CartesianMesh mesh = getMesh(vcdID);
// Automatically 'special' (non-optimized timedata retrieval) if isAllowOptimizedTimeDataRetrieval() == false
boolean bIsSpecial = !isAllowOptimizedTimeDataRetrieval();
if (!bIsSpecial) {
VCData simData = getVCData(vcdID);
//
// Gradient and FieldData functions are special.
// They have to be evaluated using the 'full data' method using evaluateFunction(...).
// They cannot be evaluated using the fast findFunctionIndexes(...) method.
// Also if the 'roi' is a significant fraction of the whole dataset then
// the 'full data' method of evaluation is more efficient
// a guess for best efficiency
final double SIGNIFICANT_ROI_FRACTION = .2;
final int ABSOLUTE_SIZE_LIMIT = 10000;
for (int i = 0; i < variableNames.length; i += 1) {
DataSetIdentifier dsi = (DataSetIdentifier) simData.getEntry(variableNames[i]);
if (dsi.getVariableType().equals(VariableType.VOLUME)) {
if (timeSeriesJobSpec.getIndices()[i].length > mesh.getNumVolumeElements() * SIGNIFICANT_ROI_FRACTION) {
bIsSpecial = true;
break;
}
} else if (dsi.getVariableType().equals(VariableType.MEMBRANE)) {
if (timeSeriesJobSpec.getIndices()[i].length > mesh.getNumMembraneElements() * SIGNIFICANT_ROI_FRACTION) {
bIsSpecial = true;
break;
}
}
AnnotatedFunction functionFromVarName = getFunction(outputContext, vcdID, variableNames[i]);
if (functionFromVarName != null) {
FieldFunctionArguments[] fieldFunctionArgumentsArr = FieldUtilities.getFieldFunctionArguments(functionFromVarName.getExpression());
if (hasGradient(functionFromVarName.getExpression()) || (fieldFunctionArgumentsArr != null && fieldFunctionArgumentsArr.length > 0)) {
bIsSpecial = true;
break;
}
// check function absolute size limit
Expression exp = functionFromVarName.getExpression();
String[] funcSymbols = exp.getSymbols();
int varCount = 0;
if (funcSymbols != null) {
for (int j = 0; j < funcSymbols.length; j++) {
SymbolTableEntry ste = exp.getSymbolBinding(funcSymbols[j]);
if (ste instanceof DataSetIdentifier) {
varCount += 1;
}
}
}
varCount = Math.max(varCount, 1);
if (varCount * timeSeriesJobSpec.getIndices()[i].length > ABSOLUTE_SIZE_LIMIT) {
bIsSpecial = true;
break;
}
} else if (timeSeriesJobSpec.getIndices()[i].length > ABSOLUTE_SIZE_LIMIT) {
bIsSpecial = true;
break;
}
}
}
if (!bIsSpecial) {
return null;
}
TimeSeriesJobResults tsjr = null;
if (timeSeriesJobSpec.isCalcTimeStats()) {
throw new RuntimeException("Time Stats Not yet implemented for 'special' data");
} else if (timeSeriesJobSpec.isCalcSpaceStats()) {
// Get spatial statistics at each time point
SpatialStatsInfo ssi = calcSpatialStatsInfo(outputContext, timeSeriesJobSpec, vcdID);
double[][] /*time*/
argMin = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[][] argMax = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[][] argUnweightedMean = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[][] argWeightedMean = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[][] argUnweightedSum = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[][] argWeightedSum = new double[variableNames.length][timeInfo.desiredTimeValues.length];
double[] /*varName*/
argTotalSpace = new double[variableNames.length];
double[][][] indicesForVarForOneTimepoint = new double[1][][];
for (int varNameIndex = 0; varNameIndex < variableNames.length; varNameIndex += 1) {
int[] dataIndices = timeSeriesJobSpec.getIndices()[varNameIndex];
indicesForVarForOneTimepoint[0] = new double[dataIndices.length + 1][1];
for (int timeIndex = 0; timeIndex < timeInfo.desiredTimeValues.length; timeIndex += 1) {
indicesForVarForOneTimepoint[0][0] = new double[] { timeInfo.desiredTimeValues[timeIndex] };
int num = varNameIndex * timeInfo.desiredTimeValues.length + timeIndex;
int denom = variableNames.length * timeInfo.desiredTimeValues.length;
double progressTime = 100.0 * (double) num / (double) denom;
fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(progressTime), null, null);
SimDataBlock simDatablock = getSimDataBlock(outputContext, vcdID, variableNames[varNameIndex], timeInfo.desiredTimeValues[timeIndex]);
double[] data = simDatablock.getData();
// Put indices in format expected by calcStats (SHOULD BE CHANGED)
for (int dataIndex = 0; dataIndex < dataIndices.length; dataIndex += 1) {
indicesForVarForOneTimepoint[0][dataIndex + 1][0] = data[dataIndices[dataIndex]];
}
if (timeSeriesJobSpec.getCrossingMembraneIndices() != null && timeSeriesJobSpec.getCrossingMembraneIndices().length > 0) {
adjustMembraneAdjacentVolumeValues(outputContext, indicesForVarForOneTimepoint[0], true, simDatablock, timeSeriesJobSpec.getIndices()[varNameIndex], timeSeriesJobSpec.getCrossingMembraneIndices()[varNameIndex], vcdID, variableNames[varNameIndex], mesh, timeInfo);
}
tsjr = calculateStatisticsFromWhole(timeSeriesJobSpec, indicesForVarForOneTimepoint, indicesForVarForOneTimepoint[0][0], ssi);
argMin[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getMinimums()[0][0];
argMax[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getMaximums()[0][0];
argUnweightedMean[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getUnweightedMean()[0][0];
if (((TSJobResultsSpaceStats) tsjr).getWeightedMean() == null) {
argWeightedMean = null;
} else {
argWeightedMean[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getWeightedMean()[0][0];
}
argUnweightedSum[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getUnweightedSum()[0][0];
if (((TSJobResultsSpaceStats) tsjr).getWeightedSum() == null) {
argWeightedSum = null;
} else {
argWeightedSum[varNameIndex][timeIndex] = ((TSJobResultsSpaceStats) tsjr).getWeightedSum()[0][0];
}
if (((TSJobResultsSpaceStats) tsjr).getTotalSpace() == null) {
argTotalSpace = null;
} else {
argTotalSpace[varNameIndex] = ((TSJobResultsSpaceStats) tsjr).getTotalSpace()[0];
}
}
}
tsjr = new TSJobResultsSpaceStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), timeInfo.desiredTimeValues, argMin, argMax, argUnweightedMean, argWeightedMean, argUnweightedSum, argWeightedSum, argTotalSpace);
} else {
// Get the values for for all the variables and indices
double[][][] varIndicesTimesArr = new double[variableNames.length][][];
for (int varNameIndex = 0; varNameIndex < variableNames.length; varNameIndex += 1) {
int[] dataIndices = timeSeriesJobSpec.getIndices()[varNameIndex];
varIndicesTimesArr[varNameIndex] = new double[dataIndices.length + 1][timeInfo.desiredTimeValues.length];
varIndicesTimesArr[varNameIndex][0] = timeInfo.desiredTimeValues;
for (int timeIndex = 0; timeIndex < timeInfo.desiredTimeValues.length; timeIndex += 1) {
int num = varNameIndex * timeInfo.desiredTimeValues.length + timeIndex;
int denom = variableNames.length * timeInfo.desiredTimeValues.length;
double progressTime = 100.0 * (double) num / (double) denom;
fireDataJobEventIfNecessary(timeSeriesJobSpec.getVcDataJobID(), MessageEvent.DATA_PROGRESS, vcdID, new Double(progressTime), null, null);
SimDataBlock simDatablock = getSimDataBlock(outputContext, vcdID, variableNames[varNameIndex], timeInfo.desiredTimeValues[timeIndex]);
double[] data = simDatablock.getData();
for (int dataIndex = 0; dataIndex < dataIndices.length; dataIndex += 1) {
varIndicesTimesArr[varNameIndex][dataIndex + 1][timeIndex] = data[dataIndices[dataIndex]];
}
if (timeSeriesJobSpec.getCrossingMembraneIndices() != null && timeSeriesJobSpec.getCrossingMembraneIndices().length > 0) {
adjustMembraneAdjacentVolumeValues(outputContext, varIndicesTimesArr[varNameIndex], true, simDatablock, timeSeriesJobSpec.getIndices()[varNameIndex], timeSeriesJobSpec.getCrossingMembraneIndices()[varNameIndex], vcdID, variableNames[varNameIndex], mesh, timeInfo);
}
}
}
tsjr = new TSJobResultsNoStats(timeSeriesJobSpec.getVariableNames(), timeSeriesJobSpec.getIndices(), timeInfo.desiredTimeValues, varIndicesTimesArr);
}
return tsjr;
}
use of cbit.vcell.solvers.CartesianMesh in project vcell by virtualcell.
the class DataSetControllerImpl method fieldDataFileOperation.
public FieldDataFileOperationResults fieldDataFileOperation(FieldDataFileOperationSpec fieldDataFileOperationSpec) throws ObjectNotFoundException {
if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_COPYSIM) {
Vector<File> removeFilesIfErrorV = new Vector<File>();
try {
int simJobIndex = fieldDataFileOperationSpec.sourceSimParamScanJobIndex;
// Determine style so file names can be constructed properly
VCSimulationDataIdentifier sourceSimDataID = new VCSimulationDataIdentifier(new VCSimulationIdentifier(fieldDataFileOperationSpec.sourceSimDataKey, fieldDataFileOperationSpec.sourceOwner), simJobIndex);
SimulationData simulationData = (SimulationData) getVCData(sourceSimDataID);
boolean isOldStyle = (simulationData.getResultsInfoObject() instanceof VCSimulationDataIdentifierOldStyle);
//
// log,mesh,zip,func
//
KeyValue origSimKey = fieldDataFileOperationSpec.sourceSimDataKey;
File meshFile_orig = simulationData.getMeshFile(false);
File funcFile_orig = simulationData.getFunctionsFile(false);
File subdomainFile_orig = simulationData.getSubdomainFile();
File fdLogFile_orig = simulationData.getLogFile();
File zipFile_orig = simulationData.getZipFile(false, 0);
boolean bCopySubdomainFile = subdomainFile_orig.exists();
// Dont' check subdomainFile_orig
if (!(meshFile_orig.exists() && funcFile_orig.exists() && fdLogFile_orig.exists() && zipFile_orig.exists())) {
throw new RuntimeException("Couldn't find all of the files required to copy sim");
}
File userDir = getPrimaryUserDir(fieldDataFileOperationSpec.owner, true);
File meshFile_new = new File(userDir, SimulationData.createCanonicalMeshFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File funcFile_new = new File(userDir, SimulationData.createCanonicalFunctionsFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File subdomainFile_new = new File(userDir, SimulationData.createCanonicalSubdomainFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File fdLogFile_new = new File(userDir, SimulationData.createCanonicalSimLogFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
File zipFile_new = new File(userDir, SimulationData.createCanonicalSimZipFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, 0, false, false));
if (meshFile_new.exists() || funcFile_new.exists() || fdLogFile_new.exists() || zipFile_new.exists() || (bCopySubdomainFile && subdomainFile_new.exists())) {
throw new RuntimeException("File names required for new Field Data already exist on server");
}
removeFilesIfErrorV.add(funcFile_new);
removeFilesIfErrorV.add(meshFile_new);
removeFilesIfErrorV.add(fdLogFile_new);
// Simple copy of mesh and funcfile because they do not have to be changed
FileUtils.copyFile(meshFile_orig, meshFile_new, false, false, 8 * 1024);
FileUtils.copyFile(funcFile_orig, funcFile_new, false, false, 8 * 1024);
if (bCopySubdomainFile) {
FileUtils.copyFile(subdomainFile_orig, subdomainFile_new, false, false, 8 * 1024);
}
// Copy Log file and replace original simID with ExternalDataIdentifier id
BufferedWriter writer = null;
try {
String origLog = FileUtils.readFileToString(fdLogFile_orig);
String newLogStr;
String replace_new = SimulationData.createSimIDWithJobIndex(fieldDataFileOperationSpec.specEDI.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
if (isOldStyle) {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, 0, true);
newLogStr = origLog.replaceAll(replace_orig, replace_new);
} else {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, fieldDataFileOperationSpec.sourceSimParamScanJobIndex, false);
newLogStr = origLog.replaceAll(replace_orig, replace_new);
}
writer = new BufferedWriter(new FileWriter(fdLogFile_new));
writer.write(newLogStr);
writer.close();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (Exception e) {
/*ignore*/
}
;
}
//
// Copy zip file and rename entries
//
int zipIndex = 0;
while (true) {
// Loop because there might be more than 1 zip file for large datasets
zipFile_orig = simulationData.getZipFile(false, zipIndex);
if (!zipFile_orig.exists()) {
// done
break;
}
zipFile_new = new File(userDir, SimulationData.createCanonicalSimZipFileName(fieldDataFileOperationSpec.specEDI.getKey(), zipIndex, 0, false, false));
if (zipFile_new.exists()) {
throw new DataAccessException("new zipfile name " + zipFile_new.getAbsolutePath() + " already exists");
}
removeFilesIfErrorV.add(zipFile_new);
ZipFile inZipFile = null;
InputStream zis = null;
ZipOutputStream zos = null;
try {
inZipFile = new ZipFile(zipFile_orig);
;
zos = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile_new)));
Enumeration<? extends ZipEntry> zipEntryEnum = inZipFile.getEntries();
while (zipEntryEnum.hasMoreElements()) {
ZipEntry zeIN = zipEntryEnum.nextElement();
byte[] zdataIN = new byte[(int) zeIN.getSize()];
int num = 0;
int numTotal = 0;
zis = new BufferedInputStream(inZipFile.getInputStream((ZipArchiveEntry) zeIN));
// long startTime = System.currentTimeMillis();
while ((num = zis.read(zdataIN, numTotal, zdataIN.length - numTotal)) != -1 && numTotal != zdataIN.length) {
numTotal += num;
}
// System.out.println("zipread time="+((System.currentTimeMillis()-startTime)/1000.0));
zis.close();
String newName;
String replace_new = SimulationData.createSimIDWithJobIndex(fieldDataFileOperationSpec.specEDI.getKey(), FieldDataFileOperationSpec.JOBINDEX_DEFAULT, false);
if (isOldStyle) {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, 0, true);
newName = zeIN.getName().replaceAll(replace_orig, replace_new);
} else {
String replace_orig = SimulationData.createSimIDWithJobIndex(origSimKey, fieldDataFileOperationSpec.sourceSimParamScanJobIndex, false);
newName = zeIN.getName().replaceAll(replace_orig, replace_new);
}
ZipEntry zeOUT = new ZipEntry(newName);
zeOUT.setComment(zeIN.getComment());
zeOUT.setCompressedSize(zeIN.getCompressedSize());
zeOUT.setCrc(zeIN.getCrc());
zeOUT.setExtra(zeIN.getExtra());
zeOUT.setMethod(zeIN.getMethod());
zeOUT.setSize(zeIN.getSize());
zeOUT.setTime(zeIN.getTime());
// startTime = System.currentTimeMillis();
zos.putNextEntry(zeOUT);
zos.write(zdataIN, 0, zdataIN.length);
// System.out.println("zipwrite time="+((System.currentTimeMillis()-startTime)/1000.0)+"\n");
}
} finally {
try {
if (zis != null) {
zis.close();
}
} catch (Exception e) {
/*ignore*/
}
;
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
/*ignore*/
}
;
}
zipIndex += 1;
}
// Now see if we can read what we just wrote
return fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.owner, FieldDataFileOperationSpec.JOBINDEX_DEFAULT));
} catch (Exception e) {
e.printStackTrace();
try {
for (int i = 0; i < removeFilesIfErrorV.size(); i += 1) {
removeFilesIfErrorV.elementAt(i).delete();
}
} catch (Throwable e2) {
// ignore, we tried to cleanup
}
throw new RuntimeException("Error copying sim data to new Field Data\n" + e.getMessage());
}
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_ADD) {
if (fieldDataFileOperationSpec.cartesianMesh == null) {
throw new RuntimeException("Field Data Operation 'ADD' cartesianMesh cannot be null");
}
if (fieldDataFileOperationSpec.times == null || fieldDataFileOperationSpec.times.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' times cannot be null");
}
if (fieldDataFileOperationSpec.times[0] != 0) {
throw new RuntimeException("Field Data Operation 'ADD' first time must be 0.0");
}
if (fieldDataFileOperationSpec.varNames == null || fieldDataFileOperationSpec.varNames.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' variable names cannot be null");
}
if ((fieldDataFileOperationSpec.shortSpecData != null && fieldDataFileOperationSpec.doubleSpecData != null) || (fieldDataFileOperationSpec.shortSpecData == null && fieldDataFileOperationSpec.doubleSpecData == null)) {
throw new RuntimeException("Field Data Operation 'ADD' must have ONLY 1 data specifier, short or double");
}
if (fieldDataFileOperationSpec.shortSpecData != null && (fieldDataFileOperationSpec.shortSpecData.length != fieldDataFileOperationSpec.times.length || fieldDataFileOperationSpec.shortSpecData[0].length != fieldDataFileOperationSpec.varNames.length)) {
throw new RuntimeException("Field Data Operation 'ADD' 'short' data dimension does not match\n" + "times and variable names array lengths");
}
if (fieldDataFileOperationSpec.doubleSpecData != null && (fieldDataFileOperationSpec.doubleSpecData.length != fieldDataFileOperationSpec.times.length || fieldDataFileOperationSpec.doubleSpecData[0].length != fieldDataFileOperationSpec.varNames.length)) {
throw new RuntimeException("Field Data Operation 'ADD' 'double' data dimension does not match\n" + "times and variable names array lengths");
}
if (fieldDataFileOperationSpec.variableTypes == null || fieldDataFileOperationSpec.variableTypes.length == 0) {
throw new RuntimeException("Field Data Operation 'ADD' variable types cannot be null");
}
if (fieldDataFileOperationSpec.variableTypes.length != fieldDataFileOperationSpec.varNames.length) {
throw new RuntimeException("Field Data Operation 'ADD' variable types count does not match variable names count");
}
// byte[][][] allData = fieldDataFileOperationSpec.specData;
double[] times = fieldDataFileOperationSpec.times;
ExternalDataIdentifier dataset = fieldDataFileOperationSpec.specEDI;
String[] vars = fieldDataFileOperationSpec.varNames;
VariableType[] varTypes = fieldDataFileOperationSpec.variableTypes;
File userDir = null;
try {
userDir = getPrimaryUserDir(fieldDataFileOperationSpec.owner, true);
} catch (FileNotFoundException e) {
throw new RuntimeException("Couldn't create new user directory on server");
}
double[][][] convertedData = null;
if (fieldDataFileOperationSpec.doubleSpecData != null) {
convertedData = fieldDataFileOperationSpec.doubleSpecData;
} else {
// convert short to double
convertedData = new double[times.length][vars.length][];
for (int i = 0; i < times.length; i += 1) {
for (int j = 0; j < vars.length; j += 1) {
if (fieldDataFileOperationSpec.shortSpecData != null) {
convertedData[i][j] = new double[fieldDataFileOperationSpec.shortSpecData[i][j].length];
for (int k = 0; k < fieldDataFileOperationSpec.shortSpecData[i][j].length; k += 1) {
convertedData[i][j][k] = (double) (((int) fieldDataFileOperationSpec.shortSpecData[i][j][k]) & 0x0000FFFF);
}
} else {
throw new RuntimeException("no pixel data found");
}
}
}
}
// Write Log file
File fdLogFile = new File(userDir, SimulationData.createCanonicalSimLogFileName(dataset.getKey(), 0, false));
PrintStream ps = null;
File zipFile = new File(userDir, SimulationData.createCanonicalSimZipFileName(dataset.getKey(), 0, 0, false, false));
Vector<String> simFileNamesV = new Vector<String>();
try {
if (!fdLogFile.createNewFile()) {
throw new Exception("File.createNewFile() returned null");
}
ps = new PrintStream(fdLogFile);
for (int i = 0; i < times.length; i += 1) {
String simFilename = SimulationData.createCanonicalSimFilePathName(dataset.getKey(), i, 0, false);
simFileNamesV.add(simFilename);
ps.println(i + "\t" + simFilename + "\t" + zipFile.getName() + "\t" + times[i] + "");
}
ps.flush();
} catch (Exception e) {
throw new RuntimeException("Couldn't create log file " + fdLogFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
if (ps != null) {
ps.close();
}
}
// Write zipFile
java.util.zip.ZipOutputStream zipOut = null;
try {
java.io.BufferedOutputStream bout = new java.io.BufferedOutputStream(new java.io.FileOutputStream(zipFile));
zipOut = new java.util.zip.ZipOutputStream(bout);
for (int t = 0; t < times.length; t += 1) {
java.io.File temp = java.io.File.createTempFile("temp", null);
DataSet.writeNew(temp, vars, varTypes, fieldDataFileOperationSpec.isize, convertedData[t]);
java.util.zip.ZipEntry zipEntry = new java.util.zip.ZipEntry(simFileNamesV.get(t));
zipOut.putNextEntry(zipEntry);
// ----------------------------
java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.io.FileInputStream(temp));
byte[] bytes = new byte[65536];
try {
int b = in.read(bytes);
while (b != -1) {
zipOut.write(bytes, 0, b);
b = in.read(bytes);
}
} catch (IOException e) {
throw new Exception("Error writing zip file bytes");
} finally {
// cleanup
in.close();
temp.delete();
}
// ----------------------------
}
} catch (Exception e) {
throw new RuntimeException("Couldn't create zip file " + zipFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
try {
zipOut.close();
} catch (IOException e) {
e.printStackTrace();
// ignore
}
}
// Write Mesh file
FileOutputStream fos = null;
File meshFile = null;
try {
CartesianMesh mesh = fieldDataFileOperationSpec.cartesianMesh;
meshFile = new File(userDir, SimulationData.createCanonicalMeshFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
fos = new FileOutputStream(meshFile);
mesh.write(new PrintStream(fos));
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error writing mesh file " + meshFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
// ignore
}
}
// Write Functionfile file
PrintStream ps2 = null;
File funcFile = null;
try {
funcFile = new File(userDir, SimulationData.createCanonicalFunctionsFileName(fieldDataFileOperationSpec.specEDI.getKey(), 0, false));
FileOutputStream fos2 = new FileOutputStream(funcFile);
ps2 = new PrintStream(fos2);
ps2.println("##---------------------------------------------" + "\n" + "## " + funcFile.getAbsolutePath() + "\n" + "##---------------------------------------------" + "\n");
ps2.flush();
ps2.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Error writing function file " + funcFile.getAbsolutePath() + "\n" + e.getMessage());
} finally {
if (ps2 != null) {
ps2.close();
}
}
try {
// return Info
return fieldDataFileOperation(FieldDataFileOperationSpec.createInfoFieldDataFileOperationSpec(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.owner, FieldDataFileOperationSpec.JOBINDEX_DEFAULT));
} catch (Exception e) {
e.printStackTrace();
// ignore
}
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_DEPENDANTFUNCS) {
throw new RuntimeException("This function is not currently used");
// //
// //Check for references to FieldData from users User Defined Functions
// //
// HashMap<String, KeyValue> dbFuncFileNamesAndSimKeys = null;
// try{
// dbFuncFileNamesAndSimKeys =
// FieldDataDBOperationDriver.getFunctionFileNamesAndSimKeys(
// fieldDataFileOperationSpec.specEDI.getOwner());
// }catch(Exception e){
// e.printStackTrace();
// throw new RuntimeException("couldn't get Function File names from Database\n"+e.getMessage());
// }
// //String regex = "^.*"+MathMLTags.FIELD+"\\s*\\(\\s*"+fieldDataFileOperationSpec.specEDI.getName()+"\\s*,.*$";
// String regex = "^.*?field\\s*\\(\\s*"+fieldDataFileOperationSpec.specEDI.getName()+"\\s*,.*?$";
// java.util.regex.Pattern pattern =
// java.util.regex.Pattern.compile(regex);//,java.util.regex.Pattern.MULTILINE|java.util.regex.Pattern.DOTALL);
// Matcher matcher = pattern.matcher("");
// Set<Map.Entry<String,KeyValue>> funcAndSimsES = dbFuncFileNamesAndSimKeys.entrySet();
// Vector<FieldDataFileOperationResults.FieldDataReferenceInfo> referencingFuncFileDescription =
// new Vector<FieldDataFileOperationResults.FieldDataReferenceInfo>();
// boolean bSearchSecondary =
// secondaryRootDirectory != null &&
// !primaryRootDirectory.equals(secondaryRootDirectory);
// TreeSet<String> searchedFuncFilesTS = new TreeSet<String>();
// Iterator<Map.Entry<String,KeyValue>> iter = funcAndSimsES.iterator();
// FunctionFileGenerator.FuncFileLineInfo funcfileInfo = null;
// while(iter.hasNext()){
// Map.Entry<String,KeyValue> currentEntry = iter.next();
// File currentFile = null;
// for (int i = 0; i < (bSearchSecondary?2:1); i++) {
// if(searchedFuncFilesTS.contains(currentEntry.getKey())){
// continue;
// }
// currentFile = new File(
// getUserDirectoryName(
// (i==0?primaryRootDirectory:secondaryRootDirectory),
// fieldDataFileOperationSpec.specEDI.getOwner()),currentEntry.getKey());
// if(!currentFile.exists()){
// continue;
// }
// searchedFuncFilesTS.add(currentEntry.getKey());
// LineNumberReader lineNumberReader = null;
// Vector<String> referringFieldfunctionNamesV = new Vector<String>();
// try{
// lineNumberReader = new LineNumberReader(new FileReader(currentFile));
// String funcFileLine = null;
// while((funcFileLine = lineNumberReader.readLine()) != null){
// funcfileInfo = FunctionFileGenerator.readFunctionLine(funcFileLine);
// if(funcfileInfo != null && funcfileInfo.functionExpr != null){
// matcher.reset(funcfileInfo.functionExpr);
// if(matcher.matches()){
// referringFieldfunctionNamesV.add(funcfileInfo.functionName);
// }
// }
// }
// lineNumberReader.close();
// if(referringFieldfunctionNamesV.size() > 0){
// FieldDataFileOperationResults.FieldDataReferenceInfo fieldDataReferenceInfo =
// FieldDataDBOperationDriver.getModelDescriptionForSimulation(
// fieldDataFileOperationSpec.specEDI.getOwner(), currentEntry.getValue());
// fieldDataReferenceInfo.funcNames = referringFieldfunctionNamesV.toArray(new String[0]);
// referencingFuncFileDescription.add(fieldDataReferenceInfo);
// // for (int j = 0; j < referringFieldfunctionNamesV.size(); j++) {
// // referencingFuncFileDescription.add(new String[][] {
// // referringFieldfunctionNamesV.elementAt(j),modelDescription});
// // }
// }
// }catch(Exception e){
// e.printStackTrace();
// throw new RuntimeException(e.getMessage(),e);
// }finally{
// if(lineNumberReader != null){try{lineNumberReader.close();}catch(Exception e){e.printStackTrace();}}
// }
// }
// }
// if(referencingFuncFileDescription.size() > 0){
// FieldDataFileOperationResults fdfor = new FieldDataFileOperationResults();
// fdfor.dependantFunctionInfo =
// referencingFuncFileDescription.toArray(new FieldDataFileOperationResults.FieldDataReferenceInfo[0]);
// return fdfor;
// }
// return null;
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_DELETE) {
//
if (cacheTable0 != null) {
VCSimulationIdentifier vcSimID = new VCSimulationIdentifier(fieldDataFileOperationSpec.specEDI.getKey(), fieldDataFileOperationSpec.specEDI.getOwner());
VCSimulationDataIdentifier simDataID = new VCSimulationDataIdentifier(vcSimID, FieldDataFileOperationSpec.JOBINDEX_DEFAULT);
cacheTable0.removeAll(simDataID);
cacheTable0.removeAll(fieldDataFileOperationSpec.specEDI);
}
if (userExtDataIDH != null) {
userExtDataIDH.remove(fieldDataFileOperationSpec.specEDI.getOwner());
}
SimulationData simulationData = null;
try {
simulationData = (SimulationData) getVCData(fieldDataFileOperationSpec.specEDI);
} catch (Exception e) {
throw new ObjectNotFoundException(e.getMessage(), e);
}
File fdLogFile = simulationData.getLogFile();
File fdMeshFile = simulationData.getMeshFile(false);
File fdFunctionFile = simulationData.getFunctionsFile(true);
File fdSubdomainFile = simulationData.getSubdomainFile();
if (!fdLogFile.delete()) {
System.out.println("Couldn't delete log file " + fdLogFile.getAbsolutePath());
}
if (!fdMeshFile.delete()) {
System.out.println("Couldn't delete Mesh file " + fdMeshFile.getAbsolutePath());
}
if (!fdFunctionFile.delete()) {
System.out.println("Couldn't delete Functions file " + fdFunctionFile.getAbsolutePath());
}
if (fdSubdomainFile.exists() && fdSubdomainFile.delete()) {
System.out.println("Couldn't delete Subdomains file " + fdSubdomainFile.getAbsolutePath());
}
int index = 0;
while (true) {
File fdZipFile = simulationData.getZipFile(false, index);
if (index != 0 && !fdZipFile.exists()) {
break;
}
if (!fdZipFile.delete()) {
System.out.println("Couldn't delete zip file " + fdZipFile.getAbsolutePath());
}
index += 1;
}
return null;
} else if (fieldDataFileOperationSpec.opType == FieldDataFileOperationSpec.FDOS_INFO) {
try {
FieldDataFileOperationResults fdor = new FieldDataFileOperationResults();
VCDataIdentifier sourceSimDataID = new VCSimulationDataIdentifier(new VCSimulationIdentifier(fieldDataFileOperationSpec.sourceSimDataKey, fieldDataFileOperationSpec.sourceOwner), fieldDataFileOperationSpec.sourceSimParamScanJobIndex);
fdor.dataIdentifierArr = getDataIdentifiers(null, sourceSimDataID);
CartesianMesh mesh = getMesh(sourceSimDataID);
fdor.extent = mesh.getExtent();
fdor.origin = mesh.getOrigin();
fdor.iSize = new ISize(mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ());
fdor.times = getDataSetTimes(sourceSimDataID);
return fdor;
} catch (FileNotFoundException e) {
throw new ObjectNotFoundException("Error FieldDataOp get INFO", e);
} catch (Exception e) {
throw new RuntimeException("Error FieldDataFileOperationSpec INFO Operation\n" + e.getMessage());
}
}
throw new RuntimeException("Field data operation " + fieldDataFileOperationSpec.opType + " unknown.");
}
use of cbit.vcell.solvers.CartesianMesh in project vcell by virtualcell.
the class DataSetControllerImpl method getLineScan.
/**
* This method was created by a SmartGuide.
* @return cbit.plot.PlotData
* @param varName java.lang.String
* @param begin cbit.vcell.math.CoordinateIndex
* @param end cbit.vcell.math.CoordinateIndex
*/
public PlotData getLineScan(OutputContext outputContext, VCDataIdentifier vcdID, String varName, double time, SpatialSelection spatialSelection) throws DataAccessException, MathException {
try {
if (spatialSelection == null) {
throw new IllegalArgumentException("null spatialSelection");
}
if (spatialSelection.isPoint()) {
throw new RuntimeException("'Point' spatialSelection not expected");
}
double[] dataTimes = getDataSetTimes(vcdID);
if (dataTimes == null || dataTimes.length <= 0) {
return null;
}
CartesianMesh mesh = getMesh(vcdID);
// mesh is transient and is null if we got here by a serialized path (e.g. rmi)
spatialSelection.setMesh(mesh);
SimDataBlock simDataBlock = getSimDataBlock(outputContext, vcdID, varName, time);
if (simDataBlock == null) {
return null;
}
DataIdentifier dataIdentifier = null;
try {
DataIdentifier[] dataIdentifiers = getDataIdentifiers(outputContext, vcdID);
for (int i = 0; i < dataIdentifiers.length; i++) {
if (dataIdentifiers[i].getName().equals(varName)) {
dataIdentifier = dataIdentifiers[i];
}
}
} catch (IOException e) {
throw new DataAccessException(e.getMessage());
}
double[] data = simDataBlock.getData();
if (data == null) {
return null;
}
if (spatialSelection instanceof SpatialSelectionVolume) {
SpatialSelectionVolume ssVolume = (SpatialSelectionVolume) spatialSelection;
SpatialSelection.SSHelper ssvHelper = ssVolume.getIndexSamples(0.0, 1.0);
if (dataIdentifier.getVariableType().equals(VariableType.VOLUME)) {
ssvHelper.initializeValues_VOLUME(data);
} else if (dataIdentifier.getVariableType().equals(VariableType.VOLUME_REGION)) {
ssvHelper.initializeValues_VOLUMEREGION(data);
} else {
throw new RuntimeException(SpatialSelectionVolume.class.getName() + " does not support variableType=" + dataIdentifier.getVariableType());
}
try {
if (ssvHelper.getMembraneIndexesInOut() != null && ssvHelper.getMembraneIndexesInOut().length > 0) {
adjustMembraneAdjacentVolumeValues(outputContext, new double[][] { ssvHelper.getSampledValues() }, false, simDataBlock, ssvHelper.getSampledIndexes(), ssvHelper.getMembraneIndexesInOut(), vcdID, varName, mesh, // PostProcess never calls LineScan so send in VCell times
new TimeInfo(vcdID, time, 1, time, getDataSetTimes(vcdID)));
}
} catch (Exception e) {
throw new DataAccessException("Error getLineScan adjustingMembraneValues\n" + e.getMessage(), e);
}
double[] values = ssvHelper.getSampledValues();
if (mesh.isChomboMesh()) {
// convert NaN to 0
for (int i = 0; i < values.length; i++) {
if (Double.isNaN(values[i])) {
values[i] = 0;
}
}
}
return new PlotData(ssvHelper.getWorldCoordinateLengths(), values);
} else if (spatialSelection instanceof SpatialSelectionContour) {
//
// get length of span (in microns)
//
double lengthMicrons = spatialSelection.getLengthInMicrons();
if (lengthMicrons <= 0) {
return null;
}
int sizeScan;
int[] sampleIndexes = null;
double[] lineScan = null;
double[] distance = null;
SpatialSelectionContour ssContour = (SpatialSelectionContour) spatialSelection;
sampleIndexes = ssContour.getIndexSamples();
sizeScan = sampleIndexes.length;
//
if (dataIdentifier.getVariableType().equals(VariableType.CONTOUR_REGION)) {
for (int i = 0; i < sampleIndexes.length; i++) {
sampleIndexes[i] = mesh.getContourRegionIndex(sampleIndexes[i]);
}
}
lineScan = new double[sizeScan];
distance = new double[sizeScan];
for (int i = 0; i < sizeScan; i++) {
lineScan[i] = data[sampleIndexes[i]];
distance[i] = (((double) i) / (sizeScan - 1)) * lengthMicrons;
}
return new PlotData(distance, lineScan);
} else if (spatialSelection instanceof SpatialSelectionMembrane) {
SpatialSelectionMembrane ssMembrane = (SpatialSelectionMembrane) spatialSelection;
SpatialSelection.SSHelper ssmHelper = ssMembrane.getIndexSamples();
if (dataIdentifier.getVariableType().equals(VariableType.MEMBRANE)) {
ssmHelper.initializeValues_MEMBRANE(data);
} else if (dataIdentifier.getVariableType().equals(VariableType.MEMBRANE_REGION)) {
ssmHelper.initializeValues_MEMBRANEREGION(data);
} else {
throw new RuntimeException(SpatialSelectionMembrane.class.getName() + " does not support variableType=" + dataIdentifier.getVariableType());
}
return new PlotData(ssmHelper.getWorldCoordinateLengths(), ssmHelper.getSampledValues());
} else {
throw new RuntimeException("unexpected SpatialSelection type " + spatialSelection.getClass().toString());
}
} catch (DataAccessException e) {
lg.error(e.getMessage(), e);
throw e;
} catch (IOException e) {
lg.error(e.getMessage(), e);
throw new DataAccessException(e.getMessage());
}
}
use of cbit.vcell.solvers.CartesianMesh in project vcell by virtualcell.
the class DataSetControllerImpl method evaluatePostProcessFunction.
private static DataProcessingOutputDataValues evaluatePostProcessFunction(DataOperationResults.DataProcessingOutputInfo dataProcessingOutputInfo, String[] postProcessSymbols, double[][][] postProcessData, DataIndexHelper dataIndexHelper, TimePointHelper timePointHelper, Expression flattenedBoundExpression, String varName) throws Exception {
ISize iSize = dataProcessingOutputInfo.getVariableISize(postProcessSymbols[0]);
VCImageUncompressed vcImage = new VCImageUncompressed(null, new byte[iSize.getXYZ()], new Extent(1, 1, 1), iSize.getX(), iSize.getY(), iSize.getZ());
int dimension = 1 + (iSize.getY() > 1 ? 1 : 0) + (iSize.getZ() > 1 ? 1 : 0);
RegionImage regionImage = new RegionImage(vcImage, dimension, dataProcessingOutputInfo.getVariableExtent(postProcessSymbols[0]), dataProcessingOutputInfo.getVariableOrigin(postProcessSymbols[0]), RegionImage.NO_SMOOTHING);
CartesianMesh cartesianMesh = CartesianMesh.createSimpleCartesianMesh(dataProcessingOutputInfo.getVariableOrigin(postProcessSymbols[0]), dataProcessingOutputInfo.getVariableExtent(postProcessSymbols[0]), dataProcessingOutputInfo.getVariableISize(postProcessSymbols[0]), regionImage);
double[] timePoints = null;
if (timePointHelper.isAllTimePoints()) {
timePoints = dataProcessingOutputInfo.getVariableTimePoints();
} else {
timePoints = timePointHelper.getTimePoints();
}
double[][] evaluatedValues = new double[timePoints.length][];
int dataIndexCount = 0;
ISize DATA_SIZE = dataProcessingOutputInfo.getVariableISize(postProcessSymbols[0]);
int DATA_SIZE_XY = DATA_SIZE.getX() * DATA_SIZE.getY();
if (dataIndexHelper.isAllDataIndexes()) {
dataIndexCount = DATA_SIZE.getXYZ();
} else if (dataIndexHelper.isSingleSlice()) {
dataIndexCount = DATA_SIZE_XY;
} else {
dataIndexCount = dataIndexHelper.getDataIndexes().length;
}
double[] args = new double[TXYZ_OFFSET + postProcessSymbols.length];
for (int t = 0; t < timePoints.length; t++) {
evaluatedValues[t] = new double[dataIndexCount];
args[0] = timePoints[t];
for (int i = 0; i < dataIndexCount; i++) {
Coordinate coord;
if (dataIndexHelper.isAllDataIndexes()) {
coord = cartesianMesh.getCoordinateFromVolumeIndex(i);
} else if (dataIndexHelper.isSingleSlice()) {
coord = cartesianMesh.getCoordinateFromVolumeIndex(dataIndexHelper.getSliceIndex() * DATA_SIZE_XY + i);
} else {
coord = cartesianMesh.getCoordinateFromVolumeIndex(dataIndexHelper.getDataIndexes()[i]);
}
args[1] = coord.getX();
args[2] = coord.getY();
args[3] = coord.getZ();
for (int j = 0; j < postProcessSymbols.length; j++) {
args[TXYZ_OFFSET + j] = postProcessData[j][t][i];
}
evaluatedValues[t][i] = flattenedBoundExpression.evaluateVector(args);
// System.out.println("in="+args[4]+" out="+evaluatedValues[t][i]+" sin(in)="+Math.sin(args[4]));
}
}
return new DataOperationResults.DataProcessingOutputDataValues(dataProcessingOutputInfo.getVCDataIdentifier(), varName, timePointHelper, dataIndexHelper, evaluatedValues);
}
use of cbit.vcell.solvers.CartesianMesh in project vcell by virtualcell.
the class RunRefSimulationFastOp method saveROIsAsExternalData.
private void saveROIsAsExternalData(ROI[] rois, LocalWorkspace localWorkspace, ExternalDataIdentifier newROIExtDataID) throws ObjectNotFoundException, ImageException, IOException {
ISize isize = rois[0].getISize();
Origin origin = rois[0].getRoiImages()[0].getOrigin();
Extent extent = rois[0].getRoiImages()[0].getExtent();
VCImage vcImage = new VCImageUncompressed(null, new byte[isize.getXYZ()], extent, isize.getX(), isize.getY(), isize.getZ());
RegionImage regionImage = new RegionImage(vcImage, 0, null, null, RegionImage.NO_SMOOTHING);
CartesianMesh cartesianMesh = CartesianMesh.createSimpleCartesianMesh(origin, extent, isize, regionImage);
int NumTimePoints = 1;
int NumChannels = rois.length;
// dimensions: time points, channels, whole image ordered by z slices.
double[][][] pixData = new double[NumTimePoints][NumChannels][];
int index = 0;
for (ROI roi : rois) {
pixData[0][index++] = createDoubleArray(roi.getBinaryPixelsXYZ(1));
}
// Origin origin = new Origin(0,0,0);
FieldDataFileOperationSpec fdos = new FieldDataFileOperationSpec();
fdos.opType = FieldDataFileOperationSpec.FDOS_ADD;
fdos.cartesianMesh = cartesianMesh;
fdos.doubleSpecData = pixData;
fdos.specEDI = newROIExtDataID;
ArrayList<String> varNames = new ArrayList<String>();
ArrayList<VariableType> varTypes = new ArrayList<VariableType>();
for (ROI roi : rois) {
varNames.add(ROI_MASK_NAME_PREFIX + roi.getROIName());
varTypes.add(VariableType.VOLUME);
}
fdos.varNames = varNames.toArray(new String[0]);
fdos.owner = LocalWorkspace.getDefaultOwner();
fdos.times = new double[] { 0.0 };
fdos.variableTypes = varTypes.toArray(new VariableType[0]);
fdos.origin = origin;
fdos.extent = extent;
fdos.isize = isize;
localWorkspace.getDataSetControllerImpl().fieldDataFileOperation(fdos);
}
Aggregations