Skip to content

Instantly share code, notes, and snippets.

View thomasaarholt's full-sized avatar

Thomas Aarholt thomasaarholt

View GitHub Profile
@thomasaarholt
thomasaarholt / branch.sh
Created March 21, 2023 20:41
git branch change using fzf in lovely color
branch () {
branches=$(git for-each-ref --sort=-committerdate refs/heads/ --format='%(color:cyan)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:magenta)%(authorname)%(color:reset)' --color=always|column -ts'|') &&
branch=$(echo "$branches" | fzf --multi --ansi --height=20%)
if [ -z "${branch}" ];
then return 0
else git switch $(echo "$branch" | sed "s/ .*//")
fi
}
@thomasaarholt
thomasaarholt / plot_arc.py
Last active February 2, 2023 02:01
Create a matplotlib Arc patch to show the angle between two lines
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
Arc = matplotlib.patches.Arc
def halfangle(a, b):
"Gets the middle angle between a and b, when increasing from a to b"
if b < a:
b += 360
return (a + b)/2 % 360
@thomasaarholt
thomasaarholt / autoscale.py
Last active January 23, 2023 15:52
Matplotlib autoscale
def autoscale(ax=None, axis='y', margin=0.1):
'''Autoscales the x or y axis of a given matplotlib ax object
to fit the margins set by manually limits of the other axis,
with margins in fraction of the width of the plot
Defaults to current axes object if not specified.
'''
import matplotlib.pyplot as plt
import numpy as np
if ax is None:
@thomasaarholt
thomasaarholt / venv.sh
Created November 6, 2022 15:22
Bash function for creating virtual environments that are automatically (de)activated upon cd entry(exit).
venv () {
RED='\033[0;31m'
NC='\033[0m' # No Color
# create virtualenv
if [ ! -d .venv ] # if venv does not exist already
then
python -m venv .venv --system-site-packages
else
echo -e "${RED}.venv${NC} already exists!"
return 0
@thomasaarholt
thomasaarholt / train_test_split_polars.py
Created July 24, 2022 13:06
Train and test split function for polars dataframes
def train_test_split(
df: pl.DataFrame, train_fraction: float = 0.75
) -> Tuple[pl.DataFrame, pl.DataFrame]:
"""Split polars dataframe into two sets.
Args:
df (pl.DataFrame): Dataframe to split
train_fraction (float, optional): Fraction that goes to train. Defaults to 0.75.
@thomasaarholt
thomasaarholt / xgboost_gpu.sh
Last active July 24, 2022 11:01
Building XGBoost with GPU support from source on Red Hat with mload
# must not use gcc version > 10
module load CUDA/11.0 CMake/3.21.1-GCCcore-11.2.0
# Clone with recursive! Otherwise you get dmlc error later on!
git clone --recursive https://github.com/dmlc/xgboost
cd xgboost/python-package
# clean out any old build files that may break things if try to recompile
rm -rf build
@thomasaarholt
thomasaarholt / lightgbm_gpu.sh
Last active July 24, 2022 11:01
Building lightgbm with GPU support from source on Red Hat with mload
module load CUDA/11.0 CMake/3.21.1-GCCcore-11.2.0 Boost/1.74.0-GCC-10.2.0
pip install lightgbm --install-option=--gpu
# If running from notebook, ensure that the following contains Boost:
# import os
# os.environ["LD_LIBRARY_PATH"].split(":")
@thomasaarholt
thomasaarholt / docker_terminal.sh
Created June 6, 2022 08:23
Instructions for building a docker image and logging into its bash terminal
# From a directory with a Dockerfile
# Example Dockerfile with "Dockerfile" as filename
FROM python:3.10-slim-buster
RUN pip install numpy
# Then, run the following
docker build -t your_image_name .
@thomasaarholt
thomasaarholt / stem_temscript.py
Created February 15, 2021 16:20
Acquire STEM Rotation Series with temscript
from pprint import pprint
import temscript as ts
from math import pi
dwelltime = 1e-5 # seconds
tem = ts.Microscope()
illumination = tem._tem_illumination
instrument = ts.GetInstrument()
@thomasaarholt
thomasaarholt / savefig_no_whitespace.py
Last active March 17, 2022 03:07
Save matplotlib figures with no whitespace
import matplotlib.pyplot as plt
def save(filepath="image.png", fig=None):
'''Save the current image with no whitespace
Example filepath: "myfig.png" or r"C:\myfig.pdf"
Based on answers from https://stackoverflow.com/questions/11837979/
'''
import matplotlib.pyplot as plt
if not fig:
fig = plt.gcf()