Esempio n. 1
0
def test_csv_colnames_equiv_datatype_fields(csvy_full_fname):
    yaml_dict, csv = csvy.load_csvy(csvy_full_fname)
    datatype_names = [od["name"] for od in yaml_dict["datatype"]["fields"]]
    for key in csv.columns:
        assert key in datatype_names
    for name in datatype_names:
        assert name in csv.columns
Esempio n. 2
0
def test_missing_required_property(csvy_missing_fname):
    yaml_dict, csv = csvy.load_csvy(csvy_missing_fname)
    with pytest.raises(Exception):
        vy = validate_dict(yaml_dict,
                           schemapath=os.path.join(tardis.__path__[0], 'io',
                                                   'schemas',
                                                   'csvy_model.yml'))
Esempio n. 3
0
def test_csv_colnames_equiv_datatype_fields(csvy_full_fname):
    yaml_dict, csv = csvy.load_csvy(csvy_full_fname)
    datatype_names = [od['name'] for od in yaml_dict['datatype']['fields']]
    for key in csv.columns:
        assert key in datatype_names
    for name in datatype_names:
        assert name in csv.columns
Esempio n. 4
0
    def from_csvy(cls, config):
        """
        Create a new Radial1DModel instance from a Configuration object.

        Parameters
        ----------
        config : tardis.io.config_reader.Configuration

        Returns
        -------
        Radial1DModel

        """
        if os.path.isabs(config.csvy_model):
            csvy_model_fname = config.csvy_model
        else:
            csvy_model_fname = os.path.join(config.config_dirname,
                                            config.csvy_model)
        csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname)
        base_dir = os.path.abspath(os.path.dirname(__file__))
        schema_dir = os.path.join(base_dir, '..', 'io', 'schemas')
        csvy_schema_file = os.path.join(schema_dir, 'csvy_model.yml')
        csvy_model_config = Configuration(
            validate_dict(csvy_model_config, schemapath=csvy_schema_file))

        time_explosion = config.supernova.time_explosion.cgs

        electron_densities = None
        temperature = None

        if hasattr(csvy_model_config, 'v_inner_boundary'):
            v_boundary_inner = csvy_model_config.v_inner_boundary
        else:
            v_boundary_inner = None

        if hasattr(csvy_model_config, 'v_outer_boundary'):
            v_boundary_outer = csvy_model_config.v_outer_boundary
        else:
            v_boundary_outer = None

        if hasattr(csvy_model_config, 'velocity'):
            velocity = quantity_linspace(csvy_model_config.velocity.start,
                                         csvy_model_config.velocity.stop,
                                         csvy_model_config.velocity.num +
                                         1).cgs
        else:
            velocity_field_index = [
                field['name'] for field in csvy_model_config.datatype.fields
            ].index('velocity')
            velocity_unit = u.Unit(
                csvy_model_config.datatype.fields[velocity_field_index]
                ['unit'])
            velocity = csvy_model_data['velocity'].values * velocity_unit
            velocity = velocity.to('cm/s')

        if hasattr(csvy_model_config, 'density'):
            homologous_density = HomologousDensity.from_csvy(
                config, csvy_model_config)
        else:
            time_0 = csvy_model_config.model_density_time_0
            density_field_index = [
                field['name'] for field in csvy_model_config.datatype.fields
            ].index('density')
            density_unit = u.Unit(
                csvy_model_config.datatype.fields[density_field_index]['unit'])
            density_0 = csvy_model_data['density'].values * density_unit
            density_0 = density_0.to('g/cm^3')[1:]
            density_0 = density_0.insert(0, 0)
            homologous_density = HomologousDensity(density_0, time_0)

        no_of_shells = len(velocity) - 1

        # TODO -- implement t_radiative
        #t_radiative = None
        if temperature:
            t_radiative = temperature
        elif hasattr(csvy_model_data, 'columns'):
            if 't_rad' in csvy_model_data.columns:
                t_rad_field_index = [
                    field['name']
                    for field in csvy_model_config.datatype.fields
                ].index('t_rad')
                t_rad_unit = u.Unit(
                    csvy_model_config.datatype.fields[t_rad_field_index]
                    ['unit'])
                t_radiative = csvy_model_data['t_rad'].iloc[
                    0:].values * t_rad_unit
            else:
                t_radiative = None

        dilution_factor = None
        if hasattr(csvy_model_data, 'columns'):
            if 'w' in csvy_model_data.columns:
                dilution_factor = csvy_model_data['w'].iloc[0:].to_numpy()

        elif config.plasma.initial_t_rad > 0 * u.K:
            t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad
        else:
            t_radiative = None

        if config.plasma.initial_t_inner < 0.0 * u.K:
            luminosity_requested = config.supernova.luminosity_requested
            t_inner = None
        else:
            luminosity_requested = None
            t_inner = config.plasma.initial_t_inner

        if hasattr(csvy_model_config, 'abundance'):
            abundances_section = csvy_model_config.abundance
            abundance, isotope_abundance = read_uniform_abundances(
                abundances_section, no_of_shells)
        else:
            index, abundance, isotope_abundance = parse_csv_abundances(
                csvy_model_data)

        abundance = abundance.replace(np.nan, 0.0)
        abundance = abundance[abundance.sum(axis=1) > 0]

        norm_factor = abundance.sum(axis=0) + isotope_abundance.sum(axis=0)

        if np.any(np.abs(norm_factor - 1) > 1e-12):
            logger.warning("Abundances have not been normalized to 1."
                           " - normalizing")
            abundance /= norm_factor
            isotope_abundance /= norm_factor

        #isotope_abundance = IsotopeAbundances(isotope_abundance)
        isotope_abundance = IsotopeAbundances(
            isotope_abundance, time_0=csvy_model_config.model_isotope_time_0)
        #isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0

        return cls(velocity=velocity,
                   homologous_density=homologous_density,
                   abundance=abundance,
                   isotope_abundance=isotope_abundance,
                   time_explosion=time_explosion,
                   t_radiative=t_radiative,
                   t_inner=t_inner,
                   luminosity_requested=luminosity_requested,
                   dilution_factor=dilution_factor,
                   v_boundary_inner=v_boundary_inner,
                   v_boundary_outer=v_boundary_outer,
                   electron_densities=electron_densities)
