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 / 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()
@thomasaarholt
thomasaarholt / tophat1d.py
Created Feb 11, 2021
1D Tophat function using radius and center
View tophat1d.py
import hyperspy.api as hs
expr = "where(abs(x - center) < radius, A, 0)"
tophat = hs.model.components1D.Expression(expr, 'Top Hat', position="center", A=1, center=0, radius=1)
@thomasaarholt
thomasaarholt / tophat2d.py
Created Feb 11, 2021
2D TopHat Hyperspy Sympy Component
View tophat2d.py
import hyperspy.api as hs
expr = "where(sqrt((x - xc)**2 + (y - yc)**2) < radius, A, 0)"
tophat = hs.model.components2D.Expression(expr, 'Top Hat', position=("xc", "yc"), A=1, xc=0, yc=0, radius=1)