Skip to content

Commit

Permalink
reorg code
Browse files Browse the repository at this point in the history
  • Loading branch information
molpopgen committed Mar 15, 2023
1 parent 4793d76 commit cd71c31
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 38 deletions.
36 changes: 0 additions & 36 deletions fwdpy11/_functions/import_demes.py
@@ -1,4 +1,3 @@
import decimal
from typing import Dict, Optional, Union

import demes
Expand All @@ -8,12 +7,6 @@
from fwdpy11._types.forward_demes_graph import ForwardDemesGraph


def _round_via_decimal(value):
with decimal.localcontext() as ctx:
ctx.rounding = decimal.ROUND_HALF_UP
return int(decimal.Decimal(value).to_integral_value())


def demography_from_demes(
dg: Union[str, demes.Graph], burnin: int,
round_non_integer_sizes=Optional[bool],
Expand Down Expand Up @@ -98,32 +91,3 @@ def _build_deme_id_to_int_map(dg: demes.Graph) -> Dict:
temp = sorted(temp, key=lambda x: -x[0])

return {j[1]: i for i, j in enumerate(temp)}


def __get_most_ancient_deme_start_time(dg: demes.Graph) -> demes.demes.Time:
return max([d.start_time for d in dg.demes])


def _get_ancestral_population_size(dg: demes.Graph) -> int:
"""
Need this for the burnin time.
If there are > 1 demes with the same most ancient start_time,
then the ancestral size is considered to be the size
of all those demes (size of ancestral metapopulation).
"""
oldest_deme_time = __get_most_ancient_deme_start_time(dg)

rv = sum(
[
_round_via_decimal(e.start_size)
for d in dg.demes
for e in d.epochs
if e.start_time == oldest_deme_time
]
)
if rv == 0:
raise ValueError(
"could not determinine ancestral metapopulation size")
raise RuntimeError("think about removing this fn")
return rv
36 changes: 34 additions & 2 deletions fwdpy11/_types/forward_demes_graph.py
@@ -1,3 +1,4 @@
import decimal
import typing

import attr
Expand All @@ -12,6 +13,12 @@
import fwdpy11


def _round_via_decimal(value):
with decimal.localcontext() as ctx:
ctx.rounding = decimal.ROUND_HALF_UP
return int(decimal.Decimal(value).to_integral_value())


@attr.s(repr_ns="fwdpy11")
@attr_class_pickle_with_super
@attr_class_to_from_dict
Expand Down Expand Up @@ -46,9 +53,8 @@ class ForwardDemesGraph(fwdpy11._fwdpy11._ForwardDemesGraph):
burnin_generation: int

def __attrs_post_init__(self):
from fwdpy11._functions.import_demes import _get_ancestral_population_size
self.graph = demes.loads(self.yaml)
Nref = _get_ancestral_population_size(self.graph)
Nref = self._get_ancestral_population_size(self.graph)
assert np.modf(Nref)[0] == 0.0
if self.burnin_is_exact is True:
burnin = self.burnin
Expand All @@ -60,6 +66,32 @@ def __attrs_post_init__(self):
x = self._sum_deme_sizes_at_time_zero()
assert Nref == x, f"{Nref}, {x}, {self.yaml}"

def __get_most_ancient_deme_start_time(self, dg: demes.Graph) -> demes.demes.Time:
return max([d.start_time for d in dg.demes])

def _get_ancestral_population_size(self, dg: demes.Graph) -> int:
"""
Need this for the burnin time.
If there are > 1 demes with the same most ancient start_time,
then the ancestral size is considered to be the size
of all those demes (size of ancestral metapopulation).
"""
oldest_deme_time = self.__get_most_ancient_deme_start_time(dg)

rv = sum(
[
_round_via_decimal(e.start_size)
for d in dg.demes
for e in d.epochs
if e.start_time == oldest_deme_time
]
)
if rv == 0:
raise ValueError(
"could not determinine ancestral metapopulation size")
return rv

def number_of_demes(self) -> int:
return len(self.graph.demes)

Expand Down

0 comments on commit cd71c31

Please sign in to comment.