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

Improve Python class implementation for editor/IDE integration #677

Merged
merged 1 commit into from
Mar 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
62 changes: 30 additions & 32 deletions fwdpy11/discrete_demography.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,17 @@
attr_class_to_from_dict,
attr_class_to_from_dict_no_recurse)

_common_attr_attribs = {"frozen": True, "auto_attribs": True, "repr_ns": "fwdpy11"}


@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class MassMigration(fwdpy11._fwdpy11._ll_MassMigration):
"""
Mass migration events.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param when: When the mass migration happens
Expand All @@ -51,12 +49,12 @@ class MassMigration(fwdpy11._fwdpy11._ll_MassMigration):
:type destination: int
:param fraction: The fraction of `source` to move to `destination`.
:type fraction: float
:param move_individuals: If ``True``, the event moves individuals.
If ``False``, individuals are copied.
:param move_individuals: If `True`, the event moves individuals.
If `False`, individuals are copied.
:type move_individuals: bool
:param resets_growth_rate: (True) Whether or not to reset the
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
:type resets_growth_rate: bool

.. note::
Expand Down Expand Up @@ -107,8 +105,8 @@ def move_individuals(
:param fraction: The fraction of `source` to move to `destination`.
:type fraction: float
:param resets_growth_rate: (True) Whether or not to reset the
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
:type resets_growth_rate: bool

:rtype: :class:`fwdpy11.MassMigration`
Expand All @@ -133,8 +131,8 @@ def copy_individuals(
:param fraction: The fraction of `source` to copy to `destination`.
:type fraction: float
:param resets_growth_rate: (True) Whether or not to reset the
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
growth rates of `source` and `destination` to
:data:`fwdpy11.NOGROWTH`
:type resets_growth_rate: bool

:rtype: :class:`fwdpy11.MassMigration`
Expand All @@ -145,13 +143,13 @@ def copy_individuals(
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class SetDemeSize(fwdpy11._fwdpy11._ll_SetDemeSize):
"""
Set the size of a deme at a given time.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param when: The generation when the event occurs
Expand All @@ -161,7 +159,7 @@ class SetDemeSize(fwdpy11._fwdpy11._ll_SetDemeSize):
:param new_size: The new size
:type new_size: int
:param resets_growth_rate: (True) If deme size change resets
growth rate to :data:`fwdpy11.NOGROWTH`
growth rate to :data:`fwdpy11.NOGROWTH`
:type resets_growth_rate: bool

