def test_sample_gmpe(self): (value, weight, branch_ids) = logictree.sample_one( self.gmpe_lt, self.rnd) self.assertEqual(value, {'Subduction Interface': 'SadighEtAl1997', 'Active Shallow Crust': 'ChiouYoungs2008'}) self.assertEqual(weight, 0.5) self.assertEqual(('b2', 'b3'), branch_ids)
def initialize_realizations(self): """ Create records for the `hzrdr.lt_realization`. This function works either in random sampling mode (when lt_realization models get the random seed value) or in enumeration mode (when weight values are populated). In both cases we record the logic tree paths for both trees in the `lt_realization` record, as well as ordinal number of the realization (zero-based). """ logs.LOG.progress("initializing realizations") num_samples = self.hc.number_of_logic_tree_samples gsim_lt_dict = {} # gsim_lt per source model logic tree path for idx, (sm, weight, sm_lt_path) in enumerate(self.source_model_lt): try: lt_model = models.LtSourceModel.objects.get( hazard_calculation=self.job, sm_lt_path=sm_lt_path) except ObjectDoesNotExist: # this happens if there are no sources for the source # model at the given path logs.LOG.warn('No sources for sm_lt_path %s', sm_lt_path) continue if not sm_lt_path in gsim_lt_dict: gsim_lt_dict[sm_lt_path] = lt_model.make_gsim_lt() gsim_lt = gsim_lt_dict[sm_lt_path] if num_samples: # sampling, pick just one gsim realization rnd = random.Random(self.hc.random_seed + idx) rlzs = [logictree.sample_one(gsim_lt, rnd)] else: rlzs = list(gsim_lt) # full enumeration logs.LOG.info('Creating %d GMPE realization(s) for model %s, %s', len(rlzs), lt_model.sm_name, lt_model.sm_lt_path) self._initialize_realizations(idx, lt_model, rlzs, gsim_lt) num_ind_rlzs = sum(gsim_lt.get_num_paths() for gsim_lt in gsim_lt_dict.itervalues()) if num_samples > num_ind_rlzs: logs.LOG.warn(""" The number of independent realizations is %d but you are using %d samplings. That means that some GMPEs will be sampled more than once, resulting in duplicated data and redundant computation. You should switch to full enumeration mode, i.e. set number_of_logic_tree_samples=0 in your .ini file. """, num_ind_rlzs, num_samples)