示例#1
0
    def __init__(self, spline_vals, coeff_vals, scmos_data, verbose=False):
        CSplineFit.__init__(self, scmos_data, verbose)

        # Initialize spline.
        self.py_spline = spline2D.Spline2D(spline_vals, coeff=coeff_vals)
        self.c_spline = self.clib.initSpline2D(
            numpy.ascontiguousarray(self.py_spline.coeff, dtype=numpy.float64),
            self.py_spline.max_i, self.py_spline.max_i)
示例#2
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 = spline2D.Spline2D(spline_vals, coeff=coeff_vals)
        cubic_fit.initSpline2D(
            numpy.ascontiguousarray(self.py_spline.coeff, dtype=numpy.float64),
            self.py_spline.max_i, self.py_spline.max_i)
示例#3
0
def test_psf_2D_dy():

    # 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_2d.spline")
    with open(spline_filename, "rb") as fp:
        spline_data = pickle.load(fp)

    py_spline = spline2D.Spline2D(spline_data["spline"], spline_data["coeff"])
    c_spline = cubicSplineC.CSpline2D(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)
        #print("{0:.3f} {1:.3f}".format(py_spline.dyf(x, y), c_spline.dyf(x, y)))
        assert (abs(py_spline.dyf(x, y) - c_spline.dyf(x, y)) < 1.0e-6)
    def __init__(self, spline_file = None, **kwds):
        super(SplineToPSF2D, 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!"
        
        # These are used when we check the starting z-value(s)
        # provided by the user, if any.
        self.zmin = -1.0
        self.zmax = 1.0

        # The Python representation of the spline.
        self.spline = spline2D.Spline2D(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.CSpline2D(self.spline)
示例#5
0
    exit()

psf_data = pickle.load(open(sys.argv[1], 'rb'))
np_psf = psf_data["psf"]
s_size = int(sys.argv[3])
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 1:
        import sa_library.daxwriter as daxwriter
示例#6
0
    def __init__(self, d, coeff=False, verbose=False):

        if (d.shape[0] != d.shape[1]) or (d.shape[0] != d.shape[2]):
            assert "input matrix must be square!"

        size = d.shape[0]
        self.max_i = size - 1

        #
        # The coefficients have already been calculated, so just use them.
        #
        if (type(coeff) == type(numpy.array([]))):
            self.coeff = coeff
            return

        if verbose:
            print("Calculating spline coefficients.")

        #
        # Create 2D splines in the "yz-plane".
        #
        yzs = []
        for i in range(size):
            yzs.append(spline2D.Spline2D(d[i, :, :]))

        #
        # Use splines in the "yz-plane" to create splines on the
        # "x axis" with sub-integer spacing.
        #
        xs = []
        cx = 0.0
        cy = 0.0
        while (cx <= (float(self.max_i) + 0.01)):
            if (cx > float(self.max_i)):
                cx = float(self.max_i)
            cy = 0.0
            while (cy <= (float(self.max_i) + 0.01)):
                if (cy > float(self.max_i)):
                    cy = float(self.max_i)
                xv = numpy.zeros(size)
                for i in range(size):
                    xv[i] = yzs[i].f(cy, cx)
                xs.append(spline1D.Spline1D(xv))
                cy += 1.0 / 3.0
            cx += 1.0 / 3.0

        #
        # Compute spline coefficients using the "x axis" splines
        # to generate 64 values per cell and then solving for
        # the coefficients.
        #
        self.coeff = numpy.zeros((self.max_i, self.max_i, self.max_i, 64))

        A = numpy.zeros((64, 64))
        for i in range(4):
            dx = float(i) / 3.0
            for j in range(4):
                dy = float(j) / 3.0
                for k in range(4):
                    dz = float(k) / 3.0
                    for l in range(4):
                        for m in range(4):
                            for n in range(4):
                                A[i * 16 + j * 4 + k, l * 16 +
                                  m * 4 + n] = math.pow(dx, l) * math.pow(
                                      dy, m) * math.pow(dz, n)

        b = numpy.zeros(64)
        row_size = 3 * self.max_i + 1
        for i in range(self.max_i):
            for j in range(self.max_i):
                for k in range(self.max_i):
                    for m in range(4):
                        for n in range(4):
                            sp = xs[i * 3 * row_size + j * 3 + m * row_size +
                                    n]
                            for o in range(4):
                                cx = float(k) + float(o) / 3.0
                                b[m * 16 + n * 4 + o] = sp.f(cx)
                    self.coeff[i, j, k, :] = numpy.linalg.solve(A, b)

        if verbose:
            print("Finished calculating spline coefficients.")
示例#7
0
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'))
示例#8
0
 def __init__(self, spline_file):
     spline_data = self.loadSplineFile(spline_file)
     self.spline = spline2D.Spline2D(spline_data["spline"],
                                     spline_data["coeff"])
     self.spline_size = self.spline.getSize()
示例#9
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)