use of org.vcell.util.Extent in project vcell by virtualcell.
the class GenerateNormalizedFrapDataOp method generate.
public NormalizedFrapDataResults generate(ImageTimeSeries<UShortImage> rawImageTimeSeries, ROI backgroundROI_2D, Integer indexPostbleach) throws Exception {
UShortImage firstImage = rawImageTimeSeries.getAllImages()[0];
ISize isize = firstImage.getISize();
int nX = isize.getX();
int nY = isize.getY();
int nZ = isize.getZ();
int numTimes = rawImageTimeSeries.getSizeT();
Extent extent = firstImage.getExtent();
org.vcell.util.Origin origin = firstImage.getOrigin();
if (indexPostbleach == 0) {
throw new RuntimeException("no prebleach images found - indexOfFirstPostbleach is 0");
}
//
// find average "dark count" in background of pre-bleach images (for background subtraction)
//
float avgBackground = 0.0f;
int numPrebleach = indexPostbleach;
for (int i = 0; i < numPrebleach; i++) {
avgBackground += getAverage(rawImageTimeSeries.getAllImages()[i], backgroundROI_2D);
}
avgBackground /= numPrebleach;
//
// find averaged prebleach image (corrected for background).
//
int numPostbleachImages = numTimes - numPrebleach;
// holds new averaged image pixels
float[] prebleachAveragePixels = new float[nX * nY * nZ];
for (int i = 0; i < numPrebleach; i++) {
short[] currPrebleachImage = rawImageTimeSeries.getAllImages()[i].getPixels();
for (int j = 0; j < isize.getXYZ(); j++) {
float intPixel = 0x0000ffff & ((int) currPrebleachImage[j]);
prebleachAveragePixels[j] += intPixel / numPrebleach;
}
}
FloatImage prebleachAverageImage = new FloatImage(prebleachAveragePixels, origin, extent, nX, nY, nZ);
//
// create normalized dataset
//
// normalized postbleach = (origPostbleach - background)/(prebleach - background)
//
FloatImage[] normalizedImages = new FloatImage[numPostbleachImages];
double[] postbleachTimeStamps = new double[numPostbleachImages];
for (int i = 0; i < numPostbleachImages; i++) {
double[] origTimeStamps = rawImageTimeSeries.getImageTimeStamps();
postbleachTimeStamps[i] = origTimeStamps[indexPostbleach + i] - origTimeStamps[indexPostbleach];
float[] normalizedPixels = new float[isize.getXYZ()];
normalizedImages[i] = new FloatImage(normalizedPixels, origin, extent, nX, nY, nZ);
short[] uncorrectedPixels = rawImageTimeSeries.getAllImages()[i + indexPostbleach].getPixels();
for (int j = 0; j < isize.getXYZ(); j++) {
int intPixel = 0x0000ffff & ((int) uncorrectedPixels[j]);
normalizedPixels[j] = (intPixel - avgBackground) / (Math.max(1, prebleachAveragePixels[j] - avgBackground));
}
normalizedImages[i] = new FloatImage(normalizedPixels, origin, extent, nX, nY, nZ);
}
ImageTimeSeries<FloatImage> normalizedData = new ImageTimeSeries<FloatImage>(FloatImage.class, normalizedImages, postbleachTimeStamps, nZ);
NormalizedFrapDataResults results = new NormalizedFrapDataResults();
results.normalizedFrapData = normalizedData;
results.prebleachAverage = prebleachAverageImage;
return results;
}
use of org.vcell.util.Extent in project vcell by virtualcell.
the class FitTimeSeries method main.
public static void main(String[] args) {
try {
int numX = 100;
int numY = 100;
double center_i = 52.44;
double center_j = 51.39;
double K = 30;
double high = 0.9;
double sigma2 = 4;
float[] pixels = new float[numX * numY];
Origin origin = new Origin(0, 0, 0);
Extent extent = new Extent(1, 1, 1);
FloatImage image = new FloatImage(pixels, origin, extent, numX, numY, 1);
int index = 0;
for (int j = 0; j < numY; j++) {
for (int i = 0; i < numX; i++) {
double radius = ((i - center_i) * (i - center_i) + (j - center_j) * (j - center_j));
pixels[index++] = (float) (high - FastMath.exp(-K * FastMath.exp(-radius / sigma2)));
}
}
double init_center_i = 50;
double init_center_j = 50;
double init_sigma2 = 2;
GaussianFitResults fitResults = fitToGaussian(init_center_i, init_center_j, init_sigma2, image);
System.out.println(fitResults);
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.vcell.util.Extent in project vcell by virtualcell.
the class GenerateTrivial2DPsfOp method generateTrivial2D_Psf.
public UShortImage generateTrivial2D_Psf() {
// psf image is now expecting a 3x3 image that are all zeros with a 1 in the middle (Kronecker delta)
short[] pixels = new short[] { 0, 0, 0, 0, 1, 0, 0, 0, 0 };
Origin origin = new Origin(0, 0, 0);
Extent extent = new Extent(1, 1, 1);
ISize isize = new ISize(3, 3, 1);
UShortImage psfImage;
try {
psfImage = new UShortImage(pixels, origin, extent, isize.getX(), isize.getY(), isize.getZ());
} catch (ImageException e) {
e.printStackTrace();
throw new RuntimeException("unexpected image exception: " + e.getMessage(), e);
}
return psfImage;
}
use of org.vcell.util.Extent in project vcell by virtualcell.
the class RunFakeSimOp method runRefSimulation.
public ImageTimeSeries<UShortImage> runRefSimulation(LocalWorkspace localWorkspace, Simulation simulation, double max_intensity, double bleachBlackoutStartTime, double bleachBlackoutStopTime, boolean hasNoise, ClientTaskStatusSupport progressListener) throws Exception {
User owner = LocalWorkspace.getDefaultOwner();
KeyValue simKey = LocalWorkspace.createNewKeyValue();
runFVSolverStandalone(new File(localWorkspace.getDefaultSimDataDirectory()), simulation, progressListener);
Extent extent = simulation.getMathDescription().getGeometry().getExtent();
Origin origin = simulation.getMathDescription().getGeometry().getOrigin();
VCDataIdentifier vcDataIdentifier = new VCSimulationDataIdentifier(new VCSimulationIdentifier(simulation.getKey(), simulation.getVersion().getOwner()), 0);
CartesianMesh mesh = localWorkspace.getDataSetControllerImpl().getMesh(vcDataIdentifier);
ISize isize = new ISize(mesh.getSizeX(), mesh.getSizeY(), mesh.getSizeZ());
double[] dataTimes = localWorkspace.getDataSetControllerImpl().getDataSetTimes(vcDataIdentifier);
DataProcessingOutputDataValues dataProcessingOutputDataValues = (DataProcessingOutputDataValues) localWorkspace.getDataSetControllerImpl().doDataOperation(new DataProcessingOutputDataValuesOP(vcDataIdentifier, SimulationContext.FLUOR_DATA_NAME, TimePointHelper.createAllTimeTimePointHelper(), DataIndexHelper.createSliceDataIndexHelper(0), null, null));
ArrayList<SourceDataInfo> sourceDataInfoArr = dataProcessingOutputDataValues.createSourceDataInfos(new ISize(mesh.getSizeX(), mesh.getSizeY(), 1), origin, extent);
// find scale factor to scale up the data to avoid losing precision when casting double to short
double maxDataValue = 0;
for (int i = 0; i < dataTimes.length; i++) {
if (sourceDataInfoArr.get(i).getMinMax() != null) {
maxDataValue = Math.max(maxDataValue, sourceDataInfoArr.get(i).getMinMax().getMax());
} else {
double[] doubleData = (double[]) sourceDataInfoArr.get(i).getData();
for (int j = 0; j < doubleData.length; j++) {
maxDataValue = Math.max(maxDataValue, doubleData[j]);
}
}
}
double scale = max_intensity / maxDataValue;
ArrayList<UShortImage> outputImages = new ArrayList<UShortImage>();
ArrayList<Double> outputTimes = new ArrayList<Double>();
for (int i = 0; i < dataTimes.length; i++) {
if (dataTimes[i] < bleachBlackoutStartTime || dataTimes[i] > bleachBlackoutStopTime) {
// saving each time step 2D double array to a UShortImage
double[] doubleData = (double[]) sourceDataInfoArr.get(i).getData();
short[] shortData = new short[isize.getX() * isize.getY()];
for (int j = 0; j < shortData.length; j++) {
double dData = doubleData[j] * scale;
if (dData < 0 || dData > 65535.0) {
throw new RuntimeException("scaled pixel out of range of unsigned 16 bit integer, original simulated value = " + doubleData[j] + ", scale = " + scale + ", scaled value = " + dData);
}
short sData = (short) (0x0000ffff & ((int) dData));
if (hasNoise && dData > 0.0) {
if (dData > 20) {
shortData[j] = (short) (0x0000ffff & (int) RandomVariable.normal(dData, Math.sqrt(dData)));
} else {
shortData[j] = (short) (0x0000ffff & RandomVariable.poisson(dData));
}
} else {
shortData[j] = sData;
}
}
outputTimes.add(dataTimes[i]);
outputImages.add(new UShortImage(shortData, sourceDataInfoArr.get(i).getOrigin(), sourceDataInfoArr.get(i).getExtent(), sourceDataInfoArr.get(i).getXSize(), sourceDataInfoArr.get(i).getYSize(), 1));
if (progressListener != null) {
int progress = (int) (((i + 1) * 1.0 / dataTimes.length) * 100);
progressListener.setProgress(progress);
}
}
}
double[] outputTimesArray = new double[outputTimes.size()];
for (int i = 0; i < outputTimes.size(); i++) {
outputTimesArray[i] = outputTimes.get(i);
}
ImageTimeSeries<UShortImage> fakeFluorTimeSeries = new ImageTimeSeries<UShortImage>(UShortImage.class, outputImages.toArray(new UShortImage[0]), outputTimesArray, 1);
return fakeFluorTimeSeries;
}
use of org.vcell.util.Extent in project vcell by virtualcell.
the class RunRefSimulationFastOp method getROIDataGenerator.
private ROIDataGenerator getROIDataGenerator(LocalContext localWorkspace, ROI[] rois) throws ImageException, IOException {
// create ROI image
short[] roiFieldData = null;
if (rois.length > 0) {
Origin origin = new Origin(0, 0, 0);
Extent extent = rois[0].getRoiImages()[0].getExtent();
ISize isize = rois[0].getISize();
int numROIX = rois[0].getISize().getX();
int numROIY = rois[0].getISize().getY();
roiFieldData = new short[numROIX * numROIY];
short regionCounter = 1;
for (int roiIdx = 0; roiIdx < rois.length; roiIdx++) {
short[] roiImg = rois[roiIdx].getPixelsXYZ();
for (int pixelIdx = 0; pixelIdx < (numROIX * numROIY); pixelIdx++) {
if (roiImg[pixelIdx] > 0) {
roiFieldData[pixelIdx] = regionCounter;
}
}
regionCounter++;
}
// create field data
int NumTimePoints = 1;
// 8 rois integrated into 1 image
int NumChannels = 1;
short[][][] pixData = new short[NumTimePoints][NumChannels][];
pixData[0][0] = roiFieldData;
// get extental data id
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 simpleCartesianMesh = CartesianMesh.createSimpleCartesianMesh(origin, extent, isize, regionImage);
ExternalDataIdentifier newROIExtDataID = createNewExternalDataInfo(localWorkspace, ROI_SUMDATA_NAME).getExternalDataIdentifier();
try {
FieldDataFileOperationSpec fdos = new FieldDataFileOperationSpec();
fdos.opType = FieldDataFileOperationSpec.FDOS_ADD;
fdos.cartesianMesh = simpleCartesianMesh;
fdos.shortSpecData = pixData;
fdos.specEDI = newROIExtDataID;
fdos.varNames = new String[] { "roiSumDataVar" };
fdos.owner = LocalWorkspace.getDefaultOwner();
fdos.times = new double[] { 0.0 };
fdos.variableTypes = new VariableType[] { VariableType.VOLUME };
fdos.origin = origin;
fdos.extent = extent;
fdos.isize = isize;
localWorkspace.getDataSetControllerImpl().fieldDataFileOperation(fdos);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new ROIDataGenerator(ROI_EXTDATA_NAME, /*name*/
new int[] { 0 }, /* volumePoints*/
new int[0], /* membranePoints*/
regionCounter, /*numRegions*/
0, /*zSlice*/
newROIExtDataID.getKey(), /* fieldDataKey, sample image*/
new FieldFunctionArguments(ROI_SUMDATA_NAME, ROI_SUMDATA_VARNAME, new Expression(0), VariableType.VOLUME), /*FieldFunctionArguments, sample image*/
false);
}
return null;
}
Aggregations