Ejemplo n.º 1
0
def run_tardis(
    config,
    atom_data=None,
    packet_source=None,
    simulation_callbacks=[],
    virtual_packet_logging=False,
):
    """
    This function is one of the core functions to run TARDIS from a given
    config object.

    It will return a model object containing

    Parameters
    ----------
    config : str or dict or tardis.io.config_reader.Configuration
        filename of configuration yaml file or dictionary or TARDIS Configuration object
    atom_data : str or tardis.atomic.AtomData
        if atom_data is a string it is interpreted as a path to a file storing
        the atomic data. Atomic data to use for this TARDIS simulation. If set to None, the
        atomic data will be loaded according to keywords set in the configuration
        [default=None]
    virtual_packet_logging : bool
        option to enable virtual packet logging
        [default=False]

    Returns
    -------
    Simulation
    """
    from tardis.io.config_reader import Configuration
    from tardis.io.atom_data.base import AtomData
    from tardis.simulation import Simulation

    if atom_data is not None:
        try:
            atom_data = AtomData.from_hdf(atom_data)
        except TypeError:
            atom_data = atom_data

    if isinstance(config, Configuration):
        tardis_config = config
    else:
        try:
            tardis_config = Configuration.from_yaml(config)
        except TypeError:
            tardis_config = Configuration.from_config_dict(config)

    simulation = Simulation.from_config(
        tardis_config,
        packet_source=packet_source,
        atom_data=atom_data,
        virtual_packet_logging=virtual_packet_logging,
    )
    for cb in simulation_callbacks:
        simulation.add_callback(*cb)

    simulation.run()

    return simulation
Ejemplo n.º 2
0
def atomic_dataset(atomic_data_fname):
    atomic_data = AtomData.from_hdf(atomic_data_fname)

    if atomic_data.md5 != DEFAULT_ATOM_DATA_UUID:
        pytest.skip('Need default Kurucz atomic dataset (md5="{}"'.format(
            DEFAULT_ATOM_DATA_UUID))
    else:
        return atomic_data
Ejemplo n.º 3
0
def atomic_dataset(atomic_data_fname):
    atomic_data = AtomData.from_hdf(atomic_data_fname)

    if atomic_data.md5 != DEFAULT_ATOM_DATA_UUID:
        pytest.skip(
                'Need default Kurucz atomic dataset (md5="{}"'.format(
                    DEFAULT_ATOM_DATA_UUID))
    else:
        return atomic_data
Ejemplo n.º 4
0
def run_tardis(config,
               atom_data=None,
               packet_source=None,
               simulation_callbacks=[]):
    """
    This function is one of the core functions to run TARDIS from a given
    config object.

    It will return a model object containing

    Parameters
    ----------

    config: ~str or ~dict
        filename of configuration yaml file or dictionary

    atom_data: ~str or ~tardis.atomic.AtomData
        if atom_data is a string it is interpreted as a path to a file storing
        the atomic data. Atomic data to use for this TARDIS simulation. If set to None, the
        atomic data will be loaded according to keywords set in the configuration
        [default=None]
    """
    from tardis.io.config_reader import Configuration
    from tardis.io.atom_data.base import AtomData
    from tardis.simulation import Simulation

    if atom_data is not None:
        try:
            atom_data = AtomData.from_hdf(atom_data)
        except TypeError:
            atom_data = atom_data

    try:
        tardis_config = Configuration.from_yaml(config)
    except TypeError:
        tardis_config = Configuration.from_config_dict(config)

    simulation = Simulation.from_config(tardis_config,
                                        packet_source=packet_source,
                                        atom_data=atom_data)
    for cb in simulation_callbacks:
        simulation.add_callback(*cb)

    simulation.run()

    return simulation
Ejemplo n.º 5
0
def run_tardis(config, atom_data=None, simulation_callbacks=[]):
    """
    This function is one of the core functions to run TARDIS from a given
    config object.

    It will return a model object containing

    Parameters
    ----------

    config: ~str or ~dict
        filename of configuration yaml file or dictionary

    atom_data: ~str or ~tardis.atomic.AtomData
        if atom_data is a string it is interpreted as a path to a file storing
        the atomic data. Atomic data to use for this TARDIS simulation. If set to None, the
        atomic data will be loaded according to keywords set in the configuration
        [default=None]
    """
    from tardis.io.config_reader import Configuration
    from tardis.io.atom_data.base import AtomData
    from tardis.simulation import Simulation

    if atom_data is not None:
        try:
            atom_data = AtomData.from_hdf(atom_data)
        except TypeError:
            atom_data = atom_data

    try:
        tardis_config = Configuration.from_yaml(config)
    except TypeError:
        tardis_config = Configuration.from_config_dict(config)

    simulation = Simulation.from_config(tardis_config, atom_data=atom_data)
    for cb in simulation_callbacks:
        simulation.add_callback(cb)

    simulation.run()

    return simulation
