This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import hashlib | |
import pickle | |
import functools | |
import threading | |
def cache_to_file(cache_dir): | |
def decorator(func): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# qpy main.py x y | |
# means run main.py with ncpus=x and mem=y gb | |
qpy(){ | |
ncpus=${2:-1} | |
mem=${3:-1} | |
cwd=$(pwd) | |
echo "#PBS -N "${1%.*} > qsub_${1%.*}.sh | |
echo "#PBS -j oe" >> qsub_${1%.*}.sh | |
echo "#PBS -l select=1:ncpus="$ncpus":mem="$mem"gb" >> qsub_${1%.*}.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 转极坐标(theta 取全体实数) | |
# 用日界线的思路实现直角坐标转theta角取全体实数的极坐标 | |
def to_polar(x, y): | |
rho = np.sqrt(x**2 + y**2) | |
# theta = np.arctan2(y / (x + 1e-10)) | |
theta = np.zeros_like(x) | |
circle = 0 | |
x_sign = np.sign(x) | |
y_sign = np.sign(y) | |
combine = x_sign * y_sign |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def histogram2d(X, Y, *arg, **darg): | |
z, x, y = np.histogram2d(X, Y, *arg, **darg) | |
xx = (x[:-1] + x[1:]) / 2 | |
yy = (y[:-1] + y[1:]) / 2 | |
return xx, yy, z | |
ax = plt.figure(dpi=200).add_subplot(projection='3d') | |
edges = np.linspace(-5, 7, 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis | |
def LDA_multi(X, Y, n, **drag): | |
X_bar = np.mean(X, axis=0) | |
X0 = X - X_bar | |
X0_tmp = X0.copy() | |
clf = LinearDiscriminantAnalysis(**drag) | |
X_tran = np.zeros(shape=(X0.shape[0], n)) | |
for i in range(n): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Just use non-RegularGridInterpolator with only masked data | |
data = np.random.randint(0, 10, (5,5)) | |
mask = np.random.uniform(0,1,(5,5)) > 0.5 | |
z = np.ma.array(data, mask=mask) | |
x, y = np.mgrid[0:z.shape[0], 0:z.shape[1]] | |
x1 = x[~z.mask] | |
y1 = y[~z.mask] | |
z1 = z[~z.mask] | |
interp2d(x1, y1, z1)(np.arange(z.shape[0]), np.arange(z.shape[1])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# hhmmss, ddmmss to deg | |
# example: 16360688+4436110 | |
def name2radec(name): | |
if '-' in name: | |
ra, dec = name.split('-') | |
dec = '-' + dec | |
elif '+' in name: | |
ra, dec = name.split('+') | |
dec = '+' + dec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.stats import binned_statistic | |
from scipy.stats import entropy | |
from collections import Counter | |
def get_entropy(X, bins=20, k=3): | |
nan = np.isnan(X) | |
Q1, median, Q3 = np.nanquantile(X, [0.25, 0.5, 0.75]) | |
min_ = max(Q1 - k * (median - Q1), np.nanmin(X)) | |
max_ = min((Q3 + k * (Q3 - median)), np.nanmax(X)) |