Skip to content

Instantly share code, notes, and snippets.

@petehamilton
Last active February 8, 2018 17:43
Show Gist options
  • Save petehamilton/1f3dbfa7da4adddd61e7 to your computer and use it in GitHub Desktop.
Save petehamilton/1f3dbfa7da4adddd61e7 to your computer and use it in GitHub Desktop.

Revisions

  1. petehamilton revised this gist Jan 22, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions plot-data.r
    Original file line number Diff line number Diff line change
    @@ -17,14 +17,14 @@ gocardless_dark_red = rgb(0.753, 0.361, 0.361)
    # Set variables
    width = 2000
    height = 3000
    output_directory = "/Users/petehamilton/projects/gocardless-map/output/gc-customers-improved2"
    output_directory = "/Users/petehamilton/projects/gocardless-map/output/gc-customers"

    # Initialize parallel
    cl <- makeCluster(detectCores() - 1)
    registerDoParallel(cl, cores = detectCores() - 1)

    # Read in the data
    all_customers <- read.csv("/Users/petehamilton/projects/gocardless-map/data/r-customers-export.csv", header = TRUE)
    all_customers <- read.csv("/Users/petehamilton/projects/gocardless-map/data/customers-export.csv", header = TRUE)

    # Make dates actual dates rather than strings (for comparison)
    all_customers$date <- as.Date(as.character(all_customers$date),format="%Y-%m-%d")
  2. petehamilton revised this gist Jan 21, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion plot-data.r
    Original file line number Diff line number Diff line change
    @@ -90,4 +90,4 @@ stopCluster(cl)

    # For making a .mov, I found I needed to change a few things:
    # See http://superuser.com/questions/820134/why-cant-quicktime-play-a-movie-file-encoded-by-ffmpeg
    # ffmpeg -f image2 -r 40 -i frame_%06d.png -vcodec libx264 -pix_fmt yuv420p out.mov
    # ffmpeg -f image2 -r 30 -i frame_%06d.png -vcodec libx264 -pix_fmt yuv420p out.mov
  3. petehamilton revised this gist Jan 21, 2015. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions plot-data.r
    Original file line number Diff line number Diff line change
    @@ -84,6 +84,10 @@ foreach(i = 1:final_frame_count, .packages = c("maps","mapdata")) %dopar% {

    stopCluster(cl)

    # Then in the images dir
    # Then in the images dir:
    # ffmpeg -r 30 -i frame_%06d.png -b:v 2048k customer-growth.mpg
    # avconv -f image2 -i %06d.png -c:v h264 -crf 1 -r 30 out.mov
    # avconv -f image2 -i %06d.png -c:v h264 -crf 1 -r 30 out.mpg

    # For making a .mov, I found I needed to change a few things:
    # See http://superuser.com/questions/820134/why-cant-quicktime-play-a-movie-file-encoded-by-ffmpeg
    # ffmpeg -f image2 -r 40 -i frame_%06d.png -vcodec libx264 -pix_fmt yuv420p out.mov
  4. petehamilton revised this gist Jan 21, 2015. 1 changed file with 4 additions and 3 deletions.
    7 changes: 4 additions & 3 deletions plot-data.r
    Original file line number Diff line number Diff line change
    @@ -50,6 +50,7 @@ set_title <- function(text, color) {
    print(paste("Num. Customers:", nrow(all_customers)))
    print(paste("Total Days:", length(all_days)))


    main_frame_count = length(all_days)
    foreach(i = 1:main_frame_count, .packages = c("maps","mapdata")) %dopar% {
    setwd(output_directory)
    @@ -67,7 +68,7 @@ foreach(i = 1:main_frame_count, .packages = c("maps","mapdata")) %dopar% {
    dev.off(dev.cur())
    }

    final_frame_count = 60 # 1s @ 60fps
    final_frame_count = 60 # 2s @ 30fps
    foreach(i = 1:final_frame_count, .packages = c("maps","mapdata")) %dopar% {
    setwd(output_directory)
    png(sprintf("frame_%06d.png", main_frame_count + i), height=height, width=width, pointsize = 80)
    @@ -84,5 +85,5 @@ foreach(i = 1:final_frame_count, .packages = c("maps","mapdata")) %dopar% {
    stopCluster(cl)

    # Then in the images dir
    # ffmpeg -r 60 -i frame_%06d.png -b:v 2048k customer-growth.mpg
    # avconv -f image2 -i %06d.png -c:v h264 -crf 1 -r 60 out.mov
    # ffmpeg -r 30 -i frame_%06d.png -b:v 2048k customer-growth.mpg
    # avconv -f image2 -i %06d.png -c:v h264 -crf 1 -r 30 out.mov
  5. petehamilton revised this gist Jan 21, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion plot-data.r
    Original file line number Diff line number Diff line change
    @@ -50,7 +50,6 @@ set_title <- function(text, color) {
    print(paste("Num. Customers:", nrow(all_customers)))
    print(paste("Total Days:", length(all_days)))


    main_frame_count = length(all_days)
    foreach(i = 1:main_frame_count, .packages = c("maps","mapdata")) %dopar% {
    setwd(output_directory)
  6. petehamilton revised this gist Jan 21, 2015. 1 changed file with 61 additions and 42 deletions.
    103 changes: 61 additions & 42 deletions plot-data.r
    Original file line number Diff line number Diff line change
    @@ -1,70 +1,89 @@
    # Execute with: R --slave -f draw.r

    library(maps)
    library(mapdata)
    library(doParallel)
    library(foreach)
    library(doParallel)

    setwd("/Users/petehamilton/gocardless-maps")
    # Use the GoCardless font
    par(family="Gotham-Book")

    # Set variables
    # Set colours
    gocardless_blue = rgb(0.31,0.57,0.85)
    gocardless_blue_translucent = rgb(0.31,0.57,0.85,0.1)
    dark_red = rgb(0.753, 0.361, 0.361)
    par(family="Gotham-Book")
    gocardless_dark_grey = rgb(0.298, 0.308, 0.394)
    gocardless_dark_red = rgb(0.753, 0.361, 0.361)

    # Read data
    customer_data <- read.csv("data/r-customers-export.csv", header = TRUE)
    # Set variables
    width = 2000
    height = 3000
    output_directory = "/Users/petehamilton/projects/gocardless-map/output/gc-customers-improved2"

    # Use ALL cores
    # Initialize parallel
    cl <- makeCluster(detectCores() - 1)
    registerDoParallel(cl, cores = detectCores() - 1)

    num_frames <- 1800 # 30s @ 60fps
    batch_size = round(nrow(customerdata) / num_frames)
    # Read in the data
    all_customers <- read.csv("/Users/petehamilton/projects/gocardless-map/data/r-customers-export.csv", header = TRUE)

    # Make dates actual dates rather than strings (for comparison)
    all_customers$date <- as.Date(as.character(all_customers$date),format="%Y-%m-%d")

    foreach(i = 1:num_frames, .packages = c("maps","mapdata")) %dopar% {
    # Set name for frame file
    filename = paste("frame_", sprintf("%06d", i), ".png")
    png(filename, height=3000, width=2000, pointsize = 80)
    # Get a list of all days from first to last customer
    first_record = head(all_customers, 1)
    last_record = tail(all_customers, 1)
    all_days = seq(from=as.Date(first_record$date), to=as.Date(last_record$date), "days")

    # Split dataset
    from = 1 # Or, for non-cumulative: (i-1)*batch_size
    new_start = min((i - 1)*batch_size, nrow(customerdata))
    new_end = min(i*batch_size - 1, nrow(customerdata))
    previous_customers = customerdata[from:max(new_start - 1, 0),]
    new_customers = customerdata[new_start:new_end,]
    # Helper functions
    draw_map <- function(border_color) {
    map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col=border_color, mar=rep(1,4))
    }

    plot_customers <- function(customers, point_color, point_size_cex) {
    points(customers$lon,customers$lat, col=point_color, pch=20, cex=point_size_cex)
    }

    set_title <- function(text, color) {
    title(text, col.main=color)
    }

    print(paste("Num. Customers:", nrow(all_customers)))
    print(paste("Total Days:", length(all_days)))

    # Initialize map
    map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col='white', mar=rep(1,4))

    # Plot older points in blue
    points(previous_customers$lon,previous_customers$lat, col=gocardless_blue_translucent, pch=20, cex=0.2)
    main_frame_count = length(all_days)
    foreach(i = 1:main_frame_count, .packages = c("maps","mapdata")) %dopar% {
    setwd(output_directory)
    png(sprintf("frame_%06d.png", i), height=height, width=width, pointsize = 80)

    # Plot new points in red
    points(new_customers$lon, new_customers$lat,col=new_point_colour, pch=20, cex=0.3)
    # Split customers added prior to and on the current date
    historic_customers <- all_customers[all_customers$date < all_days[i],]
    new_customers <- all_customers[all_customers$date == all_days[i],]

    # Make title the date of latest customer plotted
    title(customerdata[new_end,]$date, col.main=title_colour)
    draw_map("white")
    plot_customers(historic_customers, gocardless_blue_translucent, 0.2)
    plot_customers(new_customers, gocardless_dark_red, 0.3)
    set_title(format(all_days[i], format="%Y-%m-%d"), gocardless_blue)

    dev.off(dev.cur())
    }

    # 2 second fade in at the end
    fadein_frames = 60
    final_frame_count = 60 # 1s @ 60fps
    foreach(i = 1:final_frame_count, .packages = c("maps","mapdata")) %dopar% {
    setwd(output_directory)
    png(sprintf("frame_%06d.png", main_frame_count + i), height=height, width=width, pointsize = 80)

    foreach(i = 1:fadein_frames, .packages = c("maps","mapdata")) %dopar% {
    # Set name for frame file
    filename = paste("frame_", sprintf("%06d", num_frames + i), ".png")
    png(filename, height=3000, width=2000, pointsize = 80)
    opacity = i/final_frame_count

    opacity_level = i/fadein_frames
    draw_map(rgb(0.31,0.57,0.85, opacity))
    plot_customers(all_customers, gocardless_blue_translucent, 0.2)
    set_title("GoCardless", rgb(0.31,0.57,0.85, opacity))

    # Initialize map
    map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col=rgb(0.298, 0.308, 0.394, opacity_level), mar=rep(1,4))

    # Plot all points
    points(customerdata$lon, customerdata$lat, col=old_point_colour, pch=20, cex=0.2)
    title("GoCardless", col.main=rgb(0.31,0.57,0.85, opacity_level))
    dev.off(dev.cur())
    }

    stopCluster(cl)
    stopCluster(cl)

    # Then in the images dir
    # ffmpeg -r 60 -i frame_%06d.png -b:v 2048k customer-growth.mpg
    # avconv -f image2 -i %06d.png -c:v h264 -crf 1 -r 60 out.mov
  7. petehamilton created this gist Jan 20, 2015.
    70 changes: 70 additions & 0 deletions plot-data.r
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    library(maps)
    library(mapdata)
    library(doParallel)
    library(foreach)

    setwd("/Users/petehamilton/gocardless-maps")

    # Set variables
    gocardless_blue = rgb(0.31,0.57,0.85)
    gocardless_blue_translucent = rgb(0.31,0.57,0.85,0.1)
    dark_red = rgb(0.753, 0.361, 0.361)
    par(family="Gotham-Book")

    # Read data
    customer_data <- read.csv("data/r-customers-export.csv", header = TRUE)

    # Use ALL cores
    cl <- makeCluster(detectCores() - 1)
    registerDoParallel(cl, cores = detectCores() - 1)

    num_frames <- 1800 # 30s @ 60fps
    batch_size = round(nrow(customerdata) / num_frames)

    foreach(i = 1:num_frames, .packages = c("maps","mapdata")) %dopar% {
    # Set name for frame file
    filename = paste("frame_", sprintf("%06d", i), ".png")
    png(filename, height=3000, width=2000, pointsize = 80)

    # Split dataset
    from = 1 # Or, for non-cumulative: (i-1)*batch_size
    new_start = min((i - 1)*batch_size, nrow(customerdata))
    new_end = min(i*batch_size - 1, nrow(customerdata))
    previous_customers = customerdata[from:max(new_start - 1, 0),]
    new_customers = customerdata[new_start:new_end,]

    # Initialize map
    map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col='white', mar=rep(1,4))

    # Plot older points in blue
    points(previous_customers$lon,previous_customers$lat, col=gocardless_blue_translucent, pch=20, cex=0.2)

    # Plot new points in red
    points(new_customers$lon, new_customers$lat,col=new_point_colour, pch=20, cex=0.3)

    # Make title the date of latest customer plotted
    title(customerdata[new_end,]$date, col.main=title_colour)

    dev.off(dev.cur())
    }

    # 2 second fade in at the end
    fadein_frames = 60

    foreach(i = 1:fadein_frames, .packages = c("maps","mapdata")) %dopar% {
    # Set name for frame file
    filename = paste("frame_", sprintf("%06d", num_frames + i), ".png")
    png(filename, height=3000, width=2000, pointsize = 80)

    opacity_level = i/fadein_frames

    # Initialize map
    map('worldHires', c('UK', 'Ireland', 'Isle of Man','Isle of Wight'), xlim=c(-11,3), ylim=c(49,60.9), fill=FALSE, col=rgb(0.298, 0.308, 0.394, opacity_level), mar=rep(1,4))

    # Plot all points
    points(customerdata$lon, customerdata$lat, col=old_point_colour, pch=20, cex=0.2)
    title("GoCardless", col.main=rgb(0.31,0.57,0.85, opacity_level))
    dev.off(dev.cur())
    }

    stopCluster(cl)