Esempio n. 5
0
def test_csvy_nocsv_data_is_none(csvy_nocsv_fname):
    yaml_dict, csv = csvy.load_csvy(csvy_nocsv_fname)
    assert csv is None
Esempio n. 6
0
def test_csvy_finds_csv_first_line(csvy_full_fname):
    yaml_dict, csv = csvy.load_csvy(csvy_full_fname)
    npt.assert_almost_equal(csv["velocity"][0], 10000)
Esempio n. 7
0
    def from_csvy(cls, config):
        """
        Create a new Radial1DModel instance from a Configuration object.

        Parameters
        ----------
        config : tardis.io.config_reader.Configuration

        Returns
        -------
        Radial1DModel

        """
        CSVY_SUPPORTED_COLUMNS = {
            'velocity', 'density', 't_rad', 'dilution_factor'
        }

        if os.path.isabs(config.csvy_model):
            csvy_model_fname = config.csvy_model
        else:
            csvy_model_fname = os.path.join(config.config_dirname,
                                            config.csvy_model)
        csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname)
        base_dir = os.path.abspath(os.path.dirname(__file__))
        schema_dir = os.path.join(base_dir, '..', 'io', 'schemas')
        csvy_schema_file = os.path.join(schema_dir, 'csvy_model.yml')
        csvy_model_config = Configuration(
            validate_dict(csvy_model_config, schemapath=csvy_schema_file))

        if hasattr(csvy_model_data, 'columns'):
            abund_names = set([
                name for name in csvy_model_data.columns
                if nucname.iselement(name) or nucname.isnuclide(name)
            ])
            unsupported_columns = set(
                csvy_model_data.columns) - abund_names - CSVY_SUPPORTED_COLUMNS

            field_names = set(
                [field['name'] for field in csvy_model_config.datatype.fields])
            assert set(csvy_model_data.columns) - field_names == set(),\
                'CSVY columns exist without field descriptions'
            assert field_names - set(csvy_model_data.columns) == set(),\
                'CSVY field descriptions exist without corresponding csv data'
            if unsupported_columns != set():
                logger.warning(
                    "The following columns are specified in the csvy"
                    "model file, but are IGNORED by TARDIS: %s" %
                    (str(unsupported_columns)))

        time_explosion = config.supernova.time_explosion.cgs

        electron_densities = None
        temperature = None

        #if hasattr(csvy_model_config, 'v_inner_boundary'):
        #    v_boundary_inner = csvy_model_config.v_inner_boundary
        #else:
        #    v_boundary_inner = None

        #if hasattr(csvy_model_config, 'v_outer_boundary'):
        #    v_boundary_outer = csvy_model_config.v_outer_boundary
        #else:
        #    v_boundary_outer = None

        if hasattr(config, 'model'):
            if hasattr(config.model, 'v_inner_boundary'):
                v_boundary_inner = config.model.v_inner_boundary
            else:
                v_boundary_inner = None

            if hasattr(config.model, 'v_outer_boundary'):
                v_boundary_outer = config.model.v_outer_boundary
            else:
                v_boundary_outer = None
        else:
            v_boundary_inner = None
            v_boundary_outer = None

        if hasattr(csvy_model_config, 'velocity'):
            velocity = quantity_linspace(csvy_model_config.velocity.start,
                                         csvy_model_config.velocity.stop,
                                         csvy_model_config.velocity.num +
                                         1).cgs
        else:
            velocity_field_index = [
                field['name'] for field in csvy_model_config.datatype.fields
            ].index('velocity')
            velocity_unit = u.Unit(
                csvy_model_config.datatype.fields[velocity_field_index]
                ['unit'])
            velocity = csvy_model_data['velocity'].values * velocity_unit
            velocity = velocity.to('cm/s')

        if hasattr(csvy_model_config, 'density'):
            homologous_density = HomologousDensity.from_csvy(
                config, csvy_model_config)
        else:
            time_0 = csvy_model_config.model_density_time_0
            density_field_index = [
                field['name'] for field in csvy_model_config.datatype.fields
            ].index('density')
            density_unit = u.Unit(
                csvy_model_config.datatype.fields[density_field_index]['unit'])
            density_0 = csvy_model_data['density'].values * density_unit
            density_0 = density_0.to('g/cm^3')[1:]
            density_0 = density_0.insert(0, 0)
            homologous_density = HomologousDensity(density_0, time_0)

        no_of_shells = len(velocity) - 1

        # TODO -- implement t_radiative
        #t_radiative = None
        if temperature:
            t_radiative = temperature
        elif hasattr(csvy_model_data, 'columns'):
            if 't_rad' in csvy_model_data.columns:
                t_rad_field_index = [
                    field['name']
                    for field in csvy_model_config.datatype.fields
                ].index('t_rad')
                t_rad_unit = u.Unit(
                    csvy_model_config.datatype.fields[t_rad_field_index]
                    ['unit'])
                t_radiative = csvy_model_data['t_rad'].iloc[
                    0:].values * t_rad_unit
            else:
                t_radiative = None

        dilution_factor = None
        if hasattr(csvy_model_data, 'columns'):
            if 'dilution_factor' in csvy_model_data.columns:
                dilution_factor = csvy_model_data['dilution_factor'].iloc[
                    0:].to_numpy()

        elif config.plasma.initial_t_rad > 0 * u.K:
            t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad
        else:
            t_radiative = None

        if config.plasma.initial_t_inner < 0.0 * u.K:
            luminosity_requested = config.supernova.luminosity_requested
            t_inner = None
        else:
            luminosity_requested = None
            t_inner = config.plasma.initial_t_inner

        if hasattr(csvy_model_config, 'abundance'):
            abundances_section = csvy_model_config.abundance
            abundance, isotope_abundance = read_uniform_abundances(
                abundances_section, no_of_shells)
        else:
            index, abundance, isotope_abundance = parse_csv_abundances(
                csvy_model_data)

        abundance = abundance.replace(np.nan, 0.0)
        abundance = abundance[abundance.sum(axis=1) > 0]
        abundance = abundance.loc[:, 1:]
        abundance.columns = np.arange(abundance.shape[1])

        norm_factor = abundance.sum(axis=0) + isotope_abundance.sum(axis=0)

        if np.any(np.abs(norm_factor - 1) > 1e-12):
            logger.warning("Abundances have not been normalized to 1."
                           " - normalizing")
            abundance /= norm_factor
            isotope_abundance /= norm_factor

        #isotope_abundance = IsotopeAbundances(isotope_abundance)
        isotope_abundance = IsotopeAbundances(
            isotope_abundance, time_0=csvy_model_config.model_isotope_time_0)
        #isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0

        return cls(velocity=velocity,
                   homologous_density=homologous_density,
                   abundance=abundance,
                   isotope_abundance=isotope_abundance,
                   time_explosion=time_explosion,
                   t_radiative=t_radiative,
                   t_inner=t_inner,
                   luminosity_requested=luminosity_requested,
                   dilution_factor=dilution_factor,
                   v_boundary_inner=v_boundary_inner,
                   v_boundary_outer=v_boundary_outer,
                   electron_densities=electron_densities)
