Display the actual number of ruptures by source in event based calculations
483878
def view_act_ruptures_by_src(token, dstore):
    
    data = dstore['ruptures'].value[['srcidx', 'serial']]
    counts = sorted(countby(data, 'srcidx').items(),
                    key=operator.itemgetter(1), reverse=True)
    src_info = dstore['source_info'].value[['grp_id', 'source_id']]
    table = [['src_id', 'grp_id', 'act_ruptures']]
    for srcidx, act_ruptures in counts:
        src = src_info[srcidx]
        table.append([src['source_id'], src['grp_id'], act_ruptures])
    return rst_table(table)
1254718
def gen_rupture_getters(dstore, slc=slice(None),
                        concurrent_tasks=1, hdf5cache=None):
    
    if dstore.parent:
        dstore = dstore.parent
    csm_info = dstore['csm_info']
    trt_by_grp = csm_info.grp_by("trt")
    samples = csm_info.get_samples_by_grp()
    rlzs_by_gsim = csm_info.get_rlzs_by_gsim_grp()
    rup_array = dstore['ruptures'][slc]
    maxweight = numpy.ceil(len(rup_array) / (concurrent_tasks or 1))
    nr, ne = 0, 0
    for grp_id, arr in general.group_array(rup_array, 'grp_id').items():
        if not rlzs_by_gsim[grp_id]:
            # this may happen if a source model has no sources, like
            # in event_based_risk/case_3
            continue
        for block in general.block_splitter(arr, maxweight):
            rgetter = RuptureGetter(
                hdf5cache or dstore.filename, numpy.array(block), grp_id,
                trt_by_grp[grp_id], samples[grp_id], rlzs_by_gsim[grp_id])
            rgetter.weight = getattr(block, 'weight', len(block))
            yield rgetter
            nr += len(block)
            ne += rgetter.num_events
    logging.info('Read %d ruptures and %d events', nr, ne)
999703
def command_inner_burst_op_count(self, event=None):
        
        try:
            inner_burst_op_count = self.inner_burst_op_count_var.get()
        except ValueError:
            inner_burst_op_count = self.runtime_cfg.inner_burst_op_count

        if inner_burst_op_count < 1:
            inner_burst_op_count = self.runtime_cfg.inner_burst_op_count

        self.runtime_cfg.inner_burst_op_count = inner_burst_op_count
        self.inner_burst_op_count_var.set(self.runtime_cfg.inner_burst_op_count)
491405
def get_num_rlzs(self, source_model=None):
        
        if source_model is None:
            return sum(self.get_num_rlzs(sm) for sm in self.source_models)
        if self.num_samples:
            return source_model.samples
        trts = set(sg.trt for sg in source_model.src_groups if sg.eff_ruptures)
        if sum(sg.eff_ruptures for sg in source_model.src_groups) == 0:
            return 0
        return self.gsim_lt.reduce(trts).get_num_paths()
992650
def export_agg_losses_ebr(ekey, dstore):
    
    if 'ruptures' not in dstore:
        logging.warning('There are no ruptures in the datastore')
        return []
    name, ext = export.keyfunc(ekey)
    agg_losses = dstore['losses_by_event']
    has_rup_data = 'ruptures' in dstore
    extra_list = [('magnitude', F32),
                  ('centroid_lon', F32),
                  ('centroid_lat', F32),
                  ('centroid_depth', F32)] if has_rup_data else []
    oq = dstore['oqparam']
    lti = oq.lti
    dtlist = ([('event_id', U64), ('rup_id', U32), ('year', U32)]
              + extra_list + oq.loss_dt_list())
    elt_dt = numpy.dtype(dtlist)
    elt = numpy.zeros(len(agg_losses), elt_dt)
    writer = writers.CsvWriter(fmt=writers.FIVEDIGITS)
    events = dstore['events'].value
    events_by_rupid = collections.defaultdict(list)
    for event in events:
        rupid = event['eid'] // TWO32
        events_by_rupid[rupid].append(event)
    year_of = year_dict(events['eid'], oq.investigation_time, oq.ses_seed)
    rup_data = {}
    event_by_eid = {}  # eid -> event
    # populate rup_data and event_by_eid
    # TODO: avoid reading the events twice
    for rgetter in getters.gen_rupture_getters(dstore):
        ruptures = rgetter.get_ruptures()
        for ebr in ruptures:
            for event in events_by_rupid[ebr.serial]:
                event_by_eid[event['eid']] = event
        if has_rup_data:
            rup_data.update(get_rup_data(ruptures))
    for r, row in enumerate(agg_losses):
        rec = elt[r]
        event = event_by_eid[row['eid']]
        rec['event_id'] = eid = event['eid']
        rec['year'] = year_of[eid]
        if rup_data:
            rec['rup_id'] = rup_id = event['eid'] // TWO32
            (rec['magnitude'], rec['centroid_lon'], rec['centroid_lat'],
             rec['centroid_depth']) = rup_data[rup_id]
        for lt, i in lti.items():
            rec[lt] = row['loss'][i]
    elt.sort(order=['year', 'event_id'])
    dest = dstore.build_fname('elt', '', 'csv')
    writer.save(elt, dest)
    return writer.getsaved()
