Skip to content

PhiManip

Module for manipulating population frequency spectra phi, such as population splittings and admixture.

check_xx(xx)

Check whether the input xx is monotonically increasing from 0 to 1.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies.

required

Raises:

Type Description
ValueError

If xx does not start at 0, end at 1, or is not monotonically increasing.

Source code in dadi/PhiManip.py
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
def check_xx(xx):
    """
    Check whether the input xx is monotonically increasing from 0 to 1.

    Args:
        xx (array): One-dimensional grid of frequencies.

    Raises:
        ValueError: If xx does not start at 0, end at 1, or is not monotonically increasing.
    """
    if not xx[0] == 0 and xx[1] == 1:
        raise ValueError('Input xx argument does not run from 0 to 1.'
                         'Have you passed in an incorrect argument?')
    if not numpy.all(numpy.diff(xx) >= 0):
        raise ValueError('Input xx argument is not monotonically increasing. '
                         'Have you passed in an incorrect argument?')

filter_pops(phi, xx, tokeep)

Filter phi to keep only certain populations.

Returns new phi with len(tokeep) populations.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original populations.

required
xx array

Mapping of points in phi to frequencies in population to be removed.

required
tokeep list

List of population numbers to keep, numbering from 1.

required

Returns:

Name Type Description
phi array

The updated phi array with only the specified populations.

Source code in dadi/PhiManip.py
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
def filter_pops(phi, xx, tokeep):
    """
    Filter phi to keep only certain populations.

    Returns new phi with len(tokeep) populations.

    Args:
        phi (array): Phi corresponding to original populations.
        xx (array): Mapping of points in phi to frequencies in population to be removed.
        tokeep (list): List of population numbers to keep, numbering from 1.

    Returns:
        phi (array): The updated phi array with only the specified populations.
    """
    toremove = list(range(1, phi.ndim+1))
    for pop_ii in tokeep:
        toremove.remove(pop_ii)
    for pop_ii in sorted(toremove)[::-1]:
        phi = remove_pop(phi, xx, pop_ii)
    return phi

phi_1D(xx, nu=1.0, theta0=1.0, gamma=0, h=0.5, theta=None, beta=1, deme_ids=None)

Compute a one-dimensional phi for a constant-sized population with genic selection.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
nu float

Size of this population, relative to the reference population size Nref.

1.0
theta0 float

Scaled mutation rate, equal to 4*Nref * u, where u is the mutation event rate per generation for the simulated locus and Nref is the reference population size.

1.0
gamma float

Scaled selection coefficient, equal to 2*Nref * s, where s is the selective advantage.

0
h float

Dominance coefficient. If A is the selected allele, the aa has fitness 1, aA has fitness 1+2sh, and AA has fitness 1+2s. h = 0.5 corresponds to genic selection.

0.5
theta float

Deprecated in favor of distinct nu and theta0 arguments, for consistency with Integration functions.

None
beta float

Scaling factor for selection.

1
deme_ids list

Sequence of strings representing the names of demes.

None

Returns:

Name Type Description
phi array

A new phi array.

Source code in dadi/PhiManip.py
 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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
def phi_1D(xx, nu=1.0, theta0=1.0, gamma=0, h=0.5, theta=None, beta=1, deme_ids=None):
    """
    Compute a one-dimensional phi for a constant-sized population with genic selection.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        nu (float): Size of this population, relative to the reference population size Nref.
        theta0 (float): Scaled mutation rate, equal to 4*Nref * u, where u is the mutation 
            event rate per generation for the simulated locus and Nref is the reference 
            population size.
        gamma (float): Scaled selection coefficient, equal to 2*Nref * s, where s is the
            selective advantage.
        h (float): Dominance coefficient. If A is the selected allele, the aa has fitness 1,
            aA has fitness 1+2sh, and AA has fitness 1+2s. h = 0.5 corresponds to genic selection.
        theta (float, optional): Deprecated in favor of distinct nu and theta0 arguments, for 
            consistency with Integration functions.
        beta (float): Scaling factor for selection.
        deme_ids (list, optional): Sequence of strings representing the names of demes.

    Returns:
        phi (array): A new phi array.
    """
    Demes.cache = [Demes.Initiation(nu, deme_ids=deme_ids)]

    if theta is not None:
        raise ValueError('The parameter theta has been deprecated in favor of '
                         'parameters nu and theta0, for consistency with the '
                         'Integration functions.')

    if h == 0.5:
        return phi_1D_genic(xx, nu, theta0, gamma, beta=beta)

    # Eqn 1 from Williamson, Fledel-Alon, Bustamante _Genetics_ 168:463 (2004).

    # Modified to incorporate fact that for beta != 1, we get a term of 
    # 4*beta/(beta+1)^2 in V. This can be implemented by rescaling gamma
    # and rescaling the final phi.
    gamma = gamma * 4.*beta/(beta+1.)**2

    # Our final result is of the form 
    # exp(Q) * int_0_x exp(-Q) / int_0_1 exp(-Q)

    # For large negative gamma, exp(-Q) becomes numerically infinite.
    # To work around this, we can adjust Q in both the top and bottom
    # integrals by the same factor. We choose to make that factor the 
    # maximum of -Q, which is -2*gamma.
    Qadjust = 0
    # For negative gamma, the maximum of -Q is -2*gamma.
    if gamma < 0 and numpy.isinf(numpy.exp(-2*gamma)):
        Qadjust = -2*gamma

    # For large positive gamma, the prefactor exp(Q) becomes numerically
    # infinite, while the numerator becomes very small. To work around this,
    # we'll can pull the prefactor into the numerator integral.

    # Evaluate the denominator integral.
    integrand = lambda xi: numpy.exp(-4*gamma*h*xi - 2*gamma*(1-2*h)*xi**2
                                     - Qadjust)
    int0, eps = scipy.integrate.quad(integrand, 0, 1, epsabs=0,
                                     points=numpy.linspace(0,1,41))

    ints = numpy.empty(len(xx))
    # Evaluate the numerator integrals
    if gamma < 0:
        # In this case, the prefactor is not divergent, so we can evaluate
        # the numerator as before, using the Qadjust if necessary.
        for ii,q in enumerate(xx):
            val, eps = scipy.integrate.quad(integrand, q, 1, epsabs=0,
                                            points=numpy.linspace(q,1,41))
            ints[ii] = val
        phi = numpy.exp(4*gamma*h*xx + 2*gamma*(1-2*h)*xx**2)*ints/int0
    else:
        # In this case, the prefactor may be divergent, so we do the integral
        # with the prefactor pulled inside
        integrand = lambda xi, q: numpy.exp(-4*gamma*h*(xi-q) -
                                            2*gamma*(1-2*h)*(xi**2-q**2))
        for ii,q in enumerate(xx):
            val, eps = scipy.integrate.quad(integrand, q, 1, args=(q,))
            ints[ii] = val
        phi = ints/int0

    # Protect from division by zero errors
    phi[1:-1] *= 1./(xx[1:-1]*(1-xx[1:-1]))
    # Technically, phi diverges at 0. This kludge lets us do numerics
    # sensibly.
    phi[0] = phi[1]
    # I used Mathematica to calculate the proper limit for x goes to 1.
    # But if we've adjusted the denominator integrand, then that limit doesn't
    # hold. We only need to do that in cases of strong negative selection,
    # when phi near 1 should be almost zero anyways. So we'll just ensure
    # that it is at least monotonically decreasing.
    if Qadjust == 0:
        phi[-1] = 1./int0
    else:
        phi[-1] = min(phi[-1], phi[-2])

    return phi * nu*theta0 * 4.*beta/(beta+1.)**2

