Search in sources :

Example 6 with FilePeakResults

use of gdsc.smlm.results.FilePeakResults in project GDSC-SMLM by aherbert.

the class ResultsMatchCalculator method compareCoordinates.

private void compareCoordinates(MemoryPeakResults results1, MemoryPeakResults results2, double dThreshold, int increments, double delta) {
    boolean requirePairs = showPairs || saveClassifications;
    FilePeakResults fileResults = createFilePeakResults(results2);
    List<PointPair> allMatches = new LinkedList<PointPair>();
    List<PointPair> pairs = (requirePairs) ? new LinkedList<PointPair>() : null;
    List<PeakResult> actualPoints = results1.getResults();
    List<PeakResult> predictedPoints = results2.getResults();
    double maxDistance = dThreshold + increments * delta;
    // Old implementation
    //// Process each time point
    //for (Integer t : getTimepoints(actualPoints, predictedPoints))
    //{
    //	Coordinate[] actual = getCoordinates(actualPoints, t);
    //	Coordinate[] predicted = getCoordinates(predictedPoints, t);
    // Divide the results into time points
    TIntObjectHashMap<ArrayList<Coordinate>> actualCoordinates = getCoordinates(actualPoints);
    TIntObjectHashMap<ArrayList<Coordinate>> predictedCoordinates = getCoordinates(predictedPoints);
    int n1 = 0;
    int n2 = 0;
    // Process each time point
    for (Integer t : getTimepoints(actualCoordinates, predictedCoordinates)) {
        Coordinate[] actual = getCoordinates(actualCoordinates, t);
        Coordinate[] predicted = getCoordinates(predictedCoordinates, t);
        List<Coordinate> TP = null;
        List<Coordinate> FP = null;
        List<Coordinate> FN = null;
        List<PointPair> matches = new LinkedList<PointPair>();
        if (requirePairs) {
            FP = new LinkedList<Coordinate>();
            FN = new LinkedList<Coordinate>();
        }
        MatchCalculator.analyseResults2D(actual, predicted, maxDistance, TP, FP, FN, matches);
        // Aggregate
        n1 += actual.length;
        n2 += predicted.length;
        allMatches.addAll(matches);
        if (showPairs) {
            pairs.addAll(matches);
            for (Coordinate c : FN) pairs.add(new PointPair(c, null));
            for (Coordinate c : FP) pairs.add(new PointPair(null, c));
        }
        if (fileResults != null) {
            // Matches are marked in the original value with 1 for true, 0 for false 
            for (PointPair pair : matches) {
                PeakResult p = ((PeakResultPoint) pair.getPoint2()).peakResult;
                fileResults.add(p.getFrame(), p.origX, p.origY, 1, p.error, p.noise, p.params, null);
            }
            for (Coordinate c : FP) {
                PeakResult p = ((PeakResultPoint) c).peakResult;
                fileResults.add(p.getFrame(), p.origX, p.origY, 0, p.error, p.noise, p.params, null);
            }
        }
    }
    if (fileResults != null)
        fileResults.end();
    // XXX : DEBUGGING : Output for signal correlation and fitting analysis
    /*
		 * try
		 * {
		 * OutputStreamWriter o = new OutputStreamWriter(new FileOutputStream("/tmp/ResultsMatchCalculator.txt"));
		 * FilePeakResults r1 = new FilePeakResults("/tmp/" + results1.getName() + ".txt", false);
		 * FilePeakResults r2 = new FilePeakResults("/tmp/" + results2.getName() + ".txt", false);
		 * r1.begin();
		 * r2.begin();
		 * //OutputStreamWriter o2 = new OutputStreamWriter(new FileOutputStream("/tmp/"+results1.getName()+".txt"));
		 * //OutputStreamWriter o3 = new OutputStreamWriter(new FileOutputStream("/tmp/"+results2.getName()+".txt"));
		 * for (PointPair pair : allMatches)
		 * {
		 * PeakResult p1 = ((PeakResultPoint) pair.getPoint1()).peakResult;
		 * PeakResult p2 = ((PeakResultPoint) pair.getPoint2()).peakResult;
		 * r1.add(p1);
		 * r2.add(p2);
		 * o.write(Float.toString(p1.getSignal()));
		 * o.write('\t');
		 * o.write(Float.toString(p2.getSignal()));
		 * o.write('\n');
		 * }
		 * o.close();
		 * r1.end();
		 * r2.end();
		 * }
		 * catch (Exception e)
		 * {
		 * e.printStackTrace();
		 * }
		 */
    boolean doIdAnalysis1 = (idAnalysis) ? haveIds(results1) : false;
    boolean doIdAnalysis2 = (idAnalysis) ? haveIds(results2) : false;
    boolean doIdAnalysis = doIdAnalysis1 || doIdAnalysis2;
    // Create output
    if (!java.awt.GraphicsEnvironment.isHeadless()) {
        String header = createResultsHeader(doIdAnalysis);
        Utils.refreshHeadings(resultsWindow, header, true);
        if (showTable && (resultsWindow == null || !resultsWindow.isShowing())) {
            resultsWindow = new TextWindow(TITLE + " Results", header, "", 900, 300);
        }
        if (showPairs) {
            if (pairsWindow == null || !pairsWindow.isShowing()) {
                pairsWindow = new TextWindow(TITLE + " Pairs", createPairsHeader(pairs), "", 900, 300);
                if (resultsWindow != null) {
                    Point p = resultsWindow.getLocation();
                    p.y += resultsWindow.getHeight();
                    pairsWindow.setLocation(p);
                }
                pairPainter = new ImageROIPainter(pairsWindow.getTextPanel(), "", this);
            }
            pairsWindow.getTextPanel().clear();
            String title = "Results 1";
            if (results1.getSource() != null && results1.getSource().getOriginal().getName().length() > 0)
                title = results1.getSource().getOriginal().getName();
            pairPainter.setTitle(title);
            IJ.showStatus("Writing pairs table");
            IJ.showProgress(0);
            int c = 0;
            final int total = pairs.size();
            final int step = Utils.getProgressInterval(total);
            final ArrayList<String> list = new ArrayList<String>(total);
            boolean flush = true;
            for (PointPair pair : pairs) {
                if (++c % step == 0)
                    IJ.showProgress(c, total);
                list.add(addPairResult(pair));
                if (flush && c == 9) {
                    pairsWindow.getTextPanel().append(list);
                    list.clear();
                    flush = false;
                }
            }
            pairsWindow.getTextPanel().append(list);
            IJ.showProgress(1);
        }
    } else {
        if (writeHeader && showTable) {
            writeHeader = false;
            IJ.log(createResultsHeader(idAnalysis));
        }
    }
    if (!showTable)
        return;
    // We have the results for the largest distance.
    // Now reduce the distance threshold and recalculate the results
    double[] distanceThresholds = getDistances(dThreshold, increments, delta);
    double[] pairDistances = getPairDistances(allMatches);
    // Re-use storage for the ID analysis
    TIntHashSet id1 = null, id2 = null, matchId1 = null, matchId2 = null;
    if (doIdAnalysis) {
        if (doIdAnalysis1) {
            id1 = getIds(results1);
            matchId1 = new TIntHashSet(id1.size());
        }
        if (doIdAnalysis2) {
            id2 = getIds(results2);
            matchId2 = new TIntHashSet(id2.size());
        }
    }
    for (double distanceThreshold : distanceThresholds) {
        double rms = 0;
        int tp2 = 0;
        final double d2 = distanceThreshold * distanceThreshold;
        for (double d : pairDistances) {
            if (d <= d2) {
                rms += d;
                tp2++;
            }
        }
        // All non-true positives must be added to the false totals.
        int fp2 = n2 - tp2;
        int fn2 = n1 - tp2;
        MatchResult result = new MatchResult(tp2, fp2, fn2, (tp2 > 0) ? Math.sqrt(rms / tp2) : 0);
        MatchResult idResult1 = null, idResult2 = null;
        if (doIdAnalysis) {
            if (doIdAnalysis1)
                matchId1.clear();
            if (doIdAnalysis2)
                matchId2.clear();
            int i = 0;
            for (PointPair pair : allMatches) {
                if (pairDistances[i++] <= d2) {
                    if (doIdAnalysis1)
                        matchId1.add(((PeakResultPoint) pair.getPoint1()).peakResult.getId());
                    if (doIdAnalysis2)
                        matchId2.add(((PeakResultPoint) pair.getPoint2()).peakResult.getId());
                }
            }
            // => Only the recall will be valid: tp / (tp + fn)
            if (doIdAnalysis1)
                idResult1 = new MatchResult(matchId1.size(), 0, id1.size() - matchId1.size(), 0);
            if (doIdAnalysis2)
                idResult2 = new MatchResult(matchId2.size(), 0, id2.size() - matchId2.size(), 0);
        }
        addResult(inputOption1, inputOption2, distanceThreshold, result, idResult1, idResult2);
    }
}
Also used : ArrayList(java.util.ArrayList) MatchResult(gdsc.core.match.MatchResult) PeakResult(gdsc.smlm.results.PeakResult) TIntHashSet(gnu.trove.set.hash.TIntHashSet) PointPair(gdsc.core.match.PointPair) ImageROIPainter(gdsc.smlm.ij.utils.ImageROIPainter) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint) LinkedList(java.util.LinkedList) Point(java.awt.Point) BasePoint(gdsc.core.match.BasePoint) FilePeakResults(gdsc.smlm.results.FilePeakResults) TextWindow(ij.text.TextWindow) Coordinate(gdsc.core.match.Coordinate)

