Skip to content

Commit

Permalink
add weighted sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
HollowPrincess committed Dec 26, 2023
1 parent cfd17a9 commit 7402cb6
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 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,19 @@ 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):
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

0 comments on commit 7402cb6

Please sign in to comment.