phi_1D_X(xx, nu=1.0, theta0=1.0, gamma=0, h=0.5, beta=1, alpha=1)

Compute a one-dimensional phi for a constant-sized population with genic selection.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
nu float

Size of this population, relative to the reference population size Nref.

1.0
theta0 float

Scaled mutation rate, equal to 4*Nref * u, where u is the mutation event rate per generation for the simulated locus and Nref is the reference population size.

1.0
gamma float

Scaled selection coefficient, equal to 2*Nref * s, where s is the selective advantage.

0
h float

Dominance coefficient. If A is the selected allele, the aa has fitness 1, aA has fitness 1+2sh, and AA has fitness 1+2s. Male carriers have fitness 1+2s. h = 0.5 corresponds to genic selection.

0.5
beta float

Scaling factor for selection.

1
alpha float

Additional scaling factor for selection.

1

Returns:

Name Type Description
phi array

A new phi array.

Source code in dadi/PhiManip.py
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
def phi_1D_X(xx, nu=1.0, theta0=1.0, gamma=0, h=0.5, beta=1, alpha=1):
    """
    Compute a one-dimensional phi for a constant-sized population with genic selection.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        nu (float): Size of this population, relative to the reference population size Nref.
        theta0 (float): Scaled mutation rate, equal to 4*Nref * u, where u is the mutation 
            event rate per generation for the simulated locus and Nref is the reference 
            population size.
        gamma (float): Scaled selection coefficient, equal to 2*Nref * s, where s is the
            selective advantage.
        h (float): Dominance coefficient. If A is the selected allele, the aa has fitness 1,
            aA has fitness 1+2sh, and AA has fitness 1+2s. Male carriers have fitness 1+2s.
            h = 0.5 corresponds to genic selection.
        beta (float): Scaling factor for selection.
        alpha (float): Additional scaling factor for selection.

    Returns:
        phi (array): A new phi array.
    """
    Kv = (2.*beta+4.)*(beta+1.)/(9.*beta)
    Km1 = 4./3. * gamma*(0.5+h)
    Km2 = 4./3.*gamma*(1.-2.*h)
    g1 = Km1/Kv
    g2 = Km2/Kv

    # First we evaluate the relevant integrals.
    ints = numpy.empty(len(xx))
    integrand = lambda xi: numpy.exp(-2*g1*xi - g2*xi**2)
    val, eps = scipy.integrate.quad(integrand, 0, 1)
    int0 = val
    for ii,q in enumerate(xx):
        val, eps = scipy.integrate.quad(integrand, q, 1)
        ints[ii] = val

    phi = numpy.exp(2*g1*xx + g2*xx**2)*ints/int0
    # Protect from division by zero errors
    if xx[0] == 0 and xx[-1] == 1:
        phi[1:-1] *= 1./(xx[1:-1]*(1-xx[1:-1]))
    else:
        phi *= 1./(xx*(1-xx))

    if xx[0] == 0:
        # Technically, phi diverges at 0. This fixes lets us do numerics
        # sensibly.
        phi[0] = phi[1]
    if xx[-1] == 1:
        # I used Mathematica to check that this was the proper limit.
        phi[-1] = 1./int0
    return phi * nu*theta0 * 1./Kv * 2./(1.+2.*beta)*(1./(1.+alpha) + beta)

phi_1D_genic(xx, nu=1.0, theta0=1.0, gamma=0, theta=None, beta=1)

Compute a one-dimensional phi for a constant-sized population with genic selection.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
nu float

Size of this population, relative to the reference population size Nref.

1.0
theta0 float

Scaled mutation rate, equal to 4*Nref * u, where u is the mutation event rate per generation for the simulated locus and Nref is the reference population size.

1.0
gamma float

Scaled selection coefficient, equal to 2*Nref * s, where s is the selective advantage.

0
theta float

Deprecated in favor of distinct nu and theta0 arguments, for consistency with Integration functions.

None
beta float

Scaling factor for selection.

1

Returns:

Name Type Description
phi array

A new phi array.

Source code in dadi/PhiManip.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
def phi_1D_genic(xx, nu=1.0, theta0=1.0, gamma=0, theta=None, beta=1):
    """
    Compute a one-dimensional phi for a constant-sized population with genic selection.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        nu (float): Size of this population, relative to the reference population size Nref.
        theta0 (float): Scaled mutation rate, equal to 4*Nref * u, where u is the mutation 
            event rate per generation for the simulated locus and Nref is the reference 
            population size.
        gamma (float): Scaled selection coefficient, equal to 2*Nref * s, where s is the
            selective advantage.
        theta (float, optional): Deprecated in favor of distinct nu and theta0 arguments, for 
            consistency with Integration functions.
        beta (float): Scaling factor for selection.

    Returns:
        phi (array): A new phi array.
    """

    if theta is not None:
        raise ValueError('The parameter theta has been deprecated in favor of '
                         'parameters nu and theta0, for consistency with the '
                         'Integration functions.')
    if gamma == 0:
        return phi_1D_snm(xx, nu, theta0, beta=beta)

    # Beta effectively re-scales gamma.
    gamma = gamma * 4.*beta/(beta+1.)**2

    exp = numpy.exp
    # Protect from warnings on division by zero
    if xx[0] == 0 and xx[-1] == 1:
        phi = 0*xx
        if gamma > -300:
            phi[1:-1] = 1./(xx[1:-1]*(1-xx[1:-1]))\
                    * (1-exp(-2*gamma*(1-xx[1:-1])))/(1-exp(-2*gamma))
        else:
            # Avoid overflow issues for very negative gammas
            phi[1:-1] = 1./(xx[1:-1]*(1-xx[1:-1])) * exp(2*gamma*xx[1:-1])
    else:
        if gamma > -300:
            phi = 1./(xx*(1-xx)) * (1-exp(-2*gamma*(1-xx)))/(1-exp(-2*gamma))
        else:
            phi = 1./(xx*(1-xx)) * exp(2*gamma*xx)

    if xx[0] == 0:
        phi[0] = phi[1]
    if xx[-1] == 1:
        if gamma < 300:
            limit = 2*gamma * exp(2*gamma)/(exp(2*gamma)-1)
        else:
            limit = 2*gamma
        phi[-1] = limit
    return phi * nu*theta0 * 4.*beta/(beta+1.)**2

phi_1D_snm(xx, nu=1.0, theta0=1.0, theta=None, beta=1)

Compute the standard neutral one-dimensional probability density.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
nu float

Size of this population, relative to the reference population size Nref.

1.0
theta0 float

Scaled mutation rate, equal to 4*Nref * u, where u is the mutation event rate per generation for the simulated locus and Nref is the reference population size.

1.0
theta float

Deprecated in favor of distinct nu and theta0 arguments, for consistency with Integration functions.

None
beta float

Scaling factor for selection.

1

Returns:

Name Type Description
phi array

A new phi array.

Source code in dadi/PhiManip.py
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
def phi_1D_snm(xx, nu=1.0, theta0=1.0, theta=None, beta=1):
    """
    Compute the standard neutral one-dimensional probability density.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        nu (float): Size of this population, relative to the reference population size Nref.
        theta0 (float): Scaled mutation rate, equal to 4*Nref * u, where u is the mutation 
            event rate per generation for the simulated locus and Nref is the reference 
            population size.
        theta (float, optional): Deprecated in favor of distinct nu and theta0 arguments, for 
            consistency with Integration functions.
        beta (float): Scaling factor for selection.

    Returns:
        phi (array): A new phi array.
    """

    if theta is not None:
        raise ValueError('The parameter theta has been deprecated in favor of '
                         'parameters nu and theta0, for consistency with the '
                         'Integration functions.')
    # Protect from division by zero errors
    if xx[0] == 0:
        phi = 0*xx
        phi[1:] = nu*theta0/xx[1:]
        phi[0] = phi[1]
    else:
        phi = nu*theta0/xx
    return phi * 4.*beta/(beta+1.)**2

phi_1D_to_2D(xx, phi_1D, deme_ids=None)

Implement a one-to-two population split.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
phi_1D array

Initial probability density.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_2D array

A new two-dimensional phi array.

Source code in dadi/PhiManip.py
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
def phi_1D_to_2D(xx, phi_1D, deme_ids=None):
    """
    Implement a one-to-two population split.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        phi_1D (array): Initial probability density.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_2D (array): A new two-dimensional phi array.
    """
    check_xx(xx)

    Demes.cache.append(Demes.Split(proportions=[1], deme_ids=deme_ids))

    pts = len(xx)
    phi_2D = numpy.zeros((pts, pts))
    for ii in range(1, pts-1):
        phi_2D[ii,ii] = phi_1D[ii] * 2/(xx[ii+1]-xx[ii-1])
    return phi_2D

phi_2D_admix_1_into_2(phi, f, xx, yy)

Admix population 1 into population 2.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 2 populations.

required
f float

Fraction of updated population 2 to be derived from population 1. A fraction 1-f will be derived from the original population 2.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
def phi_2D_admix_1_into_2(phi, f, xx,yy):
    """
    Admix population 1 into population 2.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 2 populations.
        f (float): Fraction of updated population 2 to be derived from population 1. 
            A fraction 1-f will be derived from the original population 2.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.

    Returns:
        phi (array): The updated phi array.
    """
    # This is just like the the split_admix situation, but we're splitting into
    # a population with zz=yy. We could do this by creating a xx by yy by yy
    # array, then integrating out the second population. That's a big waste of
    # memory, however.
    Demes.cache.append(Demes.Pulse(sources=[1], dest=2, proportions=[f]))
    lower_z_index, upper_z_index, frac_lower, frac_upper, norm \
            = _two_pop_admixture_intermediates(phi, f, xx,yy,yy)

    # Basically, we're splitting into a third zz population, then integrating
    # over yy to be left with the two populations we care about.
    lower_cont = frac_lower*norm
    upper_cont = frac_upper*norm
    idx_j = numpy.arange(phi.shape[1])
    for ii in range(phi.shape[0]):
        phi_int = numpy.zeros((phi.shape[1], phi.shape[1]))
        # Use fancy indexing to avoid the commented out loop.
        #for jj in range(len(yy)):
        #    phi_int[jj, upper_z_index[ii,jj]] += frac_upper[ii,jj]*norm[ii,jj]
        #    phi_int[jj, lower_z_index[ii,jj]] += frac_lower[ii,jj]*norm[ii,jj]
        phi_int[idx_j, lower_z_index[ii]] = lower_cont[ii]
        phi_int[idx_j, upper_z_index[ii]] += upper_cont[ii]
        phi[ii] = Numerics.trapz(phi_int, yy, axis=0)

    return phi

phi_2D_admix_2_into_1(phi, f, xx, yy)

Admix population 2 into population 1.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 2 populations.

required
f float

Fraction of updated population 1 to be derived from population 2. A fraction 1-f will be derived from the original population 1.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
def phi_2D_admix_2_into_1(phi, f, xx,yy):
    """
    Admix population 2 into population 1.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 2 populations.
        f (float): Fraction of updated population 1 to be derived from population 2. 
            A fraction 1-f will be derived from the original population 1.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.

    Returns:
        phi (array): The updated phi array.
    """
    # Note that it's 1-f here since f now denotes the fraction coming from
    # population 2.
    Demes.cache.append(Demes.Pulse(sources=[2], dest=1, proportions=[f]))
    lower_z_index, upper_z_index, frac_lower, frac_upper, norm \
            = _two_pop_admixture_intermediates(phi, 1-f, xx,yy,xx)

    idx_i = numpy.arange(phi.shape[0])
    lower_cont = frac_lower*norm
    upper_cont = frac_upper*norm
    for jj in range(len(yy)):
        phi_int = numpy.zeros((len(xx), len(xx)))
        phi_int[idx_i, lower_z_index[:,jj]] = lower_cont[:,jj]
        phi_int[idx_i, upper_z_index[:,jj]] = upper_cont[:,jj]
        phi[:,jj] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_2D_to_3D_admix(phi, f1, xx, yy, zz, deme_ids=None)

