Example #1
0
 def __iter__(self):
     """
     Yield Realization tuples. Notice that the weight is homogeneous when
     sampling is enabled, since it is accounted for in the sampling
     procedure.
     """
     if self.num_samples:
         # random sampling of the logic tree
         probs = random((self.num_samples, len(self.bsetdict)),
                        self.seed, self.sampling_method)
         ordinal = 0
         for branches in self.root_branchset.sample(
                 probs, self.sampling_method):
             name = branches[0].value
             smlt_path_ids = [br.branch_id for br in branches]
             if self.sampling_method.startswith('early_'):
                 weight = 1. / self.num_samples  # already accounted
             elif self.sampling_method.startswith('late_'):
                 weight = numpy.prod([br.weight for br in branches])
             else:
                 raise NotImplementedError(self.sampling_method)
             yield Realization(name, weight, ordinal, tuple(smlt_path_ids))
             ordinal += 1
     else:  # full enumeration
         ordinal = 0
         for weight, branches in self.root_branchset.enumerate_paths():
             name = branches[0].value  # source model name
             branch_ids = [branch.branch_id for branch in branches]
             yield Realization(name, weight, ordinal, tuple(branch_ids))
             ordinal += 1
Example #2
0
 def sample(self, n, seed, sampling_method='early_weights'):
     """
     :param n: number of samples
     :param seed: random seed
     :param sampling_method: by default 'early_weights'
     :returns: n Realization objects
     """
     m = len(self.values)  # number of TRTs
     probs = lt.random((n, m), seed, sampling_method)
     brlists = [
         lt.sample([b for b in self.branches if b.trt == trt], probs[:, i],
                   sampling_method) for i, trt in enumerate(self.values)
     ]
     rlzs = []
     for i in range(n):
         weight = 1
         lt_path = []
         lt_uid = []
         value = []
         for brlist in brlists:  # there is branch list for each TRT
             branch = brlist[i]
             lt_path.append(branch.id)
             lt_uid.append(branch.id if branch.effective else '@')
             weight *= branch.weight
             value.append(branch.gsim)
         rlz = lt.Realization(tuple(value), weight, i, tuple(lt_uid))
         rlzs.append(rlz)
     return rlzs