Beispiel #1
0
def test_unpickle():
    cmd = load.unpickle(cmd_path)
    assert cmd is not None, 'Unpickle returned None'
Beispiel #2
0
 def test_plot_pickle(self):
     cmd = unpickle(cmd_path)
     myplot = cmd.plot(log_age=7)
     myplot.set_xlabel('bla')
     del myplot
     del cmd
Beispiel #3
0
import hoki.age_utils as au
import hoki.load as load
import pkg_resources
import numpy as np
import pandas as pd
import pytest
from hoki.utils.exceptions import HokiFatalError, HokiUserWarning, HokiFormatError

# Loading Data

data_path = pkg_resources.resource_filename('hoki', 'data')
hr_file = data_path + '/hrs-sin-imf_chab100.zem4.dat'
cmd_file = data_path + '/cmd_bv_z002_bin_imf135_300'
myhrd = load.model_output(hr_file, hr_type='TL')
mycmd = load.unpickle(cmd_file)
# Creating Test Inputs

fake_hrd_input = pd.DataFrame.from_dict({
    'name': ['star1', 'star2', 'star3'],
    'logT': np.array([4.58, 4.48, 4.14]),
    'logL': np.array([4.83, 5.07, 5.40])
})

bad_hrd_input = pd.DataFrame.from_dict({
    'logT': np.array(['bla']),
    'logL': np.array([4.83])
})

no_name_input = pd.DataFrame.from_dict({
    'logT': np.array([4.58, 4.48, 4.14]),
    'logL': np.array([4.83, 5.07, 5.40])
Beispiel #4
0
    def __init__(self, obs_df, model):
        """
        Initialisation of the AgeWizard object

        Parameters
        ----------
        obs_df: pandas.DataFrame
            Observational data. MUST contain a logT and logL column (for HRD comparison) or a col and mag column
            (for CMD comparison)
        model: str or hoki.hrdiagrams.HRDiagrams() hoki.cmd.CMD()
            Location of the modeled HRD or CMD. This can be an already instanciated HRDiagram or CMD() object, or a
            path to an HR Diagram file or a pickled CMD.
        """

        # Making sure the osbervational properties are given in a format we can use.
        if not isinstance(obs_df, pd.DataFrame):
            raise HokiFormatError(
                "Observations should be stored in a Data Frame")

        if 'name' not in obs_df.columns:
            warnings.warn(
                "We expect the name of sources to be given in the 'name' column. "
                "If I can't find names I'll make my own ;)", HokiFormatWarning)

        # Checking what format they giving for the model:
        if isinstance(model, hoki.hrdiagrams.HRDiagram):
            self.model = model
        elif isinstance(model, hoki.cmd.CMD):
            self.model = model
        elif isinstance(model, str) and 'hrs' in model:
            self.model = load.model_output(model, hr_type='TL')
        elif isinstance(model, str):
            try:
                self.model = load.unpickle(path=model)
            except AssertionError:
                print('-----------------')
                print(
                    'HOKI DEBUGGER:\nThe model param should be a path to \na BPASS HRDiagram output file or pickled CMD,'
                    'or\na hoki.hrdiagrams.HRDiagram or a hoki.cmd.CMD')
                print('-----------------')
                raise HokiFatalError('model is ' + str(type(model)))

        else:
            print('-----------------')
            print(
                'HOKI DEBUGGER:\nThe model param should be a path to \na BPASS HRDiagram output file or pickled CMD,'
                'or\na hoki.hrdiagrams.HRDiagram or a hoki.cmd.CMD')
            print('-----------------')
            raise HokiFatalError('model is ' + str(type(model)))

        self.obs_df = obs_df
        self.coordinates = find_coordinates(self.obs_df, self.model)

        # This line is obsolete but might need revival if we ever want to add the not normalised distributions again
        # self._distributions = calculate_distributions_normalised(self.obs_df, self.model)

        self.pdfs = calculate_individual_pdfs(self.obs_df,
                                              self.model).fillna(0)
        self.sources = self.pdfs.columns.to_list()
        self.sample_pdf = None
        self._most_likely_age = None
Beispiel #5
0
    def __init__(self, obs_df, model, nsamples=100):
        """
        Initialisation of the AgeWizard object

        Parameters
        ----------
        obs_df: pandas.DataFrame
            Observational data. MUST contain a logT and logL column (for HRD comparison) or a col and mag column
            (for CMD comparison)
        model: str or hoki.hrdiagrams.HRDiagrams() hoki.cmd.CMD()
            Location of the modeled HRD or CMD. This can be an already instanciated HRDiagram or CMD() object, or a
            path to an HR Diagram file or a pickled CMD.
        nsamples: int, optional
            Number of times each data point should be sampled from its error distribution. Default is 100.
            This only matters if you are taking errors into account.
        """

        print(f"{Dialogue.info()} AgeWizard Starting")
        print(f"{Dialogue.running()} Initial Checks")

        # Making sure the osbervational properties are given in a format we can use.
        if not isinstance(obs_df, pd.DataFrame):
            raise HokiFormatError(
                "Observations should be stored in a Data Frame")

        if 'name' not in obs_df.columns:
            warnings.warn(
                "We expect the name of sources to be given in the 'name' column. "
                "If I can't find names I'll make my own ;)", HokiFormatWarning)

        # Checking what format they giving for the model:
        if isinstance(model, hoki.hrdiagrams.HRDiagram):
            self.model = model
        elif isinstance(model, hoki.cmd.CMD):
            self.model = model
        elif isinstance(model, str) and 'hrs' in model:
            self.model = load.model_output(model, hr_type='TL')
        elif isinstance(model, str):
            try:
                self.model = load.unpickle(path=model)
            except AssertionError:
                print(f'{Dialogue.ORANGE}-----------------{Dialogue.ENDC}')
                print(
                    f'{Dialogue.debugger()}\nThe model param should be a path to \na BPASS HRDiagram output file or pickled CMD,'
                    'or\na hoki.hrdiagrams.HRDiagram or a hoki.cmd.CMD')
                print(f'{Dialogue.ORANGE}-----------------{Dialogue.ENDC}')
                raise HokiFatalError('model is ' + str(type(model)))

        else:
            print(f'{Dialogue.ORANGE}-----------------{Dialogue.ENDC}')
            print(
                f'{Dialogue.debugger()}\nThe model param should be a path to \na BPASS HRDiagram output file or pickled CMD,'
                'or\na hoki.hrdiagrams.HRDiagram or a hoki.cmd.CMD')
            print(f'{Dialogue.ORANGE}-----------------{Dialogue.ENDC}')
            raise HokiFatalError('model is ' + str(type(model)))

        print(f"{Dialogue.complete()} Initial Checks")

        self.obs_df = obs_df.copy()

        # not needed?
        # self.coordinates = find_coordinates(self.obs_df, self.model)

        # This line is obsolete but might need revival if we ever want to add the not normalised distributions again
        # self._distributions = calculate_distributions_normalised(self.obs_df, self.model)

        self.pdfs = au.calculate_individual_pdfs(self.obs_df,
                                                 self.model,
                                                 nsamples=nsamples).fillna(0)
        self.sources = self.pdfs.columns.to_list()
        self.sample_pdf = None
        self._most_likely_age = None