In [2]:
from pyCodeLib import *
import warnings
import glob
import re
import numpy as np
import pandas as pd
from Bio.PDB.Polypeptide import one_to_three

warnings.filterwarnings('ignore')


# sys.path.insert(0, MYHOME)
%load_ext autoreload
%autoreload 2

In [3]:
# plt.rcParams['figure.figsize'] = [16.18033, 10]
plt.rcParams['figure.figsize'] = 0.5*np.array([16.18033, 10])
# def gamma_format_convertion_iteration_to_simulation(iteration_gamma, gamma_for_simulation):
#     # gamma_location = "/Users/weilu/Research/server_backup/jan_2019/optimization/gammas_dec30/cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma"
#     # gamma_for_simulation = "/Users/weilu/Research/server_backup/jan_2019/optimization/iteration_gamma.dat"
#     gamma = iteration_gamma
#     gamma = -gamma  # caused by tradition.
#     # convert gamma to gamma used by simulation
#     with open(gamma_for_simulation, "w") as out:
#         c = 0
#         for i in range(20):
#             for j in range(i, 20):
#                 out.write(f"{gamma[c]:<.5f} {gamma[c]:10.5f}\n")
#                 c += 1
#         out.write("\n")
#         for i in range(20):
#             for j in range(i, 20):
#                 # protein, water
#                 out.write(f"{gamma[c]:<.5f} {gamma[c+210]:10.5f}\n")
#                 c += 1

res_type_map = {
    'A': 0,
    'C': 4,
    'D': 3,
    'E': 6,
    'F': 13,
    'G': 7,
    'H': 8,
    'I': 9,
    'K': 11,
    'L': 10,
    'M': 12,
    'N': 2,
    'P': 14,
    'Q': 5,
    'R': 1,
    'S': 15,
    'T': 16,
    'V': 19,
    'W': 17,
    'Y': 18
}
# res_type_map = gamma_se_map_1_letter = {   'A': 0,  'R': 1,  'N': 2,  'D': 3,  'C': 4,
#                             'Q': 5,  'E': 6,  'G': 7,  'H': 8,  'I': 9,
#                             'L': 10, 'K': 11, 'M': 12, 'F': 13, 'P': 14,
#                             'S': 15, 'T': 16, 'W': 17, 'Y': 18, 'V': 19}
res_type_map_letters = ['A', 'R', 'N', 'D', 'C', 'Q', 'E', 'G',
                        'H', 'I', 'L', 'K', 'M', 'F', 'P', 'S', 'T', 'W', 'Y', 'V']

inverse_res_type_map = dict(list(zip(list(range(20)), res_type_map_letters)))

def gamma_format_convertion_iteration_to_simulation(iteration_gamma, gamma_for_simulation, burial_gamma_for_simulation=None):
    # gamma_location = "/Users/weilu/Research/server_backup/jan_2019/optimization/gammas_dec30/cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma"
    # gamma_for_simulation = "/Users/weilu/Research/server_backup/jan_2019/optimization/iteration_gamma.dat"
    gamma = iteration_gamma
    gamma = -gamma  # caused by tradition.
    # convert gamma to gamma used by simulation
    with open(gamma_for_simulation, "w") as out:
        c = 0
        for i in range(20):
            for j in range(i, 20):
                out.write(f"{gamma[c]:<.5f} {gamma[c]:10.5f}\n")
                c += 1
        out.write("\n")
        for i in range(20):
            for j in range(i, 20):
                # protein, water
                out.write(f"{gamma[c]:<.5f} {gamma[c+210]:10.5f}\n")
                c += 1
    if burial_gamma_for_simulation:
        rhoGamma = pd.DataFrame(gamma[630:].reshape(3,20).T, columns=["rho1", "rho2", "rho3"]).reset_index()
        rhoGamma["oneLetter"] = rhoGamma["index"].apply(lambda x: inverse_res_type_map[x])
        rhoGamma["Residue"] = rhoGamma["index"].apply(lambda x: one_to_three(inverse_res_type_map[x]))
        rhoGamma = rhoGamma[["Residue", "rho1", "rho2", "rho3", "index", "oneLetter"]]
        g = rhoGamma[["rho1", "rho2", "rho3"]].values
        np.savetxt(burial_gamma_for_simulation, g, fmt='%7.4f')

with burial


In [73]:
# pre = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/gammas/"
pre = "/Users/weilu/Research/server/april_2019/optimization_test/gammas/"
# pp = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0"
pp = "proteins_name_list_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0phi_burial_well4.0"
A_name = pp + "_A"
B_name = pp + "_B"
B_filtered_name = pp + "_B_filtered"
P_name = pp + "_P"
Gamma_name = pp + "_gamma"
Gamma_filtered_name = pp + "_gamma_filtered"
Lamb_name = pp + "_lamb"
Lamb_filtered_name = pp + "_lamb_filtered"

