Example #1
0
 def __init__(self, spline_file):
     spline_data = self.loadSplineFile(spline_file)
     self.zmin = spline_data["zmin"]
     self.zmax = spline_data["zmax"]
     self.spline = spline3D.Spline3D(spline_data["spline"],
                                     spline_data["coeff"])
     self.spline_size = self.spline.getSize()
Example #2
0
    def __init__(self, spline_vals, coeff_vals, scmos_data, verbose=False):
        CSplineFit.__init__(self, scmos_data, verbose)

        # Initialize spline.
        self.py_spline = spline3D.Spline3D(spline_vals, coeff=coeff_vals)
        self.c_spline = self.clib.initSpline3D(
            numpy.ascontiguousarray(self.py_spline.coeff, dtype=numpy.float64),
            self.py_spline.max_i, self.py_spline.max_i, self.py_spline.max_i)
        self.inv_zscale = 1.0 / self.clib.getZSize(self.c_spline)
Example #3
0
    def __init__(self, splines, coeffs, independent_heights, verbose=False):
        super(MPSplineFit, self).__init__(verbose)

        self.clib = loadMPFitC()
        self.c_splines = []
        self.independent_heights = independent_heights
        self.n_channels = 0
        self.py_splines = []

        # Default clamp parameters.
        #
        # These set the (initial) scale for how much these parameters
        # can change in a single fitting iteration.
        #
        self.clamp = numpy.array([
            100.0,  # Height
            1.0,  # x position
            0.3,  # width in x
            1.0,  # y position
            0.3,  # width in y
            5.0,  # background
            1.0
        ])  # z position

        # Initialize splines.
        for i in range(len(splines)):
            self.py_splines.append(
                spline3D.Spline3D(splines[i], coeff=coeffs[i]))
            self.c_splines.append(
                self.clib.initSpline3D(
                    numpy.ascontiguousarray(self.py_splines[i].coeff,
                                            dtype=numpy.float64),
                    self.py_splines[i].max_i, self.py_splines[i].max_i,
                    self.py_splines[i].max_i))
            self.n_channels += 1
        self.inv_zscale = 1.0 / self.clib.getZSize(self.c_splines[0])

        #
        # Initialize weights. These are used to weight the per channel parameter
        # update values based on the localizations z value. The idea is that
        # at any particular z value some channels will contribute more information
        # than others to the value of each parameter.
        #
        # The z scale of these is not particularly fine, it just matches the number
        # of z values in the spline.
        #
        # The C library expects these arrays to be indexed by z value, then channel.
        #
        self.w_bg = numpy.ones((self.clib.getZSize(
            self.c_splines[0]), self.n_channels)) / float(self.n_channels)
        self.w_h = numpy.ones(self.w_bg.shape) / float(self.n_channels)
        self.w_x = numpy.ones(self.w_bg.shape) / float(self.n_channels)
        self.w_y = numpy.ones(self.w_bg.shape) / float(self.n_channels)
        self.w_z = numpy.ones(self.w_bg.shape) / float(self.n_channels)
Example #4
0
    def __init__(self,
                 spline_vals,
                 coeff_vals,
                 scmos_data,
                 tolerance=default_tol):
        CSplineFit.__init__(self, spline_vals, scmos_data, tolerance)

        # Initialize spline.
        self.py_spline = spline3D.Spline3D(spline_vals, coeff=coeff_vals)
        cubic_fit.initSpline3D(
            numpy.ascontiguousarray(self.py_spline.coeff, dtype=numpy.float64),
            self.py_spline.max_i, self.py_spline.max_i, self.py_spline.max_i)
