# Path: phydmslib/constants.py
# N_CODON = len(CODON_TO_INDEX)
# 
# NT_TO_INDEX = {nt: i for (i, nt) in INDEX_TO_NT.items()}
# 
# CODON_TO_AA = []
# 
# INDEX_TO_CODON = {}
# 
# INDEX_TO_AA = dict(enumerate('ACDEFGHIKLMNPQRSTVWY'))
# 
# CODON_SINGLEMUT = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# CODON_TRANSITION = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# CODON_NONSYN = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# N_NT = len(INDEX_TO_NT)
# 
# AA_TO_INDEX = {aa: i for (i, aa) in INDEX_TO_AA.items()}
# 
# N_AA = len(INDEX_TO_AA)


import random
import unittest
import numpy
import scipy.linalg
import scipy.optimize
import sympy
import phydmslib.models
from phydmslib.constants import (N_CODON, NT_TO_INDEX, CODON_TO_AA,
                                 INDEX_TO_CODON, INDEX_TO_AA, CODON_SINGLEMUT,
                                 CODON_TRANSITION, CODON_NONSYN, N_NT,
                                 AA_TO_INDEX, N_AA)
            rprefs = numpy.random.dirichlet([0.5] * N_AA)
            rprefs[rprefs < minpref] = minpref
            rprefs /= rprefs.sum()
            self.prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        # create initial ExpCM
        phi = numpy.random.dirichlet([2] * N_NT)
        omega = 0.7
        kappa = 2.5
        beta = 1.9
        self.expcm = phydmslib.models.ExpCM(
            self.prefs, phi=phi, omega=omega, kappa=kappa, beta=beta)
        self.assertTrue(numpy.allclose(phi, self.expcm.phi))
        self.assertTrue(numpy.allclose(omega, self.expcm.omega))
        self.assertTrue(numpy.allclose(kappa, self.expcm.kappa))
        self.assertTrue(numpy.allclose(beta, self.expcm.beta))

        self.assertTrue(
            numpy.allclose(
                numpy.repeat(1.0, self.nsites),
                self.expcm.stationarystate.sum(axis=1)))

        # now check ExpCM attributes / derivates, updating several times
        for _update in range(2):
            self.params = {
                "omega": random.uniform(*self.expcm.PARAMLIMITS["omega"]),
                "kappa": random.uniform(*self.expcm.PARAMLIMITS["kappa"]),
                "beta": random.uniform(0.5, 2.5),
                "eta": numpy.array(
                    [random.uniform(*self.expcm.PARAMLIMITS["eta"])

# Path: phydmslib/constants.py
# N_CODON = len(CODON_TO_INDEX)
# 
# NT_TO_INDEX = {nt: i for (i, nt) in INDEX_TO_NT.items()}
# 
# CODON_TO_AA = []
# 
# INDEX_TO_CODON = {}
# 
# INDEX_TO_AA = dict(enumerate('ACDEFGHIKLMNPQRSTVWY'))
# 
# CODON_SINGLEMUT = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# CODON_TRANSITION = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# CODON_NONSYN = numpy.full((N_CODON, N_CODON), False, dtype='bool')
# 
# N_NT = len(INDEX_TO_NT)
# 
# AA_TO_INDEX = {aa: i for (i, aa) in INDEX_TO_AA.items()}
# 
# N_AA = len(INDEX_TO_AA)


# Path: tests/test_expcm.py
import random
import unittest
import numpy
import scipy.linalg
import scipy.optimize
import sympy
import phydmslib.models
from phydmslib.constants import (N_CODON, NT_TO_INDEX, CODON_TO_AA,
                                 INDEX_TO_CODON, INDEX_TO_AA, CODON_SINGLEMUT,
                                 CODON_TRANSITION, CODON_NONSYN, N_NT,
                                 AA_TO_INDEX, N_AA)

            rprefs = numpy.random.dirichlet([0.5] * N_AA)
            rprefs[rprefs < minpref] = minpref
            rprefs /= rprefs.sum()
            self.prefs.append(dict(zip(sorted(AA_TO_INDEX.keys()), rprefs)))

        # create initial ExpCM
        phi = numpy.random.dirichlet([2] * N_NT)
        omega = 0.7
        kappa = 2.5
        beta = 1.9
        self.expcm = phydmslib.models.ExpCM(
            self.prefs, phi=phi, omega=omega, kappa=kappa, beta=beta)
        self.assertTrue(numpy.allclose(phi, self.expcm.phi))
        self.assertTrue(numpy.allclose(omega, self.expcm.omega))
        self.assertTrue(numpy.allclose(kappa, self.expcm.kappa))
        self.assertTrue(numpy.allclose(beta, self.expcm.beta))

        self.assertTrue(
            numpy.allclose(
                numpy.repeat(1.0, self.nsites),
                self.expcm.stationarystate.sum(axis=1)))

        # now check ExpCM attributes / derivates, updating several times
        for _update in range(2):
            self.params = {
                "omega": random.uniform(*self.expcm.PARAMLIMITS["omega"]),
                "kappa": random.uniform(*self.expcm.PARAMLIMITS["kappa"]),
                "beta": random.uniform(0.5, 2.5),
                "eta": numpy.array(
                    [random.uniform(*self.expcm.PARAMLIMITS["eta"])

                     for i in range(N_NT - 1)]),