Skip to content

Instantly share code, notes, and snippets.

View ntessore's full-sized avatar

Nicolas Tessore ntessore

View GitHub Profile
@ntessore
ntessore / all.py
Last active September 29, 2022 22:47
Automatically import all packages from a Python namespace package
# author: Nicolas Tessore <n.tessore@ucl.ac.uk>
# license: MIT
'''meta-module that imports all modules from its namespace package
Package this module in your namespace. Then use ``import mynamespace.all`` and
every module in the namespace will be available under the import.
'''
@ntessore
ntessore / plot_hp3d.py
Last active March 27, 2022 04:14
plot HEALPix maps in 3D using matplotlib
import numpy as np
import healpy as hp
import matplotlib.pyplot as plt
import matplotlib as mpl
def plot_hp3d(ax, m, r=1.0, vmin=None, vmax=None, cmap=None, norm=None,
alpha=None, shade=False, **kwargs):
m = np.asanyarray(m)
*nmaps, npix = m.shape
@ntessore
ntessore / array.py
Last active October 27, 2021 12:33
quickly create ndarray subtypes with attributes
# author: Nicolas Tessore <n.tessore@ucl.ac.uk>
# license: MIT
'''utility functions for numpy arrays'''
import numpy as np
def array_with_attributes(name, **attrs):
'''create an array subclass with given attributes'''
@ntessore
ntessore / flask-in-conda.sh
Last active April 15, 2021 15:56
install FLASK in a conda environment
# create and activate a new environment for flask
conda create -n flask
conda activate flask
# switch to the root of the environment
cd $CONDA_PREFIX
# use ONLY conda-forge for packages
echo 'channels: [conda-forge]' > .condarc
@ntessore
ntessore / algo.hpp
Created February 12, 2021 01:08
C++ algorithm scrapbook
// argsort(first, last, a_first, [compare])
// ---
// Store the order of the input range [`first`, `last`) in the output range
// starting at `a_first`. Both iterator types must allow random access.
//
template<class ValueIt, class IndexIt,
class Op = std::less<typename std::iterator_traits<ValueIt>::value_type>>
void argsort(ValueIt first, ValueIt last, IndexIt a_first, Op compare = Op())
{
using T = typename std::iterator_traits<IndexIt>::value_type;
@ntessore
ntessore / pixshape.ipynb
Created December 11, 2020 16:16
HEALPix pixel shape
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@ntessore
ntessore / lnbessel.c
Last active December 6, 2020 14:53
compute the logarithm of Bessel functions of large argument and order
// compute the logarithm of Bessel functions of large argument and order
//
// author: Nicolas Tessore <n.tessore@ucl.ac.uk>
// date: 6 Dec 2020
//
#include <math.h>
// compute the Bessel function ratio J_{nu-1}(x)/J_nu(x) from a continued
@ntessore
ntessore / fftlognt.py
Last active November 13, 2020 10:13
FFTLog in Python, using only numpy and scipy.special.loggamma
import numpy as np
from scipy.fft import rfft, irfft
from scipy.special import loggamma
# constants
LN_2 = np.log(2)
LN_10 = np.log(10)
@ntessore
ntessore / dl.py
Last active December 7, 2019 08:19
download a file if not already downloaded
# download file if not already downloaded
def dl(url, file=None):
from urllib.request import urlretrieve
from urllib.parse import urlparse
import os.path
if file is None:
file = os.path.basename(urlparse(url).path)
if not os.path.exists(file):
urlretrieve(url, file)
return file
@ntessore
ntessore / get_lensed.sh
Created October 18, 2019 12:56
Install MultiNest and Lensed
export FC='/opt/local/bin/gfortran-mp-4.8'
set -e
mkdir lensed
pushd lensed
git clone --depth=1 https://github.com/farhanferoz/MultiNest
pushd MultiNest/MultiNest_v3.11_cmake/multinest
mkdir build
pushd build