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.6.2 (2019-12-12)
## Platform: x86_64-apple-darwin15.6.0 (64-bit)
## Running under: macOS Catalina 10.15.2
## 
## Matrix products: default
## BLAS:   /Library/Frameworks/R.framework/Versions/3.6/Resources/lib/libRblas.0.dylib
## LAPACK: /Library/Frameworks/R.framework/Versions/3.6/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] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] signal_0.7-6 e1071_1.7-3 
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_1.0.3      bookdown_0.17   class_7.3-15    digest_0.6.23  
##  [5] MASS_7.3-51.5   magrittr_1.5    evaluate_0.14   blogdown_0.17  
##  [9] rlang_0.4.2     stringi_1.4.5   rmarkdown_2.0   tools_3.6.2    
## [13] stringr_1.4.0   xfun_0.11       yaml_2.2.0      compiler_3.6.2 
## [17] htmltools_0.4.0 knitr_1.26

Citation

For attribution, please cite this work as:

Markus Gesmann (Nov 11, 2014) Unknown pleasures. Retrieved from https://magesblog.com/post/2014-11-11-unknown-pleasures/

BibTeX citation:

@misc{ 2014-unknown-pleasures,
 author = { Markus Gesmann },
 title = { Unknown pleasures },
 url = { https://magesblog.com/post/2014-11-11-unknown-pleasures/ },
 year = { 2014 }
 updated = { Nov 11, 2014 }
}

Related