Esempio n. 1
0
def test_mfit_9():
    """
    Test peak significance calculation.
    """
    height = 10.0
    sigma = 1.5
    x_size = 100
    y_size = 120
    background = numpy.zeros((x_size, y_size)) + 10.0
    image = dg.drawGaussians((x_size, y_size),
                             numpy.array([[50.0, 30.0, height, sigma, sigma],
                                          [50.0, 50.0, 2.0*height, sigma, sigma],
                                          [50.0, 70.0, 3.0*height, sigma, sigma]]))
    image += background

    mfit = daoFitC.MultiFitter2D(sigma_range = [1.0, 2.0])
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(background)

    peaks = {"x" : numpy.array([30.0, 50.0, 70.0]),
             "y" : numpy.array([50.0, 50.0, 50.0]),
             "z" : numpy.array([0.0, 0.0, 0.0]),
             "sigma" : numpy.array([sigma, sigma, sigma])}

    mfit.newPeaks(peaks, "finder")

    if False:
        with tifffile.TiffWriter("test_mfit_9.tif") as tf:
            tf.save(image.astype(numpy.float32))
    
    sig = mfit.getPeakProperty("significance")
    sig = sig/sig[0]
    assert(numpy.allclose(sig, numpy.arange(1,4)))
Esempio n. 2
0
def test_mfit_10():
    """
    Test 'pre-specified' peak locations addition (hdf5).
    """
    image = numpy.ones((40,40))
    
    mfit = daoFitC.MultiFitter2D(sigma_range = [1.0, 2.0])
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Add peaks.
    peaks = {"background" : numpy.array([10.0, 20.0]),
             "height" : numpy.array([11.0, 21.0]),
             "x" : numpy.array([12.0, 22.0]),
             "xsigma" : numpy.array([1.0, 2.0]),
             "y" : numpy.array([14.0, 24.0]),
             "ysigma" : numpy.array([3.0, 4.0]),
             "z" : numpy.array([16.0, 26.0])}
    mfit.newPeaks(peaks, "hdf5")

    # Round trip verification.
    for pname in peaks:
        pvals = peaks[pname]
        mvals = mfit.getPeakProperty(pname)
        for i in range(pvals.size):
            assert(abs(pvals[i] - mvals[i]) < 1.0e-6)

    mfit.cleanup(verbose = False)