Example 7 with FilePeakResults

use of gdsc.smlm.results.FilePeakResults in project GDSC-SMLM by aherbert.

the class TraceMolecules method saveTraces.

/**
	 * Save the traces to the file. A File open dialog is presented and the selected filename returned.
	 * <p>
	 * If the id is above zero then the file open dialog title will have the id appended and the filename is searched
	 * for .[0-9]+. and it is replaced with .id.
	 * 
	 * @param sourceResults
	 * @param traces
	 * @param comment
	 * @param filename
	 *            The initial filename
	 * @param id
	 *            The traces id (used if above 0)
	 * @return The select filename (or null)
	 */
static String saveTraces(MemoryPeakResults sourceResults, Trace[] traces, String comment, String filename, int id) {
    IJ.showStatus("Saving traces");
    String title = "Traces_File";
    if (id > 0) {
        title += id;
        String regex = "\\.[0-9]+\\.";
        if (filename.matches(regex))
            filename.replaceAll(regex, "." + (id) + ".");
        else
            Utils.replaceExtension(filename, id + ".xls");
    }
    filename = Utils.getFilename(title, filename);
    if (filename != null) {
        filename = Utils.replaceExtension(filename, "xls");
        boolean showDeviations = (traces.length > 0 && traces[0].getHead().paramsStdDev != null);
        // Assume that are results are from a single frame but store the trace ID
        FilePeakResults traceResults = new FilePeakResults(filename, showDeviations, false, true);
        traceResults.copySettings(sourceResults);
        traceResults.begin();
        if (!traceResults.isActive()) {
            IJ.error("Failed to write to file: " + filename);
        } else {
            traceResults.addComment(comment);
            for (Trace trace : traces) traceResults.addTrace(trace);
            traceResults.end();
        }
    }
    IJ.showStatus("");
    return filename;
}
Also used : Trace(gdsc.smlm.results.Trace) FilePeakResults(gdsc.smlm.results.FilePeakResults)

