Skip to content
Merged
91 changes: 0 additions & 91 deletions tests/test_FFBbasis2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from scipy.special import jv

from aspire.basis import Coef, FFBBasis2D
from aspire.image import Image
from aspire.source import Simulation
from aspire.utils.misc import grid_2d
from aspire.volume import Volume
Expand Down Expand Up @@ -81,96 +80,6 @@ def testElements(self, basis):
for ell, k, sgn in zip(ells, ks, sgns):
self._testElement(basis, ell, k, sgn)

def testRotate(self, basis):
# Now low res (8x8) had problems;
# better with odd (7x7), but still not good.
# We'll use a higher res test image.
# fh = np.load(os.path.join(DATA_DIR, 'ffbbasis2d_xcoef_in_8_8.npy'))[:7,:7]
# Use a real data volume to generate a clean test image.
v = Volume(
np.load(os.path.join(DATA_DIR, "clean70SRibosome_vol.npy")).astype(
basis.dtype
)
)
src = Simulation(L=v.resolution, n=1, vols=v, dtype=v.dtype)
# Extract, this is the original image to transform.
x1 = src.images[0]

# Rotate 90 degrees CCW in cartesian coordinates.
x2 = Image(np.rot90(x1.asnumpy(), axes=(1, 2)))

# Express in an FB basis
basis = FFBBasis2D(x1.resolution, dtype=x1.dtype)
v1 = basis.evaluate_t(x1)
v2 = basis.evaluate_t(x2)

# Reflect in the FB basis space
v4 = basis.rotate(v1, 0, refl=[True])

# Rotate in the FB basis space
v3 = basis.rotate(v1, 2 * np.pi)
v1 = basis.rotate(v1, np.pi / 2)

# Evaluate back into cartesian
y1 = basis.evaluate(v1).asnumpy()
y2 = basis.evaluate(v2).asnumpy()
y3 = basis.evaluate(v3).asnumpy()
y4 = basis.evaluate(v4).asnumpy()

# Rotate 90
assert np.allclose(y1[0], y2[0], atol=1e-5)

# 2*pi Identity
assert np.allclose(x1[0], y3[0], atol=1e-5)

# Refl (flipped using flipud)
assert np.allclose(np.flipud(x1.asnumpy()[0]), y4[0], atol=1e-5)

def testRotateComplex(self, basis):
# Now low res (8x8) had problems;
# better with odd (7x7), but still not good.
# We'll use a higher res test image.
# fh = np.load(os.path.join(DATA_DIR, 'ffbbasis2d_xcoef_in_8_8.npy'))[:7,:7]
# Use a real data volume to generate a clean test image.
v = Volume(
np.load(os.path.join(DATA_DIR, "clean70SRibosome_vol.npy")).astype(
basis.dtype
)
)
src = Simulation(L=v.resolution, n=1, vols=v, dtype=v.dtype)
# Extract, this is the original image to transform.
x1 = src.images[0]

# Rotate 90 degrees CCW in cartesian coordinates.
x2 = Image(np.rot90(x1.asnumpy(), axes=(1, 2)))

# Express in an FB basis
basis = FFBBasis2D(x1.resolution, dtype=x1.dtype)
v1 = basis.evaluate_t(x1)
v2 = basis.evaluate_t(x2)

# Reflect in the FB basis space
v4 = basis.to_real(basis.complex_rotate(basis.to_complex(v1), 0, refl=[True]))

# Complex Rotate in the FB basis space
v3 = basis.to_real(basis.complex_rotate(basis.to_complex(v1), 2 * np.pi))
v1 = basis.to_real(basis.complex_rotate(basis.to_complex(v1), np.pi / 2))

# Evaluate back into cartesian
y1 = basis.evaluate(v1).asnumpy()
y2 = basis.evaluate(v2).asnumpy()
y3 = basis.evaluate(v3).asnumpy()
y4 = basis.evaluate(v4).asnumpy()

