-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26152da
commit 1b664eb
Showing
4 changed files
with
135 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
"Module for constant distributions" | ||
import polars as pl | ||
from metasyncontrib.disclosure.base import metadist_disclosure | ||
from metasyn.distribution.constant import ( | ||
ConstantDistribution, | ||
DiscreteConstantDistribution, | ||
StringConstantDistribution, | ||
DateTimeConstantDistribution, | ||
TimeConstantDistribution, | ||
DateConstantDistribution, | ||
) | ||
|
||
|
||
def disclosure_constant(cls): | ||
"""Decorator that overrides _fit method for constant distributions.""" | ||
def _fit(values: pl.Series, n_avg=11): | ||
# if unique, just get that value if it occurs at least n_avg times | ||
if values.n_unique() == 1 & values.len() >= n_avg: | ||
return cls(values.unique()[0]) | ||
|
||
# otherwise get most common value | ||
val_counts = values.value_counts(sort=True) | ||
value = val_counts[0,0] | ||
count = val_counts[0,1] | ||
|
||
if count >= n_avg: | ||
return cls(value) | ||
|
||
return cls.default_distribution() | ||
|
||
setattr(cls, "_fit", _fit) | ||
return cls | ||
|
||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureConstant(ConstantDistribution): | ||
"Disclosure controlled ConstantDistribution" | ||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureDiscreteConstant(DiscreteConstantDistribution): | ||
"Disclosure controlled DiscreteConstantDistribution" | ||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureStringConstant(StringConstantDistribution): | ||
"Disclosure controlled StringConstantDistribution" | ||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureDateTimeConstant(DateTimeConstantDistribution): | ||
"Disclosure controlled DateTimeConstantDistribution" | ||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureTimeConstant(TimeConstantDistribution): | ||
"Disclosure controlled TimeConstantDistribution" | ||
|
||
@metadist_disclosure() | ||
@disclosure_constant | ||
class DisclosureDateConstant(DateConstantDistribution): | ||
"Disclosure controlled DateConstantDistribution" | ||
|
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,37 @@ | ||
from pytest import mark | ||
|
||
from metasyn.distribution.constant import ( | ||
ConstantDistribution, | ||
DiscreteConstantDistribution, | ||
StringConstantDistribution, | ||
DateTimeConstantDistribution, | ||
TimeConstantDistribution, | ||
DateConstantDistribution, | ||
) | ||
|
||
from metasyncontrib.disclosure.constant import ( | ||
DisclosureConstant, | ||
DisclosureDiscreteConstant, | ||
DisclosureStringConstant, | ||
DisclosureDateTimeConstant, | ||
DisclosureTimeConstant, | ||
DisclosureDateConstant, | ||
) | ||
|
||
@mark.parametrize( | ||
"dist_builtin, dist_disclosure, value", | ||
[ | ||
(ConstantDistribution, DisclosureConstant, 8.0), | ||
(DiscreteConstantDistribution, DisclosureDiscreteConstant, 8), | ||
(StringConstantDistribution, DisclosureStringConstant, "Secretvalue"), | ||
(DateTimeConstantDistribution, DisclosureDateTimeConstant, "2024-02-23T12:08:38+00:00"), | ||
(TimeConstantDistribution, DisclosureTimeConstant, "12:08:38"), | ||
(DateConstantDistribution, DisclosureDateConstant, "2024-02-23") | ||
] | ||
) | ||
def test_constant(dist_builtin, dist_disclosure, value): | ||
dist = dist_builtin(value) | ||
data = [dist.draw() for _ in range(21)] | ||
|
||
assert dist_disclosure.fit(data, n_avg = 22)._param_dict().get("value") != value | ||
assert dist_disclosure.fit(data, n_avg = 11)._param_dict().get("value") == value |
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