예제 #1
0
def test_MMVT_data_sample_fill_out_data_quantities():
    model = make_simple_model()
    N_alpha_beta = {(0,1):12, (1,0):12,
                    (1,2):12, (2,1):12,
                    (2,3):6,  (3,2):6}
    k_alpha_beta = {(0,1):20.0, (1,0):10.0,
                    (1,2):10.0,  (2,1):(40.0/3.0),
                    (2,3):(20.0/3.0), (3,2):20.0}
    N_i_j_alpha = [{},
                   {(0,1):4, (1,0):4}, 
                   {(1,2):2, (2,1):2},
                   {}]
    R_i_alpha_total = [{0: 1.2},
                       {0: 1.2, 1:1.2},
                       {1: 1.2, 2:0.6},
                       {2: 0.6}]
    T_alpha_total = [1.2,
                     2.4,
                     1.8,
                     0.6]
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, N_alpha_beta, k_alpha_beta, N_i_j_alpha, 
            R_i_alpha_total, T_alpha_total)
    main_data_sample.calculate_pi_alpha()
    main_data_sample.fill_out_data_quantities()
    N_ij = {(0,1): 1, (1,0): 1, (1,2): 0.5, (2,1): 0.5}
    R_i = {0: 0.6, 1: 0.6, 2: 0.3}
    compare_dicts(N_ij, main_data_sample.N_ij)
    compare_dicts(R_i, main_data_sample.R_i)
    return
예제 #2
0
def test_MMVT_data_sample_calculate_pi_alpha():
    model = make_simple_model()
    N_alpha_beta = {(0,1):10, (1,0):10,
                    (1,2):10, (2,1):10,
                    (2,3):10,  (3,2):10}
    k_alpha_beta = {(0,1):4.0, (1,0):4.0,
                    (1,2):4.0,  (2,1):4.0,
                    (2,3):4.0, (3,2):4.0}
    N_i_j_alpha = [{},
                   {(0,1):3, (1,0):3}, 
                   {(1,2):3, (2,1):3},
                   {}]
    R_i_alpha_total = [{0: 8.0},
                       {0: 8.0, 1:8.0},
                       {1: 8.0, 2:8.0},
                       {2: 8.0}]
    T_alpha_total = [2.5,
                     2.5,
                     2.5,
                     2.5]
    
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, N_alpha_beta, k_alpha_beta, N_i_j_alpha, 
            R_i_alpha_total, T_alpha_total)
    main_data_sample.calculate_pi_alpha()
    assert np.all(np.isclose(main_data_sample.pi_alpha[:-1], 
                             main_data_sample.pi_alpha[0]))
    assert np.isclose(main_data_sample.pi_alpha[-1], 0.0)
    return
예제 #3
0
def test_MMVT_data_sample_fill_k_from_matrices():
    model = make_simple_model()
    k_alpha_beta_exp = {(0,1):20.0, (1,0):10.0,
                    (1,2):10.0,  (2,1):(40.0/3.0),
                    (2,3):(20.0/3.0), (3,2):20.0}
    k_alpha_beta_matrix = np.array([
        [-20.0, 20.0, 0.0, 0.0],
        [10.0, -20.0, 10.0, 0.0],
        [0.0, (40.0/3.0), -(40.0/3.0)-(20.0/3.0), (20.0/3.0)],
        [0.0, 0.0, 20.0, -20.0]])
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, None, None, None, 
            None, None)
    main_data_sample.fill_k_from_matrices(k_alpha_beta_matrix)
    compare_dicts(k_alpha_beta_exp, main_data_sample.k_alpha_beta)
    return
