Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
dec65b6
use mpi4py and write unique file names
PaulHancock Apr 18, 2024
4a37007
add module to handle MPI checking
PaulHancock Apr 18, 2024
4a1d3ea
use new mpi module
PaulHancock Apr 18, 2024
e7bce01
refactor mpi
PaulHancock Apr 18, 2024
1d0d1d6
set up barrier
PaulHancock Apr 18, 2024
a79c295
update barrier
PaulHancock Apr 18, 2024
152489b
debug message
PaulHancock Apr 18, 2024
3d463e3
update debug
PaulHancock Apr 18, 2024
7adecf7
collect all table names into a list and append the tables together.
AhmedHamadto Apr 23, 2024
279e77a
Added a function addSuffix to append suffixes to file names before th…
AhmedHamadto Apr 28, 2024
6fa42dc
Added a new exception, and raise line in addSuffix function
AhmedHamadto Apr 30, 2024
7a586ef
Created a fix for the first character being an underscore, updated ch…
AhmedHamadto Apr 30, 2024
7601a58
added mpi4py to dependencies, added logger.info("MPI is available")
AhmedHamadto Apr 30, 2024
97a9715
Updated Changelog, added unit test for addSuffix, and corrected the a…
AhmedHamadto May 2, 2024
0e4c3d4
amended the if function at the end of the test file.
AhmedHamadto May 2, 2024
20d99c1
imported the function from aegean in the test file
AhmedHamadto May 7, 2024
f9a06b2
removed the TODO comment as it has been addressed.
AhmedHamadto May 9, 2024
2cb4cba
Developer_Documentation
AhmedHamadto May 19, 2024
080d0aa
Update python-test.yml
AhmedHamadto Aug 27, 2024
848473a
made mpi an optional dependency
AhmedHamadto Aug 27, 2024
2e71bdf
Starting point
AhmedHamadto Aug 27, 2024
4d5a17e
Merge branch 'load_cube' of https://github.com/AhmedHamadto/Aegean-CI…
AhmedHamadto Aug 27, 2024
0a25c60
Commented out optional dependency
AhmedHamadto Aug 27, 2024
9e65b89
Updated numpy version
AhmedHamadto Aug 27, 2024
b5e3924
added a description of the suffix error, updated the data load functi…
AhmedHamadto Sep 3, 2024
0321fe0
Added the synthetic_with_alpha.fits file
AhmedHamadto Sep 3, 2024
84543b8
Removed a comment
AhmedHamadto Sep 3, 2024
17edf70
Ported the work over to this branch
AhmedHamadto Sep 10, 2024
d26da9f
Implementation and Unit Tests for the load_image_band function
AhmedHamadto Sep 10, 2024
ae42f5f
added the .DS_Store to the git ignore
AhmedHamadto Sep 10, 2024
fb47514
Deleted the .DS_Store file
AhmedHamadto Sep 10, 2024
2240501
Updated the git ignore file to exclude .DS_Store
AhmedHamadto Sep 10, 2024
47a433f
Added the synthetic_with_alpha.fits file
AhmedHamadto Sep 10, 2024
e8dc847
Merge pull request #2 from AhmedHamadto/load_cube
AhmedHamadto Sep 10, 2024
1c6553c
Merge branch 'dev' into load_globals
AhmedHamadto Sep 10, 2024
8b22935
Added a new class for the ComponentSource3D
AhmedHamadto Sep 17, 2024
0b7f2ae
Commented what the next steps are and what needs to be done
AhmedHamadto Sep 17, 2024
5ca9ad1
Fixed the indent and commented out the --table test in test_find()
AhmedHamadto Sep 24, 2024
7145678
Merge pull request #3 from AhmedHamadto/load_globals
AhmedHamadto Oct 1, 2024
2ceb795
Added the pix2freq and freq2pix methods
AhmedHamadto Oct 1, 2024
c8cdfef
added the CRVAL3, CDELT3, CRPIX3 as attributes and modified the metho…
AhmedHamadto Oct 1, 2024
4410f89
Comments to write tests and then pr
AhmedHamadto Oct 1, 2024
9f04650
Amended the doc string
AhmedHamadto Oct 1, 2024
8458691
added the new 3D functions that need to be modified
AhmedHamadto Oct 9, 2024
0748e84
Created the componentsource3d class
AhmedHamadto Oct 9, 2024
83659a8
amended some lines from .format to f strings, altered the param to in…
AhmedHamadto Oct 15, 2024
80e42ae
Moved formatter, and names outside of the init method.
AhmedHamadto Oct 15, 2024
53e8e97
Merge pull request #4 from AhmedHamadto/models
AhmedHamadto Oct 15, 2024
2b3cc62
Added the changes from the models branch, as this branch was forked f…
AhmedHamadto Oct 15, 2024
6e886af
Added the threeD flag and argument.
AhmedHamadto Oct 15, 2024
e1e956c
added the '--3d' flag
AhmedHamadto Oct 15, 2024
51ff4f3
Corrected the indent that caused the tests to fail
AhmedHamadto Oct 15, 2024
cc23475
removed a comment
AhmedHamadto Oct 15, 2024
f3cc9f0
Added a test for pix2freq
AhmedHamadto Oct 15, 2024
af6cf0e
Added the other test files
AhmedHamadto Oct 15, 2024
30f7cce
Completed the tests for WCS_Helper
AhmedHamadto Oct 15, 2024
cb53f8a
Merge pull request #7 from AhmedHamadto/WCS-Helper
AhmedHamadto Oct 16, 2024
481257a
Added the threeD flag and argument.
AhmedHamadto Oct 15, 2024
33ebcec
added the '--3d' flag
AhmedHamadto Oct 15, 2024
212504c
Corrected the indent that caused the tests to fail
AhmedHamadto Oct 15, 2024
b7834c5
removed a comment
AhmedHamadto Oct 15, 2024
dbec687
Merge branch 'CLI-2' of https://github.com/AhmedHamadto/Aegean-CIDS i…
AhmedHamadto Oct 16, 2024
40f7bb0
Set the dojac default value to False
AhmedHamadto Oct 22, 2024
240c61a
Set the dojac default to False
AhmedHamadto Oct 22, 2024
32d969a
undid the last commit.
AhmedHamadto Oct 22, 2024
c3b7a24
imported dm_lmfit_3D
AhmedHamadto Oct 22, 2024
9653c0d
amended the header attribute in the ComponentSource3D
AhmedHamadto Oct 22, 2024
b0de7e8
Merge branch 'dev' into Priorised_3D_fitting
AhmedHamadto Oct 22, 2024
cb62eb0
Merge pull request #8 from AhmedHamadto/Priorised_3D_fitting
AhmedHamadto Oct 22, 2024
f968562
Merge pull request #6 from AhmedHamadto/CLI-2
AhmedHamadto Oct 22, 2024
f278cdb
CHeckpoint: 3D Priorized fitting now works.
AhmedHamadto Oct 25, 2024
b9eed61
Removed a comment
AhmedHamadto Nov 9, 2024
6e87691
Pull Ahmed's dev branch
PaulHancock Dec 10, 2024
2ce8899
add test files
PaulHancock Dec 11, 2024
736f34b
remove nu/alpha from non 3d priorized fitting
PaulHancock Dec 11, 2024
5cf68a7
cleanup files properly
PaulHancock Dec 11, 2024
adde126
Merge branch 'dev' into AhmedHamadto-dev
PaulHancock Dec 11, 2024
d8ae55e
update warn->warning for logging
PaulHancock Dec 11, 2024
11a07ec
remove deprecated arg to filter_image
PaulHancock Dec 11, 2024
fcdea6e
treat all data as 3d, working on 2d images
PaulHancock Dec 16, 2024
a6efd43
correctly working wth --3d
PaulHancock Dec 17, 2024
d327e23
handle 2d/3d images as intended
PaulHancock Dec 17, 2024
6df79f0
fix error with 4d images
PaulHancock Dec 17, 2024
ffe6133
remove deprecated arg from testing
PaulHancock Dec 17, 2024
f740443
add placeholder for new test
PaulHancock Dec 17, 2024
6828e96
fix issue with loading 3d bkg/rms maps
PaulHancock Dec 17, 2024
2342115
finish test_cube_as_cube
PaulHancock Dec 17, 2024
4ea72d2
Pull changes from branch 'dev' into AhmedHamadto-dev
PaulHancock Dec 19, 2024
3310115
Merge branch 'dev' into AhmedHamadto-dev
PaulHancock Jan 14, 2025
45f99a1
update changelog with configargparse info
PaulHancock Jan 16, 2025
a2ed065
note updates to aegean/bane
PaulHancock Jan 17, 2025
a507c4f
remove unused code related to bias correction
PaulHancock Jan 17, 2025
0bfc24a
remove unused hessian code
PaulHancock Jan 17, 2025
2e16a92
allow AeRes to load sources with spectral index
PaulHancock Feb 3, 2025
4be26d1
update docstring
PaulHancock Feb 3, 2025
3f9bea5
allow AeRes to make / work with 3d data and catalogues with spectral …
PaulHancock Feb 3, 2025
7ce7e21
version/date bump
PaulHancock Feb 3, 2025
4fb6963
update test data
PaulHancock Feb 4, 2025
23272f6
add attribute has_freq which is true if CDELT3 is defined
PaulHancock Feb 4, 2025
0559944
formatting
PaulHancock Feb 4, 2025
8cfaab8
add method to create ComponentSource3D from ComponentSource
PaulHancock Feb 4, 2025
9e73483
fix bug that called wcs.pix2freq when there is no freq axis
PaulHancock Feb 4, 2025
ef63997
complete testing for 2/3d and with/without alpha
PaulHancock Feb 4, 2025
f1330e5
clean up test files
PaulHancock Feb 4, 2025
512a368
update test files
PaulHancock Feb 4, 2025
15ae200
update tests to use new files
PaulHancock Feb 4, 2025
673b0f4
Merge branch 'dev' into AhmedHamadto-dev
PaulHancock Jul 30, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ dist/
wiki/
doc/_build/
.vscode
env/*
env/*
.DS_Store
210 changes: 120 additions & 90 deletions AegeanTools/AeRes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@

from AegeanTools import catalogs, fitting, wcs_helpers
from AegeanTools.logging import logger, logging
from AegeanTools.models import ComponentSource, ComponentSource3D
from AegeanTools.fits_tools import load_image_band, write_fits

__author__ = "Paul Hancock"

FWHM2CC = 1 / (2 * np.sqrt(2 * np.log(2)))


def load_sources(filename,
ra_col='ra', dec_col='dec',
peak_col='peak_flux',
a_col='a', b_col='b', pa_col='pa'):
def load_sources(
filename,
ra_col="ra",
dec_col="dec",
peak_col="peak_flux",
a_col="a",
b_col="b",
pa_col="pa",
alpha_col="alpha",
nu0_col="nu0",
):
"""
Open a file, read contents, return a list of all the sources in that file.

Expand All @@ -32,13 +41,14 @@ def load_sources(filename,

ra_col, dec_col, peak_col, a_col, b_col, pa_col : str
The column names for each of the parameters.
Default = ['ra', 'dec', 'peak_flux', 'a', 'b', 'pa']
Default = ['ra', 'dec', 'peak_flux', 'a', 'b', 'pa', 'alpha' ,'nu0']

Return
------
catalog : [`class:AegeanTools.models.ComponentSource`, ...]
A list of source components
"""
srctype = ComponentSource
table = catalogs.load_table(filename)
required_cols = [ra_col, dec_col, peak_col, a_col, b_col, pa_col]
# required_cols = ['ra','dec','peak_flux','a','b','pa']
Expand All @@ -51,11 +61,19 @@ def load_sources(filename,
logger.error("Some required columns missing or mis-labeled")
return None
# rename the table columns
for old, new in zip([ra_col, dec_col, peak_col, a_col, b_col, pa_col],
['ra', 'dec', 'peak_flux', 'a', 'b', 'pa']):
for old, new in zip(
[ra_col, dec_col, peak_col, a_col, b_col, pa_col],
["ra", "dec", "peak_flux", "a", "b", "pa"],
):
table.rename_column(old, new)

catalog = catalogs.table_to_source_list(table)
# rename the alpha column if it exists
if alpha_col in table.colnames:
table.rename_column(alpha_col, "alpha")
table.rename_column(nu0_col, "nu0")
srctype = ComponentSource3D

catalog = catalogs.table_to_source_list(table, src_type=srctype)
logger.info("read {0} sources from {1}".format(len(catalog), filename))
return catalog

Expand All @@ -69,7 +87,7 @@ def make_model(sources, shape, wcshelper, mask=False, frac=None, sigma=4):
sources : [`class:AegeanTools.models.ComponentSource`, ...]
a list of sources

shape : [float, float]
shape : [int, int, int]
the shape of the input (and output) image

wcshelper : 'class:AegeanTools.wcs_helpers.WCSHelper'
Expand All @@ -96,79 +114,98 @@ def make_model(sources, shape, wcshelper, mask=False, frac=None, sigma=4):
factor = 5

i_count = 0
for src in sources:
xo, yo, sx, sy, theta = wcshelper.sky2pix_ellipse([src.ra, src.dec],
src.a/3600,
src.b/3600, src.pa)
phi = np.radians(theta)

# skip sources that have a center that is outside of the image
if not 0 < xo < shape[0]:
logger.debug("source {0} is not within image".format(src.island))
continue
if not 0 < yo < shape[1]:
logger.debug("source {0} is not within image".format(src.island))
continue

# pixels over which this model is calculated
xoff = factor*(abs(sx*np.cos(phi)) + abs(sy*np.sin(phi)))
xmin = xo - xoff
xmax = xo + xoff

yoff = factor*(abs(sx*np.sin(phi)) + abs(sy*np.cos(phi)))
ymin = yo - yoff
ymax = yo + yoff

# clip to the image size
ymin = max(np.floor(ymin), 0)
ymax = min(np.ceil(ymax), shape[1])

xmin = max(np.floor(xmin), 0)
xmax = min(np.ceil(xmax), shape[0])

if not np.all(np.isfinite([ymin, ymax, xmin, xmax])):
continue

if logger.isEnabledFor(logging.DEBUG): # pragma: no cover
logger.debug("Source ({0},{1})".format(src.island, src.source))
logger.debug(" xo, yo: {0} {1}".format(xo, yo))
logger.debug(" sx, sy: {0} {1}".format(sx, sy))
logger.debug(" theta, phi: {0} {1}".format(theta, phi))
logger.debug(" xoff, yoff: {0} {1}".format(xoff, yoff))
logger.debug(" xmin, xmax, ymin, ymax: {0}:{1} {2}:{3}".format(
xmin, xmax, ymin, ymax))
logger.debug(" flux, sx, sy: {0} {1} {2}".format(
src.peak_flux, sx, sy))

# positions for which we want to make the model
x, y = np.mgrid[int(xmin):int(xmax), int(ymin):int(ymax)]
x = x.ravel()
y = y.ravel()

# TODO: understand why xo/yo -1 is needed
model = fitting.elliptical_gaussian(x, y, src.peak_flux, xo-1, yo-1,
sx*FWHM2CC, sy*FWHM2CC, theta)

# Mask the output image if requested
if mask:
if frac is not None:
indices = np.where(model >= (frac*src.peak_flux))
for s in range(shape[0]):
for src in sources:
xo, yo, sx, sy, theta = wcshelper.sky2pix_ellipse(
[src.ra, src.dec], src.a / 3600, src.b / 3600, src.pa
)
phi = np.radians(theta)

# skip sources that have a center that is outside of the image
if not 0 < xo < shape[1]:
logger.debug("source {0} is not within image".format(src.island))
continue
if not 0 < yo < shape[2]:
logger.debug("source {0} is not within image".format(src.island))
continue

# pixels over which this model is calculated
xoff = factor * (abs(sx * np.cos(phi)) + abs(sy * np.sin(phi)))
xmin = xo - xoff
xmax = xo + xoff

yoff = factor * (abs(sx * np.sin(phi)) + abs(sy * np.cos(phi)))
ymin = yo - yoff
ymax = yo + yoff

# clip to the image size
ymin = max(np.floor(ymin), 0)
ymax = min(np.ceil(ymax), shape[2])

xmin = max(np.floor(xmin), 0)
xmax = min(np.ceil(xmax), shape[1])

if not np.all(np.isfinite([ymin, ymax, xmin, xmax])):
continue

# Compute the peak flux at this frequency
peak_flux = src.peak_flux
if hasattr(src, "nu0") and wcshelper.has_freq:
peak_flux *= (wcshelper.pix2freq(s) / src.nu0) ** src.alpha

if logger.isEnabledFor(logging.DEBUG): # pragma: no cover
logger.debug("Source ({0},{1})".format(src.island, src.source))
logger.debug(" xo, yo: {0} {1}".format(xo, yo))
logger.debug(" sx, sy: {0} {1}".format(sx, sy))
logger.debug(" theta, phi: {0} {1}".format(theta, phi))
logger.debug(" xoff, yoff: {0} {1}".format(xoff, yoff))
logger.debug(
" xmin, xmax, ymin, ymax: {0}:{1} {2}:{3}".format(
xmin, xmax, ymin, ymax
)
)
logger.debug(" flux, sx, sy: {0} {1} {2}".format(peak_flux, sx, sy))

# positions for which we want to make the model
x, y = np.mgrid[int(xmin) : int(xmax), int(ymin) : int(ymax)]
x = x.ravel()
y = y.ravel()

# TODO: understand why xo/yo -1 is needed
model = fitting.elliptical_gaussian(
x, y, src.peak_flux, xo - 1, yo - 1, sx * FWHM2CC, sy * FWHM2CC, theta
)

# Mask the output image if requested
if mask:
if frac is not None:
indices = np.where(model >= (frac * peak_flux))
else:
indices = np.where(model >= (sigma * src.local_rms))
# somehow m[x,y][indices] = np.nan doesn't assign any values
# so we have to do the more complicated
# m[x[indices],y[indices]] = np.nan
m[s, x[indices], y[indices]] = np.nan
else:
indices = np.where(model >= (sigma*src.local_rms))
# somehow m[x,y][indices] = np.nan doesn't assign any values
# so we have to do the more complicated
# m[x[indices],y[indices]] = np.nan
m[x[indices], y[indices]] = np.nan
else:
m[x, y] += model
i_count += 1
m[s, x, y] += model

if s == 0:
i_count += 1
logger.info("modeled {0} sources".format(i_count))
return m


def make_residual(fitsfile, catalog, rfile, mfile=None,
add=False, mask=False, frac=None, sigma=4,
colmap=None):
def make_residual(
fitsfile,
catalog,
rfile,
mfile=None,
add=False,
mask=False,
frac=None,
sigma=4,
colmap=None,
):
"""
Take an input image and catalogue, make a model of the catalogue, and then
add/subtract or mask the input image. Saving the residual and (optionally)
Expand Down Expand Up @@ -205,7 +242,7 @@ def make_residual(fitsfile, catalog, rfile, mfile=None,

colmap : dict
A mapping of column names. Default is: {'ra_col':'ra', 'dec_col':'dec',
'peak_col':'peak_flux', 'a_col':'a', 'b_col':'b', 'pa_col':'pa}
'peak_col':'peak_flux', 'a_col':'a', 'b_col':'b', 'pa_col':'pa, 'alpha_col':'alpha'}

Return
------
Expand All @@ -219,12 +256,8 @@ def make_residual(fitsfile, catalog, rfile, mfile=None,

if source_list is None:
return None
# force two axes so that we dump redundant stokes/freq axes if they are
# present.
hdulist = fits.open(fitsfile, naxis=2)
# ignore dimensions of length 1
data = np.squeeze(hdulist[0].data)
header = hdulist[0].header

data, header = load_image_band(fitsfile, as_cube=True)

wcshelper = wcs_helpers.WCSHelper.from_header(header)

Expand All @@ -235,11 +268,8 @@ def make_residual(fitsfile, catalog, rfile, mfile=None,
else:
residual = data - model

hdulist[0].data = residual
hdulist.writeto(rfile, overwrite=True)
logger.info("wrote residual to {0}".format(rfile))
write_fits(residual, header, rfile)
if mfile is not None:
hdulist[0].data = model
hdulist.writeto(mfile, overwrite=True)
logger.info("wrote model to {0}".format(mfile))
write_fits(model, header, mfile)

return
Loading