Skip to content

Instantly share code, notes, and snippets.

View MySlientWind's full-sized avatar

Tao Jing (荆韬) MySlientWind

  • Department of Astronomy, Tsinghua University
  • Beijing
View GitHub Profile
@MySlientWind
MySlientWind / cache_to_file.py
Last active November 21, 2023 16:48
cache_to_file
import os
import hashlib
import pickle
import functools
import threading
def cache_to_file(cache_dir):
def decorator(func):
@MySlientWind
MySlientWind / qpy.sh
Last active March 12, 2023 07:56
submit the python job to PBS in one line
# 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
# 转极坐标(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
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)
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):
# 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]))
# 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
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))