def check_ExpCM_derivatives(self):
(Prxy, Qxy, phiw, beta, omega, eta0, eta1, eta2, kappa) = sympy.symbols(<EOL>'<STR_LIT>')<EOL>values = {'<STR_LIT>':self.params['<STR_LIT>'],<EOL>'<STR_LIT>':self.params['<STR_LIT>'],<EOL>'<STR_LIT>':self.params['<STR_LIT>'],<EOL>'<STR_LIT>':self.params['<STR_LIT>'][<NUM_LIT:0>],<EOL>'<STR_LIT>':self.params['<STR_LIT>'][<NUM_LIT:1>],<EOL>'<STR_LIT>':self.params['<STR_LIT>'][<NUM_LIT:2>],<EOL>}<EOL>for r in range(self.nsites):<EOL><INDENT>for x in range(N_CODON):<EOL><INDENT>pirAx = self.prefs[r][INDEX_TO_AA[CODON_TO_AA[x]]]<EOL>for y in [yy for yy in range(N_CODON) if yy != x]:<EOL><INDENT>pirAy = self.prefs[r][INDEX_TO_AA[CODON_TO_AA[y]]]<EOL>if not CODON_SINGLEMUT[x][y]:<EOL><INDENT>Prxy = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>w = NT_TO_INDEX[[ynt for (xnt, ynt) in zip(INDEX_TO_CODON[x],<EOL>INDEX_TO_CODON[y]) if xnt != ynt][<NUM_LIT:0>]]<EOL>if w == <NUM_LIT:0>:<EOL><INDENT>phiw = <NUM_LIT:1> - eta0<EOL><DEDENT>elif w == <NUM_LIT:1>:<EOL><INDENT>phiw = eta0 * (<NUM_LIT:1> - eta1)<EOL><DEDENT>elif w == <NUM_LIT:2>:<EOL><INDENT>phiw = eta0 * eta1 * (<NUM_LIT:1> - eta2)<EOL><DEDENT>elif w == <NUM_LIT:3>:<EOL><INDENT>phiw = eta0 * eta1 * eta2<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>self.assertTrue(scipy.allclose(float(phiw.subs(values)),<EOL>self.expcm.phi[w]))<EOL>if CODON_TRANSITION[x][y]:<EOL><INDENT>Qxy = kappa * phiw<EOL><DEDENT>else:<EOL><INDENT>Qxy = phiw<EOL><DEDENT>self.assertTrue(scipy.allclose(float(Qxy.subs(values)),<EOL>self.expcm.Qxy[x][y]))<EOL>if CODON_NONSYN[x][y]:<EOL><INDENT>if pirAx == pirAy:<EOL><INDENT>Prxy = Qxy * omega<EOL><DEDENT>else:<EOL><INDENT>Prxy = Qxy * omega * (-beta * scipy.log(pirAx / pirAy) / (<NUM_LIT:1> - (pirAx / pirAy)**beta))<EOL><DEDENT><DEDENT>else:<EOL><INDENT>Prxy = Qxy<EOL><DEDENT><DEDENT>for (name, actual, expect) in [<EOL>('<STR_LIT>', self.expcm.Prxy[r][x][y], Prxy),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][r][x][y], sympy.diff(Prxy, kappa)),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][r][x][y], sympy.diff(Prxy, omega)),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][r][x][y], sympy.diff(Prxy, beta)),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][<NUM_LIT:0>][r][x][y], sympy.diff(Prxy, eta0)),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][<NUM_LIT:1>][r][x][y], sympy.diff(Prxy, eta1)),<EOL>('<STR_LIT>', self.expcm.dPrxy['<STR_LIT>'][<NUM_LIT:2>][r][x][y], sympy.diff(Prxy, eta2)),<EOL>]:<EOL><INDENT>if Prxy == <NUM_LIT:0>:<EOL><INDENT>expectval = <NUM_LIT:0><EOL><DEDENT>else:<EOL><INDENT>expectval = float(expect.subs(values))<EOL><DEDENT>self.assertTrue(scipy.allclose(actual, expectval, atol=<NUM_LIT>),<EOL>"<STR_LIT>".format(name, actual, expectval))<EOL><DEDENT><DEDENT><DEDENT><DEDENT>qxs = [sympy.Symbol('<STR_LIT>'.format(x)) for x in range(N_CODON)]<EOL>frxs = [sympy.Symbol('<STR_LIT>'.format(x)) for x in range(N_CODON)]<EOL>prx = sympy.Symbol('<STR_LIT>')<EOL>phixs = [sympy.Symbol('<STR_LIT>'.format(w)) for w in range(<NUM_LIT:3>)]<EOL>for r in range(self.nsites):<EOL><INDENT>for x in range(N_CODON):<EOL><INDENT>pirAx = self.prefs[r][INDEX_TO_AA[CODON_TO_AA[x]]]<EOL>frxs[x] = pirAx**beta<EOL>xcodon = INDEX_TO_CODON[x]<EOL>assert len(phixs) == len(xcodon)<EOL>for (w, xwnt) in enumerate(xcodon):<EOL><INDENT>xw = NT_TO_INDEX[xwnt]<EOL>if xw == <NUM_LIT:0>:<EOL><INDENT>phixs[w] = <NUM_LIT:1> - eta0<EOL><DEDENT>elif xw == <NUM_LIT:1>:<EOL><INDENT>phixs[w] = eta0 * (<NUM_LIT:1> - eta1)<EOL><DEDENT>elif xw == <NUM_LIT:2>:<EOL><INDENT>phixs[w] = eta0 * eta1 * (<NUM_LIT:1> - eta2)<EOL><DEDENT>elif xw == <NUM_LIT:3>:<EOL><INDENT>phixs[w] = eta0 * eta1 * eta2<EOL><DEDENT>else:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT><DEDENT>qxs[x] = phixs[<NUM_LIT:0>] * phixs[<NUM_LIT:1>] * phixs[<NUM_LIT:2>]<EOL><DEDENT>for x in range(N_CODON):<EOL><INDENT>prx = frxs[x] * qxs[x] / sum(frx * qx for (frx, qx) in zip(frxs, qxs))<EOL>for (name, actual, expect) in [<EOL>('<STR_LIT>', self.expcm.prx[r][x], prx),<EOL>('<STR_LIT>', self.expcm.dprx['<STR_LIT>'][r][x], sympy.diff(prx, beta)),<EOL>('<STR_LIT>', self.expcm.dprx['<STR_LIT>'][<NUM_LIT:0>][r][x], sympy.diff(prx, eta0)),<EOL>('<STR_LIT>', self.expcm.dprx['<STR_LIT>'][<NUM_LIT:1>][r][x], sympy.diff(prx, eta1)),<EOL>('<STR_LIT>', self.expcm.dprx['<STR_LIT>'][<NUM_LIT:2>][r][x], sympy.diff(prx, eta2)),<EOL>]:<EOL><INDENT>expectval = float(expect.subs(values))<EOL>self.assertTrue(scipy.allclose(actual, expectval, atol=<NUM_LIT>),<EOL>"<STR_LIT>".format(name, actual, expectval))<EOL><DEDENT><DEDENT><DEDENT>
Use `sympy` to check values and derivatives of `ExpCM` attributes.