def test_spec_version_match(): spec = AsdfSpec('>=1.1.0') assert spec.match(AsdfVersion('1.1.0')) assert spec.match(AsdfVersion('1.2.0')) assert not spec.match(AsdfVersion('1.0.0')) assert not spec.match(AsdfVersion('1.0.9'))
def test_version_constructor(): ver0 = AsdfVersion('1.0.0') ver1 = AsdfVersion((1,0,0)) ver2 = AsdfVersion([1,0,0]) assert str(ver0) == '1.0.0' assert str(ver1) == '1.0.0' assert str(ver2) == '1.0.0'
def test_version_and_version_equality(): ver0 = AsdfVersion('1.0.0') ver1 = AsdfVersion('1.0.0') assert ver0 is not ver1 assert ver0 == ver1 assert ver1 == ver0 assert not (ver0 != ver1) assert not (ver1 != ver0)
def get_mappings(self, version: Union[AsdfVersion, str] = None): """Get the manifest and schema mapping for the specified version. Parameters ---------- version : Union[AsdfVersion, str] Requested standard version. If `None` is provided, the latest will be used. Returns ------- ResourceMappingProxy : Manifest mapping ResourceMappingProxy : Schema mapping """ if version is None: version = self._max_version elif not isinstance(version, AsdfVersion): version = AsdfVersion(version) file_mappings = self._versions[version] manifest_mapping = self._map_file_content( file_mappings["manifest_file_mapping"], "manifests", version) schema_mapping = self._map_file_content( file_mappings["schema_file_mapping"], "schemas", version) return manifest_mapping, schema_mapping
def test_spec_version_select(): spec = AsdfSpec('>=1.1.0') versions = [AsdfVersion(x) for x in ['1.0.0', '1.0.9', '1.1.0', '1.2.0']] assert spec.select(versions) == '1.2.0' assert spec.select(versions[:-1]) == '1.1.0' assert spec.select(versions[:-2]) == None
def main(): args = parse_args() path = Path(args.filename) version = AsdfVersion(args.version) generate_file(path, version)
def test_version_and_string_equality(): version = AsdfVersion('1.0.0') string_ver = '1.0.0' assert version == string_ver assert string_ver == version assert not (version != string_ver) assert not (string_ver != version)
def test_version_and_tuple_equality(): version = AsdfVersion('1.0.0') tuple_ver = (1,0,0) assert version == tuple_ver assert tuple_ver == version assert not (version != tuple_ver) assert not (tuple_ver != version)
def from_tree_transform(cls, node, ctx): if cls.version < AsdfVersion('1.4.0'): # The 'dimensions' property was added in 1.4.0, # previously all values were 1D. return functional_models.Const1D(node['value']) elif node['dimensions'] == 1: return functional_models.Const1D(node['value']) elif node['dimensions'] == 2: return functional_models.Const2D(node['value'])
def from_tree_transform(self, node): if self.version < AsdfVersion('1.4.0'): # The 'dimensions' property was added in 1.4.0, # previously all values were 1D. return models.Const1D(node['value']) elif node['dimensions'] == 1: return models.Const1D(node['value']) elif node['dimensions'] == 2: return models.Const2D(node['value'])
def from_tree_transform(cls, node, ctx): if cls.version < AsdfVersion('1.4.0'): # The 'dimensions' property was added in 1.4.0, # previously all values were 1D. return functional_models.Const1D(node['value']) elif node['dimensions'] == 1: return functional_models.Const1D(node['value']) elif node['dimensions'] == 2: return functional_models.Const2D(node['value']) else: raise TypeError('Only 1D and 2D constant models are supported.')
def test_spec_equal(): """Make sure that equality means match""" spec = AsdfSpec('>=1.2.0') version0 = AsdfVersion('1.1.0') version1 = AsdfVersion('1.3.0') assert spec != version0 assert version0 != spec assert spec == version1 assert version1 == spec assert spec != '1.1.0' assert '1.1.0' != spec assert spec == '1.3.0' assert '1.3.0' == spec assert spec != (1, 1, 0) assert (1, 1, 0) != spec assert spec == (1, 3, 0) assert (1, 3, 0) == spec
def test_asdf_file_version(): with config_context() as config: config.default_version = "1.2.0" af = AsdfFile() assert af.version == AsdfVersion("1.2.0") assert af.version_string == "1.2.0" af = AsdfFile(version="1.3.0") assert af.version == AsdfVersion("1.3.0") assert af.version_string == "1.3.0" af = AsdfFile(version=AsdfVersion("1.3.0")) assert af.version == AsdfVersion("1.3.0") assert af.version_string == "1.3.0" with pytest.raises(ValueError): AsdfFile(version="0.5.4") with pytest.raises(ValueError): AsdfFile(version=AsdfVersion("0.5.4")) af = AsdfFile() af.version = "1.3.0" assert af.version == AsdfVersion("1.3.0") assert af.version_string == "1.3.0" af.version = AsdfVersion("1.4.0") assert af.version == AsdfVersion("1.4.0") assert af.version_string == "1.4.0" with pytest.raises(ValueError): af.version = "0.5.4" with pytest.raises(ValueError): af.version = AsdfVersion("2.5.4") af.version = "1.0.0" assert af.version_map["tags"][ "tag:stsci.edu:asdf/core/asdf"] == "1.0.0" af.version = "1.2.0" assert af.version_map["tags"][ "tag:stsci.edu:asdf/core/asdf"] == "1.1.0"
def to_tree_transform(cls, data, ctx): if cls.version < AsdfVersion('1.4.0'): if not isinstance(data, functional_models.Const1D): raise ValueError( f'constant-{cls.version} does not support models with > 1 dimension' ) return {'value': _parameter_to_value(data.amplitude)} else: if isinstance(data, functional_models.Const1D): dimension = 1 elif isinstance(data, functional_models.Const2D): dimension = 2 return { 'value': _parameter_to_value(data.amplitude), 'dimensions': dimension }
def to_tree_transform(self, model): if self.version < AsdfVersion('1.4.0'): if not isinstance(model, models.Const1D): raise ValueError( f'constant-{self.version} does not support models with > 1 dimension') return { 'value': _parameter_to_value(model.amplitude) } else: if isinstance(model, models.Const1D): dimension = 1 elif isinstance(model, models.Const2D): dimension = 2 return { 'value': _parameter_to_value(model.amplitude), 'dimensions': dimension }
def test_version_and_string_inequality(): version = AsdfVersion('2.0.0') assert version > '1.0.0' assert version > '1.0.1' assert version > '1.1.0' assert version > '1.1.1' assert (version > '2.0.0') == False assert (version < '2.0.0') == False assert version < '2.0.1' assert version < '2.1.0' assert version < '2.1.1' assert version >= '1.0.0' assert version >= '1.0.1' assert version >= '1.1.0' assert version >= '1.1.1' assert version >= '2.0.0' assert version <= '2.0.0' assert version <= '2.0.1' assert version <= '2.1.0' assert version <= '2.1.1' assert '1.0.0' < version assert '1.0.1' < version assert '1.1.0' < version assert '1.1.1' < version assert ('2.0.0' < version) == False assert ('2.0.0' > version) == False assert '2.0.1' > version assert '2.1.0' > version assert '2.1.1' > version assert '1.0.0' <= version assert '1.0.1' <= version assert '1.1.0' <= version assert '1.1.1' <= version assert '2.0.0' <= version assert '2.0.0' >= version assert '2.0.1' >= version assert '2.1.0' >= version assert '2.1.1' >= version
def test_version_and_tuple_inequality(): version = AsdfVersion('2.0.0') assert version > (1,0,0) assert version > (1,0,1) assert version > (1,1,0) assert version > (1,1,1) assert (version > (2,0,0)) == False assert (version < (2,0,0)) == False assert version < (2,0,1) assert version < (2,1,0) assert version < (2,1,1) assert version >= (1,0,0) assert version >= (1,0,1) assert version >= (1,1,0) assert version >= (1,1,1) assert version >= (2,0,0) assert version <= (2,0,0) assert version <= (2,0,1) assert version <= (2,1,0) assert version <= (2,1,1) assert (1,0,0) < version assert (1,0,1) < version assert (1,1,0) < version assert (1,1,1) < version assert ((2,0,0) < version) == False assert ((2,0,0) > version) == False assert (2,0,1) > version assert (2,1,0) > version assert (2,1,1) > version assert (1,0,0) <= version assert (1,0,1) <= version assert (1,1,0) <= version assert (1,1,1) <= version assert (2,0,0) <= version assert (2,0,0) >= version assert (2,0,1) >= version assert (2,1,0) >= version assert (2,1,1) >= version
def test_version_and_version_inequality(): ver0 = AsdfVersion('1.0.0') ver1 = AsdfVersion('1.0.1') ver2 = AsdfVersion('1.1.0') ver3 = AsdfVersion('1.1.1') ver4 = AsdfVersion('2.0.0') ver5 = AsdfVersion('2.0.1') ver6 = AsdfVersion('2.1.0') ver7 = AsdfVersion('2.1.1') versions = [ver0, ver1, ver2, ver3, ver4, ver5, ver6, ver7] for x,y in combinations(versions, 2): assert not (x == y) assert x != y assert ver0 < ver1 < ver2 < ver3 < ver4 < ver5 < ver6 < ver7 assert ver7 > ver6 > ver5 > ver4 > ver3 > ver2 > ver1 > ver0 assert (ver0 < ver1 < ver2 < ver4 < ver3 < ver5 < ver6 < ver7) == False assert (ver7 > ver6 > ver5 > ver3 > ver4 > ver2 > ver1 > ver0) == False assert ver0 <= ver1 <= ver2 <= ver3 <= ver4 <= ver5 <= ver6 <= ver7 assert ver7 >= ver6 >= ver5 >= ver4 >= ver3 >= ver2 >= ver1 >= ver0
def test_spec_version_filter(): spec = AsdfSpec('>=1.1.0') versions = [AsdfVersion(x) for x in ['1.0.0', '1.0.9', '1.1.0', '1.2.0']] for x,y in zip(spec.filter(versions), ['1.1.0', '1.2.0']): assert x == y
class PolynomialTypeBase(TransformType): DOMAIN_WINDOW_MIN_VERSION = AsdfVersion("1.2.0") name = "transform/polynomial" types = [ 'astropy.modeling.models.Polynomial1D', 'astropy.modeling.models.Polynomial2D' ] @classmethod def from_tree_transform(cls, node, ctx): coefficients = np.asarray(node['coefficients']) n_dim = coefficients.ndim if n_dim == 1: domain = node.get('domain', None) window = node.get('window', None) model = modeling.models.Polynomial1D(coefficients.size - 1, domain=domain, window=window) model.parameters = coefficients elif n_dim == 2: x_domain, y_domain = tuple(node.get('domain', (None, None))) x_window, y_window = tuple(node.get('window', (None, None))) shape = coefficients.shape degree = shape[0] - 1 if shape[0] != shape[1]: raise TypeError("Coefficients must be an (n+1, n+1) matrix") coeffs = {} for i in range(shape[0]): for j in range(shape[0]): if i + j < degree + 1: name = 'c' + str(i) + '_' + str(j) coeffs[name] = coefficients[i, j] model = modeling.models.Polynomial2D(degree, x_domain=x_domain, y_domain=y_domain, x_window=x_window, y_window=y_window, **coeffs) else: raise NotImplementedError( "Asdf currently only supports 1D or 2D polynomial transform.") return model @classmethod def to_tree_transform(cls, model, ctx): if isinstance(model, modeling.models.Polynomial1D): coefficients = np.array(model.parameters) elif isinstance(model, modeling.models.Polynomial2D): degree = model.degree coefficients = np.zeros((degree + 1, degree + 1)) for i in range(degree + 1): for j in range(degree + 1): if i + j < degree + 1: name = 'c' + str(i) + '_' + str(j) coefficients[i, j] = getattr(model, name).value node = {'coefficients': coefficients} typeindex = cls.types.index(model.__class__) ndim = (typeindex % 2) + 1 if cls.version >= PolynomialTypeBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 included an unrelated "domain" # property. We can't serialize the new domain values with those # versions because they don't validate. if ndim == 1: if model.domain is not None: node['domain'] = model.domain if model.window is not None: node['window'] = model.window else: if model.x_domain or model.y_domain is not None: node['domain'] = (model.x_domain, model.y_domain) if model.x_window or model.y_window is not None: node['window'] = (model.x_window, model.y_window) return node @classmethod def assert_equal(cls, a, b): # TODO: If models become comparable themselves, remove this. TransformType.assert_equal(a, b) assert (isinstance( a, (modeling.models.Polynomial1D, modeling.models.Polynomial2D)) and isinstance(b, (modeling.models.Polynomial1D, modeling.models.Polynomial2D))) assert_array_equal(a.parameters, b.parameters) if cls.version > PolynomialTypeBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 are known not to serialize # domain or window. if isinstance(a, modeling.models.Polynomial1D): assert a.domain == b.domain assert a.window == b.window else: assert a.x_domain == b.x_domain assert a.x_window == b.x_window assert a.y_domain == b.y_domain assert a.y_window == b.y_window
class PolynomialConverterBase(TransformConverter): DOMAIN_WINDOW_MIN_VERSION = AsdfVersion("1.2.0") tags = { "http://asdf-format.org/schemas/transform/polynomial-2.0.0", "tag:stsci.edu:asdf/transform/polynomial-1.0.0", "tag:stsci.edu:asdf/transform/polynomial-1.1.0", "tag:stsci.edu:asdf/transform/polynomial-1.2.0", } types = {models.Polynomial1D, models.Polynomial2D} def from_tree_transform(self, node): coefficients = np.asarray(node['coefficients']) n_dim = coefficients.ndim if n_dim == 1: domain = node.get('domain', None) window = node.get('window', None) model = models.Polynomial1D(coefficients.size - 1, domain=domain, window=window) model.parameters = coefficients elif n_dim == 2: x_domain, y_domain = tuple(node.get('domain', (None, None))) x_window, y_window = tuple(node.get('window', (None, None))) shape = coefficients.shape degree = shape[0] - 1 if shape[0] != shape[1]: raise TypeError("Coefficients must be an (n+1, n+1) matrix") coeffs = {} for i in range(shape[0]): for j in range(shape[0]): if i + j < degree + 1: name = 'c' + str(i) + '_' + str(j) coeffs[name] = coefficients[i, j] model = models.Polynomial2D(degree, x_domain=x_domain, y_domain=y_domain, x_window=x_window, y_window=y_window, **coeffs) else: raise NotImplementedError( "Asdf currently only supports 1D or 2D polynomial transform.") return model def to_tree_transform(self, model): if isinstance(model, models.Polynomial1D): coefficients = np.array(model.parameters) elif isinstance(model, models.Polynomial2D): degree = model.degree coefficients = np.zeros((degree + 1, degree + 1)) for i in range(degree + 1): for j in range(degree + 1): if i + j < degree + 1: name = 'c' + str(i) + '_' + str(j) coefficients[i, j] = getattr(model, name).value node = {'coefficients': coefficients} ndim = model.n_inputs if self.version >= PolynomialConverterBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 included an unrelated "domain" # property. We can't serialize the new domain values with those # versions because they don't validate. if ndim == 1: if model.domain is not None: node['domain'] = model.domain if model.window is not None: node['window'] = model.window else: if model.x_domain or model.y_domain is not None: node['domain'] = (model.x_domain, model.y_domain) if model.x_window or model.y_window is not None: node['window'] = (model.x_window, model.y_window) return node def assert_equal(self, a, b): # TODO: If models become comparable themselves, remove this. super().assert_equal(a, b) assert (isinstance(a, (models.Polynomial1D, models.Polynomial2D)) and isinstance(b, (models.Polynomial1D, models.Polynomial2D))) assert_array_equal(a.parameters, b.parameters) if self.version > PolynomialConverterBase.DOMAIN_WINDOW_MIN_VERSION: # Schema versions prior to 1.2 are known not to serialize # domain or window. if isinstance(a, models.Polynomial1D): assert a.domain == b.domain assert a.window == b.window else: assert a.x_domain == b.x_domain assert a.x_window == b.x_window assert a.y_domain == b.y_domain assert a.y_window == b.y_window