-
Notifications
You must be signed in to change notification settings - Fork 26
New logical (F)FBBasis2D tests #576
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
e5e989a
Encode size in 2D FB test
janden 4eddd36
Add test for 2D FB Gaussian expansion
janden a17c14e
Add eval–expand test for 2D FB
janden ce42bc8
Add adjoint test for 2D FB
janden a303dd5
Add test for isotropic and modulated in 2D FB
janden 7161ebb
Add test for indices
janden b440cb2
Add test for specific FB 2D basis element
janden bf42cf8
Make 2D FB tests FFB friendly
janden e08ec5c
Move tests not specific to FB into a mixin
janden e10ac32
Add mixin to FFB 2D tests
janden 09ffe80
Remove hardcoding from complex conversion tests
janden e592055
First attempt at testElement for FFB2D
janden 5761f6a
Remove hardcoded (F)FB2D tests
janden dc622d7
Parameterize tests
janden b93bbdf
Pass dtype to gaussian_2d
janden 7f2c892
Expand on comment regarding factor of 1/2
janden f3c540b
Fix rtoc bug
janden be9c860
Add shape checks
janden eb92614
Loop over different indices in 2D (F)FB tests
janden File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| from unittest.case import SkipTest | ||
|
|
||
| import numpy as np | ||
|
|
||
| from aspire.image import Image | ||
| from aspire.utils import gaussian_2d, utest_tolerance | ||
| from aspire.utils.coor_trans import grid_2d | ||
| from aspire.utils.random import randn | ||
|
|
||
|
|
||
| class Steerable2DMixin: | ||
| def testIndices(self): | ||
| ell_max = self.basis.ell_max | ||
| k_max = self.basis.k_max | ||
|
|
||
| indices = self.basis.indices() | ||
|
|
||
| i = 0 | ||
|
|
||
| for ell in range(ell_max + 1): | ||
| if ell == 0: | ||
| sgns = [1] | ||
| else: | ||
| sgns = [1, -1] | ||
|
|
||
| for sgn in sgns: | ||
| for k in range(k_max[ell]): | ||
| self.assertTrue(indices["ells"][i] == ell) | ||
| self.assertTrue(indices["sgns"][i] == sgn) | ||
| self.assertTrue(indices["ks"][i] == k) | ||
|
|
||
| i += 1 | ||
|
|
||
| def testGaussianExpand(self): | ||
| # Offset slightly | ||
| x0 = 0.50 | ||
| y0 = 0.75 | ||
|
|
||
| # Want sigma to be as large as possible without the Gaussian | ||
| # spilling too much outside the central disk. | ||
| sigma = self.L / 8 | ||
| im1 = gaussian_2d( | ||
| self.L, x0=x0, y0=y0, sigma_x=sigma, sigma_y=sigma, dtype=self.dtype | ||
| ) | ||
|
|
||
| coef = self.basis.expand(im1) | ||
| im2 = self.basis.evaluate(coef) | ||
|
|
||
| if isinstance(im2, Image): | ||
| im2 = im2.asnumpy() | ||
| im2 = im2[0] | ||
|
|
||
| # For small L there's too much clipping at high freqs to get 1e-3 | ||
| # accuracy. | ||
| if self.L < 32: | ||
| atol = 1e-2 | ||
| else: | ||
| atol = 1e-3 | ||
|
|
||
| self.assertTrue(im1.shape == im2.shape) | ||
| self.assertTrue(np.allclose(im1, im2, atol=atol)) | ||
|
|
||
| def testIsotropic(self): | ||
| sigma = self.L / 8 | ||
| im = gaussian_2d(self.L, sigma_x=sigma, sigma_y=sigma, dtype=self.dtype) | ||
|
|
||
| coef = self.basis.expand(im) | ||
|
|
||
| ells = self.basis.indices()["ells"] | ||
|
|
||
| energy_outside = np.sum(np.abs(coef[ells != 0]) ** 2) | ||
| energy_total = np.sum(np.abs(coef) ** 2) | ||
|
|
||
| energy_ratio = energy_outside / energy_total | ||
|
|
||
| self.assertTrue(energy_ratio < 0.01) | ||
|
|
||
| def testModulated(self): | ||
| if self.L < 32: | ||
| raise SkipTest | ||
|
|
||
| ell = 1 | ||
|
|
||
| sigma = self.L / 8 | ||
| im = gaussian_2d(self.L, sigma_x=sigma, sigma_y=sigma, dtype=self.dtype) | ||
|
|
||
| g2d = grid_2d(self.L) | ||
|
|
||
| for trig_fun in (np.sin, np.cos): | ||
| im1 = im * trig_fun(ell * g2d["phi"]) | ||
|
|
||
| coef = self.basis.expand(im1) | ||
|
|
||
| ells = self.basis.indices()["ells"] | ||
|
|
||
| energy_outside = np.sum(np.abs(coef[ells != ell]) ** 2) | ||
| energy_total = np.sum(np.abs(coef) ** 2) | ||
|
|
||
| energy_ratio = energy_outside / energy_total | ||
|
|
||
| self.assertTrue(energy_ratio < 0.10) | ||
|
|
||
| def testEvaluateExpand(self): | ||
| coef1 = randn(self.basis.count, seed=self.seed) | ||
| coef1 = coef1.astype(self.dtype) | ||
|
|
||
| im = self.basis.evaluate(coef1) | ||
| if isinstance(im, Image): | ||
| im = im.asnumpy() | ||
| coef2 = self.basis.expand(im)[0] | ||
|
|
||
| self.assertTrue(coef1.shape == coef2.shape) | ||
| self.assertTrue(np.allclose(coef1, coef2, atol=utest_tolerance(self.dtype))) | ||
|
|
||
| def testAdjoint(self): | ||
| u = randn(self.basis.count, seed=self.seed) | ||
| u = u.astype(self.dtype) | ||
|
|
||
| Au = self.basis.evaluate(u) | ||
| if isinstance(Au, Image): | ||
| Au = Au.asnumpy() | ||
|
|
||
| x = randn(*self.basis.sz, seed=self.seed) | ||
| x = x.astype(self.dtype) | ||
|
|
||
| ATx = self.basis.evaluate_t(x) | ||
|
|
||
| Au_dot_x = np.sum(Au * x) | ||
| u_dot_ATx = np.sum(u * ATx) | ||
|
|
||
| self.assertTrue(Au_dot_x.shape == u_dot_ATx.shape) | ||
| self.assertTrue(np.isclose(Au_dot_x, u_dot_ATx)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.