Commits

Alexander Konovalov committed d9760e9

Added several new baseline models.

  • Participants
  • Parent commits 93f0c18

Comments (0)

Files changed (1)

             rmse_y += sum((real_gaze[:, 1] - predicted_gaze[:, 1])**2)
             rmse_t += sum((real_gaze - predicted_gaze)**2)
 
-        print sqrt(rmse_x / count), sqrt(rmse_y / count), sqrt(rmse_t / count)
+        #print sqrt(rmse_x / count), sqrt(rmse_y / count), sqrt(rmse_t / count)
         cv_rmse_x.append(sqrt(rmse_x / count))
         cv_rmse_y.append(sqrt(rmse_y / count))
         cv_rmse_t.append(sqrt(rmse_t / count))
         
     return (mean(cv_rmse_x), mean(cv_rmse_y), mean(cv_rmse_t))
 
-class BaselineModel:
+class Baseline1Model:
     def learn(self, learn_set):
         pass
     def predict(self, time, mouse, click):
         return mouse
 
+class Baseline2Model:
+    def learn(self, learn_set):
+        import numpy as np
+
+        learn_set = list(learn_set)
+
+        lengths = [len(entry.time) for entry in learn_set]
+        start = np.cumsum([0] + lengths)[:-1]
+        end = np.cumsum(lengths)
+
+        size_x = 1
+        size_y = end[-1]
+
+        ax = np.zeros((size_y, size_x))
+        bx = np.zeros(size_y)
+
+        ay = np.zeros((size_y, size_x))
+        by = np.zeros(size_y)
+
+        for i in xrange(len(learn_set)):
+            entry = learn_set[i]
+            time, gaze, mouse, click = entry.time, entry.gaze, entry.mouse, entry.click
+
+            gx, gy = gaze[:,0], gaze[:,1]
+            cx, cy = mouse[:,0], mouse[:,1]
+
+            ax[start[i]:end[i]] = np.ones_like(ax[start[i]:end[i]])
+            bx[start[i]:end[i]] = gx
+
+            ay[start[i]:end[i]] = np.ones_like(ay[start[i]:end[i]])
+            by[start[i]:end[i]] = gy
+
+
+        from numpy.linalg import lstsq
+
+        x, _, _, _ = lstsq(ax, bx)
+        self.sx = x
+
+        x, _, _, _ = lstsq(ay, by)
+        self.sy = x
+
+
+    def predict(self, time, mouse, click):
+        import numpy as np
+
+        cx, cy = mouse[:,0], mouse[:,1]
+        gx = np.ones_like(cx) * self.sx
+        gy = np.ones_like(cy) * self.sy
+
+        return np.array([gx, gy]).T
+
+class Baseline3Model:
+    def learn(self, learn_set):
+        import numpy as np
+
+        learn_set = list(learn_set)
+
+        lengths = [len(entry.time) for entry in learn_set]
+        start = np.cumsum([0] + lengths)[:-1]
+        end = np.cumsum(lengths)
+
+        size_x = 2
+        size_y = end[-1]
+
+        ax = np.zeros((size_y, size_x))
+        bx = np.zeros(size_y)
+
+        ay = np.zeros((size_y, size_x))
+        by = np.zeros(size_y)
+
+        for i in xrange(len(learn_set)):
+            entry = learn_set[i]
+            time, gaze, mouse, click = entry.time, entry.gaze, entry.mouse, entry.click
+
+            gx, gy = gaze[:,0], gaze[:,1]
+            cx, cy = mouse[:,0], mouse[:,1]
+            t = (time - time[0]) / 1000.0
+
+            ax[start[i]:end[i], :] = np.vstack((cx, np.ones_like(cx))).T
+            bx[start[i]:end[i]] = gx
+
+            ay[start[i]:end[i], :] = np.vstack((cy, np.ones_like(cx))).T
+            by[start[i]:end[i]] = gy
+
+
+        from numpy.linalg import lstsq
+
+        x, _, _, _ = lstsq(ax, bx)
+        self.sx = x
+
+        x, _, _, _ = lstsq(ay, by)
+        self.sy = x
+
+
+    def predict(self, time, mouse, click):
+        import numpy as np
+
+        t = (time - time[0]) / 1000.0
+        cx, cy = mouse[:,0], mouse[:,1]
+        ax = np.vstack((cx, np.ones_like(cx))).T
+        ay = np.vstack((cy, np.ones_like(cx))).T
+        gx = ax.dot(self.sx)
+        gy = ay.dot(self.sy)
+
+        return np.array([gx, gy]).T
+
+class Baseline4Model:
+    def learn(self, learn_set):
+        import numpy as np
+
+        learn_set = list(learn_set)
+
+        lengths = [len(entry.time) for entry in learn_set]
+        start = np.cumsum([0] + lengths)[:-1]
+        end = np.cumsum(lengths)
+
+        size_y = end[-1]
+
+        ax = np.zeros((size_y, 1))
+        bx = np.zeros(size_y)
+
+        ay = np.zeros((size_y, 2))
+        by = np.zeros(size_y)
+
+        for i in xrange(len(learn_set)):
+            entry = learn_set[i]
+            time, gaze, mouse, click = entry.time, entry.gaze, entry.mouse, entry.click
+
+            gx, gy = gaze[:,0], gaze[:,1]
+            cx, cy = mouse[:,0], mouse[:,1]
+
+            ax[start[i]:end[i]] = np.ones_like(ax[start[i]:end[i]])
+            bx[start[i]:end[i]] = gx
+
+            ay[start[i]:end[i]] = np.vstack((cy, np.ones_like(cx))).T
+            by[start[i]:end[i]] = gy
+
+
+        from numpy.linalg import lstsq
+
+        x, _, _, _ = lstsq(ax, bx)
+        self.sx = x
+
+        x, _, _, _ = lstsq(ay, by)
+        self.sy = x
+
+
+    def predict(self, time, mouse, click):
+        import numpy as np
+
+        cx, cy = mouse[:,0], mouse[:,1]
+        gx = np.ones_like(cx) * self.sx
+        ay = np.vstack((cy, np.ones_like(cx))).T
+        gy = ay.dot(self.sy)
+
+        return np.array([gx, gy]).T
+
 class HuangModel:
     def features(self, time, mouse, click):
         t = (time - time[0]) / 1000.0
         for i in xrange(len(learn_set)):
             entry = learn_set[i]
             time, gaze, mouse, click = entry.time, entry.gaze, entry.mouse, entry.click
