1- from collections .abc import Generator
2- from typing import Any
1+ import sys
32
43import astropy .units as u
54import numpy as np
4746 Supergalactic ,
4847)
4948from astropy .time import Time
50- from xarray .tests import requires_zarr
5149
5250from astropy_xarray .coordinates import (
5351 dataset_to_skycoord ,
5452 skycoord_to_dataset ,
5553)
5654from astropy_xarray .coordinates .sky_coord import (
57- DatasetRepresentation ,
5855 _skycoord_differential_component_names ,
5956 _skycoord_representation_component_names ,
6057)
6158
59+ pytestmark = pytest .mark .skipif (
60+ sys .version_info < (3 , 11 ),
61+ reason = "python3.10 or higher required for skycoords support" ,
62+ )
63+
6264
6365@pytest .mark .parametrize (
6466 ("expected_base" , "expected_s" ),
@@ -84,26 +86,15 @@ def test_unitspherical_repr_components(expected_base, expected_s):
8486
8587
8688@pytest .mark .parametrize (
87- ("representation" , " expected_base" , "expected_s" ),
89+ ("expected_base" , "expected_s" ),
8890 [
8991 (
90- DatasetRepresentation .FRAME ,
9192 ("ra" , "dec" , "distance" ),
9293 ("pm_ra_cosdec" , "pm_dec" ),
93- ),
94- (
95- DatasetRepresentation .FRAME_DEFAULT ,
96- ("ra" , "dec" , "distance" ),
97- ("pm_ra_cosdec" , "pm_dec" , "radial_velocity" ),
98- ),
99- (
100- DatasetRepresentation .DATA ,
101- ("lon" , "lat" , "distance" ),
102- ("d_lon_coslat" , "d_lat" ),
103- ),
94+ )
10495 ],
10596)
106- def test_unitspherical_diff_components (representation , expected_base , expected_s ):
97+ def test_unitspherical_diff_components (expected_base , expected_s ):
10798 sc = SkyCoord (
10899 ra = [[0.1 ], [2 ], [0.2 ]] * u .deg ,
109100 dec = [[0.5 ], [7 ], [0.7 ]] * u .deg ,
@@ -118,31 +109,26 @@ def test_unitspherical_diff_components(representation, expected_base, expected_s
118109
119110
120111@pytest .mark .parametrize (
121- ("representation" , " frame" , "expected_base" , "expected_s" ),
112+ ("frame" , "expected_base" , "expected_s" ),
122113 [
123114 (
124- DatasetRepresentation .FRAME_DATA ,
125115 ICRS (),
126116 ("ra" , "dec" ),
127117 ("pm_ra_cosdec" , "pm_dec" ),
128118 ),
129119 (
130- DatasetRepresentation .FRAME_DATA ,
131120 AltAz (),
132121 ("az" , "alt" ),
133122 ("pm_az_cosalt" , "pm_alt" ),
134123 ),
135124 (
136- DatasetRepresentation .FRAME_DATA ,
137125 ITRS (),
138126 ("lon" , "lat" ),
139127 ("pm_lon_coslat" , "pm_lat" ),
140128 ),
141129 ],
142130)
143- def test_spherical_direction_components_realize_frame (
144- representation , frame , expected_base , expected_s
145- ):
131+ def test_spherical_direction_components_realize_frame (frame , expected_base , expected_s ):
146132 rep = UnitSphericalRepresentation (
147133 [[0.1 ], [2 ], [0.2 ]] * u .deg ,
148134 [[0.5 ], [7 ], [0.7 ]] * u .deg ,
@@ -157,12 +143,12 @@ def test_spherical_direction_components_realize_frame(
157143
158144
159145@pytest .mark .parametrize (
160- ("representation" , " expected_base" , "expected_s" ),
146+ ("expected_base" , "expected_s" ),
161147 [
162- (DatasetRepresentation . FRAME_DATA , ("x" , "y" , "z" ), ("v_x" , "v_y" , "v_z" )),
148+ (("x" , "y" , "z" ), ("v_x" , "v_y" , "v_z" )),
163149 ],
164150)
165- def test_cartesian_direction_components (representation , expected_base , expected_s ):
151+ def test_cartesian_direction_components (expected_base , expected_s ):
166152 sc = SkyCoord (
167153 x = [[0.1 ], [2 ], [0.2 ]] * u .dimensionless_unscaled ,
168154 y = [[0.5 ], [7 ], [0.7 ]] * u .dimensionless_unscaled ,
@@ -176,20 +162,11 @@ def test_cartesian_direction_components(representation, expected_base, expected_
176162 assert _skycoord_differential_component_names (sc ) == expected_s
177163
178164
179- @pytest .fixture (name = "store" , scope = "session" )
180- def store_fixture () -> Generator [Any , None , None ]:
181- import zarr .storage as zs
182-
183- with zs .MemoryStore () as store :
184- yield store
185-
186-
187- @requires_zarr
188165@pytest .mark .parametrize (
189- "representation " ,
166+ "use_default_representation " ,
190167 [
191- pytest .param (DatasetRepresentation . FRAME_DATA ),
192- pytest .param (DatasetRepresentation . FRAME_DEFAULT ),
168+ pytest .param (True ),
169+ pytest .param (False ),
193170 ],
194171)
195172@pytest .mark .parametrize (
@@ -308,37 +285,29 @@ def store_fixture() -> Generator[Any, None, None]:
308285 ids = lambda param : param .name ,
309286)
310287def test_skycoord_roundtrip (
311- store ,
312288 frame : BaseCoordinateFrame ,
313289 data : BaseRepresentation ,
314290 expected_data_classes : tuple [type , ...],
315- representation : DatasetRepresentation ,
291+ use_default_representation : bool ,
316292):
317293 # all frames support all representation types
318294 representation_type = type (data )
319295 differential_type = type (data .differentials ["s" ])
320296
321- if representation == DatasetRepresentation .FRAME_DATA :
322- expected = SkyCoord (frame .realize_frame (data ))
323- elif representation == DatasetRepresentation .FRAME_DEFAULT :
297+ if use_default_representation :
324298 expected = SkyCoord (
325299 frame .realize_frame (
326300 data .represent_as (
327301 frame .default_representation , frame .default_differential
328302 )
329303 )
330304 )
331- else :
332- raise NotImplementedError ()
333-
334- if representation == DatasetRepresentation .FRAME_DEFAULT :
335305 expected_frame_data_rep_name = frame .default_representation .name
336306 expected_frame_data_diff_name = frame .default_differential .name
337- elif representation == DatasetRepresentation .FRAME_DATA :
307+ else :
308+ expected = SkyCoord (frame .realize_frame (data ))
338309 expected_frame_data_rep_name = representation_type .name
339310 expected_frame_data_diff_name = differential_type .name
340- else :
341- raise NotImplementedError ()
342311
343312 assert expected .frame .data .name == expected_frame_data_rep_name
344313 assert expected .frame .data .differentials ["s" ].name == expected_frame_data_diff_name
@@ -360,46 +329,9 @@ def test_skycoord_roundtrip(
360329 )
361330 assert set (ds .data_vars ) == expected_keys
362331
363- # data_var value types
364- if representation in (
365- DatasetRepresentation .FRAME ,
366- DatasetRepresentation .FRAME_DEFAULT ,
367- ):
368- print (expected_keys )
369- elif representation == DatasetRepresentation .DATA :
370- expected_classes = zip (
371- expected .get_representation_component_names (), expected_data_classes
372- )
373- for expected_key , expected_class_type in expected_classes :
374- assert isinstance (ds .data_vars [expected_key ].data , expected_class_type )
375- assert ds .data_vars [expected_key ].coords .identical (
376- xr .Coordinates (dict (coords ))
377- )
378-
379- # sanity check dataset
380- assert ds .attrs ["frame" ]["representation_type" ] == representation_type .name
381- assert ds .attrs ["frame" ]["differential_type" ] == differential_type .name
382- assert ds .attrs ["frame" ]["data" ]["representation_type" ] == data .name
383- assert (
384- ds .attrs ["frame" ]["data" ]["differential_type" ]
385- == data .differentials ["s" ].name
386- )
387-
388332 # Convert back
389333 actual = dataset_to_skycoord (ds )
390334
391- # check representations
392- if representation in (
393- DatasetRepresentation .FRAME ,
394- DatasetRepresentation .FRAME_DEFAULT ,
395- ):
396- print (actual )
397- if representation == DatasetRepresentation .DATA :
398- assert actual .representation_type .name == representation_type .name
399- assert actual .differential_type .name == differential_type .name
400- assert actual .frame .data .name == data .name
401- assert actual .frame .data .differentials ["s" ].name == data .differentials ["s" ].name
402-
403335 # Check Representation
404336 for component_name in actual .frame .representation_component_names :
405337 np .testing .assert_array_equal (
@@ -418,10 +350,3 @@ def test_skycoord_roundtrip(
418350 assert actual .is_equivalent_frame (expected )
419351 assert ds .coords .equals (xr .Coordinates (dict (coords )))
420352 np .testing .assert_array_equal (actual , expected )
421-
422- # Check intermediate dataset is serializable
423- # ds.astropy.dequantify().to_zarr(store, mode="w", consolidated=True)
424- # ds_store = xr.open_dataset(
425- # store, engine="zarr", consolidated=True
426- # ).astropy.quantify()
427- # xr.testing.assert_identical(ds, ds_store)
0 commit comments