예제 #4
0
 def fill_out_data_samples_mmvt(self):
     """
     Now that the statistics for each anchor have been extracted
     from the output files, construct the global transition
     statistics objects. Applies to systems using MMVT milestoning.
     """
     N_alpha_beta = defaultdict(int)
     k_alpha_beta = defaultdict(float)
     N_i_j_alpha = []
     R_i_alpha_total = []
     T_alpha_total = []
     
     for alpha, anchor1 in enumerate(self.model.anchors):
         if anchor1.bulkstate:
             continue
         anchor_N_alpha_beta = self.anchor_stats_list[alpha].N_alpha_beta
         anchor_k_alpha_beta = self.anchor_stats_list[alpha].k_alpha_beta
         for beta, anchor2 in enumerate(self.model.anchors):
             if anchor2.bulkstate:
                 continue
             if alpha == beta:
                 continue
             id_alias = anchor1.alias_from_neighbor_id(anchor2.index)
             if id_alias is None:
                 continue
             if len(anchor_N_alpha_beta) == 0:
                 # Then no transitions were observed in this anchor
                 default_flux = FAST_FLUX
             else:
                 default_flux = LOW_FLUX
             
             if id_alias in anchor_N_alpha_beta:
                 N_alpha_beta[(alpha, beta)] = anchor_N_alpha_beta[id_alias]
                 k_alpha_beta[(alpha, beta)] = anchor_k_alpha_beta[id_alias]
             else:
                 N_alpha_beta[(alpha, beta)] = 0
                 k_alpha_beta[(alpha, beta)] = default_flux #0.0
             
         anchor_N_i_j_alpha = self.anchor_stats_list[alpha].N_i_j_alpha
         N_i_j_alpha_element = defaultdict(int)
         R_i_alpha_element = defaultdict(float)
         
         # Fill out empty milestone statistics
         for milestone1 in anchor1.milestones:
             R_i_alpha_element[milestone1.index] = 0.0
             for milestone2 in anchor1.milestones:
                 if milestone1.index == milestone2.index:
                     continue
                 new_key = (milestone1.index, milestone2.index)
                 N_i_j_alpha_element[new_key] = 0
         
         for key in anchor_N_i_j_alpha:
             (alias_id_i, alias_id_j) = key
             id_i = anchor1.id_from_alias(alias_id_i)
             id_j = anchor1.id_from_alias(alias_id_j)
             new_key = (id_i, id_j)
             N_i_j_alpha_element[new_key] = anchor_N_i_j_alpha[key]
         N_i_j_alpha.append(N_i_j_alpha_element)
         
         anchor_R_i_alpha = self.anchor_stats_list[alpha].R_i_alpha_total
         
         for key in anchor_R_i_alpha:
             alias_id_i = key
             id_i = anchor1.id_from_alias(alias_id_i)
             assert id_i is not None, \
                 "Anchor {} missing alias {}.".format(anchor1.index, 
                                                      alias_id_i)
             R_i_alpha_element[id_i] = anchor_R_i_alpha[key]
         
         R_i_alpha_total.append(R_i_alpha_element)
         anchor_T_alpha = self.anchor_stats_list[alpha].T_alpha_total
         T_alpha_total.append(anchor_T_alpha)
         
     self.main_data_sample = mmvt_analyze.MMVT_data_sample(
         self.model, N_alpha_beta, k_alpha_beta, N_i_j_alpha, 
         R_i_alpha_total, T_alpha_total)
     
     return
