-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to enums for distributions instead of strings
- Loading branch information
Sosnowsky
committed
Jan 29, 2025
1 parent
6683351
commit f91d773
Showing
8 changed files
with
127 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,77 @@ | ||
from enum import Enum | ||
from abc import ABC, abstractmethod | ||
import numpy as np | ||
|
||
|
||
class Distribution(Enum): | ||
"""Enum class used to identify distribution functions.""" | ||
|
||
deg = 1 | ||
zeros = 2 | ||
exp = 3 | ||
gamma = 4 | ||
normal = 5 | ||
uniform = 6 | ||
rayleigh = 7 | ||
|
||
|
||
class AbstractDistribution(ABC): | ||
"""Abstract class used to represent and implement a distribution function.""" | ||
|
||
@abstractmethod | ||
def sample( | ||
self, | ||
num_blobs: int, | ||
**kwargs, | ||
) -> np.ndarray: | ||
raise NotImplementedError | ||
|
||
|
||
def _sample_deg(num_blobs, **kwargs): | ||
free_param = kwargs["free_param"] | ||
return free_param * np.ones(num_blobs).astype(np.float64) | ||
|
||
|
||
def _sample_zeros(num_blobs, **kwargs): | ||
return np.zeros(num_blobs).astype(np.float64) | ||
|
||
|
||
def _sample_exp(num_blobs, **kwargs): | ||
free_param = kwargs["free_param"] | ||
return np.random.exponential(scale=free_param, size=num_blobs).astype(np.float64) | ||
|
||
|
||
def _sample_gamma(num_blobs, **kwargs): | ||
free_param = kwargs["free_param"] | ||
return np.random.gamma( | ||
shape=free_param, scale=1 / free_param, size=num_blobs | ||
).astype(np.float64) | ||
|
||
|
||
def _sample_normal(num_blobs, **kwargs): | ||
free_param = kwargs["free_param"] | ||
return np.random.normal(loc=0, scale=free_param, size=num_blobs).astype(np.float64) | ||
|
||
|
||
def _sample_uniform(num_blobs, **kwargs): | ||
free_param = kwargs["free_param"] | ||
return np.random.uniform( | ||
low=1 - free_param / 2, high=1 + free_param / 2, size=num_blobs | ||
).astype(np.float64) | ||
|
||
|
||
def _sample_rayleigh(num_blobs, **kwargs): | ||
return np.random.rayleigh(scale=np.sqrt(2.0 / np.pi), size=num_blobs).astype( | ||
np.float64 | ||
) | ||
|
||
|
||
DISTRIBUTIONS = { | ||
Distribution.deg: _sample_deg, | ||
Distribution.zeros: _sample_zeros, | ||
Distribution.exp: _sample_exp, | ||
Distribution.gamma: _sample_gamma, | ||
Distribution.normal: _sample_normal, | ||
Distribution.uniform: _sample_uniform, | ||
Distribution.rayleigh: _sample_rayleigh, | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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