Esempio n. 8
0
    def from_csvy(cls, config):
        """
        Create a new Radial1DModel instance from a Configuration object.

        Parameters
        ----------
        config : tardis.io.config_reader.Configuration

        Returns
        -------
        Radial1DModel
        """
        CSVY_SUPPORTED_COLUMNS = {
            "velocity",
            "density",
            "t_rad",
            "dilution_factor",
        }

        if os.path.isabs(config.csvy_model):
            csvy_model_fname = config.csvy_model
        else:
            csvy_model_fname = os.path.join(
                config.config_dirname, config.csvy_model
            )
        csvy_model_config, csvy_model_data = load_csvy(csvy_model_fname)
        base_dir = os.path.abspath(os.path.dirname(__file__))
        schema_dir = os.path.join(base_dir, "..", "io", "schemas")
        csvy_schema_file = os.path.join(schema_dir, "csvy_model.yml")
        csvy_model_config = Configuration(
            validate_dict(csvy_model_config, schemapath=csvy_schema_file)
        )

        if hasattr(csvy_model_data, "columns"):
            abund_names = set(
                [
                    name
                    for name in csvy_model_data.columns
                    if is_valid_nuclide_or_elem(name)
                ]
            )
            unsupported_columns = (
                set(csvy_model_data.columns)
                - abund_names
                - CSVY_SUPPORTED_COLUMNS
            )

            field_names = set(
                [field["name"] for field in csvy_model_config.datatype.fields]
            )
            assert (
                set(csvy_model_data.columns) - field_names == set()
            ), "CSVY columns exist without field descriptions"
            assert (
                field_names - set(csvy_model_data.columns) == set()
            ), "CSVY field descriptions exist without corresponding csv data"
            if unsupported_columns != set():
                logger.warning(
                    "The following columns are "
                    "specified in the csvy model file,"
                    f" but are IGNORED by TARDIS: {str(unsupported_columns)}"
                )

        time_explosion = config.supernova.time_explosion.cgs

        electron_densities = None
        temperature = None

        # if hasattr(csvy_model_config, 'v_inner_boundary'):
        #    v_boundary_inner = csvy_model_config.v_inner_boundary
        # else:
        #    v_boundary_inner = None

        # if hasattr(csvy_model_config, 'v_outer_boundary'):
        #    v_boundary_outer = csvy_model_config.v_outer_boundary
        # else:
        #    v_boundary_outer = None

        if hasattr(config, "model"):
            if hasattr(config.model, "v_inner_boundary"):
                v_boundary_inner = config.model.v_inner_boundary
            else:
                v_boundary_inner = None

            if hasattr(config.model, "v_outer_boundary"):
                v_boundary_outer = config.model.v_outer_boundary
            else:
                v_boundary_outer = None
        else:
            v_boundary_inner = None
            v_boundary_outer = None

        if hasattr(csvy_model_config, "velocity"):
            velocity = quantity_linspace(
                csvy_model_config.velocity.start,
                csvy_model_config.velocity.stop,
                csvy_model_config.velocity.num + 1,
            ).cgs
        else:
            velocity_field_index = [
                field["name"] for field in csvy_model_config.datatype.fields
            ].index("velocity")
            velocity_unit = u.Unit(
                csvy_model_config.datatype.fields[velocity_field_index]["unit"]
            )
            velocity = csvy_model_data["velocity"].values * velocity_unit
            velocity = velocity.to("cm/s")

        if hasattr(csvy_model_config, "density"):
            homologous_density = HomologousDensity.from_csvy(
                config, csvy_model_config
            )
        else:
            time_0 = csvy_model_config.model_density_time_0
            density_field_index = [
                field["name"] for field in csvy_model_config.datatype.fields
            ].index("density")
            density_unit = u.Unit(
                csvy_model_config.datatype.fields[density_field_index]["unit"]
            )
            density_0 = csvy_model_data["density"].values * density_unit
            density_0 = density_0.to("g/cm^3")[1:]
            density_0 = density_0.insert(0, 0)
            homologous_density = HomologousDensity(density_0, time_0)

        no_of_shells = len(velocity) - 1

        # TODO -- implement t_radiative
        # t_radiative = None
        if temperature:
            t_radiative = temperature
        elif hasattr(csvy_model_data, "columns"):
            if "t_rad" in csvy_model_data.columns:
                t_rad_field_index = [
                    field["name"] for field in csvy_model_config.datatype.fields
                ].index("t_rad")
                t_rad_unit = u.Unit(
                    csvy_model_config.datatype.fields[t_rad_field_index]["unit"]
                )
                t_radiative = (
                    csvy_model_data["t_rad"].iloc[0:].values * t_rad_unit
                )
            else:
                t_radiative = None

        dilution_factor = None
        if hasattr(csvy_model_data, "columns"):
            if "dilution_factor" in csvy_model_data.columns:
                dilution_factor = (
                    csvy_model_data["dilution_factor"].iloc[0:].to_numpy()
                )

        elif config.plasma.initial_t_rad > 0 * u.K:
            t_radiative = np.ones(no_of_shells) * config.plasma.initial_t_rad
        else:
            t_radiative = None

        if config.plasma.initial_t_inner < 0.0 * u.K:
            luminosity_requested = config.supernova.luminosity_requested
            t_inner = None
        else:
            luminosity_requested = None
            t_inner = config.plasma.initial_t_inner

        if hasattr(csvy_model_config, "abundance"):
            abundances_section = csvy_model_config.abundance
            abundance, isotope_abundance = read_uniform_abundances(
                abundances_section, no_of_shells
            )
        else:
            index, abundance, isotope_abundance = parse_csv_abundances(
                csvy_model_data
            )
            abundance = abundance.loc[:, 1:]
            abundance.columns = np.arange(abundance.shape[1])
            isotope_abundance = isotope_abundance.loc[:, 1:]
            isotope_abundance.columns = np.arange(isotope_abundance.shape[1])

        abundance = abundance.replace(np.nan, 0.0)
        abundance = abundance[abundance.sum(axis=1) > 0]
        isotope_abundance = isotope_abundance.replace(np.nan, 0.0)
        isotope_abundance = isotope_abundance[isotope_abundance.sum(axis=1) > 0]
        norm_factor = abundance.sum(axis=0) + isotope_abundance.sum(axis=0)

        if np.any(np.abs(norm_factor - 1) > 1e-12):
            logger.warning(
                "Abundances have not been normalized to 1." " - normalizing"
            )
            abundance /= norm_factor
            isotope_abundance /= norm_factor

        # isotope_abundance = IsotopeAbundances(isotope_abundance)
        isotope_abundance = IsotopeAbundances(
            isotope_abundance, time_0=csvy_model_config.model_isotope_time_0
        )
        # isotope_abundance.time_0 = csvy_model_config.model_isotope_time_0

        return cls(
            velocity=velocity,
            homologous_density=homologous_density,
            abundance=abundance,
            isotope_abundance=isotope_abundance,
            time_explosion=time_explosion,
            t_radiative=t_radiative,
            t_inner=t_inner,
            luminosity_requested=luminosity_requested,
            dilution_factor=dilution_factor,
            v_boundary_inner=v_boundary_inner,
            v_boundary_outer=v_boundary_outer,
            electron_densities=electron_densities,
        )