Esempio n. 3
0
def test_mfit_3():
    """
    Test error peak removal.
    """
    image = numpy.ones((40, 40))

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Add good peaks.
    n_peaks = 2
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks)
    }
    mfit.newPeaks(peaks, "testing")

    # Check that no peaks are removed.
    mfit.removeErrorPeaks()
    assert (mfit.getNFit() == 2)

    # Add error peaks.
    n_peaks = 2
    peaks = {
        "x": numpy.zeros(n_peaks),
        "y": numpy.zeros(n_peaks),
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks) * 2.0
    }
    mfit.newPeaks(peaks, "testing")
    assert (mfit.getNFit() == 4)

    # Add more good peaks.
    n_peaks = 2
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks)
    }
    mfit.newPeaks(peaks, "testing")
    assert (mfit.getNFit() == 6)

    # Check that two peaks are removed.
    mfit.removeErrorPeaks()
    assert (mfit.getNFit() == 4)

    # Check that the right peaks were removed.
    w = mfit.getPeakProperty("xsigma")
    for i in range(w.size):
        assert (abs(w[i] - 1.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 4
0
def test_mfit_12():
    """
    Test RQE correction is as expected.
    """
    height = 10.0
    rqe_term = 0.7
    sigma = 1.5
    x_size = 60
    y_size = 120

    # RQE corrected image.
    background = numpy.zeros((x_size, y_size)) + 10.0
    image = dg.drawGaussians((x_size, y_size),
                             numpy.array([[30.0, 30.0, height, sigma, sigma],
                                          [30.0, 60.0, height, sigma, sigma],
                                          [30.0, 90.0, height, sigma, sigma]]))
    image += background

    # Variable RQE.
    rqe = numpy.ones_like(image)
    rqe[:,:60] = rqe_term

    mfit = daoFitC.MultiFitter2D(rqe = rqe,
                                 sigma_range = [1.0, 2.0])
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(background)

    peaks = {"x" : numpy.array([30.0, 60.0, 90.0]),
             "y" : numpy.array([30.0, 30.0, 30.0]),
             "z" : numpy.array([0.0, 0.0, 0.0]),
             "sigma" : numpy.array([sigma, sigma, sigma])}

    mfit.newPeaks(peaks, "finder")

    # All localizations should have the same RQE.
    sig = mfit.getPeakProperty("significance")
    sig = sig/sig[0]
    assert(numpy.allclose(sig, numpy.ones_like(sig)))

    # Fit image should be RQE corrected.
    fit_image = mfit.getFitImage() + background

    assert(numpy.allclose(image, fit_image, atol = 0.2))
    
    if False:
        with tifffile.TiffWriter("test_mfit_12.tif") as tf:
            tf.save(image.astype(numpy.float32))
            tf.save(fit_image.astype(numpy.float32))

    mfit.cleanup(verbose = False)
Esempio n. 5
0
def test_mfit_5():
    """
    Test initialization and fitting.
    """
    height = 20.0
    sigma = 1.5
    x_size = 100
    y_size = 120
    background = numpy.zeros((x_size, y_size)) + 10.0
    image = dg.drawGaussians((x_size, y_size),
                             numpy.array([[50.0, 50.0, height, sigma, sigma],
                                          [50.0, 54.0, height, sigma, sigma]]))
    image += background

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(background)

    peaks = {
        "x": numpy.array([50.0, 54.0]),
        "y": numpy.array([50.0, 50.0]),
        "z": numpy.array([0.0, 0.0]),
        "sigma": numpy.array([sigma, sigma])
    }

    mfit.newPeaks(peaks, "finder")
    mfit.doFit()

    if False:
        with tifffile.TiffWriter("test_mfit_5.tif") as tf:
            tf.save((image - background).astype(numpy.float32))
            tf.save(mfit.getFitImage().astype(numpy.float32))

    # Check peak x,y.
    x = mfit.getPeakProperty("x")
    y = mfit.getPeakProperty("y")
    for i in range(x.size):
        assert (abs(x[i] - peaks["x"][i]) < 1.0e-2)
        assert (abs(y[i] - peaks["y"][i]) < 1.0e-2)

    # Check peak w.
    w = mfit.getPeakProperty("xsigma")
    for i in range(w.size):
        assert (abs((w[i] - sigma) / sigma) < 0.02)

    mfit.cleanup(verbose=False)
def test_ia_util_3():
    """
    Test agreement with fitting regarding orientation.
    """
    height = 20.0
    sigma = 1.5
    x_size = 100
    y_size = 120
    background = numpy.zeros((x_size, y_size)) + 10.0
    image = dg.drawGaussians((x_size, y_size),
                             numpy.array([[20.0, 40.0, height, sigma, sigma]]))
    image += background

    # Configure fitter.
    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(background)

    # Configure finder.
    z_values = [0.0]
    mxf = iaUtilsC.MaximaFinder(margin=1,
                                radius=2 * sigma,
                                threshold=background[0, 0] + 0.5 * height,
                                z_values=z_values)

    # Find peaks.
    [x, y, z] = mxf.findMaxima([image])

    sigma = numpy.ones(x.size) * sigma
    peaks = {"x": x, "y": y, "z": z, "sigma": sigma}

    # Pass peaks to fitter.
    mfit.newPeaks(peaks, "finder")

    # Check height.
    h = mfit.getPeakProperty("height")
    for i in range(h.size):
        assert (abs(h[i] - height) / height < 0.1)

    # Check background.
    bg = mfit.getPeakProperty("background")
    for i in range(bg.size):
        assert (abs(bg[i] - 10.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 7
0
def test_mfit_7():
    """
    Test removing RUNNING peaks.
    """
    image = numpy.ones((40, 40))

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Add good peaks.
    n_peaks = 2
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks) * 1.0
    }
    peaks["sigma"][1] = 2.0
    mfit.newPeaks(peaks, "testing")

    # Mark peaks as converged.
    status = mfit.getPeakProperty("status")
    status[:] = iaUtilsC.CONVERGED
    mfit.setPeakStatus(status)

    # Check that no peaks are removed.
    mfit.removeRunningPeaks()
    assert (mfit.getNFit() == 2)

    # Mark the first peak as running.
    status = mfit.getPeakProperty("status")
    status[0] = iaUtilsC.RUNNING
    mfit.setPeakStatus(status)

    # Check that one peak was removed.
    mfit.removeRunningPeaks()
    assert (mfit.getNFit() == 1)

    # Check that the right peak was removed.
    w = mfit.getPeakProperty("xsigma")
    for i in range(w.size):
        assert (abs(w[i] - 2.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 8
0
def test_mfit_4():
    """
    Test height and background initialization.
    """
    height = 20.0
    sigma = 1.5
    x_size = 100
    y_size = 120
    background = numpy.zeros((x_size, y_size)) + 10.0
    image = dg.drawGaussians((x_size, y_size),
                             numpy.array([[50.0, 50.0, height, sigma, sigma],
                                          [50.0, 54.0, height, sigma, sigma]]))
    image += background

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(background)

    peaks = {
        "x": numpy.array([50.0, 54.0]),
        "y": numpy.array([50.0, 50.0]),
        "z": numpy.array([0.0, 0.0]),
        "sigma": numpy.array([sigma, sigma])
    }

    mfit.newPeaks(peaks, "finder")

    if False:
        with tifffile.TiffWriter("test_mfit_4.tif") as tf:
            tf.save((image - background).astype(numpy.float32))
            tf.save(mfit.getFitImage().astype(numpy.float32))

    # Check height.
    h = mfit.getPeakProperty("height")
    for i in range(h.size):
        assert (abs(h[i] - height) / height < 0.1)

    # Check background.
    bg = mfit.getPeakProperty("background")
    for i in range(bg.size):
        assert (abs(bg[i] - 10.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 9
0
def test_mfit_2():
    """
    Test initialization & growing with error peak removal.
    """
    image = numpy.ones((40, 40))

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Create ERROR peaks (too close to edge).
    n_peaks = 5
    peaks = {
        "x": numpy.zeros(n_peaks),
        "y": numpy.zeros(n_peaks),
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks)
    }

    # Add peaks & check size.
    mfit.newPeaks(peaks, "testing")
    assert (mfit.getNFit() == 5)
    assert (mfit.getNFitMax() == 500)

    # Add good peaks. ERROR peaks should be removed.
    n_peaks = 500
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks) * 2.0
    }
    mfit.newPeaks(peaks, "testing")

    assert (mfit.getNFit() == n_peaks)
    assert (mfit.getNFitMax() == 1000)

    # Check for correct peak values.
    w = mfit.getPeakProperty("xsigma")
    assert (abs(w[0] - 2.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 10
0
def test_mfit_1():
    """
    Test initialization & growing peak array size.
    """
    image = numpy.ones((40, 40))

    mfit = daoFitC.MultiFitter2D()
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Check correct initialization.
    assert (mfit.getNFit() == 0)
    assert (mfit.getNFitMax() == 0)

    # Create peaks in the center of the image.
    n_peaks = 1020
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": numpy.ones(n_peaks)
    }

    # Add peaks & check size.
    mfit.newPeaks(peaks, "testing")
    assert (mfit.getNFit() == n_peaks)
    assert (mfit.getNFitMax() == 1500)

    # Add again & check size.
    peaks["sigma"][:] = 2.0
    mfit.newPeaks(peaks, "testing")
    assert (mfit.getNFit() == 2 * n_peaks)
    assert (mfit.getNFitMax() == 2500)

    # Check some peak values.
    w = mfit.getPeakProperty("xsigma")
    assert (abs(w[0] - 1.0) < 1.0e-6)
    assert (abs(w[n_peaks] - 2.0) < 1.0e-6)

    mfit.cleanup(verbose=False)
Esempio n. 11
0
def test_mfit_6():
    """
    Test marking peak status.
    """
    image = numpy.ones((40, 40))

    mfit = daoFitC.MultiFitter2D(sigma_range=[1.0, 2.0])
    mfit.initializeC(image)
    mfit.newImage(image)
    mfit.newBackground(image)

    # Add good peaks.
    n_peaks = 5
    peaks = {
        "x": numpy.ones(n_peaks) * 20.0,
        "y": numpy.ones(n_peaks) * 20.0,
        "z": numpy.zeros(n_peaks),
        "sigma": 0.1 * numpy.arange(n_peaks) + 1.0
    }
    mfit.newPeaks(peaks, "testing")

    # Check that no peaks are removed.
    mfit.removeErrorPeaks()
    assert (mfit.getNFit() == 5)

    # Mark every other peak as bad.
    status = mfit.getPeakProperty("status")
    status[1] = iaUtilsC.ERROR
    status[3] = iaUtilsC.ERROR
    mfit.setPeakStatus(status)

    # Check that two peaks were removed.
    mfit.removeErrorPeaks()
    assert (mfit.getNFit() == 3)

    # Check that the right peaks were removed.
    w = mfit.getPeakProperty("xsigma")
    assert numpy.allclose(w, numpy.array([1.0, 1.2, 1.4]))

    mfit.cleanup(verbose=False)
Esempio n. 12
0
def initFindAndFit(parameters):
    """
    Return the appropriate type of finder and fitter.
    """
    fmodel = parameters.getAttr("model")
    
    # Create peak finder.
    finder = fitting.PeakFinderGaussian(parameters = parameters)

    # Initialize Z fitting parameters.
    wx_params = None
    wy_params = None
    min_z = None
    max_z = None
    if (parameters.getAttr("model", "na") == "Z"):
        [wx_params, wy_params] = parameters.getWidthParams(for_mu_Zfit = True)
        [min_z, max_z] = parameters.getZRange()

    # Check for camera calibration (this function is also used by sCMOS analysis).
    rqe = None
    variance = None
    if parameters.hasAttr("camera_calibration"):
        
        # Load variance, scale by gain.
        #
        # Offset is in units of ADU.
        # Variance is in units of ADU*ADU.
        # Gain is ADU/photo-electron.
        # RQE is dimensionless, it should be around 1.0.
        #
        [offset, variance, gain, rqe] = analysisIO.loadCMOSCalibration(parameters.getAttr("camera_calibration"))
        variance = variance/(gain*gain)

        # Set variance in the peak finder, this method also pads the
        # variance to the correct size.
        variance = finder.setVariance(variance)

        # Pad relative quantum efficiency array to the correct size.
        rqe = finder.padArray(rqe)

    # Create C fitter object.
    kwds = {'roi_size' : finder.getROISize(),
            'als_fit' : (parameters.getAttr("anscombe", 0) != 0),
            'rqe' : rqe,
            'scmos_cal' : variance,
            'wx_params' : wx_params,
            'wy_params' : wy_params,
            'min_z' : min_z,
            'max_z' : max_z}
    if (fmodel == '2dfixed'):
        mfitter = daoFitC.MultiFitter2DFixed(**kwds)
    elif (fmodel == '2d'):
        sigma = parameters.getAttr("sigma")
        kwds['sigma_range'] = [0.5 * sigma, 5.0 * sigma]
        mfitter = daoFitC.MultiFitter2D(**kwds)
    elif (fmodel == '3d'):
        sigma = parameters.getAttr("sigma")
        kwds['sigma_range'] = [0.5 * sigma, 5.0 * sigma]
        mfitter = daoFitC.MultiFitter3D(**kwds)
    elif (fmodel == 'Z'):
        mfitter = daoFitC.MultiFitterZ(**kwds)
    else:
        raise Exception("Unknown fitting model " + fmodel)

    # Create peak fitter.
    fitter = fitting.PeakFitter(mfitter = mfitter,
                                parameters = parameters)

    # Specify which properties we want from the analysis.
    properties = ["background", "error", "height", "iterations", "significance", "sum", "x", "y"]
    if (fmodel == "2dfixed") or (fmodel == "2d"):
        properties.append("xsigma")
    elif (fmodel == "3d"):
        properties.append("xsigma")
        properties.append("ysigma")
    elif (fmodel == "Z"):
        properties.append("xsigma")
        properties.append("ysigma")
        properties.append("z")

    return fitting.PeakFinderFitter(peak_finder = finder,
                                    peak_fitter = fitter,
                                    properties = properties)
Esempio n. 13
0
 def __init__(self, parameters):
     SCMOSPeakFitter.__init__(self, parameters)
     self.mfitter = daoFitC.MultiFitter2D(self.scmos_cal, self.wx_params,
                                          self.wy_params, self.min_z,
                                          self.max_z)