RomanCuts
utils
Collection of utility functions
wrapped_spline
Creates a vector of folded-spline basis according to the input data. This is meant to be used to build the basis vectors for periodic data, like the angle in polar coordinates.
Parameters:
-
input_vector
(ndarray
) –Input data to create basis, angle values MUST BE BETWEEN -PI and PI.
-
order
(int
, default:2
) –Order of the spline basis
-
nknots
(int
, default:10
) –Number of knots for the splines
Returns:
-
folded_basis
(ndarray
) –Array of folded-spline basis
_make_A_polar
_make_A_polar(
phi: ArrayLike,
r: ArrayLike,
cut_r: float = 6,
rmin: float = 1,
rmax: float = 18,
n_r_knots: int = 12,
n_phi_knots: int = 15,
) -> spmatrix
Creates a design matrix (DM) in polar coordinates (r, phi). It will enforce r-only
dependency within cut_r
radius. This is useful when less data points are available
near the center.
Parameters:
-
phi
(ndarray
) –Array of angle (phi) values in polar coordinates. Must have values in the [-pi, pi] range.
-
r
(ndarray
) –Array of radii values in polar coordinates.
-
cut_r
(float
, default:6
) –Radius (units consistent with
r
) whitin the DM only has radius dependency and not angle. -
rmin
(float
, default:1
) –Radius where the DM starts.
-
rmax
(float
, default:18
) –Radius where the DM ends.
-
n_r_knots
(int
, default:12
) –Number of knots used for the spline in radius.
-
n_phi_knots
(int
, default:15
) –Number of knots used for the spline in angle.
Returns:
-
X
(sparse CSR matrix
) –A DM with bspline basis in polar coordinates.
_make_A_cartesian
_make_A_cartesian(
x: ArrayLike,
y: ArrayLike,
n_knots: int = 10,
radius: Optional[float] = None,
degree: int = 3,
knot_spacing_type: str = "sqrt",
) -> spmatrix
Creates a design matrix (DM) in Cartersian coordinates (r, phi).
Parameters:
-
x
(ndarray
) –Array of x values in Cartersian coordinates.
-
y
(ndarray
) –Array of y values in Cartersian coordinates.
-
n_knots
(int
, default:10
) –Number of knots used for the spline.
-
radius
(float
, default:None
) –Distance from 0 to the furthes knot.
-
knot_spacing_type
(string
, default:'sqrt'
) –Type of spacing betwen knots, options are "linear" or "sqrt".
-
degree
(int
, default:3
) –Degree of the spline, default is 3.
Returns:
-
X
(sparse CSR matrix
) –A DM with bspline basis in Cartersian coordinates.
solve_linear_model
solve_linear_model(
A: Union[ArrayLike, spmatrix],
y: ArrayLike,
y_err: Optional[ArrayLike] = None,
prior_mu: Optional[float] = None,
prior_sigma: Optional[float] = None,
k: Optional[ArrayLike] = None,
errors: bool = False,
nnls: bool = False,
) -> Union[ndarray, Tuple[ndarray, ndarray]]
Solves a linear model with design matrix A and observations y: Aw = y return the solutions w for the system assuming Gaussian priors. Alternatively the observation errors, priors, and a boolean mask for the observations (row axis) can be provided.
Adapted from Luger, Foreman-Mackey & Hogg, 2017 (https://ui.adsabs.harvard.edu/abs/2017RNAAS...1....7L/abstract)
Parameters:
-
A
(Union[ArrayLike, spmatrix]
) –Desging matrix with solution basis shape n_observations x n_basis
-
y
(ArrayLike
) –Observations shape n_observations
-
y_err
(Optional[ArrayLike]
, default:None
) –Observation errors shape n_observations
-
prior_mu
(Optional[float]
, default:None
) –Mean of Gaussian prior values for the weights (w)
-
prior_sigma
(Optional[float]
, default:None
) –Standard deviation of Gaussian prior values for the weights (w)
-
k
(Optional[ArrayLike]
, default:None
) –Mask that sets the observations to be used to solve the system shape n_observations
-
errors
(bool
, default:False
) –Whether to return error estimates of the best fitting weights
Returns:
-
w
(numpy ndarray
) –Array with the estimations for the weights shape n_basis
-
werrs
(numpy ndarray
) –Array with the error estimations for the weights, returned if
error
is True shape n_basis
bspline_smooth
bspline_smooth(
y: ArrayLike,
x: Optional[ArrayLike] = None,
degree: int = 3,
do_segments: bool = False,
breaks: Optional[List[int]] = None,
n_knots: int = 100,
) -> ndarray
Applies a spline smoothing to a curve.
Parameters:
-
y
(numpy.ndarray or list of numpy.ndarray
) –Arrays to be smoothen in the last axis
-
x
(ndarray
, default:None
) –Optional. x array, as
y = f(x)`` used to find discontinuities in
f(x). If x is given then splits will be computed, if not
breaks` argument as to be provided. -
degree
(int
, default:3
) –Degree of the spline fit, default is 3.
-
do_segments
(boolean
, default:False
) –Do the splines per segments with splits computed from data
x
or given inbreaks
. -
breaks
(list of ints
, default:None
) –List of break indexes in
y
. -
nknots
(int
) –Number of knots for the B-Spline. If
do_segments
is True, knots will be distributed in each segment.
Returns:
-
y_smooth
(ndarray
) –Smooth array.
gaussian_smooth
gaussian_smooth(
y: ArrayLike,
x: Optional[ArrayLike] = None,
do_segments: bool = False,
filter_size: int = 13,
mode: str = "mirror",
breaks: Optional[List[int]] = None,
) -> ndarray
Applies a Gaussian smoothing to a curve.
Parameters:
-
y
(numpy.ndarray or list of numpy.ndarray
) –Arrays to be smoothen in the last axis
-
x
(ndarray
, default:None
) –Time array of same shape of
y
last axis used to find data discontinuity. -
filter_size
(int
, default:13
) –Filter window size
-
mode
(str
, default:'mirror'
) –The
mode
parameter determines how the input array is extended beyond its boundaries. Options are {'reflect', 'constant', 'nearest', 'mirror', 'wrap'}. Default is 'mirror'
Returns:
-
y_smooth
(ndarray
) –Smooth array.
to_fits
to_fits(
data: dict,
path: Optional[str] = None,
overwrite: bool = False,
**extra_data: Any,
) -> HDUList
Converts the light curve to a FITS file in the Kepler/TESS file format.
The FITS file will be returned as a ~astropy.io.fits.HDUList
object.
If a path
is specified then the file will also be written to disk.
Parameters:
-
data
(dict
) –Lightcurve data, time, flux, and flux_err
-
path
(str or None
, default:None
) –Location where the FITS file will be written, which is optional.
-
overwrite
(bool
, default:False
) –Whether or not to overwrite the file, if
path
is set. -
extra_data
(dict
, default:{}
) –Extra keywords or columns to include in the FITS file. Arguments of type str, int, float, or bool will be stored as keywords in the primary header. Arguments of type np.array or list will be stored as columns in the first extension.
Returns:
-
hdu
(`~astropy.io.fits.HDUList`
) –Returns an
~astropy.io.fits.HDUList
object.
clean_blends_in_catalog
clean_blends_in_catalog(
catalog: DataFrame,
blend_limit: float,
filter: str = "F146",
) -> DataFrame
Cleans the catalog by removing sources withing blend_limit
.
Parameters:
-
catalog
(DataFrame
) –The input catalog DataFrame containing 'ra' and 'dec' columns.
-
blend_limit
(float
) –The distance limit in arcseconds to consider a source as a blend.
-
remove
(str
) –The type of sources to remove. Options are 'faint' or 'bright'.
Returns:
-
DataFrame
–A cleaned DataFrame with rows containing NaN in 'ra' or 'dec' removed.