122757
def _get_total_read_size(self):
        """"""
        if self.read_size:
            read_size = EVENT_SIZE * self.read_size
        else:
            read_size = EVENT_SIZE
        return read_size
882696
def export_ruptures_csv(ekey, dstore):
    
    oq = dstore['oqparam']
    if 'scenario' in oq.calculation_mode:
        return []
    dest = dstore.export_path('ruptures.csv')
    header = ('rupid multiplicity mag centroid_lon centroid_lat '
              'centroid_depth trt strike dip rake boundary').split()
    rows = []
    for rgetter in gen_rupture_getters(dstore):
        rups = rgetter.get_ruptures()
        rup_data = calc.RuptureData(rgetter.trt, rgetter.rlzs_by_gsim)
        for r in rup_data.to_array(rups):
            rows.append(
                (r['rup_id'], r['multiplicity'], r['mag'],
                 r['lon'], r['lat'], r['depth'],
                 rgetter.trt, r['strike'], r['dip'], r['rake'],
                 r['boundary']))
    rows.sort()  # by rupture serial
    comment = 'investigation_time=%s, ses_per_logic_tree_path=%s' % (
        oq.investigation_time, oq.ses_per_logic_tree_path)
    writers.write_csv(dest, rows, header=header, sep='\t', comment=comment)
    return [dest]
951229
def _get_sa_at_1180(self, C, imt, sites, rup, dists):
        
        # reference vs30 = 1180 m/s
        vs30_1180 = np.ones_like(sites.vs30) * 1180.
        # reference shaking intensity = 0
        ref_iml = np.zeros_like(sites.vs30)
        # fake Z1.0 - Since negative it will be replaced by the default Z1.0
        # for the corresponding region
        fake_z1pt0 = np.ones_like(sites.vs30) * -1
        return (self._get_basic_term(C, rup, dists) +
                self._get_faulting_style_term(C, rup) +
                self._get_site_response_term(C, imt, vs30_1180, ref_iml) +
                self._get_hanging_wall_term(C, dists, rup) +
                self._get_top_of_rupture_depth_term(C, imt, rup) +
                self._get_soil_depth_term(C, fake_z1pt0, vs30_1180) +
                self._get_regional_term(C, imt, vs30_1180, dists.rrup)
                )
438636
def num_failures(self):
        
        min_time = time.time() - self.window

        while self.failures and self.failures[0] < min_time:
            self.failures.popleft()

        return len(self.failures)
760995
function showLoadingErrors() {
  const failedTasks = []
  const taskStatus = {}

  // Construct data for full report
  for (const data of scripts) {
    const messages = []
    // check sources
    const mSources = data.sources.filter(s => glob.sync(s).length === 0)
    if (mSources.length !== 0) {
      messages.push(
        `✘ Missing sources: ${(mSources.length > 1 ? '\n  ' : '') +
          mSources.map(s => `'${s}'`).join('\n  ')}`
      )
    }
    // display remaining errors
    for (const error of data.errors) {
      let msg = error.message || error.toString()
      const prefix = msg.startsWith('Error') ? '' : 'Error: '
      messages.push(`✘ ${prefix}${msg.replace(/\n/g, '\n  ')}`)
    }
    if (messages.length > 0) {
      failedTasks.push(data.name)
    } else {
      messages.push(`✔ No issues detected`)
    }
    taskStatus[data.name] = messages
  }

  if (failedTasks.length !== 0) {
    const messages = []
    for (const key of Object.keys(taskStatus)) {
      messages.push(
        `${key}:\n  ${taskStatus[key].join('\n').replace(/\n/g, '\n  ')}`
      )
    }

    showError(
      new Error(
        `${failedTasks.length > 1 ? 'Errors' : 'Error'} in ${failedTasks
          .map(s => `'${s}'`)
          .join(', ')}\n${messages.join('\n')}`
      )
    )
  }
}
211551
def get_rupture(oqparam):
    
    rup_model = oqparam.inputs['rupture_model']
    [rup_node] = nrml.read(rup_model)
    conv = sourceconverter.RuptureConverter(
        oqparam.rupture_mesh_spacing, oqparam.complex_fault_mesh_spacing)
    rup = conv.convert_node(rup_node)
    rup.tectonic_region_type = '*'  # there is not TRT for scenario ruptures
    rup.serial = oqparam.random_seed
    return rup
