Module dadi.Demes.DemesUtil

Functions

def slice(g, t)
Expand source code
def slice(g, t):
    """
    Slice a Demes graph at a given time, return the top portion with times shifted
    to the slice time.

    :param g: The input resolved Demes graph.
    :param t: The time in the past at which to slice the graph.
    """
    if t < 0:
        raise ValueError("Slice time must be positive")
    if t == math.inf:
        raise ValueError("Slice time must be finite")
    if t == 0:
        return g
    else:
        # initialize new builder object
        gdict = g.asdict()
        b = dict(time_units=gdict["time_units"])
        if gdict["time_units"] != "generations":
            b["generation_time"] = gdict["generation_time"]
        # add demes with shifted times
        b["demes"] = []
        for d in gdict["demes"]:
            if d["start_time"] <= t:
                continue
            else:
                d = _shift_deme_time(d, t)
                b["demes"].append(d)
        # adjust times of any pulses
        if "pulses" in gdict:
            b["pulses"] = []
            for p in gdict["pulses"]:
                if p["time"] <= t:
                    continue
                p["time"] -= t
                b["pulses"].append(p)
        # adjust times of any migrations
        if "migrations" in gdict:
            b["migrations"] = []
            for m in gdict["migrations"]:
                if m["start_time"] <= t:
                    continue
                m["start_time"] -= t
                m["end_time"] = max(0, m["end_time"] - t)
                b["migrations"].append(m)
        b = demes.Builder.fromdict(b)
        return b.resolve()

Slice a Demes graph at a given time, return the top portion with times shifted to the slice time.

:param g: The input resolved Demes graph. :param t: The time in the past at which to slice the graph.

def swipe(g, t)
Expand source code
def swipe(g, t):
    """
    ..note::
        Don't really like this function name... any suggestions?

    Returns a new demes graph with demography about the given time removed.
    Demes that existed before that time are removed, and demes that overlap
    with that time are given a constant size equal to their size at that time
    extending into the past.

    :param g: The input demes graph object.
    :param t: The time at which to erase preceding demographic events.
    """
    if t <= 0:
        raise ValueError("Slice time must be positive")
    if t == math.inf:
        raise ValueError("Slice time must be finite")

    gdict = g.asdict()
    b = dict(time_units=gdict["time_units"])
    if gdict["time_units"] != "generations":
        b["generation_time"] = gdict["generation_time"]
    b["demes"] = []
    for d in gdict["demes"]:
        if d["epochs"][-1]["end_time"] > t:
            # skip demes that existed entirely before slice time
            continue
        elif d["start_time"] <= t:
            # add demes that started at slice time or more recent
            b["demes"].append(d)
        else:
            # start time is greater than slice time, and end time
            # is equal or more recent than the slice time
            d_new = _get_sliced_deme(d, t)
            b["demes"].append(d_new)
    if "migrations" in gdict:
        b["migrations"] = []
        for m in gdict["migrations"]:
            if m["end_time"] >= t:
                continue
            elif m["start_time"] <= t:
                b["migrations"].append(m)
            else:
                m["start_time"] = t
                b["migrations"].append(m)
    if "pulses" in gdict:
        b["pulses"] = []
        for p in gdict["pulses"]:
            if p["time"] >= t:
                continue
            else:
                b["pulses"].append(p)
    b = demes.Builder.fromdict(b)
    return b.resolve()

Note

Don't really like this function name… any suggestions?

Returns a new demes graph with demography about the given time removed. Demes that existed before that time are removed, and demes that overlap with that time are given a constant size equal to their size at that time extending into the past.

:param g: The input demes graph object. :param t: The time at which to erase preceding demographic events.