Skip to content

Commit

Permalink
remove use of attr repr_ns (#1331)
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Dec 2, 2024
1 parent 7e7af02 commit 0261a99
Show file tree
Hide file tree
Showing 14 changed files with 66 additions and 40 deletions.
8 changes: 7 additions & 1 deletion doc/long_vignettes/tskitconvert_vignette.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ We can pass the `params` object on when exporting the data to `tskit`:

```{code-cell} python
ts = pop.dump_tables_to_tskit(model_params=params)
recovered_params = fwdpy11.ModelParams(**eval(ts.metadata["model_params"]))
def rebuild_params(md):
import fwdpy11
for i in dir(fwdpy11):
exec(f"from fwdpy11 import {i}")
recovered_params = fwdpy11.ModelParams(**eval(md))
return recovered_params
recovered_params = rebuild_params(ts.metadata["model_params"])
assert recovered_params == params
```
Note that we can recover our demographic model from the restored parameters:
Expand Down
2 changes: 1 addition & 1 deletion fwdpy11/_types/demographic_model_citation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

@attr_class_pickle
@attr_class_to_from_dict
@attr.s(kw_only=True, frozen=True, auto_attribs=True, repr_ns="fwdpy11")
@attr.s(kw_only=True, frozen=True, auto_attribs=True)
@deprecated(reason="prefer ForwardDemesGraph")
class DemographicModelCitation(object):
"""
Expand Down
2 changes: 1 addition & 1 deletion fwdpy11/_types/demographic_model_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@attr_class_pickle
@attr_class_to_from_dict_no_recurse
@attr.s(kw_only=True, frozen=True, auto_attribs=True, repr_ns="fwdpy11")
@attr.s(kw_only=True, frozen=True, auto_attribs=True)
@deprecated(reason="prefer ForwardDemesGraph")
class DemographicModelDetails(object):
"""
Expand Down
2 changes: 1 addition & 1 deletion fwdpy11/_types/forward_demes_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ForwardTimeInterval:
end_time: int


@attr.s(repr_ns="fwdpy11")
@attr.s()
@attr_class_pickle_with_super
@attr_class_to_from_dict
class ForwardDemesGraph(fwdpy11._fwdpy11._ForwardDemesGraph):
Expand Down
4 changes: 2 additions & 2 deletions fwdpy11/_types/model_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@


@attr_class_to_from_dict_no_recurse
@attr.s(kw_only=True, frozen=True, slots=True, repr_ns="fwdpy11")
@attr.s(kw_only=True, frozen=True, slots=True)
class MutationAndRecombinationRates(object):
"""
Stores and validates the mutation and recombination rate parameters
Expand Down Expand Up @@ -111,7 +111,7 @@ def validate_timings(


@attr_class_to_from_dict_no_recurse
@attr.s(kw_only=True, frozen=True, slots=True, repr_ns="fwdpy11")
@attr.s(kw_only=True, frozen=True, slots=True)
class ModelParams(object):
"""
This class stores and validates the parameters of a simulation.
Expand Down
2 changes: 1 addition & 1 deletion fwdpy11/_types/new_mutation_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .._fwdpy11 import ll_NewMutationData


@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11", kw_only=True)
@attr.s(auto_attribs=True, frozen=True, kw_only=True)
class NewMutationData(ll_NewMutationData):
"""
Data object for :func:`fwdpy11.DiploidPopulation.add_mutation`
Expand Down
12 changes: 6 additions & 6 deletions fwdpy11/genetic_map_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def _is_integer_if_discrete(self, attribute, value):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class PoissonInterval(fwdpy11._fwdpy11._ll_PoissonInterval):
"""
Generate poisson number of crossover breakpoints.
Expand Down Expand Up @@ -90,7 +90,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class PoissonPoint(fwdpy11._fwdpy11._ll_PoissonPoint):
"""
Generate a recombination breakpoint at a fixed position if the
Expand Down Expand Up @@ -149,7 +149,7 @@ def __setstate__(self, d):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class BinomialInterval(fwdpy11._fwdpy11._ll_BinomialInterval):
"""
Generate exactly one crossover with a given probability
Expand Down Expand Up @@ -202,7 +202,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class BinomialPoint(fwdpy11._fwdpy11._ll_BinomialPoint):
"""
Generate a crossover breakpoint at a fixed position with a
Expand Down Expand Up @@ -255,7 +255,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class FixedCrossovers(fwdpy11._fwdpy11._ll_FixedCrossovers):
"""
Generate a fixed number of crossover breakpoints.
Expand Down Expand Up @@ -312,7 +312,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class BinomialIntervalMap(fwdpy11._fwdpy11._ll_BinomialIntervalMap):
"""
Generate exactly one crossover with a given probability
Expand Down
27 changes: 18 additions & 9 deletions fwdpy11/genetic_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class TimingError(Exception):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, auto_detect=True)
class Optimum(_ll_Optimum):
"""
Parameters for a trait optimum.
Expand Down Expand Up @@ -93,10 +93,15 @@ def validate_when(self, attribute, value):
def __attrs_post_init__(self):
super(Optimum, self).__init__(self.optimum, self.VS, self.when)

def __repr__(self) -> str:
return (
f"fwdpy11.Optimum(optimum={self.optimum}, VS={self.VS}, when={self.when})"
)


@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11", eq=False)
@attr.s(auto_attribs=True, frozen=True, eq=False, auto_detect=True)
class PleiotropicOptima(_ll_PleiotropicOptima):
"""
Parameters for multiple trait optima
Expand Down Expand Up @@ -141,8 +146,12 @@ def __eq__(self, other):

return optima_equal and VS_equal and when_equal

def __repr__(self) -> str:
args = f"optima={self.optima}, VS={self.VS}, when={self.when}"
return f"(fwdpy11.PleiotropicOptima({args}))"


@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class GaussianStabilizingSelection(_ll_GaussianStabilizingSelection):
"""
Define a mapping of phenotype-to-fitness according to a
Expand Down Expand Up @@ -224,7 +233,7 @@ def validate_timings(self, deme: int, demography: ForwardDemesGraph) -> None:

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class NoNoise(_ll_NoNoise):
"""
No random effects on genetic values
Expand All @@ -241,7 +250,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class GaussianNoise(_ll_GaussianNoise):
"""
Gaussian noise added to genetic values.
Expand Down Expand Up @@ -269,7 +278,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class Additive(_ll_Additive):
"""
Additive effects on genetic values.
Expand Down Expand Up @@ -320,7 +329,7 @@ def validate_timings(self, deme: int, demography: ForwardDemesGraph) -> None:

@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class Multiplicative(_ll_Multiplicative):
"""
Multiplicative effects on genetic values.
Expand Down Expand Up @@ -371,7 +380,7 @@ def validate_timings(self, deme: int, demography: ForwardDemesGraph) -> None:

@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class GBR(_ll_GBR):
"""
The "gene-based recessive" trait model described in Thornton et al.
Expand Down Expand Up @@ -411,7 +420,7 @@ def validate_timings(self, deme: int, demography: ForwardDemesGraph) -> None:

@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class AdditivePleiotropy(_ll_StrictAdditiveMultivariateEffects):
"""
Multivariate trait values under strictly additive effects.
Expand Down
8 changes: 4 additions & 4 deletions fwdpy11/mutation_dominance.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class FixedDominance(fwdpy11._fwdpy11.MutationDominance):
"""
Fixed heterozygous effects.
Expand All @@ -48,7 +48,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class ExponentialDominance(fwdpy11._fwdpy11.MutationDominance):
"""
Exponential distribution of heterozygous effects.
Expand All @@ -69,7 +69,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class UniformDominance(fwdpy11._fwdpy11.MutationDominance):
"""
Uniform distribution of heterozygous effects.
Expand All @@ -93,7 +93,7 @@ def __attrs_post_init__(self):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True)
class LargeEffectExponentiallyRecessive(fwdpy11._fwdpy11.MutationDominance):
"""
Large effect mutations are more recessive according to
Expand Down
20 changes: 10 additions & 10 deletions fwdpy11/regions.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class Region(fwdpy11._fwdpy11._ll_Region):
"""
A genomic region, defined by half-open interval [beg, end)
Expand Down Expand Up @@ -85,7 +85,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class ConstantS(fwdpy11._fwdpy11._ll_ConstantS):
"""
Mutations with fixed effect sizes
Expand Down Expand Up @@ -145,7 +145,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class ExpS(fwdpy11._fwdpy11._ll_ExpS):
"""
Exponential distribution of effect sizes
Expand Down Expand Up @@ -207,7 +207,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class GammaS(fwdpy11._fwdpy11._ll_GammaS):
"""
Gamma distribution of effect sizes
Expand Down Expand Up @@ -271,7 +271,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class GaussianS(fwdpy11._fwdpy11._ll_GaussianS):
"""
Gaussian distribution of effect sizes
Expand Down Expand Up @@ -331,7 +331,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class LogNormalS(fwdpy11._fwdpy11._ll_LogNormalS):
"""
Log-normal distribution of effect sizes.
Expand Down Expand Up @@ -441,7 +441,7 @@ def mv(
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(auto_attribs=True, frozen=True, repr=False)
class UniformS(fwdpy11._fwdpy11._ll_UniformS):
"""
Uniform distrubution of effect sizes
Expand Down Expand Up @@ -505,7 +505,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False)
class MultivariateGaussianEffects(fwdpy11._fwdpy11._ll_MultivariateGaussianEffects):
"""
Pleiotropic effects via a multivariate Gaussian distribution.
Expand Down Expand Up @@ -596,7 +596,7 @@ def __eq__(self, other):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False)
class mvDES(fwdpy11._fwdpy11._ll_mvDES):
"""
General multivariate distribution of effect sizes.
Expand Down Expand Up @@ -631,7 +631,7 @@ def __attrs_post_init__(self):
@region_custom_repr
@attr_class_pickle_with_super
@attr_class_to_from_dict_no_recurse
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False, repr_ns="fwdpy11")
@attr.s(eq=False, auto_attribs=True, frozen=True, repr=False)
class DiscreteDESD(fwdpy11._fwdpy11._ll_DiscreteDESD):
"""
Discretized distribution of effect sizes and dominance.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
]
dynamic = ["version"]
dependencies = [
"attrs >= 20.3.0, < 24.1.0",
"attrs >= 20.3.0",
"numpy>=1.23.5",
"tskit >= 0.5.6",
"demes ~= 0.2.2",
Expand Down
2 changes: 1 addition & 1 deletion requirements/minimal_install_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
attrs < 20.4
attrs >= 20.30.0
demes == 0.2.2
numpy >= 1.23.5
tskit >= 0.5.6
Expand Down
12 changes: 10 additions & 2 deletions tests/test_metadata_roundtrips_via_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,16 @@ def test_metadata_roundtrip_single_deme_sim_with_parameters(rng, pdict, pop, inc
)

provenance = json.loads(ts.provenance(0).record)
print(provenance["parameters"]["params_dict"])
params_dict = eval(provenance["parameters"]["params_dict"])

def rebuild_pdict(provenance):
import fwdpy11

for i in dir(fwdpy11):
exec(f"from fwdpy11 import {i}")
params_dict = eval(provenance["parameters"]["params_dict"])
return params_dict

params_dict = rebuild_pdict(provenance)

assert params_dict == params.asdict()
script = provenance["parameters"]["script"]
Expand Down
3 changes: 3 additions & 0 deletions tests/test_tskit_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ def test_single_model_params(pop, pdict1):
ts = pop.dump_tables_to_tskit(model_params=mp)

# reconstruct
for i in dir(fwdpy11):
exec(f"from fwdpy11 import {i}")

mp_rebuilt = fwdpy11.ModelParams(**eval(ts.metadata["model_params"]))

assert mp == mp_rebuilt
Expand Down

0 comments on commit 0261a99

Please sign in to comment.