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)
Exemple #2
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.")
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'))
Exemple #4
0
    def __init__(self, d, coeff=False, verbose=False):

        if (d.shape[0] != d.shape[1]):
            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 splines along y axis.
        #
        ys = []
        for i in range(size):
            ys.append(spline1D.Spline1D(d[i, :]))

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

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

        A = numpy.zeros((16, 16))
        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):
                    for l in range(4):
                        # This is the indicing that is necessary to get d(out) to equal d(in) when printed.
                        A[i * 4 + j,
                          k * 4 + l] = math.pow(dx, k) * math.pow(dy, l)

        b = numpy.zeros(16)
        for i in range(self.max_i):
            for j in range(self.max_i):
                for k in range(4):
                    sp = xs[3 * i + k]
                    for l in range(4):
                        cy = float(j) + float(l) / 3.0
                        b[k * 4 + l] = sp.f(cy)
                self.coeff[i, j, :] = numpy.linalg.solve(A, b)

        if verbose:
            print("Finished calculating spline coefficients.")
Exemple #5
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)