Skip to content

Instantly share code, notes, and snippets.

View j-faria's full-sized avatar
🎯
Focusing

João Faria j-faria

🎯
Focusing
View GitHub Profile
@j-faria
j-faria / fast.py
Created June 20, 2018 17:51
Three slightly different ways to do get stellar mass using the Torres calibration
def massTorres(teff, erteff, logg, erlogg, feh, erfeh,
ntrials=10000, corrected=True):
randomteff = teff + erteff * np.random.randn(ntrials)
randomlogg = logg + erlogg * np.random.randn(ntrials)
randomfeh = feh + erfeh * np.random.randn(ntrials)
# Parameters for the Torres calibration
a1 = 1.5689
a2 = 1.3787
a3 = 0.4243
@j-faria
j-faria / countcolor.sh
Last active April 20, 2018 14:21
Count the number of color pages in a pdf
gs -o - -sDEVICE=inkcov input.pdf | grep -v "^ 0.00000 0.00000 0.00000" | grep "^ " | wc -l
# MIT Licensed
# Copyright (c) 2009-2010 Peter Shinners <pete@shinners.org>
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
import numpy as np
from scipy.stats import binned_statistic
from astropy.convolution import convolve, Box1DKernel
def f8(time, flux):
"""
Calculate the F8 statistic, as in Bastien et al.
Both `time` and `flux` should be numpy arrays.
The 8-hour flicker (F8) is determined by performing a 16-point (8 hour)
boxcar smoothing of the light curve, subtracting it from the original light curve
@j-faria
j-faria / timer.py
Created March 9, 2018 16:55
With IPython, timing snippets of code is easy
from IPython import get_ipython
timer = lambda code: get_ipython().run_line_magic('timeit', '-o ' + code)
out = timer('pass')
out = timer('a = 0')
@j-faria
j-faria / matrix_exponential.cpp
Last active December 4, 2022 13:16
C++ and pybind11 code for matrix exponential
#include <iostream>
#include <mkl.h>
#include <math.h>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h>
namespace py = pybind11;
using namespace pybind11::literals;
using namespace std;
@j-faria
j-faria / develop.py
Created September 6, 2017 13:39
re-import everything at any stage
def develop():
import reimport, os
mod = reimport.modified()
try:
mod.remove('__main__')
except ValueError:
pass
reimport.reimport(*mod)
print 'Done re-importing'
@j-faria
j-faria / acknowledgements.py
Last active September 8, 2017 13:51
Print the damn acknowledgements!
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Print the acknowledgements sentence with all the project references!
See acknowledgements.py -h or run acknowledgements.py ? for help or simply use as
acknowledgements.py Author1 Author2
"""
name = 'acknowledgements'
@j-faria
j-faria / planet.py
Created May 10, 2017 19:16
Calculate the mass and semi-major axis of a planet, from its orbital period, semi-amplitude and eccentricity
from astropy.constants import G
import astropy.units as u
# we assume m_planet << m_star
def get_planet_mass(P, K, e, star_mass=1.0):
"""
P in days, K in m/s, star_mass in solar masses
output is planet mass in Jupiter masses
"""
@j-faria
j-faria / binom_fraction.py
Created May 10, 2017 12:37
Probability distribution for fraction of X, given the total sample size N, and the number of Xs in the sample, n. Follows the Appendix of Burgasser et al. (ApJ, 586:512, 2003)
import numpy as np
from scipy.optimize import bisect
from scipy.special import binom as binom_coeff
from scipy.integrate import quad
from functools import partial
def binom_function(N, n, p):
c = binom_coeff(N, n)
return c * p**n * (1.-p)**(N-n)