예제 #5
0
파일: analyze.py 프로젝트: astokely/seekr2
    def fill_out_data_samples_mmvt(self):
        """
        Now that the statistics for each anchor have been extracted
        from the output files, construct the global transition
        statistics objects. Applies to systems using MMVT milestoning.
        """
        N_alpha_beta = defaultdict(int)
        k_alpha_beta = defaultdict(float)
        N_i_j_alpha = []
        R_i_alpha_total = []
        R_i_alpha_average = []
        R_i_alpha_std_dev = []
        R_i_alpha_count = []
        T_alpha_total = []
        T_alpha_average = []
        T_alpha_std_dev = []
        T_alpha_count = []
        for alpha, anchor1 in enumerate(self.model.anchors):
            if anchor1.bulkstate:
                continue
            anchor_N_alpha_beta = self.anchor_stats_list[alpha].N_alpha_beta
            anchor_k_alpha_beta = self.anchor_stats_list[alpha].k_alpha_beta
            for beta, anchor2 in enumerate(self.model.anchors):
                if anchor2.bulkstate:
                    continue
                if alpha == beta:
                    continue
                id_alias = anchor1.alias_from_neighbor_id(anchor2.index)
                if id_alias is None:
                    continue
                if id_alias in anchor_N_alpha_beta:
                    N_alpha_beta[(alpha, beta)] = anchor_N_alpha_beta[id_alias]
                    k_alpha_beta[(alpha, beta)] = anchor_k_alpha_beta[id_alias]
                else:
                    N_alpha_beta[(alpha, beta)] = 0
                    k_alpha_beta[(alpha, beta)] = 0.0

            anchor_N_i_j_alpha = self.anchor_stats_list[alpha].N_i_j_alpha
            N_i_j_alpha_element = defaultdict(int)
            for key in anchor_N_i_j_alpha:
                (alias_id_i, alias_id_j) = key
                id_i = anchor1.id_from_alias(alias_id_i)
                id_j = anchor1.id_from_alias(alias_id_j)
                new_key = (id_i, id_j)
                N_i_j_alpha_element[new_key] = anchor_N_i_j_alpha[key]
            N_i_j_alpha.append(N_i_j_alpha_element)

            anchor_R_i_alpha = self.anchor_stats_list[alpha].R_i_alpha_total
            anchor_R_i_alpha_std = self.anchor_stats_list[
                alpha].R_i_alpha_std_dev
            anchor_R_i_alpha_list = self.anchor_stats_list[
                alpha].R_i_alpha_list
            R_i_alpha_element = defaultdict(float)
            R_i_alpha_count_element = defaultdict(int)
            R_i_alpha_std_element = defaultdict(float)
            for key in anchor_R_i_alpha:
                alias_id_i = key
                id_i = anchor1.id_from_alias(alias_id_i)
                R_i_alpha_element[id_i] = anchor_R_i_alpha[key]
                R_i_alpha_std_element[id_i] = anchor_R_i_alpha_std[key]
                R_i_alpha_count_element[id_i] = len(anchor_R_i_alpha_list[key])

            R_i_alpha_total.append(R_i_alpha_element)
            R_i_alpha_std_dev.append(R_i_alpha_std_element)
            R_i_alpha_count.append(R_i_alpha_count_element)
            anchor_T_alpha = self.anchor_stats_list[alpha].T_alpha_total
            anchor_T_alpha_std = self.anchor_stats_list[alpha].T_alpha_std_dev
            anchor_T_alpha_list = self.anchor_stats_list[alpha].T_alpha_list
            T_alpha_total.append(anchor_T_alpha)
            T_alpha_std_dev.append(anchor_T_alpha_std)
            T_alpha_count.append(len(anchor_T_alpha_list))

        self.main_data_sample = mmvt_analyze.MMVT_data_sample(
            self.model, N_alpha_beta, k_alpha_beta, N_i_j_alpha,
            R_i_alpha_total, T_alpha_total)

        for i in range(self.num_error_samples):
            sampled_k_alpha_beta, sampled_N_i_j_alpha, \
            sampled_R_i_alpha_total, sampled_T_alpha_total \
                = self.resample_k_N_R_T(
                    N_alpha_beta, N_i_j_alpha, R_i_alpha_total,
                    R_i_alpha_average, R_i_alpha_std_dev, R_i_alpha_count,
                    T_alpha_total, T_alpha_average, T_alpha_std_dev,
                    T_alpha_count)
            data_sample = mmvt_analyze.MMVT_data_sample(
                self.model, N_alpha_beta, sampled_k_alpha_beta,
                sampled_N_i_j_alpha, sampled_R_i_alpha_total,
                sampled_T_alpha_total)
            self.data_sample_list.append(data_sample)
        return
