Search in sources :

Example 61 with ROI

use of cbit.vcell.VirtualMicroscopy.ROI in project vcell by virtualcell.

the class NonGUIFRAPTest method runXMLFile.

public static void runXMLFile(String xmlFileName, String testDirectoryPath) throws Exception {
    String xmlString = XmlUtil.getXMLString(xmlFileName);
    // System.out.println(xmlString);
    MicroscopyXmlReader xmlReader = new MicroscopyXmlReader(true);
    FRAPStudy frapStudy = xmlReader.getFrapStudy(XmlUtil.stringToXML(xmlString, null).getRootElement(), null);
    if (frapStudy == null || frapStudy.getFrapData() == null) {
        throw new Exception("no FrapData in file " + xmlFileName);
    }
    if (frapStudy.getFrapData().getImageDataset().getSizeC() != 1 || frapStudy.getFrapData().getImageDataset().getSizeZ() != 1) {
        throw new Exception("Only single channel, single z test implemented");
    }
    File imageDataSetZipFile = getCanonicalFilename(testDirectoryPath, null);
    File cellROIFile = getCanonicalFilename(testDirectoryPath, FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
    File bleachROIFile = getCanonicalFilename(testDirectoryPath, FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name());
    File backgroundROIFile = getCanonicalFilename(testDirectoryPath, FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name());
    FileOutputStream fos = new FileOutputStream(imageDataSetZipFile);
    BufferedOutputStream bos = new BufferedOutputStream(fos);
    ZipOutputStream zos = new ZipOutputStream(bos);
    ImageDataset imageDataset = frapStudy.getFrapData().getImageDataset();
    System.out.println(imageDataset.getISize());
    StringBuffer timeStampsSB = new StringBuffer();
    for (int i = 0; i < imageDataset.getSizeT(); i++) {
        // ZipEntry zipEntry = new ZipEntry((i<10?"00":(i<100?"0":""))+i+".bmp");
        ZipEntry zipEntry = new ZipEntry((i < 10 ? "00" : (i < 100 ? "0" : "")) + i + ".tif");
        zos.putNextEntry(zipEntry);
        UShortImage timePointImage = imageDataset.getImage(0, 0, i);
        File tempF = writeTempTiff(timePointImage.getPixels(), timePointImage.getNumX(), timePointImage.getNumY());
        FileInputStream fis = new FileInputStream(tempF);
        byte[] tempbytes = new byte[(int) tempF.length()];
        int offset = 0;
        while ((offset += fis.read(tempbytes, offset, tempbytes.length - offset)) != tempbytes.length) {
        }
        zos.write(tempbytes);
        // writeUShortFile(timePointImage, zos);
        timeStampsSB.append((i != 0 ? "," : "") + imageDataset.getImageTimeStamps()[i]);
        zos.closeEntry();
        fis.close();
        tempF.delete();
    }
    zos.close();
    fos.close();
    // fos = new FileOutputStream(cellROIFile);
    ROI cellROI = frapStudy.getFrapData().getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
    File cellTempF = writeTempTiff(cellROI.getRoiImages()[0].getPixels(), cellROI.getRoiImages()[0].getNumX(), cellROI.getRoiImages()[0].getNumY());
    FileUtils.copyFile(cellTempF, cellROIFile);
    cellTempF.delete();
    // writeUShortFile(cellROI.getRoiImages()[0], fos);
    // fos.close();
    // fos = new FileOutputStream(bleachROIFile);
    ROI bleachROI = frapStudy.getFrapData().getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name());
    File bleachTempf = writeTempTiff(bleachROI.getRoiImages()[0].getPixels(), bleachROI.getRoiImages()[0].getNumX(), bleachROI.getRoiImages()[0].getNumY());
    FileUtils.copyFile(bleachTempf, bleachROIFile);
    bleachTempf.delete();
    // writeUShortFile(bleachROI.getRoiImages()[0],fos);
    // fos.close();
    // fos = new FileOutputStream(backgroundROIFile);
    ROI backgroundROI = frapStudy.getFrapData().getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name());
    File backgroundTempf = writeTempTiff(backgroundROI.getRoiImages()[0].getPixels(), backgroundROI.getRoiImages()[0].getNumX(), backgroundROI.getRoiImages()[0].getNumY());
    FileUtils.copyFile(backgroundTempf, backgroundROIFile);
    backgroundTempf.delete();
    // writeUShortFile(backgroundROI.getRoiImages()[0],fos);
    // fos.close();
    checkExternalDataSameAsOriginal(frapStudy, testDirectoryPath);
    // if(true){System.exit(0);}
    String[] args = new String[] { // frapStudy.getFrapModelParameters().getPureDiffModelParameters().secondaryMobileFraction,
    testDirectoryPath, imageDataSetZipFile.getAbsolutePath(), cellROIFile.getAbsolutePath(), bleachROIFile.getAbsolutePath(), backgroundROIFile.getAbsolutePath(), new File(testDirectoryPath, "testCreated.xml").getAbsolutePath(), timeStampsSB.toString(), imageDataset.getExtent().getX() + "," + imageDataset.getExtent().getY() + "," + imageDataset.getExtent().getZ() };
    NonGUIFRAPTest.runSolver(args);
}
Also used : ImageDataset(cbit.vcell.VirtualMicroscopy.ImageDataset) ZipEntry(java.util.zip.ZipEntry) UShortImage(cbit.vcell.VirtualMicroscopy.UShortImage) ROI(cbit.vcell.VirtualMicroscopy.ROI) FileInputStream(java.io.FileInputStream) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 62 with ROI

