def test_set_with_config_item_string_item_access_quantity(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.set_config_item('a.b.param2.item2', 7) item = config_ns.get_config_item('a.b.param2.item2') assert_almost_equal(item, 7 * u.km)
def test_set_with_config_item_string_item_access(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.set_config_item('a.b.param2.item0', 2) item = config_ns.get_config_item('a.b.param2.item0') assert item == 2
def test_set_with_config_item_string_item_access_quantity(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.set_config_item('a.b.param2.item2', 7 ) item = config_ns.get_config_item('a.b.param2.item2') assert_almost_equal(item ,7*u.km)
def _generate_param_class_dict(fname, param_names): """ Generate the parameter and class dictionaries for the Model Parameters ---------- fname: ~str param_names: ~list of ~str Returns ------- """ config = ConfigurationNameSpace.from_yaml(fname) class_dict = {} param_dict = {} short_param_name_dict = _convert_param_names(param_names) class_dict['convert_param_dict'] = short_param_name_dict for key in short_param_name_dict: try: value = config.get_config_item(short_param_name_dict[key]) except KeyError: raise ValueError('{0} is not a valid parameter'.format(key)) else: pass class_dict[key] = Parameter() param_dict[key] = getattr(value, 'value', value) return class_dict, param_dict, config
def test_quantity_configuration_namespace(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.a.b.param3 = 3 assert_almost_equal(config_ns["a"]["b"]["param3"].to(u.km).value, 3) config_ns.a.b.param3 = 5000 * u.m assert_almost_equal(config_ns["a"]["b"]["param3"].to(u.km).value, 5)
def test_simple_configuration_namespace(): config_ns = ConfigurationNameSpace(simple_config_dict) assert config_ns.a.b.param1 == 1 config_ns.a.b.param1 = 2 assert (config_ns['a']['b']['param1'] == 2) config_ns['a']['b']['param1'] = 3 assert config_ns.a.b.param1 == 3
def test_quantity_configuration_namespace(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.a.b.param3 = 3 assert_almost_equal(config_ns['a']['b']['param3'], 3 * u.km) config_ns.a.b.param3 = 5000 * u.m assert_almost_equal(config_ns['a']['b']['param3'], 5 * u.km)
def __init__(self, config_fname, atom_data=None, log_dir='./logs/'): self._log_dir = log_dir self.set_logger('startup') self._config = ConfigurationNameSpace.from_yaml(config_fname) if atom_data is None: self._atom_data = AtomData.from_hdf5(self._config.atom_data) else: self._atom_data = atom_data
def test_simple_configuration_namespace(): config_ns = ConfigurationNameSpace(simple_config_dict) assert config_ns.a.b.param1 == 1 config_ns.a.b.param1 = 2 assert config_ns["a"]["b"]["param1"] == 2 config_ns["a"]["b"]["param1"] = 3 assert config_ns.a.b.param1 == 3
def __init__(self, *args, **kwargs): config_fname = args[0] atom_data = kwargs.pop('atom_data', None) self._log_dir = kwargs.pop('log_dir', './logs/') self.set_logger('startup') self._config = ConfigurationNameSpace.from_yaml(config_fname) if atom_data is None: self._atom_data = AtomData.from_hdf5(self._config.atom_data) else: self._atom_data = atom_data
def _load_comparison_config(self, comparison_config_fname): comparison_config = ConfigurationNameSpace.from_yaml( comparison_config_fname) self.comparison_dict = OrderedDict() for column in self.data_columns: default_value = comparison_config.get_config_item(column) #removing the quantity-ness #default_value = getattr(default_value, 'value', default_value) self.comparison_dict[column] = default_value
def from_yaml(cls, fname, resume_fit=None): """ Reading the fitter configuration from a yaml file Parameters ---------- fname: ~str filename """ conf_dict = yaml.load(open(fname), OrderedDictYAMLLoader) default_config = ConfigurationNameSpace.from_yaml( conf_dict['tardis']['default_conf']) atom_data = AtomData.from_hdf5(conf_dict['tardis']['atom_data']) parameter_config = ParameterConfiguration.from_conf_dict(conf_dict['fitter']['parameters']) number_of_samples = conf_dict['fitter']['number_of_samples'] max_iterations = conf_dict['fitter']['max_iterations'] optimizer_dict = conf_dict['fitter'].pop('optimizer') optimizer_class = all_optimizer_dict[optimizer_dict.pop('name')] optimizer = optimizer_class(parameter_config, number_of_samples, **optimizer_dict) fitness_function_dict = conf_dict['fitter'].pop('fitness_function') fitness_function_class = all_fitness_function_dict[ fitness_function_dict.pop('name')] fitness_function = fitness_function_class(**fitness_function_dict) resume = conf_dict['fitter'].get('resume', resume_fit) fitter_log = conf_dict['fitter'].get('fitter_log', None) spectral_store_dict = conf_dict['fitter'].get('spectral_store', None) if spectral_store_dict is not None: spectral_store_fname = spectral_store_dict['fname'] spectral_store_mode = spectral_store_dict.get('mode', 'all') spectral_store_clobber = spectral_store_dict.get('clobber', False) spectral_store = SpectralStore(spectral_store_fname, mode=spectral_store_mode, resume=resume, clobber=spectral_store_clobber) else: spectral_store = None return cls(optimizer, fitness_function, parameter_config=parameter_config, default_config=default_config, atom_data=atom_data, number_of_samples=number_of_samples, max_iterations=max_iterations, fitter_log=fitter_log, spectral_store=spectral_store, resume=resume)
def simple_triangle_plot(dalek_log_file, truth_config=None, plot_contours=False, bins=100): """ Parameters ---------- dalek_log_file : ~str path to normal dalek log file truth_config : ~str path to tardis config file containing the truth values [optional] plot_contours : ~bool plotting contours for the distributions :return: """ dalek_data = pd.read_csv(dalek_log_file, index_col=0) if truth_config is not None: truth_config = ConfigurationNameSpace.from_yaml(truth_config) fitness = dalek_data['dalek.fitness'] labels = [] truths = [] for column in dalek_data.columns: if 'dalek' in column: continue if column.endswith('item0'): label = '.'.join(column.split('.')[-2:]) else: label = column.split('.')[-1] labels.append(label) if truth_config is not None: default_value = truth_config.get_config_item(column) default_value = getattr(default_value, 'value', default_value) truths.append(default_value) if truths == []: truths = None triangle.corner(dalek_data[data_columns], weights=1/fitness, labels=labels, plot_contours=plot_contours, normed=True, truths=truths, bins=bins)
def setup(self): self.nlte_species = [(2, 0), (2, 1)] self.nlte_config = ConfigurationNameSpace( {'species': self.nlte_species}) self.atom_data = atomic.AtomData.from_hdf5(helium_test_db) self.plasma = plasma_array.BasePlasmaArray.from_abundance( {'He': 1.0}, 1e-15 * u.Unit('g/cm3'), self.atom_data, 10 * u.day, nlte_config=self.nlte_config) self.plasma.j_blues = 0.5 * pd.DataFrame( intensity_black_body(self.atom_data.lines.nu.values[np.newaxis].T, np.array([10000.]))) self.plasma.tau_sobolevs = pd.DataFrame( np.zeros_like(self.plasma.j_blues)) self.plasma.t_rads = np.array([10000.]) self.plasma.t_electrons = np.array([9000.]) self.plasma.ws = np.array([0.5]) self.plasma.electron_densities = pd.Series([1.e9]) self.plasma.ion_populations = ion_populations self.plasma.calculate_nlte_level_populations()
def test_set_with_config_item_string_quantity(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns.set_config_item('a.b.param3', 2) assert_almost_equal(config_ns.a.b.param3, 2 * u.km)
def test_config_namespace_copy(): config_ns = ConfigurationNameSpace(simple_config_dict) config_ns2 = config_ns.deepcopy() config_ns2.a.b.param1 = 2 assert config_ns2.a.b.param1 != config_ns.a.b.param1
def test_get_with_config_item_string_item_access(): config_ns = ConfigurationNameSpace(simple_config_dict) item = config_ns.get_config_item("a.b.param2.item0") assert item == 0 item = config_ns.get_config_item("a.b.param2.item1") assert item == 1
def config_ns(): """Return example instance of `ConfigurationNameSpace` class.""" config_name_space = ConfigurationNameSpace(simple_config_dict) return config_name_space
def test_access_with_config_item_string(): config_ns = ConfigurationNameSpace(simple_config_dict) assert config_ns.get_config_item('a.b.param1') == 1 config_ns.set_config_item('a.b.param1', 2) assert config_ns.a.b.param1 == 2