A = np.loadtxt(pre+A_name)
B = np.loadtxt(pre+B_name)
B_filtered = np.loadtxt(pre+B_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Gamma = np.loadtxt(pre+Gamma_name)
Gamma_filtered = np.loadtxt(pre+Gamma_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb = np.loadtxt(pre+Lamb_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb_filtered = np.loadtxt(pre+Lamb_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})

half_B_name = pp + "_half_B"
half_B = np.loadtxt(pre+half_B_name)
other_half_B_name = pp + "_other_half_B"
other_half_B = np.loadtxt(pre+other_half_B_name)
std_half_B_name = pp + "_std_half_B"
std_half_B = np.loadtxt(pre+std_half_B_name)


pre = "/Users/weilu/Research/server/april_2019/"
location = pre + "phis/proteins_name_list_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0phi_burial_well4.0_phi_decoy_summary.txt"
A_prime = np.loadtxt(location)

In [72]:


In [34]:
A_prime.shape


Out[34]:
(690,)

In [74]:
B_inverse = np.linalg.pinv(B)

In [75]:
up = A.dot(B_inverse).dot(A) - A_prime.dot(B_inverse).dot(A)
down = A.dot(B_inverse).dot(A_prime) - A_prime.dot(B_inverse).dot(A_prime)
lambda_1 = up / down

In [76]:
up


Out[76]:
5.051516017418994

In [77]:
down


Out[77]:
-3499.9666820036396

In [78]:
lambda_1


Out[78]:
-0.0014433040301192618

In [79]:
g = B_inverse.dot(A - A_prime*lambda_1)

In [96]:
np.mean(g)


Out[96]:
0.06050412205310636

In [100]:
np.std(g)


Out[100]:
0.7123715906451454

In [97]:
g1 = B_inverse.dot(A)

In [98]:
np.mean(g1)


Out[98]:
0.05483645057684526

In [99]:
np.std(g1)


Out[99]:
0.7116097967447709

In [80]:
g.dot(A)


Out[80]:
16.257922504420286

In [88]:
g.dot(A_prime)


Out[88]:
16.2579225044202

In [101]:
g1.dot(A)


Out[101]:
16.24177156349257

In [102]:
g1.dot(A_prime)


Out[102]:
11.190255546073505

In [41]:
plt.plot(A)


Out[41]:
[<matplotlib.lines.Line2D at 0x1a1b733780>]

In [94]:
np.sum(abs(lambda_1*A_prime))


Out[94]:
0.3914945872362957

In [95]:
np.sum(abs(A))


Out[95]:
73.01364000000001

In [111]:
plt.rcParams['figure.figsize'] = np.array([16.18033, 10])
plt.plot(g1)
plt.plot(g, alpha=0.5)

# plt.yscale("log")


Out[111]:
[<matplotlib.lines.Line2D at 0x1a1e529828>]

In [105]:
plt.plot(g1)


Out[105]:
[<matplotlib.lines.Line2D at 0x1a1beeae48>]

In [103]:
plt.plot(A_prime)
plt.plot(lambda_1*A_prime)
plt.plot(A)

# plt.yscale("log")


No handles with labels found to put in legend.
Out[103]:
<matplotlib.legend.Legend at 0x1a1c128630>

In [ ]:


In [16]:
c = A0
A = [[-3, 1], [1, 2]]
b = [6, 4]
x0_bounds = (None, None)
x1_bounds = (-3, None)
from scipy.optimize import linprog
res = linprog(c, A_ub=A, b_ub=b, bounds=(x0_bounds, x1_bounds),
              options={"disp": True})



print(res)


Optimization terminated successfully.
         Current function value: -22.000000  
         Iterations: 5
     con: array([], dtype=float64)
     fun: -22.0
 message: 'Optimization terminated successfully.'
     nit: 5
   slack: array([3.90000000e+01, 1.77635684e-15])
  status: 0
 success: True
       x: array([10., -3.])

In [ ]:


In [13]:
test = np.dot(np.linalg.pinv(B), A)

In [14]:
test[:10]


Out[14]:
array([-0.1696517 , -0.09684112,  0.32163425, -0.24861446,  0.54592761,
       -0.25149968,  0.06390231,  0.34662149,  0.11408598,  0.28950261])

In [15]:
Gamma[:10]


Out[15]:
array([-0.16938, -0.09585,  0.32145, -0.24856,  0.5464 , -0.25204,
        0.06395,  0.34685,  0.11461,  0.28942])

In [10]:
total_phis = 690
num_decoys = 15000
filtered_gamma, filtered_B, filtered_lamb, P, lamb = get_filtered_gamma_B_lamb_P_and_lamb(A, B, half_B, other_half_B, std_half_B, total_phis, num_decoys, mode=2)


654

In [11]:
Gamma_filtered[:10]


Out[11]:
array([-0.16178+0.j, -0.08368+0.j,  0.31874+0.j, -0.24923+0.j,
        0.51143+0.j, -0.2453 +0.j,  0.06829+0.j,  0.34626+0.j,
        0.13185+0.j,  0.29272+0.j])

In [12]:
filtered_gamma[:10]


Out[12]:
array([-0.16204062, -0.10045653,  0.32024084, -0.24975294,  0.52752271,
       -0.24230321,  0.06673051,  0.34728753,  0.12864665,  0.28935561])

In [80]:
Gamma[:10]


Out[80]:
array([-0.36672,  0.08898, -0.00318,  0.33267, -1.4817 ,  0.13133,
        0.40348, -0.14097, -0.20817, -2.20075])

In [79]:
Gamma[630:]


Out[79]:
array([-0.14009, -0.25496, -0.25682, -0.27643,  0.17467, -0.28242,
       -0.31422, -0.13472, -0.17776,  0.18318,  0.04142, -0.26107,
       -0.17891,  0.06365, -0.17609, -0.22773, -0.0658 , -0.18529,
       -0.04176,  0.23534,  0.09679, -0.49751, -0.46232, -0.77906,
        1.26307, -0.61399, -0.86138, -0.2185 , -0.18548,  1.29716,
        0.71169, -0.73069,  0.24583,  0.64734, -0.26806, -0.35472,
        0.12022,  0.19507,  0.42189,  1.43618,  0.55265, -0.9863 ,
       -0.87701, -1.7389 ,  3.17942, -1.26118, -1.9753 , -0.30654,
       -0.18941,  3.40786,  2.03997, -1.70513,  1.08636,  1.77866,
       -0.42379, -0.57809,  0.4644 ,  0.9425 ,  1.34357,  3.72756])

In [101]:
burialOnly = np.loadtxt("/Users/weilu/Research/server/feb_2019/optimization_debug_only_burial/gammas/cath-dataset-nonredundant-S20Clean_phi_burial_well4.0_gamma")

In [112]:
# now, positive means favored.
rhoGamma = pd.DataFrame(-burialOnly.reshape(3,20).T, columns=["rho1", "rho2", "rho3"]).reset_index()
rhoGamma["oneLetter"] = rhoGamma["index"].apply(lambda x: inverse_res_type_map[x])
rhoGamma["Residue"] = rhoGamma["index"].apply(lambda x: one_to_three(inverse_res_type_map[x]))
rhoGamma = rhoGamma[["Residue", "rho1", "rho2", "rho3", "index", "oneLetter"]]
g = rhoGamma[["rho1", "rho2", "rho3"]].values
# np.savetxt("/Users/weilu/Research/server/feb_2019/burial_only_gamma.dat", g, fmt='%7.4f')
# rhoGamma
rhoGamma["hydrophobicityOrder"] = rhoGamma["oneLetter"].apply(lambda x: hydrophobicity_map[x])
rhoGamma.sort_values("hydrophobicityOrder")


Out[112]:
Residue rho1 rho2 rho3 index oneLetter hydrophobicityOrder
1 ARG 1.68010 1.69144 1.18949 1 R 0
11 LYS 0.96183 0.58746 0.13187 11 K 1
2 ASN 0.42031 0.32944 0.30461 2 N 2
5 GLN 1.41756 1.35558 1.02570 5 Q 3
3 ASP 0.64722 0.28147 0.05978 3 D 4
6 GLU 1.11516 0.71254 0.37777 6 E 5
8 HIS 1.10384 1.26635 0.96425 8 H 6
18 TYR 1.79590 2.60115 2.43177 18 Y 7
17 TRP 1.88372 2.69206 2.37320 17 W 8
15 SER 1.06307 0.99539 1.08353 15 S 9
16 THR 1.44128 1.61552 1.97838 16 T 10
7 GLY -0.07449 -0.48857 -0.26064 7 G 11
14 PRO -1.61721 -2.10260 -2.19886 14 P 12
0 ALA 1.01175 1.53878 2.09204 0 A 13
12 MET 1.36311 2.13074 2.47273 12 M 14
4 CYS 1.42019 2.55789 3.83290 4 C 15
13 PHE 1.72563 2.62465 2.51049 13 F 16
10 LEU 1.49584 2.54803 3.20308 10 L 17
19 VAL 1.07457 2.12543 3.85984 19 V 18
9 ILE 1.28959 2.43544 3.78443 9 I 19

In [106]:
hydrophobicity_letters = ['R', 'K', 'N', 'Q', 'D', 'E', 'H', 'Y',
                          'W', 'S', 'T', 'G', 'P', 'A', 'M', 'C', 'F', 'L', 'V', 'I']

hydrophobicity_map = dict(list(zip(hydrophobicity_letters, list(range(20)))))

In [108]:


In [113]:


In [121]:
location = "/Users/weilu/Research/database/queriedPDB.dat"
with open(location) as f:
    n = int(next(f))
    print(n)
    all_lines = f.read().splitlines()
    first_20 = all_lines[:20]
    print(first_20)


456
['1A2J', '1A3H', '1A5Y', '1A8Q', '1AGY', '1AK1', '1AKZ', '1AOV', '1ARL', '1AUZ', '1B1A', '1B31', '1B7U', '1B8X', '1BCO', '1BEE', '1BGT', '1BN6', '1BOH', '1BOI']

In [102]:
burialOnly.shape


Out[102]:
(60,)

In [ ]:
Gamma = np.loadtxt(pre+Gamma_name)

In [94]:
rho_table = [[0.0, 3.0], [3.0, 6.0], [6.0, 9.0]]
for i in range(3):
    print(rho_table[i][0], rho_table[i][1])


0.0 3.0
3.0 6.0
6.0 9.0

In [97]:
Gamma.shape


Out[97]:
(690,)

In [89]:
rhoGamma = pd.DataFrame(Gamma[630:].reshape(3,20).T, columns=["rho1", "rho2", "rho3"]).reset_index()
rhoGamma["oneLetter"] = rhoGamma["index"].apply(lambda x: inverse_res_type_map[x])
rhoGamma["Residue"] = rhoGamma["index"].apply(lambda x: one_to_three(inverse_res_type_map[x]))
rhoGamma = rhoGamma[["Residue", "rho1", "rho2", "rho3", "index", "oneLetter"]]
g = rhoGamma[["rho1", "rho2", "rho3"]].values
# np.savetxt("/Users/weilu/Research/server/feb_2019/iter_burial_gamma.dat", g, fmt='%7.4f')

In [90]:
rhoGamma


Out[90]:
Residue rho1 rho2 rho3 index oneLetter
0 ALA -0.14009 0.09679 0.55265 0 A
1 ARG -0.25496 -0.49751 -0.98630 1 R
2 ASN -0.25682 -0.46232 -0.87701 2 N
3 ASP -0.27643 -0.77906 -1.73890 3 D
4 CYS 0.17467 1.26307 3.17942 4 C
5 GLN -0.28242 -0.61399 -1.26118 5 Q
6 GLU -0.31422 -0.86138 -1.97530 6 E
7 GLY -0.13472 -0.21850 -0.30654 7 G
8 HIS -0.17776 -0.18548 -0.18941 8 H
9 ILE 0.18318 1.29716 3.40786 9 I
10 LEU 0.04142 0.71169 2.03997 10 L
11 LYS -0.26107 -0.73069 -1.70513 11 K
12 MET -0.17891 0.24583 1.08636 12 M
13 PHE 0.06365 0.64734 1.77866 13 F
14 PRO -0.17609 -0.26806 -0.42379 14 P
15 SER -0.22773 -0.35472 -0.57809 15 S
16 THR -0.06580 0.12022 0.46440 16 T
17 TRP -0.18529 0.19507 0.94250 17 W
18 TYR -0.04176 0.42189 1.34357 18 Y
19 VAL 0.23534 1.43618 3.72756 19 V

In [91]:
plot_contact_well(Gamma[:210], inferBound=True, invert_sign=False)



In [99]:
gamma_for_simulation = "/Users/weilu/Research/server/feb_2019/optimization/iteration_gamma.dat"
burial_gamma_for_simulation = "/Users/weilu/Research/server/feb_2019/optimization/iteration_burial_gamma.dat"
gamma_format_convertion_iteration_to_simulation(Gamma, gamma_for_simulation, burial_gamma_for_simulation=burial_gamma_for_simulation)

In [12]:
pre = "/Users/weilu/Research/server/feb_2019/jan_optimization/gammas/"
pp = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0"
Gamma_name = pp + "_gamma"
withoutBurialGamma = np.loadtxt(pre+Gamma_name)

In [84]:
plot_contact_well((-Gamma[:210]) - (-withoutBurialGamma[:210]), inferBound=True, invert_sign=False)



In [14]:
plot_contact_well(withoutBurialGamma[:210], inferBound=True)



In [85]:
plot_contact_well(Gamma[:210], inferBound=True, invert_sign=False)



In [67]:
plot_contact_well(Gamma_filtered[:210], inferBound=True)



In [8]:
plot_contact_well(Gamma[210:420], inferBound=True)



In [21]:
plot_contact_well(Gamma[420:630], inferBound=True)



In [19]:
plot_contact_well(withoutBurialGamma[210:420] - Gamma[210:420], inferBound=True)



In [20]:
plot_contact_well(withoutBurialGamma[420:630] - Gamma[420:630], inferBound=True)


without burial


In [35]:
# pre = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/gammas/"
pre = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/back_up_gammas/"
A_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_A"
B_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_B"
B_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_B_filtered"
P_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_P"
Gamma_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma"
Gamma_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma_filtered"
Lamb_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_lamb"
Lamb_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_lamb_filtered"

A = np.loadtxt(pre+A_name)
B = np.loadtxt(pre+B_name)
B_filtered = np.loadtxt(pre+B_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Gamma = np.loadtxt(pre+Gamma_name)
Gamma_filtered = np.loadtxt(pre+Gamma_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb = np.loadtxt(pre+Lamb_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb_filtered = np.loadtxt(pre+Lamb_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})

half_B_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_half_B"
half_B = np.loadtxt(pre+half_B_name)
other_half_B_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_other_half_B"
other_half_B = np.loadtxt(pre+other_half_B_name)
std_half_B_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_std_half_B"
std_half_B = np.loadtxt(pre+std_half_B_name)

In [57]:
pre = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/gammas/"
A_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_A"
B_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_B"
B_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_B_filtered"
P_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_P"
Gamma_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma"
Gamma_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_gamma_filtered"
Lamb_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_lamb"
Lamb_filtered_name = "cath-dataset-nonredundant-S20Clean_phi_pairwise_contact_well4.5_6.5_5.0_10phi_density_mediated_contact_well6.5_9.5_5.0_10_2.6_7.0_lamb_filtered"

A = np.loadtxt(pre+A_name)
B = np.loadtxt(pre+B_name)
B_filtered = np.loadtxt(pre+B_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Gamma = np.loadtxt(pre+Gamma_name)
Gamma_filtered = np.loadtxt(pre+Gamma_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb = np.loadtxt(pre+Lamb_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})
Lamb_filtered = np.loadtxt(pre+Lamb_filtered_name, dtype=complex, converters={
                           0: lambda s: complex(s.decode().replace('+-', '-'))})

In [84]:
plot_contact_well(Gamma[210:420], inferBound=True)



In [63]:
gamma_for_simulation = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/iteration_gamma.dat"
burial_gamma_for_simulation = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/iteration_burial_gamma.dat"
gamma_format_convertion_iteration_to_simulation(Gamma, gamma_for_simulation, burial_gamma_for_simulation=burial_gamma_for_simulation)


---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-63-1619c059697b> in <module>
      1 gamma_for_simulation = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/iteration_gamma.dat"
      2 burial_gamma_for_simulation = "/Users/weilu/Research/server_backup/feb_2019/jan_optimization/iteration_burial_gamma.dat"
----> 3 gamma_format_convertion_iteration_to_simulation(Gamma, gamma_for_simulation, burial_gamma_for_simulation=burial_gamma_for_simulation)
      4 

<ipython-input-62-9ac8d004bb3e> in gamma_format_convertion_iteration_to_simulation(iteration_gamma, gamma_for_simulation, burial_gamma_for_simulation)
     54     gamma = -gamma  # caused by tradition.
     55     # convert gamma to gamma used by simulation
---> 56     with open(gamma_for_simulation, "w") as out:
     57         c = 0
     58         for i in range(20):

FileNotFoundError: [Errno 2] No such file or directory: '/Users/weilu/Research/server_backup/feb_2019/jan_optimization/iteration_gamma.dat'

In [59]:
plt.plot(Lamb)
plt.plot(Lamb_filtered)


Out[59]:
[<matplotlib.lines.Line2D at 0x1a1bbec278>]

In [27]:
def get_filtered_gamma_B_lamb_P_and_lamb(A, B, half_B, other_half_B, std_half_B, total_phis, num_decoys, noise_iterations=10, relative_error_threshold=0.5):
    lamb, P = np.linalg.eig(B)
    lamb, P = sort_eigenvalues_and_eigenvectors(lamb, P)

    cutoff_modes = []
    for i_noise in range(noise_iterations):
        noisy_B = np.zeros((total_phis, total_phis))
        for i in range(total_phis):
            for j in range(i, total_phis):
                random_B_ij = np.random.normal(
                    loc=half_B[i][j], scale=std_half_B[i][j] / float(num_decoys))
                noisy_B[i][j] = noisy_B[j][i] = random_B_ij - \
                    other_half_B[i][j]

        noisy_lamb, noisy_P = np.linalg.eig(noisy_B)
        noisy_lamb, noisy_P = sort_eigenvalues_and_eigenvectors(
            noisy_lamb, noisy_P)

        try:
            cutoff_mode = np.where(
                np.abs(lamb - noisy_lamb) / lamb > relative_error_threshold)[0][0]
        except IndexError:
            cutoff_mode = len(lamb)
        cutoff_modes.append(cutoff_mode)

    cutoff_mode = min(cutoff_modes)
    print(cutoff_mode)

    filtered_lamb = np.copy(lamb)
    filtered_B_inv, filtered_lamb, P = get_filtered_B_inv_lambda_and_P(
        filtered_lamb, cutoff_mode, P)

    filtered_gamma = np.dot(filtered_B_inv, A)
    filtered_B = np.linalg.inv(filtered_B_inv)
    return filtered_gamma, filtered_B, filtered_lamb, P, lamb
def get_filtered_B_inv_lambda_and_P(filtered_lamb, cutoff_mode, P, method='extend_all_after_first_noisy_mode'):
    if method == 'zero_all_after_first_noisy_mode':
        filtered_lamb_inv = 1 / filtered_lamb
        # for "zeroing unreliable eigenvalues"
        filtered_lamb_inv[cutoff_mode:] = 0.0
        filtered_B_inv = np.dot(
            P, np.dot(np.diag(filtered_lamb_inv), np.linalg.inv(P)))
        filtered_lamb = 1 / filtered_lamb_inv
    if method == 'extend_all_after_first_noisy_mode':
        # for "extending lowest reliable eigenvalue"
        filtered_lamb[cutoff_mode:] = filtered_lamb[cutoff_mode - 1]
        filtered_B_inv = np.dot(
            P, np.dot(np.diag(1 / filtered_lamb), np.linalg.inv(P)))

    return filtered_B_inv, filtered_lamb, P


def sort_eigenvalues_and_eigenvectors(eigenvalues, eigenvectors):
    idx = eigenvalues.argsort()[::-1]
    eigenvalues = eigenvalues[idx]
    eigenvectors = eigenvectors[:, idx]
    return eigenvalues, eigenvectors

In [61]:
total_phis = 630
num_decoys = 1000
noise_iterations=10
relative_error_threshold=0.5


lamb, P = np.linalg.eig(B)
lamb, P = sort_eigenvalues_and_eigenvectors(lamb, P)

cutoff_modes = []
for i_noise in range(noise_iterations):
    noisy_B = np.zeros((total_phis, total_phis))
    for i in range(total_phis):
        for j in range(i, total_phis):
#             random_B_ij = np.random.normal(
#                 loc=half_B[i][j], scale=std_half_B[i][j] / float(num_decoys))
            random_B_ij = np.random.normal(
                loc=half_B[i][j], scale=std_half_B[i][j])
            noisy_B[i][j] = noisy_B[j][i] = random_B_ij - other_half_B[i][j]

    noisy_lamb, noisy_P = np.linalg.eig(noisy_B)
    noisy_lamb, noisy_P = sort_eigenvalues_and_eigenvectors(
        noisy_lamb, noisy_P)

    try:
        cutoff_mode = np.where(
            np.abs(lamb - noisy_lamb) / lamb > relative_error_threshold)[0][0]
    except IndexError:
        cutoff_mode = len(lamb)
    cutoff_modes.append(cutoff_mode)

cutoff_mode = min(cutoff_modes)
print(cutoff_mode)

filtered_lamb = np.copy(lamb)
filtered_B_inv, filtered_lamb, P = get_filtered_B_inv_lambda_and_P(
    filtered_lamb, cutoff_mode, P)

filtered_gamma = np.dot(filtered_B_inv, A)
filtered_B = np.linalg.inv(filtered_B_inv)
# return filtered_gamma, filtered_B, filtered_lamb, P, lamb


0

In [78]:
x = np.zeros(10000)
for i in range(10):
    x1 = np.random.normal(10, scale=10, size=10000)
    x += x1
x = x/10

In [82]:
plt.hist(x1, bins=100)
plt.hist(x, bins=100)


Out[82]:
(array([  1.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,   0.,
          0.,   2.,   0.,   3.,   1.,   2.,   5.,   9.,   9.,   8.,   9.,
         18.,  15.,  16.,  19.,  20.,  40.,  25.,  35.,  45.,  59.,  92.,
         82.,  82., 106.,  98., 135., 144., 172., 174., 215., 219., 226.,
        222., 272., 280., 297., 290., 322., 318., 322., 329., 302., 312.,
        323., 316., 294., 280., 310., 297., 264., 262., 235., 220., 232.,
        199., 163., 159., 152., 135., 127.,  96.,  87.,  79.,  67.,  66.,
         55.,  41.,  34.,  29.,  23.,  16.,  21.,  15.,  20.,   2.,   8.,
          7.,   5.,   1.,   4.,   0.,   1.,   1.,   0.,   0.,   0.,   0.,
          2.]),
 array([-3.95227878, -3.69337766, -3.43447654, -3.17557543, -2.91667431,
        -2.65777319, -2.39887208, -2.13997096, -1.88106984, -1.62216873,
        -1.36326761, -1.10436649, -0.84546538, -0.58656426, -0.32766314,
        -0.06876203,  0.19013909,  0.44904021,  0.70794132,  0.96684244,
         1.22574356,  1.48464467,  1.74354579,  2.00244691,  2.26134802,
         2.52024914,  2.77915026,  3.03805137,  3.29695249,  3.55585361,
         3.81475473,  4.07365584,  4.33255696,  4.59145808,  4.85035919,
         5.10926031,  5.36816143,  5.62706254,  5.88596366,  6.14486478,
         6.40376589,  6.66266701,  6.92156813,  7.18046924,  7.43937036,
         7.69827148,  7.95717259,  8.21607371,  8.47497483,  8.73387594,
         8.99277706,  9.25167818,  9.51057929,  9.76948041, 10.02838153,
        10.28728264, 10.54618376, 10.80508488, 11.06398599, 11.32288711,
        11.58178823, 11.84068934, 12.09959046, 12.35849158, 12.61739269,
        12.87629381, 13.13519493, 13.39409604, 13.65299716, 13.91189828,
        14.17079939, 14.42970051, 14.68860163, 14.94750274, 15.20640386,
        15.46530498, 15.72420609, 15.98310721, 16.24200833, 16.50090944,
        16.75981056, 17.01871168, 17.2776128 , 17.53651391, 17.79541503,
        18.05431615, 18.31321726, 18.57211838, 18.8310195 , 19.08992061,
        19.34882173, 19.60772285, 19.86662396, 20.12552508, 20.3844262 ,
        20.64332731, 20.90222843, 21.16112955, 21.42003066, 21.67893178,
        21.9378329 ]),
 <a list of 100 Patch objects>)

In [70]:
plt.imshow(noisy_B)
plt.colorbar()


Out[70]:
<matplotlib.colorbar.Colorbar at 0x1a22376cf8>

In [71]:
plt.imshow(B)
plt.colorbar()


Out[71]:
<matplotlib.colorbar.Colorbar at 0x1a1c01e518>

In [67]:
plt.plot(noisy_lamb)


Out[67]:
[<matplotlib.lines.Line2D at 0x1a1bedfef0>]

In [68]:
plt.plot(lamb)


Out[68]:
[<matplotlib.lines.Line2D at 0x1a1bf355f8>]

In [49]:
np.std([1,2,3])


Out[49]:
0.816496580927726

In [52]:
x = np.array([1,2,3])
np.sqrt(np.mean(abs(x - x.mean())**2))


Out[52]:
0.816496580927726

In [53]:
np.mean(abs(x - x.mean())**2)


Out[53]:
0.6666666666666666

In [ ]:
np.std()

In [48]:
np.abs(lamb - noisy_lamb) / lamb


Out[48]:
array([4.42362404e-03, 1.93893429e-03, 2.64110767e-03, 1.42428813e-03,
       1.50997454e-03, 2.87469480e-03, 2.46747095e-03, 6.72837058e-04,
       4.61360827e-03, 4.42200233e-04, 1.75412824e-04, 3.93785708e-05,
       1.31943593e-04, 7.79426405e-04, 1.51753926e-02, 9.32879407e-04,
       1.52171863e-02, 3.85908448e-03, 1.80824521e-03, 7.61036944e-03,
       1.50037002e-03, 5.59814416e-03, 5.66798828e-04, 5.91414606e-03,
       1.44194857e-03, 2.68685516e-04, 7.05757823e-03, 2.02485028e-03,
       2.78607884e-03, 5.84702011e-03, 8.21529995e-03, 2.75890912e-03,
       1.60692462e-03, 2.86493136e-03, 3.02323239e-03, 6.98463693e-03,
       4.19093134e-03, 7.71313831e-03, 5.09829324e-04, 4.21634388e-03,
       8.68223284e-03, 5.27529835e-03, 6.44646008e-03, 7.12112177e-03,
       6.70522986e-03, 7.12967458e-04, 2.57357373e-03, 5.14090074e-03,
       5.35104515e-03, 7.94396756e-03, 2.15530435e-03, 3.12668421e-03,
       4.02619438e-04, 3.29334879e-03, 3.70430819e-03, 6.91930688e-03,
       5.19251578e-03, 3.95339598e-03, 4.57757630e-03, 1.11066834e-03,
       5.55676027e-05, 2.05370629e-03, 2.51762026e-03, 9.46572806e-03,
       1.54067848e-03, 3.65582827e-03, 3.48280581e-03, 8.96836349e-03,
       2.49350499e-03, 2.40055571e-03, 8.29528598e-03, 8.11869956e-03,
       6.48527136e-03, 2.68067931e-03, 1.35665295e-03, 8.10169835e-04,
       5.25226399e-03, 6.18052337e-04, 2.12497960e-03, 1.04271965e-03,
       3.05137266e-04, 3.62293307e-03, 5.60445397e-04, 2.13466301e-03,
       7.40460689e-03, 2.04794830e-03, 8.72190972e-04, 3.00575635e-03,
       2.42360382e-03, 4.63745319e-03, 2.45516922e-03, 2.56259104e-03,
       7.69791245e-04, 6.81322051e-03, 3.93850502e-03, 4.36511926e-03,
       6.39813354e-04, 1.52781233e-03, 1.19383982e-03, 1.61118461e-03,
       4.71652708e-03, 6.95752424e-03, 3.55878057e-03, 5.38471749e-04,
       2.57421873e-03, 2.10041318e-03, 2.74124202e-03, 5.45966218e-03,
       5.81026270e-04, 1.39600335e-03, 1.85399357e-03, 3.26563459e-03,
       1.19100047e-03, 2.23323899e-03, 2.84330600e-03, 2.43734001e-03,
       6.29440226e-03, 2.13871690e-03, 3.63677643e-03, 1.83308769e-03,
       2.29555503e-03, 3.29037925e-03, 5.37135283e-03, 2.57387608e-04,
       6.54019278e-03, 2.46059278e-03, 1.66212662e-03, 1.51654172e-03,
       6.70082602e-04, 7.89205285e-03, 9.29016343e-03, 4.98407758e-03,
       9.67256310e-04, 3.54551978e-03, 8.38682109e-04, 2.00142010e-03,
       4.75566371e-03, 6.96593254e-03, 1.34840337e-03, 2.00568209e-03,
       6.95812077e-03, 2.33319972e-03, 2.85794930e-03, 3.26141128e-03,
       8.01414442e-06, 3.18304283e-03, 6.40739013e-03, 1.05402864e-03,
       2.16632860e-03, 6.75838967e-03, 4.47320510e-03, 2.29949688e-04,
       3.51336541e-03, 1.48250612e-03, 1.43457901e-03, 1.94294589e-03,
       2.95637458e-03, 1.13844660e-03, 1.97916551e-03, 2.62677875e-03,
       5.35764288e-03, 1.84437837e-03, 3.29815442e-03, 1.17321667e-03,
       9.87540927e-04, 6.77763011e-04, 1.60533728e-03, 5.91378665e-04,
       5.80526309e-04, 3.07604280e-03, 5.72148858e-04, 1.79251156e-03,
       8.76828160e-04, 6.59287000e-03, 3.87650423e-04, 3.13479573e-03,
       1.38083127e-04, 2.30093313e-03, 1.89271194e-03, 9.85456995e-04,
       1.72499314e-04, 8.36731090e-04, 2.87479909e-03, 2.81058083e-03,
       5.69138726e-03, 1.65798968e-03, 1.57026876e-03, 3.59093367e-03,
       4.48070854e-03, 2.58758264e-03, 4.13197502e-03, 1.10278459e-03,
       9.34345503e-04, 1.89240916e-03, 1.48243024e-03, 2.03778373e-03,
       9.54160575e-05, 1.94608701e-03, 4.05025455e-03, 7.26620600e-04,
       5.46660822e-03, 4.25108755e-03, 5.40004353e-03, 3.52644406e-03,
       3.51413141e-03, 2.72248384e-03, 8.90894369e-04, 3.23692369e-03,
       3.57870142e-03, 4.84781585e-04, 1.76052760e-03, 9.15669025e-04,
       2.71647173e-03, 1.16819136e-03, 1.65164687e-03, 2.29971980e-03,
       3.13032246e-03, 2.43658486e-03, 1.01524264e-03, 3.25438206e-03,
       2.55696461e-03, 5.09619580e-03, 3.88329429e-03, 3.24731694e-03,
       2.02623257e-03, 3.38605024e-04, 1.24808366e-04, 6.91265880e-04,
       2.62393941e-03, 2.42920871e-03, 2.53481436e-03, 3.16624832e-03,
       5.55205257e-05, 3.76934088e-04, 1.79711512e-03, 1.80061887e-03,
       5.48630057e-03, 2.49609974e-03, 3.14921905e-03, 3.53048914e-03,
       5.91337168e-04, 8.44378402e-03, 4.08573501e-03, 4.62247852e-03,
       4.84933688e-04, 3.35979155e-03, 1.36157704e-03, 4.07307651e-03,
       1.49796615e-05, 2.74055910e-03, 4.83275573e-03, 3.57433756e-03,
       3.23362818e-03, 1.46792824e-03, 2.69909265e-03, 1.28859101e-03,
       3.27429731e-03, 2.32492590e-03, 4.40924208e-03, 3.76652543e-03,
       3.40057383e-03, 4.52326558e-06, 4.28112001e-03, 2.35760899e-03,
       5.19803516e-03, 3.35234781e-03, 4.28798936e-03, 4.10734867e-03,
       2.91884405e-03, 1.39627408e-03, 5.61737698e-03, 4.75562054e-03,
       9.78552165e-05, 3.74737626e-03, 1.04468515e-03, 4.74853106e-05,
       3.52030644e-03, 1.41709256e-03, 1.65568899e-03, 5.98527497e-04,
       1.63115595e-03, 4.29346686e-03, 1.05580639e-03, 1.27258548e-03,
       2.46410732e-03, 3.37771488e-03, 1.06668644e-03, 2.94519763e-03,
       3.47468926e-03, 1.64502825e-03, 3.22087071e-03, 4.27629467e-03,
       4.86550519e-03, 9.01006239e-04, 3.74724614e-03, 1.33544924e-03,
       1.94446616e-03, 9.20055115e-03, 3.36314830e-03, 4.29653012e-03,
       1.74502151e-03, 4.11489382e-03, 2.78724620e-04, 1.88303518e-03,
       3.21409376e-03, 5.11097179e-03, 2.95187771e-04, 2.09503125e-03,
       3.38329397e-04, 5.98657350e-04, 9.04333121e-04, 3.70583572e-03,
       2.46236708e-03, 1.42449624e-03, 1.58487363e-03, 3.70109282e-03,
       2.62724148e-03, 2.70955816e-03, 5.01427274e-03, 2.64845410e-03,
       1.91988039e-03, 9.39790228e-04, 2.66040493e-03, 9.69308016e-04,
       1.88420651e-03, 2.29243668e-04, 2.00340615e-03, 2.49602538e-03,
       4.95371097e-04, 1.69089732e-03, 2.53690681e-03, 3.02710013e-03,
       2.56657996e-03, 9.63577288e-06, 7.24910030e-04, 2.85980452e-03,
       2.09736841e-03, 1.81419442e-03, 8.99800230e-04, 3.78746188e-05,
       6.16256060e-04, 3.56465069e-03, 3.30808670e-03, 1.72607387e-03,
       3.50827263e-03, 1.85007628e-03, 1.43986626e-03, 2.27910182e-03,
       4.47829650e-04, 1.76083435e-03, 2.12595125e-04, 2.31983078e-05,
       3.33914715e-04, 2.68955586e-03, 1.72718191e-03, 3.88014136e-04,
       8.31266654e-04, 5.45520339e-04, 1.76726285e-03, 1.10943753e-03,
       3.98492684e-03, 1.30254111e-03, 2.36008661e-03, 9.17075792e-04,
       1.99998968e-04, 3.88098838e-03, 2.40207049e-03, 6.18277343e-04,
       2.85580698e-03, 5.31984904e-04, 2.41821989e-04, 2.51212603e-03,
       1.29244610e-03, 5.13591456e-03, 1.51073270e-03, 2.85007051e-04,
       1.37581040e-04, 2.53649211e-03, 1.10773006e-03, 2.01816841e-03,
       3.23312836e-03, 3.05929327e-03, 8.73170441e-04, 1.92455669e-03,
       9.85868736e-04, 2.46451063e-03, 2.39546630e-04, 4.96373668e-04,
       2.16225964e-03, 2.67758559e-03, 1.72616982e-03, 1.87555334e-03,
       3.99572680e-04, 4.77601271e-04, 5.32563554e-03, 5.73888739e-03,
       1.67588255e-03, 1.65576227e-03, 2.06565838e-04, 2.22004472e-03,
       3.20984751e-03, 8.67597645e-04, 1.67376360e-03, 1.04466775e-04,
       9.13822600e-04, 5.47783659e-04, 4.84090546e-03, 4.33265465e-03,
       3.40762814e-03, 3.99482831e-03, 1.68681081e-03, 1.70157325e-03,
       9.03266730e-04, 7.75054158e-04, 7.55453954e-04, 7.47158207e-04,
       2.25114029e-03, 1.82577064e-03, 4.51910832e-04, 2.84171902e-03,
       1.16701215e-03, 2.61135870e-03, 2.44832268e-03, 1.95645335e-03,
       1.76338594e-03, 1.16068626e-03, 2.48330064e-03, 3.09704417e-03,
       2.61770328e-03, 3.23191645e-04, 2.01302680e-03, 9.09650895e-04,
       2.21906677e-03, 3.97221169e-03, 2.42523538e-03, 4.07159072e-03,
       2.69697374e-03, 2.14921063e-03, 1.03313067e-03, 1.97954730e-03,
       2.29465024e-03, 1.25265523e-04, 3.97548695e-04, 8.90690009e-04,
       3.19203896e-03, 3.45084705e-03, 1.33603071e-04, 5.58404169e-04,
       1.69784258e-03, 1.05612365e-03, 2.25786778e-03, 2.26947420e-03,
       1.00470834e-03, 2.19432505e-03, 1.40431201e-03, 3.70475393e-05,
       2.98294013e-03, 1.23863108e-04, 9.36587431e-04, 1.93375076e-04,
       2.87685837e-03, 2.41548029e-03, 1.98152074e-04, 2.14010971e-05,
       1.05381256e-04, 3.84781065e-03, 7.57608958e-04, 3.25539741e-03,
       2.23063362e-03, 3.99716811e-03, 2.24836624e-03, 2.79072566e-03,
       1.14234351e-03, 1.52243981e-03, 3.99407440e-03, 4.62254037e-03,
       2.04603634e-03, 1.68944868e-03, 2.17836512e-03, 1.10627014e-03,
       5.90666847e-04, 8.68062260e-04, 1.45217016e-04, 1.65221675e-04,
       5.34391144e-04, 2.38612536e-03, 2.87440587e-03, 1.45710181e-03,
       5.92717752e-04, 2.14863486e-03, 7.46122812e-03, 1.27350781e-03,
       3.77846649e-04, 2.68190776e-03, 3.12234906e-03, 2.26653377e-03,
       2.00160543e-03, 1.77533843e-03, 2.90624478e-04, 1.67029667e-03,
       7.74682261e-04, 2.64293372e-03, 3.91618603e-04, 2.44366831e-03,
       4.63513081e-03, 2.56965624e-03, 4.99525154e-03, 4.75741115e-03,
       3.70670198e-03, 2.19642660e-03, 1.65308397e-04, 1.47405453e-03,
       9.19112890e-04, 2.69276336e-03, 1.09019300e-03, 2.71990005e-03,
       3.27738919e-05, 1.92034836e-03, 2.29854372e-03, 1.55699275e-03,
       4.40641294e-03, 4.93420091e-03, 1.64706937e-03, 2.47266198e-03,
       4.99920786e-03, 4.03563022e-03, 1.18834132e-03, 4.09304327e-03,
       5.37033757e-03, 4.26120758e-03, 4.92659292e-03, 2.38518845e-03,
       1.05579736e-03, 6.29342939e-04, 2.21656608e-03, 3.97325784e-03,
       5.69173569e-03, 3.74493759e-04, 6.45108692e-03, 3.49158858e-04,
       1.04573774e-03, 1.55634756e-03, 2.56000181e-05, 7.53691497e-03,
       1.76729903e-03, 6.53838595e-03, 2.08820847e-03, 5.17964219e-03,
       3.66086898e-03, 3.70556638e-03, 6.25160567e-03, 1.87461366e-03,
       5.71962646e-03, 6.06187625e-04, 2.86389725e-03, 1.89211684e-03,
       1.80179123e-03, 4.31861250e-03, 6.47350108e-03, 4.30546052e-03,
       1.23651128e-03, 9.45514104e-04, 8.49082522e-04, 3.96387197e-03,
       3.15647728e-03, 1.06901204e-03, 1.68005594e-03, 5.40318405e-03,
       1.99912931e-03, 7.24345635e-03, 1.42458348e-03, 9.08007869e-04,
       1.99362351e-03, 7.62758182e-04, 5.95416930e-04, 3.32360358e-04,
       1.70782768e-03, 5.16087881e-03, 3.57652859e-03, 3.87260212e-04,
       2.57549228e-03, 8.01105075e-04, 1.45280474e-03, 1.98751737e-03,
       1.26832629e-03, 1.52510476e-03, 8.35611100e-04, 2.53782312e-03,
       1.07794870e-03, 9.25721510e-06, 2.64332904e-03, 1.38074009e-05,
       4.05658602e-03, 3.68977408e-04, 6.86421992e-03, 1.84638529e-03,
       1.32425590e-03, 4.07595544e-03, 1.66965327e-04, 1.52281734e-03,
       4.89992714e-05, 7.09341460e-05, 8.68265289e-03, 5.92787653e-03,
       3.65727046e-03, 3.64530273e-03, 3.80774137e-03, 1.18598645e-03,
       5.93196254e-04, 1.00136771e-02, 2.95821126e-04, 3.48247461e-03,
       5.25775895e-03, 1.33507390e-03, 3.37283345e-03, 2.96220889e-03,
       5.87344323e-03, 2.52115459e-03, 8.03239411e-03, 3.50339356e-02,
       2.20024602e-04, 2.29353004e-02, 3.20332091e-02, 1.78633132e-03,
       4.72466300e-03, 4.22161311e-03, 1.16678858e-03, 4.88051923e-04,
       1.55335198e-03, 2.37771015e-03])

In [47]:
np.where(np.abs(lamb - noisy_lamb) / lamb > relative_error_threshold)


Out[47]:
(array([], dtype=int64),)

In [37]:
np.sum(filtered_B-B_filtered)


Out[37]:
(1.0000000248911933e-05+0j)

In [45]:
plt.plot(noisy_lamb)


Out[45]:
[<matplotlib.lines.Line2D at 0x1a1b8089b0>]

In [43]:
plt.imshow(half_B)
plt.colorbar()


Out[43]:
<matplotlib.colorbar.Colorbar at 0x1a1a984e10>

In [42]:
plt.imshow(std_half_B)
plt.colorbar()


Out[42]:
<matplotlib.colorbar.Colorbar at 0x1a1a355be0>

In [22]:
lamb, P = np.linalg.eig(B)
lamb, P = sort_eigenvalues_and_eigenvectors(lamb, P)

In [28]:
plt.plot(lamb)


Out[28]:
[<matplotlib.lines.Line2D at 0x1a19ac3208>]

In [ ]:


In [54]:
plot_contact_well(Gamma[:210], inferBound=True)



In [55]:
plot_contact_well(Gamma[210:420], inferBound=True)



In [56]:
plot_contact_well(Gamma[420:], inferBound=True)



In [5]:
plot_contact_well(Gamma_filtered[:210], inferBound=True)



In [6]:
plot_contact_well(Gamma[210:420], inferBound=True)



In [7]:
plot_contact_well(Gamma_filtered[210:420], inferBound=True)



In [8]:
plot_contact_well(Gamma[420:], inferBound=True)



In [9]:
plot_contact_well(Gamma_filtered[420:], inferBound=True)



In [10]:
np.sum(Gamma - Gamma_filtered)


Out[10]:
0j

In [17]:
plot_contact_well(Gamma[:210], inferBound=True)



In [18]:
plot_contact_well(Gamma_filtered[:210], inferBound=True)



In [19]:
plot_contact_well(Gamma[210:420], inferBound=True)



In [20]:
plot_contact_well(Gamma_filtered[210:420], inferBound=True)



In [16]:
plot_contact_well(Gamma[420:], inferBound=True)



In [15]:
plot_contact_well(Gamma_filtered[420:], inferBound=True)



In [ ]: