Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
@thomasaarholt
thomasaarholt / pytorch_cpu_mamba.sh
Created Jun 12, 2021
Installing CPU pytorch using mamba, which does include cudatoolkit
View pytorch_cpu_mamba.sh
(base) ~: mamba create --name torchtest pytorch torchvision torchaudio cpuonly -c pytorch
__ __ __ __
/ \ / \ / \ / \
/ \/ \/ \/ \
███████████████/ /██/ /██/ /██/ /████████████████████████
/ / \ / \ / \ / \ \____
/ / \_/ \_/ \_/ \ o \__,
/ _/ \_____/ `
|/
@thomasaarholt
thomasaarholt / pytorch_cpu_conda.sh
Created Jun 12, 2021
Installing CPU pytorch using conda, which does not include cudatoolkit
View pytorch_cpu_conda.sh
(base) ~: conda create --name torch pytorch torchvision torchaudio cpuonly -c pytorch
Collecting package metadata (current_repodata.json): done
Solving environment: done
## Package Plan ##
environment location: /home/thomasaar/mambaforge/envs/torch
added / updated specs:
- cpuonly
@thomasaarholt
thomasaarholt / torch_LBFGS.py
Last active Jun 7, 2021
Function minimizer using PyTorch and L-BFGS
View torch_LBFGS.py
# Let's minimize the function f(x,y) = (x-50)**2 + (y-100)**2
# We can tell from looking at the equation that the minimum should be at (50, 100).
def func(params):
x, y = params
return (x-50)**2 + (y-100)**2
# Optionally, view what it looks like
View save_the_americans.py
fortune = 500_000_000
total_americans = 327_000_000
americans_lives_changed = 0
for american in range(total_americans):
if fortune - 1_000_000 >= 0:
fortune -= 1_000_000
americans_lives_changed += 1
else:
raise ValueError(f"Uh oh! He ran out of money! Lives changed: {americans_lives_changed}")
@thomasaarholt
thomasaarholt / unpecked_hens.py
Last active May 23, 2021
If a number of hens in a circle peck either left or right, what is the expected number of unpecked hens at the end?
View unpecked_hens.py
import numpy as np
import random
number_of_hens = 100
repeats = 1000 # repeat many times so that we get some decent statistics
sums = []
for _ in range(repeats):
hens = np.zeros(number_of_hens, dtype=bool) # reset an array of False
View vilde.py
x_ = np.linspace(0, 12, 1000)
die_y = die_fit_func(x_, *guess)
fig, ax = plt.subplots()
ax.plot(x_, die_y.real)
ax2 = ax.twinx()
ax2.plot(x_, die_y.imag, color='red')
ax.set(xlabel='Energy', ylabel='Real')
View getP1.py
def P1(P0=(0,0), Pc=(2,2), step=1):
"Get the point P1, the point to which one moves from P0 with a fixed step in the direction of Pc"
X0, Y0 = P0
Xc, Yc = Pc
diffX = Xc - X0
diffY = Yc - Y0
magnitude = (diffX**2 + diffY**2)**0.5
@thomasaarholt
thomasaarholt / .profile
Last active Mar 23, 2021
.profile used on Machine Learning Cluster
View .profile
echo 'Hello Thomas - edit PATH using "profile"'
alias ls="ls --color"
alias cd..='cd ../' # Go back 1 directory level (for fast typers)
alias cd...='cd ../../'
alias ..='cd ../' # Go back 1 directory level
alias ...='cd ../../' # Go back 2 directory levels
alias .3='cd ../../../' # Go back 3 directory levels
alias .4='cd ../../../../' # Go back 4 directory levels
@thomasaarholt
thomasaarholt / is_notebook.py
Created Aug 30, 2019
Check if code is running in jupyter notebook
View is_notebook.py
def is_notebook():
try:
from IPython import get_ipython
if "IPKernelApp" not in get_ipython().config: # pragma: no cover
raise ImportError("console")
return False
if "VSCODE_PID" in os.environ: # pragma: no cover
raise ImportError("vscode")
return False
@thomasaarholt
thomasaarholt / stem_temscript.py
Created Feb 15, 2021
Acquire STEM Rotation Series with temscript
View stem_temscript.py
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()