def get_rlzs_assoc(cinfo, sm_lt_path=None, trts=None): """ :param cinfo: a :class:`openquake.commonlib.source.CompositionInfo` :param sm_lt_path: logic tree path tuple used to select a source model :param trts: tectonic region types to accept """ assoc = RlzsAssoc(cinfo) trtset = set(cinfo.gsim_lt.values) offset = 0 for smodel in cinfo.source_models: # discard source models with non-acceptable lt_path if sm_lt_path and not accept_path(smodel.path, sm_lt_path): continue elif cinfo.num_samples: # sampling, do not reduce the logic tree rlzs = cinfo.gsim_lt.sample(smodel.samples, cinfo.seed + smodel.ordinal) all_trts = list(cinfo.gsim_lt.values) else: # full enumeration # collect the effective tectonic region types and ruptures trts_ = set() for sg in smodel.src_groups: if sg.eff_ruptures: if (trts and sg.trt in trts) or not trts: trts_.add(sg.trt) # recompute the GSIM logic tree if needed if trts_ != {'*'} and trtset != trts_: before = cinfo.gsim_lt.get_num_paths() gsim_lt = cinfo.gsim_lt.reduce(trts_) after = gsim_lt.get_num_paths() if sm_lt_path and before > after: # print the warning only when saving the logic tree, # i.e. when called with sm_lt_path in store_rlz_info logging.warning( 'Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) rlzs = get_effective_rlzs(gsim_lt) all_trts = list(gsim_lt.values) else: rlzs = get_effective_rlzs(cinfo.gsim_lt) all_trts = list(cinfo.gsim_lt.values) assoc._add_realizations(offset, smodel, all_trts, rlzs) offset += len(rlzs) if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(self, get_weight=lambda tm: tm.num_ruptures): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param get_weight: a function trt_model -> positive number """ assoc = RlzsAssoc(self.get_info()) random_seed = self.source_model_lt.seed num_samples = self.source_model_lt.num_samples idx = 0 for smodel in self.source_models: # collect the effective tectonic region types trts = set(tm.trt for tm in smodel.trt_models if get_weight(tm)) # recompute the GSIM logic tree if needed if trts != set(smodel.gsim_lt.tectonic_region_types): before = smodel.gsim_lt.get_num_paths() smodel.gsim_lt.reduce(trts) after = smodel.gsim_lt.get_num_paths() logging.warn( 'Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) if num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(smodel.gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(smodel.gsim_lt) if rlzs: idx = assoc._add_realizations(idx, smodel, rlzs, trts) for trt_model in smodel.trt_models: trt_model.gsims = smodel.gsim_lt.values[trt_model.trt] else: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) if assoc.realizations: if num_samples: assert len(assoc.realizations) == num_samples for rlz in assoc.realizations: rlz.weight = 1. / num_samples else: tot_weight = sum(rlz.weight for rlz in assoc.realizations) if tot_weight == 0: raise ValueError('All realizations have zero weight??') elif abs(tot_weight - 1) > 1E-12: # allow for rounding errors logging.warn('Some source models are not contributing, ' 'weights are being rescaled') for rlz in assoc.realizations: rlz.weight = rlz.weight / tot_weight assoc.gsims_by_trt_id = groupby( assoc.rlzs_assoc, operator.itemgetter(0), lambda group: sorted(gsim for trt_id, gsim in group)) return assoc
def _get_rlzs(self, smodel, all_rlzs, seed): if self.num_samples: # NB: the weights are considered when combining the results, not # when sampling, therefore there are no weights in the function # numpy.random.choice below numpy.random.seed(seed) idxs = numpy.random.choice(len(all_rlzs), smodel.samples) rlzs = [all_rlzs[idx] for idx in idxs] else: # full enumeration rlzs = logictree.get_effective_rlzs(all_rlzs) return rlzs
def get_rlzs_assoc(self, get_weight=lambda tm: tm.num_ruptures): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param get_weight: a function trt_model -> positive number """ assoc = RlzsAssoc(self.get_info()) random_seed = self.source_model_lt.seed num_samples = self.source_model_lt.num_samples idx = 0 for smodel in self.source_models: # collect the effective tectonic region types trts = set(tm.trt for tm in smodel.trt_models if get_weight(tm)) # recompute the GSIM logic tree if needed if trts != set(smodel.gsim_lt.tectonic_region_types): before = smodel.gsim_lt.get_num_paths() smodel.gsim_lt.reduce(trts) after = smodel.gsim_lt.get_num_paths() logging.warn('Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) if num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(smodel.gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(smodel.gsim_lt) if rlzs: idx = assoc._add_realizations(idx, smodel, rlzs, trts) for trt_model in smodel.trt_models: trt_model.gsims = smodel.gsim_lt.values[trt_model.trt] else: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) if assoc.realizations: if num_samples: assert len(assoc.realizations) == num_samples for rlz in assoc.realizations: rlz.weight = 1. / num_samples else: tot_weight = sum(rlz.weight for rlz in assoc.realizations) if tot_weight == 0: raise ValueError('All realizations have zero weight??') elif abs(tot_weight - 1) > 1E-12: # allow for rounding errors logging.warn('Some source models are not contributing, ' 'weights are being rescaled') for rlz in assoc.realizations: rlz.weight = rlz.weight / tot_weight assoc.gsims_by_trt_id = groupby( assoc.rlzs_assoc, operator.itemgetter(0), lambda group: sorted(valid.gsim(gsim) for trt_id, gsim in group)) return assoc
def init(self): sm_rlzs = logictree.get_effective_rlzs(self.source_model_lt) if not self.num_samples: num_gsim_rlzs = self.gsim_lt.get_num_paths() offset = 0 for sm_rlz in sm_rlzs: sm_rlz.offset = offset if self.num_samples: offset += sm_rlz.samples else: offset += num_gsim_rlzs self.sm_rlzs = sm_rlzs
def get_rlzs_assoc(self, count_ruptures=None): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param count_ruptures: a function src_group -> num_ruptures """ assoc = RlzsAssoc(self) random_seed = self.seed idx = 0 trtset = set(self.gsim_lt.tectonic_region_types) for i, smodel in enumerate(self.source_models): # collect the effective tectonic region types and ruptures trts = set() for sg in smodel.src_groups: if count_ruptures: sg.eff_ruptures = count_ruptures(sg) if sg.eff_ruptures: trts.add(sg.trt) # recompute the GSIM logic tree if needed if trtset != trts: before = self.gsim_lt.get_num_paths() gsim_lt = self.gsim_lt.reduce(trts) after = gsim_lt.get_num_paths() if count_ruptures and before > after: logging.warn( 'Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) else: gsim_lt = self.gsim_lt if self.num_samples: # sampling # the int is needed on Windows to convert numpy.uint32 objects rnd = random.Random(int(random_seed + idx)) rlzs = logictree.sample(gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(gsim_lt) if rlzs: indices = numpy.arange(idx, idx + len(rlzs)) idx += len(indices) assoc._add_realizations(indices, smodel, gsim_lt, rlzs) elif trts: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) if len(rlzs) > TWO16: raise ValueError( 'The source model %s has %d realizations, the maximum ' 'is %d' % (smodel.name, len(rlzs), TWO16)) # NB: realizations could be filtered away by logic tree reduction if assoc.realizations: assoc._init() return assoc
def _get_rlzs(self, smodel, all_rlzs, seed): if self.num_samples: # NB: the weights are considered when combining the results, not # when sampling, therefore there are no weights in the function # numpy.random.choice below numpy.random.seed(seed) idxs = numpy.random.choice(len(all_rlzs), smodel.samples) rlzs = [all_rlzs[idx] for idx in idxs] else: # full enumeration rlzs = logictree.get_effective_rlzs(all_rlzs) if len(rlzs) > TWO16: raise ValueError( 'The source model %s has %d realizations, the maximum ' 'is %d' % (smodel.names, len(rlzs), TWO16)) return rlzs
def get_rlzs_assoc(self, count_ruptures=lambda tm: -1): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param count_ruptures: a function trt_model -> num_ruptures """ assoc = RlzsAssoc(self) random_seed = self.seed num_samples = self.num_samples idx = 0 for i, smodel in enumerate(self.source_models): # collect the effective tectonic region types and ruptures trts = set() rups = [] for tm in smodel.trt_models: nr = count_ruptures(tm) if nr: rups.append('%r: %d' % (capitalize(tm.trt), nr)) trts.add(tm.trt) self.eff_ruptures[i] = '{%s}' % cjoin(rups, LENGTH - len('{}')) # recompute the GSIM logic tree if needed if trts != set(smodel.gsim_lt.tectonic_region_types): before = smodel.gsim_lt.get_num_paths() smodel.gsim_lt.reduce(trts) after = smodel.gsim_lt.get_num_paths() logging.warn( 'Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) if num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(smodel.gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(smodel.gsim_lt) if rlzs: indices = numpy.arange(idx, idx + len(rlzs)) idx += len(indices) assoc._add_realizations(indices, smodel, rlzs) for trt_model in smodel.trt_models: trt_model.gsims = smodel.gsim_lt.values[trt_model.trt] else: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) # NB: realizations could be filtered away by logic tree reduction if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(self, count_ruptures=lambda tm: -1): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param count_ruptures: a function trt_model -> num_ruptures """ assoc = RlzsAssoc(self) random_seed = self.seed num_samples = self.num_samples idx = 0 for i, smodel in enumerate(self.source_models): # collect the effective tectonic region types and ruptures trts = set() rups = [] for tm in smodel.trt_models: nr = count_ruptures(tm) if nr: rups.append('%r: %d' % (capitalize(tm.trt), nr)) trts.add(tm.trt) self.eff_ruptures[i] = '{%s}' % cjoin(rups, LENGTH - len('{}')) # recompute the GSIM logic tree if needed if trts != set(smodel.gsim_lt.tectonic_region_types): before = smodel.gsim_lt.get_num_paths() smodel.gsim_lt.reduce(trts) after = smodel.gsim_lt.get_num_paths() logging.warn('Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) if num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(smodel.gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(smodel.gsim_lt) if rlzs: indices = numpy.arange(idx, idx + len(rlzs)) idx += len(indices) assoc._add_realizations(indices, smodel, rlzs) for trt_model in smodel.trt_models: trt_model.gsims = smodel.gsim_lt.values[trt_model.trt] else: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) # NB: realizations could be filtered away by logic tree reduction if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(cinfo, sm_lt_path=None, trts=None): """ :param cinfo: a :class:`openquake.commonlib.source.CompositionInfo` :param sm_lt_path: logic tree path tuple used to select a source model :param trts: tectonic region types to accept """ assoc = RlzsAssoc(cinfo) trtset = set(cinfo.gsim_lt.values) offset = 0 missing = set(trtset) all_trts = list(cinfo.gsim_lt.values) if not cinfo.num_samples: rlzs = get_effective_rlzs(cinfo.gsim_lt) for smodel in cinfo.source_models: # discard source models with non-acceptable lt_path if sm_lt_path and not accept_path(smodel.path, sm_lt_path): continue elif cinfo.num_samples: # sampling, do not reduce the logic tree rlzs = cinfo.gsim_lt.sample(smodel.samples, cinfo.seed + smodel.ordinal) else: # full enumeration # collect the effective tectonic region types and ruptures trts_ = set() for sg in smodel.src_groups: if sg.eff_ruptures: if (trts and sg.trt in trts) or not trts: trts_.add(sg.trt) # check if the GSIM logic tree could be reduced if trts_ != {'*'} and trtset != trts_: before = cinfo.gsim_lt.get_num_paths() gsim_lt = cinfo.gsim_lt.reduce(trts_) after = gsim_lt.get_num_paths() if before > after: missing &= trtset - trts_ assoc._add_realizations(offset, smodel, all_trts, rlzs) offset += len(rlzs) if missing != trtset: logging.warning('You should reduce the gsim logic tree by removing %s', missing) if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(self, count_ruptures=None): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param count_ruptures: a function trt_model -> num_ruptures """ assoc = RlzsAssoc(self) random_seed = self.seed idx = 0 trtset = set(self.gsim_lt.tectonic_region_types) for i, smodel in enumerate(self.source_models): # collect the effective tectonic region types and ruptures trts = set() for tm in smodel.trt_models: if count_ruptures: tm.eff_ruptures = count_ruptures(tm) if tm.eff_ruptures: trts.add(tm.trt) # recompute the GSIM logic tree if needed if trtset != trts: before = self.gsim_lt.get_num_paths() gsim_lt = self.gsim_lt.reduce(trts) after = gsim_lt.get_num_paths() if count_ruptures and before > after: logging.warn( 'Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) else: gsim_lt = self.gsim_lt if self.num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(gsim_lt) if rlzs: indices = numpy.arange(idx, idx + len(rlzs)) idx += len(indices) assoc._add_realizations(indices, smodel, gsim_lt, rlzs) elif trts: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) # NB: realizations could be filtered away by logic tree reduction if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(self, count_ruptures=None): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param count_ruptures: a function src_group -> num_ruptures """ assoc = RlzsAssoc(self) random_seed = self.seed idx = 0 trtset = set(self.gsim_lt.tectonic_region_types) for i, smodel in enumerate(self.source_models): # collect the effective tectonic region types and ruptures trts = set() for sg in smodel.src_groups: if count_ruptures: sg.eff_ruptures = count_ruptures(sg) if sg.eff_ruptures: trts.add(sg.trt) # recompute the GSIM logic tree if needed if trtset != trts: before = self.gsim_lt.get_num_paths() gsim_lt = self.gsim_lt.reduce(trts) after = gsim_lt.get_num_paths() if count_ruptures and before > after: logging.warn('Reducing the logic tree of %s from %d to %d ' 'realizations', smodel.name, before, after) else: gsim_lt = self.gsim_lt if self.num_samples: # sampling # the int is needed on Windows to convert numpy.uint32 objects rnd = random.Random(int(random_seed + idx)) rlzs = logictree.sample(gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(gsim_lt) if rlzs: indices = numpy.arange(idx, idx + len(rlzs)) idx += len(indices) assoc._add_realizations(indices, smodel, gsim_lt, rlzs) elif trts: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) # NB: realizations could be filtered away by logic tree reduction if assoc.realizations: assoc._init() return assoc
def get_rlzs_assoc(self, get_weight=lambda tm: tm.num_ruptures): """ Return a RlzsAssoc with fields realizations, gsim_by_trt, rlz_idx and trt_gsims. :param get_weight: a function trt_model -> positive number """ assoc = RlzsAssoc(self.info) random_seed = self.source_model_lt.seed num_samples = self.source_model_lt.num_samples idx = 0 for smodel in self.source_models: # count the number of ruptures per tectonic region type trts = set() for trt_model in smodel.trt_models: if get_weight(trt_model) > 0: trts.add(trt_model.trt) # recompute the GSIM logic tree if needed if trts != set(smodel.gsim_lt.tectonic_region_types): smodel.gsim_lt.reduce(trts) if num_samples: # sampling rnd = random.Random(random_seed + idx) rlzs = logictree.sample(smodel.gsim_lt, smodel.samples, rnd) else: # full enumeration rlzs = logictree.get_effective_rlzs(smodel.gsim_lt) if rlzs: idx = assoc._add_realizations(idx, smodel, rlzs) else: logging.warn('No realizations for %s, %s', '_'.join(smodel.path), smodel.name) if assoc.realizations: if num_samples: assert len(assoc.realizations) == num_samples for rlz in assoc.realizations: rlz.weight = 1. / num_samples else: tot_weight = sum(rlz.weight for rlz in assoc.realizations) if tot_weight == 0: raise ValueError('All realizations have zero weight??') elif abs(tot_weight - 1) > 1E-12: # allow for rounding errors logging.warn('Some source models are not contributing, ' 'weights are being rescaled') for rlz in assoc.realizations: rlz.weight = rlz.weight / tot_weight return assoc
def get_rlzs(self, eri): """ :returns: a list of LtRealization objects """ rlzs = [] sm = self.sm_rlzs[eri] if self.num_samples: gsim_rlzs = self.gsim_lt.sample(sm.samples, self.seed + sm.ordinal) elif hasattr(self, 'gsim_rlzs'): # cache gsim_rlzs = self.gsim_rlzs else: self.gsim_rlzs = gsim_rlzs = logictree.get_effective_rlzs( self.gsim_lt) for i, gsim_rlz in enumerate(gsim_rlzs): weight = sm.weight * gsim_rlz.weight rlz = LtRealization(sm.offset + i, sm.lt_path, gsim_rlz, weight) rlzs.append(rlz) return rlzs
def get_source_models(oqparam, source_model_lt, sitecol=None, in_memory=True): """ Build all the source models generated by the logic tree. :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param source_model_lt: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance :param in_memory: if True, keep in memory the sources, else just collect the TRTs :returns: an iterator over :class:`openquake.commonlib.source.SourceModel` tuples """ converter = sourceconverter.SourceConverter( oqparam.investigation_time, oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing, oqparam.width_of_mfd_bin, oqparam.area_source_discretization) # consider only the effective realizations rlzs = logictree.get_effective_rlzs(source_model_lt) samples_by_lt_path = source_model_lt.samples_by_lt_path() for i, rlz in enumerate(rlzs): sm = rlz.value # name of the source model smpath = rlz.lt_path num_samples = samples_by_lt_path[smpath] if num_samples > 1: logging.warn('The source path %s was sampled %d times', smpath, num_samples) fname = possibly_gunzip(os.path.join(oqparam.base_path, sm)) if in_memory: apply_unc = source_model_lt.make_apply_uncertainties(smpath) try: trt_models = source.parse_source_model( fname, converter, apply_unc) except ValueError as e: if str(e) in ('Surface does not conform with Aki & ' 'Richards convention', 'Edges points are not in the right order'): raise InvalidFile('''\ %s: %s. Probably you are using an obsolete model. In that case you can fix the file with the command python -m openquake.engine.tools.correct_complex_sources %s ''' % (fname, e, fname)) else: raise else: # just collect the TRT models smodel = read_nodes(fname, lambda el: 'sourceModel' in el.tag, source.nodefactory['sourceModel']).next() trt_models = source.TrtModel.collect(smodel) trts = [mod.trt for mod in trt_models] source_model_lt.tectonic_region_types.update(trts) gsim_file = oqparam.inputs.get('gsim_logic_tree') if gsim_file: # check TRTs gsim_lt = get_gsim_lt(oqparam, trts) for trt_model in trt_models: if trt_model.trt not in gsim_lt.values: raise ValueError( "Found in %r a tectonic region type %r inconsistent " "with the ones in %r" % (sm, trt_model.trt, gsim_file)) trt_model.gsims = gsim_lt.values[trt_model.trt] else: gsim_lt = logictree.DummyGsimLogicTree() weight = rlz.weight / num_samples yield source.SourceModel( sm, weight, smpath, trt_models, gsim_lt, i, num_samples)
def get_source_models(oqparam, gsim_lt, source_model_lt, in_memory=True): """ Build all the source models generated by the logic tree. :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param gsim_lt: a :class:`openquake.commonlib.logictree.GsimLogicTree` instance :param source_model_lt: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance :param in_memory: if True, keep in memory the sources, else just collect the TRTs :returns: an iterator over :class:`openquake.commonlib.source.SourceModel` tuples """ converter = sourceconverter.SourceConverter( oqparam.investigation_time, oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing, oqparam.width_of_mfd_bin, oqparam.area_source_discretization) parser = source.SourceModelParser(converter) # consider only the effective realizations rlzs = logictree.get_effective_rlzs(source_model_lt) samples_by_lt_path = source_model_lt.samples_by_lt_path() num_source_models = len(rlzs) for i, rlz in enumerate(rlzs): sm = rlz.value # name of the source model smpath = rlz.lt_path num_samples = samples_by_lt_path[smpath] fname = possibly_gunzip(os.path.join(oqparam.base_path, sm)) if in_memory: apply_unc = source_model_lt.make_apply_uncertainties(smpath) try: src_groups = parser.parse_src_groups(fname, apply_unc) except ValueError as e: if str(e) in ('Surface does not conform with Aki & ' 'Richards convention', 'Edges points are not in the right order'): raise InvalidFile('''\ %s: %s. Probably you are using an obsolete model. In that case you can fix the file with the command python -m openquake.engine.tools.correct_complex_sources %s ''' % (fname, e, fname)) else: raise else: # just collect the TRT models smodel = nrml.read(fname).sourceModel src_groups = sourceconverter.SourceGroup.collect(smodel) trts = [mod.trt for mod in src_groups] source_model_lt.tectonic_region_types.update(trts) gsim_file = oqparam.inputs.get('gsim_logic_tree') if gsim_file: # check TRTs for src_group in src_groups: if src_group.trt not in gsim_lt.values: raise ValueError( "Found in %r a tectonic region type %r inconsistent " "with the ones in %r" % (sm, src_group.trt, gsim_file)) else: gsim_lt = logictree.GsimLogicTree.from_(oqparam.gsim) weight = rlz.weight / num_samples num_gsim_paths = (num_samples if oqparam.number_of_logic_tree_samples else gsim_lt.get_num_paths()) logging.info('Processed source model %d/%d with %d gsim path(s)', i + 1, num_source_models, num_gsim_paths) yield source.SourceModel( sm, weight, smpath, src_groups, num_gsim_paths, i, num_samples) # log if some source file is being used more than once for fname, hits in parser.fname_hits.items(): if hits > 1: logging.info('%s has been considered %d times', fname, hits)
def get_source_models(oqparam, gsim_lt, source_model_lt, in_memory=True): """ Build all the source models generated by the logic tree. :param oqparam: an :class:`openquake.commonlib.oqvalidation.OqParam` instance :param gsim_lt: a :class:`openquake.commonlib.logictree.GsimLogicTree` instance :param source_model_lt: a :class:`openquake.commonlib.logictree.SourceModelLogicTree` instance :param in_memory: if True, keep in memory the sources, else just collect the TRTs :returns: an iterator over :class:`openquake.commonlib.source.SourceModel` tuples """ converter = sourceconverter.SourceConverter( oqparam.investigation_time, oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing, oqparam.width_of_mfd_bin, oqparam.area_source_discretization) parser = source.SourceModelParser(converter) # consider only the effective realizations rlzs = logictree.get_effective_rlzs(source_model_lt) samples_by_lt_path = source_model_lt.samples_by_lt_path() num_source_models = len(rlzs) for i, rlz in enumerate(rlzs): sm = rlz.value # name of the source model smpath = rlz.lt_path num_samples = samples_by_lt_path[smpath] fname = possibly_gunzip(os.path.join(oqparam.base_path, sm)) if in_memory: apply_unc = source_model_lt.make_apply_uncertainties(smpath) try: trt_models = parser.parse_trt_models(fname, apply_unc) except ValueError as e: if str(e) in ('Surface does not conform with Aki & ' 'Richards convention', 'Edges points are not in the right order'): raise InvalidFile('''\ %s: %s. Probably you are using an obsolete model. In that case you can fix the file with the command python -m openquake.engine.tools.correct_complex_sources %s ''' % (fname, e, fname)) else: raise else: # just collect the TRT models smodel = next( read_nodes(fname, lambda el: 'sourceModel' in el.tag, source.nodefactory['sourceModel'])) trt_models = source.TrtModel.collect(smodel) trts = [mod.trt for mod in trt_models] source_model_lt.tectonic_region_types.update(trts) gsim_file = oqparam.inputs.get('gsim_logic_tree') if gsim_file: # check TRTs for trt_model in trt_models: if trt_model.trt not in gsim_lt.values: raise ValueError( "Found in %r a tectonic region type %r inconsistent " "with the ones in %r" % (sm, trt_model.trt, gsim_file)) else: gsim_lt = logictree.GsimLogicTree.from_(oqparam.gsim) weight = rlz.weight / num_samples num_gsim_paths = (num_samples if oqparam.number_of_logic_tree_samples else gsim_lt.get_num_paths()) logging.info('Processed source model %d/%d with %d gsim path(s)', i + 1, num_source_models, num_gsim_paths) yield source.SourceModel(sm, weight, smpath, trt_models, num_gsim_paths, i, num_samples) # log if some source file is being used more than once for fname, hits in parser.fname_hits.items(): if hits > 1: logging.info('%s has been considered %d times', fname, hits)
def _get_rlzs(self, smodel, all_rlzs, seed): if self.num_samples: rlzs = logictree.sample(all_rlzs, smodel.samples, seed) else: # full enumeration rlzs = logictree.get_effective_rlzs(all_rlzs) return rlzs
def _get_rlzs(num_samples, smodel, gsim_lt, seed): if num_samples: rlzs = gsim_lt.sample(smodel.samples, seed) else: # full enumeration rlzs = get_effective_rlzs(gsim_lt) return rlzs