Skip to content
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

Add weighted sampler #732

Merged
merged 2 commits into from
Jan 15, 2024
Merged
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
17 changes: 15 additions & 2 deletions batchflow/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ def __and__(self, other):
result of the multiplication.
"""
if isinstance(other, (float, int)):
self.weight *= other
return self
return WeightedSampler(self, self.weight * other)

return AndSampler(self, other)

Expand Down Expand Up @@ -341,6 +340,20 @@ def sample(self, size):

return np.concatenate(samples)[:size]

class WeightedSampler(Sampler):
""" Class for implementing `&` (weighting) operation on a number and a `Sampler` instance.
"""
def __init__(self, base, weight, *args, **kwargs):
super().__init__(*args, **kwargs)
self.bases = [base]
self.weight = weight

def sample(self, size):
""" Sampling procedure of a product of the number and the sampler instance. Check out the docstring of
`Sampler.sample` for more info.
"""
return self.bases[0].sample(size)


class BaseOperationSampler(Sampler):
""" Base class for implementing all arithmetic operations on `Sampler`-instances.
Expand Down
Loading