Skip to content

Instantly share code, notes, and snippets.

View romunov's full-sized avatar

Roman Luštrik romunov

View GitHub Profile
""" The task is to write comments (after ## ??) on every instance and describe it with the phrase is-a and has-a.
For further reference, please see ex41. """
## Animal is-a object (yes, sort of cinfusing) look at the extra credit
class Animal(object):
pass
## ?? Class Dog is-a Animal that has-a __init__that takes self and name as parameters
class Dog(Animal):
@romunov
romunov / skip_processed_entities.py
Created September 4, 2021 08:53
Skip processed entities
"""
Off the shelf way to skip already processed entities
This scripts processes files, remembers which files have been already
processed and skips them on the next run.
"""
import glob
import os
import shelve
@romunov
romunov / gist:b4da5124d022c112defc24799fe96e2c
Created February 10, 2021 19:47
plot a funny function with X in exponent with ggplot2
naloga <-data.frame("U308"=c(8.23, 8.78, 9.90, 10.90, 17.88, 29.00, 44.17, 83.01))
naloga$Leto <- seq(from=2000, to=1999+length(naloga$U308))
fit <- lm(log(U308) ~ Leto, data=naloga)
ggplot(naloga, aes(x = Leto, y = U308)) +
geom_point() +
ylab("Cena funta urana U308 v $") +
geom_function(fun = function(x) exp(coef(fit)[1]) * (exp(coef(fit)[2])^(x)), col="black", lty=2, size=0.3)
@romunov
romunov / rundapc.R
Created September 12, 2020 10:52
modified version of scatter.dapc for plotting points as individual points
library(adegenet)
data(H3N2)
dapc1 <- dapc(H3N2, pop=H3N2$other$epid, n.pca=30,n.da=6)
custom.cols <- rep(c("red", "blue"), times = c(1000,903))
scatter(dapc1, col = custom.cols)
@romunov
romunov / plot_two_densities.R
Last active September 5, 2020 08:01
Plot two densities with shaded area under the curve
# http://www.neilmclatchie.com/using-r-shading-area-under-a-distribution/
stp <- 0.01 # "resolution"
left.limit <- -6
right.limit <- 6
plot(
x = NA,
type = "n",
lwd = 2,
lty = 1,
@romunov
romunov / summarize_weekly_data_to_monthly.R
Created May 1, 2020 08:09
Summarize weekly data into monthly
# Use case:
# You have a weekly dataset and some variable for that week, i.e.
# week value year
# 1 528 2019
# 2 503 2019
# 3 493 2019
# 4 487 2019
# 5 526 2019
# 6 523 2019
@romunov
romunov / calcpathdistance.R
Created June 7, 2017 09:05
gist for daniel
xy <- read.table(text = "date | lon | lat | trip | distancetocoast
2014-06-11 19:02:00 | -58,3508585 | -51,88438373 | 1 | 2236.067977
2014-06-12 01:02:00 | -58,35589725 | -51,88349529 | 1 | 1000
2014-06-12 13:02:00 | -58,27224941 | -51,91903677 | 1 | 7211,102551
2014-06-12 19:02:00 | -58,27974654 | -51,90535003 | 1 | 5830,951895
2014-06-13 01:02:00 | -58,32331901 | -51,89410464 | 1 | 3605,551275
2014-06-13 07:02:00 | -58,35833139 | -51,88809227 | 1 | 1414,213562
2014-06-13 13:02:00 | -58,35617673 | -51,88156281 | 1 | 1000
2014-06-13 19:02:00 | -58,34055711 | -51,89002367 | 1 | 2236,067977
2014-06-14 01:02:00 | -58,34982536 | -51,8715761 | 2 | 1000
@romunov
romunov / .R
Created June 5, 2017 14:19
felix's problem with code
require(xts)
dailyip=readRDS(gzcon(url("https://dl.dropboxusercontent.com/s/kfxg1vi1s6hv6cg/dailyip.rds")))
dailyrx=readRDS(gzcon(url("https://dl.dropboxusercontent.com/s/of47enf79ugkq8c/dailyrx.rds")))
populate = function(x) {
if(unique(is.na(x))=='TRUE') return(x)
naseries=cbind(x,NA)[,2]
where=which(x<(yy))[1]
if(!is.na(where)) return(na.locf(cbind(x[1:where],naseries)[,1]))
@romunov
romunov / comparingmeans.R
Last active November 13, 2020 23:15
calculating means for raster stacks, cell-wise
library(raster)
library(microbenchmark)
logo <- stack(system.file("external/rlogo.grd", package="raster"))
la <- as.array(logo)
microbenchmark(vanilla = mean(logo),
overlay = overlay(logo, fun = mean), times = 50, calc(logo, fun = mean),
rasterapply = {raster(apply(la, MARGIN = c(1,2), FUN = mean))},
norasterapply = {apply(la, MARGIN = c(1,2), FUN = mean)})
@romunov
romunov / scrPID.um2.R
Created November 28, 2016 21:12
This function will return some intermediate results. Be warned that the objects may become large with time so think hard how many intermediate results you really can afford to save.
library(scrbook)
scrPID.um2 <- function (n, X, y, M, mmax, obsmod = c("pois", "bern"), niters,
npics, xlims, ylims, inits, delta)
{
obsmod <- match.arg(obsmod)
J <- nrow(n)
K <- ncol(n)
S <- inits$S
D <- e2dist(S, X)