From 7d2f6e34675955b19d194abeddda29980b7fa1d1 Mon Sep 17 00:00:00 2001 From: Joshua Hoskins Date: Tue, 29 Aug 2023 13:15:50 -0400 Subject: [PATCH] Added more coverage, still a few more to go. --- src/astrohack/_utils/_holog.py | 16 ++- tests/unit/test_holog.py | 251 +++++++++++++++++---------------- 2 files changed, 141 insertions(+), 126 deletions(-) diff --git a/src/astrohack/_utils/_holog.py b/src/astrohack/_utils/_holog.py index 912712dc..25070bdd 100644 --- a/src/astrohack/_utils/_holog.py +++ b/src/astrohack/_utils/_holog.py @@ -141,16 +141,19 @@ def _holog_chunk(holog_chunk_params): ############### - pol = beam_grid,ant_data_dict[ddi][holog_map].pol.values + pol = ant_data_dict[ddi][holog_map].pol.values if to_stokes: beam_grid = _to_stokes(beam_grid, ant_data_dict[ddi][holog_map].pol.values) pol = ['I', 'Q', 'U', 'V'] + ############### if holog_chunk_params["scan_average"]: beam_grid = np.mean(beam_grid,axis=0)[None,...] time_centroid = np.mean(np.array(time_centroid)) + + logger.info("Calculating aperture pattern ...") # Current bottleneck aperture_grid, u, v, uv_cell_size = _calculate_aperture_pattern( grid=beam_grid, @@ -172,7 +175,7 @@ def _holog_chunk(holog_chunk_params): else: raise Exception("{function_name}: Antenna type not found: {name}".format(function_name=function_name, name=meta_data['ant_name'])) - + telescope = Telescope(telescope_name) min_wavelength = clight/freq_chan[0] @@ -256,6 +259,7 @@ def _holog_chunk(holog_chunk_params): focus_z_offset=do_z_foc_off, subreflector_tilt=do_sub_til, cassegrain_offset=do_cass_off) + else: logger.info('[{function_name}]: Skipping phase correction') @@ -281,7 +285,7 @@ def _holog_chunk(holog_chunk_params): xds.attrs["telescope_name"] = meta_data['telescope_name'] xds.attrs["time_centroid"] = np.array(time_centroid) xds.attrs["ddi"] = ddi - + coords = { "ddi": list(ant_data_dict.keys()), "pol": pol, @@ -293,13 +297,12 @@ def _holog_chunk(holog_chunk_params): "v_prime": v_prime, "chan": freq_chan } - - xds = xds.assign_coords(coords) + xds = xds.assign_coords(coords) + xds.to_zarr("{name}/{ant}/{ddi}".format(name=holog_chunk_params["image_name"], ant=holog_chunk_params["this_ant"], ddi=ddi), mode="w", compute=True, consolidated=True) - def _create_average_chan_map(freq_chan, chan_tolerance_factor): n_chan = len(freq_chan) cf_chan_map = np.zeros((n_chan,), dtype=int) @@ -393,6 +396,7 @@ def _export_to_fits_holog_chunk(parm_dict): 'SCAN_AVE': _bool_to_string(metadata['scan_average']), 'TO_STOKE': _bool_to_string(metadata['to_stokes']), } + ntime = len(inputxds.time) if ntime != 1: raise Exception("[{function_name}]: Data with multiple times not supported for FITS export") diff --git a/tests/unit/test_holog.py b/tests/unit/test_holog.py index 5c543c64..9eb6a42b 100644 --- a/tests/unit/test_holog.py +++ b/tests/unit/test_holog.py @@ -25,26 +25,6 @@ def setup_class(cls): astrohack.gdown_utils.download(file='extract_holog_verification.json') astrohack.gdown_utils.download(file='holog_numerical_verification.json') - @classmethod - def teardown_class(cls): - """ teardown any state that was previously setup with a call to setup_class - such as deleting test data """ - shutil.rmtree("data") - - def setup_method(self): - """ setup any state specific to all methods of the given class """ - pass - - def teardown_method(self): - """ teardown any state that was previously setup for all methods of the given class """ - pass - - - def test_holog_grid_cell_size(self): - - tolerance = 2.e-5 - - # Generate pointing file extract_pointing( ms_name="data/ea25_cal_small_after_fixed.split.ms", point_name="data/ea25_cal_small_after_fixed.split.point.zarr", @@ -73,17 +53,31 @@ def test_holog_grid_cell_size(self): holog( holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', image_name='data/ea25_cal_small_after_fixed.split.image.zarr', - padding_factor=50, - grid_interpolation_mode='linear', - chan_average=True, - scan_average=True, overwrite=True, - phase_fit=True, - apply_mask=True, - to_stokes=True, parallel=False ) + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + cls.json_file = json.load(json_attr) + + @classmethod + def teardown_class(cls): + """ teardown any state that was previously setup with a call to setup_class + such as deleting test data """ + shutil.rmtree("data") + + def setup_method(self): + """ setup any state specific to all methods of the given class """ + pass + + def teardown_method(self): + """ teardown any state that was previously setup for all methods of the given class """ + pass + + + def test_holog_grid_cell_size(self): + + tolerance = 2.e-5 with open("holog_numerical_verification.json") as file: reference_dict = json.load(file) @@ -104,39 +98,18 @@ def test_holog_grid_cell_size(self): def test_holog_image_name(self): - # Generate pointing file - extract_pointing( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - overwrite=True, - parallel=False - ) - # Extract holography data using holog_obd_dict - holog_mds = extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - data_column="CORRECTED_DATA", - parallel=False, - overwrite=True - ) + assert os.path.exists('data/ea25_cal_small_after_fixed.split.image.zarr') - extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', - data_column='CORRECTED_DATA', - parallel=False, - overwrite=True - ) + def test_holog_ant_id(self): - holog( + image_mds = holog( holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', image_name='data/ea25_cal_small_after_fixed.split.image.zarr', - padding_factor=50, grid_interpolation_mode='linear', chan_average=True, scan_average=True, + ant_id=['ea25'], overwrite=True, phase_fit=True, apply_mask=True, @@ -144,34 +117,32 @@ def test_holog_image_name(self): parallel=False ) - assert os.path.exists('data/ea25_cal_small_after_fixed.split.image.zarr') + assert list(image_mds.keys()) == ['ant_ea25'] - def test_holog_ant_id(self): + def test_holog_ddi(self): - extract_pointing( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", + image_mds = holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + padding_factor=50, + grid_interpolation_mode='linear', + chan_average=True, + scan_average=True, overwrite=True, + ddi=[0], + phase_fit=True, + apply_mask=True, + to_stokes=True, parallel=False ) - # Extract holography data using holog_obd_dict - holog_mds = extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - data_column="CORRECTED_DATA", - parallel=False, - overwrite=True - ) + for ant in image_mds.keys(): + for ddi in image_mds[ant].keys(): + assert ddi == "ddi_0" - extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', - data_column='CORRECTED_DATA', - parallel=False, - overwrite=True - ) + + + def test_holog_padding_factor(self): image_mds = holog( holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', @@ -180,7 +151,6 @@ def test_holog_ant_id(self): grid_interpolation_mode='linear', chan_average=True, scan_average=True, - ant_id=['ea25'], overwrite=True, phase_fit=True, apply_mask=True, @@ -188,73 +158,114 @@ def test_holog_ant_id(self): parallel=False ) - assert list(image_mds.keys()) == ['ant_ea25'] - - def test_holog_ddi(self): + for ant in image_mds.keys(): + for ddi in image_mds[ant].keys(): + assert image_mds[ant][ddi].APERTURE.shape == (1, 1, 4, 529, 529) - extract_pointing( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", + def test_holog_chan_average(self): + image_mds = holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + chan_average=True, overwrite=True, parallel=False ) - # Extract holography data using holog_obd_dict - holog_mds = extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - data_column="CORRECTED_DATA", - parallel=False, - overwrite=True - ) + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + json_file = json.load(json_attr) + + assert json_file['chan_average'] == True - extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", + def test_holog_scan_average(self): + image_mds = holog( holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', - data_column='CORRECTED_DATA', - parallel=False, - overwrite=True - ) + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + scan_average=False, + overwrite=True, + parallel=False + ) + + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + json_file = json.load(json_attr) + assert json_file['scan_average'] == False + + def test_holog_grid_interpolation(self): image_mds = holog( holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', image_name='data/ea25_cal_small_after_fixed.split.image.zarr', - padding_factor=50, - grid_interpolation_mode='linear', - chan_average=True, - scan_average=True, + grid_interpolation_mode='nearest', overwrite=True, - ddi=[0], - phase_fit=True, - apply_mask=True, - to_stokes=True, parallel=False ) - for ant in image_mds.keys(): - for ddi in image_mds[ant].keys(): - assert ddi == "ddi_0" + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + json_file = json.load(json_attr) - ''' + assert json_file['grid_interpolation_mode'] == 'nearest' - def test_holog_padding_factor(self): + def test_holog_chan_tolerance(self): + image_mds = holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + chan_tolerance_factor=0.0049, + overwrite=True, + parallel=False + ) - # Generate pointing file - extract_pointing( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + json_file = json.load(json_attr) + + assert json_file['chan_tolerance_factor'] == 0.0049 + + def test_holog_to_stokes(self): + image_mds = holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + to_stokes=False, overwrite=True, parallel=False ) - # Extract holography data using holog_obd_dict - holog_mds = extract_holog( - ms_name="data/ea25_cal_small_after_fixed.split.ms", - point_name="data/ea25_cal_small_after_fixed.split.point.zarr", - data_column="CORRECTED_DATA", - parallel=False, - overwrite=True + with open('data/ea25_cal_small_after_fixed.split.image.zarr/.image_attr') as json_attr: + json_file = json.load(json_attr) + + assert json_file['to_stokes'] == False + + def test_holog_overwrite(self): + initial_time = os.path.getctime('data/ea25_cal_small_after_fixed.split.image.zarr') + + image_mds = holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + overwrite=True, + parallel=False ) - ''' \ No newline at end of file + modified_time = os.path.getctime('data/ea25_cal_small_after_fixed.split.image.zarr') + + assert initial_time != modified_time + + def test_holog_not_overwrite(self): + initial_time = os.path.getctime('data/ea25_cal_small_after_fixed.split.image.zarr') + + try: + holog( + holog_name='data/ea25_cal_small_after_fixed.split.holog.zarr', + image_name='data/ea25_cal_small_after_fixed.split.image.zarr', + overwrite=False, + parallel=False + ) + + except FileExistsError: + pass + + finally: + modified_time = os.path.getctime('data/ea25_cal_small_after_fixed.split.image.zarr') + + assert initial_time == modified_time + + + + + \ No newline at end of file