Commit 789ac98d authored by gonciarz's avatar gonciarz

Distance plots are handled by GUI now

parent 92fa0cb4
......@@ -8,14 +8,13 @@ import org.apache.log4j.Logger;
import org.scijava.vecmath.Point3d;
import fr.inria.optimization.cmaes.CMAEvolutionStrategy;
import ij.IJ;
import ij.ImagePlus;
import mosaic.ia.HypothesisTesting.TestResult;
import mosaic.ia.Potentials.Potential;
import mosaic.ia.Potentials.PotentialType;
import mosaic.ia.gui.DistributionsPlot;
import mosaic.ia.gui.EstimatedPotentialPlot;
import mosaic.ia.gui.PlotHistogram;
import mosaic.ia.gui.Utils;
import mosaic.utils.Debug;
import mosaic.utils.math.StatisticsUtils;
import mosaic.utils.math.StatisticsUtils.MinMaxMean;
......@@ -25,10 +24,10 @@ public class Analysis {
private Potential iPotential;
private DistanceCalculations iDistanceCalculations;
private double[] iContextQdDistancesGrid;
private double[] iContextQdPdf;
private double[] iNearestNeighborDistancesXtoY;
private double[] iNearestNeighborDistancesXtoYPdf;
public double[] iContextQdDistancesGrid;
public double[] iContextQdPdf;
public double[] iNearestNeighborDistancesXtoY;
public double[] iNearestNeighborDistancesXtoYPdf;
private double[][] iBestPointsFound;
private int iBestPointIndex = -1;
......@@ -51,12 +50,6 @@ public class Analysis {
StatisticsUtils.normalizePdf(iContextQdPdf, iContextQdDistancesGrid, false);
StatisticsUtils.normalizePdf(iNearestNeighborDistancesXtoYPdf, iContextQdDistancesGrid, false);
new DistributionsPlot(iContextQdDistancesGrid, iContextQdPdf, iNearestNeighborDistancesXtoYPdf).show();
PlotHistogram.plot("ObservedDistances", iNearestNeighborDistancesXtoY, getOptimBins(iNearestNeighborDistancesXtoY, 8, iNearestNeighborDistancesXtoY.length / 8));
double suggestedKernel = calcWekaWeights(iNearestNeighborDistancesXtoY);
IJ.showMessage("Suggested Kernel wt(p): " + suggestedKernel);
logger.debug("Suggested kernel wt(p)=" + suggestedKernel);
}
public static class CmaResult {
......@@ -124,7 +117,7 @@ public class Analysis {
logger.debug("Best Parameters Found:" + Debug.getString(iBestPointsFound[iBestPointIndex]) + " fit function value=" + bestFunctionValue[iBestPointIndex]);
if (diffFitness) {
IJ.showMessage("Warning: Optimization returned different results for reruns. The results may not be accurate. Displaying the parameters and the plots corr. to best fitness.");
Utils.messageDialog("IA - CMA optimization", "Warning: Optimization returned different results for reruns. The results may not be accurate. Displaying the parameters and the plots corr. to best fitness.");
}
fitfun.l2Norm(iBestPointsFound[iBestPointIndex]); // to calc pgrid for best params
new EstimatedPotentialPlot(iContextQdDistancesGrid, iPotential, iBestPointsFound[iBestPointIndex], bestFunctionValue[iBestPointIndex]).show();
......@@ -210,11 +203,11 @@ public class Analysis {
public TestResult hypothesisTesting(int monteCarloRunsForTest, double alpha) {
if (iBestPointsFound == null) {
IJ.showMessage("Error: Run estimation first");
Utils.messageDialog("IA - hypothesis testing", "Error: Run estimation first");
return null;
}
else if (iPotential.getType() == PotentialType.NONPARAM) {
IJ.showMessage("Hypothesis test is not applicable for Non Parametric potential \n since it does not have 'strength' parameter");
Utils.messageDialog("IA - hypothesis testing", "Hypothesis test is not applicable for Non Parametric potential \n since it does not have 'strength' parameter");
return null;
}
else {
......@@ -284,6 +277,6 @@ public class Analysis {
}
public void setPotentialType(Potential potentialType) {
this.iPotential = potentialType;
iPotential = potentialType;
}
}
......@@ -7,6 +7,7 @@ import org.apache.log4j.Logger;
import org.scijava.vecmath.Point3d;
import ij.IJ;
import mosaic.ia.gui.Utils;
import mosaic.utils.Debug;
import mosaic.utils.math.NearestNeighborTree;
import mosaic.utils.math.StatisticsUtils;
......@@ -84,7 +85,7 @@ public abstract class DistanceCalculations {
logger.debug("Number of points (X/Y): " + iParticlesX.length + " / " + iParticlesY.length);
logger.debug("min/max of x: " + aMinX + "/" + aMaxX + " y: " + aMinY + "/" + aMaxY + " z: " + aMinZ + "/" + aMaxZ);
if (iParticlesX.length == 0 || iParticlesY.length == 0) {
IJ.showMessage("Number discaverd (and filtered) particles cannot be 0 for further calculations.\nNumber of particles (x/y): " + iParticlesX.length + " " + iParticlesY.length);
Utils.messageDialog("IA - state density", "Number discaverd (and filtered) particles cannot be 0 for further calculations.\nNumber of particles (x/y): " + iParticlesX.length + " " + iParticlesY.length);
throw new RuntimeException("Not enough particles to perform calculations!");
}
......
......@@ -5,14 +5,13 @@ import java.io.File;
import java.util.Vector;
import org.scijava.vecmath.Point3d;
import org.supercsv.cellprocessor.ParseDouble;
import org.supercsv.cellprocessor.ift.CellProcessor;
import ij.IJ;
import ij.ImagePlus;
import ij.io.OpenDialog;
import ij.io.Opener;
import mosaic.ia.gui.Utils;
import mosaic.utils.io.csv.CSV;
import mosaic.utils.io.csv.CsvColumnConfig;
......@@ -75,7 +74,7 @@ public class FileUtils {
file = new File(aFileName);
}
if (!file.exists()) {
IJ.showMessage("There is no file [" + file.getName() + "]");
Utils.messageDialog("IA - open CSV file", "There is no file [" + file.getName() + "]");
return null;
}
......@@ -83,7 +82,7 @@ public class FileUtils {
CSV<Point3dCsvReadWrapper>csv = new CSV<Point3dCsvReadWrapper>(Point3dCsvReadWrapper.class);
int numOfCols = csv.setCSVPreferenceFromFile(file.getAbsolutePath());
if (numOfCols <= 1 || numOfCols > 3) {
IJ.showMessage("CSV file should have 2 or 3 columns of data with comma or semicolon delimieters and no header!");
Utils.messageDialog("IA - open CSV file", "CSV file should have 2 or 3 columns of data with comma or semicolon delimieters and no header!");
return null;
}
CsvColumnConfig ccc = numOfCols == 2
......@@ -91,7 +90,7 @@ public class FileUtils {
: Point3dCsvReadWrapper.getConfig3D();
final Vector<Point3dCsvReadWrapper> outdst = csv.Read(file.getAbsolutePath(), ccc, true);
if (outdst.isEmpty()) {
IJ.showMessage("Incorrect CSV file chosen [" + file.getName() + "]\nCSV file should have 2 or 3 columns of data with comma or semicolon delimieters and no header!\n");
Utils.messageDialog("IA - open CSV file", "Incorrect CSV file chosen [" + file.getName() + "]\nCSV file should have 2 or 3 columns of data with comma or semicolon delimieters and no header!\n");
return null;
}
......
......@@ -8,6 +8,7 @@ import org.apache.log4j.Logger;
import ij.IJ;
import mosaic.ia.Potentials.Potential;
import mosaic.ia.gui.Utils;
class HypothesisTesting {
......@@ -58,7 +59,7 @@ class HypothesisTesting {
}
logger.debug(s);
IJ.showMessage(s);
Utils.messageDialog("IA - hypothesis testing result", s);
return new TestResult (
i,
......
......@@ -12,6 +12,7 @@ import java.util.Set;
import javax.swing.JButton;
import org.apache.log4j.Logger;
import org.scijava.vecmath.Point3d;
import ij.IJ;
......@@ -36,8 +37,10 @@ import mosaic.ia.Potentials.PotentialType;
public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
private static final Logger logger = Logger.getLogger(InteractionAnalysisGui.class);
public static void main(String[] args) {
// new ImageJ();
ImagePlus image = IJ.openImage("/Users/gonciarz/4rois.tif");
image.show();
// OverlayCommands oc = new OverlayCommands();
......@@ -105,7 +108,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
private void testHypothesis() {
if (iAnalysis == null) {
IJ.showMessage("Error: Calculate distances first!");
Utils.messageDialog("IA - test hypothesis", "Error: Calculate distances first!");
return;
}
int monteCarloRunsForTest = Integer.parseInt(monteCarloRuns.getText());
......@@ -116,7 +119,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
private void estimatePotential() {
if (iAnalysis == null) {
IJ.showMessage("Error: Calculate distances first!");
Utils.messageDialog("IA - estimate potential", "Error: Calculate distances first!");
return;
}
int numOfSupportPointsValue = Integer.parseInt(numOfSupportPoints.getText());
......@@ -162,7 +165,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
double ymax = Double.parseDouble(yMax.getText());
double zmax = Double.parseDouble(zMax.getText());
if (xmax < xmin || ymax < ymin || zmax < zmin) {
IJ.showMessage("Error: boundary values are not correct");
Utils.messageDialog("IA - calculate distances", "Error: boundary values are not correct");
return;
}
......@@ -172,14 +175,14 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
iAnalysis.calcDist(gridDelta, qkernelWeight, pkernelWeight, mask3d, iCsvX, iCsvY, xmin, xmax, ymin, ymax, zmin, zmax);
}
else {
IJ.showMessage("Load X and Y coordinates first.");
Utils.messageDialog("IA - calculate distances", "Load X and Y coordinates first.");
}
}
else if (getTabType() == Tabs.IMG) {
// image
if (iImgY != null && iImgX != null) {
if (!checkIfImagesAreSameSize()) {
IJ.showMessage("Error: Image sizes/scale/unit do not match");
Utils.messageDialog("IA - calculate distances", "Error: Image sizes/scale/unit do not match");
}
else {
iAnalysis = new Analysis();
......@@ -187,7 +190,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
}
}
else {
IJ.showMessage("Load X and Y images first.");
Utils.messageDialog("IA - calculate distances", "Load X and Y coordinates first.");
}
}
else if (getTabType() == Tabs.ROI) {
......@@ -203,7 +206,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
double ymax = Double.parseDouble(yMax.getText());
double zmax = Double.parseDouble(zMax.getText());
if (xmax < xmin || ymax < ymin || zmax < zmin) {
IJ.showMessage("Error: boundary values are not correct");
Utils.messageDialog("IA - calculate distances", "Error: boundary values are not correct");
return;
}
System.out.println("X=" + iCsvX.length + " Y=" + iCsvY.length);
......@@ -216,6 +219,12 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
else {
throw new RuntimeException("Unknown tab chosen in IA GUI");
}
new DistributionsPlot(iAnalysis.iContextQdDistancesGrid, iAnalysis.iContextQdPdf, iAnalysis.iNearestNeighborDistancesXtoYPdf).show();
Utils.plotHistogram("ObservedDistances", iAnalysis.iNearestNeighborDistancesXtoY, Analysis.getOptimBins(iAnalysis.iNearestNeighborDistancesXtoY, 8, iAnalysis.iNearestNeighborDistancesXtoY.length / 8));
double suggestedKernel = Analysis.calcWekaWeights(iAnalysis.iNearestNeighborDistancesXtoY);
Utils.messageDialog("IA - kernel", "Suggested Kernel wt(p): " + suggestedKernel);
logger.debug("Suggested kernel wt(p)=" + suggestedKernel);
}
@Override
......@@ -355,7 +364,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
private void generateMask() {
if (getTabType() != Tabs.IMG) {
IJ.showMessage("Cannot generate mask for coordinates. Load a mask instead.");
Utils.messageDialog("IA - generate mask", "Cannot generate mask for coordinates. Load a mask instead.");
}
else if (iImgY != null) {
iMaskImg = new Duplicator().run(iImgY);
......@@ -365,18 +374,18 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
maskPane.setTitleAt(0, "Mask: <generated>");
}
else {
IJ.showMessage("Image Y is null: Cannot generate mask");
Utils.messageDialog("IA - generate mask", "Image Y is null: Cannot generate mask");
}
}
private boolean loadMask() {
ImagePlus tempMask = FileUtils.openImage();
if (tempMask == null) {
IJ.showMessage("Filetype not recognized");
Utils.messageDialog("IA - load mask", "Filetype not recognized");
return false;
}
else if (tempMask.getType() != ImagePlus.GRAY8) {
IJ.showMessage("ERROR: Loaded mask not 8 bit gray");
Utils.messageDialog("IA - load mask", "ERROR: Loaded mask not 8 bit gray");
return false;
}
......@@ -390,7 +399,7 @@ public class InteractionAnalysisGui extends InteractionAnalysisGuiBase {
private ImagePlus loadImage(String aStatusBarMessage, JButton aLoadImgButton) {
ImagePlus tempImg = FileUtils.openImage();
if (tempImg == null) {
IJ.showMessage("Cancelled/Filetype not recognized");
Utils.messageDialog("IA - load image", "Cancelled/Filetype not recognized");
return null;
}
tempImg.show(aStatusBarMessage);
......
package mosaic.ia.gui;
import javax.swing.JOptionPane;
import ij.IJ;
import ij.ImagePlus;
import ij.gui.HistogramWindow;
import ij.process.FloatProcessor;
public class PlotHistogram {
public class Utils {
/**
* Plots histogram for provided values using aNumOfBins
......@@ -15,8 +18,21 @@ public class PlotHistogram {
* @param aNumOfBins
* @return number of elements in each bin
*/
public static HistogramWindow plot(String aTitle, double[] aValues, int aNumOfBins) {
public static HistogramWindow plotHistogram(String aTitle, double[] aValues, int aNumOfBins) {
final FloatProcessor hist = new FloatProcessor(aValues.length, 1, aValues);
System.out.println("HISTOGRAM..");
return new ij.gui.HistogramWindow(aTitle, new ImagePlus(aTitle, hist), aNumOfBins);
}
/**
* Shows message - when IJ instance is found then IJ component is used (which is good since it is IJ window
* and it's managed by IJ), swig component otherwise.
* @param aTitle
* @param aMessage
*/
public static void messageDialog(String aTitle, String aMessage) {
if (IJ.getInstance() != null) IJ.showMessage(aMessage);
else JOptionPane.showMessageDialog(null, aMessage, aTitle, JOptionPane.PLAIN_MESSAGE);
}
}
......@@ -5,14 +5,14 @@ import static org.junit.Assert.assertArrayEquals;
import org.junit.Test;
import ij.gui.HistogramWindow;
import mosaic.ia.gui.PlotHistogram;
import mosaic.ia.gui.Utils;
public class PlotHistogramTest {
@Test
public void testPlotHistogram() {
HistogramWindow hist = PlotHistogram.plot("Hello", /* input values */ new double[] {1, 2, 10, 19}, /* num of bins */ 3);
HistogramWindow hist = Utils.plotHistogram("Hello", /* input values */ new double[] {1, 2, 10, 19}, /* num of bins */ 3);
assertArrayEquals(new int[] {2, 1, 1}, hist.getHistogram());
// Expected ranges are: [1, 7) [7, 13) [13, 19)
assertArrayEquals(new double[] {1, 7, 13}, hist.getXValues(), 0.01);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment