Commit 51d90205 authored by Ryan Gutenkunst's avatar Ryan Gutenkunst
Browse files

Unit tests for 4D integration

parent f589c50b
import unittest
import numpy as np import numpy as np
import dadi import dadi
## Simple SNM test. Check that all splits yield SNM. class Int4DTestCase(unittest.TestCase):
#pts = 50 def test_splitting(self):
#xx = dadi.Numerics.default_grid(pts) """
#phi = dadi.PhiManip.phi_1D(xx) Test splitting into 4D.
#fs1 = dadi.Spectrum.from_phi(phi, [10], (xx,)) """
# # Check that all splits yield SNM
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_2D_to_3D_admix(phi, 0, xx,xx,xx) pts = 50
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) xx = dadi.Numerics.default_grid(pts)
#fs4 = dadi.Spectrum.from_phi(phi, [10,10,10,10], (xx,xx,xx,xx)) phi = dadi.PhiManip.phi_1D(xx)
# fs1 = dadi.Spectrum.from_phi(phi, [10], (xx,))
#for ii in range(4):
# tomarg = list(range(4)) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
# tomarg.remove(ii) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
# fsm = fs4.marginalize(tomarg) phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
# print(np.allclose(fs1, fsm, rtol=1e-3, atol=1e-3)) fs4 = dadi.Spectrum.from_phi(phi, [10,10,10,10], (xx,xx,xx,xx))
#
## Simple SNM test with integration. # Test each marginal spectrum
#pts = 30 for ii in range(4):
#xx = dadi.Numerics.default_grid(pts) tomarg = list(range(4))
#phi = dadi.PhiManip.phi_1D(xx) tomarg.remove(ii)
#fs1 = dadi.Spectrum.from_phi(phi, [5], (xx,)) fsm = fs4.marginalize(tomarg)
# self.assertTrue(np.allclose(fs1, fsm, rtol=1e-3, atol=1e-3))
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_2D_to_3D_admix(phi, 0, xx,xx,xx) def test_integration_SNM(self):
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) """
#phi = dadi.Integration.four_pops(phi, xx, T=0.1) Test simple SNM integration.
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) """
# pts = 30
#for ii in range(4): xx = dadi.Numerics.default_grid(pts)
# tomarg = list(range(4)) phi = dadi.PhiManip.phi_1D(xx)
# tomarg.remove(ii) fs1 = dadi.Spectrum.from_phi(phi, [5], (xx,))
# fsm = fs4.marginalize(tomarg)
# print(np.allclose(fs1, fsm, rtol=1e-2, atol=1e-2)) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
phi = dadi.PhiManip.phi_2D_to_3D_admix(phi, 0, xx,xx,xx)
# 2D comparison tests phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
#pts = 20 # No demography, so all pops should still be SNM
#xx = dadi.Numerics.default_grid(pts) phi = dadi.Integration.four_pops(phi, xx, T=0.1)
#phi = dadi.PhiManip.phi_1D(xx) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) # Loose tolerance of this comparison is okay. We're looking
#fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx)) # for gross violation due to bugs.
# for ii in range(4):
#phi = dadi.PhiManip.phi_1D(xx) tomarg = list(range(4))
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) tomarg.remove(ii)
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) fsm = fs4.marginalize(tomarg)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) self.assertTrue(np.allclose(fs1, fsm, rtol=1e-2, atol=1e-2))
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu4=10, m14=2, m41=0.5, gamma1=-1, gamma4=1, h1=0.2, h4=0.9)
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) def test_integration_2Dcomp(self):
#fsm = fs4.marginalize((1,2)) """
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) Integration tested by comparison to 2D integrations
# """
#phi = dadi.PhiManip.phi_1D(xx) pts = 20
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) nu1 = lambda t: 0.5 + 5*t
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) nu2 = lambda t: 10-50*t
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) m12 = lambda t: 2-t
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu2=0.5, nu4=10, m24=2, m42=0.5, gamma2=-1, gamma4=1, h2=0.2, h4=0.7) m21 = lambda t: 0.5+3*t
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) gamma1 = lambda t: -2*t
#fsm = fs4.marginalize((0,2)) gamma2 = lambda t: 3*t
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) h1 = lambda t: 0.2+t
# h2 = lambda t: 0.9-t
#phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) xx = dadi.Numerics.default_grid(pts)
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=0.5, nu4=10, m34=2, m43=0.5, gamma3=-1, gamma4=1, h3=0.2, h4=0.7) phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=nu1, nu2=nu2,
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) m12=m12, m21=m21, gamma1=gamma1, gamma2=gamma2, h1=h1, h2=h2)
#fsm = fs4.marginalize((0,1)) fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx))
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
phi = dadi.PhiManip.phi_1D(xx)
#for ii in range(4): phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
# tomarg = list(range(4)) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
# tomarg.remove(ii) phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
# fsm = fs4.marginalize(tomarg) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=nu1, nu4=nu2,
# print(np.allclose(fs1, fsm, rtol=1e-2, atol=1e-2)) m14=m12, m41=m21, gamma1=gamma1, gamma4=gamma2, h1=h1, h4=h2)
fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
# fsm = fs4.marginalize((1,2))
# admix_props test self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#
phi = dadi.PhiManip.phi_1D(xx)
#pts = 20 phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#xx = dadi.Numerics.default_grid(pts) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#phi = dadi.PhiManip.phi_1D(xx) phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu2=nu1, nu4=nu2,
#phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) m24=m12, m42=m21, gamma2=gamma1, gamma4=gamma2, h2=h1, h4=h2)
#fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx), admix_props=((0.3,0.7),(0.9,0.1))) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
# fsm = fs4.marginalize((0,2))
#phi = dadi.PhiManip.phi_1D(xx) self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=0.5, nu4=10, m34=2, m43=0.5, gamma3=-1, gamma4=1, h3=0.2, h4=0.7) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx), phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
# admix_props=((1,0,0,0),(0,1,0,0),(0,0,0.3,0.7),(0,0,0.9,0.1))) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=nu1, nu4=nu2,
#fsm = fs4.marginalize((0,1)) m34=m12, m43=m21, gamma3=gamma1, gamma4=gamma2, h3=h1, h4=h2)
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
fsm = fs4.marginalize((0,1))
# Next phi_4D_admix methods self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#pts = 20 def test_admix_props(self):
#xx = dadi.Numerics.default_grid(pts) """
#phi = dadi.PhiManip.phi_1D(xx) Test admix_props in from_phi
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) """
#phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) pts = 20
#phi = dadi.PhiManip.phi_2D_admix_1_into_2(phi,0.8,xx,xx) xx = dadi.Numerics.default_grid(pts)
#fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx)) phi = dadi.PhiManip.phi_1D(xx)
# phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_1D(xx) phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx), admix_props=((0.3,0.7),(0.9,0.1)))
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=0.5, nu4=10, m34=2, m43=0.5, gamma3=-1, gamma4=1, h3=0.2, h4=0.7) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_4D_admix_into_4(phi,0,0,0.8,xx,xx,xx,xx) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
#fsm = fs4.marginalize((0,1)) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=0.5, nu4=10, m34=2, m43=0.5, gamma3=-1, gamma4=1, h3=0.2, h4=0.7)
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx),
admix_props=((1,0,0,0),(0,1,0,0),(0,0,0.3,0.7),(0,0,0.9,0.1)))
#pts = 20 fsm = fs4.marginalize((0,1))
#xx = dadi.Numerics.default_grid(pts) self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) def test_admixture(self):
#phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) """
#phi = dadi.PhiManip.phi_2D_admix_2_into_1(phi,0.8,xx,xx) Test phi_4D_admix_into_4
#fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx)) """
# pts = 20
#phi = dadi.PhiManip.phi_1D(xx) xx = dadi.Numerics.default_grid(pts)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9)
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.7) phi = dadi.PhiManip.phi_2D_admix_1_into_2(phi,0.8,xx,xx)
#phi = dadi.PhiManip.phi_4D_admix_into_1(phi,0.8,0,0,xx,xx,xx,xx) fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx))
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
#fsm = fs4.marginalize((2,3)) phi = dadi.PhiManip.phi_1D(xx)
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#pts = 20 phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
#xx = dadi.Numerics.default_grid(pts) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu3=0.5, nu4=10, m34=2, m43=0.5, gamma3=-1, gamma4=1, h3=0.2, h4=0.7)
#phi = dadi.PhiManip.phi_1D(xx) phi = dadi.PhiManip.phi_4D_admix_into_4(phi,0,0,0.8,xx,xx,xx,xx)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
#phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) fsm = fs4.marginalize((0,1))
#phi = dadi.PhiManip.phi_2D_admix_1_into_2(phi,0.8,xx,xx) self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx))
# phi = dadi.PhiManip.phi_1D(xx)
#phi = dadi.PhiManip.phi_1D(xx) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
#phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
#phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
#phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.7)
#phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.7) phi = dadi.PhiManip.phi_4D_admix_into_2(phi,0.8,0,0,xx,xx,xx,xx)
#phi = dadi.PhiManip.phi_4D_admix_into_2(phi,0.8,0,0,xx,xx,xx,xx) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
#fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) fsm = fs4.marginalize((2,3))
#fsm = fs4.marginalize((2,3)) self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
#print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
phi = dadi.PhiManip.phi_1D(xx)
pts = 20 phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
xx = dadi.Numerics.default_grid(pts) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
nu1_func = lambda t: 0.5 + 5*t phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
phi = dadi.PhiManip.phi_1D(xx) phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu3=10, m13=2, m31=0.5, gamma1=-1, gamma3=1, h1=0.2, h3=0.7)
phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) phi = dadi.PhiManip.phi_4D_admix_into_3(phi,0.8,0,0,xx,xx,xx,xx)
phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=nu1_func, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9) fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
phi = dadi.PhiManip.phi_2D_admix_1_into_2(phi,0.8,xx,xx) fsm = fs4.marginalize((1,3))
fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx)) self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
phi = dadi.PhiManip.phi_1D(xx) phi = dadi.PhiManip.phi_1D(xx)
phi = dadi.PhiManip.phi_1D_to_2D(xx, phi) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx) phi = dadi.Integration.two_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.9)
phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx) phi = dadi.PhiManip.phi_2D_admix_2_into_1(phi,0.8,xx,xx)
phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=nu1_func, nu3=10, m13=2, m31=0.5, gamma1=-1, gamma3=1, h1=0.2, h3=0.7) fs2 = dadi.Spectrum.from_phi(phi, [5,5], (xx,xx))
phi = dadi.PhiManip.phi_4D_admix_into_3(phi,0.8,0,0,xx,xx,xx,xx)
fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx)) phi = dadi.PhiManip.phi_1D(xx)
fsm = fs4.marginalize((1,3)) phi = dadi.PhiManip.phi_1D_to_2D(xx, phi)
print(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2)) phi = dadi.PhiManip.phi_2D_to_3D(phi, 0, xx,xx,xx)
\ No newline at end of file phi = dadi.PhiManip.phi_3D_to_4D(phi, 0, 0, xx,xx,xx,xx)
phi = dadi.Integration.four_pops(phi, xx, T=0.1, nu1=0.5, nu2=10, m12=2, m21=0.5, gamma1=-1, gamma2=1, h1=0.2, h2=0.7)
phi = dadi.PhiManip.phi_4D_admix_into_1(phi,0.8,0,0,xx,xx,xx,xx)
fs4 = dadi.Spectrum.from_phi(phi, [5,5,5,5], (xx,xx,xx,xx))
fsm = fs4.marginalize((2,3))
self.assertTrue(np.allclose(fs2, fsm, rtol=1e-2, atol=1e-2))
suite = unittest.TestLoader().loadTestsFromTestCase(Int4DTestCase)
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment