def test_noconstruct(): result = deserialize_class(('astropy.units.Quantity', (), { 'unit': 'deg' }), construct=False) assert result == (u.Quantity, (), {'unit': 'deg'})
def test_construct(): result = deserialize_class(('astropy.units.Quantity', (10,), {'unit': 'deg'})) assert_quantity_allclose(result, 10 * u.deg)
def test_invalid(): with raises(ValueError) as exc: deserialize_class(('astropy.units.Quantity', (), {'unit': 'deg'}, ())) assert exc.value.args[0] == 'Expected a tuple of three values'
def test_noconstruct(): result = deserialize_class(('astropy.units.Quantity', (), {'unit': 'deg'}), construct=False) assert result == (u.Quantity, (), {'unit': 'deg'})
def test_construct(): result = deserialize_class(('astropy.units.Quantity', (10, ), { 'unit': 'deg' })) assert_quantity_allclose(result, 10 * u.deg)
def _convert_dropped_to_internal(dropped_dimensions): """ Convert the `~astropy.wcs.wcsapi.wrappers.SlicedLowLevelWCS` style ``dropped_world_dimensions`` dictionary to the GlobalCoords internal representation. """ # Most of this method is adapted from # astropy.wcs.wcsapi.high_level_wcs.HighLevelWCSMixin.pixel_to_world new_internal_coords = {} world = dropped_dimensions.pop("value") components = dropped_dimensions.pop("world_axis_object_components") classes = dropped_dimensions.pop("world_axis_object_classes") # Deserialize classes if dropped_dimensions.get("serialized_classes", False): classes_new = {} for key, value in classes.items(): classes_new[key] = deserialize_class(value, construct=False) classes = classes_new args = defaultdict(list) kwargs = defaultdict(dict) for i, (key, attr, _) in enumerate(components): if isinstance(attr, str): kwargs[key][attr] = world[i] else: while attr > len(args[key]) - 1: args[key].append(None) args[key][attr] = world[i] # key is the unique names of the classes in the order they appear in components for key in default_order(components): key_ele = [ i for i, components in enumerate(components) if components[0] == key ] physical_types = [ dropped_dimensions["world_axis_physical_types"][i] for i in key_ele ] # Use name if it's set, drop back to physical type if not names = tuple([ dropped_dimensions["world_axis_names"][i] or dropped_dimensions["world_axis_physical_types"][i] for i in key_ele ]) # convert lists to strings if a single coordinate physical_types = physical_types[0] if len( physical_types) == 1 else tuple(physical_types) names = names[0] if len(set(names)) == 1 else names klass, ar, kw, *rest = classes[key] if len(rest) == 0: klass_gen = klass elif len(rest) == 1: klass_gen = rest[0] else: raise ValueError( "Tuples in world_axis_object_classes should have length 3 or 4" ) high_level_object = klass_gen(*args[key], *ar, **kwargs[key], **kw) # Special case SkyCoord to get a pretty name if isinstance(high_level_object, SkyCoord): names = high_level_object.name new_internal_coords[names] = (physical_types, high_level_object) return new_internal_coords