99_estimate_spaces.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. # %% import modules ======================
  2. import os
  3. n_threads = str(1)
  4. os.environ["OMP_NUM_THREADS"] = n_threads
  5. os.environ["OPENBLAS_NUM_THREADS"] = n_threads
  6. os.environ["MKL_NUM_THREADS"] = n_threads
  7. os.environ["VECLIB_MAXIMUM_THREADS"] = n_threads
  8. os.environ["NUMEXPR_NUM_THREADS"] = n_threads
  9. import sys
  10. import os.path as op
  11. sys.path.append(op.join(op.dirname(__file__), '..'))
  12. import scold
  13. from scold import draw, text_arr_sim, arr_sim, text_arr_sim_wasserstein
  14. import numpy as np
  15. from tqdm import tqdm
  16. import itertools
  17. from joblib import Parallel, delayed
  18. n_jobs = -1
  19. # %% setup
  20. font = 'Arial-Lgt.ttf'
  21. font_size = 75
  22. round_method = None
  23. char1 = 'a'
  24. char2 = 'c'
  25. char2_arr = draw.text_array(char2, font=font, size=font_size, method=round_method)
  26. n_scale = 250
  27. n_rotation = 250
  28. logscale = np.linspace(np.log(0.5), np.log(2), num=n_scale, endpoint=True)
  29. rotation = np.linspace(-180, 180, num=n_rotation, endpoint=True)
  30. # %% the Jaccard distances
  31. space_j_list = Parallel(n_jobs=n_jobs)(
  32. delayed(text_arr_sim.text_arr_sim)(
  33. a=char1, b_arr=char2_arr,
  34. font_a=font,
  35. size=font_size, method=round_method,
  36. measure='jaccard',
  37. translate=True,
  38. scale_val=np.exp(ls), fliplr=False, flipud=False, rotate_val=r)
  39. for ls, r in tqdm(
  40. itertools.product(logscale, rotation),
  41. desc='Mapping Jaccard space', total=n_scale*n_rotation))
  42. space_j = np.array([1 - x['jaccard'] for x in space_j_list]).reshape((n_scale, n_rotation))
  43. np.save(op.join('fig_code', '99_estimate_spaces_jaccard'), space_j)
  44. # %% the wasserstein distances
  45. space_w_list = Parallel(n_jobs=n_jobs)(
  46. delayed(text_arr_sim.text_arr_sim)(
  47. a=char1, b_arr=char2_arr,
  48. font_a=font,
  49. size=font_size, method=round_method,
  50. measure='partial_wasserstein',
  51. translate=True,
  52. scale_val=np.exp(ls), fliplr=False, flipud=False, rotate_val=r,
  53. 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'})
  54. for ls, r in tqdm(
  55. itertools.product(logscale, rotation),
  56. desc='Mapping Wasserstein space', total=n_scale*n_rotation))
  57. space_w = np.array([x['partial_wasserstein'] for x in space_w_list]).reshape((n_scale, n_rotation))
  58. np.save(op.join('fig_code', '99_estimate_spaces_wasserstein'), space_w)