# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Deep AI
#     language: python
#     name: dl
# ---

# +
import numpy as np
import torch

from dcj_comp import dcj_dist
# -

# version 2.0
# distance between result sequence and the median sequence
for seqlen in [10, 50, 100]:
    print(f'seqlen: {seqlen}')
    print(f'\t lower_bound \t dist_mean \t ratio \t\t std \t\t number')
    for rate in list(range(1, 10)) + [10, 15, 20]:
        rate = rate / 10
        fname = (f'val_seq_g3m_3_' 
                 f'{seqlen}_{int(seqlen * rate)}' 
                 f'_200/raw/g3raw_' 
                 f'{seqlen}_{int(seqlen * rate)}.pt')
        source, target = torch.load(fname)

        lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
                                       dcj_dist(g1, g3)[-1], 
                                       dcj_dist(g2, g3)[-1]])/2) 
                      for g1, g2, g3 in source])
        
        tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
        # dist_list = np.array([d[-1] for d in tmp])
        # dist_diff = np.abs(dist_list - lower_bound)
        
        dist_diff = np.abs([dcj_dist(a, b[0])[-1] for a, b in zip(target, tmp)])
        
        print(f'{rate} & \t'
              f'{np.mean(lower_bound):>7.4f} &\t' 
              f'{dist_diff.mean():>7.4f} &\t'
              f'{dist_diff.mean()/np.mean(lower_bound) :>7.4f} &\t'
              f'{dist_diff.std():>7.4f} &\t'
             f'{np.sum(dist_diff == 0):>4} &\t \\\\ \hline')
    print('*************')

import matplotlib as mpl
mpl.rcParams['pdf.fonttype'] = 42

import matplotlib.pyplot as plt

# for seqlen in [10, 50, 100]:
result = []
seqlen = 10
for rate in list(range(1, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    source, target = torch.load(fname)

    # lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
    #                                dcj_dist(g1, g3)[-1], 
    #                                dcj_dist(g2, g3)[-1]])/2) 
    #               for g1, g2, g3 in source])

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
    # dist_list = np.array([d[-1] for d in tmp])
    # dist_diff = np.abs(dist_list - lower_bound)

    dist_diff = np.abs([dcj_dist(a, b[0])[-1] for a, b in zip(target, tmp)])

    # tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
    # dist_list = np.array([d[-1] for d in tmp])
    # dist_diff = np.abs(dist_list - lower_bound)
    result.append(dist_diff)

bins = np.arange(np.max(result) + 1)
# x = np.arange(np.max(result) + 1)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(np.ceil(np.max(H[1])) + 1))
# plt.xticklabels(x)
plt.savefig('g3val_dist_10.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)

# for seqlen in [10, 50, 100]:
result = []
seqlen = 50
for rate in list(range(1, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    source, target = torch.load(fname)

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')

    dist_diff = np.abs([dcj_dist(a, b[0])[-1] for a, b in zip(target, tmp)])
    result.append(dist_diff)

# bins = np.arange(np.max(result) + 1)
# x = np.arange(11)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(0, np.ceil(np.max(H[1])) + 1, 3))
# plt.xticklabels(x)
plt.savefig('g3val_dist_50.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)

# for seqlen in [10, 50, 100]:
result = []
seqlen = 100
for rate in list(range(1, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    source, target = torch.load(fname)

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')

    dist_diff = np.abs([dcj_dist(a, b[0])[-1] for a, b in zip(target, tmp)])
    result.append(dist_diff)

x = np.arange(11)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(0, np.ceil(np.max(H[1])) + 1, 5))
# plt.xticklabels(x)
plt.savefig('g3val_dist_100.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)

for seqlen in [10, 50, 100]:
    print(f'seqlen: {seqlen}')
    print(f'\t\t lower_bound \t dist_mean \t ratio \t\t std \t\t number')
    for rate in list(range(1, 10)) + [10, 15, 20]:
        rate = rate / 10
        fname = (f'val_seq_g3m_3_' 
                 f'{seqlen}_{int(seqlen * rate)}' 
                 f'_200/raw/g3raw_' 
                 f'{seqlen}_{int(seqlen * rate)}.pt')
        seq = torch.load(fname)[0]

        lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
                                       dcj_dist(g1, g3)[-1], 
                                       dcj_dist(g2, g3)[-1]])/2) 
                      for g1, g2, g3 in seq])

        tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
        dist_list = np.array([d[-1] for d in tmp])
        dist_diff = np.abs(dist_list - lower_bound)
        print(f'{rate} & \t'
              f'{np.mean(lower_bound):>7.4f} &\t' 
              f'{dist_diff.mean():>7.4f} &\t'
              f'{dist_diff.mean()/np.mean(lower_bound) :>7.4f} &\t'
              f'{dist_diff.std():>7.4f} &\t'
             f'{np.sum(dist_list == lower_bound):>4} &\t \\\\ \hline')
    print('*************')



import matplotlib as mpl
mpl.rcParams['pdf.fonttype'] = 42

import matplotlib.pyplot as plt

# for seqlen in [10, 50, 100]:
result = []
seqlen = 10
for rate in list(range(2, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    seq = torch.load(fname)[0]

    lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
                                   dcj_dist(g1, g3)[-1], 
                                   dcj_dist(g2, g3)[-1]])/2) 
                  for g1, g2, g3 in seq])

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
    dist_list = np.array([d[-1] for d in tmp])
    dist_diff = np.abs(dist_list - lower_bound)
    result.append(dist_diff)

