Skip to content

Instantly share code, notes, and snippets.

View nden's full-sized avatar

Nadia Dencheva nden

  • Space Telescope Science Institute
View GitHub Profile
@nden
nden / models_TODO_list
Last active December 16, 2015 05:09
Proposed new features in astropy.models
This page lists features to be added to astropy.models. It was compiled from suggestions in the initial astropy.models PR#493. Feel free to add to this list. Please make a note if you are interested in working on any of them. This list is not prioritized.
* Add spline models and fitters.
* Write a general `Model.inverse()` method.
This is intended to be used in models for which an analytical inverse is not available.
* Add a `CompositeModel.simplify` method, similar to AST.
@nden
nden / newfitter.py
Last active September 5, 2018 18:56
Example of creating a new fitter
from astropy.models.fitting import Fitter
import numpy as np
from scipy import optimize
class SLSQPFitter(Fitter):
def __init__(self):
super().__init__(optimizer=SLSQP, statistic=leastsquare)
def errorfunc(self, fps, *args):
model = args[0]
@nden
nden / gist:6808601
Created October 3, 2013 11:51
gaussian2d_example.py
from astropy.modeling import models, fitting
import numpy as np
from scipy.optimize import leastsq
def gaussian2d(p, x, y):
amplitude, x_mean, y_mean, x_stddev, y_stddev, theta = p[:]
a = 0.5 * ((np.cos(theta) / x_stddev) ** 2 + (np.sin(theta) / y_stddev) ** 2)
b = 0.5 * (np.cos(theta) * np.sin(theta) * (1. / x_stddev ** 2 - 1. / y_stddev ** 2))
c = 0.5 * ((np.sin(theta) / x_stddev) ** 2 + (np.cos(theta) / y_stddev) ** 2)
return amplitude * np.exp(-a * (x - x_mean) ** 2 - b * (x - x_mean) * (y - y_mean) - c * (y - y_mean) ** 2)
@nden
nden / gist:7028218
Last active December 25, 2015 19:29
subclass_model
from astropy.modeling.core import Model
from astropy.modeling.parameters import Parameter
class MyModel1(Model):
param_names = ['a']
def __init__(self, a):
self._a = Parameter('a', a, self, 1)
@nden
nden / line_fitter.py
Last active August 29, 2015 13:56
Fitting a straight line example
import numpy as np
from astropy.modeling.fitting import (_validate_model, _fitter_to_model_params, Fitter, _convert_input)
from astropy.modeling.optimizers import *
def chi_line(measured_vals, updated_model, x_sigma, y_sigma, x):
"""
Chi^2 statistic for fitting a straight line with uncertainties in x and y.
Parameters
----------
@nden
nden / sip-transformations.py
Last active August 29, 2015 14:00
SIP transformations
'''
Definitions:
SIP forward transformation:
'''
foc_X = (px_x - crpix1) + Forward_SIP_X((px_x - crpix1), (px_y - crpix2))
foc_Y = (px_y - crpix2) + Forward_SIP_Y((px_x - crpix1), (px_y - crpix2))
# SIP inverse transformation:
@nden
nden / hst_wcs.py
Created July 26, 2016 17:15
simulating HST WCS
# Simulate WFC3 WCS
import numpy as np
from astropy.io import fits
from stwcs import updatewcs
primary_header_kw = {'PA_V3': 81.872513, 'instrume': 'WFC3', 'detector': 'IR',
'idctab': 'w3m18525i_idc.fits', 'filter': 'F160W',
'telescop': 'HST', 'date-obs': '2009-08-07'}
sci_header_kw = {'CRPIX1': 562, 'CRPIX2': 562,
@nden
nden / par_models.py
Created August 8, 2017 13:16
parameters can be models
from astropy.modeling.core import Model
from astropy.modeling.models import Polynomial2D, Polynomial1D
from astropy.modeling.parameters import Parameter
class MyModel(Model):
inputs = ('x', 'y', 't')
outputs = ('z',)
@nden
nden / memory_comparison.md
Last active November 28, 2017 14:20
memory_comparison_modeling

Script for memory profiling astropy.modeling

from astropy.modeling.models import Mapping, Tabular1D
from astropy.modeling.functional_models import Shift, Scale
from astropy.modeling.polynomial import Polynomial2D
import gc
from memory_profiler import profile
import numpy as np
@nden
nden / image_wcs.py
Last active November 30, 2017 18:55
Imaging WCS
import numpy as np
from astropy import wcs
from gwcs import wcs as gw
from astropy.modeling.models import Shift, Scale, AffineTransformation2D, RotateNative2Celestial, Pix2Sky_TAN
crpix1 = 512.5
crpix2 = 512.5
cdelt1 = 2.402792
cdelt2 = 2.402792
ctype1 = 'RA---TAN'