def test_two_1d_from_lookup_tables(time_lut): """ Create ExtraCoords from both tables at once using `from_lookup_tables` with `physical_types`. """ exposure_lut = range(10) * u.s pt = ["custom:time:creation"] with pytest.raises(ValueError, match=r"The number of physical types and lookup_tables"): ec = ExtraCoords.from_lookup_tables(["time", "exposure_time"], (0, 0), [time_lut, exposure_lut], pt) pt.append("custom:time:duration") ec = ExtraCoords.from_lookup_tables(["time", "exposure_time"], (0, 0), [time_lut, exposure_lut], pt) # This has created an "orphan" extra_coords with no NDCube connected. with pytest.raises(AttributeError, match=r"'NoneType' object has no attribute 'dimensions'"): assert ec.mapping == (0, 0) assert len(ec._lookup_tables) == 2 assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("time", "exposure_time") for i, physical_types in enumerate(pt): assert ec._lookup_tables[i][1].physical_types == [physical_types]
def extra_coords_wave(wave_lut): cube = MagicMock() cube.dimensions = [10] * u.pix ec = ExtraCoords(cube) ec.add("wave", 0, wave_lut) return ec
def test_length1_extra_coord(wave_lut): ec = ExtraCoords() ec.add("wavey", 0, wave_lut) item = slice(1, 2) sec = ec[item] assert (sec.wcs.pixel_to_world(0)[0] == wave_lut[item]).all() assert (sec.wcs.world_to_pixel(wave_lut[item])[0] == [0]).all()
def test_skycoord_mesh_false(skycoord_2d_lut): ec = ExtraCoords() ec.add_coordinate(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) assert len(ec._lookup_tables) == 1 assert ec.mapping == (0, 1) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("lat", "lon")
def test_skycoord_mesh_false(skycoord_2d_lut): cube = MagicMock() cube.dimensions = [10, 10] * u.pix ec = ExtraCoords(cube) ec.add(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) assert len(ec._lookup_tables) == 1 assert ec.mapping == (1, 0) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("lat", "lon")
def test_empty_ec(wcs_1d_l): ec = ExtraCoords() # Test slice of an empty EC assert ec[0].wcs is None assert ec.mapping == tuple() assert ec.wcs is None assert ec.keys() == tuple() ec.wcs = wcs_1d_l assert ec.wcs is wcs_1d_l ec.mapping = (0, ) assert ec.mapping == (0, )
def test_skycoord_1_pixel(skycoord_1d_lut): ec = ExtraCoords() ec.add_coordinate(("lon", "lat"), 0, skycoord_1d_lut, mesh=False) assert len(ec._lookup_tables) == 1 assert ec.mapping == (0, ) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 1 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("lon", "lat") sec = ec[1:4] assert sec.wcs.pixel_n_dim == 1 assert sec.wcs.world_n_dim == 2 assert sec.wcs.world_axis_names == ("lon", "lat")
def test_wcs_1d(wcs_1d_l): ec = ExtraCoords(wcs=wcs_1d_l, mapping=(0, )) assert ec.keys() == ('spectral', ) assert ec.mapping == (0, ) assert ec.wcs is wcs_1d_l subec = ec[1:] assert ec.keys() == ('spectral', ) assert ec.mapping == (0, ) assert np.allclose(ec.wcs.pixel_to_world_values(1), subec.wcs.pixel_to_world_values(1)) subec = ec[0] assert subec.wcs is None
def __init__(self, data, wcs=None, uncertainty=None, mask=None, meta=None, unit=None, extra_coords=None, copy=False, **kwargs): super().__init__(data, wcs=wcs, uncertainty=uncertainty, mask=mask, meta=meta, unit=unit, copy=copy, **kwargs) # Enforce that the WCS object is not None if self.wcs is None: raise TypeError("The WCS argument can not be None.") # Format extra coords. if not extra_coords: extra_coords = ExtraCoords() if not isinstance(extra_coords, ExtraCoords): raise TypeError( "The extra_coords argument must be a ndcube.ExtraCoords object." ) self._extra_coords = extra_coords self._global_coords = GlobalCoords(self)
def test_single_from_lut(wave_lut): ec = ExtraCoords.from_lookup_tables(("wave", ), (0, ), (wave_lut, )) assert len(ec._lookup_tables) == 1 assert ec.mapping == (0, ) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 1 assert ec.wcs.world_n_dim == 1 assert ec.wcs.world_axis_names == ("wave", )
def test_skycoord(skycoord_1d_lut): ec = ExtraCoords.from_lookup_tables((("lat", "lon"), ), ((0, 1), ), (skycoord_1d_lut, )) assert len(ec._lookup_tables) == 1 assert ec.mapping == (0, 1) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("lat", "lon")
def test_exceptions(wcs_1d_l): # Test unable to specify inconsistent dimensions and tables with pytest.raises(ValueError): ExtraCoords.from_lookup_tables(None, (0,), (0, 0)) # Test unable to add to WCS EC ec = ExtraCoords() ec.wcs = wcs_1d_l ec.mapping = (0,) with pytest.raises(ValueError): ec.add(None, 0, None) with pytest.raises(KeyError): ExtraCoords()['empty']
def test_two_1d_from_lut(time_lut): exposure_lut = range(10) * u.s ec = ExtraCoords.from_lookup_tables(("time", "exposure_time"), (0, 0), (time_lut, exposure_lut)) assert len(ec._lookup_tables) == 2 assert ec.mapping == (0, 0) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("time", "exposure_time")
def test_skycoord_1_pixel(skycoord_1d_lut): cube = MagicMock() cube.dimensions = [10] * u.pix ec = ExtraCoords(cube) ec.add(("lon", "lat"), 0, skycoord_1d_lut, mesh=False) assert len(ec._lookup_tables) == 1 assert ec.mapping == (0,) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 1 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("lon", "lat") sec = ec[1:4] assert sec.wcs.pixel_n_dim == 1 assert sec.wcs.world_n_dim == 2 assert sec.wcs.world_axis_names == ("lon", "lat") assert isinstance(sec.wcs.pixel_to_world(0), SkyCoord)
def test_extra_coords_index(skycoord_2d_lut, time_lut): ec = ExtraCoords() ec.add_coordinate(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) ec.add_coordinate("exposure_time", (0, ), time_lut) assert len(ec._lookup_tables) == 2 assert ec.mapping == (0, 1, 0) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 3 assert ec.wcs.world_n_dim == 3 assert ec.wcs.world_axis_names == ("lat", "lon", "exposure_time") sub_ec = ec["lon"] assert len(sub_ec._lookup_tables) == 1 assert sub_ec.mapping == (0, 1) assert isinstance(ec.wcs, gwcs.WCS) assert sub_ec.wcs.pixel_n_dim == 2 assert sub_ec.wcs.world_n_dim == 2 assert sub_ec.wcs.world_axis_names == ("lat", "lon") sub_ec = ec["exposure_time"] assert len(sub_ec._lookup_tables) == 1 assert sub_ec.mapping == (0, ) assert isinstance(ec.wcs, gwcs.WCS) assert sub_ec.wcs.pixel_n_dim == 1 assert sub_ec.wcs.world_n_dim == 1 assert sub_ec.wcs.world_axis_names == ("exposure_time", )
def test_extra_coords_index(skycoord_2d_lut, time_lut): cube = MagicMock() cube.dimensions = [10, 10] * u.pix ec = ExtraCoords(cube) ec.add(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) ec.add("exposure_time", (0,), time_lut) assert len(ec._lookup_tables) == 2 assert ec.mapping == (1, 0, 1) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 3 assert ec.wcs.world_n_dim == 3 assert ec.wcs.world_axis_names == ("lat", "lon", "exposure_time") sub_ec = ec["lon"] sub_ec._ndcube = cube assert len(sub_ec._lookup_tables) == 1 assert sub_ec.mapping == (1, 0) assert isinstance(ec.wcs, gwcs.WCS) assert sub_ec.wcs.pixel_n_dim == 2 assert sub_ec.wcs.world_n_dim == 2 assert sub_ec.wcs.world_axis_names == ("lat", "lon") sub_ec = ec["exposure_time"] sub_ec._ndcube = cube assert len(sub_ec._lookup_tables) == 1 assert sub_ec.mapping == (1,) assert isinstance(ec.wcs, gwcs.WCS) assert sub_ec.wcs.pixel_n_dim == 1 assert sub_ec.wcs.world_n_dim == 1 assert sub_ec.wcs.world_axis_names == ("exposure_time",)
def test_slice_drop_dimensions(time_lut, skycoord_2d_lut): ec = ExtraCoords() ec.add_coordinate(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) ec.add_coordinate("exposure_time", (0, ), time_lut) sec = ec[0, :] assert len(sec._lookup_tables) == 1 assert u.allclose( sec['lat'].wcs.pixel_to_world_values(list(range(2))), ec['lat'].wcs.pixel_to_world_values([0, 0], list(range(2)))) assert u.allclose( sec['lon'].wcs.pixel_to_world_values(list(range(2))), ec['lon'].wcs.pixel_to_world_values([0, 0], list(range(2)))) sec = ec[:, 0] assert len(sec._lookup_tables) == 2 assert u.allclose( sec['lat'].wcs.pixel_to_world_values(list(range(2))), ec['lat'].wcs.pixel_to_world_values(list(range(2)), [0, 0])) assert u.allclose( sec['lon'].wcs.pixel_to_world_values(list(range(2))), ec['lon'].wcs.pixel_to_world_values(list(range(2)), [0, 0])) assert u.allclose( sec['exposure_time'].wcs.pixel_to_world_values(list(range(2)), list(range(2))), ec['exposure_time'].wcs.pixel_to_world_values(list(range(2)), list(range(2))))
def test_exceptions(wcs_1d_l): # Test fail when one of wcs or mapping specified with pytest.raises(ValueError): ExtraCoords(wcs=wcs_1d_l) with pytest.raises(ValueError): ExtraCoords(mapping=0) # Test unable to specify inconsistent dimensions and tables with pytest.raises(ValueError): ExtraCoords.from_lookup_tables(None, (0, ), (0, 0)) # Test unable to add to WCS EC ec = ExtraCoords(wcs=wcs_1d_l, mapping=(0, )) with pytest.raises(ValueError): ec.add_coordinate(None, 0, None) with pytest.raises(KeyError): ExtraCoords()['empty']
def test_slice_extra_1d(time_lut, wave_lut): ec = ExtraCoords() ec.add_coordinate("time", 0, time_lut) ec.add_coordinate("wavey", 1, wave_lut) sec = ec[:, 3:7] assert len(sec._lookup_tables) == 2 assert u.allclose(sec['wavey'].wcs.pixel_to_world_values(list(range(4))), ec['wavey'].wcs.pixel_to_world_values(list(range(3, 7)))) assert u.allclose(sec['time'].wcs.pixel_to_world_values(list(range(4))), ec['time'].wcs.pixel_to_world_values(list(range(4))))
def test_two_1d_from_lut(time_lut): cube = MagicMock() cube.dimensions = [10] * u.pix ec = ExtraCoords(cube) exposure_lut = range(10) * u.s ec.add("time", 0, time_lut) ec.add("exposure_time", 0, exposure_lut) assert len(ec._lookup_tables) == 2 assert ec.mapping == (0, 0) assert isinstance(ec.wcs, gwcs.WCS) assert ec.wcs.pixel_n_dim == 2 assert ec.wcs.world_n_dim == 2 assert ec.wcs.world_axis_names == ("time", "exposure_time")
def test_slice_extra_1d_drop_alter_mapping_int_item(time_lut, wave_lut): ec = ExtraCoords() ec.add("time", 0, time_lut) ec.add("wavey", 1, wave_lut) sec = ec[0] assert len(sec._lookup_tables) == 1 assert sec._lookup_tables[0][0] == (0,) assert u.allclose(sec['wavey'].wcs.pixel_to_world_values(list(range(10))), ec['wavey'].wcs.pixel_to_world_values(list(range(10)))) dwd = sec.dropped_world_dimensions dwd.pop("world_axis_object_classes") assert dwd assert dwd["world_axis_units"] == ["s"]
def test_slice_extra_2d(time_lut, skycoord_2d_lut): ec = ExtraCoords() ec.add(("lat", "lon"), (0, 1), skycoord_2d_lut, mesh=False) ec.add("exposure_time", (0,), time_lut) sec = ec[1:5, 1:5] assert len(sec._lookup_tables) == 2 assert u.allclose(sec['lat'].wcs.pixel_to_world_values(list(range(2)), list(range(2))), ec['lat'].wcs.pixel_to_world_values(list(range(1, 3)), list(range(1, 3)))) assert u.allclose(sec['lon'].wcs.pixel_to_world_values(list(range(2)), list(range(2))), ec['lon'].wcs.pixel_to_world_values(list(range(1, 3)), list(range(1, 3)))) assert u.allclose(sec['exposure_time'].wcs.pixel_to_world_values(list(range(0, 3))), ec['exposure_time'].wcs.pixel_to_world_values(list(range(1, 4))))
def test_slice_extra_1d_drop(time_lut, wave_lut): ec = ExtraCoords() ec.add("time", 0, time_lut) ec.add("wavey", 1, wave_lut) sec = ec[:, 3] assert len(sec._lookup_tables) == 1 assert u.allclose(sec['time'].wcs.pixel_to_world_values(list(range(4))), ec['time'].wcs.pixel_to_world_values(list(range(4)))) dwd = sec.dropped_world_dimensions dwd.pop("world_axis_object_classes") assert dwd assert dwd["world_axis_units"] == ["nm"]
def test_slice_extra_twice(time_lut, wave_lut): ec = ExtraCoords() ec.add_coordinate("time", 0, time_lut) ec.add_coordinate("wavey", 1, wave_lut) sec = ec[1:, 0] assert len(sec._lookup_tables) == 1 assert u.allclose(sec['time'].wcs.pixel_to_world_values(list(range(0, 2))), ec['time'].wcs.pixel_to_world_values(list(range(1, 3)))) sec = sec[1:, 0] assert len(sec._lookup_tables) == 1 assert u.allclose(sec['time'].wcs.pixel_to_world_values(list(range(0, 2))), ec['time'].wcs.pixel_to_world_values(list(range(2, 4))))
def test_mapping_setter(wcs_1d_l, wave_lut): ec = ExtraCoords(wcs=wcs_1d_l, mapping=(0, )) with pytest.raises(AttributeError): ec.mapping = None ec = ExtraCoords() ec.wcs = wcs_1d_l with pytest.raises(ValueError): ec.mapping = (1, ) ec = ExtraCoords() ec.add_coordinate("wave", (0, ), wave_lut) with pytest.raises(AttributeError): ec.mapping = None
def test_wcs_setter(wcs_1d_l, wave_lut): ec = ExtraCoords() ec.wcs = wcs_1d_l ec.mapping = (0,) with pytest.raises(AttributeError): ec.wcs = None ec = ExtraCoords() ec.mapping = (1,) with pytest.raises(ValueError): ec.wcs = wcs_1d_l ec = ExtraCoords() ec.add("wave", (0,), wave_lut) with pytest.raises(AttributeError): ec.wcs = None
def test_extra_coords_2d_quantity(quantity_2d_lut): ec = ExtraCoords() ec.add_coordinate("velocity", (0, 1), quantity_2d_lut) ec.wcs.pixel_to_world(0, 0)