use of cbit.vcell.VirtualMicroscopy.ROI in project vcell by virtualcell.

the class FrapDataUtils method saveImageDatasetAsExternalMatlabData.

// export the frap data to matlab file. The matlab file contains timestamps(1*Tn) , mask(numImgX * numImgY),
// ImageDataSet(1*Tn) each cell of (1*Tn) point to a 2d image(numImgX * numImgY)
public static void saveImageDatasetAsExternalMatlabData(FRAPData frapData, LocalWorkspace localWorkspace, String matlabFileName, int startingIndexForRecovery, CartesianMesh cartesianMesh) throws IOException {
    ImageDataset imageDataset = frapData.getImageDataset();
    if (imageDataset.getSizeC() > 1) {
        throw new RuntimeException("FRAPData.saveImageDatasetAsExternalMatlabData(): multiple channels not yet supported");
    }
    int numX = cartesianMesh.getSizeX();
    int numY = cartesianMesh.getSizeY();
    // prepare variable to write into matlab file, listOfVars is the outmost structure to write to Matlab file.
    ArrayList<MLArray> listOfVars = new ArrayList<MLArray>();
    double[] timeArray = imageDataset.getImageTimeStamps();
    ROI cellROI = frapData.getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name());
    short[] shortCellMask = cellROI.getPixelsXYZ();
    // add image data set to Matlab cell, each cell points to a numX*numY array
    MLCell imageCell = new MLCell("ImageDataSet", new int[] { timeArray.length, 1 });
    for (int tIndex = 0; tIndex < imageDataset.getSizeT(); tIndex++) {
        // images according to zIndex at specific time points(tIndex)
        short[] originalData = imageDataset.getPixelsZ(0, tIndex);
        double[] doubleImgData = new double[originalData.length];
        for (int i = 0; i < originalData.length; i++) {
            doubleImgData[i] = 0x0000ffff & originalData[i];
        }
        MLDouble mlDoublImgData = new MLDouble("ImageDataAtTime_" + tIndex, doubleImgData, numX);
        imageCell.set(mlDoublImgData, tIndex, 0);
    }
    listOfVars.add(imageCell);
    // create mask in a Matlab 2D double(numX*numY)
    double[] doubleCellMask = new double[shortCellMask.length];
    for (int i = 0; i < shortCellMask.length; i++) {
        doubleCellMask[i] = 0x0000ffff & shortCellMask[i];
    }
    MLDouble cellMask = new MLDouble("CellMask", doubleCellMask, numX);
    listOfVars.add(cellMask);
    // create times in a Matlab 2D double(1*numTimePoints)
    MLDouble times = new MLDouble("ExperimentalTimeStamps", new double[][] { timeArray });
    listOfVars.add(times);
    MatFileWriter writer = new MatFileWriter();
    writer.write(matlabFileName, listOfVars);
}
Also used : MLDouble(com.jmatio.types.MLDouble) MLArray(com.jmatio.types.MLArray) ImageDataset(cbit.vcell.VirtualMicroscopy.ImageDataset) MLCell(com.jmatio.types.MLCell) ArrayList(java.util.ArrayList) ROI(cbit.vcell.VirtualMicroscopy.ROI) MatFileWriter(com.jmatio.io.MatFileWriter)

Example 63 with ROI

use of cbit.vcell.VirtualMicroscopy.ROI in project vcell by virtualcell.

the class FRAPData method crop.

