Skip to content

DemesUtil

slice(g, t)

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

Parameters:

Name Type Description Default
g DemeGraph

The input resolved Demes graph.

required
t float

The time in the past at which to slice the graph.

required
Source code in dadi/Demes/DemesUtil.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def slice(g, t):
    """
    Slice a Demes graph at a given time, return the top portion with times shifted
    to the slice time.

    Args:
        g (demes.DemeGraph): The input resolved Demes graph.
        t (float): 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()

swipe(g, t)

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.

Parameters:

Name Type Description Default
g DemeGraph

The input demes graph object.

required
t float

The time at which to erase preceding demographic events.

required
Source code in dadi/Demes/DemesUtil.py
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
def swipe(g, t):
    """
    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.

    Args:
        g (demes.DemeGraph): The input demes graph object.
        t (float): 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()