-
Notifications
You must be signed in to change notification settings - Fork 60
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add an experimental "homebrew" CASA .image reader #607
Changes from 2 commits
0300b8b
702a053
c3a4faf
d43c160
770714d
6e4908c
e386235
09b43c1
b55d3a7
85fae67
171b263
d525792
5221336
1e1a1e7
ebb5ebc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,16 +8,17 @@ | |
from astropy import units as u | ||
|
||
from ..io.casa_masks import make_casa_mask | ||
from ..io.casa_image import wcs_casa2astropy | ||
from ..io.casa_image import wcs_casa2astropy, casa_image_array_reader | ||
from .. import SpectralCube, StokesSpectralCube, BooleanArrayMask, VaryingResolutionSpectralCube | ||
from . import path | ||
|
||
try: | ||
import casatools | ||
from casatools import image | ||
casaOK = True | ||
except ImportError: | ||
try: | ||
from taskinit import ia | ||
from taskinit import ia as image | ||
casaOK = True | ||
except ImportError: | ||
print("Run in CASA environment.") | ||
|
@@ -33,7 +34,7 @@ def make_casa_testimage(infile, outname): | |
raise Exception("Attempted to make a CASA test image in a non-CASA " | ||
"environment") | ||
|
||
ia = casatools.image() | ||
ia = image() | ||
|
||
ia.fromfits(infile=infile, outfile=outname, overwrite=True) | ||
ia.unlock() | ||
|
@@ -63,6 +64,16 @@ def make_casa_testimage(infile, outname): | |
ia.done() | ||
|
||
|
||
def make_casa_testimage_of_shape(shape, outfile): | ||
ia = image() | ||
|
||
size = np.product(shape) | ||
im = np.arange(size).reshape(shape) | ||
|
||
ia.fromarray(outfile=outfile, pixels=im, overwrite=True) | ||
ia.close() | ||
|
||
|
||
@pytest.fixture | ||
def filename(request): | ||
return request.getfixturevalue(request.param) | ||
|
@@ -96,6 +107,45 @@ def test_casa_read_stokes(data_advs, tmp_path): | |
assert casacube.I.shape == cube.I.shape | ||
|
||
|
||
@pytest.mark.skipif(not casaOK, reason='CASA tests must be run in a CASA environment.') | ||
@pytest.mark.parametrize('filename', ('data_adv', 'data_advs', 'data_sdav', | ||
'data_vad', 'data_vsad'), | ||
indirect=['filename']) | ||
def test_casa_numpyreader_read(filename, tmp_path): | ||
|
||
cube = SpectralCube.read(filename) | ||
|
||
make_casa_testimage(filename, tmp_path / 'casa.image') | ||
|
||
casacube = SpectralCube.read(tmp_path / 'casa.image') | ||
|
||
assert casacube.shape == cube.shape | ||
|
||
arr = casa_image_array_reader(tmp_path / 'casa.image') | ||
|
||
assert np.all(arr == casacube.unmasked_data[:]) | ||
|
||
|
||
@pytest.mark.skipif(not casaOK, reason='CASA tests must be run in a CASA environment.') | ||
@pytest.mark.parametrize('shape', ((129,128,130), (513,128,128), (128,513,128), | ||
(128,128,513), (512,64,64)), | ||
) | ||
def test_casa_numpyreader_read_shape(shape, tmp_path): | ||
|
||
cube = SpectralCube.read(filename) | ||
|
||
make_casa_testimage_of_shape(shape, tmp_path / 'casa.image') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you have any control over the chunking? If so would be good to try different chunking options? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no, the chunking is on-disk There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to clarify, I mean can you create files on disk with different degrees of chunking? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaik, no. CASA makes some choice for you, and I do not know of any way to control that. It's at least not obviously accessible in any of the current interfaces. |
||
|
||
casacube = SpectralCube.read(tmp_path / 'casa.image') | ||
|
||
assert casacube.shape == cube.shape | ||
|
||
arr = casa_image_array_reader(tmp_path / 'casa.image') | ||
|
||
assert np.all(arr == casacube.unmasked_data[:]) | ||
|
||
|
||
|
||
@pytest.mark.skipif(not casaOK, reason='CASA tests must be run in a CASA environment.') | ||
def test_casa_mask(data_adv, tmp_path): | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A more memory efficient way to do this which doesn't rely on too many temporary arrays as this does would be to just create the final array then use index to insert each chunk at the correct location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And just saw the comments below :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep. I am irrationally confident that there is a trivial way to do what I'm trying to do in a single line, but I haven't figured out what that magical invocation is yet.