<jupyter_start><jupyter_text>Style transfer<jupyter_code>m = 500
x_train_encoded = encoder.predict(x_train[:m], batch_size=batch_size)
x_train_encoded.shape
gd = genre_dict
one_to_one = False
one_to_one = True

dn = config.plots_dir
if one_to_one:
    transformations = io.load(dn + 'transformations-one-to-one.pkl')
    min_transformations = io.load(dn + 'min_transformations-one-to-one.pkl')    
    labelled_result = io.load(dn + 'ncd_evaluation-one-to-one_AB.pkl')
    labelled_result_min = io.load(dn + 'ncd_evaluation-one-to-one_AB_min.pkl')
    genre_dict = label_dict
else:
    best_dims = io.load(dn + 'transformations-best_dims.pkl')
    transformations = io.load(dn + 'transformations.pkl')
    min_transformations = io.load(dn + 'min_transformations.pkl')
    labelled_result = io.load(dn + 'ncd_evaluation_AB.pkl')
    labelled_result_min = io.load(dn + 'ncd_evaluation_AB_min.pkl')
    genre_dict = gd
def sample_dict(d, i=0):
    k = list(d.keys())[i]
    return k, d[k]
# utils.reload(utils)
labelled_result = utils.clean_dict(labelled_result, r=2, verbose=1)
# utils.reload(utils)
sample = utils.get(labelled_result, i=10)[0]
sample
# labelled_result[sample]
genre_a = utils.get(labelled_result[sample])[0]
genre_b,_, scores = utils.get(labelled_result[sample][genre_a])
scores_min = utils.get(labelled_result[sample][genre_a])[2]
xlabels = np.round(list(scores.keys()), 2)
genre_a, genre_b
# # for k,v in scores.items():
# #     print('%s: \t%f' % (k,v))
# print(xlabels)
# plot.custom({sample: list(scores.values())}, options={'x_labels': xlabels},
#             type_='bar', y_scale_margin=0.01,figsize=(10,3), show=True)
# plot.custom({sample: list(scores_min.values())}, options={'x_labels': xlabels},
#             type_='bar', y_scale_margin=0.01,figsize=(10,3), show=True)
genre_dict[sample], sample
utils.reload(plot, midi.pitches)
z_sample = x_train_encoded[genre_dict[genre_a][0]]
print('Original')
plot.single(x_train[genre_dict[sample][0]][:40])
print('Identity (transformation)')
plot.single(generator.predict(np.array([z_sample]))[0,:40]);
transformation = transformations[genre_a][genre_b]
min_transformation = min_transformations[genre_a][genre_b]
z_transformed = []
z_transformed_min = []
d = {}
for a in [0, 0.125, 0.2, 0.21, 0.22, 0.24]:
    z_ = models.apply_transformation(z_sample, transformation, a)
    z_transformed.append(z_)
    z_ = models.apply_transformation(z_sample, min_transformation, a)
    z_transformed_min.append(z_)

x_generated = generator.predict(np.array(z_transformed))
x_generated_min = generator.predict(np.array(z_transformed_min))
print('%s \n  %s \t-> %s '%(sample, genre_a, genre_b))
# latent_space = models.gen_latent(generator, latent_dim=latent_dim, n=3, m=2, plot_result=True)
plot.multi(x_generated, crop_size=100)
plot.multi(x_generated_min, crop_size=100)
# utils.reload(midi, midi.decode)
for i in range(x_generated.shape[0]):
    x_ = x_generated[i]
    mid = midi.decode.track(context, x_)
    io.export_midifile(mid, config.export_dir + ('x-generated-%i'%i))
    x_ = x_generated_min[i]
    mid = midi.decode.track(context, x_)    
    io.export_midifile(mid, config.export_dir + ('x-generated_min-%i'%i))    
def summarize(data):
    """data :: {samples: {genre a: {genre b: grid}}}
    grid = {parameter: outcome}
    
    return :: {parameter: [value] }
    """
    summary = collections.defaultdict(list)
    for original in data.keys():
        for genre_a in data[original].keys():
            for genre_b, grid in data[original][genre_a].items():
                for k,v in grid.items():
                    summary[k].append(v)
    return summary

def evaluate(data, verbose=0):
    """
    data :: {parameter: [value] }
    return :: {statistic: {parameter: value}}
    """
    result = utils.summary_multi(data, mode=list)
    std = []
    for k,v in data.items():
        std.append(np.std(v))
    
    result['std'] = std
    if verbose: 
        print(result['mean'])
    return result
