Skip to content

Instantly share code, notes, and snippets.

View andycasey's full-sized avatar

Andy Casey andycasey

View GitHub Profile
https://monash.zoom.us/my/andy.casey?pwd=U3JHOThFYzEzbXVMbWs5eGFmV1F2UT09
@andycasey
andycasey / asac_orcids.py
Created February 14, 2024 03:05
Get ORCIDs of Australian astronomers who have written a refereed paper that includes the term "Square Kilometer Array"
# This uses `ads` v1 (nightly).
# Running this reveals 533 unique astronomers and their ORCIDs,
# but there are more: I limited the query to 1000 ADS entries.
from ads import Document
from tqdm import tqdm
import json
docs = (
# Example to sample from the observed quantities and run each sample through StarFit
# In this example we will say that we have the following abundances:
# [C/Fe] = -1.1 +/- 0.2
# [N/Fe] = -0.5 +/- 0.1
# [Mg/Fe] < 0.3
# Whenever you're generating random numbers, you should always "seed" the random number generator.
# That way you'll still get "random"-esque numbers, but if you run your code twice you will get the same "random" numbers.
# If the numbers were really random each time you ran the code then it would be difficult to debug things.
@andycasey
andycasey / gist:c28d73ca3f51ae26c787dd8957cc58b6
Created January 19, 2024 01:00
Create fake mwmVisit/mwmStar files
import numpy as np
from astropy.io import fits
def generate_apogee_hdu(observatory="APO", with_wl=True):
wl = (10**(4.179 + 6e-6 * np.arange(8575))).reshape((1, -1))
flux = np.zeros_like(wl)
ivar = np.zeros_like(wl)
pixel_flags = np.zeros_like(wl)
@andycasey
andycasey / the_cannon.py
Created September 27, 2023 00:36
The Cannon, using coordinate descent for training and a more efficient s2 calculation
import os
import numpy as np
import pickle
import warnings
from itertools import cycle
from functools import cached_property
from scipy import optimize as op
from sklearn.linear_model import Lasso, LinearRegression
from joblib import Parallel, delayed
from time import time
cartons = [
SimplifiedCarton.create(id=5, carton="star"),
SimplifiedCarton.create(id=8, carton="galaxy")
]
source = Source.create(ra=0, dec=0)
source.carton_flags.set_bit(5) # add to star
source.carton_flags.set_bit(8) # add to galaxy
print(source.carton_flags)
@andycasey
andycasey / target_bitfield_flags.py
Last active August 13, 2023 03:58
This demonstrates a way to use bitfield flags to store all targeting information for SDSS-V
"""
This minimum reproducible example demonstrates a way to use bitfield flags to store the cartons
that a source is assigned to.
What does it do?
----------------
1. Creates a `Source` table and inserts 10,000 random sources.
2. Creates some `SimplifiedCarton` entries based on unique names currently in targetdb.
3. Assigns sources to random cartons (many more than what would exist in reality).
from __future__ import annotations
import numpy as np
import warnings
from sklearn.decomposition._nmf import non_negative_factorization, _fit_multiplicative_update
from sklearn.exceptions import ConvergenceWarning
from astropy.nddata import InverseVariance
from typing import Optional, Union, Tuple, List
from specutils import SpectralAxis, Spectrum1D
@andycasey
andycasey / di_cook.py
Created January 15, 2023 00:54
SDSS DR17 subset for Di Cook
from astropy.table import Table
data = Table.read("/uufs/chpc.utah.edu/common/home/sdss50/dr17/apogee/spectro/aspcap/dr17/synspec_rev1/allStar-dr17-synspec_rev1.fits")
column_names = data.dtype.names
ignore = ["TIII_FE"]
available_elements = [ea for ea in column_names if f"{ea}_FLAG" in column_names and ea not in ignore]
keep = (
@andycasey
andycasey / find_apogee_for_froe.py
Created November 11, 2022 14:50
APOGEE 2D frames for FROE
# /uufs/chpc.utah.edu/common/home/sdss43/dr17/apogee/spectro/redux/dr17/exposures/apogee-n/57282
# or
# https://dr17.sdss.org/sas/dr17/apogee/spectro/redux/dr17/exposures/apogee-n/57282/
from glob import glob
from astropy.io import fits
# Just use the middle chip for now
paths = glob("ap2D-b-*.fits")