Ejemplo n.º 6
0
    def setup(self, request, reference, data_path, pytestconfig):
        """
        This method does initial setup of creating configuration and performing
        a single run of integration test.
        """
        # Get capture manager
        capmanager = pytestconfig.pluginmanager.getplugin('capturemanager')

        # The last component in dirpath can be extracted as name of setup.
        self.name = data_path['setup_name']

        self.config_file = os.path.join(data_path['config_dirpath'], "config.yml")

        # A quick hack to use atom data per setup. Atom data is ingested from
        # local HDF or downloaded and cached from a url, depending on data_path
        # keys.
        atom_data_name = yaml.load(
            open(self.config_file), Loader=yaml.CLoader)['atom_data']

        # Get the path to HDF file:
        atom_data_filepath = os.path.join(
            data_path['atom_data_path'], atom_data_name
        )

        # Load atom data file separately, pass it for forming tardis config.
        self.atom_data = AtomData.from_hdf(atom_data_filepath)

        # Check whether the atom data file in current run and the atom data
        # file used in obtaining the reference data are same.
        # TODO: hard coded UUID for kurucz atom data file, generalize it later.
        # kurucz_data_file_uuid1 = "5ca3035ca8b311e3bb684437e69d75d7"
        # assert self.atom_data.uuid1 == kurucz_data_file_uuid1

        # Create a Configuration through yaml file and atom data.
        tardis_config = Configuration.from_yaml(self.config_file)

        # Check whether current run is with less packets.
        if request.config.getoption("--less-packets"):
            less_packets = request.config.integration_tests_config['less_packets']
            tardis_config['montecarlo']['no_of_packets'] = (
                less_packets['no_of_packets']
            )
            tardis_config['montecarlo']['last_no_of_packets'] = (
                less_packets['last_no_of_packets']
            )




        # We now do a run with prepared config and get the simulation object.
        self.result = Simulation.from_config(tardis_config,
                                             atom_data=self.atom_data)
        capmanager.suspend_global_capture(True)

        # If current test run is just for collecting reference data, store the
        # output model to HDF file, save it at specified path. Skip all tests.
        # Else simply perform the run and move further for performing
        # assertions.
        self.result.run()
        if request.config.getoption("--generate-reference"):
            ref_data_path = os.path.join(
                data_path['reference_path'], "{0}.h5".format(self.name)
            )
            if os.path.exists(ref_data_path):
                pytest.skip(
                    'Reference data {0} does exist and tests will not '
                    'proceed generating new data'.format(ref_data_path))
            self.result.to_hdf(file_path=ref_data_path)
            pytest.skip("Reference data saved at {0}".format(
                data_path['reference_path']
            ))
        capmanager.resume_global_capture()

        # Get the reference data through the fixture.
        self.reference = reference