/**
 * Method crop.
 * @param rect Rectangle
 * @return FRAPData
 * @throws ImageException
 */
@Override
public FRAPData crop(Rectangle rect) throws ImageException {
    ImageDataset croppedImageDataset = getImageDataset().crop(rect);
    ROI[] rois = getRois();
    ROI[] croppedROIs = new ROI[rois.length];
    ROI currentlyDisplayedROI = getCurrentlyDisplayedROI();
    ROI croppedCurrentROI = null;
    for (int i = 0; i < croppedROIs.length; i++) {
        croppedROIs[i] = rois[i].crop(rect);
        if (currentlyDisplayedROI == rois[i]) {
            croppedCurrentROI = croppedROIs[i];
        }
    }
    FRAPData croppedFrapData = new FRAPData(croppedImageDataset, croppedROIs);
    setCurrentlyDisplayedROI(croppedCurrentROI);
    return croppedFrapData;
}
Also used : ImageDataset(cbit.vcell.VirtualMicroscopy.ImageDataset) ROI(cbit.vcell.VirtualMicroscopy.ROI)

Example 64 with ROI

use of cbit.vcell.VirtualMicroscopy.ROI in project vcell by virtualcell.

the class FRAPData method fixROIConstraints.

/**
 * must be consistent with checkBleachROIViolatesConstraints() and checkBackgroundROIViolatesConstraints()
 * @return
 * @throws Exception
 */
public boolean fixROIConstraints() throws Exception {
    short[] cellPixels = getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_CELL.name()).getPixelsXYZ();
    short[] bleachPixels = getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name()).getPixelsXYZ();
    short[] backgroundPixels = getRoi(FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name()).getPixelsXYZ();
    boolean bFixedBleach = false;
    boolean bFixedBackground = false;
    for (int i = 0; i < cellPixels.length; i++) {
        if (cellPixels[i] == 0 && bleachPixels[i] != 0) {
            bFixedBleach = true;
            bleachPixels[i] = 0;
        }
        if (cellPixels[i] != 0 && backgroundPixels[i] != 0) {
            bFixedBackground = true;
            backgroundPixels[i] = 0;
        }
    }
    if (bFixedBackground || bFixedBleach) {
        if (bFixedBleach) {
            UShortImage ushortImage = new UShortImage(bleachPixels, getCurrentlyDisplayedROI().getRoiImages()[0].getOrigin(), getCurrentlyDisplayedROI().getRoiImages()[0].getExtent(), getCurrentlyDisplayedROI().getISize().getX(), getCurrentlyDisplayedROI().getISize().getY(), getCurrentlyDisplayedROI().getISize().getZ());
            ROI newBleachROI = new ROI(ushortImage, FRAPData.VFRAP_ROI_ENUM.ROI_BLEACHED.name());
            addReplaceRoi(newBleachROI);
        }
        if (bFixedBackground) {
            UShortImage ushortImage = new UShortImage(backgroundPixels, getCurrentlyDisplayedROI().getRoiImages()[0].getOrigin(), getCurrentlyDisplayedROI().getRoiImages()[0].getExtent(), getCurrentlyDisplayedROI().getISize().getX(), getCurrentlyDisplayedROI().getISize().getY(), getCurrentlyDisplayedROI().getISize().getZ());
            ROI newBackgroundROI = new ROI(ushortImage, FRAPData.VFRAP_ROI_ENUM.ROI_BACKGROUND.name());
            addReplaceRoi(newBackgroundROI);
        }
        return true;
    } else // nothing to fix
    {
        return true;
    }
}
Also used : UShortImage(cbit.vcell.VirtualMicroscopy.UShortImage) ROI(cbit.vcell.VirtualMicroscopy.ROI)

Example 65 with ROI

use of cbit.vcell.VirtualMicroscopy.ROI in project vcell by virtualcell.

the class KenworthyWorkflowTest method analyzeKeyworthy.

/**
 * Fits raw image time series data to uniform disk models (with Guassian or Uniform fluorescence).
 *
 * @param rawTimeSeriesImages
 * @param localWorkspace
 * @throws Exception
 */
