Skip to content

Instantly share code, notes, and snippets.

# Abbreviated way to create groups of (nearly) identical values.
import numpy as np
# Random example data.
a = np.array([11.1, 1, 1, 2, 3, 4, 5, 5, 1, 5, 5, 6, 6, 7, 9, 7, 8, 7, 10, 11.1, 11.2])
EPS = 1e-7 # matching precision
asorted = np.sort(a) # line up the values neatly, but don't alter
# Get all indices where a change in value occurs
indices = np.where(np.abs(np.diff(asorted)) > EPS)[0] + 1
# Group identical values
@evertrol
evertrol / setup.py
Last active August 29, 2015 14:01
Testing NumPy with Cython
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
import numpy
ext_modules = cythonize([
Extension("testnp",
["testnp.pyx"],
include_dirs=[numpy.get_include()],
@evertrol
evertrol / gist:47325e62f8caae1b72ee
Last active February 16, 2021 13:16
Asyncio: cancel a set of coroutines from another coroutine, by stopping the event loop
"""Example to cancel a set of asyncio coroutines (futures),
using one coroutine to signal the event loop to stop.
"""
import asyncio
import logging
from datetime import datetime
from concurrent.futures import CancelledError
@evertrol
evertrol / apache-modwsgi-django-astropy.md
Last active September 1, 2016 23:01
Apache, Django, mod_wsgi and Astropy, oh my

Apache, Django, mod_wsgi and Astropy, oh my

This post provides a description of setting Astropy in a Django project that is run with Apache and mod_wsgi. I ran into enough issues that I decided to write them down for my future self.

For the GOTO project, I'm running the webpage as a Django project in an Apache server through mod_wsgi. This is overkill for the few, rather static, pages currently in use, but it is done somehwat in

Installation of the astronomical photometry packages.

This describes the installation of photometry pipeline external dependencies, starting from a minimal CentOS 7.1 (1503) system. (Depending on the selected ISO image, you may want to select 'minimal'.)

Note that the minimal installations comes without a default (X) window manager, nor will it have a network connection.

@evertrol
evertrol / astropy-table-aggregate
Last active January 20, 2016 04:48
astropy.table.Table: aggregrate over & combine multiple columns
from astropy.table import Table
def average(col):
# Manipulate multiple columns at once?
return col.mean()
def average_pd(df):
weight = df['weight']
total = weight.sum()
df['value'] *= weight / total
@evertrol
evertrol / argparse-like-git.py
Last active January 27, 2016 11:33
Python argparse -h/--help like git
"""The doc string
Very lengthy documentation
"""
import argparse
import pydoc
@evertrol
evertrol / pdfpages_oo.py
Last active February 10, 2016 03:49 — forked from marcelm/pdfpages_oo.py
Plot multiple figures into a single PDF with matplotlib, using the object-oriented interface
"""
Plot multiple figures into a single PDF with matplotlib, using the
object-oriented interface.
"""
from matplotlib.backends.backend_pdf import FigureCanvasPdf, PdfPages
from matplotlib.figure import Figure
import numpy as np
with PdfPages('multi.pdf') as pages:
for i in range(10):
import sys
import numpy as np
import healpy as hp
from astropy.io import fits
from astropy.wcs import WCS
from astropy.coordinates import SkyCoord
from astropy import units
from spherical_geometry.polygon import SphericalPolygon

Astrometry gone wrong

SExtractor (source extractor) is a convenient tool to run on a FITS image and return a bunch of detected sources, with their fluxes and magnitudes. Unfortunately, being written quite a while ago, it hasn't fully kept up-to-date with developments in astrometry, in particular the introduction of Simple Imaging Polynomial (SIP) corrections. SExtractor uses an older style WCS correction, which has become abandoned over the years, not least because of the avaibility of Astrometry.net (which uses SIP corrections).