Skip to content

Commit

Permalink
Fixing #53. Following advice from @suvayu: class DataAccessor(object,…
Browse files Browse the repository at this point in the history
… metaclass=RequiredAttributesMetaclass), i.e. DataAccessor inheriting from RequiredAttributesMetaclass is an overkill, since RequiredAttributesMetaclass does nothing more than checking for required attributes. requiredatts.py can be removed and DataAccessor can be turned into a dataclass, which will have the required arguments declared.
  • Loading branch information
HannoSpreeuw committed Aug 15, 2024
1 parent 1af7698 commit 7e58d00
Showing 1 changed file with 72 additions and 72 deletions.
144 changes: 72 additions & 72 deletions sourcefinder/accessors/dataaccessor.py
Original file line number Diff line number Diff line change
@@ -1,80 +1,80 @@
import logging
from math import degrees, sqrt, sin, pi, cos

from sourcefinder.accessors.requiredatts import RequiredAttributesMetaclass
import numpy
from math import degrees, sqrt, sin, pi, cos
from dataclasses import dataclass
from sourcefinder.utility.coordinates import WCS

logger = logging.getLogger(__name__)


class DataAccessor(object, metaclass=RequiredAttributesMetaclass):
_required_attributes = [
'beam',
'centre_ra',
'centre_decl',
'data',
'freq_bw',
'freq_eff',
'pixelsize',
'tau_time',
'taustart_ts',
'url',
'wcs',
]

def __init__(self):
# Sphinx only picks up the class docstring if it's under an __init__
# *le sigh*
"""
Base class for accessors used with
:class:`sourcefinder.image.ImageData`.
Data accessors provide a uniform way for the ImageData class (ie,
generic image representation) to access the various ways in which
images may be stored (FITS files, arrays in memory, potentially HDF5,
etc).
This class cannot be instantiated directly, but should be subclassed
and the abstract properties provided. Note that all abstract
properties are required to provide a valid accessor.
Additional properties may also be provided by subclasses. However,
TraP components are required to degrade gracefully in the absence of
this optional properties.
The required attributes are as follows:
Attributes:
beam (tuple): Restoring beam. Tuple of three floats:
semi-major axis (in pixels), semi-minor axis (pixels)
and position angle (radians).
centre_ra (float): Right ascension at the central pixel of the image.
Units of J2000 decimal degrees.
centre_decl (float): Declination at the central pixel of the image.
Units of J2000 decimal degrees.
data(numpy.ndarray): Two dimensional numpy.ndarray of floating point
pixel values.
(TODO: Definitive statement on orientation/transposing.)
freq_bw(float): The frequency bandwidth of this image in Hz.
freq_eff(float): Effective frequency of the image in Hz.
That is, the mean frequency of all the visibility data which
comprises this image.
pixelsize(tuple): (x, y) tuple representing the size of a pixel
along each axis in units of degrees.
tau_time(float): Total time on sky in seconds.
taustart_ts(float): Timestamp of the first integration which
constitutes part of this image. MJD in seconds.
url(string): A (string) URL representing the location of the image
at time of processing.
wcs(:class:`sourcefinder.utility.coordinates.WCS`): An instance of
:py:class:`sourcefinder.utility.coordinates.WCS`,
describing the mapping from data pixels to sky-coordinates.
The class also provides some common functionality:
static methods used for parsing datafiles, and an 'extract_metadata'
function which provides key info in a simple dict format.
"""

def extract_metadata(self):
@dataclass
class DataAccessor:
# Sphinx only picks up the class docstring if it's under an __init__
# *le sigh*
"""
Base class for accessors used with
:class:`sourcefinder.image.ImageData`.
Data accessors provide a uniform way for the ImageData class (ie,
generic image representation) to access the various ways in which
images may be stored (FITS files, arrays in memory, potentially HDF5,
etc).
This class cannot be instantiated directly, but should be subclassed
and the abstract properties provided. Note that all abstract
properties are required to provide a valid accessor.
Additional properties may also be provided by subclasses. However,
TraP components are required to degrade gracefully in the absence of
this optional properties.
The required attributes are as follows:
Attributes:
beam (tuple): Restoring beam. Tuple of three floats:
semi-major axis (in pixels), semi-minor axis (pixels)
and position angle (radians).
centre_ra (float): Right ascension at the central pixel of the image.
Units of J2000 decimal degrees.
centre_decl (float): Declination at the central pixel of the image.
Units of J2000 decimal degrees.
data(numpy.ndarray): Two dimensional numpy.ndarray of floating point
pixel values.
(TODO: Definitive statement on orientation/transposing.)
freq_bw(float): The frequency bandwidth of this image in Hz.
freq_eff(float): Effective frequency of the image in Hz.
That is, the mean frequency of all the visibility data which
comprises this image.
pixelsize(tuple): (x, y) tuple representing the size of a pixel
along each axis in units of degrees.
tau_time(float): Total time on sky in seconds.
taustart_ts(float): Timestamp of the first integration which
constitutes part of this image. MJD in seconds.
url(string): A (string) URL representing the location of the image
at time of processing.
wcs(:class:`sourcefinder.utility.coordinates.WCS`): An instance of
:py:class:`sourcefinder.utility.coordinates.WCS`,
describing the mapping from data pixels to sky-coordinates.
The class also provides some common functionality:
static methods used for parsing datafiles, and an 'extract_metadata'
function which provides key info in a simple dict format.
"""

beam: tuple
centre_ra: float
centre_decl: float
data: numpy.ndarray
freq_bw: float
freq_eff: float
pixelsize: tuple
tau_time: float
taustart_ts: float
url: str
wcs: WCS

def extract_metadata(self) -> dict:
"""
Massage the class attributes into a flat dictionary with
database-friendly values.
Expand Down Expand Up @@ -155,4 +155,4 @@ def degrees2pixels(bmaj, bmin, bpa, deltax, deltay):
(sin(pi * bpa / 180.) ** 2) / (deltay ** 2))
)
theta = pi * bpa / 180
return (semimaj, semimin, theta)
return semimaj, semimin, theta

0 comments on commit 7e58d00

Please sign in to comment.