738338
def get_rup_array(ebruptures, srcfilter=nofilter):
    
    if not BaseRupture._code:
        BaseRupture.init()  # initialize rupture codes

    rups = []
    geoms = []
    nbytes = 0
    offset = 0
    for ebrupture in ebruptures:
        rup = ebrupture.rupture
        mesh = surface_to_array(rup.surface)
        sy, sz = mesh.shape[1:]  # sanity checks
        assert sy < TWO16, 'Too many multisurfaces: %d' % sy
        assert sz < TWO16, 'The rupture mesh spacing is too small'
        points = mesh.reshape(3, -1).T   # shape (n, 3)
        minlon = points[:, 0].min()
        minlat = points[:, 1].min()
        maxlon = points[:, 0].max()
        maxlat = points[:, 1].max()
        if srcfilter.integration_distance and len(srcfilter.close_sids(
                (minlon, minlat, maxlon, maxlat),
                rup.tectonic_region_type, rup.mag)) == 0:
            continue
        hypo = rup.hypocenter.x, rup.hypocenter.y, rup.hypocenter.z
        rate = getattr(rup, 'occurrence_rate', numpy.nan)
        tup = (ebrupture.serial, ebrupture.srcidx, ebrupture.grp_id,
               rup.code, ebrupture.n_occ, rup.mag, rup.rake, rate,
               minlon, minlat, maxlon, maxlat,
               hypo, offset, offset + len(points), sy, sz)
        offset += len(points)
        rups.append(tup)
        geoms.append(numpy.array([tuple(p) for p in points], point3d))
        nbytes += rupture_dt.itemsize + mesh.nbytes
    if not rups:
        return ()
    dic = dict(geom=numpy.concatenate(geoms), nbytes=nbytes)
    # TODO: PMFs for nonparametric ruptures are not converted
    return hdf5.ArrayWrapper(numpy.array(rups, rupture_dt), dic)
23389
def update_eff_ruptures(self, count_ruptures):
        
        for smodel in self.source_models:
            for sg in smodel.src_groups:
                sg.eff_ruptures = (count_ruptures(sg.id)
                                   if callable(count_ruptures)
                                   else count_ruptures.get(sg.id, 0))
607417
private static double probObsGivenTruth(Base observed, Genotype trueGenotype,
      ReadStatus trueStatus) {
    return P_OBS_GIVEN_TRUTH.get(toTableIndex(observed, trueGenotype, trueStatus));
  }
1192829
def export(self, mesh, rlzs_by_gsim, num_ses):
        
        rupture = self.rupture
        events = self.get_events(rlzs_by_gsim)
        events_by_ses = self.get_events_by_ses(events, num_ses)
        new = ExportedRupture(self.serial, events_by_ses)
        new.mesh = mesh[()]
        if isinstance(rupture.surface, geo.ComplexFaultSurface):
            new.typology = 'complexFaultsurface'
        elif isinstance(rupture.surface, geo.SimpleFaultSurface):
            new.typology = 'simpleFaultsurface'
        elif isinstance(rupture.surface, geo.GriddedSurface):
            new.typology = 'griddedRupture'
        elif isinstance(rupture.surface, geo.MultiSurface):
            new.typology = 'multiPlanesRupture'
        else:
            new.typology = 'singlePlaneRupture'
        new.is_from_fault_source = iffs = isinstance(
            rupture.surface, (geo.ComplexFaultSurface,
                              geo.SimpleFaultSurface))
        new.is_gridded_surface = igs = isinstance(
            rupture.surface, geo.GriddedSurface)
        new.is_multi_surface = ims = isinstance(
            rupture.surface, geo.MultiSurface)
        new.lons, new.lats, new.depths = get_geom(
            rupture.surface, iffs, ims, igs)
        new.surface = rupture.surface
        new.strike = rupture.surface.get_strike()
        new.dip = rupture.surface.get_dip()
        new.rake = rupture.rake
        new.hypocenter = rupture.hypocenter
        new.tectonic_region_type = rupture.tectonic_region_type
        new.magnitude = new.mag = rupture.mag
        new.top_left_corner = None if iffs or ims or igs else (
            new.lons[0], new.lats[0], new.depths[0])
        new.top_right_corner = None if iffs or ims or igs else (
            new.lons[1], new.lats[1], new.depths[1])
        new.bottom_left_corner = None if iffs or ims or igs else (
            new.lons[2], new.lats[2], new.depths[2])
        new.bottom_right_corner = None if iffs or ims or igs else (
            new.lons[3], new.lats[3], new.depths[3])
        return new
674363
def blob_counter(self):
        """"""
        import aa    # pylint: disablF0401        # noqa
        from ROOT import EventFile    # pylint: disable F0401

        try:
            event_file = EventFile(self.filename)
        except Exception:
            raise SystemExit("Could not open file")

        num_blobs = 0
        for event in event_file:
            num_blobs += 1

        return num_blobs
602726
def update_event_data(self, num, data):
        """"""
        if num < 0 or num >= self.params["events_num"]:
            raise IndexError("Index out of range [0:%s]" %
                             (self.params["events_num"]))

        if isinstance(data, np.ndarray):
            raise TypeError("data should be np.ndarray")

        if data.dtype != np.short:
            raise TypeError("data array dtype should be dtype('int16')")

        ch_num = self.params['channel_number']
        ev_size = self.params['b_size']

        if data.shape != (ch_num * ev_size,):
            raise Exception("data should contain same number of elements "
                            "(%s)" % (ch_num * ev_size))

        self.file.seek(7168 + num * (96 + 2 * ch_num * ev_size) + 96)
        self.file.write(data.tostring())
        self.file.flush()