Skip to content

Instantly share code, notes, and snippets.

@swayson
swayson / minimal_barchart.R
Created September 30, 2017 22:08
An example of a minimalistic barchart using ggplot2
library(tidyverse)
airquality %>%
group_by(Month) %>%
summarise(average_temperature = mean(Temp)) %>%
ggplot(aes(x=Month, y=average_temperature)) +
geom_bar(stat='identity', position = 'dodge') +
geom_text(aes(label=round(average_temperature, 0)), position=position_dodge(width=0.9), vjust=-0.5) +
labs(x='Month', y='Average temperature', title='Average temperature per month') +
theme_minimal() +
theme(plot.background = element_blank(),
@swayson
swayson / netrw quick reference.md
Created April 25, 2017 10:48 — forked from t-mart/netrw quick reference.md
A quick reference for Vim's built-in netrw file selector.
Map Action
<F1> Causes Netrw to issue help
<cr> Netrw will enter the directory or read the file
<del> Netrw will attempt to remove the file/directory
- Makes Netrw go up one directory
a Toggles between normal display, hiding (suppress display of files matching g:netrw_list_hide) showing (display only files which match g:netrw_list_hide)
c Make browsing directory the current directory
C Setting the editing window
d Make a directory
@swayson
swayson / xlsx_sheetnames.py
Last active April 9, 2017 07:47
Simple utility command line tool to list the sheet names of an Excel workbook.
"""
Simple utility command line tool to list the sheet names of an Excel workbook.
"""
import argparse
from xlrd import open_workbook
def get_args():
"""This function parses and return command line arguments"""
parser = argparse.ArgumentParser(description='List sheetnames in Excel workbook.')
@swayson
swayson / install.sh
Created March 11, 2017 10:44 — forked from wdullaer/install.sh
Install Latest Docker and Docker-compose on Ubuntu
# Ask for the user password
# Script only works if sudo caches the password for a few minutes
sudo true
# Install kernel extra's to enable docker aufs support
# sudo apt-get -y install linux-image-extra-$(uname -r)
# Add Docker PPA and install latest version
# sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 36A1D7869245C8950F966E92D8576A8BA88D21E9
# sudo sh -c "echo deb https://get.docker.io/ubuntu docker main > /etc/apt/sources.list.d/docker.list"
@swayson
swayson / simple_example_zipfile.py
Created November 29, 2016 11:49
Simple example on how to use Python Zipfile to make a compressed archive. Here additional care is taken to provide an arcname, such that the root is avoided in the archive.
files = [path/to/file, path/to/file2, path/to/file3]
with zipfile.ZipFile('/tmp/test.zip', 'w', zipfile.ZIP_DEFLATED) as out_file:
for rel_filename in files:
absname = os.path.abspath(rel_filename)
root = os.path.dirname(absname)
filename = os.path.relpath(rel_filename, root)
out_file.write(rel_filename, filename)
print(absname, root, filename)
@swayson
swayson / standard_folders.py
Last active March 12, 2017 10:10
Simple python cli application to construct directories according to specifications.
import os
import click
def read_file(filename):
with open(filename) as in_file:
for line in in_file:
if line.strip() != '':
yield line.strip()
@swayson
swayson / kulback_leibler_divergence.py
Last active September 28, 2022 07:21
Numpy and scipy ways to calculate KL Divergence.
"""
Specifically, the Kullback–Leibler divergence from Q to P, denoted DKL(P‖Q), is
a measure of the information gained when one revises one's beliefs from the
prior probability distribution Q to the posterior probability distribution P. In
other words, it is the amount of information lost when Q is used to approximate
P.
"""
import numpy as np
from scipy.stats import entropy
@swayson
swayson / rolling_block.py
Last active November 14, 2016 05:30
Rolling block Numpy using strided tricks and padding.
import numpy as np
from numpy.lib.stride_tricks import as_strided
def rolling_block(A, block=(3, 3)):
shape = (A.shape[0] - block[0] + 1, A.shape[1] - block[1] + 1) + block
strides = (A.strides[0], A.strides[1]) + A.strides
return as_strided(A, shape=shape, strides=strides)
X = np.random.randint(0, 200, (10, 10))
@swayson
swayson / niceness.txt
Created October 31, 2016 05:51
linux-niceness CPU priority
WHERE XX defines the priority. -19 being highest priority. 19 being the lowest.
sudo renice -n [XX] [PID]
nice -n [XX] [COMMAND]
@swayson
swayson / get_executing_script_dir.py
Created October 16, 2016 06:11
Snippet to construct the absolute path of the directory, in which the currently executing script is located.
import os
os.path.dirname(os.path.realpath(__file__))