Unknown pleasures

Have I missed unknown pleasures in Python by focusing on R?

A comment on my blog post of last week suggested just that. Reason enough to explore Python a little. Learning another computer language is like learning another human language - it takes time. Often it is helpful to start by translating from the new language back into the old one.

I found a Python script by Ludwig Schwardt that creates a plot like this:

It is only a small Python script, but it illustrated how to:

  • load packages
  • use conditional statements
  • add comments
  • write functions
  • deal with arrays
  • write loops
  • create plots
  • save output into a PDF file

Here is my translation into R, which actually generated the plot above.

## Markus Gesmann, based on python script by Ludwig Schwardt
## https://github.com/scipy-conference/jhepc/blob/master/2013/entry16/unknown_pleasures.py
library(e1071)
library(signal)
set.seed(1234567)
pulse_profile <- c(60.3604279, 479.295197, 1965.36938, 3677.84521,
                   3769.74854, 3510.14917, 3006.14600, 2514.34790,
                   2126.71875, 2143.31738, 2370.14624, 2517.27686,
                   2097.36572, 1361.16357, 499.311279, 152.751862)

pad <- round((0.45 * length(pulse_profile)))
padded_profile <- c(rep(0, pad-1), pulse_profile, rep(0, pad-1))
profile <- resample(padded_profile,10)
profile <- abs(profile) / max(profile)
N <- length(profile)
mask <- (profile >= 0.01)

edge_window <- hamming.window(17)[1:8]
edge_window <-c(edge_window, rep(1, (N - 16)), rev(edge_window))

smooth <- function(z, M){
  filtfilt(rep(1, M)/M, 1, z) * edge_window
}
x <- 1:N
par(mar=c(4,8,4,8), bg = 'black', fg='white')
plot(x = c(1,N), y = c(0, 88), type = "n", 
     bty="n", axes=FALSE, xlab="", ylab="")

for(row in 80:1){
  signal <- rep(0, N)
  signal[mask] <- rchisq(length(profile[mask]), profile[mask])
  noise <- 0.05 * rchisq(N, rep(1, N))
  
  y <- smooth(signal, 16) + smooth(noise, 4)
  y <- row + 2*(exp(y) - 1)
  
  polygon(x = c(x, max(x), min(x)), 
          y = c(y, NA, NA),
          border = "black",
          col="black")
  lines(x, y, lwd=1.2, col="white")
}

Session Info

sessionInfo()
## R version 3.4.2 (2017-09-28)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS High Sierra 10.13
## 
## Matrix products: default
## BLAS: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib
## 
## locale:
## [1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8
## 
## attached base packages:
## [1] methods   stats     graphics  grDevices utils     datasets  base     
## 
## other attached packages:
## [1] signal_0.7-6 e1071_1.6-8 
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.13    bookdown_0.5    class_7.3-14    digest_0.6.12  
##  [5] rprojroot_1.2   MASS_7.3-47     backports_1.1.1 magrittr_1.5   
##  [9] evaluate_0.10.1 blogdown_0.1    stringi_1.1.5   rmarkdown_1.6  
## [13] tools_3.4.2     stringr_1.2.0   yaml_2.1.14     compiler_3.4.2 
## [17] htmltools_0.3.6 knitr_1.17

Related

comments powered by Disqus