Returns ------- `~astropy.cosmology.Cosmology` subclass instance Just ``cosmo`` passed through. """ return cosmo def cosmology_identify(origin, format, *args, **kwargs): """Identify if object is a `~astropy.cosmology.Cosmology`. Returns ------- bool """ itis = False if origin == "read": itis = isinstance( args[1], Cosmology) and (format in (None, "astropy.cosmology")) return itis # =================================================================== # Register convert_registry.register_reader("astropy.cosmology", Cosmology, from_cosmology) convert_registry.register_writer("astropy.cosmology", Cosmology, to_cosmology) convert_registry.register_identifier("astropy.cosmology", Cosmology, cosmology_identify)
ps = {n: getattr(cosmology, n) for n in params.keys()} model = CosmoModel(**ps, name=cosmology.name, meta=copy.deepcopy(cosmology.meta)) return model def model_identify(origin, format, *args, **kwargs): """Identify if object uses the :class:`~astropy.modeling.Model` format. Returns ------- bool """ itis = False if origin == "read": itis = isinstance(args[1], Model) and (format in (None, "astropy.model")) return itis # =================================================================== # Register convert_registry.register_reader("astropy.model", Cosmology, from_model) convert_registry.register_writer("astropy.model", Cosmology, to_model) convert_registry.register_identifier("astropy.model", Cosmology, model_identify)
""" return dump(cosmology) # ``read`` cannot handle non-path strings. # TODO! this says there should be different types of I/O registries. # not just hacking object conversion on top of file I/O. # def yaml_identify(origin, format, *args, **kwargs): # """Identify if object uses the yaml format. # # Returns # ------- # bool # """ # itis = False # if origin == "read": # itis = isinstance(args[1], str) and args[1][0].startswith("!") # itis &= format in (None, "yaml") # # return itis # =================================================================== # Register for cosmo_cls in _COSMOLOGY_CLASSES.values(): register_cosmology_yaml(cosmo_cls) convert_registry.register_reader("yaml", Cosmology, from_yaml) convert_registry.register_writer("yaml", Cosmology, to_yaml) # convert_registry.register_identifier("yaml", Cosmology, yaml_identify)
m["cosmology"] = cosmology.__class__ m["name"] = cosmology.name # here only for dict ordering # get all the immutable inputs m.update({k: v for k, v in cosmology._init_arguments.items() if k not in ("meta", "name")}) # add the mutable metadata m["meta"] = copy.deepcopy(cosmology.meta) return m def mapping_identify(origin, format, *args, **kwargs): """Identify if object uses the mapping format. Returns ------- bool """ itis = False if origin == "read": itis = isinstance(args[1], Mapping) and (format in (None, "mapping")) return itis # =================================================================== # Register convert_registry.register_reader("mapping", Cosmology, from_mapping) convert_registry.register_writer("mapping", Cosmology, to_mapping) convert_registry.register_identifier("mapping", Cosmology, mapping_identify)
col = convert_parameter_to_column(getattr(cosmo_cls, k), v, cosmology.meta.get(k)) else: col = Column([v]) data[k] = col tbl = cls(data, meta=meta) tbl.add_index("name", unique=True) return tbl def table_identify(origin, format, *args, **kwargs): """Identify if object uses the Table format. Returns ------- bool """ itis = False if origin == "read": itis = isinstance(args[1], Table) and (format in (None, "astropy.table")) return itis # =================================================================== # Register convert_registry.register_reader("astropy.table", Cosmology, from_table) convert_registry.register_writer("astropy.table", Cosmology, to_table) convert_registry.register_identifier("astropy.table", Cosmology, table_identify)
m["initial_matter_density"] = m.pop("Om0") m["initial_temperature"] = m.pop("Tcmb0").to_value(u.K) # m["Neff"] = m.pop("Neff") # skip b/c unchanged m["neutrino_masses"] = m.pop("m_nu").to_value(u.eV) m["initial_baryon_density"] = m.pop("Ob0") m["current_age"] = m.pop("t0", cosmology.age(0 * cu.redshift)).to_value(u.Gyr) # optional if "z_reion" in m: m["reionization_redshift"] = (m.pop("z_reion") << cu.redshift).value # ... # keep remapping return MyCosmology(**m) def mypackage_identify(origin, format, *args, **kwargs): """Identify if object uses format "mypackage".""" itis = False if origin == "read": itis = isinstance(args[1], MyCosmology) and (format in (None, "mypackage")) return itis # ------------------------------------------------------------------- # Register to/from_format & identify methods with Astropy Unified I/O convert_registry.register_reader("mypackage", Cosmology, from_mypackage, force=True) convert_registry.register_writer("mypackage", Cosmology, to_mypackage, force=True) convert_registry.register_identifier("mypackage", Cosmology, mypackage_identify, force=True)
FlatLambdaCDM Planck18 67.66 0.30966 2.7255 3.046 0.0 .. 0.06 0.04897 The cosmological class and other metadata, e.g. a paper reference, are in the Table's metadata. """ from .table import to_table table = to_table(cosmology, cls=table_cls, cosmology_in_meta=cosmology_in_meta) return table[0] # extract row from table def row_identify(origin, format, *args, **kwargs): """Identify if object uses the `~astropy.table.Row` format. Returns ------- bool """ itis = False if origin == "read": itis = isinstance(args[1], Row) and (format in (None, "astropy.row")) return itis # =================================================================== # Register convert_registry.register_reader("astropy.row", Cosmology, from_row) convert_registry.register_writer("astropy.row", Cosmology, to_row) convert_registry.register_identifier("astropy.row", Cosmology, row_identify)