Aggregations

FilePeakResults (gdsc.smlm.results.FilePeakResults)7 OpenDialog (ij.io.OpenDialog)3 IJImagePeakResults (gdsc.smlm.ij.results.IJImagePeakResults)2 IJTablePeakResults (gdsc.smlm.ij.results.IJTablePeakResults)2 BinaryFilePeakResults (gdsc.smlm.results.BinaryFilePeakResults)2 MALKFilePeakResults (gdsc.smlm.results.MALKFilePeakResults)2 MemoryPeakResults (gdsc.smlm.results.MemoryPeakResults)2 PeakResults (gdsc.smlm.results.PeakResults)2 TSFPeakResultsWriter (gdsc.smlm.results.TSFPeakResultsWriter)2 Trace (gdsc.smlm.results.Trace)2 File (java.io.File)2 BasePoint (gdsc.core.match.BasePoint)1 Coordinate (gdsc.core.match.Coordinate)1 MatchResult (gdsc.core.match.MatchResult)1 PointPair (gdsc.core.match.PointPair)1 ImageROIPainter (gdsc.smlm.ij.utils.ImageROIPainter)1 PeakResult (gdsc.smlm.results.PeakResult)1 TIntHashSet (gnu.trove.set.hash.TIntHashSet)1 YesNoCancelDialog (ij.gui.YesNoCancelDialog)1 TextWindow (ij.text.TextWindow)1