Create population 3 admixed from populations 1 and 2.

Returns a 3D sfs of shape (len(xx),len(yy),len(zz))

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 2 populations.

required
f1 float

Fraction of population 3 derived from population 1. A fraction 1-f1 will be derived from population 2.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Frequency mapping that will be used along population 3 axis.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_3D array

A new three-dimensional phi array.

Source code in dadi/PhiManip.py
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
def phi_2D_to_3D_admix(phi, f1, xx,yy,zz, deme_ids=None):
    """
    Create population 3 admixed from populations 1 and 2.

    Returns a 3D sfs of shape (len(xx),len(yy),len(zz))

    Args:
        phi (array): Phi corresponding to original 2 populations.
        f1 (float): Fraction of population 3 derived from population 1. A fraction 1-f1
            will be derived from population 2.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Frequency mapping that will be used along population 3 axis.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_3D (array): A new three-dimensional phi array.
    """
    Demes.cache.append(Demes.Split(proportions=[f1, 1-f1], deme_ids=deme_ids))

    lower_z_index, upper_z_index, frac_lower, frac_upper, norm \
            = _two_pop_admixture_intermediates(phi, f1, xx,yy,zz)


    # Assemble our result.
    # This uses numpy's fancy indexing. It is much, much faster than an
    # explicit loop.
    # See the numpy-discussion post "Numpy Advanced Indexing Question" by
    # Robert Kern on July 16, 2008
    # http://projects.scipy.org/pipermail/numpy-discussion/2008-July/035776.html
    idx_i = numpy.arange(len(xx))[:,numpy.newaxis]
    idx_j = numpy.arange(len(yy))[numpy.newaxis,:]

    phi_3D = numpy.zeros((len(xx), len(yy), len(zz)))
    phi_3D[idx_i, idx_j, lower_z_index] = frac_lower*norm
    phi_3D[idx_i, idx_j, upper_z_index] += frac_upper*norm

    return phi_3D

phi_2D_to_3D_split_1(xx, phi_2D, deme_ids=None)

Split population 1 into populations 1 and 3.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
phi_2D array

Initial probability density.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_2D_to_3D_admix array

A new three-dimensional phi array.

Source code in dadi/PhiManip.py
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def phi_2D_to_3D_split_1(xx, phi_2D, deme_ids=None):
    """
    Split population 1 into populations 1 and 3.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        phi_2D (array): Initial probability density.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_2D_to_3D_admix (array): A new three-dimensional phi array.
    """
    check_xx(xx)

    return phi_2D_to_3D_admix(phi_2D,1,xx,xx,xx, deme_ids)

phi_2D_to_3D_split_2(xx, phi_2D, deme_ids=None)

Split population 2 into populations 2 and 3.

Parameters:

Name Type Description Default
xx array

One-dimensional grid of frequencies upon which phi is defined.

required
phi_2D array

Initial probability density.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_2D_to_3D_admix array

A new three-dimensional phi array.

Source code in dadi/PhiManip.py
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
def phi_2D_to_3D_split_2(xx, phi_2D, deme_ids=None):
    """
    Split population 2 into populations 2 and 3.

    Args:
        xx (array): One-dimensional grid of frequencies upon which phi is defined.
        phi_2D (array): Initial probability density.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_2D_to_3D_admix (array): A new three-dimensional phi array.
    """
    check_xx(xx)

    return phi_2D_to_3D_admix(phi_2D,0,xx,xx,xx, deme_ids)

phi_3D_admix_1_and_2_into_3(phi, f1, f2, xx, yy, zz)

Admix populations 1 and 2 into population 3.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 3 populations.

required
f1 float

Fraction of updated population 3 to be derived from population 1.

required
f2 float

Fraction of updated population 3 to be derived from population 2. A fraction (1-f1-f2) will be derived from the original pop 3.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
def phi_3D_admix_1_and_2_into_3(phi, f1,f2, xx,yy,zz):
    """
    Admix populations 1 and 2 into population 3.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 3 populations.
        f1 (float): Fraction of updated population 3 to be derived from population 1. 
        f2 (float): Fraction of updated population 3 to be derived from population 2. 
            A fraction (1-f1-f2) will be derived from the original pop 3.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[1,2], dest=3, proportions=[f1,f2]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _three_pop_admixture_intermediates(phi, f1,f2, xx,yy,zz, zz)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    # Basically, we're splitting into a fourth ww population, then integrating
    # over zz to be left with the two populations we care about.
    idx_k = numpy.arange(phi.shape[2])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            phi_int = numpy.zeros((phi.shape[2], phi.shape[2]))
            phi_int[idx_k, lower_w_index[ii,jj]] = lower_cont[ii,jj]
            phi_int[idx_k, upper_w_index[ii,jj]] = upper_cont[ii,jj]
            phi[ii,jj] = Numerics.trapz(phi_int, zz, axis=0)

    return phi

phi_3D_admix_1_and_3_into_2(phi, f1, f3, xx, yy, zz)

Admix populations 1 and 3 into population 2.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 3 populations.

required
f1 float

Fraction of updated population 2 to be derived from population 1.

required
f3 float

Fraction of updated population 2 to be derived from population 3. A fraction (1-f1-f3) will be derived from the original pop 2.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
def phi_3D_admix_1_and_3_into_2(phi, f1,f3, xx,yy,zz):
    """
    Admix populations 1 and 3 into population 2.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 3 populations.
        f1 (float): Fraction of updated population 2 to be derived from population 1. 
        f3 (float): Fraction of updated population 2 to be derived from population 3. 
            A fraction (1-f1-f3) will be derived from the original pop 2.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[1,3], dest=2, proportions=[f1,f3]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _three_pop_admixture_intermediates(phi, f1,1-f1-f3, xx,yy,zz, yy)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    # Basically, we're splitting into a fourth ww population, then integrating
    # over yy to be left with the two populations we care about.
    idx_j = numpy.arange(phi.shape[1])
    for ii in range(phi.shape[0]):
        for kk in range(phi.shape[2]):
            phi_int = numpy.zeros((phi.shape[1], phi.shape[1]))
            phi_int[idx_j, lower_w_index[ii,:,kk]] = lower_cont[ii,:,kk]
            phi_int[idx_j, upper_w_index[ii,:,kk]] = upper_cont[ii,:,kk]
            phi[ii,:,kk] = Numerics.trapz(phi_int, yy, axis=0)

    return phi

phi_3D_admix_2_and_3_into_1(phi, f2, f3, xx, yy, zz)

Admix populations 2 and 3 into population 1.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 3 populations.

required
f2 float

Fraction of updated population 1 to be derived from population 2.