예제 #6
0
def test_mcmc_3x3_mmvt(tmpdir_factory):
    """
    Use the same statistics to generate both MMVT and Elber rate matrices.
    Then use the data to sample matrices and compare the distributions to
    ensure that the MMVT and Elber matrix sampling methods are consistent.
    """
    num = 10000 #100000
    stride = 4
    skip = 90
    n_anchors = 4
    n_milestones = 3
    
    # generate data to feed directly into MMVT_data_sample()
    model = base.Model()
    model.num_milestones = n_milestones
    model.num_anchors = n_anchors
    anchor0 = mmvt_base.MMVT_toy_anchor()
    anchor0.index = 0
    milestone_0_0 = base.Milestone()
    milestone_0_0.index = 0
    milestone_0_0.neighbor_anchor_index = 1
    milestone_0_0.alias_index = 1
    anchor0.milestones = [milestone_0_0]
    
    anchor1 = mmvt_base.MMVT_toy_anchor()
    anchor1.index = 1
    milestone_1_0 = base.Milestone()
    milestone_1_0.index = 0
    milestone_1_0.neighbor_anchor_index = 0
    milestone_1_0.alias_index = 1
    milestone_1_1 = base.Milestone()
    milestone_1_1.index = 1
    milestone_1_1.neighbor_anchor_index = 2
    milestone_1_1.alias_index = 2
    anchor1.milestones = [milestone_1_0, milestone_1_1]
    
    anchor2 = mmvt_base.MMVT_toy_anchor()
    anchor2.index = 2
    milestone_2_0 = base.Milestone()
    milestone_2_0.index = 1
    milestone_2_0.neighbor_anchor_index = 1
    milestone_2_0.alias_index = 1
    milestone_2_1 = base.Milestone()
    milestone_2_1.index = 2
    milestone_2_1.neighbor_anchor_index = 3
    milestone_2_1.alias_index = 2
    anchor2.milestones = [milestone_2_0, milestone_2_1]
    
    anchor3 = mmvt_base.MMVT_toy_anchor()
    anchor3.index = 3
    milestone_3_0 = base.Milestone()
    milestone_3_0.index = 2
    milestone_3_0.neighbor_anchor_index = 2
    milestone_3_0.alias_index = 1
    anchor3.milestones = [milestone_3_0]
    
    model.anchors = [anchor0, anchor1, anchor2, anchor3]
    
    # MMVT stats
    N_alpha_beta = {(0,1):12, (1,0):12,
                    (1,2):12, (2,1):12,
                    (2,3):6,  (3,2):6}
    k_alpha_beta = {(0,1):20.0, (1,0):10.0,
                    (1,2):10.0,  (2,1):(40.0/3.0),
                    (2,3):(20.0/3.0), (3,2):20.0}
    N_i_j_alpha = [{},
                   {(0,1):4, (1,0):4}, 
                   {(1,2):2, (2,1):2},
                   {}]
    R_i_alpha_total = [{0: 1.2},
                       {0: 1.2, 1:1.2},
                       {1: 1.2, 2:0.6},
                       {2: 0.6}]
    T_alpha_total = [1.2,
                     2.4,
                     1.8,
                     0.6]
    
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, N_alpha_beta, k_alpha_beta, N_i_j_alpha, 
            R_i_alpha_total, T_alpha_total)
    main_data_sample.calculate_pi_alpha()
    main_data_sample.fill_out_data_quantities()
    main_data_sample.compute_rate_matrix()
    main_data_sample.calculate_thermodynamics()
    main_data_sample.calculate_kinetics()
    mmvt_Q = main_data_sample.Q
    
    # Elber stats
    N_i_j = {(0,1): 4, (1,0): 4, (1,2): 2, (2,1): 2}
    R_i = {0: 2.4, 1: 2.4, 2: 1.2}
    elber_N = np.array([[0, 4, 0],
                        [4, 0, 2],
                        [0, 2, 0]])
    elber_R = np.array([[2.4],
                        [2.4],
                        [1.2]])
    
    elber_Q = np.zeros((n_milestones, n_milestones))
    for i in range(n_milestones):
        for j in range(n_milestones):
            key = (i,j)
            if key in N_i_j:
                elber_Q[i,j] = N_i_j[key] / R_i[i]
    
    for i in range(n_milestones):
        elber_Q[i,i] = -np.sum(elber_Q[i,:])
    
    # Make sure the two models make identical matrices
    assert np.isclose(mmvt_Q, elber_Q).all()
    
    # Now compare the distributions of both of them
    
    # MMVT matrix sampler
    data_sample_list, p_i_error, free_energy_profile_err, MFPTs_error, \
        k_off_error, k_ons_error = mmvt_analyze.monte_carlo_milestoning_error(
        main_data_sample, num=num, stride=stride, skip=skip, verbose=True)
    
    mmvt_q1_distribution = []
    mmvt_q2_distribution = []
    for data_sample in data_sample_list:
        mmvt_q1_distribution.append(data_sample.Q[0,1])
        mmvt_q2_distribution.append(data_sample.Q[1,0])
    
    # Elber matrix sampler
    elber_q1_distribution = []
    elber_q2_distribution = []
    for counter in range(num * (stride) + skip):
        #if verbose: print("MCMC stepnum: ", counter)
        elber_Qnew = markov_chain_monte_carlo\
            .irreversible_stochastic_matrix_algorithm_sample(
                elber_Q, elber_N, elber_R)
        if counter > skip and counter % stride == 0:
            elber_q1_distribution.append(elber_Q[0,1])
            elber_q2_distribution.append(elber_Q[1,0])
        
        elber_Q = elber_Qnew
    
    assert np.isclose(np.mean(elber_q1_distribution), 
                      np.mean(mmvt_q1_distribution), rtol=0.5, atol=0.01)
    assert np.isclose(np.std(elber_q1_distribution), 
                      np.std(mmvt_q1_distribution), rtol=0.5, atol=0.01)
    assert np.isclose(np.mean(elber_q2_distribution), 
                      np.mean(mmvt_q2_distribution), rtol=0.5, atol=0.01)
    assert np.isclose(np.std(elber_q2_distribution), 
                      np.std(mmvt_q2_distribution), rtol=0.5, atol=0.01)
    
    return
    
    
