Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .mega-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ PRE_COMMANDS:
venv: pylint

PYTHON_PYLINT_CONFIG_FILE: LINTER_DEFAULT
PYTHON_PYLINT_ARGUMENTS: --disable=import-error

DISABLE_LINTERS:
- SPELL_CSPELL
Expand Down
3 changes: 2 additions & 1 deletion src/pyrecest/distributions/abstract_mixture.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

# pylint: disable=redefined-builtin,no-name-in-module,no-member
from pyrecest.backend import (
array,
asarray,
empty,
int32,
Expand Down Expand Up @@ -58,7 +59,7 @@ def __init__(
"Elements with zero weights detected. Pruning elements in mixture with weight zero."
)
dists = [dists[i] for i in non_zero_indices]
weights = weights[non_zero_indices]
weights = weights[array(non_zero_indices, dtype=int64)]

self.dists = dists

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@
arccos,
array,
clip,
concatenate,
cos,
exp,
int32,
int64,
isnan,
linalg,
ndim,
ones,
pi,
sin,
sinh,
stack,
zeros,
)
from scipy.special import iv, ive
Expand Down Expand Up @@ -125,8 +128,6 @@ def sample(self, n):
def sample_deterministic(self):
"""Return deterministic sigma points matched to the mean direction."""
n_samples = self.dim * 2 + 1
samples = zeros((self.input_dim, n_samples))
samples[0, 0] = 1.0

mean_res_length = self.a_d(self.input_dim, self.kappa)
cos_alpha = clip(
Expand All @@ -135,23 +136,31 @@ def sample_deterministic(self):
1.0,
)
alpha = arccos(cos_alpha)
sin_alpha = sin(alpha)
cos_alpha_val = cos(alpha)

first_row = concatenate((array([1.0]), cos_alpha_val * ones(2 * self.dim)))
tangent_rows = []
for i in range(self.dim):
positive_col = 2 * i + 1
negative_col = positive_col + 1
tangent_row = i + 1
samples[0, positive_col] = cos(alpha)
samples[0, negative_col] = cos(alpha)
samples[tangent_row, positive_col] = sin(alpha)
samples[tangent_row, negative_col] = -sin(alpha)
tangent_rows.append(
concatenate(
(
array([0.0]),
zeros(2 * i),
array([sin_alpha, -sin_alpha]),
zeros(2 * (self.dim - i - 1)),
)
)
)
samples = stack([first_row] + tangent_rows, axis=0)

Q = self.get_rotation_matrix()
samples = Q @ samples
return samples

def get_rotation_matrix(self):
"""Return an orthogonal matrix whose first column is ``mu``."""
M = zeros((self.dim + 1, self.dim + 1))
M[:, 0] = self.mu
M = concatenate((self.mu[:, None], zeros((self.dim + 1, self.dim))), axis=1)
Q, R = linalg.qr(M)
if R[0, 0] < 0:
Q = -Q
Expand Down
2 changes: 1 addition & 1 deletion src/pyrecest/evaluation/eot_shape_database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class PolygonWithSampling(Polygon): # pylint: disable=abstract-method
__slots__ = Polygon.__slots__

def __new__(cls, shell=None, holes=None): # nosec
polygon = super().__new__(cls, shell=shell, holes=holes) # nosec
polygon = super().__new__(cls, shell=shell, holes=holes) # nosec # pylint: disable=unexpected-keyword-arg,no-value-for-parameter
polygon.__class__ = cls
return polygon

Expand Down
2 changes: 1 addition & 1 deletion src/pyrecest/evaluation/evaluate_for_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def evaluate_for_file(
np.ndarray,
np.ndarray,
np.ndarray,
np.ndarray[np.ndarray],
np.ndarray,
]:
data = np.load(input_file_name, allow_pickle=True).item()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def test_pdf_custom_grid_flattens_grid_values_for_nearest_neighbor(self):

query_points = array([[pi, 0.0], [pi, pi]])
expected = array([3.0, 4.0]) / normalizer
npt.assert_allclose(hgd.pdf(query_points), expected)
npt.assert_allclose(hgd.pdf(query_points), expected, rtol=2e-7)

def test_custom_grid_uses_elementwise_toroidal_distance(self):
grid = array(
Expand Down
9 changes: 7 additions & 2 deletions tests/distributions/test_von_mises_fisher_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,14 @@ def test_sample_deterministic_matches_mean_resultant_vector(self, _, mu, kappa):
weights = ones(expected_n_samples) / expected_n_samples
self.assertEqual(samples.shape, (vmf.input_dim, expected_n_samples))
npt.assert_allclose(
linalg.norm(samples, axis=0), ones(expected_n_samples), atol=1e-12
linalg.norm(samples, axis=0),
ones(expected_n_samples),
rtol=5e-7,
atol=2e-7,
)
npt.assert_allclose(
samples @ weights, vmf.mean_resultant_vector(), rtol=5e-7, atol=2e-7
)
npt.assert_allclose(samples @ weights, vmf.mean_resultant_vector(), atol=1e-12)

def test_a_d_uses_scaled_bessel_ratio_for_large_kappa(self):
npt.assert_allclose(VonMisesFisherDistribution.a_d(3, 1000.0), 0.999)
Expand Down