Example #5
0
    def __init__(self, psf_filename=None, **kwds):
        kwds["psf_filename"] = psf_filename
        super(CRSplineToPSF3D, self).__init__(**kwds)

        # Load the spline.
        with open(psf_filename, 'rb') as fp:
            spline_data = pickle.load(fp)

        self.zmax = spline_data["zmax"]
        self.zmin = spline_data["zmin"]
        self.spline = spline3D.Spline3D(spline_data["spline"],
                                        spline_data["coeff"])

        self.delta_xy = self.pixel_size
        self.delta_z = (self.getZMax() - self.getZMin()) / float(
            self.spline.getSize())
    def __init__(self, spline_file = None, **kwds):
        super(SplineToPSF3D, self).__init__(**kwds)

        spline_data = self.loadSplineFile(spline_file)

        # Check that this is not an old spline, which will be transposed.
        assert("version" in spline_data), "v0 spline file detected! Please re-measure!"
        assert(spline_data["version"] >= 2.0), "v0/v1 spline file detected! Please re-measure!"
        
        self.zmin = spline_data["zmin"]
        self.zmax = spline_data["zmax"]

        # The Python representation of the spline.        
        self.spline = spline3D.Spline3D(spline_data["spline"], spline_data["coeff"])
        self.spline_size = self.spline.getSize()

        # The C representation of the spline. This class does not use
        # this, but it keeps track of it for the C fitting library.
        self.c_spline = cubicSplineC.CSpline3D(self.spline)
Example #7
0
def test_psf_3D_f():

    # Only test for Python3 due to pickle incompatibility issues.
    if (sys.version_info < (3, 0)):
        return

    spline_filename = storm_analysis.getData(
        "test/data/test_spliner_psf.spline")
    with open(spline_filename, "rb") as fp:
        spline_data = pickle.load(fp)

    py_spline = spline3D.Spline3D(spline_data["spline"], spline_data["coeff"])
    c_spline = cubicSplineC.CSpline3D(py_spline)

    size = py_spline.getSize() - 1.0e-6

    for i in range(reps):
        x = random.uniform(1.0e-6, size)
        y = random.uniform(1.0e-6, size)
        z = random.uniform(1.0e-6, size)
        #print("{0:.3f} {1:.3f}".format(py_spline.f(x, y), c_spline.f(x, y)))
        assert (abs(py_spline.f(x, y, z) - c_spline.f(x, y, z)) < 1.0e-6)
Example #8
0
            for k in range(np_psf.shape[0]):
                zvals[k] = xy_splines[k].f(y, x)
            z_spline = spline1D.Spline1D(zvals)

            max_z = float(np_psf.shape[0]) - 1.0
            inc = max_z / (float(s_size) - 1.0)
            for k in range(s_size):
                z = float(k) * inc
                if (z > max_z):
                    z = max_z
                np_spline[k, j, i] = z_spline.f(z)

            y += 1.0
        x += 1.0

    print("Calculating spline coefficients.")
    spline = spline3D.Spline3D(np_spline)

    if 1:
        import storm_analysis.sa_library.daxwriter as daxwriter
        dxw = daxwriter.DaxWriter("spline.dax", np_spline.shape[1],
                                  np_spline.shape[2])
        for i in range(s_size):
            dxw.addFrame(1000.0 * np_spline[i, :, :] + 100)
        dxw.close()

del psf_data["psf"]
psf_data["spline"] = np_spline
psf_data["coeff"] = spline.getCoeff()
pickle.dump(psf_data, open(sys.argv[2], 'wb'))
def psfToSpline(psf_name, spline_name, s_size):
    
    psf_data = pickle.load(open(psf_name, 'rb'))
    np_psf = psf_data["psf"]
    spline = False
    start = np_psf.shape[1]/2.0 - s_size - 0.5


    # 2D spline
    if (len(np_psf.shape) == 2):
        print("Generating 2D spline.")
        s_size = 2*s_size

        np_spline = numpy.zeros((s_size, s_size))
        #np_psf = np_psf/numpy.max(np_psf)
        xy_spline = spline2D.Spline2D(np_psf)
        
        x = start
        for i in range(s_size):
            y = start
            for j in range(s_size):
                np_spline[j,i] = xy_spline.f(y,x)
            
                y += 1.0
            x += 1.0

        print("Calculating spline coefficients.")
        spline = spline2D.Spline2D(np_spline)

        if True:
            import storm_analysis.sa_library.daxwriter as daxwriter
            daxwriter.singleFrameDax(os.path.join(os.path.dirname(spline_name), "spline.dax"), 1000.0*np_spline + 100)


    # 3D spline
    else:
        print("Generating 3D spline.")
        s_size = 2*s_size

        np_spline = numpy.zeros((s_size, s_size, s_size))
        xy_splines = []

        print("Generating XY splines.")
        for i in range(np_psf.shape[0]):
            xy_splines.append(spline2D.Spline2D(np_psf[i,:,:]))

        print("Generating fitting spline.")
        x = start
        for i in range(s_size):
            y = start
            for j in range(s_size):

                zvals = numpy.zeros(np_psf.shape[0])
                for k in range(np_psf.shape[0]):
                    zvals[k] = xy_splines[k].f(y,x)
                    z_spline = spline1D.Spline1D(zvals)

                max_z = float(np_psf.shape[0]) - 1.0
                inc = max_z/(float(s_size)-1.0)
                for k in range(s_size):
                    z = float(k)*inc
                    if (z > max_z):
                        z = max_z
                    np_spline[k,j,i] = z_spline.f(z)

                y += 1.0
            x += 1.0

        print("Calculating spline coefficients.")
        spline = spline3D.Spline3D(np_spline)

        if True:
            import storm_analysis.sa_library.daxwriter as daxwriter
            dxw = daxwriter.DaxWriter(os.path.join(os.path.dirname(spline_name), "spline.dax"),
                                      np_spline.shape[1],
                                      np_spline.shape[2])
            for i in range(s_size):
                dxw.addFrame(1000.0*np_spline[i,:,:] + 100)
            dxw.close()

    del psf_data["psf"]
    psf_data["spline"] = np_spline
    psf_data["coeff"] = spline.getCoeff()
    pickle.dump(psf_data, open(spline_name, 'wb'))
