<jupyter_start><jupyter_text>Key functions<jupyter_code>def gen_seed(x_k, x_l):
    str_repr = ''.join(x_k.astype(str)) + ''.join(x_l.astype(str))
    return int(hashlib.sha256(str_repr.encode('utf-8')).hexdigest(), 16)  % 10**9
    

def braid(x_k, x_l, q):
    np.random.seed(gen_seed(x_k, x_l))
    mask = np.random.rand(len(x_k)) < 1/(q+1)
    u = x_k.copy()
    u[mask] = x_l[mask]
    return u


def hamming(x_k, x_l):
    return np.mean(x_k != x_l)<jupyter_output><empty_output><jupyter_text>Setup<jupyter_code>np.random.seed(0)
x_star = np.random.randint(0, Z, N)
xs = [np.random.randint(0, Z, N) for i in range(M)]

def encode(s):
    y = x_star.copy()
    for t, a in enumerate(s):
        idx = A.index(a)
        y = braid(y, xs[idx], t+1)
    return y


def decode(y, l, return_d=False, force_decode=None):
    # get *set* of elements in sequence
    min_idxs = np.argsort([hamming(x, y) for x in xs])[:l]
    vs = [xs[idx] for idx in min_idxs]
    # reconstruct sequence
    y_star = x_star.copy()
    s_hat = ''
    d = [hamming(y_star, y)]
    for t in range(1, l+1):
        us = [braid(y_star, v, t) for v in vs]
        j = np.argmin([hamming(u, y) for u in us])
        
        if force_decode is not None and len(force_decode) >= t:
            next_sym = force_decode[t-1]
            next_x = xs[A.index(next_sym)]
        else:
            next_sym = A[min_idxs[j]]
            next_x = vs[j]
            
        s_hat += next_sym
        y_star = braid(y_star, next_x, t)
        d.append(hamming(y_star, y))
        
    if not all(y_star == y):
        print('Default reconstruction failed.')
        
    if not return_d:
        return s_hat
    else:
        return s_hat, np.array(d)<jupyter_output><empty_output><jupyter_text>Demo<jupyter_code>print('Hamming similarities between symbols:')
symbol_dists = []
for k in range(M-1):
    for l in range(k+1, M):
        symbol_dists.append(1 - hamming(xs[k], xs[l]))
print('Min = ', np.min(symbol_dists))
print('Max = ', np.max(symbol_dists))
print('Mean = ', np.mean(symbol_dists))
print('Std = ', np.std(symbol_dists))
s = 'random vectors for the win'
y = encode(s)
print(y)
s_star = decode(y, len(s))
print(s_star)
test_seqs = [
    'abcde',
    'abced',
    'aabcd',
    'zyxwv',
    'zyxvw',
    'aaazz',
    'abbbaaab',
    'hi agostina'
]

for s in test_seqs:
    s_hat = decode(encode(s), len(s))
    print(s, '--> y --> ', s_hat, '(', s == s_hat, ')')
s = 'hyperdimensional computing via crossover'
y = encode(s)
s_hat, d = decode(y, len(s), return_d=True)

t = np.arange(len(s)+1)
fig, ax = plt.subplots(1, 1, figsize=(10, 5), tight_layout=True)
ax.plot(t, d, lw=2, c='k')

ax.plot(t, 1 - t/(len(t)-1), c='gray', ls='--')

ax.set_xlim(-1, len(t))
ax.set_ylim(-.05, 1.05)

ax.set_xticks(t)
ax.set_xticklabels('*' + s_hat)

ax.grid()

set_font_size(ax, 16)
s = 'hyperdimensional computing via crossover'
y = encode(s)
s_hat, d = decode(y, len(s), return_d=True, force_decode='h')

t = np.arange(len(s)+1)
fig, ax = plt.subplots(1, 1, figsize=(10, 5), tight_layout=True)
ax.plot(t, d, lw=2, c='k')

ax.plot(t, 1 - t/(len(t)-1), c='gray', ls='--')

ax.set_xlim(-1, len(t))
ax.set_ylim(-.05, 1.05)

ax.set_xticks(t)
ax.set_xticklabels('*' + s_hat)

ax.grid()

set_font_size(ax, 16)
s = 'aaaaaaabbbbbbbccccccc'
y = encode(s)
s_hat, d = decode(y, len(s), return_d=True, force_decode='aaaaaaabbbbbbbccccccc')

t = np.arange(len(s)+1)
fig, ax = plt.subplots(1, 1, figsize=(10, 5), tight_layout=True)
ax.plot(t, d, lw=2, c='k')

ax.plot(t, 1 - t/(len(t)-1), c='gray', ls='--')

ax.set_xlim(-1, len(t))
ax.set_ylim(-.05, 1.05)

ax.set_xticks(t)
ax.set_xticklabels('*' + s_hat)

ax.grid()

set_font_size(ax, 16)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1890293089540199, 'fraction_numerical': 0.01936004302231783, 'mean_word_length': 3.330616996507567, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '665520', 'license': 'MIT', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1649, 'n_words': 332, 'path': 'analysis.ipynb', 'repo_name': 'rkp8000/crossing_over', 'size': '3719'}