import numpy as np
import eyekit
import algorithms
import core

data = eyekit.io.load(core.FIXATIONS / 'sample.json')
passages = eyekit.io.load(core.DATA / 'passages.json')

original_sequence = data['trial_5']['fixations']

fixation_XY = np.array([fixation.xy for fixation in original_sequence], dtype=int)
word_XY = np.array([word.center for word in passages['1B'].words(alphabetical_only=False)], dtype=int)

start_times = np.array([i*100 for i in range(len(word_XY))], dtype=int)
expected_sequence = eyekit.FixationSequence(np.column_stack([word_XY, start_times, start_times+100]))

diagram = eyekit.vis.Image(1920, 1080)
diagram.draw_text_block(passages['1B'], mask_text=True)
diagram.draw_fixation_sequence(expected_sequence, color='#E32823', fixation_radius=6)
diagram.draw_fixation_sequence(original_sequence, color='#205E84', fixation_radius=6)

_, warping_path = algorithms.dynamic_time_warping(fixation_XY, word_XY)

for fixation, mapped_words in zip(original_sequence, warping_path):
	for word_i in mapped_words:
		word_x, word_y = word_XY[word_i]
		diagram.draw_line(fixation.xy, (word_x, word_y), color='black', stroke_width=0.5, dashed=True)

fig = eyekit.vis.Figure()
fig.add_image(diagram)
fig.set_crop_margin(2)
fig.set_padding(vertical=2, horizontal=3, edge=1)
fig.set_enumeration(False)
fig.save(core.VISUALS / 'illustration_warp.pdf', width=83)
# fig.save(core.FIGS / 'fig02_single_column.eps', width=83)

[(6, 'eyekit.io.load', 'eyekit.io.load', False, 'import eyekit\n'), (7, 'eyekit.io.load', 'eyekit.io.load', False, 'import eyekit\n'), (11, 'numpy.array', 'np.array', True, 'import numpy as np\n'), (17, 'eyekit.vis.Image', 'eyekit.vis.Image', False, 'import eyekit\n'), (22, 'algorithms.dynamic_time_warping', 'algorithms.dynamic_time_warping', False, 'import algorithms\n'), (29, 'eyekit.vis.Figure', 'eyekit.vis.Figure', False, 'import eyekit\n'), (15, 'numpy.column_stack', 'np.column_stack', True, 'import numpy as np\n')]