12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- from PIL import ImageEnhance
- import pandas as pd
- import os.path as op
- import matplotlib.pyplot as plt
- import matplotlib as mpl
- from random import choices, seed, sample
- # functions taken from https://github.com/jackedtaylor/randphase
- from randphase import draw
- from randphase import phase
- seed_nr = 999
- seed(seed_nr)
- experiment_font = 'cour.ttf'
- bacs_font = 'BACS2serif.otf'
- strings = pd.read_csv('localiser_stim.csv')
- font_size = 36
- n_examples = 10
- min_char = min([len(x) for x in strings.string])
- max_char = max([len(x) for x in strings.string])
- if (max_char-min_char+1 < n_examples):
- print('Too mamy examples for limits of word length?')
- example_strings = []
- for len_i in range(min_char, min_char+n_examples):
- rel_strings = strings[strings.Length == len_i]
- string_i = sample(rel_strings.string.tolist(), 1)[0]
- example_strings.append(string_i)
- fig_height = round((n_examples*0.7 + 1)/2.5)
- fig, axs = plt.subplots(n_examples, 3, figsize=(6.5, fig_height))
- largest_img_width = 0
- for item_nr_idx in range(n_examples):
-
- word_str = str(example_strings[item_nr_idx])
- word_img = draw.text(word_str, font=experiment_font, size=font_size, crop_x='font', crop_y='font')
-
- bacs_img = draw.text(word_str, font=bacs_font, size=font_size, crop_x='font', crop_y='font')
-
- noise_img = phase.randomise(word_img, noise='permute', contrast_adj=0.5)
-
- axs[item_nr_idx, 0].imshow(word_img)
- axs[item_nr_idx, 1].imshow(bacs_img)
- axs[item_nr_idx, 2].imshow(noise_img)
-
- if word_img.size[0] > largest_img_width:
- largest_img_width = word_img.size[0]
- for item_nr in range(n_examples):
- for col_nr in range(3):
- axs[item_nr, col_nr].set_axis_off()
- # centre-align with consistent sizing
- xl = axs[item_nr, col_nr].get_xlim()
- xl_adj = (largest_img_width - xl[1])/2
- axs[item_nr, col_nr].set(xlim=[-xl_adj, xl[1]+xl_adj])
- title_font_dict = {'fontsize': 12}
- title_padding = 7.5
- axs[0,0].set_title('Words', fontdict=title_font_dict, pad=title_padding)
- axs[0,1].set_title('False-Font\nStrings', fontdict=title_font_dict, pad=title_padding)
- axs[0,2].set_title('Phase-Shuffled\nWords', fontdict=title_font_dict, pad=title_padding)
- fig.subplots_adjust(wspace=0.1, hspace=0.1)
- mpl.rcParams['savefig.pad_inches'] = 0
- fig.savefig(fname=op.join('fig', 'example_localiser_images.png'), dpi=600, bbox_inches='tight')
|