In [1]:
%run prelude.ipy
In [21]:
code = data.hansen_2012.program_code("overload", "plusmixed")
aois = aoi.code_to_aois(code, filename="test.py", offset=(10, 10))
aois.to_csv("/home/hansenm/Fall 2013/Research/eyecode/models/mr_bits/programs/overload_plusmixed.txt",
index=False, sep=' ')
aois
Out[21]:
In [20]:
display_html(aois.to_html())
Out[20]:
In []:
pandas.set_printoptions(precision=2)
df = programs.copy()
df.columns = ["Base", "Version", "Code Ch", "Code Ln", "CC", "HE", "HV", "Ouput Ch", "Output Ln"]
df
In []:
s = df.to_latex(index=False)
open("../../../Journal Article/programs_table.tex", "w").write(s)
In [2]:
raw_fixes = data.busjahn_2013.raw_fixations()
#screen = data.busjahn_2013.program_image("rectangle")
screen = data.busjahn_2013.program_image("basketball")
#offset = (-320, -88)
#raw_fixes.fix_x = raw_fixes.fix_x.apply(lambda x: x + offset[0])
#raw_fixes.fix_y = raw_fixes.fix_y.apply(lambda y: y + offset[1])
In []:
fixes = raw_fixes[raw_fixes.trial_id == 8]
plot.fixations.circles(fixes, screen, radius_min=5, radius_max=5)
In []:
from PIL import Image
font_path = "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf"
blank = Image.new("RGB", screen.size, color="#FFFFFF")
lines = open("../../eyecode/data/busjahn_2013/programs/rectangle.java", "r").readlines()
plot.misc.render_code(lines, blank, font_path, 22, 21, offset=(340, 93))
plot.fixations.circles(fixes, blank, radius_min=5, radius_max=5)
In [2]:
img = data.busjahn_2013.program_image("basketball")
aois = data.busjahn_2013.areas_of_interest()
bb_aois = aois[aois.program == "basketball"]
In [3]:
plot.aoi.draw_rectangles(bb_aois[bb_aois.kind == "sub-line"], img)
Out[3]:
In [63]:
df2 = rects.copy()
df2["kind"] = "line"
df2["name"] = "line "
df2.width += df2.x - 300 + 15
df2.x = 300
df2.y -= 8
df2.height += 16
df2.to_csv("../../eyecode/data/busjahn_2013/areas_of_interest.csv")
aoi.draw_rectangles(df2, img)
Out[63]:
In [2]:
raw_fixes = data.busjahn_2013.raw_fixations()
aois = data.busjahn_2013.areas_of_interest()
In [10]:
df = aoi.hit_test(raw_fixes, aois)
df[:25]
Out[10]:
In [9]:
line_fixes = data.busjahn_2013.line_fixations()
t_fixes = line_fixes[line_fixes.trial_id == 8]
fig = plot.fixations.line_timeline_single(t_fixes).figure
fig.tight_layout()
fig
Out[9]:
In [10]:
line_fixes = data.busjahn_2013.line_fixations()
t_fixes = line_fixes[line_fixes.trial_id == 9]
fig = plot.fixations.line_timeline_single(t_fixes).figure
fig.tight_layout()
fig
Out[10]:
In [4]:
aois = data.busjahn_2013.areas_of_interest()
screen = data.busjahn_2013.program_image("rectangle")
plot.aoi.draw_rectangles(aois[aois.kind == "signature"], screen)
Out[4]:
In [43]:
all_fixations = data.busjahn_2013.all_fixations()
all_fixations["aoi_method-call"].unique()
Out[43]:
In [20]:
all_fixations = data.busjahn_2013.all_fixations()
aois = data.busjahn_2013.areas_of_interest()
line_fixations = data.busjahn_2013.line_fixations()
In [3]:
t8_fixes = all_fixations[all_fixations.trial_id == 8]
t9_fixes = all_fixations[all_fixations.trial_id == 9]
In [5]:
aoi.scanpath_from_fixations(t8_fixes, aoi_names={"line":[]}).to_csv("../../../Gaze Workshop/subject1_lines.csv")
In [6]:
aoi.scanpath_from_fixations(t9_fixes, aoi_names={"line":[]}).to_csv("../../../Gaze Workshop/subject2_lines.csv")
In [26]:
plot.fixations.line_timeline(line_fixations, step_size=500).figure
Out[26]:
In [27]:
code_image = Image.open("../../eyecode/data/busjahn_2013/images/rectangle-pygment.png")
In [48]:
plot.fixations.line_code_image(line_fixations, code_image, image_padding=5, bar_height=0.75, bar_mult=1.015, num_lines=23)
Out[48]:
In [50]:
plot.fixations.line_code_image(line_fixations[line_fixations.trial_id == 9], code_image, image_padding=5, bar_height=0.75, bar_mult=1.015, num_lines=23)
Out[50]:
In [32]:
line_order = data.busjahn_2013.lines_execution_order()
In [6]:
all_fixations = data.busjahn_2013.all_fixations()
t8_fixes = all_fixations[all_fixations.trial_id == 8]
sp = aoi.scanpath_from_fixations(t8_fixes, aoi_names={"line":[]}, repeats=False)
In [24]:
from nltk.util import ngrams
from nltk import FreqDist
In [35]:
N = 4
line_grams = set(ngrams(line_order.name, N))
fd = { k: v for k, v in FreqDist(ngrams(sp.values, N)).iteritems() if k in line_grams }
fd
Out[35]:
In [3]:
all_fixations = data.busjahn_2013.all_fixations()
aois = data.busjahn_2013.areas_of_interest()
t9_fixes = all_fixations[all_fixations.trial_id == 9]
main_aoi = aois[(aois.kind == "block") & (aois.name == "main")].iloc[0]
main_bbox = main_aoi[["x", "y", "width", "height"]].values
In [79]:
main_fixes = t9_fixes.iloc[56:72]
density, counts = metrics.fixation_spatial_density(main_fixes, grid_bbox=main_bbox, num_rows=6, num_cols=8)
print density / len(main_fixes)
print counts.T
main_fixes.duration_ms.median()
Out[79]:
In [80]:
flat_counts = counts.flatten()
equal_counts = np.zeros(shape=(1, len(flat_counts))) + (flat_counts.sum() / len(flat_counts))
scipy.spatial.distance.cosine(flat_counts, equal_counts)
Out[80]:
In [36]:
con_aoi = aois[(aois.kind == "block") & (aois.name == "constructor")].iloc[0]
con_fixes = t9_fixes.iloc[73:89]
con_bbox = con_aoi[["x", "y", "width", "height"]].values
In [81]:
density, counts = metrics.fixation_spatial_density(con_fixes, grid_bbox=con_bbox, num_rows=6, num_cols=8)
print density / len(con_fixes)
print counts.T
con_fixes.duration_ms.median()
Out[81]:
In [82]:
flat_counts = counts.flatten()
equal_counts = np.zeros(shape=(1, len(flat_counts))) + (flat_counts.sum() / len(flat_counts))
scipy.spatial.distance.cosine(flat_counts, equal_counts)
Out[82]:
In [69]:
main_fixes = t9_fixes.iloc[90:117]
density, counts = metrics.fixation_spatial_density(main_fixes, grid_bbox=main_bbox, num_rows=6, num_cols=8)
print density / len(main_fixes)
print counts.T
pyplot.imshow(counts.T, interpolation="none").figure
Out[69]:
In [74]:
import scipy.spatial
In [75]:
flat_counts = counts.flatten()
equal_counts = np.zeros(shape=(1, len(flat_counts))) + (flat_counts.sum() / len(flat_counts))
scipy.spatial.distance.cosine(flat_counts, equal_counts)
Out[75]:
In [78]:
main_fixes = t9_fixes.iloc[128:]
density, counts = metrics.fixation_spatial_density(main_fixes, grid_bbox=main_bbox, num_rows=6, num_cols=8)
print density / len(main_fixes)
print counts.T
main_fixes.duration_ms.median()
pyplot.imshow(counts.T, interpolation="none").figure
Out[78]:
In [77]:
flat_counts = counts.flatten()
equal_counts = np.zeros(shape=(1, len(flat_counts))) + (flat_counts.sum() / len(flat_counts))
scipy.spatial.distance.cosine(flat_counts, equal_counts)
Out[77]:
In [2]:
all_fixations = data.busjahn_2013.all_fixations()
aois = data.busjahn_2013.areas_of_interest()
t8_fixes = all_fixations[all_fixations.trial_id == 8]
t9_fixes = all_fixations[all_fixations.trial_id == 9]
code_aoi = aois[aois.name == "code box"].iloc[0]
code_bbox = code_aoi[["x", "y", "width", "height"]].values
In [8]:
fun = lambda fixes: metrics.fixation_spatial_density(fixes, grid_bbox=code_bbox, num_rows=rows, num_cols=cols)[0]
s = util.rolling_func(t8_fixes, fun, 3000)
s.plot(figsize=(20, 8)).figure
In [118]:
s = util.rolling_func(t9_fixes, fun, 3000)
s.plot(figsize=(20, 8)).figure
Out[118]:
In [3]:
funs = {
"density" : lambda fixes: metrics.fixation_spatial_density(fixes, grid_bbox=code_bbox, num_rows=10, num_cols=1)[0],
"fcount" : lambda fixes: len(fixes),
"duration" : lambda fixes: fixes["duration_ms"].mean()
}
In [4]:
def rolling_plot(df):
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, FuncFormatter
fig = pyplot.figure(figsize=(25, 10))
ax = pyplot.axes()
axes = [ax, ax.twinx(), ax.twinx()]
plots = [None] * len(axes)
fig.subplots_adjust(left=0, right=0.75)
axes[2].spines['right'].set_position(('axes', 1.1))
axes[2].set_frame_on(True)
axes[2].patch.set_visible(False)
plots[0] = axes[0].plot(df.index, df.density, color="b", marker="o", markersize=10, label="Density")
axes[0].set_ylabel("Spatial Density", color="b")
axes[0].tick_params(axis="y", colors="b")
axes[1].plot(df.index, df.duration, color="r", marker="*", markersize=10, label="Duration")
axes[1].set_ylabel("Mean Fixation Duration (ms)", color="r")
axes[1].tick_params(axis="y", colors="r")
axes[2].plot(df.index, df.fcount, color="g", marker="^", markersize=10, label="Fix Count")
axes[2].set_ylabel("Fixation Count", color="g")
axes[2].tick_params(axis="y", colors="g")
#ax.grid()
ax.set_xlabel("Time (sec)")
ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: str(int(x) / 1000)))
ax.xaxis.set_major_locator(MultipleLocator(2000))
for label in ax.get_xticklabels():
label.set_rotation(90)
return fig
In [12]:
def rolling_plot(df):
from matplotlib.ticker import MultipleLocator, FormatStrFormatter, FuncFormatter
fig = pyplot.figure(figsize=(15, 8))
ax = pyplot.axes()
axes = [ax, ax.twinx(), ax.twinx()]
plots = [None] * len(axes)
plots[0] = axes[0].plot(df.index, df.density, color="blue", linewidth=2, label="Density")
plots[1] = axes[1].plot(df.index, df.duration, color="red", linestyle="-.", linewidth=3, label="Duration")
plots[2] = axes[2].plot(df.index, df.fcount, color="green", linestyle="--", linewidth=2, label="Count")
for a in axes:
a.yaxis.set_visible(False)
a.set_frame_on(False)
a.xaxis.tick_bottom()
a.xaxis.set_major_locator(pyplot.NullLocator())
a.set_xlabel(r"Time $\rightarrow$", fontsize=25)
fig.tight_layout()
fig.subplots_adjust(top=0.9)
axes[0].legend(bbox_to_anchor=(0.4, 1.1), frameon=False)
axes[1].legend(bbox_to_anchor=(0.8, 1.1), frameon=False)
axes[2].legend(bbox_to_anchor=(0.6, 1.1), frameon=False)
ax.set_ylim(df.density.min() - 0.01, df.density.max() + 0.01)
for a in axes:
pyplot.setp(a.get_legend().get_texts(), fontsize=25)
return fig
In [13]:
df = util.rolling_func(t8_fixes, funs, 4000, 1000).dropna()
fig = rolling_plot(df)
#fig.subplots_adjust(top=0.9)
#fig.axes[0].set_title("Rolling Fixation Metrics (Trial 8, W=4s, S=1s)", fontsize=20)
fig.savefig("../../../Gaze Workshop/paper/figures/rolling_8.pdf")
fig
Out[13]:
In [10]:
df = util.rolling_func(t9_fixes, funs, 4000, 1000).dropna()
fig = rolling_plot(df)
#fig.subplots_adjust(top=0.9)
#fig.axes[0].set_title("Rolling Fixation Metrics (Trial 9, W=4s, S=1s)", fontsize=20)
fig.savefig("../../../Gaze Workshop/paper/figures/rolling_9.pdf")
fig
Out[10]:
In [26]:
lf_all = data.busjahn_2013.line_fixations()
line_fixations = lf_all[lf_all.trial_id == 8]
num_lines = 23 #line_fixations.line.max()
# Gather list of times and line numbers (output box is line 0)
times_lines = dict(list(line_fixations[["start_ms", "line"]].values) +
list(line_fixations[["end_ms", "line"]].values))
sorted_times = sorted(times_lines.keys())
sorted_lines = [times_lines[k] for k in sorted_times]
max_time = max(sorted_times)
# Plot fixation lines and points
fig_width = int(np.ceil(max_time / 1000.0 / 8.0))
fig_height = int(np.ceil((num_lines + 1) / 4.0))
figsize = (fig_width, fig_height)
pyplot.figure(figsize=figsize)
ax = pyplot.axes()
ax.set_frame_on(False)
ax.yaxis.set_visible(False)
ax.xaxis.tick_bottom()
ax.xaxis.set_major_locator(pyplot.NullLocator())
ax.plot(sorted_times, sorted_lines, linewidth=1, color="black", label="Line")
#ax.grid()
#ax.set_title("Fixations By Line")
lines = np.arange(1, num_lines + 1)
ax.set_ylim(0.5, num_lines + 0.5)
#ax.set_yticks(lines)
#ax.set_yticklabels(lines)
#ax.set_ylabel("Line")
ax.invert_yaxis()
# Show time in seconds instead of millis
#ax.xaxis.set_major_formatter(FuncFormatter(lambda x, pos: int(x / 1000)))
#ax.xaxis.set_major_locator(MultipleLocator(1000))
ax.set_xlim(-500, max_time + 1000)
ax.set_xlabel(r"Time $\rightarrow$", fontsize=20)
ax.set_xticklabels([])
fig = ax.figure
fig.tight_layout()
fig.subplots_adjust(top=0.9)
ax.legend(bbox_to_anchor=(0.55, 1.1), frameon=False, fontsize=20)
scan_coords = (2034, 18642)
min_y, max_y = ax.yaxis.get_data_interval()
scan_rect = pyplot.Rectangle((scan_coords[0], min_y), scan_coords[1] - scan_coords[0], max_y - min_y,
color="yellow", alpha=0.5)
ax.add_patch(scan_rect)
fig.savefig("../../../Gaze Workshop/paper/figures/timeline_8.pdf")
fig
Out[26]:
In [15]:
img = data.busjahn_2013.program_image("basketball")
code = data.busjahn_2013.program_code("basketball")
lines = [i + 1 if len(l.strip()) > 0 else "" for i, l in enumerate(code)]
plot.misc.add_line_numbers(img, "/usr/share/fonts/truetype/msttcorefonts/Arial.ttf", 18, lines,
line_height=1.905, offset=(10, 28))
Out[15]: