def init_vars(self, variables=None): """ Initiate priors based on the problem mode and datatypes. Parameters ---------- variables : list of str of variable names to initialise """ if variables is None: variables = self.select_variables() self.priors = OrderedDict() for variable in variables: if variable in block_vars: nvars = 1 else: nvars = self.n_sources lower = default_bounds[variable][0] upper = default_bounds[variable][1] self.priors[variable] = \ Parameter( name=variable, lower=num.ones( nvars, dtype=tconfig.floatX) * lower, upper=num.ones( nvars, dtype=tconfig.floatX) * upper, testvalue=num.ones( nvars, dtype=tconfig.floatX) * (lower + (upper / 5.)))
def update_hypers(self): """ Evaluate the whole config and initialise necessary hyperparameters. """ hypernames = [] if self.geodetic_config is not None: hypernames.extend(self.geodetic_config.get_hypernames()) if self.seismic_config is not None: hypernames.extend(self.seismic_config.get_hypernames()) hypers = dict() for name in hypernames: logger.info('Added hyperparameter %s to config and ' 'model setup!' % name) defaultb_name = 'hypers' hypers[name] = Parameter( name=name, lower=num.ones(1, dtype=tconfig.floatX) * default_bounds[defaultb_name][0], upper=num.ones(1, dtype=tconfig.floatX) * default_bounds[defaultb_name][1], testvalue=num.ones(1, dtype=tconfig.floatX) * num.mean(default_bounds[defaultb_name])) self.problem_config.hyperparameters = hypers self.problem_config.validate_hypers() n_hypers = len(hypers) logger.info('Number of hyperparameters! %i' % n_hypers) if n_hypers == 0: self.hyper_sampler_config = None
def init_vars(self): variables = self.select_variables() self.priors = [] for variable in variables: self.priors.append( Parameter( name=variable, lower=num.ones(self.n_faults, dtype=num.float) * \ default_bounds[variable][0], upper=num.ones(self.n_faults, dtype=num.float) * \ default_bounds[variable][1], testvalue=num.ones(self.n_faults, dtype=num.float) * \ num.mean(default_bounds[variable])) )
def update_hierarchicals(self): """ Evaluate the whole config and initialise necessary hierarchical parameters. """ hierarnames = [] if self.geodetic_config is not None: hierarnames.extend(self.geodetic_config.get_hierarchical_names()) if self.seismic_config is not None: hierarnames.extend(self.seismic_config.get_hierarchical_names()) hierarchicals = OrderedDict() for name in hierarnames: logger.info('Added hierarchical parameter %s to config and ' 'model setup!' % name) if name == 'time_shift': shp = 1 defaultb_name = name else: shp = 2 defaultb_name = 'ramp' hierarchicals[name] = Parameter( name=name, lower=num.ones(shp, dtype=tconfig.floatX) * default_bounds[defaultb_name][0], upper=num.ones(shp, dtype=tconfig.floatX) * default_bounds[defaultb_name][1], testvalue=num.ones(shp, dtype=tconfig.floatX) * num.mean(default_bounds[defaultb_name])) self.problem_config.hierarchicals = hierarchicals self.problem_config.validate_hierarchicals() n_hierarchicals = len(hierarchicals) logger.info('Number of hierarchicals! %i' % n_hierarchicals)
def update_hypers(self): """ Evaluate the whole config and initialise necessary hyperparameters. """ hypernames = [] if self.geodetic_config is not None: for ty in self.geodetic_config.types: hypernames.append(hyper_pars[ty]) if self.seismic_config is not None: for ch in self.seismic_config.channels: hypernames.append(hyper_pars[ch]) # need one hyperparameter less than datasets, throw one out! hypernames.pop(0) hypers = dict() for name in hypernames: hypers[name] = Parameter( name=name, lower=num.ones(1, dtype=num.float) * \ default_bounds[name][0], upper=num.ones(1, dtype=num.float) * \ default_bounds[name][1], testvalue=num.ones(1, dtype=num.float) * \ num.mean(default_bounds[name]) ) self.problem_config.hyperparameters = hypers self.problem_config.validate_hypers() n_hypers = len(hypers) logger.info('Number of hyperparameters! %i' % n_hypers) if n_hypers == 0: self.hyper_sampler_config = None
class ProblemConfig(Object): """ Config for inversion problem to setup. """ mode = String.T(default='geometry', help='Problem to solve: "geometry", "static","kinematic"') n_faults = Int.T(default=1, help='Number of Sub-faults to solve for') datasets = List.T(default=['geodetic']) hyperparameters = Dict.T( help='Hyperparameters to weight different types of datasets.') priors = List.T(Parameter.T()) def init_vars(self): variables = self.select_variables() self.priors = [] for variable in variables: self.priors.append( Parameter( name=variable, lower=num.ones(self.n_faults, dtype=num.float) * \ default_bounds[variable][0], upper=num.ones(self.n_faults, dtype=num.float) * \ default_bounds[variable][1], testvalue=num.ones(self.n_faults, dtype=num.float) * \ num.mean(default_bounds[variable])) ) def select_variables(self): """ Return model variables depending on problem config. """ if self.mode not in modes: raise ValueError('Problem mode %s not implemented' % self.mode) if self.mode == 'geometry': if 'geodetic' in self.datasets: variables = geo_vars_geometry if 'seismic' in self.datasets: variables = joint_vars_geometry elif self.mode == 'static': variables = static_dist_vars elif self.mode == 'kinematic': variables = kinematic_dist_vars if 'seismic' not in self.datasets: logger.error('A kinematic model cannot be resolved with' 'geodetic data only.') raise Exception('Kinematic model not resolvable with only' 'geodetic data!') return variables def validate_priors(self): """ Check if priors and their test values do not contradict! """ for param in self.priors: param() logger.info('All parameter-priors ok!') def validate_hypers(self): """ Check if hyperparameters and their test values do not contradict! """ if self.hyperparameters is not None: for hp in self.hyperparameters.itervalues(): hp() logger.info('All hyper-parameters ok!') else: logger.info('No hyper-parameters defined!')