<jupyter_start><jupyter_text>Fetch Dataset<jupyter_code>from SCFInitialGuess.utilities.dataset import extract_triu_batch, AbstractDataset
from sklearn.model_selection import train_test_split

data_path = "../../../dataset/EthenT/"
postfix = "EthenT"
dim = 72
N_ELECTRONS = 16
basis = "6-311++g**"

#data_path = "../../../butadien/data/"
#postfix = ""
#dim = 26


def split(x, y, ind):
    return x[:ind], y[:ind], x[ind:], y[ind:]

#[S, P] = np.load()
S = np.load(join(data_path, "S" + postfix + ".npy"))
P = np.load(join(data_path, "P" + postfix + ".npy"))

index = np.load(join(data_path, "index" + postfix + ".npy"))


#ind = int(0.8 * len(index))
ind = 0

molecules = np.load(join(data_path, "molecules" + postfix + ".npy"))
molecules = (molecules[:ind], molecules[ind:])
#molecules = ([], molecules)

s_triu_norm, mu, std = AbstractDataset.normalize(S)

s_train, p_train, s_test, p_test = split(S.reshape(-1, dim, dim), P.reshape(-1, dim, dim), ind)
#s_test = S.reshape(-1, dim, dim)
#p_test = S.reshape(-1, dim, dim)
subfolder = "EmbeddedDensities/"
import seaborn as sns

sns.heatmap(P[0].reshape(dim, dim))<jupyter_output><empty_output><jupyter_text>Utilities<jupyter_code>from SCFInitialGuess.utilities.analysis import mf_initializer as mf_initializer_standard

def mf_initializer(mol):
    mf = mf_initializer_standard(mol)
    return mf
from SCFInitialGuess.utilities.analysis import make_results_str, measure_all_quantities
from SCFInitialGuess.utilities.dataset import StaticDataset

