<jupyter_start><jupyter_text>CryptoPETS PETS is the characters set used by early Commodore computers e.g. the Commodore C64. It is comfortingly familiar to the elder millenials<jupyter_code>import hashlib
import json
import numpy as np
import pickle
import random

import sys
sys.path.append('..')
sys.path.append('/home/tnn1t1s/art/cpunks-10k')
import matplotlib.pyplot as plt
import cpunks.cpunks10k as cpunks10k
import cryptopunks.utils as cp
%matplotlib inline
data_dir="/home/tnn1t1s/art/colorpunx/cryptopunks/data"
_colors = None
_imgmap = None

with open(f"{data_dir}/_colors_count.pickle", 'rb') as f:
    _colors = pickle.load(f)
sorted_colors=cp.sort_dict_by_function_of_value(_colors)
d={}
i=1
for k in sorted_colors.keys():
    hexkey = cp.color_str_to_hex(k)
    d[hexkey]={
        'original_key': k,
        'id': i,
        'name': hexkey,
        'description': f"{hexkey} is one of 222 colors in the cryptopunx color pallette. It is used by {len(_colors[k])} unique punks",
        'punks': _colors[k]
    } 
    #with open(f"{assets_dir}/json/colorpunx{i}.json", "w") as outfile:
    #     json.dump(d[hexkey], outfile)
    #mpimg.imsave(f"{assets_dir}/images/colorpunx{i}.png", color_imgs[k])
    i = i + 1
class Colormap:
    def __init__(self, d):
        self.d = {d[k]['original_key'] : d[k]['id'] for k in d.keys()}
        self.d_ = {int(d[k]['id']) : d[k]['original_key'] for k in d.keys()}
        self.d['[0. 0. 0. 0.]'] = 221
        self.d_[221] = '[0. 0. 0. 0.]'
        
    def get_rgbt(self, i):
        return self.d_[i]
    
    def get_id(self, rgbt):
        return self.d[rgbt]

cm = Colormap(d)
(
    cm.get_id('[0.35686275 0.28627452 0.2        1.        ]') == 1,
    cm.get_rgbt(1) == '[0.35686275 0.28627452 0.2        1.        ]' 
)
def one_hot(i,N):
    X = np.zeros(N)
    X[i]=1
    return X
def bloc_party(input_arr, selector):
    return np.block([[selector(i) for i in j] for j in input_arr])
def get_tile_images(image, width=8, height=8):
    _nrows, _ncols, depth = image.shape
    _size = image.size
    _strides = image.strides

    nrows, _m = divmod(_nrows, height)
    ncols, _n = divmod(_ncols, width)
    if _m != 0 or _n != 0:
        return None

    return np.lib.stride_tricks.as_strided(
        np.ravel(image),
        shape=(nrows, ncols, height, width, depth),
        strides=(height * _strides[0], width * _strides[1], *_strides),
        writeable=False
    )

def rgb2gray(rgb):
    weights = [0.2989, 0.5870, 0.1140]
    return np.dot(rgb[...,:3], weights)

import matplotlib.image as mpimg
petscii = mpimg.imread(f"/home/tnn1t1s/Downloads/petscii.png")
PETS_ = get_tile_images(petscii, width=8, height=8)
PETS_  = PETS_.reshape(280,8,8,4)#.astype(np.uint8)
cmap = plt.get_cmap('gray')

PETS_GRAY = np.array([rgb2gray(img) for img in PETS_])
#PETS_  = PETS_.reshape(280,8,8,4)#.astype(np.uint8)
#plt.imshow(PETS_[163])
#PETS_[163].shape
plt.imshow(PETS_GRAY[163], cmap=cmap)
square = np.array([[83, 2, 0],[1, 2, 0], [1, 2, 0]])
all_hash = np.array([[163, 163, 163],[163, 163, 163], [163, 163, 163]])


fig = plt.figure(figsize=(4,8))
fig.add_subplot(1,2, 1)
plt.imshow(bloc_party(square, lambda i: PETS_GRAY[i]), cmap=cmap)
fig.add_subplot(1,2, 2)
plt.imshow(bloc_party(all_hash, lambda i: PETS_GRAY[i]), cmap=cmap)
cpunx = cpunks10k.cpunks10k()
(X_train, Y_train), (X_test, Y_test), (labels) = cpunx.load_data()
plt.imshow(X_train[0])<jupyter_output><empty_output><jupyter_text>![cmpunk.jpeg](attachment:cmpunk.jpeg)<jupyter_code>def cm_punk(punk, cm):
    '''convert a (24,24,4) punk to a (24,24) encoded punk using cm'''
    return np.array([[cm.get_id(str(y)) for y in x] for x in punk])

#Y_ = np.array([[cm.get_id(str(y)) for y in x] for x in X_train[0]])
#punk_1 = cm_punk(Y_[0], cm)
#np.unique(punk_1)
X_train_cm = [cm_punk(img, cm) for img in X_train]
# bg 221
# hair 197
# skin 219
# ? 222
# eyes_1 104
# eyes_2 103
# mouth 195
punk_to_c64 = {}
punk_to_c64[221] = 160
punk_to_c64[197] = 163
punk_to_c64[219] = 83
punk_to_c64[222] = 92  #rnd
punk_to_c64[104] = 54
punk_to_c64[103] = 63
punk_to_c64[195] = 90

def p64(i):
    try:
        return PETS_GRAY[punk_to_c64[i]]
    except:
        r = random.randint(10,150)
        punk_to_c64[i] = r
        return PETS_GRAY[r]
l = bloc_party(X_train_cm[5], lambda i: p64(i))
c64_punks = [bloc_party(X_train_cm[i], lambda x: p64(x)) for i in range(64,128)]
plt.imshow(c64_punks[7], cmap=cmap)
n=8  
(x,y) = (n,n)
# create figure
fig = plt.figure(figsize=(x*2,y*2))


for i in range(0,64):
    fig.add_subplot(x, y, i+1)
    plt.imshow(c64_punks[i], cmap=cmap)
    plt.axis('off')
    plt.title(i)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1638745704467354, 'fraction_numerical': 0.06464776632302406, 'mean_word_length': 3.7375381485249237, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '521093', 'license': 'Apache-2.0', 'n_tokens_mistral': 2233, 'n_tokens_neox': 1997, 'n_words': 423, 'path': 'nb/CryptoPETS.ipynb', 'repo_name': 'tnn1t1s/colorpunx', 'size': '4656'}