Subscribe to DSC Newsletter

The 90-seconds video showing a 'random walk' (a particular case of a Markov process) evolving over 400,000 steps, was produced using two pieces of code.

Last frame of the video:  darker areas are those visited first

1. Production of the data set

The data set consists of 2,000 video frames. Each frame is a set of 200 new (x, y) locations (2,000 times 200 = 400,000). The output data set rw.txt has 3 fields: frame number (referred to as iter in the second program), x, and y coordinates.

Here's the code (Perl version, easy to translate to Python, PHP, Java or C++):



# $end must be > $niter * $m

open(OUT,"> extremes.txt");
for ($k=0; $k<$end; $k++) {
  if ($x> $max_x) { $max_x=$x; }
  if ($y> $max_y) { $max_y=$y; }
  if ($x< $min_x) { $min_x=$x; }
  if ($y< $min_y) { $min_y=$y; }
  print OUT "$max_x\t$max_y\t$min_x\t$min_y\n";

print OUT "iter\tx\ty\n";
for ($iter=0; $iter<$niter; $iter++) { # generate niter frames for video
  for ($k=0; $k<$n; $k++) { # display n points on each frame
    $idx=$m*$iter+$k; # add m new points in each subsequent frame, delete m old points
    print OUT "$iter\t$ax[$idx]\t$ay[$idx]\n";

print "$min_x | $max_x | $min_y | $max_y\n";

Note that in addition to producing the data set rw.txt for step #2, it also produces an auxiliary file extremes.txt, to perform some ad-hoc analyses to answer some of the questions in the associated challenge of the week.

2. Production of the video, using R

The R code below reads the data set rw.txt produced in step #1, and displays the 2,000 frames on your screen.



for (n in 1:2000) {

  x<-vv$x[iter == n];
  y<-vv$y[iter == n];
  p<-vv$x[iter == n-1];
  q<-vv$y[iter == n-1];

  if (n==1) {
          plot(x,y,xlim=c(minx,maxx),ylim=c(miny,maxy),pch=40, cex=0,col=rgb(1,1,0),xlab="",ylab="",axes=TRUE);



# Sys.sleep(0.2);

For details about how the video was captured from the R GUI using an open-source screencast tool, click here.  At each iteration, the 200 points in the current frame are displayed in red (using col=rgb(1,0,0) in the R code), to help you visualize the progress on the screen. 

For more information, check:

Views: 1598

Follow Us

On Data Science Central

On DataViz

On Hadoop

© 2017 is a subsidiary and dedicated channel of Data Science Central LLC   Powered by

Badges  |  Report an Issue  |  Terms of Service