Mouse motion heat map - live visualization and analysis



Mouse motion heat map is an great way to study spatial attention distribution, styles of movement, reaction, etc. I am trying to design a code that visualizes such heat map. The requirements: 1) It must be real fast and 2) it must run for long time - no memory overload, etc. Simply collecting data points from 'MousePosition' in a list would probably run into a memory and slow interpolating graphics problems (unless you guys can prove otherwise ;-) ). So I came up with the idea of collecting data in an 'ArrayPlot'. This is pretty fast:

 IC = If[MousePosition["Graphics"] == None, IC, "fake",
   Chop@Mod[IC + splash[100, 20] @@ 
       Floor[{100 - #2, #1} & @@ MousePosition["Graphics"]], 10^6]];
 ArrayPlot[IC, PlotRange -> All, PlotRangePadding -> 0, 
  Frame -> False, ImageSize -> 400, ColorFunction -> "TemperatureMap"]
 , FrameMargins -> 0, AppearanceElements -> None
 , Initialization :> (
   IC = SparseArray[{{1, 1} -> 0., 100 {1, 1} -> 0.}];
   splash[n_, r_][x_, y_] := 
    SparseArray[Flatten[Table[{1 + Mod[i, n], 1 + Mod[j, n]} -> 
         1. Exp[-((i - x)^2 + (j - y)^2)/(n/10.)], {i, x - r, 
         x + r}, {j, y - r, y + r}]~
       Join~{{1, 1} -> 0., n {1, 1} -> 0.}, 1]])]

This is an insight into how handwriting proceeds through time, where the hand spends more time, which part are more difficult to draw:

enter image description here

And here some very basic type of further analysis or visualization:

Column[ListPlot[#, PlotRange -> All, PlotRangePadding -> 0, 
    Frame -> True, ImageSize -> 600, ColorFunction -> "DarkRainbow", 
    Joined -> True, PlotStyle -> Opacity[.3], 
    AspectRatio -> 1/5] & /@ {IC, Transpose@IC}, Spacings -> .05]

enter image description here

My questions are: is there more efficient and fast approach? what interesting Mathematica stats we could try? what cool apps we can make (games, writing, etc.)?

All approaches are welcome.

Vitaliy Kaurov

Posted 2012-07-23T11:50:24.737

Reputation: 66 672

This has the weird issue that it works even when you are hovering over another graphics. Does that bother? – Rojo – 2012-07-23T14:12:07.967

Methodologically related: Creating ghost trail effects

– Jens – 2012-07-23T22:00:46.937

@Jens Yes indeed - I feel this can inspire a few ideas. Thanks! – Vitaliy Kaurov – 2012-07-23T22:14:33.663

How about using a grayscale Arrayplot as an alpha channel, using a background image and playing "picture pairs"

– Dr. belisarius – 2012-07-24T00:53:04.177

@belisarius good idea. i constantly thinking of putting some game under the heatmap. but what could it be - that it won't slow down the performance. – Vitaliy Kaurov – 2012-07-25T07:50:10.900

This looks cool, but on my system (mid-2011 Macbook Pro, 2.2 GHz i7, 4 GB RAM, running Mac OSX Lion) with Mathematica 9.0.1, the touch samples are being acquired at a rather low rate by this program, so unless I move my finger across the trackpad really slowly, I get what looks more like a bunch of disjoint points on the heat map rather than the type of image you've shown. Any ideas? – Aky – 2013-05-28T07:19:37.287

@Aky Thanks. Maybe to rewrite code somehow to include function UpdateInterval - look it up in documentation. – Vitaliy Kaurov – 2013-05-28T16:13:13.227



It looks like ArrayPlot is a good approach. I found it a bit faster (especially for a larger splash width) to create the splash up front as a packed array and use RotateRight to move it around.

I have also switched off SynchronousUpdating for the graphics, so IC can update more smoothly.




Simon Woods

Posted 2012-07-23T11:50:24.737

Reputation: 81 905

Good touches, +1. It hangs my session, or at least did it twice, however... – Rojo – 2012-07-24T01:03:07.263

@Rojo So did for me the original one. I guess robust code for doing this is very difficult – Dr. belisarius – 2012-07-24T06:37:50.823

+1 Nice idea. Though it does, as noticed by others, hangs Front End - not sure why. Thanks anyway - go go creative thinking ;-) – Vitaliy Kaurov – 2012-07-24T06:52:03.450

@belisarius my code hangs your FE? Could you let me know your system specs? It works for me. – Vitaliy Kaurov – 2012-07-24T06:53:00.203

@VitaliyKaurov I happened a few times I forgot your code running and went to do other things. I can't reproduce it at will. Platform WinXP SP3, MMa 8.0 – Dr. belisarius – 2012-07-24T12:42:34.177