required
f3 float

Fraction of updated population 1 to be derived from population 3. A fraction (1-f2-f3) will be derived from the original pop 1.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
def phi_3D_admix_2_and_3_into_1(phi, f2,f3, xx,yy,zz):
    """
    Admix populations 2 and 3 into population 1.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 3 populations.
        f2 (float): Fraction of updated population 1 to be derived from population 2. 
        f3 (float): Fraction of updated population 1 to be derived from population 3. 
            A fraction (1-f2-f3) will be derived from the original pop 1.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[2,3], dest=1, proportions=[f2,f3]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _three_pop_admixture_intermediates(phi, 1-f2-f3,f2, xx,yy,zz, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    # Basically, we're splitting into a fourth ww population, then integrating
    # over yy to be left with the two populations we care about.
    idx_i = numpy.arange(phi.shape[0])
    for jj in range(phi.shape[1]):
        for kk in range(phi.shape[2]):
            phi_int = numpy.zeros((phi.shape[0], phi.shape[0]))
            phi_int[idx_i, lower_w_index[:,jj,kk]] = lower_cont[:,jj,kk]
            phi_int[idx_i, upper_w_index[:,jj,kk]] = upper_cont[:,jj,kk]
            phi[:,jj,kk] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_3D_to_4D(phi, f1, f2, xx, yy, zz, aa, deme_ids=None)

Create population 4 from populations 1, 2, and 3.

Returns a 4D sfs of shape (len(xx),len(yy),len(zz),len(aa))

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 3 populations.

required
f1 float

Fraction of population 4 derived from population 1.

required
f2 float

Fraction of population 4 derived from population 2. A fraction 1-f1-f2 will be derived from population 3.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Frequency mapping that will be used along population 4 axis.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_4D array

A new four-dimensional phi array.

Source code in dadi/PhiManip.py
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
def phi_3D_to_4D(phi, f1,f2, xx,yy,zz,aa, deme_ids=None):
    """
    Create population 4 from populations 1, 2, and 3.

    Returns a 4D sfs of shape (len(xx),len(yy),len(zz),len(aa))

    Args:
        phi (array): Phi corresponding to original 3 populations.
        f1 (float): Fraction of population 4 derived from population 1.
        f2 (float): Fraction of population 4 derived from population 2.
            A fraction 1-f1-f2 will be derived from population 3.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Frequency mapping that will be used along population 4 axis.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_4D (array): A new four-dimensional phi array.
    """
    Demes.cache.append(Demes.Split(proportions=[f1, f2, 1-f1-f2], deme_ids=deme_ids))

    lower_z_index, upper_z_index, frac_lower, frac_upper, norm \
            = _three_pop_admixture_intermediates(phi, f1, f2, xx,yy,zz, aa)

    # Assemble our result.
    # This uses numpy's fancy indexing. It is much, much faster than an
    # explicit loop.
    # See the numpy-discussion post "Numpy Advanced Indexing Question" by
    # Robert Kern on July 16, 2008
    # http://projects.scipy.org/pipermail/numpy-discussion/2008-July/035776.html
    idx_i = numpy.arange(len(xx))[:,nuax,nuax]
    idx_j = numpy.arange(len(yy))[nuax,:,nuax]
    idx_k = numpy.arange(len(zz))[nuax,nuax,:]

    phi_4D = numpy.zeros((len(xx), len(yy), len(zz), len(aa)))
    phi_4D[idx_i, idx_j, idx_k, lower_z_index] = frac_lower*norm
    phi_4D[idx_i, idx_j, idx_k, upper_z_index] += frac_upper*norm

    return phi_4D

phi_4D_admix_into_1(phi, f2, f3, f4, xx, yy, zz, aa)

Admix populations 2, 3, and 4 into population 1.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 4 populations.

required
f2 float

Fraction of updated population 1 to be derived from population 2.

required
f3 float

Fraction of updated population 1 to be derived from population 3.

required
f4 float

Fraction of updated population 1 to be derived from population 4. A fraction (1-f2-f3-f4) will be derived from the original pop 1.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
def phi_4D_admix_into_1(phi, f2,f3,f4, xx,yy,zz,aa):
    """
    Admix populations 2, 3, and 4 into population 1.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 4 populations.
        f2 (float): Fraction of updated population 1 to be derived from population 2. 
        f3 (float): Fraction of updated population 1 to be derived from population 3. 
        f4 (float): Fraction of updated population 1 to be derived from population 4. 
            A fraction (1-f2-f3-f4) will be derived from the original pop 1.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[2,3,4], dest=1, proportions=[f2,f3,f4]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _four_pop_admixture_intermediates(phi, 1-f2-f3-f4,f2,f3, xx,yy,zz,aa, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    # Basically, we're splitting into a fifth bb population, then integrating
    # over xx to be left with the two populations we care about.
    idx_i = numpy.arange(phi.shape[0])
    for jj in range(phi.shape[1]):
        for kk in range(phi.shape[2]):
            for ll in range(phi.shape[3]):
                phi_int = numpy.zeros((phi.shape[0], phi.shape[0]))
                phi_int[idx_i, lower_w_index[:,jj,kk,ll]] = lower_cont[:,jj,kk,ll]
                phi_int[idx_i, upper_w_index[:,jj,kk,ll]] = upper_cont[:,jj,kk,ll]
                phi[:,jj,kk,ll] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_4D_admix_into_2(phi, f1, f3, f4, xx, yy, zz, aa)

Admix populations 1,3, and 4 into population 2.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 4 populations.

required
f1 float

Fraction of updated population 2 to be derived from population 1.

required
f3 float

Fraction of updated population 2 to be derived from population 3.

required
f4 float

Fraction of updated population 2 to be derived from population 4. A fraction (1-f1-f3-f4) will be derived from the original pop 2.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
def phi_4D_admix_into_2(phi, f1,f3,f4, xx,yy,zz,aa):
    """
    Admix populations 1,3, and 4 into population 2.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 4 populations.
        f1 (float): Fraction of updated population 2 to be derived from population 1. 
        f3 (float): Fraction of updated population 2 to be derived from population 3. 
        f4 (float): Fraction of updated population 2 to be derived from population 4. 
            A fraction (1-f1-f3-f4) will be derived from the original pop 2.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[1,3,4], dest=2, proportions=[f1, f3, f4]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _four_pop_admixture_intermediates(phi, f1,1-f1-f3-f4,f3, xx,yy,zz,aa, yy)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_j = numpy.arange(phi.shape[1])
    for ii in range(phi.shape[0]):
        for kk in range(phi.shape[2]):
            for ll in range(phi.shape[3]):
                phi_int = numpy.zeros((phi.shape[1], phi.shape[1]))
                phi_int[idx_j, lower_w_index[ii,:,kk,ll]] = lower_cont[ii,:,kk,ll]
                phi_int[idx_j, upper_w_index[ii,:,kk,ll]] = upper_cont[ii,:,kk,ll]
                phi[ii,:,kk,ll] = Numerics.trapz(phi_int, yy, axis=0)

    return phi

phi_4D_admix_into_3(phi, f1, f2, f4, xx, yy, zz, aa)

Admix populations 1,2, and 4 into population 3.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 4 populations.

required
f1 float

Fraction of updated population 3 to be derived from population 1.

required
f2 float

Fraction of updated population 3 to be derived from population 2.

required
f4 float

Fraction of updated population 3 to be derived from population 4. A fraction (1-f1-f2-f4) will be derived from the original pop 3.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
def phi_4D_admix_into_3(phi, f1,f2,f4, xx,yy,zz,aa):
    """
    Admix populations 1,2, and 4 into population 3.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 4 populations.
        f1 (float): Fraction of updated population 3 to be derived from population 1. 
        f2 (float): Fraction of updated population 3 to be derived from population 2. 
        f4 (float): Fraction of updated population 3 to be derived from population 4. 
            A fraction (1-f1-f2-f4) will be derived from the original pop 3.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[1,2,4], dest=3, proportions=[f1, f2, f4]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _four_pop_admixture_intermediates(phi, f1,f2,1-f1-f2-f4, xx,yy,zz,aa, yy)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_k = numpy.arange(phi.shape[2])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            for ll in range(phi.shape[3]):
                phi_int = numpy.zeros((phi.shape[2], phi.shape[2]))
                phi_int[idx_k, lower_w_index[ii,jj,:,ll]] = lower_cont[ii,jj,:,ll]
                phi_int[idx_k, upper_w_index[ii,jj,:,ll]] = upper_cont[ii,jj,:,ll]
                phi[ii,jj,:,ll] = Numerics.trapz(phi_int, zz, axis=0)

    return phi

phi_4D_admix_into_4(phi, f1, f2, f3, xx, yy, zz, aa)

Admix populations 1,2, and 3 into population 4.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 4 populations.

required
f1 float

Fraction of updated population 4 to be derived from population 1.

required
f2 float

Fraction of updated population 4 to be derived from population 2.

required
f3 float

Fraction of updated population 4 to be derived from population 3. A fraction (1-f1-f2-f3) will be derived from the original pop 4.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
def phi_4D_admix_into_4(phi, f1,f2,f3, xx,yy,zz,aa):
    """
    Admix populations 1,2, and 3 into population 4.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 4 populations.
        f1 (float): Fraction of updated population 4 to be derived from population 1. 
        f2 (float): Fraction of updated population 4 to be derived from population 2. 
        f3 (float): Fraction of updated population 4 to be derived from population 3. 
            A fraction (1-f1-f2-f3) will be derived from the original pop 4.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.

    Returns:
        phi (array): The updated phi array.
    """
    Demes.cache.append(Demes.Pulse(sources=[1,2,3], dest=1, proportions=[f1, f2, f3]))
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _four_pop_admixture_intermediates(phi, f1,f2,f3, xx,yy,zz,aa, yy)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_l = numpy.arange(phi.shape[3])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            for kk in range(phi.shape[2]):
                phi_int = numpy.zeros((phi.shape[3], phi.shape[3]))
                phi_int[idx_l, lower_w_index[ii,jj,kk,:]] = lower_cont[ii,jj,kk,:]
                phi_int[idx_l, upper_w_index[ii,jj,kk,:]] = upper_cont[ii,jj,kk,:]
                phi[ii,jj,kk,:] = Numerics.trapz(phi_int, aa, axis=0)

    return phi

phi_4D_to_5D(phi, f1, f2, f3, xx, yy, zz, aa, bb, deme_ids=None)

Create population 5 from populations 1, 2, 3, and 4.

Returns a 5D sfs of shape (len(xx),len(yy),len(zz),len(aa),len(bb))

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 4 populations.

required
f1 float

Fraction of population 5 derived from population 1.

required
f2 float

Fraction of population 5 derived from population 2.

required
f3 float

Fraction of population 5 derived from population 3. A fraction 1-f1-f2-f3 will be derived from population 4.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Frequency mapping that will be used along population 5 axis.

required
deme_ids list

Sequence of strings representing the names of demes after split.

None

Returns:

Name Type Description
phi_5D array

A new five-dimensional phi array.

Source code in dadi/PhiManip.py
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
def phi_4D_to_5D(phi, f1,f2,f3, xx,yy,zz,aa,bb, deme_ids=None):
    """
    Create population 5 from populations 1, 2, 3, and 4.

    Returns a 5D sfs of shape (len(xx),len(yy),len(zz),len(aa),len(bb))

    Args:
        phi (array): Phi corresponding to original 4 populations.
        f1 (float): Fraction of population 5 derived from population 1.
        f2 (float): Fraction of population 5 derived from population 2.
        f3 (float): Fraction of population 5 derived from population 3.
            A fraction 1-f1-f2-f3 will be derived from population 4.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Frequency mapping that will be used along population 5 axis.
        deme_ids (list, optional): Sequence of strings representing the names of demes after split.

    Returns:
        phi_5D (array): A new five-dimensional phi array.
    """
    Demes.cache.append(Demes.Split(proportions=[f1, f2, f3, 1-f1-f2-f3], deme_ids=deme_ids))

    lower_z_index, upper_z_index, frac_lower, frac_upper, norm \
            = _four_pop_admixture_intermediates(phi, f1,f2,f3, xx,yy,zz,aa, bb)

    # Assemble our result.
    # This uses numpy's fancy indexing. It is much, much faster than an
    # explicit loop.
    # See the numpy-discussion post "Numpy Advanced Indexing Question" by
    # Robert Kern on July 16, 2008
    # http://projects.scipy.org/pipermail/numpy-discussion/2008-July/035776.html
    idx_i = numpy.arange(len(xx))[:,nuax,nuax,nuax]
    idx_j = numpy.arange(len(yy))[nuax,:,nuax,nuax]
    idx_k = numpy.arange(len(zz))[nuax,nuax,:,nuax]
    idx_l = numpy.arange(len(aa))[nuax,nuax,nuax,:]

    phi_5D = numpy.zeros((len(xx), len(yy), len(zz), len(aa), len(bb)))
    phi_5D[idx_i, idx_j, idx_k, idx_l, lower_z_index] = frac_lower*norm
    phi_5D[idx_i, idx_j, idx_k, idx_l, upper_z_index] += frac_upper*norm

    return phi_5D

phi_5D_admix_into_1(phi, f2, f3, f4, f5, xx, yy, zz, aa, bb)

Admix populations 2, 3, 4, and 5 into population 1.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 5 populations.

required
f2 float

Fraction of updated population 1 to be derived from population 2.

required
f3 float

Fraction of updated population 1 to be derived from population 3.

required
f4 float

Fraction of updated population 1 to be derived from population 4.

required
f5 float

Fraction of updated population 1 to be derived from population 5. A fraction (1-f2-f3-f4-f5) will be derived from the original pop 1.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Mapping of points in phi to frequencies in population 5.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
def phi_5D_admix_into_1(phi, f2,f3,f4,f5, xx,yy,zz,aa,bb):
    """
    Admix populations 2, 3, 4, and 5 into population 1.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 5 populations.
        f2 (float): Fraction of updated population 1 to be derived from population 2. 
        f3 (float): Fraction of updated population 1 to be derived from population 3. 
        f4 (float): Fraction of updated population 1 to be derived from population 4. 
        f5 (float): Fraction of updated population 1 to be derived from population 5. 
            A fraction (1-f2-f3-f4-f5) will be derived from the original pop 1.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Mapping of points in phi to frequencies in population 5.

    Returns:
        phi (array): The updated phi array.
    """
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _five_pop_admixture_intermediates(phi, 1-f2-f3-f4-f5,f2,f3,f4, xx,yy,zz,aa,bb, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_i = numpy.arange(phi.shape[0])
    for jj in range(phi.shape[1]):
        for kk in range(phi.shape[2]):
            for ll in range(phi.shape[3]):
                for mm in range(phi.shape[4]):
                    phi_int = numpy.zeros((phi.shape[0], phi.shape[0]))
                    phi_int[idx_i, lower_w_index[:,jj,kk,ll,mm]] = lower_cont[:,jj,kk,ll,mm]
                    phi_int[idx_i, upper_w_index[:,jj,kk,ll,mm]] = upper_cont[:,jj,kk,ll,mm]
                    phi[:,jj,kk,ll,mm] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_5D_admix_into_2(phi, f1, f3, f4, f5, xx, yy, zz, aa, bb)

Admix populations 1, 3, 4, and 5 into population 2.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 5 populations.

required
f1 float

Fraction of updated population 2 to be derived from population 1.

required
f3 float

Fraction of updated population 2 to be derived from population 3.

required
f4 float

Fraction of updated population 2 to be derived from population 4.

required
f5 float

Fraction of updated population 2 to be derived from population 5. A fraction (1-f1-f3-f4-f5) will be derived from the original pop 2.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Mapping of points in phi to frequencies in population 5.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
def phi_5D_admix_into_2(phi, f1,f3,f4,f5, xx,yy,zz,aa,bb):
    """
    Admix populations 1, 3, 4, and 5 into population 2.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 5 populations.
        f1 (float): Fraction of updated population 2 to be derived from population 1. 
        f3 (float): Fraction of updated population 2 to be derived from population 3. 
        f4 (float): Fraction of updated population 2 to be derived from population 4. 
        f5 (float): Fraction of updated population 2 to be derived from population 5. 
            A fraction (1-f1-f3-f4-f5) will be derived from the original pop 2.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Mapping of points in phi to frequencies in population 5.

    Returns:
        phi (array): The updated phi array.
    """
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _five_pop_admixture_intermediates(phi, f1, 1-f1-f3-f4-f5,f3,f4, xx,yy,zz,aa,bb, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_j = numpy.arange(phi.shape[1])
    for ii in range(phi.shape[0]):
        for kk in range(phi.shape[2]):
            for ll in range(phi.shape[3]):
                for mm in range(phi.shape[4]):
                    phi_int = numpy.zeros((phi.shape[1], phi.shape[1]))
                    phi_int[idx_j, lower_w_index[ii,:,kk,ll,mm]] = lower_cont[ii,:,kk,ll,mm]
                    phi_int[idx_j, upper_w_index[ii,:,kk,ll,mm]] = upper_cont[ii,:,kk,ll,mm]
                    phi[ii,:,kk,ll,mm] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_5D_admix_into_3(phi, f1, f2, f4, f5, xx, yy, zz, aa, bb)

Admix populations 1, 2, 4, and 5 into population 3.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 5 populations.

required
f1 float

Fraction of updated population 3 to be derived from population 1.

required
f2 float

Fraction of updated population 3 to be derived from population 2.

required
f4 float

Fraction of updated population 3 to be derived from population 4.

required
f5 float

Fraction of updated population 3 to be derived from population 5. A fraction (1-f1-f2-f4-f5) will be derived from the original pop 3.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Mapping of points in phi to frequencies in population 5.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
def phi_5D_admix_into_3(phi, f1,f2,f4,f5, xx,yy,zz,aa,bb):
    """
    Admix populations 1, 2, 4, and 5 into population 3.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 5 populations.
        f1 (float): Fraction of updated population 3 to be derived from population 1. 
        f2 (float): Fraction of updated population 3 to be derived from population 2. 
        f4 (float): Fraction of updated population 3 to be derived from population 4. 
        f5 (float): Fraction of updated population 3 to be derived from population 5. 
            A fraction (1-f1-f2-f4-f5) will be derived from the original pop 3.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Mapping of points in phi to frequencies in population 5.

    Returns:
        phi (array): The updated phi array.
    """
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _five_pop_admixture_intermediates(phi, f1, f2, 1-f1-f2-f4-f5,f4, xx,yy,zz,aa,bb, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_k = numpy.arange(phi.shape[2])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            for ll in range(phi.shape[3]):
                for mm in range(phi.shape[4]):
                    phi_int = numpy.zeros((phi.shape[2], phi.shape[2]))
                    phi_int[idx_k, lower_w_index[ii,jj,:,ll,mm]] = lower_cont[ii,jj,:,ll,mm]
                    phi_int[idx_k, upper_w_index[ii,jj,:,ll,mm]] = upper_cont[ii,jj,:,ll,mm]
                    phi[ii,jj,:,ll,mm] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_5D_admix_into_4(phi, f1, f2, f3, f5, xx, yy, zz, aa, bb)

Admix populations 1, 2, 3, and 5 into population 4.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 5 populations.

required
f1 float

Fraction of updated population 4 to be derived from population 1.

required
f2 float

Fraction of updated population 4 to be derived from population 2.

required
f3 float

Fraction of updated population 4 to be derived from population 3.

required
f5 float

Fraction of updated population 4 to be derived from population 5. A fraction (1-f1-f2-f3-f5) will be derived from the original pop 4.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Mapping of points in phi to frequencies in population 5.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
def phi_5D_admix_into_4(phi, f1,f2,f3,f5, xx,yy,zz,aa,bb):
    """
    Admix populations 1, 2, 3, and 5 into population 4.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 5 populations.
        f1 (float): Fraction of updated population 4 to be derived from population 1. 
        f2 (float): Fraction of updated population 4 to be derived from population 2. 
        f3 (float): Fraction of updated population 4 to be derived from population 3. 
        f5 (float): Fraction of updated population 4 to be derived from population 5. 
            A fraction (1-f1-f2-f3-f5) will be derived from the original pop 4.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Mapping of points in phi to frequencies in population 5.

    Returns:
        phi (array): The updated phi array.
    """
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _five_pop_admixture_intermediates(phi, f1, f2, f3, 1-f1-f2-f3-f5, xx,yy,zz,aa,bb, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_l = numpy.arange(phi.shape[3])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            for kk in range(phi.shape[2]):
                for mm in range(phi.shape[4]):
                    phi_int = numpy.zeros((phi.shape[3], phi.shape[3]))
                    phi_int[idx_l, lower_w_index[ii,jj,kk,:,mm]] = lower_cont[ii,jj,kk,:,mm]
                    phi_int[idx_l, upper_w_index[ii,jj,kk,:,mm]] = upper_cont[ii,jj,kk,:,mm]
                    phi[ii,jj,kk,:,mm] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

phi_5D_admix_into_5(phi, f1, f2, f3, f4, xx, yy, zz, aa, bb)

Admix populations 1, 2, 3, and 4 into population 5.

Alters phi in place and returns the new version.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original 5 populations.

required
f1 float

Fraction of updated population 5 to be derived from population 1.

required
f2 float

Fraction of updated population 5 to be derived from population 2.

required
f3 float

Fraction of updated population 5 to be derived from population 3.

required
f4 float

Fraction of updated population 5 to be derived from population 3. A fraction (1-f1-f2-f3-f4) will be derived from the original pop 5.

required
xx array

Mapping of points in phi to frequencies in population 1.

required
yy array

Mapping of points in phi to frequencies in population 2.

required
zz array

Mapping of points in phi to frequencies in population 3.

required
aa array

Mapping of points in phi to frequencies in population 4.

required
bb array

Mapping of points in phi to frequencies in population 5.

required

Returns:

Name Type Description
phi array

The updated phi array.

Source code in dadi/PhiManip.py
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
def phi_5D_admix_into_5(phi, f1,f2,f3,f4, xx,yy,zz,aa,bb):
    """
    Admix populations 1, 2, 3, and 4 into population 5.

    Alters phi in place and returns the new version.

    Args:
        phi (array): Phi corresponding to original 5 populations.
        f1 (float): Fraction of updated population 5 to be derived from population 1. 
        f2 (float): Fraction of updated population 5 to be derived from population 2. 
        f3 (float): Fraction of updated population 5 to be derived from population 3. 
        f4 (float): Fraction of updated population 5 to be derived from population 3. 
            A fraction (1-f1-f2-f3-f4) will be derived from the original pop 5.
        xx (array): Mapping of points in phi to frequencies in population 1.
        yy (array): Mapping of points in phi to frequencies in population 2.
        zz (array): Mapping of points in phi to frequencies in population 3.
        aa (array): Mapping of points in phi to frequencies in population 4.
        bb (array): Mapping of points in phi to frequencies in population 5.

    Returns:
        phi (array): The updated phi array.
    """
    lower_w_index, upper_w_index, frac_lower, frac_upper, norm \
            = _five_pop_admixture_intermediates(phi, f1, f2, f3, f4, xx,yy,zz,aa,bb, xx)

    lower_cont = frac_lower * norm
    upper_cont = frac_upper * norm

    idx_m = numpy.arange(phi.shape[4])
    for ii in range(phi.shape[0]):
        for jj in range(phi.shape[1]):
            for kk in range(phi.shape[2]):
                for ll in range(phi.shape[3]):
                    phi_int = numpy.zeros((phi.shape[4], phi.shape[4]))
                    phi_int[idx_m, lower_w_index[ii,jj,kk,ll,:]] = lower_cont[ii,jj,kk,ll,:]
                    phi_int[idx_m, upper_w_index[ii,jj,kk,ll,:]] = upper_cont[ii,jj,kk,ll,:]
                    phi[ii,jj,kk,ll,:] = Numerics.trapz(phi_int, xx, axis=0)

    return phi

remove_pop(phi, xx, popnum)

Remove a population from phi.

Returns new phi with one fewer population.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original populations.

required
xx array

Mapping of points in phi to frequencies in population to be removed.

required
popnum int

Population number to remove, numbering from 1.

required

Returns:

Name Type Description
phi array

The updated phi array with one fewer population.

Source code in dadi/PhiManip.py
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
def remove_pop(phi, xx, popnum):
    """
    Remove a population from phi.

    Returns new phi with one fewer population.

    Args:
        phi (array): Phi corresponding to original populations.
        xx (array): Mapping of points in phi to frequencies in population to be removed.
        popnum (int): Population number to remove, numbering from 1.

    Returns:
        phi (array): The updated phi array with one fewer population.
    """
    Demes.cache.append(Demes.Remove(removed=popnum))
    return Numerics.trapz(phi, xx, axis=popnum-1)

reorder_pops(phi, neworder)

Get phi with populations in new order.

Returns new phi with same number of populations, but in a different order.

Parameters:

Name Type Description Default
phi array

Phi corresponding to original populations.

required
neworder list

Integer list defining new order of populations, indexing the original populations from 1. Must contain all integers from 1 to number of pops.

required

Returns:

Name Type Description
phi array

The updated phi array with populations in the new order.

Source code in dadi/PhiManip.py
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
def reorder_pops(phi, neworder):
    """
    Get phi with populations in new order.

    Returns new phi with same number of populations, but in a different order.

    Args:
        phi (array): Phi corresponding to original populations.
        neworder (list): Integer list defining new order of populations, indexing the original
            populations from 1. Must contain all integers from 1 to number of pops.

    Returns:
        phi (array): The updated phi array with populations in the new order.
    """
    Demes.cache.append(Demes.Reorder(neworder=neworder))
    if sorted(neworder) != [_+1 for _ in range(phi.ndim)]:
        raise(ValueError("neworder argument misspecified"))
    newaxes = [_-1 for _ in neworder]
    phi = phi.transpose(newaxes)

    return phi