Skip to content

Instantly share code, notes, and snippets.

View syrte's full-sized avatar

Zhaozhou Li syrte

View GitHub Profile
@syrte
syrte / smooth_piecewise_linear.py
Created April 30, 2025 06:50
fit gaussian process and smooth piecewise linear function
import numpy as np
import matplotlib.pyplot as plt
def fit_gp(x, y, y_err, y_avg=None, x_pred=None, scale=1):
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn.gaussian_process.kernels import RBF, ConstantKernel, Matern, WhiteKernel
from sklearn.exceptions import ConvergenceWarning
import warnings
import numpy as np
from matplotlib import pyplot as plt
from scipy.interpolate import griddata
def add_label(x, y, x0=None, y0=None, dx=0, dy=0, s='', **kwargs):
"add label on the curve (x, y) at location specified by (x0, y0)"
if x0 is not None:
if x[-1] > x[0]:
y0 = np.interp(x0, x, y)
@syrte
syrte / flip_AM.py
Last active December 7, 2024 13:46
flip angular momentum
import astropy.units as u
import numpy as np
from matplotlib.pylab import *
import agama
agama.setUnits(length=1, velocity=1, mass=1)
def sigmoid(x, fmax=1, scale=0.5):
"""
@syrte
syrte / clean_latex.sh
Created June 4, 2024 21:27
copy from stackoverflow.
#!/bin/bash
for image_file in $(ls figs6/)
do
if grep $image_file */*.log -c > 1
then
echo "File $image_file is in use."
else
echo "File $image_file is not in use."
mv "figs6/$image_file" "figs6/moved.$image_file" # or any other action
@syrte
syrte / Sersic_Einasto_index.py
Created January 29, 2024 16:09
To compute the coefficient for Sersic or Einasto profile with half-mass radius
from scipy.special import gammaincinv
def b(n):
# 2 * n - 1 / 3 + 4 / (405 * n) + 46 / (25515 * n**2) + 131 / (1148175 * n**3)
return gammaincinv(2 * n, 0.5)
def d(n):
# 3 * n - 1 / 3 + 8 / (1215 * n) + 184 / (229635 * n**2) + 1048 / (31000725 * n**3)
return gammaincinv(3 * n, 0.5)
@syrte
syrte / sersic_lum.py
Created January 29, 2024 08:55 — forked from bamford/sersic_lum.py
Luminosity of Sérsic function in annulus
from numpy import pi, exp
from scipy.special import gamma, gammaincinv, gammainc
# Normalisation constant
def b(n):
return gammaincinv(2*n, 0.5)
# Total luminosity of a 2D Sérsic profile
def sersic_total_lum(Ie, re, n):
bn = b(n)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@syrte
syrte / mandc.py
Last active July 10, 2023 15:26
exec and load Zhao+2009 model, see http://202.127.29.4/dhzhao/mandc.html
"""
exec and load Zhao+2009 model
cf http://202.127.29.4/dhzhao/mandc.html
"""
import os
from astropy.io import ascii
# -*- coding:utf-8 -*-
from __future__ import division
from numpy import sqrt, exp, log, abs, diff, sin, cos, linspace, interp
from numpy import empty_like, atleast_1d, asarray
from numpy.testing import assert_almost_equal
from scipy.optimize import bisect as root
from numpy import e, pi
from scipy.integrate import romberg
mid = lambda x: (x[1:] + x[:-1]) / 2.
from astropy import units as u
(0 * u.ABmag).to(u.erg/u.s/u.cm**2/u.Hz)
# 0 ABmag corresponsds to 3.6307805477010035e-20 erg / (cm2 Hz s)