def test_ndcube_components_after_slicing(): """Tests all cube components are correctly propagated by slicing.""" # Slice test object item = tuple([slice(0, 1)] * 3) sliced_cube = spectrogram_instrument_axes[item] # Generate expected result. data = spectrogram_instrument_axes.data[item] uncertainty = spectrogram_instrument_axes.uncertainty[item] mask = spectrogram_instrument_axes.mask[item] extra_coords = list(EXTRA_COORDS0) ec_axis = 0 ec0 = list(extra_coords[0]) ec0[-1] = ec0[-1][item[ec_axis]] ec1 = list(extra_coords[1]) ec1[-1] = ec1[-1][item[ec_axis]] extra_coords = (tuple(ec0), tuple(ec1)) wcs = spectrogram_instrument_axes.wcs[item] expected_cube = SpectrogramCube( data=data, wcs=wcs, uncertainty=uncertainty, mask=mask, meta=spectrogram_instrument_axes.meta, unit=spectrogram_instrument_axes.unit, extra_coords=extra_coords, missing_axes=spectrogram_instrument_axes.missing_axes, instrument_axes=spectrogram_instrument_axes.instrument_axes) assert_cubes_equal(sliced_cube, expected_cube)
def test_crop_by_extra_coords_shared_axis(ndcube_3d_ln_lt_l_ec_sharing_axis): cube = ndcube_3d_ln_lt_l_ec_sharing_axis lower_corner = (1 * u.m, 1 * u.keV) upper_corner = (2 * u.m, 2 * u.keV) output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords) expected = cube[:, 1:3] helpers.assert_cubes_equal(output, expected)
def test_crop(ndcube_4d_ln_lt_l_t): intervals = ndcube_4d_ln_lt_l_t.wcs.array_index_to_world([1, 2], [0, 1], [0, 1], [0, 2]) lower_corner = [coord[0] for coord in intervals] upper_corner = [coord[-1] for coord in intervals] expected = ndcube_4d_ln_lt_l_t[1:3, 0:2, 0:2, 0:3] output = ndcube_4d_ln_lt_l_t.crop(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_by_values_1d_dependent(ndcube_4d_ln_lt_l_t): cube_1d = ndcube_4d_ln_lt_l_t[0, :, 0, 0] lat_range, lon_range = cube_1d.wcs.low_level_wcs.array_index_to_world_values([0, 1]) lower_corner = [lat_range[0] * u.deg, lon_range[0] * u.deg] upper_corner = [lat_range[-1] * u.deg, lon_range[-1] * u.deg] expected = cube_1d[0:2] output = cube_1d.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_tuple_non_tuple_input(ndcube_2d_ln_lt): cube = ndcube_2d_ln_lt frame = astropy.wcs.utils.wcs_to_celestial_frame(cube.wcs) lower_corner = SkyCoord(Tx=359.99667, Ty=-0.0011111111, unit="deg", frame=frame) upper_corner = SkyCoord(Tx=0.0044444444, Ty=0.0011111111, unit="deg", frame=frame) cropped_by_tuples = cube.crop((lower_corner,), (upper_corner,)) cropped_by_coords = cube.crop(lower_corner, upper_corner) helpers.assert_cubes_equal(cropped_by_tuples, cropped_by_coords)
def test_crop_by_values_with_nones(ndcube_4d_ln_lt_l_t): lower_corner = [None] * 4 lower_corner[0] = 0.5 * u.min upper_corner = [None] * 4 upper_corner[0] = 1.1 * u.min expected = ndcube_4d_ln_lt_l_t[:, :, :, 0:3] output = ndcube_4d_ln_lt_l_t.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_by_values_with_equivalent_units(ndcube_2d_ln_lt): # test cropping when passed units that are not identical to the cube wcs.world_axis_units intervals = ndcube_2d_ln_lt.wcs.array_index_to_world_values([0, 3], [1, 6]) lower_corner = [(coord[0] * u.deg).to(u.arcsec) for coord in intervals] upper_corner = [(coord[-1] * u.deg).to(u.arcsec) for coord in intervals] expected = ndcube_2d_ln_lt[0:4, 1:7] output = ndcube_2d_ln_lt.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_by_extra_coords(ndcube_3d_ln_lt_l_ec_time): cube = ndcube_3d_ln_lt_l_ec_time lower_corner = (Time("2000-01-01T15:00:00", scale="utc", format="fits"), None) upper_corner = (Time("2000-01-01T20:00:00", scale="utc", format="fits"), None) output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords) expected = cube[0] helpers.assert_cubes_equal(output, expected)
def test_crop_with_nones(ndcube_4d_ln_lt_l_t): lower_corner = [None] * 3 upper_corner = [None] * 3 interval0 = ndcube_4d_ln_lt_l_t.wcs.array_index_to_world([1, 2], [0, 1], [0, 1], [0, 2])[0] lower_corner[0] = interval0[0] upper_corner[0] = interval0[-1] expected = ndcube_4d_ln_lt_l_t[:, :, :, 0:3] output = ndcube_4d_ln_lt_l_t.crop(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_by_extra_coords_values(ndcube_3d_ln_lt_l_ec_time): cube = ndcube_3d_ln_lt_l_ec_time lower_corner = (3 * 60 * 60 * u.s, 0 * u.pix) upper_corner = (8 * 60 * 60 * u.s, 2 * u.pix) output = cube.crop_by_values(lower_corner, upper_corner, wcs=cube.extra_coords) expected = cube[0] helpers.assert_cubes_equal(output, expected)
def test_crop_by_values(ndcube_4d_ln_lt_l_t): intervals = ndcube_4d_ln_lt_l_t.wcs.array_index_to_world_values([1, 2], [0, 1], [0, 1], [0, 2]) units = [u.min, u.m, u.deg, u.deg] lower_corner = [coord[0] * unit for coord, unit in zip(intervals, units)] upper_corner = [coord[-1] * unit for coord, unit in zip(intervals, units)] # Ensure some quantities are in units different from each other # and those stored in the WCS. lower_corner[0] = lower_corner[0].to(u.ms) lower_corner[-1] = lower_corner[-1].to(u.arcsec) upper_corner[-1] = upper_corner[-1].to(u.arcsec) expected = ndcube_4d_ln_lt_l_t[1:3, 0:2, 0:2, 0:3] output = ndcube_4d_ln_lt_l_t.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, expected)
def test_crop_by_extra_coords_all_axes_with_coord( ndcube_3d_ln_lt_l_ec_all_axes): cube = ndcube_3d_ln_lt_l_ec_all_axes interval0 = Time(["2000-01-01T15:00:00", "2000-01-01T20:00:00"], scale="utc", format="fits") interval1 = [0, 1] * u.pix interval2 = [1, 3] * u.m lower_corner = (interval0[0], interval1[0], interval2[0]) upper_corner = (interval0[1], interval1[1], interval2[1]) output = cube.crop(lower_corner, upper_corner, wcs=cube.extra_coords) expected = cube[0, 0:2, 1:4] helpers.assert_cubes_equal(output, expected)
def test_crop_by_extra_coords_values_all_axes_with_coord( ndcube_3d_ln_lt_l_ec_all_axes): cube = ndcube_3d_ln_lt_l_ec_all_axes interval0 = [3 * 60 * 60, 8 * 60 * 60] * u.s interval1 = [0, 1] * u.pix interval2 = [1, 3] * u.m lower_corner = (interval0[0], interval1[0], interval2[0]) upper_corner = (interval0[1], interval1[1], interval2[1]) output = cube.crop_by_values(lower_corner, upper_corner, wcs=cube.extra_coords) expected = cube[0, 0:2, 1:4] helpers.assert_cubes_equal(output, expected)
def test_crop_by_coords_with_units(ndcube_4d_ln_lt_l_t): intervals = ndcube_4d_ln_lt_l_t.wcs.array_index_to_world_values([1, 2], [0, 1], [0, 1], [0, 2]) units = [u.min, u.m, u.deg, u.deg] lower_corner = [coord[0] for coord in intervals] upper_corner = [coord[-1] for coord in intervals] lower_corner[0] *= u.min upper_corner[0] *= u.min lower_corner[1] *= u.m upper_corner[1] *= u.m lower_corner[2] *= u.deg units[0] = None expected = ndcube_4d_ln_lt_l_t[1:3, 0:2, 0:2, 0:3] output = ndcube_4d_ln_lt_l_t.crop_by_values(lower_corner, upper_corner, units=units) helpers.assert_cubes_equal(output, expected)
def test_collection_pop(collection, popped_key, expected_popped, expected_collection): popped_collection = collection.copy() output = popped_collection.pop(popped_key) helpers.assert_cubes_equal(output, expected_popped) helpers.assert_collections_equal(popped_collection, expected_collection)
def test_slice_cube_from_collection(item, collection, expected): helpers.assert_cubes_equal(collection[item], expected)
def test_ndcubeordered(test_input, expected): helpers.assert_cubes_equal( NDCubeOrdered(test_input[0], test_input[1], mask=test_input[2], uncertainty=test_input[3], extra_coords=test_input[4]), expected)
def test_crop_by_extra_coord(test_input, expected): helpers.assert_cubes_equal( test_input[0].crop_by_extra_coord(*tuple(test_input[1:])), expected)
def test_crop_by_coords(test_input, expected): helpers.assert_cubes_equal( test_input[0].crop_by_coords(*test_input[1:]), expected)
def test_crop_by_values_all_nones(ndcube_4d_ln_lt_l_t): lower_corner = [None] * 4 upper_corner = [None] * 4 output = ndcube_4d_ln_lt_l_t.crop_by_values(lower_corner, upper_corner) helpers.assert_cubes_equal(output, ndcube_4d_ln_lt_l_t)
def test_crop_1d_dependent(ndcube_4d_ln_lt_l_t): cube_1d = ndcube_4d_ln_lt_l_t[0, :, 0, 0] sky_range = cube_1d.wcs.array_index_to_world([0, 1]) expected = cube_1d[0:2] output = cube_1d.crop([sky_range[0]], [sky_range[-1]]) helpers.assert_cubes_equal(output, expected)
def test_crop_1d_independent(ndcube_4d_ln_lt_l_t): cube_1d = ndcube_4d_ln_lt_l_t[0, 0, :, 0] wl_range = SpectralCoord([3e-11, 4.5e-11], unit=u.m) expected = cube_1d[0:2] output = cube_1d.crop([wl_range[0]], [wl_range[-1]]) helpers.assert_cubes_equal(output, expected)
def test_IRISSpectrogramCube_convert_to(input_cube, new_unit, expected_cube): output_cube = input_cube.convert_to(new_unit) assert_cubes_equal(output_cube, expected_cube)
def test_crop_reduces_dimensionality(ndcube_4d_ln_lt_l_t): cube = ndcube_4d_ln_lt_l_t point = (None, SpectralCoord([3e-11], unit=u.m), None) expected = cube[:, :, 0, :] output = cube.crop(point) helpers.assert_cubes_equal(output, expected)
def test_IRISSpectrogramCube_apply_exposure_time_correction(input_cube, undo, force, expected_cube): output_cube = input_cube.apply_exposure_time_correction(undo=undo, force=force) assert_cubes_equal(output_cube, expected_cube)