Repository navigation
Expand file tree
/
Copy pathwrap_folded_normal.py
More file actions
49 lines (43 loc) · 1.9 KB
/
Copy pathwrap_folded_normal.py
File metadata and controls
49 lines (43 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import torch
from typing import Optional
from abismal_torch.surrogate_posterior import FoldedNormalPosterior
from abismal_torch.symmetry import ReciprocalASUCollection
class FrequencyTrackingPosterior(FoldedNormalPosterior):
def __init__(
self,
rac: ReciprocalASUCollection,
loc: torch.Tensor,
scale: torch.Tensor,
epsilon: Optional[float] = 1e-12,
**kwargs
):
"""
A surrogate posterior that tracks how many times each HKL has been observed.
Args:
rac (ReciprocalASUCollection): ReciprocalASUCollection.
loc (torch.Tensor): Unconstrained location parameter of the distribution.
scale (torch.Tensor): Unconstrained scale parameter of the distribution.
epsilon (float, optional): Epsilon value for numerical stability. Defaults to 1e-12.
"""
super().__init__(rac, loc, scale, epsilon, **kwargs)
self.register_buffer(
"observation_count", torch.zeros(self.rac.rac_size, dtype=torch.long)
)
def update_observed(self, rasu_id: torch.Tensor, H: torch.Tensor) -> None:
"""
Update both the observed buffer and the observation count.
Args:
rasu_id (torch.Tensor): A tensor of shape (n_refln,) that contains the
rasu ID of each reflection.
H (torch.Tensor): A tensor of shape (n_refln, 3).
"""
h, k, l = H.T
observed_idx = self.rac.reflection_id_grid[rasu_id, h, k, l]
self.observed[observed_idx] = True
self.observation_count[observed_idx] += 1
def reliable_observations_mask(self, min_observations: int = 5) -> torch.Tensor:
"""
Returns a boolean tensor indicating which HKLs have been observed enough times
to be considered reliable.
"""
return self.observation_count >= min_observations