Skip to content

Commit

Permalink
Merge pull request #297 from FlorianPfaff/sampling/fibonaccihopf
Browse files Browse the repository at this point in the history
Added FibonacciHopfSampler
  • Loading branch information
FlorianPfaff authored Sep 17, 2023
2 parents 536b260 + f802102 commit 48d8f11
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
47 changes: 47 additions & 0 deletions pyrecest/sampling/hyperspherical_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,50 @@ def get_grid(self, grid_density_parameter: int | list[int]):
"layer-parameter": grid_density_parameter,
}
return grid, grid_specific_description


class FibonacciHopfSampler(AbstractHopfBasedS3Sampler):
@beartype
def get_grid(self, grid_density_parameter: int | list[int]):
"""
Hopf coordinates are (θ, ϕ, ψ) where θ and ϕ are the angles for the sphere and ψ is the angle on the circle
First parameter is the number of points on the sphere, second parameter is the number of points on the circle.
"""
if isinstance(grid_density_parameter, int):
grid_density_parameter = [grid_density_parameter]

s3_points_list = []

# Step 1: Discretize the sphere using the Fibonacci grid
spherical_sampler = SphericalFibonacciSampler()
phi, theta, _ = spherical_sampler.get_grid_spherical_coordinates(
grid_density_parameter[0]
)
spherical_points = np.column_stack(
(theta, phi)
) # stack to match expected shape

# Step 2: Discretize the unit circle using the circular grid
circular_sampler = CircularUniformSampler()
if len(grid_density_parameter) == 2:
n_sample_circle = grid_density_parameter[1]
else:
n_sample_circle = np.sqrt(grid_density_parameter[0])
psi_points = circular_sampler.get_grid(n_sample_circle)

# Step 3: Combine the two grids to generate a grid for S3
for spherical_point in spherical_points:
for psi in psi_points:
s3_point = np.array([spherical_point[0], spherical_point[1], psi])
s3_points_list.append(s3_point)

s3_points = np.vstack(s3_points_list)
grid = AbstractHopfBasedS3Sampler.hopf_coordinates_to_quaterion_yershova(
s3_points[:, 0], s3_points[:, 1], s3_points[:, 2]
)

grid_specific_description = {
"scheme": "fibonacci_hopf",
"layer-parameter": grid_density_parameter,
}
return grid, grid_specific_description
18 changes: 18 additions & 0 deletions pyrecest/tests/test_hyperspherical_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ..sampling.hyperspherical_sampler import (
AbstractHopfBasedS3Sampler,
DriscollHealySampler,
FibonacciHopfSampler,
HealpixHopfSampler,
HealpixSampler,
SphericalFibonacciSampler,
Expand Down Expand Up @@ -96,6 +97,23 @@ def test_healpix_hopf_sampler(self, input_value, expected_grid_points):
f"Expected {dim+1}-dimensional-output but got {grid.shape[1]}-dimensional output",
)

def test_fibonacci_hopf_sampler(self):
sampler = FibonacciHopfSampler()
grid_density_parameter = [12, 4]
grid, _ = sampler.get_grid(grid_density_parameter)

expected_points = np.prod(grid_density_parameter)
self.assertEqual(
grid.shape[0],
expected_points,
f"Expected {expected_points} points but got {grid.shape[0]}",
)
self.assertEqual(
grid.shape[1],
4,
f"Expected 4-dimensional-output but got {grid.shape[1]}-dimensional output",
)


class TestHopfConversion(unittest.TestCase):
def test_conversion(self):
Expand Down

0 comments on commit 48d8f11

Please sign in to comment.