예제 #7
0
def test_MMVT_data_sample_fill_N_R_alpha_from_matrices():
    model = make_simple_model()
    N_i_j_alpha_exp = [{},
                   {(0,1):4, (1,0):4}, 
                   {(1,2):2, (2,1):2},
                   {}]
    R_i_alpha_total_exp = [{0: 1.2},
                       {0: 1.2, 1:1.2},
                       {1: 1.2, 2:0.6},
                       {2: 0.6}]
    mmvt_Nij_alpha = [
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]),
        np.array([
        [0, 4, 0],
        [4, 0, 0],
        [0, 0, 0]]),
        np.array([
        [0, 0, 0],
        [0, 0, 2],
        [0, 2, 0]]),
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])]
    mmvt_Ri_alpha = [
        np.array([
        [1.2],
        [0],
        [0]]),
        np.array([
        [1.2],
        [1.2],
        [0]]),
        np.array([
        [0],
        [1.2],
        [0.6]]),
        np.array([
        [0],
        [0],
        [0.6]])]
    N_i_j_alpha_start = [{},
                   {(0,1):3, (1,0):3}, 
                   {(1,2):3, (2,1):3},
                   {}]
    R_i_alpha_total_start = [{0: 1.0},
                       {0: 1.0, 1:1.0},
                       {1: 1.0, 2:0.4},
                       {2: 0.4}]
    T_alpha_total_start = [1.2,
                     2.4,
                     1.8,
                     0.6]
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, None, None, N_i_j_alpha_start, 
            R_i_alpha_total_start, T_alpha_total_start)
    main_data_sample.fill_N_R_alpha_from_matrices(mmvt_Nij_alpha, mmvt_Ri_alpha)
    assert len(N_i_j_alpha_exp) == len(main_data_sample.N_i_j_alpha)
    for alpha in range(len(N_i_j_alpha_exp)):
        mmvt_Nij_exp = N_i_j_alpha_exp[alpha]
        mmvt_Nij = main_data_sample.N_i_j_alpha[alpha]
        compare_dicts(mmvt_Nij_exp, mmvt_Nij)
    assert len(R_i_alpha_total_exp) == len(main_data_sample.R_i_alpha)
    for alpha in range(len(R_i_alpha_total_exp)):
        mmvt_Ri_exp = R_i_alpha_total_exp[alpha]
        mmvt_Ri = main_data_sample.R_i_alpha[alpha]
        compare_dicts(mmvt_Ri_exp, mmvt_Ri)
    return
