03-generate_example_localiser_img.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from PIL import ImageEnhance
  2. import pandas as pd
  3. import os.path as op
  4. import matplotlib.pyplot as plt
  5. import matplotlib as mpl
  6. from random import choices, seed, sample
  7. # functions taken from https://github.com/jackedtaylor/randphase
  8. from randphase import draw
  9. from randphase import phase
  10. seed_nr = 999
  11. seed(seed_nr)
  12. experiment_font = 'cour.ttf'
  13. bacs_font = 'BACS2serif.otf'
  14. strings = pd.read_csv('localiser_stim.csv')
  15. font_size = 36
  16. n_examples = 10
  17. min_char = min([len(x) for x in strings.string])
  18. max_char = max([len(x) for x in strings.string])
  19. if (max_char-min_char+1 < n_examples):
  20. print('Too mamy examples for limits of word length?')
  21. example_strings = []
  22. for len_i in range(min_char, min_char+n_examples):
  23. rel_strings = strings[strings.Length == len_i]
  24. string_i = sample(rel_strings.string.tolist(), 1)[0]
  25. example_strings.append(string_i)
  26. fig_height = round((n_examples*0.7 + 1)/2.5)
  27. fig, axs = plt.subplots(n_examples, 3, figsize=(6.5, fig_height))
  28. largest_img_width = 0
  29. for item_nr_idx in range(n_examples):
  30. word_str = str(example_strings[item_nr_idx])
  31. word_img = draw.text(word_str, font=experiment_font, size=font_size, crop_x='font', crop_y='font')
  32. bacs_img = draw.text(word_str, font=bacs_font, size=font_size, crop_x='font', crop_y='font')
  33. noise_img = phase.randomise(word_img, noise='permute', contrast_adj=0.5)
  34. axs[item_nr_idx, 0].imshow(word_img)
  35. axs[item_nr_idx, 1].imshow(bacs_img)
  36. axs[item_nr_idx, 2].imshow(noise_img)
  37. if word_img.size[0] > largest_img_width:
  38. largest_img_width = word_img.size[0]
  39. for item_nr in range(n_examples):
  40. for col_nr in range(3):
  41. axs[item_nr, col_nr].set_axis_off()
  42. # centre-align with consistent sizing
  43. xl = axs[item_nr, col_nr].get_xlim()
  44. xl_adj = (largest_img_width - xl[1])/2
  45. axs[item_nr, col_nr].set(xlim=[-xl_adj, xl[1]+xl_adj])
  46. title_font_dict = {'fontsize': 12}
  47. title_padding = 7.5
  48. axs[0,0].set_title('Words', fontdict=title_font_dict, pad=title_padding)
  49. axs[0,1].set_title('False-Font\nStrings', fontdict=title_font_dict, pad=title_padding)
  50. axs[0,2].set_title('Phase-Shuffled\nWords', fontdict=title_font_dict, pad=title_padding)
  51. fig.subplots_adjust(wspace=0.1, hspace=0.1)
  52. mpl.rcParams['savefig.pad_inches'] = 0
  53. fig.savefig(fname=op.join('fig', 'example_localiser_images.png'), dpi=600, bbox_inches='tight')