Ejemplo n.º 7
0
def run_tardis(
    config,
    atom_data=None,
    packet_source=None,
    simulation_callbacks=[],
    virtual_packet_logging=False,
    show_cplots=True,
    log_level=None,
    specific_log_level=None,
    **kwargs,
):
    """
    Run TARDIS from a given config object.

    It will return a model object containing the TARDIS Simulation.

    Parameters
    ----------
    config : str or dict or tardis.io.config_reader.Configuration
        filename of configuration yaml file or dictionary or TARDIS Configuration object
    atom_data : str or tardis.atomic.AtomData, optional
        If atom_data is a string it is interpreted as a path to a file storing
        the atomic data. Atomic data to use for this TARDIS simulation. If set to None (i.e. default),
        the atomic data will be loaded according to keywords set in the configuration
    packet_source : class, optional
        A custom packet source class or a child class of `tardis.montecarlo.packet_source`
        used to override the TARDIS `BasePacketSource` class.
    simulation_callbacks : list of lists, default: `[]`, optional
        Set of callbacks to call at the end of every iteration of the Simulation.
        The format of the lists should look like:
        [[callback1, callback_arg1], [callback2, callback_arg2], ...],
        where the callback function signature should look like:
        callback_function(simulation, extra_arg1, ...)
    virtual_packet_logging : bool, default: False, optional
        Option to enable virtual packet logging.
    log_level : {'NOTSET', 'DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'}, default: None, optional
        Set the level of the TARDIS logger (follows native python logging framework log levels).
        Use this parameter to override the `log_level` specified in the configuration file.
        The default value `None` means that the `log_level` specified in the configuration file will be used.
    specific_log_level : bool, default: None, optional
        Allows to set specific logging levels, overriding the value in the configuration file.
        If True, only show the log messages from a particular log level, set by `log_level`.
        If False, the logger shows log messages belonging to the level set and all levels above it in severity.
        The default value None means that the `specific_log_level` specified in the configuration file will be used.
    show_cplots : bool, default: True, optional
        Option to enable tardis convergence plots.
    **kwargs : dict, optional
        Optional keyword arguments including those
        supported by :obj:`tardis.visualization.tools.convergence_plot.ConvergencePlots`.


    Returns
    -------
    tardis.simulation.Simulation

    Notes
    -----
    Please see the `logging tutorial <https://tardis-sn.github.io/tardis/io/optional/logging_configuration.html>`_ to know more about `log_level` and `specific` options.
    """
    from tardis.io.logger.logger import logging_state
    from tardis.io.config_reader import Configuration
    from tardis.io.atom_data.base import AtomData
    from tardis.simulation import Simulation

    if isinstance(config, Configuration):
        tardis_config = config
    else:
        try:
            tardis_config = Configuration.from_yaml(config)
        except TypeError:
            logger.debug(
                "TARDIS Config not available via YAML. Reading through TARDIS Config Dictionary"
            )
            tardis_config = Configuration.from_config_dict(config)

    if not isinstance(show_cplots, bool):
        raise TypeError("Expected bool in show_cplots argument")

    logging_state(log_level, tardis_config, specific_log_level)

    if atom_data is not None:
        try:
            atom_data = AtomData.from_hdf(atom_data)
        except TypeError:
            logger.debug(
                "Atom Data Cannot be Read from HDF. Setting to Default Atom Data"
            )
            atom_data = atom_data

    simulation = Simulation.from_config(
        tardis_config,
        packet_source=packet_source,
        atom_data=atom_data,
        virtual_packet_logging=virtual_packet_logging,
        show_cplots=show_cplots,
        **kwargs,
    )
    for cb in simulation_callbacks:
        simulation.add_callback(*cb)

    simulation.run()

    return simulation
Ejemplo n.º 8
0
    def setup(self, request, reference, data_path, pytestconfig):
        """
        This method does initial setup of creating configuration and performing
        a single run of integration test.
        """
        # Get capture manager
        capmanager = pytestconfig.pluginmanager.getplugin('capturemanager')

        # The last component in dirpath can be extracted as name of setup.
        self.name = data_path['setup_name']

        self.config_file = os.path.join(data_path['config_dirpath'], "config.yml")

        # A quick hack to use atom data per setup. Atom data is ingested from
        # local HDF or downloaded and cached from a url, depending on data_path
        # keys.
        atom_data_name = yaml.load(open(self.config_file))['atom_data']

        # Get the path to HDF file:
        atom_data_filepath = os.path.join(
            data_path['atom_data_path'], atom_data_name
        )

        # Load atom data file separately, pass it for forming tardis config.
        self.atom_data = AtomData.from_hdf(atom_data_filepath)

        # Check whether the atom data file in current run and the atom data
        # file used in obtaining the reference data are same.
        # TODO: hard coded UUID for kurucz atom data file, generalize it later.
        # kurucz_data_file_uuid1 = "5ca3035ca8b311e3bb684437e69d75d7"
        # assert self.atom_data.uuid1 == kurucz_data_file_uuid1

        # Create a Configuration through yaml file and atom data.
        tardis_config = Configuration.from_yaml(self.config_file)

        # Check whether current run is with less packets.
        if request.config.getoption("--less-packets"):
            less_packets = request.config.integration_tests_config['less_packets']
            tardis_config['montecarlo']['no_of_packets'] = (
                less_packets['no_of_packets']
            )
            tardis_config['montecarlo']['last_no_of_packets'] = (
                less_packets['last_no_of_packets']
            )




        # We now do a run with prepared config and get the simulation object.
        self.result = Simulation.from_config(tardis_config,
                                             atom_data=self.atom_data)

        capmanager.suspendcapture(True)
        # If current test run is just for collecting reference data, store the
        # output model to HDF file, save it at specified path. Skip all tests.
        # Else simply perform the run and move further for performing
        # assertions.
        self.result.run()
        if request.config.getoption("--generate-reference"):
            ref_data_path = os.path.join(
                data_path['reference_path'], "{0}.h5".format(self.name)
            )
            if os.path.exists(ref_data_path):
                pytest.skip(
                    'Reference data {0} does exist and tests will not '
                    'proceed generating new data'.format(ref_data_path))
            self.result.to_hdf(file_path=ref_data_path)
            pytest.skip("Reference data saved at {0}".format(
                data_path['reference_path']
            ))
        capmanager.resumecapture()

        # Get the reference data through the fixture.
        self.reference = reference