Created
June 28, 2015 21:06
-
-
Save nhfruchter/fd5bbe0915bf8fe8fa34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Body | Screen | Face | ||
---|---|---|---|---|
0 | 867.0 | 867.0 | 867.0 | |
1 | 267.0 | 100.0 | 167.0 | |
2 | 167.0 | 67.0 | 900.0 | |
3 | 900.0 | 566.0 | 433.0 | |
4 | 433.0 | 1000.0 | 633.0 | |
5 | 633.0 | 566.0 | 600.0 | |
6 | 600.0 | 367.0 | 534.0 | |
7 | 534.0 | 700.0 | 567.0 | |
8 | 567.0 | 400.0 | 933.0 | |
9 | 933.0 | 300.0 | 967.0 | |
10 | 967.0 | 700.0 | 733.0 | |
11 | 733.0 | 267.0 | 67.0 | |
12 | 67.0 | 167.0 | 133.0 | |
13 | 133.0 | 900.0 | 1067.0 | |
14 | 1067.0 | 433.0 | 333.0 | |
15 | 333.0 | 633.0 | 233.0 | |
16 | 233.0 | 600.0 | 533.0 | |
17 | 533.0 | 534.0 | 500.0 | |
18 | 500.0 | 567.0 | 1083.0 | |
19 | 1083.0 | 933.0 | 66.0 | |
20 | 66.0 | 967.0 | 66.0 | |
21 | 66.0 | 733.0 | 233.0 | |
22 | 233.0 | 67.0 | 1867.0 | |
23 | 1867.0 | 133.0 | 266.0 | |
24 | 266.0 | 1067.0 | 1067.0 | |
25 | 1067.0 | 333.0 | 1333.0 | |
26 | 1333.0 | 233.0 | 900.0 | |
27 | 900.0 | 533.0 | 133.0 | |
28 | 133.0 | 500.0 | 233.0 | |
29 | 100.0 | 1083.0 | 1667.0 | |
30 | 67.0 | 66.0 | 1166.0 | |
31 | 233.0 | 66.0 | 567.0 | |
32 | 1667.0 | 734.0 | 566.0 | |
33 | 1166.0 | 200.0 | 500.0 | |
34 | 233.0 | 233.0 | 500.0 | |
35 | 567.0 | 1867.0 | 200.0 | |
36 | 366.0 | 266.0 | 467.0 | |
37 | 566.0 | 1067.0 | 367.0 | |
38 | 500.0 | 1333.0 | 300.0 | |
39 | 500.0 | 900.0 | 733.0 | |
40 | 200.0 | 133.0 | 1700.0 | |
41 | 467.0 | 100.0 | 167.0 | |
42 | 367.0 | 67.0 | 1000.0 | |
43 | 300.0 | 233.0 | 600.0 | |
44 | 733.0 | 1667.0 | 133.0 | |
45 | 1700.0 | 1166.0 | 666.0 | |
46 | 167.0 | 233.0 | 733.0 | |
47 | 1100.0 | 267.0 | 534.0 | |
48 | 1000.0 | 433.0 | 400.0 | |
49 | 600.0 | 300.0 | ||
50 | 133.0 | 333.0 | ||
51 | 666.0 | 67.0 | ||
52 | 733.0 | 567.0 | ||
53 | 67.0 | 366.0 | ||
54 | 534.0 | 566.0 | ||
55 | 400.0 | 500.0 | ||
56 | 133.0 | 500.0 | ||
57 | 200.0 | |||
58 | 233.0 | |||
59 | 467.0 | |||
60 | 367.0 | |||
61 | 300.0 | |||
62 | 733.0 | |||
63 | 133.0 | |||
64 | 1700.0 | |||
65 | 166.0 | |||
66 | 167.0 | |||
67 | 66.0 | |||
68 | 233.0 | |||
69 | 1100.0 | |||
70 | 1000.0 | |||
71 | 600.0 | |||
72 | 100.0 | |||
73 | 133.0 | |||
74 | 134.0 | |||
75 | 666.0 | |||
76 | 733.0 | |||
77 | 67.0 | |||
78 | 534.0 | |||
79 | 400.0 | |||
80 | 133.0 | |||
81 | 200.0 | |||
82 | 833.0 | |||
83 | 500.0 | |||
84 | 117.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import numpy as np | |
from itertools import groupby | |
########## Part 1: Import data | |
# Filename | |
fn = "DigitalScreenTestDataFiltered.tsv" | |
# Read file in with tab (\t) delimiter | |
df = pd.read_csv(fn, sep="\t") | |
# All AOI columns start with this phrase | |
AOI_prefix = 'AOI hit' | |
# Extract the AOI name from a column | |
def AOI_from_column_name(col): | |
# 1. Starts with `AOI hit [abcdefg.png - AOI Name]` | |
# 2. Splits on the ` - `, turns into `AOI hit [abcdefg.png` and `AOI name]` | |
snapshot_file, name = col.split(" - ") | |
# 3. Remove the trailing bracket | |
return name.replace("]", "") | |
# Create a mapping of column name to AOI name | |
AOIs = {col:AOI_from_column_name(col) for col in df.columns if col.startswith(AOI_prefix)} | |
######### Part 2: Aggregate and summarize data | |
# Create time delta column | |
# df['timestamp_delta'] = pd.to_timedelta(df['Recording timestamp'], unit='ms') | |
# Now, extract the differences in milliseconds. | |
# df['timestamp_delta_ms'] = df.apply(lambda row: row['timestamp_delta'].milliseconds, axis=1) | |
df['timestamp_delta_ms'] = ( df['Recording timestamp'] - df['Recording timestamp'].shift()).fillna(0) | |
# Calculate cumulative fixation time for each group of fixations. | |
for AOI in AOIs: | |
accumulator = [] | |
last = 0 | |
for row in df.iterrows(): | |
current_delta = row[1]['timestamp_delta_ms'] + last | |
is_fixation = bool(row[1][AOI]) | |
# Reset counter if this is not a fixation | |
if not is_fixation: | |
last = 0 | |
accumulator.append(np.nan) | |
else: | |
accumulator.append(current_delta) | |
last = current_delta | |
df['AOI_Cumulative_%s' % AOIs[AOI]] = accumulator | |
######### Part 3: Calculate statistics | |
data = df.filter(regex="AOI_Cumulative").to_dict() | |
fixations = {key:[] for key in AOIs.values()} | |
for AOI in AOIs.values(): | |
key = 'AOI_Cumulative_%s' % AOI | |
grouped = groupby(data[key].values(), lambda item: (not np.isnan(item))) | |
for is_fixation, values in grouped: | |
if is_fixation: | |
fixations[AOI].append(max(values)) | |
# Export to file | |
stats = pd.DataFrame.from_dict(fixations, orient='index').transpose() | |
stats.to_csv('Stats_For_%s.csv' % fn.split(".")[0] ) | |
print stats.describe() | |
print "%s AOIs, total recording time %s ms" % (len(AOIs), df['Recording timestamp'].max()) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment