use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class FiniteVolumeFileWriter method writeChomboSpec.
private void writeChomboSpec() throws ExpressionException, SolverException, PropertyVetoException, ClassNotFoundException, IOException, GeometryException, ImageException {
if (!bChomboSolver) {
return;
}
GeometrySpec geometrySpec = resampledGeometry.getGeometrySpec();
int dimension = geometrySpec.getDimension();
if (dimension == 1) {
throw new SolverException(simTask.getSimulation().getSolverTaskDescription().getSolverDescription().getDisplayLabel() + " is only supported for simulations with 2D or 3D geometry.");
}
Simulation simulation = getSimulationTask().getSimulation();
SolverTaskDescription solverTaskDescription = simulation.getSolverTaskDescription();
ChomboSolverSpec chomboSolverSpec = solverTaskDescription.getChomboSolverSpec();
printWriter.println(FVInputFileKeyword.CHOMBO_SPEC_BEGIN);
printWriter.println(FVInputFileKeyword.DIMENSION + " " + geometrySpec.getDimension());
Extent extent = geometrySpec.getExtent();
Origin origin = geometrySpec.getOrigin();
ISize isize = simulation.getMeshSpecification().getSamplingSize();
switch(geometrySpec.getDimension()) {
case 2:
printWriter.println(FVInputFileKeyword.MESH_SIZE + " " + isize.getX() + " " + isize.getY());
printWriter.println(FVInputFileKeyword.DOMAIN_SIZE + " " + extent.getX() + " " + extent.getY());
printWriter.println(FVInputFileKeyword.DOMAIN_ORIGIN + " " + origin.getX() + " " + origin.getY());
break;
case 3:
printWriter.println(FVInputFileKeyword.MESH_SIZE + " " + isize.getX() + " " + isize.getY() + " " + isize.getZ());
printWriter.println(FVInputFileKeyword.DOMAIN_SIZE + " " + extent.getX() + " " + extent.getY() + " " + extent.getZ());
printWriter.println(FVInputFileKeyword.DOMAIN_ORIGIN + " " + origin.getX() + " " + origin.getY() + " " + origin.getZ());
break;
}
List<CompartmentSubDomain> featureList = new ArrayList<CompartmentSubDomain>();
Enumeration<SubDomain> enum1 = simulation.getMathDescription().getSubDomains();
while (enum1.hasMoreElements()) {
SubDomain sd = enum1.nextElement();
if (sd instanceof CompartmentSubDomain) {
featureList.add((CompartmentSubDomain) sd);
}
}
int numFeatures = featureList.size();
CompartmentSubDomain[] features = featureList.toArray(new CompartmentSubDomain[0]);
int[] phases = new int[numFeatures];
Arrays.fill(phases, -1);
phases[numFeatures - 1] = 0;
int[] numAssigned = new int[] { 1 };
assignPhases(features, numFeatures - 1, phases, numAssigned);
Map<String, Integer> subDomainPhaseMap = new HashMap<String, Integer>();
for (int i = 0; i < phases.length; ++i) {
if (phases[i] == -1) {
throw new SolverException("Failed to assign a phase to CompartmentSubdomain '" + features[i].getName() + "'. It might be caused by too coarsh a mesh.");
}
subDomainPhaseMap.put(features[i].getName(), phases[i]);
}
SubVolume[] subVolumes = geometrySpec.getSubVolumes();
if (geometrySpec.hasImage()) {
Geometry geometry = (Geometry) BeanUtils.cloneSerializable(simulation.getMathDescription().getGeometry());
Geometry simGeometry = geometry;
VCImage img = geometry.getGeometrySpec().getImage();
int factor = Math.max(Math.max(img.getNumX(), img.getNumY()), img.getNumZ()) < 512 ? 2 : 1;
ISize distanceMapMeshSize = new ISize(img.getNumX() * factor, img.getNumY() * factor, img.getNumZ() * factor);
Vect3d deltaX = null;
boolean bCellCentered = false;
double dx = 0.5;
double dy = 0.5;
double dz = 0.5;
int Nx = distanceMapMeshSize.getX();
int Ny = distanceMapMeshSize.getY();
int Nz = distanceMapMeshSize.getZ();
if (dimension == 2) {
// pad the 2D image with itself in order to obtain a 3D image used to compute the distance map
// because the distance map algorithm is 3D only (using distance to triangles)
byte[] oldPixels = img.getPixels();
byte[] newPixels = new byte[oldPixels.length * 3];
System.arraycopy(oldPixels, 0, newPixels, 0, oldPixels.length);
System.arraycopy(oldPixels, 0, newPixels, oldPixels.length, oldPixels.length);
System.arraycopy(oldPixels, 0, newPixels, oldPixels.length * 2, oldPixels.length);
double distX = geometry.getExtent().getX() / img.getNumX();
double distY = geometry.getExtent().getY() / img.getNumY();
// we set the distance on the z axis to something that makes sense
double distZ = Math.max(distX, distY);
Extent newExtent = new Extent(geometry.getExtent().getX(), geometry.getExtent().getY(), distZ * 3);
VCImage newImage = new VCImageUncompressed(null, newPixels, newExtent, img.getNumX(), img.getNumY(), 3);
// copy the pixel classes too
ArrayList<VCPixelClass> newPixelClasses = new ArrayList<VCPixelClass>();
for (VCPixelClass origPixelClass : geometry.getGeometrySpec().getImage().getPixelClasses()) {
SubVolume origSubvolume = geometry.getGeometrySpec().getImageSubVolumeFromPixelValue(origPixelClass.getPixel());
newPixelClasses.add(new VCPixelClass(null, origSubvolume.getName(), origPixelClass.getPixel()));
}
newImage.setPixelClasses(newPixelClasses.toArray(new VCPixelClass[newPixelClasses.size()]));
simGeometry = new Geometry(geometry, newImage);
Nz = 3;
}
GeometrySpec simGeometrySpec = simGeometry.getGeometrySpec();
Extent simExtent = simGeometrySpec.getExtent();
dx = simExtent.getX() / (Nx - 1);
dy = simExtent.getY() / (Ny - 1);
dz = simExtent.getZ() / (Nz - 1);
if (Math.abs(dx - dy) > 0.1 * Math.max(dx, dy)) {
dx = Math.min(dx, dy);
dy = dx;
Nx = (int) (simExtent.getX() / dx + 1);
Ny = (int) (simExtent.getY() / dx + 1);
if (dimension == 3) {
dz = dx;
Nz = (int) (simExtent.getZ() / dx + 1);
}
}
deltaX = new Vect3d(dx, dy, dz);
// one more point in each direction
distanceMapMeshSize = new ISize(Nx + 1, Ny + 1, Nz + 1);
Extent distanceMapExtent = new Extent(simExtent.getX() + dx, simExtent.getY() + dy, simExtent.getZ() + dz);
simGeometrySpec.setExtent(distanceMapExtent);
GeometrySurfaceDescription geoSurfaceDesc = simGeometry.getGeometrySurfaceDescription();
geoSurfaceDesc.setVolumeSampleSize(distanceMapMeshSize);
geoSurfaceDesc.updateAll();
VCImage vcImage = RayCaster.sampleGeometry(simGeometry, distanceMapMeshSize, bCellCentered);
SubvolumeSignedDistanceMap[] distanceMaps = DistanceMapGenerator.computeDistanceMaps(simGeometry, vcImage, bCellCentered);
if (dimension == 2) {
distanceMaps = DistanceMapGenerator.extractMiddleSlice(distanceMaps);
}
printWriter.println(FVInputFileKeyword.SUBDOMAINS + " " + simGeometrySpec.getNumSubVolumes() + " " + FVInputFileKeyword.DISTANCE_MAP);
for (int i = 0; i < subVolumes.length; i++) {
File distanceMapFile = new File(workingDirectory, getSimulationTask().getSimulationJobID() + "_" + subVolumes[i].getName() + DISTANCE_MAP_FILE_EXTENSION);
writeDistanceMapFile(deltaX, distanceMaps[i], distanceMapFile);
int phase = subDomainPhaseMap.get(subVolumes[i].getName());
printWriter.println(subVolumes[i].getName() + " " + phase + " " + distanceMapFile.getAbsolutePath());
}
} else {
printWriter.println(FVInputFileKeyword.SUBDOMAINS + " " + geometrySpec.getNumSubVolumes());
Expression[] rvachevExps = convertAnalyticGeometryToRvachevFunction(geometrySpec);
for (int i = 0; i < subVolumes.length; i++) {
if (subVolumes[i] instanceof AnalyticSubVolume) {
String name = subVolumes[i].getName();
int phase = subDomainPhaseMap.get(name);
printWriter.println(name + " " + phase + " ");
printWriter.println(FVInputFileKeyword.IF + " " + rvachevExps[i].infix() + ";");
printWriter.println(FVInputFileKeyword.USER + " " + ((AnalyticSubVolume) subVolumes[i]).getExpression().infix() + ";");
}
}
}
printWriter.println(FVInputFileKeyword.MAX_BOX_SIZE + " " + chomboSolverSpec.getMaxBoxSize());
printWriter.println(FVInputFileKeyword.FILL_RATIO + " " + chomboSolverSpec.getFillRatio());
printWriter.println(FVInputFileKeyword.RELATIVE_TOLERANCE + " " + simulation.getSolverTaskDescription().getErrorTolerance().getRelativeErrorTolerance());
printWriter.println(FVInputFileKeyword.SAVE_VCELL_OUTPUT + " " + chomboSolverSpec.isSaveVCellOutput());
printWriter.println(FVInputFileKeyword.SAVE_CHOMBO_OUTPUT + " " + chomboSolverSpec.isSaveChomboOutput());
printWriter.println(FVInputFileKeyword.ACTIVATE_FEATURE_UNDER_DEVELOPMENT + " " + chomboSolverSpec.isActivateFeatureUnderDevelopment());
printWriter.println(FVInputFileKeyword.SMALL_VOLFRAC_THRESHOLD + " " + chomboSolverSpec.getSmallVolfracThreshold());
printWriter.println(FVInputFileKeyword.BLOCK_FACTOR + " " + chomboSolverSpec.getBlockFactor());
printWriter.println(FVInputFileKeyword.TAGS_GROW + " " + chomboSolverSpec.getTagsGrow());
// Refinement
int numLevels = chomboSolverSpec.getNumRefinementLevels();
// Refinements #Levels ratio 1, ratio 2, etc
printWriter.print(FVInputFileKeyword.REFINEMENTS + " " + (numLevels + 1));
List<Integer> ratios = chomboSolverSpec.getRefineRatioList();
for (int i : ratios) {
printWriter.print(" " + i);
}
// write last refinement ratio, fake
printWriter.println(" 2");
// membrane rois
List<RefinementRoi> memRios = chomboSolverSpec.getMembraneRefinementRois();
printWriter.println(FVInputFileKeyword.REFINEMENT_ROIS + " " + RoiType.Membrane + " " + memRios.size());
for (RefinementRoi roi : memRios) {
if (roi.getRoiExpression() == null) {
throw new SolverException("ROI expression cannot be null");
}
// level tagsGrow ROIexpression
printWriter.println(roi.getLevel() + " " + roi.getRoiExpression().infix() + ";");
}
List<RefinementRoi> volRios = chomboSolverSpec.getVolumeRefinementRois();
printWriter.println(FVInputFileKeyword.REFINEMENT_ROIS + " " + RoiType.Volume + " " + volRios.size());
for (RefinementRoi roi : volRios) {
if (roi.getRoiExpression() == null) {
throw new SolverException("ROI expression cannot be null");
}
printWriter.println(roi.getLevel() + " " + roi.getRoiExpression().infix() + ";");
}
printWriter.println(FVInputFileKeyword.VIEW_LEVEL + " " + chomboSolverSpec.getViewLevel());
printWriter.println(FVInputFileKeyword.CHOMBO_SPEC_END);
printWriter.println();
}
use of cbit.image.VCImageUncompressed 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.image.VCImageUncompressed in project vcell by virtualcell.
the class ROIMultiPaintManager method createVCImageFromBufferedImages.
public static VCImage createVCImageFromBufferedImages(Extent extent, BufferedImage[] bufferedImages) throws Exception {
// collect z-sections into 1 array for VCImage
ISize isize = new ISize(bufferedImages[0].getWidth(), bufferedImages[0].getHeight(), bufferedImages.length);
int sizeXY = isize.getX() * isize.getY();
byte[] segmentedData = new byte[isize.getXYZ()];
int index = 0;
for (int i = 0; i < bufferedImages.length; i++) {
System.arraycopy(((DataBufferByte) bufferedImages[i].getRaster().getDataBuffer()).getData(), 0, segmentedData, index, sizeXY);
index += sizeXY;
}
return new VCImageUncompressed(null, segmentedData, extent, isize.getX(), isize.getY(), isize.getZ());
}
use of cbit.image.VCImageUncompressed 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);
}
use of cbit.image.VCImageUncompressed in project vcell by virtualcell.
the class RunRefSimulationFastOp method savePsfAsExternalData.
private void savePsfAsExternalData(UShortImage psf, String varName, ExternalDataIdentifier newPsfExtDataID, LocalWorkspace localWorkspace) throws IOException, ImageException, ObjectNotFoundException {
Origin origin = new Origin(0, 0, 0);
Extent ext = new Extent(1, 1, 1);
ISize isize = new ISize(3, 3, 1);
VCImageUncompressed vcImage = new VCImageUncompressed(null, new byte[isize.getXYZ()], ext, isize.getX(), isize.getY(), isize.getZ());
RegionImage regionImage = new RegionImage(vcImage, 0, null, null, RegionImage.NO_SMOOTHING);
CartesianMesh cartesianMesh = CartesianMesh.createSimpleCartesianMesh(origin, ext, isize, regionImage);
int NumTimePoints = 1;
// 8 rois integrated into 1 image
int NumChannels = 1;
short[][][] pixData = new short[NumTimePoints][NumChannels][1];
pixData[0][0] = psf.getPixels();
FieldDataFileOperationSpec fdos = new FieldDataFileOperationSpec();
fdos.opType = FieldDataFileOperationSpec.FDOS_ADD;
fdos.cartesianMesh = cartesianMesh;
fdos.shortSpecData = pixData;
fdos.specEDI = newPsfExtDataID;
fdos.varNames = new String[] { varName };
fdos.owner = LocalWorkspace.getDefaultOwner();
fdos.times = new double[] { 0.0 };
fdos.variableTypes = new VariableType[] { VariableType.VOLUME };
fdos.origin = origin;
fdos.extent = ext;
fdos.isize = isize;
localWorkspace.getDataSetControllerImpl().fieldDataFileOperation(fdos);
}
Aggregations