bins = np.arange(np.max(result) + 1)
# x = np.arange(np.max(result) + 1)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(np.ceil(np.max(H[1])) + 1))
# plt.xticklabels(x)
plt.savefig('g3val_dist_10.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)

# for seqlen in [10, 50, 100]:
result = []
seqlen = 50
for rate in list(range(2, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    seq = torch.load(fname)[0]

    lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
                                   dcj_dist(g1, g3)[-1], 
                                   dcj_dist(g2, g3)[-1]])/2) 
                  for g1, g2, g3 in seq])

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
    dist_list = np.array([d[-1] for d in tmp])
    dist_diff = np.abs(dist_list - lower_bound)
    result.append(dist_diff)

# bins = np.arange(np.max(result) + 1)
# x = np.arange(11)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(0, np.ceil(np.max(H[1])) + 1, 5))
# plt.xticklabels(x)
plt.savefig('g3val_dist_50.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)

# for seqlen in [10, 50, 100]:
result = []
seqlen = 100
for rate in list(range(2, 10)) + [10, 15, 20]:
    rate = rate / 10
    fname = (f'val_seq_g3m_3_' 
             f'{seqlen}_{int(seqlen * rate)}' 
             f'_200/raw/g3raw_' 
             f'{seqlen}_{int(seqlen * rate)}.pt')
    seq = torch.load(fname)[0]

    lower_bound = np.array([np.ceil(np.sum([dcj_dist(g1, g2)[-1], 
                                   dcj_dist(g1, g3)[-1], 
                                   dcj_dist(g2, g3)[-1]])/2) 
                  for g1, g2, g3 in seq])

    tmp = torch.load(f'dist_result_pt/{seqlen:0>4}_{rate}.pt')
    dist_list = np.array([d[-1] for d in tmp])
    dist_diff = np.abs(dist_list - lower_bound)
    result.append(dist_diff)

x = np.arange(11)
plt.figure()
H = plt.hist(result, bins = 11)
plt.xticks(np.arange(0, np.ceil(np.max(H[1])) + 1, 5))
# plt.xticklabels(x)
plt.savefig('g3val_dist_100.svg', bbox_inches = 'tight')
plt.show()

np.sum(H[0], axis = 1)



StarcoderdataJupyterScriptsDedupFiltered