def filter(self, src_filter): # called once per tile """ Generate a new CompositeSourceModel by filtering the sources on the given site collection. :param src_filter: a SourceFilter instance :param weight: source weight function :returns: a new CompositeSourceModel instance """ source_models = [] for sm in self.source_models: src_groups = [] for src_group in sm.src_groups: sg = copy.copy(src_group) sg.sources = [] for src, _sites in src_filter(src_group.sources): sg.sources.append(src) src_groups.append(sg) newsm = logictree.SourceModel( sm.names, sm.weight, sm.path, src_groups, sm.num_gsim_paths, sm.ordinal, sm.samples) source_models.append(newsm) new = self.__class__(self.gsim_lt, self.source_model_lt, source_models, self.optimize_same_id) new.src_filter = src_filter return new
def __fromh5__(self, dic, attrs): # TODO: this is called more times than needed, maybe we should cache it sg_data = group_array(dic['sg_data'], 'sm_id') sm_data = dic['sm_data'] vars(self).update(attrs) self.gsim_fname = decode(self.gsim_fname) if self.gsim_fname.endswith('.xml'): # otherwise it would look in the current directory GMPETable.GMPE_DIR = os.path.dirname(self.gsim_fname) trts = sorted(self.trts) tmp = writetmp(self.gsim_lt_xml, suffix='.xml') self.gsim_lt = logictree.GsimLogicTree(tmp, trts) else: # fake file with the name of the GSIM self.gsim_lt = logictree.GsimLogicTree.from_(self.gsim_fname) self.source_models = [] for sm_id, rec in enumerate(sm_data): tdata = sg_data[sm_id] srcgroups = [ sourceconverter.SourceGroup( self.trts[data['trti']], id=data['grp_id'], eff_ruptures=data['effrup'], tot_ruptures=get_totrup(data)) for data in tdata if data['effrup']] path = tuple(str(decode(rec['path'])).split('_')) trts = set(sg.trt for sg in srcgroups) sm = logictree.SourceModel( rec['name'], rec['weight'], path, srcgroups, rec['num_rlzs'], sm_id, rec['samples']) self.source_models.append(sm) self.init() try: os.remove(tmp) # gsim_lt file except NameError: # tmp is defined only in the regular case, see above pass
def filter(self, src_filter): """ Generate a new CompositeSourceModel by filtering the sources on the given site collection. :param sitecol: a SiteCollection instance :para src_filter: a SourceFilter instance """ source_models = [] weight = 0 for sm in self.source_models: src_groups = [] for src_group in sm.src_groups: sources = [] for src, sites in src_filter(src_group.sources): sources.append(src) weight += src.weight sg = copy.copy(src_group) sg.sources = sources src_groups.append(sg) newsm = logictree.SourceModel(sm.name, sm.weight, sm.path, src_groups, sm.num_gsim_paths, sm.ordinal, sm.samples) source_models.append(newsm) new = self.__class__(self.gsim_lt, self.source_model_lt, source_models) new.weight = weight new.src_filter = src_filter return new
def fake(cls, gsimlt=None): """ :returns: a fake `CompositionInfo` instance with the given gsim logic tree object; if None, builds automatically a fake gsim logic tree """ weight = 1 gsim_lt = gsimlt or logictree.GsimLogicTree.from_('FromFile') fakeSM = logictree.SourceModel( 'fake', weight, 'b1', [sourceconverter.SourceGroup('*', eff_ruptures=1)], gsim_lt.get_num_paths(), ordinal=0, samples=1) return cls(gsim_lt, seed=0, num_samples=0, source_models=[fakeSM], tot_weight=0)
def __fromh5__(self, dic, attrs): # TODO: this is called more times than needed, maybe we should cache it sg_data = group_array(dic['sg_data'], 'sm_id') sm_data = dic['sm_data'] vars(self).update(attrs) self.gsim_fname = decode(self.gsim_fname) if self.gsim_fname.endswith('.xml'): trts = sorted(self.trts) if 'gmpe_table' in self.gsim_lt_xml: # the canadian gsims depends on external files which are not # in the datastore; I am storing the path to the original # file so that the external files can be found; unfortunately, # this means that copying the datastore on a different machine # and exporting from there works only if the gsim_fname and all # the external files are copied in the exact same place self.gsim_lt = logictree.GsimLogicTree(self.gsim_fname, trts) else: # regular case: read the logic tree from self.gsim_lt_xml, # so that you do not need to copy anything except the datastore tmp = writetmp(self.gsim_lt_xml, suffix='.xml') self.gsim_lt = logictree.GsimLogicTree(tmp, trts) else: # fake file with the name of the GSIM self.gsim_lt = logictree.GsimLogicTree.from_(self.gsim_fname) self.source_models = [] for sm_id, rec in enumerate(sm_data): tdata = sg_data[sm_id] srcgroups = [ sourceconverter.SourceGroup(self.trts[trti], id=grp_id, eff_ruptures=effrup, tot_ruptures=totrup) for grp_id, trti, effrup, totrup, sm_id in tdata if effrup ] path = tuple(str(decode(rec['path'])).split('_')) trts = set(sg.trt for sg in srcgroups) num_gsim_paths = self.gsim_lt.reduce(trts).get_num_paths() sm = logictree.SourceModel(rec['name'], rec['weight'], path, srcgroups, num_gsim_paths, sm_id, rec['samples']) self.source_models.append(sm) self.init() try: os.remove(tmp) # gsim_lt file except NameError: # tmp is defined only in the regular case, see above pass
def filter(self, src_filter): # called once per tile """ Generate a new CompositeSourceModel by filtering the sources on the given site collection. :param sitecol: a SiteCollection instance :para src_filter: a SourceFilter instance """ ngsims = {trt: len(gs) for trt, gs in self.gsim_lt.values.items()} source_models = [] weight = 0 for sm in self.source_models: src_groups = [] for src_group in sm.src_groups: mutex = getattr(src_group, 'src_interdep', None) == 'mutex' self.add_infos(src_group.sources) # unsplit sources sources = [] for src in src_group.sources: if hasattr(src, '__iter__') and not mutex: # MultiPoint, AreaSource, NonParametric # NB: source.split_source is cached sources.extend(source.split_source(src)) else: # mutex sources cannot be split sources.append(src) sg = copy.copy(src_group) sg.sources = [] for src, _sites in src_filter(sources): sg.sources.append(src) src.ngsims = ngsims[src.tectonic_region_type] weight += src.weight src_groups.append(sg) newsm = logictree.SourceModel(sm.names, sm.weight, sm.path, src_groups, sm.num_gsim_paths, sm.ordinal, sm.samples) source_models.append(newsm) new = self.__class__(self.gsim_lt, self.source_model_lt, source_models) new.weight = weight new.src_filter = src_filter return new
def filter(self, src_filter): """ Generate a new CompositeSourceModel by filtering the sources on the given site collection. :param sitecol: a SiteCollection instance :para src_filter: a SourceFilter instance """ source_models = [] weight = 0 idx = 0 seed = int(self.source_model_lt.seed) # avoids F32 issues on Windows for sm in self.source_models: src_groups = [] for src_group in sm.src_groups: if self.source_model_lt.num_samples: rnd = random.Random(seed + idx) rlzs = logictree.sample(self.gsim_lt, sm.samples, rnd) idx += len(rlzs) for i, sg in enumerate(sm.src_groups): sg.gsims = sorted(set(rlz.value[i] for rlz in rlzs)) else: for sg in sm.src_groups: sg.gsims = sorted(self.gsim_lt.values[sg.trt]) sources = [] for src, sites in src_filter(src_group.sources): sources.append(src) weight += src.weight sg = copy.copy(src_group) sg.sources = sources src_groups.append(sg) newsm = logictree.SourceModel( sm.name, sm.weight, sm.path, src_groups, sm.num_gsim_paths, sm.ordinal, sm.samples) source_models.append(newsm) new = self.__class__(self.gsim_lt, self.source_model_lt, source_models) new.weight = weight return new