Skip to content

Commit

Permalink
Cleanup w/ help from mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
suvayu committed Jul 14, 2024
1 parent 1de96f0 commit 0c866db
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 103 deletions.
60 changes: 16 additions & 44 deletions sourcefinder/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@
from sourcefinder import stats
from sourcefinder import utils
from sourcefinder.utility import containers
from sourcefinder.utility.memoize import Memoize

import time
import dask.array as da
from scipy.interpolate import interp1d
import psutil
from multiprocessing import Pool
from functools import cached_property
from functools import partial

try:
Expand Down Expand Up @@ -95,53 +94,31 @@ def __init__(self, data, beam, wcs, margin=0, radius=0, back_size_x=32,
# Properties and attributes. #
# #
# Properties are attributes managed by methods; rather than calling the #
# method directly, the attribute automatically invokes it. We can use #
# this to do cunning transparent caching ("memoizing") etc; see the #
# Memoize class. #
# method directly, the attribute automatically invokes it. Result of the #
# function calls are cached, subsequent calls doesn't recompute. #
# #
# clearcache() clears all the memoized data, which can get quite large. #
# clearcache() clears all the cached data, which can get quite large. #
# It may be wise to call this, for example, in an exception handler #
# dealing with MemoryErrors. #
# #
###########################################################################
@Memoize
def _grids(self):
@cached_property
def grids(self):
"""Gridded RMS and background data for interpolating"""
return self.__grids()

grids = property(fget=_grids, fdel=_grids.delete)

@Memoize
def _backmap(self):
@cached_property
def backmap(self):
"""Background map"""
if not hasattr(self, "_user_backmap"):
return self._interpolate(self.grids['bg'])
else:
return self._user_backmap

def _set_backmap(self, bgmap):
self._user_backmap = bgmap
del (self.backmap)
del (self.data_bgsubbed)

backmap = property(fget=_backmap, fdel=_backmap.delete, fset=_set_backmap)
return self._interpolate(self.grids['bg'])

@Memoize
def _get_rm(self):
@cached_property
def rmsmap(self):
"""RMS map"""
if not hasattr(self, "_user_noisemap"):
return self._interpolate(self.grids['rms'], roundup=True)
else:
return self._user_noisemap

def _set_rm(self, noisemap):
self._user_noisemap = noisemap
del (self.rmsmap)
return self._interpolate(self.grids['rms'], roundup=True)

rmsmap = property(fget=_get_rm, fdel=_get_rm.delete, fset=_set_rm)

@Memoize
def _get_data(self):
@cached_property
def data(self):
"""Masked image data"""
# We will ignore all the data which is masked for the rest of the
# sourcefinding process. We build up the mask by stacking ("or-ing
Expand All @@ -161,16 +138,11 @@ def _get_data(self):
mask = numpy.logical_or(mask, numpy.isnan(self.rawdata))
return numpy.ma.array(self.rawdata, mask=mask)

data = property(fget=_get_data, fdel=_get_data.delete)

@Memoize
def _get_data_bgsubbed(self):
@cached_property
def data_bgsubbed(self):
"""Background subtracted masked image data"""
return self.data - self.backmap

data_bgsubbed = property(fget=_get_data_bgsubbed,
fdel=_get_data_bgsubbed.delete)

@property
def xdim(self):
"""X pixel dimension of (unmasked) data"""
Expand Down
16 changes: 0 additions & 16 deletions sourcefinder/testutil/__init__.py
Original file line number Diff line number Diff line change
@@ -1,16 +0,0 @@
"""
Utility routines to help in the construction of test cases. Not required for
core pipeline functionality.
"""
import contextlib
import sys

@contextlib.contextmanager
def nostderr():
savestderr = sys.stderr
class Devnull(object):
def write(self, _):
pass
sys.stderr = Devnull()
yield
sys.stderr = savestderr
8 changes: 3 additions & 5 deletions sourcefinder/utility/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def eq_to_cart(ra, dec):
math.sin(math.radians(dec))) # Cartesian z


class CoordSystem(object):
class CoordSystem:
"""A container for constant strings representing different coordinate
systems."""
FK4 = "B1950 (FK4)"
Expand Down Expand Up @@ -624,7 +624,7 @@ def convert_coordsystem(ra, dec, insys, outsys):
return ra, dec


class WCS(object):
class WCS:
"""
Wrapper around pywcs.WCS.
Expand Down Expand Up @@ -657,13 +657,11 @@ def __setattr__(self, attrname, value):
value = (value[0], value[1] * (1 - sys.float_info.epsilon))
self.wcs.wcs.__setattr__(attrname, value)
else:
super(WCS, self).__setattr__(attrname, value)
super().__setattr__(attrname, value)

def __getattr__(self, attrname):
if attrname in self.WCS_ATTRS:
return getattr(self.wcs.wcs, attrname)
else:
super(WCS, self).__getattr__(attrname)

def p2s(self, pixpos):
"""
Expand Down
37 changes: 0 additions & 37 deletions sourcefinder/utility/memoize.py

This file was deleted.

2 changes: 1 addition & 1 deletion sourcefinder/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def get_error_radius(wcs, x_value, x_error, y_value, y_error):
to the major/minor axes of the elliptical fit, but this should do for
now.
"""
error_radius = 0
error_radius = 0.0
try:
centre_ra, centre_dec = wcs.p2s([x_value, y_value])
# We check all possible combinations in case we have a nonlinear
Expand Down

0 comments on commit 0c866db

Please sign in to comment.