result = summarize(labelled_result)
result_min = summarize(labelled_result_min)
xlabels = np.round(list(result.keys()), 3)
summary = evaluate(result)
summary_min = evaluate(result_min)
summary_ = summary.copy() # {'Mean': summary['mean'], 'Median': summary['median']}
summary_min_ = summary_min.copy()
summary_.pop('std')
summary_min_.pop('std');
io.save_dict(config.results_dir, 'ncd_result-summary', summary)
io.save_dict(config.results_dir, 'ncd_result_min-summary', summary_min)
xlabels
options = {'x_labels': xlabels, 'x_label':'Transformation intensity', 'y_label': 'ncd', 'legend':True}
plot.custom(summary_, 'NCD', options,
            type_='line', min_y_scale=None, max_y_scale=None, y_scale_margin=0.0001,figsize=(9,3), show=True)
plot.custom(summary_, 'NCD', options,
            type_='line', min_y_scale=None, max_y_scale=None, y_scale_margin=0.0001,figsize=(9,3), show=True)
summary['mean']
# utils.reload(plot)
options = {'x_labels': xlabels}
# custom(summary, 'NCD', options, relative=True, show=True)
plot.custom({'Mean':summary['mean']}, 'NCD', options, type_='bar', min_y_scale=0, max_y_scale=1, 
            y_scale_margin=0.01,figsize=(10,3), show=True)
plot.custom({'Mean':summary_min['mean']}, 'NCD min', options, type_='bar', min_y_scale=0, max_y_scale=1, 
            y_scale_margin=0.01, figsize=(10,3), show=True)
# utils.reload(plot)
options = {'x_labels': xlabels,}
# custom(summary, 'NCD', options, relative=True, show=True)
std = {'Mean': summary['std'], 'Median': summary['std']}
plot.custom({'Mean':summary['mean']}, 'NCD', options, type_='bar',std=std,figsize=(10,3), min_y_scale=None, max_y_scale=None, y_scale_margin=0.03, show=True)
std_min = {'Mean': summary_min['std'], 'Median': summary_min['std']}
plot.custom({'Mean':summary_min['mean']}, 'NCD min', options,dn='../', type_='bar',std=std_min,figsize=(10,3), min_y_scale=None, max_y_scale=None, y_scale_margin=0.03, show=True)
# utils.reload(plot)
plot.custom({'Median':summary['median']}, 'NCD median', options, type_='bar', figsize=(10,3),std=std, show=True)
plot.custom({'Median':summary_min['median']}, 'NCD-min median', options, type_='bar',figsize=(10,3), std=std_min, show=True)
result.keys()
import scipy.stats
# assume all distributions are related, do not shuffle
# i.e. param1[0] is related to param2[0]
def evaluate(data, base_k=0, f=scipy.stats.ttest_rel, p=0.05,win_c=lambda x,y: x>y):
    result = {}
    base_v = data[base_k]
    for k,v in data.items():
        if len(v) < 1:
            raise(ValueError('Warning: len(v) == %i for k: %s'%(len(v), str(k))))
        if not k == base_k:
            score = scipy.stats.ttest_rel(base_v, v)
            won = win_c(np.mean(base_v), np.mean(v))
            significant = score.pvalue < p
            result['%s/%s' % (base_k,k)] = (won, significant)
            print('%s vs %s: \t' % (base_k, k), won, significant, round(score.pvalue,4))
evaluate(result)
evaluate(result_min)
evaluate(result_min, base_k=0, f=scipy.stats.ttest_ind)<jupyter_output>0 vs 0.05: 	 False False 0.4874
0 vs 0.1: 	 False False 0.6919
0 vs 0.15000000000000002: 	 False False 0.0906
0 vs 0.2: 	 True False 0.6938
0 vs 0.25: 	 False False 0.1045
0 vs 0.30000000000000004: 	 False False 0.165
0 vs 0.35000000000000003: 	 False False 0.5271
0 vs 0.4: 	 False False 0.0838
0 vs 0.45: 	 True False 0.9846
0 vs 0.5: 	 False False 0.9181
0 vs 0.55: 	 True False 0.6666
0 vs 0.6000000000000001: 	 True False 0.2891
0 vs 0.65: 	 False False 0.7413
0 vs 0.7000000000000001: 	 False False 0.3501
0 vs 0.75: 	 False False 0.185
0 vs 0.8: 	 False False 0.7011
0 vs 0.8500000000000001: 	 False False 0.1896
0 vs 0.9: 	 False False 0.3068
0 vs 0.9500000000000001: 	 False False 0.1725
0 vs 1.0: 	 False False 0.0651
{'fraction_non_alphanumeric': 0.16344532364768452, 'fraction_numerical': 0.05046050071345181, 'mean_word_length': 4.69423929098966, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 7, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '94813', 'license': 'MIT', 'n_tokens_mistral': 3176, 'n_tokens_neox': 2894, 'n_words': 568, 'path': 'src/2d-analysis-evaluate-transformations-individual.ipynb', 'repo_name': 'voschezang/pattern-recognition', 'size': '7709'}