# Rotate 90
assert np.allclose(y1[0], y2[0], atol=1e-5)

# 2*pi Identity
assert np.allclose(x1[0].asnumpy(), y3[0], atol=1e-5)

# Refl (flipped using flipud)
assert np.allclose(np.flipud(x1.asnumpy()[0]), y4[0], atol=1e-5)

def testShift(self, basis):
"""
Compare shifting using Image with shifting provided by the Basis.
Expand Down
75 changes: 1 addition & 74 deletions tests/test_FLEbasis2D.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@
import numpy as np
import pytest

from aspire.basis import Coef, FBBasis2D, FFBBasis2D, FLEBasis2D
from aspire.basis import Coef, FBBasis2D, FLEBasis2D
from aspire.image import Image
from aspire.nufft import backend_available
from aspire.numeric import fft
from aspire.source import Simulation
from aspire.utils import utest_tolerance
from aspire.volume import Volume

from ._basis_util import UniversalBasisMixin
Expand Down Expand Up @@ -228,78 +227,6 @@ def testLowPass():
_ = basis.lowpass(coefs[0, :], L)


def testRotate():
# test ability to accurately rotate images via
# FLE coefficients

L = 128
basis = FLEBasis2D(L, match_fb=False)

# sample image
ims = create_images(L, 1)
# rotate 90 degrees in cartesian coordinates
ims_90 = Image(np.rot90(ims.asnumpy(), axes=(1, 2)))

# get FLE coefficients
coefs = basis.evaluate_t(ims)
coefs_cart_rot = basis.evaluate_t(ims_90)

# rotate original image in FLE space using Steerable rotate method
coefs_fle_rot = basis.rotate(coefs, np.pi / 2)

# back to cartesian
ims_cart_rot = basis.evaluate(coefs_cart_rot)
ims_fle_rot = basis.evaluate(coefs_fle_rot)

# test rot90 close
assert np.allclose(ims_cart_rot[0], ims_fle_rot[0], atol=1e-4)

# 2Pi identity in FLE space (rotate by 2Pi)
coefs_fle_2pi = basis.rotate(coefs, 2 * np.pi)
ims_fle_2pi = basis.evaluate(coefs_fle_2pi)

# test 2Pi identity
assert np.allclose(ims[0], ims_fle_2pi[0], atol=utest_tolerance(basis.dtype))

# Reflect in FLE space (rotate by Pi)
coefs_fle_pi = basis.rotate(coefs, np.pi)
ims_fle_pi = basis.evaluate(coefs_fle_pi)

# test reflection
assert np.allclose(np.fliplr(np.flipud(ims.asnumpy()[0])), ims_fle_pi[0], atol=1e-4)

# make sure you can pass in a 1-D array if you want
_ = basis.lowpass(Coef(basis, np.zeros((basis.count,))), np.pi)


def testRotate45():
# test ability to accurately rotate images via
# FLE coefficients
dtype = np.float64

L = 128
fb_basis = FFBBasis2D(L, dtype=dtype)
basis = FLEBasis2D(L, match_fb=True, dtype=dtype)

# sample image
ims = create_images(L, 1)

# get FLE coefficients
fb_coefs = fb_basis.evaluate_t(ims)
coefs = basis.evaluate_t(ims)

# rotate original image in FLE space using Steerable rotate method
fb_coefs_rot = fb_basis.rotate(fb_coefs, np.pi / 4)
coefs_rot = basis.rotate(coefs, np.pi / 4)

# back to cartesian
fb_ims_rot = fb_basis.evaluate(fb_coefs_rot)
ims_rot = basis.evaluate(coefs_rot)

# test close
assert np.allclose(ims_rot[0], fb_ims_rot[0], atol=1e-4)


def testRadialConvolution():
# test ability to accurately convolve with a radial
# (e.g. CTF) function via FLE coefficients
Expand Down
Loading