-            
+
             gx, gy = gaze[:,0], gaze[:,1]
             cx, cy = mouse[:,0], mouse[:,1]
             td, mf, tm, fv = self.features(time, mouse, click)
             fx, fy = fv[:,0], fv[:,1]
-            
+
             ax[start[i]:end[i], :] = np.vstack((cx, td, tm, cx * td, cx * tm, fx)).T
             bx[start[i]:end[i]] = gx
 
             ay[start[i]:end[i], :] = np.vstack((cy, td, tm, cy * td, cy * tm, fy)).T
             by[start[i]:end[i]] = gy
-            
+
 
         from numpy.linalg import lstsq
 
 
         return np.array([gx, gy]).T
 
+class TestModel:
+    def features(self, time, mouse, click):
+        t = (time - time[0]) / 1000.0
+
+        # Logarithm of dwell time
+        dwell_time_logarithm = numpy.log(1 + t)
+
+        # Movement flag (speed > 0)
+        vx = (mouse[1:, 0] - mouse[:-1, 0]) / (t[1:] - t[:-1] + 0.0001)
+        vy = (mouse[1:, 0] - mouse[:-1, 0]) / (t[1:] - t[:-1] + 0.0001)
+        movement_flag = numpy.append(
+            numpy.array([0]),
+            numpy.sqrt(vx**2 + vy**2) > 0, axis=1)
+
+        # Time since last movement
+        time_since_last_movement = numpy.zeros_like(t)
+        last_movement_time = 0
+        for i in xrange(len(time)):
+            if movement_flag[i] != 0:
+                last_movement_time = t[i]
+            time_since_last_movement[i] = t[i] - last_movement_time
+        log_time_since_last_movement = log(time_since_last_movement + 1)
+
+        # Future values
+        future_value = future_average(t, mouse, 1.0, 0.0)
+
+        return (
+            dwell_time_logarithm,
+            movement_flag,
+            log_time_since_last_movement,
+            future_value)
+
+    def learn(self, learn_set):
+        import numpy as np
+
+        learn_set = list(learn_set)
+
+        lengths = [len(entry.time) for entry in learn_set]
+        start = np.cumsum([0] + lengths)[:-1]
+        end = np.cumsum(lengths)
+
+        size_x = 7
+        size_y = end[-1]
+
+        ax = np.zeros((size_y, size_x))
+        bx = np.zeros(size_y)
+
+        ay = np.zeros((size_y, size_x))
+        by = np.zeros(size_y)
+
+        for i in xrange(len(learn_set)):
+            entry = learn_set[i]
+            time, gaze, mouse, click = entry.time, entry.gaze, entry.mouse, entry.click
+
+            gx, gy = gaze[:,0], gaze[:,1]
+            cx, cy = mouse[:,0], mouse[:,1]
+            td, mf, tm, fv = self.features(time, mouse, click)
+            fx, fy = fv[:,0], fv[:,1]
+
+            ax[start[i]:end[i], :] = np.vstack((cx, td, tm, cx * td, cx * tm, fx, np.ones_like(cx))).T
+            bx[start[i]:end[i]] = gx
+
+            ay[start[i]:end[i], :] = np.vstack((cy, td, tm, cy * td, cy * tm, fy, np.ones_like(cx))).T
+            by[start[i]:end[i]] = gy
+
+
+        from numpy.linalg import lstsq
+
+        x, _, _, _ = lstsq(ax, bx)
+        self.sx = x
+
+        x, _, _, _ = lstsq(ay, by)
+        self.sy = x
+
+
+    def predict(self, time, mouse, click):
+        import numpy as np
+
+        cx, cy = mouse[:,0], mouse[:,1]
+        td, mf, tm, fv = self.features(time, mouse, click)
+        fx, fy = fv[:,0], fv[:,1]
+        ax = np.vstack((cx, td, tm, cx * td, cx * tm, fx, np.ones_like(cx))).T
+        ay = np.vstack((cy, td, tm, cy * td, cy * tm, fy, np.ones_like(cx))).T
+        gx = ax.dot(self.sx)
+        gy = ay.dot(self.sy)
+
+        return np.array([gx, gy]).T
+
 if __name__ == "__main__":
     data = read_data("data1.txt")
 
