Search in sources :

Example 1 with ErrorFunctionKenworthy

use of org.vcell.vmicro.workflow.data.ErrorFunctionKenworthy 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)

Example 2 with ErrorFunctionKenworthy

use of org.vcell.vmicro.workflow.data.ErrorFunctionKenworthy in project vcell by virtualcell.

the class KenworthyParticleTest 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, RowColumnResultSet reducedData, 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 RowColumnResultSet(new String[] { reducedData.getColumnDescriptions()[0].getName(), reducedData.getColumnDescriptions()[1].getName() });
    for (int i = 0; i < reducedData.getRowCount(); i++) {
        double[] row = new double[] { reducedData.getRow(i)[0], Math.sqrt(reducedData.getRow(i)[1]) };
        measurementErrors.addRow(row);
    }
    new DisplayPlotOp().displayPlot(reducedData, "raw reduced data", null);
    new DisplayPlotOp().displayPlot(measurementErrors, "raw reduced noise", null);
    // 
    // find large drop and determine first post-bleach timepoint.
    // 
    double largestDrop = -1000000;
    int indexLargestDrop = -1;
    for (int i = 1; i < reducedData.getRowCount(); i++) {
        double drop = reducedData.getRow(i - 1)[1] - reducedData.getRow(i)[1];
        if (drop > largestDrop) {
            indexLargestDrop = i;
            largestDrop = drop;
        }
    }
    System.out.println("index of first postbleach is " + indexLargestDrop);
    // 
    // normalize data and noise and start with first post-bleach index;
    // 
    RowColumnResultSet normalizedReducedData = new RowColumnResultSet(new String[] { reducedData.getColumnDescriptions()[0].getName(), reducedData.getColumnDescriptions()[1].getName() });
    RowColumnResultSet normalizedNoiseData = new RowColumnResultSet(new String[] { reducedData.getColumnDescriptions()[0].getName(), reducedData.getColumnDescriptions()[1].getName() });
    for (int i = indexLargestDrop; i < reducedData.getRowCount(); i++) {
        double timeAfterBleach = reducedData.getRow(i)[0] - reducedData.getRow(indexLargestDrop)[0];
        double normalizedData = reducedData.getRow(i)[1] / reducedData.getRow(0)[1];
        double[] newDataRow = new double[] { timeAfterBleach, normalizedData };
        normalizedReducedData.addRow(newDataRow);
        double normalizedNoise = measurementErrors.getRow(i)[1] / reducedData.getRow(0)[1];
        double[] newNoiseRow = new double[] { timeAfterBleach, normalizedNoise };
        normalizedNoiseData.addRow(newNoiseRow);
    }
    new DisplayPlotOp().displayPlot(normalizedReducedData, "normalized reduced data", null);
    new DisplayPlotOp().displayPlot(normalizedNoiseData, "normalized reduced noise", null);
    NormalizedSampleFunction[] sampleFunctions = new NormalizedSampleFunction[] { NormalizedSampleFunction.createUniform("psf", null, null, new ISize(10, 10, 1)) };
    ErrorFunction errorFunction = new ErrorFunctionKenworthy(normalizedReducedData);
    // ErrorFunction errorFunction = new ErrorFunctionMeanSquared();
    double bleachRadius_ROI = 2;
    // 
    // 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, normalizedReducedData, normalizedNoiseData, 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, normalizedReducedData, normalizedNoiseData, errorFunction);
    String title_u3 = "Uniform Disk Model - 3 parameters, (Rn=" + bleachRadius_ROI + ")";
    new DisplayInteractiveModelOp().displayOptModel(uniformDisk3Context, sampleFunctions, localWorkspace, title_u3, null);
    ProfileData[] profileData = new RunProfileLikelihoodGeneralOp().runProfileLikihood(uniformDisk3Context, null);
    new DisplayProfileLikelihoodPlotsOp().displayProfileLikelihoodPlots(profileData, "3param model", null);
// 
// GaussianFit parameter uniform disk model
// 
// FloatImage prebleachBleachAreaImage = new FloatImage(prebleachAvg);
// prebleachBleachAreaImage.and(bleachROI.getRoiImages()[0]); // mask-out all but the bleach area
// 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 : DisplayProfileLikelihoodPlotsOp(org.vcell.vmicro.op.display.DisplayProfileLikelihoodPlotsOp) ISize(org.vcell.util.ISize) RunProfileLikelihoodGeneralOp(org.vcell.vmicro.op.RunProfileLikelihoodGeneralOp) ProfileData(org.vcell.optimization.ProfileData) Generate2DOptContextOp(org.vcell.vmicro.op.Generate2DOptContextOp) OptContext(org.vcell.vmicro.workflow.data.OptContext) OptModel(org.vcell.vmicro.workflow.data.OptModel) DisplayPlotOp(org.vcell.vmicro.op.display.DisplayPlotOp) DisplayTimeSeriesOp(org.vcell.vmicro.op.display.DisplayTimeSeriesOp) NormalizedSampleFunction(org.vcell.vmicro.workflow.data.NormalizedSampleFunction) DisplayInteractiveModelOp(org.vcell.vmicro.op.display.DisplayInteractiveModelOp) OptModelKenworthyUniformDisk2P(org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk2P) ErrorFunctionKenworthy(org.vcell.vmicro.workflow.data.ErrorFunctionKenworthy) RowColumnResultSet(cbit.vcell.math.RowColumnResultSet) ErrorFunction(org.vcell.vmicro.workflow.data.ErrorFunction) OptModelKenworthyUniformDisk3P(org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk3P)

Aggregations

RowColumnResultSet (cbit.vcell.math.RowColumnResultSet)2 Generate2DOptContextOp (org.vcell.vmicro.op.Generate2DOptContextOp)2 DisplayInteractiveModelOp (org.vcell.vmicro.op.display.DisplayInteractiveModelOp)2 DisplayTimeSeriesOp (org.vcell.vmicro.op.display.DisplayTimeSeriesOp)2 ErrorFunction (org.vcell.vmicro.workflow.data.ErrorFunction)2 ErrorFunctionKenworthy (org.vcell.vmicro.workflow.data.ErrorFunctionKenworthy)2 NormalizedSampleFunction (org.vcell.vmicro.workflow.data.NormalizedSampleFunction)2 OptContext (org.vcell.vmicro.workflow.data.OptContext)2 OptModel (org.vcell.vmicro.workflow.data.OptModel)2 OptModelKenworthyUniformDisk2P (org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk2P)2 OptModelKenworthyUniformDisk3P (org.vcell.vmicro.workflow.data.OptModelKenworthyUniformDisk3P)2 FloatImage (cbit.vcell.VirtualMicroscopy.FloatImage)1 ROI (cbit.vcell.VirtualMicroscopy.ROI)1 ProfileData (org.vcell.optimization.ProfileData)1 ISize (org.vcell.util.ISize)1 ComputeMeasurementErrorOp (org.vcell.vmicro.op.ComputeMeasurementErrorOp)1 FitBleachSpotOp (org.vcell.vmicro.op.FitBleachSpotOp)1 FitBleachSpotOpResults (org.vcell.vmicro.op.FitBleachSpotOp.FitBleachSpotOpResults)1 GenerateBleachRoiOp (org.vcell.vmicro.op.GenerateBleachRoiOp)1 GenerateCellROIsFromRawFrapTimeSeriesOp (org.vcell.vmicro.op.GenerateCellROIsFromRawFrapTimeSeriesOp)1