dataset = StaticDataset(
    train=(s_train, p_train),
    #train=(None, None),
    validation=(None, None),
    test=(s_test, p_test),
    mu=mu,
    std=std
)
print(make_results_str(measure_all_quantities(
    p_test,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output>--- Absolute Error ---
0.00000E+00 +- 0.00000E+00
--- Symmetry Error ---
0.00000E+00 +- 0.00000E+00
--- Idempotence Error ---
3.07625E-17 +- 6.96180E-18
--- Occupance Error ---
5.12322E-15 +- 4.26503E-15
--- HF Energy Error ---
1.09598E-14 +- 1.22730E-14
--- Avg. Iterations ---
1.00000E+00 +- 0.00000E+00
--- Avg. Iterations W/O Non Converged ---
1.00000E+00 +- 0.00000E+00
--- Num. Not Convd. ---
0<jupyter_text>Pure blocksi will now set all other elements zero  Utilities<jupyter_code>from SCFInitialGuess.utilities.constants import number_of_basis_functions as N_BASIS

mol = molecules[1][0]

mask = np.zeros((dim, dim))

current_dim = 0
for atom in mol.species:
    
    # calculate block range
    index_start = current_dim
    current_dim += N_BASIS[basis][atom] 
    index_end = current_dim
    
    # calculate logical vector
    L = np.arange(dim)
    L = np.logical_and(index_start <= L, L < index_end)
    
    m = np.logical_and.outer(L, L)
    mask = np.logical_or(mask, m)
    
#mask
import seaborn as sns

sns.heatmap(mask.astype("int"))
from SCFInitialGuess.utilities.dataset import reconstruct_from_triu

def extract_pure(p):
    p_batch = p.copy()
    p_batch[np.logical_not(mask)] = 0
    return p_batch

p_pure = np.array(list(map(extract_pure, p_test))).astype("float64")
np.save(data_path + subfolder + "p_pure.npy", p_pure)
sns.heatmap(p_pure[0])
print(make_results_str(measure_all_quantities(
    p_pure,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output>--- Absolute Error ---
8.20873E-03 +- 6.77712E-03
--- Symmetry Error ---
0.00000E+00 +- 0.00000E+00
--- Idempotence Error ---
8.46690E-03 +- 1.43952E-03
--- Occupance Error ---
3.07556E+00 +- 6.47749E-01
--- HF Energy Error ---
4.28340E+00 +- 1.13191E+00
--- Avg. Iterations ---
3.78601E+01 +- 2.29211E+01
--- Avg. Iterations W/O Non Converged ---
3.26089E+01 +- 1.46931E+01
--- Num. Not Convd. ---
78<jupyter_text>Embedding the Blocks In Classical Guesses<jupyter_code>from SCFInitialGuess.utilities.dataset import reconstruct_from_triu

def embedd(x, y):
    p = x.copy()
    p[mask] = (y.copy())[mask]
    return p

def embedd_batch(p_batch):
    p_embedded = []
    for (p_guess, p_conv) in zip(p_batch, p_test):
        p_embedded.append(embedd(p_guess, p_conv))
    return np.array(p_embedded)<jupyter_output><empty_output><jupyter_text>Convergedn in Converged<jupyter_code>p_test_test = embedd_batch(p_test)
print(make_results_str(measure_all_quantities(
    p_test_test,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output>--- Absolute Error ---
0.00000E+00 +- 0.00000E+00
--- Symmetry Error ---
0.00000E+00 +- 0.00000E+00
--- Idempotence Error ---
3.07625E-17 +- 6.96180E-18
--- Occupance Error ---
5.12322E-15 +- 4.26503E-15
--- HF Energy Error ---
1.13005E-14 +- 1.26327E-14
--- Avg. Iterations ---
1.00000E+00 +- 0.00000E+00
--- Avg. Iterations W/O Non Converged ---
1.00000E+00 +- 0.00000E+00
--- Num. Not Convd. ---
0<jupyter_text>H_core<jupyter_code>from pyscf.scf import hf

p_1e = np.array([ 
    hf.init_guess_by_1e(mol.get_pyscf_molecule()) for mol in molecules[1]
]).astype("float64")
np.save(data_path + subfolder + "p_1e.npy", p_1e)
print(make_results_str(measure_all_quantities(
    p_1e,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output>--- Absolute Error ---
2.81846E-02 +- 3.21377E-02
--- Symmetry Error ---
0.00000E+00 +- 0.00000E+00
--- Idempotence Error ---
4.99210E-17 +- 1.44397E-17
--- Occupance Error ---
5.15694E-15 +- 4.22830E-15
--- HF Energy Error ---
1.98890E+01 +- 1.35611E+00
--- Avg. Iterations ---
9.80849E+01 +- 1.04199E+01
--- Avg. Iterations W/O Non Converged ---
4.81892E+01 +- 1.87686E+01
--- Num. Not Convd. ---
964<jupyter_text>Embedd H_core<jupyter_code>p_embedded_1e = embedd_batch(p_1e)
np.save(data_path + subfolder + "p_embedded_1e.npy", p_embedded_1e)
print(make_results_str(measure_all_quantities(
    p_embedded_1e,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
117/201<jupyter_output><empty_output><jupyter_text>GWH<jupyter_code>from pyscf.scf import hf

p_gwh = np.array([ 
    hf.init_guess_by_wolfsberg_helmholtz(mol.get_pyscf_molecule()) for mol in molecules[1]
]).astype("float64")
np.save(data_path + subfolder + "p_gwh.npy", p_gwh)
sns.heatmap(p_gwh[0])
print(make_results_str(measure_all_quantities(
    p_gwh,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
190/201<jupyter_output><empty_output><jupyter_text>Embedded GWH<jupyter_code>p_embedded_gwh = embedd_batch(p_gwh)
np.save(data_path + subfolder + "p_embedded_gwh.npy", p_embedded_gwh)
sns.heatmap(p_embedded_gwh[0])
print(make_results_str(measure_all_quantities(
    p_embedded_gwh,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
import seaborn as sns<jupyter_output><empty_output><jupyter_text>SAD<jupyter_code>from pyscf.scf import hf

p_sad = np.array([
    hf.init_guess_by_atom(mol.get_pyscf_molecule()) for mol in molecules[1]
]).astype("float64")

sns.heatmap(p_sad[0])
sns.heatmap(np.abs(p_sad[0]-p_pure[0]))
np.save(data_path + subfolder + "p_sad.npy", p_sad)
print(make_results_str(measure_all_quantities(
    p_sad,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output>--- Absolute Error ---
1.18737E-02 +- 9.66758E-03
--- Symmetry Error ---
5.57138E-20 +- 1.32777E-20
--- Idempotence Error ---
1.16617E-02 +- 6.15850E-04
--- Occupance Error ---
4.06024E-15 +- 3.48928E-15
--- HF Energy Error ---
4.72071E-01 +- 2.54231E-01
--- Avg. Iterations ---
3.24326E+01 +- 1.98629E+01
--- Avg. Iterations W/O Non Converged ---
2.91038E+01 +- 1.33407E+01
--- Num. Not Convd. ---
47<jupyter_text>Embedded SAD<jupyter_code>p_embedded_sad = embedd_batch(p_sad)
sns.heatmap(p_embedded_sad[0])
np.save(data_path + subfolder + "p_embedded_sad.npy", p_embedded_sad)
print(make_results_str(measure_all_quantities(
    p_embedded_sad,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
11/201<jupyter_output><empty_output><jupyter_text>McWheeny<jupyter_code>from SCFInitialGuess.nn.post_processing import multi_mc_wheeny

def multi_mc_wheeny_batch(p_batch):
    p_res = []
    for p, s in zip(p_batch, s_test):
        p_res.append(multi_mc_wheeny(p, s, 5))
    return np.asarray(p_res)
s_test.shape<jupyter_output><empty_output><jupyter_text>GWH<jupyter_code>p_embedded_gwh_mcw5 = multi_mc_wheeny_batch(p_embedded_gwh)
np.save(data_path + subfolder + "p_embedded_gwh_mcw5.npy", p_embedded_gwh_mcw5)
sns.heatmap(p_embedded_gwh_mcw5[0])
print(make_results_str(measure_all_quantities(
    p_embedded_gwh_mcw5,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
49/201<jupyter_output><empty_output><jupyter_text>SAD<jupyter_code>p_embedded_sad_mcw5 = multi_mc_wheeny_batch(p_embedded_sad)
np.save(data_path + subfolder + "p_embedded_sad_mcw5.npy", p_embedded_sad_mcw5)
sns.heatmap(p_embedded_gwh_mcw5[0])
print(make_results_str(measure_all_quantities(
    p_embedded_sad_mcw5,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
114/201<jupyter_output><empty_output><jupyter_text>Embedd Outer<jupyter_code>from SCFInitialGuess.utilities.dataset import reconstruct_from_triu

def embedd_outer(x, y):
    p = x.copy()
    p[np.logical_not(mask)] = (y.copy())[np.logical_not(mask)]
    return p

def embedd_batch_outer(p_batch):
    p_embedded = []
    for (p_guess, p_conv) in zip(p_batch, p_test):
        p_embedded.append(embedd_outer(p_guess, p_conv))
    return np.array(p_embedded)<jupyter_output><empty_output><jupyter_text>Converged<jupyter_code>print(make_results_str(measure_all_quantities(
    embedd_batch_outer(p_test),
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
print(make_results_str(measure_all_quantities(
    embedd_batch_outer(p_embedded_sad),
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))<jupyter_output><empty_output><jupyter_text>SAD<jupyter_code>p_embedded_outer_sad = embedd_batch_outer(p_sad)
sns.heatmap(p_embedded_outer_sad[0])
np.save(data_path + subfolder + "p_embedded_outer_sad.npy", p_embedded_outer_sad)
print(make_results_str(measure_all_quantities(
    p_embedded_outer_sad,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
68 / 201<jupyter_output><empty_output><jupyter_text>GWH<jupyter_code>p_embedded_outer_gwh = embedd_batch_outer(p_gwh)
sns.heatmap(p_embedded_outer_gwh[0])
np.save(data_path + subfolder + "p_embedded_outer_gwh.npy", p_embedded_outer_gwh)
print(make_results_str(measure_all_quantities(
    p_embedded_outer_gwh,
    dataset,
    molecules[1],
    N_ELECTRONS,
    mf_initializer,
    dim,
    is_triu=False,
    is_dataset_triu=False,
    s=S[ind:]
)))
194 / 201<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14263485477178423, 'fraction_numerical': 0.059474412171507604, 'mean_word_length': 4.114500442086649, 'pattern_counts': {'":': 0, '<': 70, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '247643', 'license': 'MIT', 'n_tokens_mistral': 5453, 'n_tokens_neox': 4897, 'n_words': 723, 'path': 'thesis/notebooks/AtomicBlocks/Density/InterpolationDensity.ipynb', 'repo_name': 'jcartus/SCFInitialGuess', 'size': '11568'}