Skip to content

Instantly share code, notes, and snippets.

Avatar

Thomas Aarholt thomasaarholt

  • University of Oslo
  • Oslo, Norway
View GitHub Profile
@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)
@thomasaarholt
thomasaarholt / gist:b383445e094d9690dc9da3733d04cbbf
Created Feb 11, 2021
1D TopHat Hyperspy Sympy component using Relationals
View gist:b383445e094d9690dc9da3733d04cbbf
import hyperspy.api as hs
expr = "where(And(left < x, x < right), A, 0)"
tophat = hs.model.components1D.Expression(expr, 'Top Hat', position="left", A=1, left=0, right=1)
View loadxrd.py
def loadXRD(filename):
with open(filename, 'r') as f:
lines = f.readlines()
angles = []
intensity = []
for line in lines[1:-2]:
x, y = line.split('\n')[0].split(",")[:-1]
angles.append(float(x))
intensity.append(float(y))
x = np.array(angles)
@thomasaarholt
thomasaarholt / convolve_fft.py
Created Jan 27, 2021
2D Convolution Using FFT and Scipy for even and odd-sized arrays
View convolve_fft.py
import numpy as np
from scipy.signal import convolve2d, gaussian
from scipy.misc import face
import matplotlib.pyplot as plt
def convolve2d_fft(arr1, arr2):
s0, s1 = arr1.shape
conv = np.fft.irfft2(
np.fft.rfft2(arr1) * np.fft.rfft2(arr2),
View gist:34f5712b07b5bee119d649808961f37c
$ pip install pytetgen
Collecting pytetgen
Using cached pytetgen-0.1.4.tar.gz (392 kB)
Building wheels for collected packages: pytetgen
Building wheel for pytetgen (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: 'C:\Users\thomasaar\Miniconda3\envs\py38\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\thomasaar\\AppData\\Local\\Temp\\pip-install-xphrb4j7\\pytetgen_dc8134ee876b4f18b1a1373353bd0c09\\setup.py'"'"'; __file__='"'"'C:\\Users\\thomasaar\\AppData\\Local\\Temp\\pip-install-xphrb4j7\\pytetgen_dc8134ee876b4f18b1a1373353bd0c09\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\thomasaar\AppData\Local\Temp\pip-wheel-igdis3c8'
cwd: C:\Users\thomasaar\AppData\Local\Temp\pip-install-xphrb4j7\pytetgen_dc8134ee876b4f18b1a1373353bd0c09\
Complete output (339 lines):
running bdist_whee
@thomasaarholt
thomasaarholt / plot_arc.py
Last active Jan 13, 2021
Create a matplotlib Arc patch to show the angle between two lines
View plot_arc.py
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 / rowroll.py
Created Jan 12, 2021
Row roll of an ndarray with optional fill_value=np.nan
View rowroll.py
def row_roll(arr, shifts, axis=1, fill=np.nan):
"""Apply an independent roll for each dimensions of a single axis.
Parameters
----------
arr : np.ndarray
Array of any shape.
shifts : np.ndarray, dtype int. Shape: `(arr.shape[:axis],)`.
Amount to roll each row by. Positive shifts row right.
@thomasaarholt
thomasaarholt / SympyGaussians.py
Created Dec 14, 2020
Example of using IndexedBase in order to do for loops in Sympy for memory conservation
View SympyGaussians.py
import sympy as sp
import matplotlib.pyplot as plt
import numpy as np
def Gaussian(x, a, xc, sigma):
return a*sp.exp(-((x-xc)**2)/(2*sigma**2))
x, i, n = sp.symbols('x i n') # x is our main independent variable. i and n are indices
a, xc, sigma = sp.symbols('A xc sigma', cls=sp.IndexedBase) # parameters that are held in arrays
symbols = (x, a, xc, sigma, n)
@thomasaarholt
thomasaarholt / gist:94ce3653cefdf0fc3f209645e9304786
Created Dec 13, 2020
Bash PS1 Prompt code to show conda environment, last two directories and nothing more, in colour
View gist:94ce3653cefdf0fc3f209645e9304786
export PROMPT_DIRTRIM=2
export PS1="\[\e[0m\](\[\e[32;40m\]\$CONDA_DEFAULT_ENV\[\e[0m\]) \[\e[36;40m\]\w: \[\e[0m\]"
@thomasaarholt
thomasaarholt / aoc2020_01_rust.rs
Created Dec 2, 2020
Beginner-rust solution to Advent of Code 2020 Day 01 - Feedback very welcome!
View aoc2020_01_rust.rs
// Uses the cargo-aoc crate to help take care of inputs and benchmarks
use aoc_runner_derive::{aoc, aoc_generator};
use std::num::ParseIntError;
#[aoc_generator(day1)]
fn parse_input_day1(input: &str) -> Result<Vec<i32>, ParseIntError> {
input.lines().map(|l| l.parse()).collect()
}
#[aoc(day1, part1)]