Fields¶
Custom Peewee field types used in astra’s database models (astra.fields). All standard field types
(e.g. FloatField, IntegerField, TextField) include a GlossaryFieldMixin that
automatically sets help_text from the glossary if not explicitly provided.
GlossaryFieldMixin¶
A mixin class applied to all astra field types. When a field is bound to a model, if no
help_text has been set, it automatically looks up the field name in Glossary and uses
the matching description.
BitField¶
class BitField(*args, **kwargs)
A binary bit-field (extends Peewee’s BitField) that supports per-flag help_text.
Defaults to 0. Each flag is a boolean descriptor on the model instance.
BitField.flag¶
def flag(value=None, help_text=None) -> FlagDescriptor
Register a new flag in the bit field.
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
The bit value. If |
|
|
|
Description of the flag. If |
Returns: A FlagDescriptor that acts as a boolean property on model instances.
Example:
from astra.fields import BitField
class MyModel(BaseModel):
result_flags = BitField(default=0)
flag_bad_fit = result_flags.flag(help_text="Fit did not converge")
flag_low_snr = result_flags.flag(help_text="Signal-to-noise ratio below threshold")
# Usage:
instance = MyModel()
instance.flag_bad_fit = True
print(instance.flag_bad_fit) # True
The FlagDescriptor also supports SQL expressions for queries:
MyModel.select().where(MyModel.flag_bad_fit)
PixelArray¶
class PixelArray(
ext=None,
column_name=None,
transform=None,
accessor_class=PixelArrayAccessorFITS,
help_text=None,
accessor_kwargs=None,
**kwargs
)
A virtual Peewee field for array data (e.g. spectra, wavelengths) stored outside the database in FITS, HDF5, or pickle files. The data is loaded lazily on first access.
Parameters:
Parameter |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
The FITS extension (or HDF5 dataset path). Can be a callable that takes the model instance and returns the extension. |
|
|
|
Column name within the extension. If |
|
callable or |
|
A function to transform the data after loading. For FITS accessors it receives |
|
class |
|
The accessor class to use. Options: |
|
|
|
Description of the field. |
|
|
|
Extra keyword arguments passed to the accessor constructor. |
Example:
from astra.fields import PixelArray, PixelArrayAccessorFITS
class BossVisitSpectrum(BaseModel):
wavelength = PixelArray(ext=1, column_name="loglam", transform=lambda x, *_: 10**x)
flux = PixelArray(ext=1, column_name="flux")
ivar = PixelArray(ext=1, column_name="ivar")
# Data is loaded lazily:
spectrum = BossVisitSpectrum.get_by_id(1)
print(spectrum.flux) # numpy array loaded from FITS on first access
BasePixelArrayAccessor¶
class BasePixelArrayAccessor(model, field, name, ext, column_name, transform=None, help_text=None, **kwargs)
Base class for all pixel array accessors. Manages a per-instance __pixel_data__ cache
dictionary. Subclasses implement __get__ to load data from their respective file formats.
Built-in accessor subclasses:
Class |
Description |
|---|---|
|
Loads arrays from FITS files using |
|
Loads arrays from HDF5 files using |
|
Loads arrays from Python pickle files. |
LogLambdaArrayAccessor¶
class LogLambdaArrayAccessor(model, field, name, ext, column_name, crval, cdelt, naxis, transform=None, help_text=None)
A pixel array accessor that generates a log-linear wavelength array from header parameters, rather than reading it from a file. The wavelength grid is computed as:
wavelength = 10 ** (crval + cdelt * np.arange(naxis))
Additional parameters (beyond BasePixelArrayAccessor):
Parameter |
Type |
Description |
|---|---|---|
|
|
Log10 of the reference wavelength. |
|
|
Log10 wavelength step per pixel. |
|
|
Number of pixels. |
Example:
from astra.fields import PixelArray, LogLambdaArrayAccessor
class ApogeeVisitSpectrum(BaseModel):
wavelength = PixelArray(
accessor_class=LogLambdaArrayAccessor,
accessor_kwargs=dict(crval=4.179, cdelt=6e-6, naxis=8575)
)