.. versionadded:: 0.5.3
Expand All @@ -186,13 +184,13 @@ def __attrs_post_init__(self):
@attr_class_to_from_dict
@attr_add_asblack
@attr_class_pickle_with_super
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class SetExponentialGrowth(fwdpy11._fwdpy11._ll_SetExponentialGrowth):
"""
Set the growth rate of a deme at a given time.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param when: The generation when the event occurs
Expand Down Expand Up @@ -221,13 +219,13 @@ def __attrs_post_init__(self):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class SetSelfingRate(fwdpy11._fwdpy11._ll_SetSelfingRate):
"""
Set the selfing probability within a deme at a given time.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param when: The generation when the event occurs
Expand Down Expand Up @@ -263,19 +261,19 @@ def __setstate__(self, d):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(eq=False, **_common_attr_attribs)
@attr.s(eq=False, auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class MigrationMatrix(fwdpy11._fwdpy11._ll_MigrationMatrix):
"""
The forward migration matrix for a simulation.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param migmatrix: A square matrix of non-negative floats.
:type migmatrix: numpy.ndarray
:param scaled: (True) If entries in `migmatrix` will be
multiplied by deme sizes during simulation
multiplied by deme sizes during simulation
:type scaled: bool

.. versionadded:: 0.5.3
Expand Down Expand Up @@ -314,15 +312,15 @@ def _set_migration_rates_convert_deme(i: typing.Optional[int]) -> int:

@attr_add_asblack
@attr_class_to_from_dict
@attr.s(eq=False, **_common_attr_attribs)
@attr.s(eq=False, auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class SetMigrationRates(fwdpy11._fwdpy11._ll_SetMigrationRates):
"""
Set the migration parameters of a simulation at a given time.
May be used to set either the migration rates from a given
deme or the entire migration matrix.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param when: The generation when the event occurs
Expand All @@ -333,11 +331,11 @@ class SetMigrationRates(fwdpy11._fwdpy11._ll_SetMigrationRates):
:type migrates: list or numpy.ndarray

The migration rates are equivalent to the single-generation ancestry proportions
with respect to ``deme``.
with respect to `deme`.

In order to change the entire migration matrix, pass ``None`` (or -1)
to ``deme`` and a 2d ``numpy.ndarray`` for ``migrates``. A value of
``None`` will be converted to ``-1`` in this case.
In order to change the entire migration matrix, pass `None` (or -1)
to `deme` and a 2d `numpy.ndarray` for `migrates`. A value of
`None` will be converted to `-1` in this case.

.. versionadded:: 0.5.3

Expand Down Expand Up @@ -430,14 +428,14 @@ def _convert_migmatrix(o):

@attr_add_asblack
@attr_class_to_from_dict_no_recurse
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class DiscreteDemography(fwdpy11._fwdpy11._ll_DiscreteDemography):
"""
Representation of demographic events acting on
discrete demes (sub-populations).

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param mass_migrations: Instances of :class:`fwdpy11.MassMigration`
Expand All @@ -449,8 +447,8 @@ class DiscreteDemography(fwdpy11._fwdpy11._ll_DiscreteDemography):
:param set_selfing_rates: Instances of :class:`fwdpy11.SetSelfingRate`
:type set_selfing_rates: None or list[fwdpy11.SetSelfingRate]
:param migmatrix: A migraton matrix. See :ref:`migration`. If not
``None``, then input are converted into an
instance of :class:`fwdpy11.MigrationMatrix`.
`None`, then input are converted into an
instance of :class:`fwdpy11.MigrationMatrix`.
:type migmatrix: None or numpy.ndarray or fwdpy11.MigrationMatrix
:param set_migration_rates: Instances of :class:`fwdpy11.SetMigrationRates`
:type set_migration_rates: None or list[fwdpy11.SetMigrationRates]
Expand Down
52 changes: 25 additions & 27 deletions fwdpy11/genetic_map_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
from .class_decorators import (attr_add_asblack, attr_class_pickle_with_super,
attr_class_to_from_dict)

_common_attr_attribs = {"frozen": True, "auto_attribs": True, "repr_ns": "fwdpy11"}


def _is_integer_if_discrete(self, attribute, value):
if self.discrete is True:
Expand All @@ -38,13 +36,13 @@ def _is_integer_if_discrete(self, attribute, value):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class PoissonInterval(fwdpy11._fwdpy11._ll_PoissonInterval):
"""
Generate poisson number of crossover breakpoints.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param beg: The beginning of the region
Expand All @@ -53,8 +51,8 @@ class PoissonInterval(fwdpy11._fwdpy11._ll_PoissonInterval):
:type end: int or float
:param mean: The mean number of breakpoints per meiosis
:type mean: float
:param discrete: If ``False``, positions are continuous and uniform from ``[beg, end)``.
If ``True``, positions take integer values uniformly from ``[beg, end)``.
:param discrete: If `False`, positions are continuous and uniform from `[beg, end)`.
If `True`, positions take integer values uniformly from `[beg, end)`.
:type discrete: bool

.. versionadded:: 0.3.0
Expand All @@ -74,7 +72,7 @@ class PoissonInterval(fwdpy11._fwdpy11._ll_PoissonInterval):

.. versionchanged:: 0.12.0

Added ``discrete`` option to initializer.
Added `discrete` option to initializer.
"""

beg: typing.Union[int, float] = attr.ib(validator=_is_integer_if_discrete)
Expand All @@ -91,22 +89,22 @@ def __attrs_post_init__(self):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class PoissonPoint(fwdpy11._fwdpy11._ll_PoissonPoint):
"""
Generate a recombination breakpoint at a fixed position if the
number of crossover events is odd.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param position: The position of the crossover
:type position: int or float
:param mean: The mean number of breakpoints per meiosis
:type mean: float
:param discrete: If ``False``, positions are continuous and uniform from ``[beg, end)``.
If ``True``, positions take integer values uniformly from ``[beg, end)``.
:param discrete: If `False`, positions are continuous and uniform from `[beg, end)`.
If `True`, positions take integer values uniformly from `[beg, end)`.
:type discrete: bool

.. versionadded:: 0.3.0
Expand All @@ -126,7 +124,7 @@ class PoissonPoint(fwdpy11._fwdpy11._ll_PoissonPoint):

.. versionchanged:: 0.12.0

Added ``discrete`` option to initializer.
Added `discrete` option to initializer.
"""

position: typing.Union[int, float] = attr.ib(validator=_is_integer_if_discrete)
Expand All @@ -148,13 +146,13 @@ def __setstate__(self, d):

@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class BinomialInterval(fwdpy11._fwdpy11._ll_BinomialInterval):
"""
Generate exactly one crossover with a given probability

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param beg: The beginning of the region
Expand All @@ -163,8 +161,8 @@ class BinomialInterval(fwdpy11._fwdpy11._ll_BinomialInterval):
:type end: int or float
:param probability: The probability of a recombination (per meiosis).
:type probability: float
:param discrete: If ``False``, positions are continuous and uniform from ``[beg, end)``.
If ``True``, positions take integer values uniformly from ``[beg, end)``.
:param discrete: If `False`, positions are continuous and uniform from `[beg, end)`.
If `True`, positions take integer values uniformly from `[beg, end)`.
:type discrete: bool

.. versionadded:: 0.5.2
Expand All @@ -180,7 +178,7 @@ class BinomialInterval(fwdpy11._fwdpy11._ll_BinomialInterval):

.. versionchanged:: 0.12.0

Added ``discrete`` option to initializer.
Added `discrete` option to initializer.
"""

beg: typing.Union[int, float] = attr.ib(validator=_is_integer_if_discrete)
Expand All @@ -200,23 +198,23 @@ def __attrs_post_init__(self):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class BinomialPoint(fwdpy11._fwdpy11._ll_BinomialPoint):
"""
Generate a crossover breakpoint at a fixed position with a
fixed probability. This class represents genetic distance
as centiMorgans/100.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param position: The beginning of the region
:type position: int or float
:param probability: The probability of a recombination (per meiosis).
:type probability: float
:param discrete: If ``False``, positions are continuous and uniform from ``[beg, end)``.
If ``True``, positions take integer values uniformly from ``[beg, end)``.
:param discrete: If `False`, positions are continuous and uniform from `[beg, end)`.
If `True`, positions take integer values uniformly from `[beg, end)`.
:type discrete: bool

.. versionadded:: 0.3.0
Expand All @@ -236,7 +234,7 @@ class BinomialPoint(fwdpy11._fwdpy11._ll_BinomialPoint):

.. versionchanged:: 0.12.0

Added ``discrete`` option to initializer.
Added `discrete` option to initializer.
"""

position: typing.Union[int, float] = attr.ib(validator=_is_integer_if_discrete)
Expand All @@ -252,13 +250,13 @@ def __attrs_post_init__(self):
@attr_add_asblack
@attr_class_pickle_with_super
@attr_class_to_from_dict
@attr.s(**_common_attr_attribs)
@attr.s(auto_attribs=True, frozen=True, repr_ns="fwdpy11")
class FixedCrossovers(fwdpy11._fwdpy11._ll_FixedCrossovers):
"""
Generate a fixed number of crossover breakpoints.

This class has the following attributes, whose names
are also ``kwargs`` for intitialization. The attribute names
are also `kwargs` for intitialization. The attribute names
also determine the order of positional arguments:

:param beg: The beginning of the region
Expand All @@ -267,8 +265,8 @@ class FixedCrossovers(fwdpy11._fwdpy11._ll_FixedCrossovers):
:type end: int or float
:param num_xovers: The number of breakpoints per meiosis
:type num_xovers: float
:param discrete: If ``False``, positions are continuous and uniform from ``[beg, end)``.
If ``True``, positions take integer values uniformly from ``[beg, end)``.
:param discrete: If `False`, positions are continuous and uniform from `[beg, end)`.
If `True`, positions take integer values uniformly from `[beg, end)`.
:type discrete: bool

.. versionadded:: 0.3.0
Expand All @@ -288,7 +286,7 @@ class FixedCrossovers(fwdpy11._fwdpy11._ll_FixedCrossovers):

.. versionchanged:: 0.12.0

Added ``discrete`` option to initializer.
Added `discrete` option to initializer.
"""

beg: typing.Union[int, float] = attr.ib(validator=_is_integer_if_discrete)
Expand Down