-    rmse_x, rmse_y, rmse_t = cross_validation(data, BaselineModel())
-    print 0.0, rmse_x, rmse_y, rmse_t
+    rmse_x, rmse_y, rmse_t = cross_validation(data, Baseline1Model())
+    print "### Baseline1 (gx = cx, gy = cy)"
+    print "  ", rmse_x, rmse_y, rmse_t
+
+    rmse_x, rmse_y, rmse_t = cross_validation(data, Baseline2Model())
+    print "### Baseline2 (gx = C1, gy = C2)"
+    print "  ", rmse_x, rmse_y, rmse_t
+
+    rmse_x, rmse_y, rmse_t = cross_validation(data, Baseline3Model())
+    print "### Baseline3 (gx = C1 + A2 * cx, gy = C1 + A2 * cx"
+    print "  ", rmse_x, rmse_y, rmse_t
+
+    rmse_x, rmse_y, rmse_t = cross_validation(data, Baseline4Model())
+    print "### Baseline4 (gx = C1, gy = C2 + A2 * cy)"
+    print "  ", rmse_x, rmse_y, rmse_t
 
     rmse_x, rmse_y, rmse_t = cross_validation(data, HuangModel())
-    print 0.0, rmse_x, rmse_y, rmse_t
+    print "### Huang    "
+    print "  ", rmse_x, rmse_y, rmse_t