<jupyter_start><jupyter_text>1) Set up<jupyter_code># rewind back to root
from os import path, chdir, listdir
chdir('..')
# imports
import numpy as np
from ucr import load_UCR_dataset
from utils import plot_with_labels
from config import PATH_TO_DATA, PATH_TO_MODELS, PATH_TO_DATA_UCR, PATH_TO_MODELS_UCR
from itertools import compress
from matplotlib import pyplot as plt
from sklearn.manifold import TSNE
from models.models_utils import load_pretrained

%matplotlib inline<jupyter_output><empty_output><jupyter_text>2) Pre-trained models<jupyter_code># List pre-trained
ls_pretrained = listdir( path.join(PATH_TO_MODELS, PATH_TO_MODELS_UCR) )
ls_pretrained = [i_[:-4] for i_ in ls_pretrained if ".pkl" in i_]

# Cut name extension
extension = [i_.index('_CausalCNN') for i_ in ls_pretrained]
ls_pretrained = [i_[:j_] for (i_, j_) in zip(ls_pretrained, extension)]


# Select a couple at random
id_selected = np.random.choice(len(ls_pretrained), 5)
ls_pretrained = list( compress(ls_pretrained, id_selected) )

print(ls_pretrained)

# Load them
loaded_models = [load_pretrained(i_, 'encoder') for i_ in ls_pretrained]
loaded_models<jupyter_output>['TwoLeadECG', 'UWaveGestureLibraryY', 'Ham', 'Meat', 'Rock']<jupyter_text>3) Data<jupyter_code># Load data
complete_path = path.join(PATH_TO_DATA, PATH_TO_DATA_UCR)
loaded_data = [load_UCR_dataset(complete_path, i_) for i_ in ls_pretrained]

print(ls_pretrained)
# Encode data
id_ = 2
print(ls_pretrained[id_])

# Pick data
mdl = loaded_models[id_]
dt_tr = loaded_data[id_][0] # training data
dt_trlab = loaded_data[id_][1] # training labels
dt_ts = loaded_data[id_][2] # test data
dt_tslab = loaded_data[id_][3] # test labels

# Encode
enc_tr = mdl.encode(dt_tr)
enc_ts = mdl.encode(dt_ts)
dt_tr.shape
enc_tr.shape
# Dimensionality reduction
tsne = TSNE(n_components=2, verbose=1, perplexity=40, n_iter=300)
ENC_tr_tsne = tsne.fit_transform(enc_tr)
ENC_ts_tsne = tsne.fit_transform(enc_ts)
print(ENC_tr_tsne.shape)
hF, Ax = plot_with_labels(ENC_tr_tsne, dt_trlab, marker='o',
                          legend='TRAIN', 
                          title='Dataset: %s'%(ls_pretrained[id_]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10664819944598337, 'fraction_numerical': 0.007848568790397045, 'mean_word_length': 6.247491638795987, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '328114', 'license': 'Apache-2.0', 'n_tokens_mistral': 853, 'n_tokens_neox': 825, 'n_words': 182, 'path': 'notebooks/pretrained_encoders.ipynb', 'repo_name': 'YounesZ/unsupervisedTS', 'size': '2166'}