Search in sources :

Example 1 with RollingArray

use of gdsc.core.utils.RollingArray in project GDSC-SMLM by aherbert.

the class DiffusionRateTest method msdAnalysis.

/**
	 * Tabulate the observed MSD for different jump distances
	 * 
	 * @param points
	 */
private void msdAnalysis(ArrayList<Point> points) {
    if (myMsdAnalysisSteps == 0)
        return;
    IJ.showStatus("MSD analysis ...");
    IJ.showProgress(1, myMsdAnalysisSteps);
    // This will only be fast if the list is an array
    Point[] list = points.toArray(new Point[points.size()]);
    // Compute the base MSD
    Point origin = new Point(0, 0, 0);
    double sum = origin.distance2(list[0]);
    int count = 1;
    for (int i = 1; i < list.length; i++) {
        Point last = list[i - 1];
        Point current = list[i];
        if (last.id == current.id) {
            sum += last.distance2(current);
        } else {
            sum += origin.distance2(current);
        }
        count++;
    }
    createMsdTable((sum / count) * settings.stepsPerSecond / conversionFactor);
    // Create a new set of points that have coordinates that 
    // are the rolling average over the number of aggregate steps
    RollingArray x = new RollingArray(aggregateSteps);
    RollingArray y = new RollingArray(aggregateSteps);
    int id = 0;
    int length = 0;
    for (Point p : points) {
        if (p.id != id) {
            x.reset();
            y.reset();
        }
        id = p.id;
        x.add(p.x);
        y.add(p.y);
        // Only create a point if the full aggregation size is reached
        if (x.isFull()) {
            list[length++] = new Point(id, x.getAverage(), y.getAverage());
        }
    }
    // Q - is this useful?
    final double p = myPrecision / settings.pixelPitch;
    final long seed = System.currentTimeMillis() + System.identityHashCode(this);
    RandomGenerator rand = new Well19937c(seed);
    final int totalSteps = (int) Math.ceil(settings.seconds * settings.stepsPerSecond - aggregateSteps);
    final int limit = Math.min(totalSteps, myMsdAnalysisSteps);
    final int interval = Utils.getProgressInterval(limit);
    final ArrayList<String> results = new ArrayList<String>(totalSteps);
    for (int step = 1; step <= myMsdAnalysisSteps; step++) {
        if (step % interval == 0)
            IJ.showProgress(step, limit);
        sum = 0;
        count = 0;
        for (int i = step; i < length; i++) {
            Point last = list[i - step];
            Point current = list[i];
            if (last.id == current.id) {
                if (p == 0) {
                    sum += last.distance2(current);
                    count++;
                } else {
                    // is the same if enough samples are present
                    for (int ii = 1; ii-- > 0; ) {
                        sum += last.distance2(current, p, rand);
                        count++;
                    }
                }
            }
        }
        if (count == 0)
            break;
        results.add(addResult(step, sum, count));
        // Flush to auto-space the columns
        if (step == 9) {
            msdTable.getTextPanel().append(results);
            results.clear();
        }
    }
    msdTable.getTextPanel().append(results);
    IJ.showProgress(1);
}
Also used : RollingArray(gdsc.core.utils.RollingArray) ArrayList(java.util.ArrayList) Well19937c(org.apache.commons.math3.random.Well19937c) RandomGenerator(org.apache.commons.math3.random.RandomGenerator)

Aggregations

RollingArray (gdsc.core.utils.RollingArray)1 ArrayList (java.util.ArrayList)1 RandomGenerator (org.apache.commons.math3.random.RandomGenerator)1 Well19937c (org.apache.commons.math3.random.Well19937c)1