12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- # %% import modules ======================
- import os
- n_threads = str(1)
- os.environ["OMP_NUM_THREADS"] = n_threads
- os.environ["OPENBLAS_NUM_THREADS"] = n_threads
- os.environ["MKL_NUM_THREADS"] = n_threads
- os.environ["VECLIB_MAXIMUM_THREADS"] = n_threads
- os.environ["NUMEXPR_NUM_THREADS"] = n_threads
- import sys
- import os.path as op
- sys.path.append(op.join(op.dirname(__file__), '..'))
- import scold
- from scold import draw, text_arr_sim, arr_sim, text_arr_sim_wasserstein
- import numpy as np
- from tqdm import tqdm
- import itertools
- from joblib import Parallel, delayed
- n_jobs = -1
- # %% setup
- font = 'Arial-Lgt.ttf'
- font_size = 75
- round_method = None
- char1 = 'a'
- char2 = 'c'
- char2_arr = draw.text_array(char2, font=font, size=font_size, method=round_method)
- n_scale = 250
- n_rotation = 250
- logscale = np.linspace(np.log(0.5), np.log(2), num=n_scale, endpoint=True)
- rotation = np.linspace(-180, 180, num=n_rotation, endpoint=True)
- # %% the Jaccard distances
- space_j_list = Parallel(n_jobs=n_jobs)(
- delayed(text_arr_sim.text_arr_sim)(
- a=char1, b_arr=char2_arr,
- font_a=font,
- size=font_size, method=round_method,
- measure='jaccard',
- translate=True,
- scale_val=np.exp(ls), fliplr=False, flipud=False, rotate_val=r)
- for ls, r in tqdm(
- itertools.product(logscale, rotation),
- desc='Mapping Jaccard space', total=n_scale*n_rotation))
- space_j = np.array([1 - x['jaccard'] for x in space_j_list]).reshape((n_scale, n_rotation))
- np.save(op.join('fig_code', '99_estimate_spaces_jaccard'), space_j)
- # %% the wasserstein distances
- space_w_list = Parallel(n_jobs=n_jobs)(
- delayed(text_arr_sim.text_arr_sim)(
- a=char1, b_arr=char2_arr,
- font_a=font,
- size=font_size, method=round_method,
- measure='partial_wasserstein',
- translate=True,
- scale_val=np.exp(ls), fliplr=False, flipud=False, rotate_val=r,
- partial_wasserstein_kwargs={'scale_mass':True, 'scale_mass_method':'proportion', 'mass_normalise':False, 'distance_normalise':False, 'ins_weight':0.0, 'del_weight':0.0, 'translation': 'opt'})
- for ls, r in tqdm(
- itertools.product(logscale, rotation),
- desc='Mapping Wasserstein space', total=n_scale*n_rotation))
- space_w = np.array([x['partial_wasserstein'] for x in space_w_list]).reshape((n_scale, n_rotation))
- np.save(op.join('fig_code', '99_estimate_spaces_wasserstein'), space_w)
|