private static void analyzeKeyworthy(ImageTimeSeries<UShortImage> rawTimeSeriesImages, LocalWorkspace localWorkspace) throws Exception {
    new DisplayTimeSeriesOp().displayImageTimeSeries(rawTimeSeriesImages, "raw images", (WindowListener) null);
    double cellThreshold = 0.5;
    GeometryRoisAndBleachTiming cellROIresults = new GenerateCellROIsFromRawFrapTimeSeriesOp().generate(rawTimeSeriesImages, cellThreshold);
    ROI backgroundROI = cellROIresults.backgroundROI_2D;
    ROI cellROI = cellROIresults.cellROI_2D;
    int indexOfFirstPostbleach = cellROIresults.indexOfFirstPostbleach;
    new DisplayImageOp().displayImage(backgroundROI.getRoiImages()[0], "background ROI", null);
    new DisplayImageOp().displayImage(cellROI.getRoiImages()[0], "cell ROI", null);
    NormalizedFrapDataResults normResults = new GenerateNormalizedFrapDataOp().generate(rawTimeSeriesImages, backgroundROI, indexOfFirstPostbleach);
    ImageTimeSeries<FloatImage> normalizedTimeSeries = normResults.normalizedFrapData;
    FloatImage prebleachAvg = normResults.prebleachAverage;
    FloatImage normalizedPostbleach = normalizedTimeSeries.getAllImages()[0];
    new DisplayTimeSeriesOp().displayImageTimeSeries(normalizedTimeSeries, "normalized images", (WindowListener) null);
    // 
    // create a single bleach ROI by thresholding
    // 
    double bleachThreshold = 0.80;
    ROI bleachROI = new GenerateBleachRoiOp().generateBleachRoi(normalizedPostbleach, cellROI, bleachThreshold);
    // 
    // only use bleach ROI for fitting etc.
    // 
    // ROI[] dataROIs = new ROI[] { bleachROI };
    // 
    // fit 2D Gaussian to normalized data to determine center, radius and K factor of bleach (assuming exp(-exp
    // 
    FitBleachSpotOpResults fitSpotResults = new FitBleachSpotOp().fit(NormalizedSampleFunction.fromROI(bleachROI), normalizedTimeSeries.getAllImages()[0]);
    double bleachFactorK_GaussianFit = fitSpotResults.bleachFactorK_GaussianFit;
    double bleachRadius_GaussianFit = fitSpotResults.bleachRadius_GaussianFit;
    double bleachRadius_ROI = fitSpotResults.bleachRadius_ROI;
    double centerX_GaussianFit = fitSpotResults.centerX_GaussianFit;
    double centerX_ROI = fitSpotResults.centerX_ROI;
    double centerY_GaussianFit = fitSpotResults.centerY_GaussianFit;
    double centerY_ROI = fitSpotResults.centerY_ROI;
    NormalizedSampleFunction[] sampleFunctions = new NormalizedSampleFunction[] { NormalizedSampleFunction.fromROI(bleachROI) };
    // 
    // get reduced data and errors for each ROI
    // 
    RowColumnResultSet reducedData = new GenerateReducedDataOp().generateReducedData(normalizedTimeSeries, sampleFunctions);
    RowColumnResultSet measurementErrors = new ComputeMeasurementErrorOp().computeNormalizedMeasurementError(sampleFunctions, indexOfFirstPostbleach, rawTimeSeriesImages, prebleachAvg, null);
    ErrorFunction errorFunction = new ErrorFunctionKenworthy(reducedData);
    // 
    // 2 parameter uniform disk model
    // 
    OptModel uniformDisk2OptModel = new OptModelKenworthyUniformDisk2P(bleachRadius_ROI);
    String title_u2 = "Uniform Disk Model - 2 parameters, (Rn=" + bleachRadius_ROI + ")";
    OptContext uniformDisk2Context = new Generate2DOptContextOp().generate2DOptContext(uniformDisk2OptModel, reducedData, measurementErrors, errorFunction);
    new DisplayInteractiveModelOp().displayOptModel(uniformDisk2Context, sampleFunctions, localWorkspace, title_u2, null);
    // 
    // 3 parameter uniform disk model
    // 
    OptModel uniformDisk3OptModel = new OptModelKenworthyUniformDisk3P(bleachRadius_ROI);
    OptContext uniformDisk3Context = new Generate2DOptContextOp().generate2DOptContext(uniformDisk3OptModel, reducedData, measurementErrors, errorFunction);
    String title_u3 = "Uniform Disk Model - 3 parameters, (Rn=" + bleachRadius_ROI + ")";
    new DisplayInteractiveModelOp().displayOptModel(uniformDisk3Context, sampleFunctions, localWorkspace, title_u3, null);
    // 
    // GaussianFit parameter uniform disk model
    // 
    FloatImage prebleachBleachAreaImage = new FloatImage(prebleachAvg);
    // mask-out all but the bleach area
    prebleachBleachAreaImage.and(bleachROI.getRoiImages()[0]);
    double prebleachAvgInROI = prebleachBleachAreaImage.getImageStatistics().meanValue;
    OptModel gaussian2OptModel = new OptModelKenworthyGaussian(prebleachAvgInROI, bleachFactorK_GaussianFit, bleachRadius_GaussianFit, bleachRadius_ROI);
    OptContext gaussianDisk2Context = new Generate2DOptContextOp().generate2DOptContext(gaussian2OptModel, reducedData, measurementErrors, errorFunction);
    String title_g2 = "Gaussian Disk Model - 2 parameters (prebleach=" + prebleachAvgInROI + ",K=" + bleachFactorK_GaussianFit + ",Re=" + bleachRadius_GaussianFit + ",Rnom=" + bleachRadius_ROI + ")";
    new DisplayInteractiveModelOp().displayOptModel(gaussianDisk2Context, sampleFunctions, localWorkspace, title_g2, null);
}
Also used : OptModelKenworthyGaussian(org.vcell.vmicro.workflow.data.OptModelKenworthyGaussian) GenerateCellROIsFromRawFrapTimeSeriesOp(org.vcell.vmicro.op.GenerateCellROIsFromRawFrapTimeSeriesOp) FloatImage(cbit.vcell.VirtualMicroscopy.FloatImage) OptContext(org.vcell.vmicro.workflow.data.OptContext) OptModel(org.vcell.vmicro.workflow.data.OptModel) DisplayImageOp(org.vcell.vmicro.op.display.DisplayImageOp) DisplayTimeSeriesOp(org.vcell.vmicro.op.display.DisplayTimeSeriesOp) NormalizedSampleFunction(org.vcell.vmicro.workflow.data.NormalizedSampleFunction) DisplayInteractiveModelOp(org.vcell.vmicro.op.display.DisplayInteractiveModelOp) GenerateReducedDataOp(org.vcell.vmicro.op.GenerateReducedDataOp) OptModelKenworthyUniformDisk2P(org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk2P) FitBleachSpotOp(org.vcell.vmicro.op.FitBleachSpotOp) ErrorFunctionKenworthy(org.vcell.vmicro.workflow.data.ErrorFunctionKenworthy) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) ErrorFunction(org.vcell.vmicro.workflow.data.ErrorFunction) GeometryRoisAndBleachTiming(org.vcell.vmicro.op.GenerateCellROIsFromRawFrapTimeSeriesOp.GeometryRoisAndBleachTiming) NormalizedFrapDataResults(org.vcell.vmicro.op.GenerateNormalizedFrapDataOp.NormalizedFrapDataResults) GenerateBleachRoiOp(org.vcell.vmicro.op.GenerateBleachRoiOp) Generate2DOptContextOp(org.vcell.vmicro.op.Generate2DOptContextOp) ROI(cbit.vcell.VirtualMicroscopy.ROI) ComputeMeasurementErrorOp(org.vcell.vmicro.op.ComputeMeasurementErrorOp) FitBleachSpotOpResults(org.vcell.vmicro.op.FitBleachSpotOp.FitBleachSpotOpResults) GenerateNormalizedFrapDataOp(org.vcell.vmicro.op.GenerateNormalizedFrapDataOp) OptModelKenworthyUniformDisk3P(org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk3P)

Aggregations

ROI (cbit.vcell.VirtualMicroscopy.ROI)72 UShortImage (cbit.vcell.VirtualMicroscopy.UShortImage)26 RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)10 ArrayList (java.util.ArrayList)10 UserCancelException (org.vcell.util.UserCancelException)10 ImageDataset (cbit.vcell.VirtualMicroscopy.ImageDataset)9 ImageException (cbit.image.ImageException)8 Image (cbit.vcell.VirtualMicroscopy.Image)7 FRAPData (cbit.vcell.microscopy.FRAPData)7 BioModel (cbit.vcell.biomodel.BioModel)6 FloatImage (cbit.vcell.VirtualMicroscopy.FloatImage)5 FieldFunctionArguments (cbit.vcell.field.FieldFunctionArguments)5 RegionInfo (cbit.vcell.geometry.RegionImage.RegionInfo)5 File (java.io.File)5 Hashtable (java.util.Hashtable)5 Element (org.jdom.Element)5 Extent (org.vcell.util.Extent)5 VCImage (cbit.image.VCImage)4 VCImageUncompressed (cbit.image.VCImageUncompressed)4 Expression (cbit.vcell.parser.Expression)4