Example #10
0
def psfToSpline(psf_name, spline_name, s_size):

    # Load PSF.
    with open(psf_name, 'rb') as fp:
        psf_data = pickle.load(fp)
    np_psf = psf_data["psf"]

    spline = False
    start = np_psf.shape[1] / 2.0 - s_size - 0.5

    # 2D spline
    if (len(np_psf.shape) == 2):
        print("Generating 2D spline.")
        s_size = 2 * s_size

        np_spline = numpy.zeros((s_size, s_size))
        #np_psf = np_psf/numpy.max(np_psf)
        xy_spline = spline2D.Spline2D(np_psf)

        x = start
        for i in range(s_size):
            y = start
            for j in range(s_size):
                np_spline[j, i] = xy_spline.f(y, x)

                y += 1.0
            x += 1.0

        print("Calculating spline coefficients.")
        spline = spline2D.Spline2D(np_spline)

        if True:
            import tifffile
            np_spline = np_spline.astype(numpy.float32)
            tiff_name = os.path.splitext(spline_name)[0] + "_sp.tif"
            tifffile.imsave(tiff_name, np_spline)

    # 3D spline
    else:
        print("Generating 3D spline.")
        s_size = 2 * s_size

        np_spline = numpy.zeros((s_size, s_size, s_size))
        xy_splines = []

        print("Generating XY splines.")
        for i in range(np_psf.shape[0]):
            xy_splines.append(spline2D.Spline2D(np_psf[i, :, :]))

        print("Generating fitting spline.")
        x = start
        for i in range(s_size):
            y = start
            for j in range(s_size):

                zvals = numpy.zeros(np_psf.shape[0])
                for k in range(np_psf.shape[0]):
                    zvals[k] = xy_splines[k].f(y, x)
                    z_spline = spline1D.Spline1D(zvals)

                max_z = float(np_psf.shape[0]) - 1.0
                inc = max_z / (float(s_size) - 1.0)
                for k in range(s_size):
                    z = float(k) * inc
                    if (z > max_z):
                        z = max_z
                    np_spline[k, j, i] = z_spline.f(z)

                y += 1.0
            x += 1.0

        print("Calculating spline coefficients.")
        spline = spline3D.Spline3D(np_spline, verbose=True)

        if True:
            import tifffile
            np_spline = np_spline.astype(numpy.float32)
            tiff_name = os.path.splitext(spline_name)[0] + "_sp.tif"
            with tifffile.TiffWriter(tiff_name) as tf:
                for i in range(s_size):
                    tf.save(np_spline[i, :, :])

    del psf_data["psf"]
    psf_data["spline"] = np_spline
    psf_data["coeff"] = spline.getCoeff()
    psf_data["psf_name"] = psf_name
    with open(spline_name, 'wb') as fp:
        pickle.dump(psf_data, fp)