Unknown pleasures
[This article was first published on mages' blog, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Have I missed unknown pleasures in Python by focusing on R? Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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
R code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## 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
R version 3.1.2 (2014-10-31) Platform: x86_64-apple-darwin13.4.0 (64-bit) 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 [7] base other attached packages: [1] signal_0.7-4 e1071_1.6-4 loaded via a namespace (and not attached): [1] class_7.3-11 MASS_7.3-35 tools_3.1.2
To leave a comment for the author, please follow the link and comment on their blog: mages' blog.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.