예제 #8
0
def test_MMVT_data_sample_make_mcmc_quantities():
    model = make_simple_model()
    N_alpha_beta = {(0,1):12, (1,0):12,
                    (1,2):12, (2,1):12,
                    (2,3):6,  (3,2):6}
    k_alpha_beta = {(0,1):20.0, (1,0):10.0,
                    (1,2):10.0,  (2,1):(40.0/3.0),
                    (2,3):(20.0/3.0), (3,2):20.0}
    N_i_j_alpha = [{},
                   {(0,1):4, (1,0):4}, 
                   {(1,2):2, (2,1):2},
                   {}]
    R_i_alpha_total = [{0: 1.2},
                       {0: 1.2, 1:1.2},
                       {1: 1.2, 2:0.6},
                       {2: 0.6}]
    T_alpha_total = [1.2,
                     2.4,
                     1.8,
                     0.6]
    main_data_sample = mmvt_analyze.MMVT_data_sample(
            model, N_alpha_beta, k_alpha_beta, N_i_j_alpha, 
            R_i_alpha_total, T_alpha_total)
    main_data_sample.calculate_pi_alpha()
    k_alpha_beta_matrix, N_alpha_beta_matrix, T_alpha_matrix,\
        mmvt_Nij_alpha, mmvt_Ri_alpha, mmvt_Qij_alpha, T \
        = main_data_sample.make_mcmc_quantities()
    k_alpha_beta_matrix_exp = np.array([
        [-20.0, 20.0, 0.0, 0.0],
        [10.0, -20.0, 10.0, 0.0],
        [0.0, (40.0/3.0), -(40.0/3.0)-(20.0/3.0), (20.0/3.0)],
        [0.0, 0.0, 20.0, -20.0]])
    N_alpha_beta_matrix_exp = np.array([
        [0, 12, 0, 0],
        [12, 0, 12, 0],
        [0, 12, 0, 6],
        [0, 0, 6, 0]])
    T_alpha_matrix_exp = np.array([T_alpha_total]).T
    mmvt_Nij_alpha_exp = [
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]),
        np.array([
        [0, 4, 0],
        [4, 0, 0],
        [0, 0, 0]]),
        np.array([
        [0, 0, 0],
        [0, 0, 2],
        [0, 2, 0]]),
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])]
    mmvt_Ri_alpha_exp = [
        np.array([
        [1.2],
        [0],
        [0]]),
        np.array([
        [1.2],
        [1.2],
        [0]]),
        np.array([
        [0],
        [1.2],
        [0.6]]),
        np.array([
        [0],
        [0],
        [0.6]])]
    mmvt_Qij_alpha_exp = [
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]]),
        np.array([
        [-4/1.2, 4/1.2, 0],
        [4/1.2, -4/1.2, 0],
        [0, 0, 0]]),
        np.array([
        [0, 0, 0],
        [0, -2/1.2, 2/1.2],
        [0, 2/0.6, -2/0.6]]),
        np.array([
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])]   
    
    assert np.all(np.isclose(k_alpha_beta_matrix, k_alpha_beta_matrix_exp))
    assert np.all(np.isclose(N_alpha_beta_matrix, N_alpha_beta_matrix_exp))
    assert np.all(np.isclose(T_alpha_matrix, T_alpha_matrix_exp))
    assert len(mmvt_Nij_alpha_exp) == len(mmvt_Nij_alpha)
    for mmvt_Nij, mmvt_Nij_exp in zip(mmvt_Nij_alpha, mmvt_Nij_alpha_exp):
        assert np.all(np.isclose(mmvt_Nij, mmvt_Nij_exp))
    assert len(mmvt_Ri_alpha_exp) == len(mmvt_Ri_alpha)
    for mmvt_Ri, mmvt_Ri_exp in zip(mmvt_Ri_alpha, mmvt_Ri_alpha_exp):
        assert np.all(np.isclose(mmvt_Ri, mmvt_Ri_exp))
    assert len(mmvt_Qij_alpha_exp) == len(mmvt_Qij_alpha)
    for mmvt_Qij, mmvt_Qij_exp in zip(mmvt_Qij_alpha, mmvt_Qij_alpha_exp):
        assert np.all(np.isclose(mmvt_Qij, mmvt_Qij_exp))
    return