Skip to content

Commit

Permalink
debug statements semi-restored, and catch the case where scubes are d…
Browse files Browse the repository at this point in the history
…ropped
  • Loading branch information
keflavich committed Jan 26, 2025
1 parent 5e308c7 commit 938c016
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
26 changes: 15 additions & 11 deletions spectral_cube/cube_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1207,7 +1207,7 @@ def two_closest_channels(cube, channel):
# reversed spectral axes still break things
# and we want two channels width, not one (which is why we use +1 here)
chans = [(ch1, ch2+1) if ch1 < ch2 else (ch2, ch1+1) for ch1, ch2 in chans]
#print(f'chans={chans}') # DEBUG
log.debug(f'chans={chans}')

if not using_pbar:
log_(f"Using neighboring channels {chans}")
Expand Down Expand Up @@ -1262,13 +1262,17 @@ def two_closest_channels(cube, channel):
raise ValueError(f"There were {len(keep)-sum(keep)} dropped cubes and fail_if_cube_dropped was set. Indices: {dropped_indices}")
scubes = [cube for cube, kp in zip(scubes, keep) if kp]

if len(scubes) == 0:
log.warn(f"No cubes overlap with channel {channel}, skipping")
continue

if weightcubes is not None:
if verbose:
print("Handling weight cubes")
print(f"Handling {len(weightcubes)} weight cubes")
# convert mincube_slices to weightcube coordinates
mincube_weight_slices = []
# need to use cubes, not scubes, because mincube_slices are in cube units
for slc, wtc, cube, scube in zip(mincube_slices, weightcubes, cubes, scubes):
for slc, wtc, cube in zip(mincube_slices, weightcubes, cubes):
ycrds = slc[1].start, slc[1].stop
xcrds = slc[2].start, slc[2].stop
if ycrds[0] is None or xcrds[0] is None:
Expand All @@ -1287,22 +1291,22 @@ def two_closest_channels(cube, channel):
warnings.warn("Cube is larger than weight cube")
assert wtxcrds[1] > 0
assert wtycrds[1] > 0
#print(f"skycrds={skycrds}") # DEBUG
#print(f"Cube slices went from x={xcrds} to {wtxcrds} and y={ycrds} to {wtycrds}") # DEBUG
#print("pixel scales: ", cube.wcs.celestial.proj_plane_pixel_area()**0.5, wtc.wcs.celestial.proj_plane_pixel_area()**0.5,)
log.debug(f"skycrds={skycrds}") # DEBUG
log.debug(f"Cube slices went from x={xcrds} to {wtxcrds} and y={ycrds} to {wtycrds}") # DEBUG
log.debug("pixel scales: ", cube.wcs.celestial.proj_plane_pixel_area()**0.5, wtc.wcs.celestial.proj_plane_pixel_area()**0.5,)

# handle spectral cutting. for cubes, we split this into min_cube_slices + chans,
# but here we're doing it all at once
ch1, ch2 = two_closest_channels(wtc, channel)
ch1, ch2 = (ch1, ch2+1) if ch1 < ch2 else (ch2, ch1+1)
#print(f'wtchans={ch1, ch2}') # DEBUG
log.debug(f'wtchans={ch1, ch2}') # DEBUG
zslc = slice(ch1, ch2)

wtslc = zslc, slice(wtycrds[0], wtycrds[1]), slice(wtxcrds[0], wtxcrds[1])
mincube_weight_slices.append(wtslc)
#print(f"mincube_slices = {mincube_slices}") # DEBUG
#print(f"mincube_weight_slices = {mincube_weight_slices}") # DEBUG
#print(f"weightcube shapes: {[wtcube.shape for wtcube in weightcubes]}") # DEBUG
log.debug(f"mincube_slices = {mincube_slices}") # DEBUG
log.debug(f"mincube_weight_slices = {mincube_weight_slices}") # DEBUG
log.debug(f"weightcube shapes: {[wtcube.shape for wtcube in weightcubes]}") # DEBUG

sweightcubes = [wtcube[slices]
for slices, wtcube, kp
Expand All @@ -1329,7 +1333,7 @@ def two_closest_channels(cube, channel):
# (this version is capable of parallelizing over many cubes, in
# theory; the previous would treat each cube in serial)
print("Loading data into memory (this step should trigger convolution for dask arrays)", flush=True)
print(f"Chunk sizes: {[cube._data.chunksize if hasattr(cube._data, 'chunksize') else 'n/a' for cube in scubes]}", flush=True)
log_(f"Chunk sizes: {[cube._data.chunksize if hasattr(cube._data, 'chunksize') else 'n/a' for cube in scubes]}")
datas = [cube._get_filled_data() for cube in scubes]
wcses = [cube.wcs for cube in scubes]

Expand Down
14 changes: 12 additions & 2 deletions spectral_cube/tests/test_regrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,9 +628,10 @@ def test_mosaic_cubes(use_memmap, data_adv, use_dask, spectral_block_size):
assert part2.wcs.wcs.restwav != 0

# Mosaic give the expected header.
result = mosaic_cubes([part1, part2], order='nearest-neighbor',
result = mosaic_cubes([part1, part2],
# order is not a mosaic_cubes argument order='nearest-neighbor',
target_header=cube.header,
roundtrip_coords=False,
# not used roundtrip_coords=False,
spectral_block_size=spectral_block_size,
save_to_tmp_dir=False,
verbose=False,
Expand Down Expand Up @@ -834,6 +835,15 @@ def test_cube_mosaic_weighted():
# so I can do some sanity checks, i.e. that cube1[0] has same velocity as target_header[0]
target_header = combine_headers(cube1.header, cube2.header)

# go super verbose (for debugging)
# (saved so I can do it later)
# from logging import getLogger
# import logging
# logger = getLogger('reproject.mosaicking.coadd')
# logger.setLevel(logging.INFO)
# handler = logging.StreamHandler()
# logger.addHandler(handler)

result = mosaic_cubes([cube1, cube2],
weightcubes=[weights1, weights2],
target_header=None,
Expand Down
1 change: 1 addition & 0 deletions spectral_cube/tests/test_spectral_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,7 @@ def test_twod_numpy_twoaxes(func, how, axis, filename, use_dask):
spec = getattr(cube, func)(axis=axis, how=how)

if func == 'mean' and axis != (1,2):
print(func, how, axis, filename, use_dask) # pytest is reporting weird things, this is a hack
assert 'Averaging over a spatial and a spectral' in str(wrn[-1].message)

# data has a redundant 1st axis
Expand Down

0 comments on commit 938c016

Please sign in to comment.