def test_no_filters(self):
        (mags, dists, lons, lats, trts, trt_bins, probs_no_exceed) = disagg._collect_bins_data(
            self.sources,
            self.site,
            self.imt,
            self.iml,
            self.gsims,
            self.truncation_level,
            n_epsilons=3,
            source_site_filter=filters.source_site_noop_filter,
            rupture_site_filter=filters.rupture_site_noop_filter,
        )

        aae = numpy.testing.assert_array_equal

        aae(mags, [5, 5, 5, 5, 9, 5, 5, 5, 6, 6, 6, 8, 7])
        aae(dists, [3, 11, 12, 13, 14, 11, 11, 10, 12, 12, 11, 5, 5])
        aae(lons, [22, 22, 22, 22, 21, 21, 21, 21, 22, 21, 22, 11, 11])
        aae(lats, [44, 44, 45, 45, 44, 44, 45, 45, 44, 44, 45, 45, 46])
        aae(trts, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

        poe = numpy.array(
            [
                [0, 0, 0],
                [0.1, 0.2, 0.1],
                [0, 0, 0.3],
                [0, 0.05, 0.001],
                [0, 0, 0],
                [0, 0, 0.02],
                [0.04, 0.1, 0.04],
                [0.2, 0.3, 0.2],
                [0.3, 0.4, 0.3],
                [0, 0, 0.1],
                [0, 0, 0],
                [0, 0.1, 0.04],
                [0.1, 0.5, 0.1],
            ]
        )
        p_one_more = numpy.array([0.1, 0.2, 0.01, 0.33, 0.4, 0.05, 0.53, 0.066, 0.1, 0.1, 0.1, 0.04, 0.03]).reshape(
            13, 1
        )
        exp_p_ne = (1 - p_one_more) ** poe
        aae(probs_no_exceed, exp_p_ne)
        self.assertEqual(trt_bins, ["trt1", "trt2"])
Example #2
0
    def test_no_filters(self):
        (mags, dists, lons, lats, trts, trt_bins, probs_one_or_more,
         probs_exceed_given_rup, src_idxs) = disagg._collect_bins_data(
             self.sources,
             self.site,
             self.imt,
             self.iml,
             self.gsims,
             self.tom,
             self.truncation_level,
             n_epsilons=3,
             source_site_filter=filters.source_site_noop_filter,
             rupture_site_filter=filters.rupture_site_noop_filter)

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [5, 5, 5, 5, 9, 5, 5, 5, 6, 6, 6, 8, 7])
        aae(dists, [3, 11, 12, 13, 14, 11, 11, 10, 12, 12, 11, 5, 5])
        aae(lons, [22, 22, 22, 22, 21, 21, 21, 21, 22, 21, 22, 11, 11])
        aae(lats, [44, 44, 45, 45, 44, 44, 45, 45, 44, 44, 45, 45, 46])
        aae(trts, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

        exp_pegr = [
            [0, 0, 0],
            [0.1, 0.2, 0.1],
            [0, 0, 0.3],
            [0, 0.05, 0.001],
            [0, 0, 0],
            [0, 0, 0.02],
            [0.04, 0.1, 0.04],
            [0.2, 0.3, 0.2],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
            [0, 0.1, 0.04],
            [0.1, 0.5, 0.1],
        ]
        aae(probs_exceed_given_rup, exp_pegr)
        aae(probs_one_or_more, [
            0.1, 0.2, 0.01, 0.33, 0.4, 0.05, 0.53, 0.066, 0.1, 0.1, 0.1, 0.04,
            0.03
        ])
        self.assertEqual(trt_bins, ['trt1', 'trt2'])
Example #3
0
    def test_filters(self):
        def source_site_filter(sources_sites):
            for source, sites in sources_sites:
                if source is self.source2:
                    continue
                yield source, sites

        def rupture_site_filter(rupture_sites):
            for rupture, sites in rupture_sites:
                if rupture.mag < 6:
                    continue
                yield rupture, sites

        (mags, dists, lons, lats, trts, trt_bins, probs_one_or_more,
         probs_exceed_given_rup, src_idxs) = disagg._collect_bins_data(
             self.sources,
             self.site,
             self.imt,
             self.iml,
             self.gsims,
             self.tom,
             self.truncation_level,
             n_epsilons=3,
             source_site_filter=source_site_filter,
             rupture_site_filter=rupture_site_filter)

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [9, 6, 6, 6])
        aae(dists, [14, 12, 12, 11])
        aae(lons, [21, 22, 21, 22])
        aae(lats, [44, 44, 44, 45])
        aae(trts, [0, 0, 0, 0])
        exp_pegr = [
            [0, 0, 0],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
        ]
        aae(probs_exceed_given_rup, exp_pegr)
        aae(probs_one_or_more, [0.4, 0.1, 0.1, 0.1])
        self.assertEqual(trt_bins, ['trt1'])
Example #4
0
    def test_no_filters(self):
        mags, dists, lons, lats, joint_probs, trts, trt_bins = disagg._collect_bins_data(
            self.sources,
            self.site,
            self.imt,
            self.iml,
            self.gsims,
            self.tom,
            self.truncation_level,
            n_epsilons=3,
            source_site_filter=filters.source_site_noop_filter,
            rupture_site_filter=filters.rupture_site_noop_filter,
        )

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [5, 5, 5, 5, 9, 5, 5, 5, 6, 6, 6, 8, 7])
        aae(dists, [3, 11, 12, 13, 14, 11, 11, 10, 12, 12, 11, 5, 5])
        aae(lons, [22, 22, 22, 22, 21, 21, 21, 21, 22, 21, 22, 11, 11])
        aae(lats, [44, 44, 45, 45, 44, 44, 45, 45, 44, 44, 45, 45, 46])
        aaae(
            joint_probs,
            [
                [0.0, 0.0, 0.0],
                [0.02, 0.04, 0.02],
                [0.0, 0.0, 0.003],
                [0.0, 0.0165, 0.00033],
                [0.0, 0.0, 0.0],
                [0.0, 0.0, 0.001],
                [0.0212, 0.053, 0.0212],
                [0.0132, 0.0198, 0.0132],
                [0.03, 0.04, 0.03],
                [0.0, 0.0, 0.01],
                [0.0, 0.0, 0.0],
                [0.0, 0.004, 0.0016],
                [0.003, 0.015, 0.003],
            ],
        )
        aae(trts, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])
        self.assertEqual(trt_bins, ["trt1", "trt2"])
    def test_filters(self):
        def source_site_filter(sources_sites):
            for source, sites in sources_sites:
                if source is self.source2:
                    continue
                yield source, sites

        def rupture_site_filter(rupture_sites):
            for rupture, sites in rupture_sites:
                if rupture.mag < 6:
                    continue
                yield rupture, sites

        (mags, dists, lons, lats, trts, trt_bins, probs_no_exceed) = \
            disagg._collect_bins_data(
                self.sources, self.site, self.imt, self.iml, self.gsims,
                self.truncation_level, n_epsilons=3,
                source_site_filter=source_site_filter,
                rupture_site_filter=rupture_site_filter
                )

        aae = numpy.testing.assert_array_equal

        aae(mags, [9, 6, 6, 6])
        aae(dists, [14, 12, 12, 11])
        aae(lons, [21, 22, 21, 22])
        aae(lats, [44, 44, 44, 45])
        aae(trts, [0, 0, 0, 0])
        poe = numpy.array([
            [0, 0, 0],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
        ])
        p_one_more = numpy.array(
            [0.4, 0.1, 0.1, 0.1]
        ).reshape(4, 1)
        exp_p_ne = (1 - p_one_more) ** poe
        aae(probs_no_exceed, exp_p_ne)
        self.assertEqual(trt_bins, ['trt1'])
Example #6
0
    def test_filters(self):
        def source_site_filter(sources, sites):
            for source in sources:
                if source is self.source2:
                    continue
                yield source, sites

        def rupture_site_filter(ruptures, sites):
            for rupture in ruptures:
                if rupture.mag < 6:
                    continue
                yield rupture, sites

        (mags, dists, lons, lats, trts, trt_bins, probs_no_exceed) = \
            disagg._collect_bins_data(
                self.sources, self.site, self.imt, self.iml, self.gsims,
                self.truncation_level, n_epsilons=3,
                source_site_filter=source_site_filter,
                rupture_site_filter=rupture_site_filter
                )

        aae = numpy.testing.assert_array_equal

        aae(mags, [9, 6, 6, 6])
        aae(dists, [14, 12, 12, 11])
        aae(lons, [21, 22, 21, 22])
        aae(lats, [44, 44, 44, 45])
        aae(trts, [0, 0, 0, 0])
        poe = numpy.array([
            [0, 0, 0],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
        ])
        p_one_more = numpy.array(
            [0.4, 0.1, 0.1, 0.1]
        ).reshape(4, 1)
        exp_p_ne = (1 - p_one_more) ** poe
        aae(probs_no_exceed, exp_p_ne)
        self.assertEqual(trt_bins, ['trt1'])
Example #7
0
    def test_no_filters(self):
        (mags, dists, lons, lats, trts, trt_bins, probs_no_exceed) = \
            disagg._collect_bins_data(
                self.sources, self.site, self.imt, self.iml, self.gsims,
                self.truncation_level, n_epsilons=3,
                source_site_filter=filters.source_site_noop_filter,
                rupture_site_filter=filters.rupture_site_noop_filter
                )

        aae = numpy.testing.assert_array_equal

        aae(mags, [5, 5, 5, 5, 9, 5, 5, 5, 6, 6, 6, 8, 7])
        aae(dists, [3, 11, 12, 13, 14, 11, 11, 10, 12, 12, 11, 5, 5])
        aae(lons, [22, 22, 22, 22, 21, 21, 21, 21, 22, 21, 22, 11, 11])
        aae(lats, [44, 44, 45, 45, 44, 44, 45, 45, 44, 44, 45, 45, 46])
        aae(trts, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

        poe = numpy.array([
            [0, 0, 0],
            [0.1, 0.2, 0.1],
            [0, 0, 0.3],
            [0, 0.05, 0.001],
            [0, 0, 0],
            [0, 0, 0.02],
            [0.04, 0.1, 0.04],
            [0.2, 0.3, 0.2],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
            [0, 0.1, 0.04],
            [0.1, 0.5, 0.1],
        ])
        p_one_more = numpy.array([
            0.1, 0.2, 0.01, 0.33, 0.4, 0.05, 0.53, 0.066, 0.1, 0.1, 0.1, 0.04,
            0.03
        ]).reshape(13, 1)
        exp_p_ne = (1 - p_one_more)**poe
        aae(probs_no_exceed, exp_p_ne)
        self.assertEqual(trt_bins, ['trt1', 'trt2'])
    def test_filters(self):
        def source_site_filter(sources_sites):
            for source, sites in sources_sites:
                if source is self.source2:
                    continue
                yield source, sites
        def rupture_site_filter(rupture_sites):
            for rupture, sites in rupture_sites:
                if rupture.mag < 6:
                    continue
                yield rupture, sites

        (mags, dists, lons, lats, trts, trt_bins, probs_one_or_more,
         probs_exceed_given_rup, src_idxs) = disagg._collect_bins_data(
            self.sources, self.site, self.imt, self.iml, self.gsims,
            self.tom, self.truncation_level, n_epsilons=3,
            source_site_filter=source_site_filter,
            rupture_site_filter=rupture_site_filter
        )

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [9, 6, 6, 6])
        aae(dists, [14, 12, 12, 11])
        aae(lons, [21, 22, 21, 22])
        aae(lats, [44, 44, 44, 45])
        aae(trts, [0, 0, 0, 0])
        exp_pegr = [
            [0, 0, 0],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
        ]
        aae(probs_exceed_given_rup, exp_pegr)
        aae(probs_one_or_more, [0.4, 0.1, 0.1, 0.1])
        self.assertEqual(trt_bins, ['trt1'])
    def test_no_filters(self):
        (mags, dists, lons, lats, trts, trt_bins, probs_one_or_more,
         probs_exceed_given_rup, src_idxs) = disagg._collect_bins_data(
            self.sources, self.site, self.imt, self.iml, self.gsims,
            self.tom, self.truncation_level, n_epsilons=3,
            source_site_filter=filters.source_site_noop_filter,
            rupture_site_filter=filters.rupture_site_noop_filter
        )

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [5, 5, 5, 5, 9, 5, 5, 5, 6, 6, 6, 8, 7])
        aae(dists, [3, 11, 12, 13, 14, 11, 11, 10, 12, 12, 11, 5, 5])
        aae(lons, [22, 22, 22, 22, 21, 21, 21, 21, 22, 21, 22, 11, 11])
        aae(lats, [44, 44, 45, 45, 44, 44, 45, 45, 44, 44, 45, 45, 46])
        aae(trts, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1])

        exp_pegr = [
            [0, 0, 0],
            [0.1, 0.2, 0.1],
            [0, 0, 0.3],
            [0, 0.05, 0.001],
            [0, 0, 0],
            [0, 0, 0.02],
            [0.04, 0.1, 0.04],
            [0.2, 0.3, 0.2],
            [0.3, 0.4, 0.3],
            [0, 0, 0.1],
            [0, 0, 0],
            [0, 0.1, 0.04],
            [0.1, 0.5, 0.1],
        ]
        aae(probs_exceed_given_rup, exp_pegr)
        aae(probs_one_or_more, [0.1, 0.2, 0.01, 0.33, 0.4, 0.05, 0.53, 0.066,
                                0.1, 0.1, 0.1, 0.04, 0.03])
        self.assertEqual(trt_bins, ['trt1', 'trt2'])
Example #10
0
    def test_filters(self):
        def source_site_filter(sources_sites):
            for source, sites in sources_sites:
                if source is self.source2:
                    continue
                yield source, sites

        def rupture_site_filter(rupture_sites):
            for rupture, sites in rupture_sites:
                if rupture.mag < 6:
                    continue
                yield rupture, sites

        mags, dists, lons, lats, joint_probs, trts, trt_bins = disagg._collect_bins_data(
            self.sources,
            self.site,
            self.imt,
            self.iml,
            self.gsims,
            self.tom,
            self.truncation_level,
            n_epsilons=3,
            source_site_filter=source_site_filter,
            rupture_site_filter=rupture_site_filter,
        )

        aae = numpy.testing.assert_array_equal
        aaae = numpy.testing.assert_array_almost_equal

        aae(mags, [9, 6, 6, 6])
        aae(dists, [14, 12, 12, 11])
        aae(lons, [21, 22, 21, 22])
        aae(lats, [44, 44, 44, 45])
        aaae(joint_probs, [[0.0, 0.0, 0.0], [0.03, 0.04, 0.03], [0.0, 0.0, 0.01], [0.0, 0.0, 0.0]])
        aae(trts, [0, 0, 0, 0])
        self.assertEqual(trt_bins, ["trt1"])
Example #11
0
def compute_disagg(sitecol, sources, src_group_id, rlzs_assoc, trt_names, curves_dict, bin_edges, oqparam, monitor):
    # see https://bugs.launchpad.net/oq-engine/+bug/1279247 for an explanation
    # of the algorithm used
    """
    :param sitecol:
        a :class:`openquake.hazardlib.site.SiteCollection` instance
    :param sources:
        list of hazardlib source objects
    :param src_group_id:
        numeric ID of a SourceGroup instance
    :param rlzs_assoc:
        a :class:`openquake.commonlib.source.RlzsAssoc` instance
    :param dict trt_names:
        a tuple of names for the given tectonic region type
    :param curves_dict:
        a dictionary with the hazard curves for sites, realizations and IMTs
    :param bin_egdes:
        a dictionary site_id -> edges
    :param oqparam:
        the parameters in the job.ini file
    :param monitor:
        monitor of the currently running job
    :returns:
        a dictionary of probability arrays, with composite key
        (sid, rlz.id, poe, imt, iml, trt_names).
    """
    trt = sources[0].tectonic_region_type
    try:
        max_dist = oqparam.maximum_distance[trt]
    except KeyError:
        max_dist = oqparam.maximum_distance["default"]
    trt_num = dict((trt, i) for i, trt in enumerate(trt_names))
    gsims = rlzs_assoc.gsims_by_grp_id[src_group_id]
    result = {}  # sid, rlz.id, poe, imt, iml, trt_names -> array

    collecting_mon = monitor("collecting bins")
    arranging_mon = monitor("arranging bins")

    for site, sid in zip(sitecol, sitecol.sids):
        # edges as wanted by disagg._arrange_data_in_bins
        try:
            edges = bin_edges[sid]
        except KeyError:
            # bin_edges for a given site are missing if the site is far away
            continue

        # generate source, rupture, sites once per site
        sitecol = SiteCollection([site])
        source_ruptures = [
            (src, src.iter_ruptures()) for src in sources if src.filter_sites_by_distance_to_source(max_dist, sitecol)
        ]
        if not source_ruptures:
            continue
        with collecting_mon:
            bdata = disagg._collect_bins_data(
                trt_num,
                source_ruptures,
                site,
                curves_dict[sid],
                src_group_id,
                rlzs_assoc,
                gsims,
                oqparam.imtls,
                oqparam.poes_disagg,
                oqparam.truncation_level,
                oqparam.num_epsilon_bins,
                monitor,
            )

        if not bdata.pnes:  # no contributions for this site
            continue

        for poe in oqparam.poes_disagg:
            for imt in oqparam.imtls:
                for gsim in gsims:
                    for rlz in rlzs_assoc[src_group_id, gsim]:
                        rlzi = rlz.ordinal
                        # extract the probabilities of non-exceedance for the
                        # given realization, disaggregation PoE, and IMT
                        iml_pne_pairs = [pne[rlzi, poe, imt] for pne in bdata.pnes]
                        iml = iml_pne_pairs[0][0]
                        probs = numpy.array([p for (i, p) in iml_pne_pairs], float)
                        # bins in a format handy for hazardlib
                        bins = [bdata.mags, bdata.dists, bdata.lons, bdata.lats, bdata.trts, None, probs]

                        # call disagg._arrange_data_in_bins
                        with arranging_mon:
                            key = (sid, rlzi, poe, imt, iml, trt_names)
                            matrix = disagg._arrange_data_in_bins(bins, edges + (trt_names,))
                            result[key] = numpy.array([fn(matrix) for fn in disagg.pmf_map.values()])
    return result
Example #12
0
def compute_disagg(src_filter, sources, src_group_id, rlzs_assoc, trt_names,
                   curves_dict, bin_edges, oqparam, monitor):
    # see https://bugs.launchpad.net/oq-engine/+bug/1279247 for an explanation
    # of the algorithm used
    """
    :param src_filter:
        a :class:`openquake.hazardlib.calc.filter.SourceFilter` instance
    :param sources:
        list of hazardlib source objects
    :param src_group_id:
        numeric ID of a SourceGroup instance
    :param rlzs_assoc:
        a :class:`openquake.commonlib.source.RlzsAssoc` instance
    :param dict trt_names:
        a tuple of names for the given tectonic region type
    :param curves_dict:
        a dictionary with the hazard curves for sites, realizations and IMTs
    :param bin_egdes:
        a dictionary site_id -> edges
    :param oqparam:
        the parameters in the job.ini file
    :param monitor:
        monitor of the currently running job
    :returns:
        a dictionary of probability arrays, with composite key
        (sid, rlz.id, poe, imt, iml, trt_names).
    """
    sitecol = src_filter.sitecol
    trt_num = dict((trt, i) for i, trt in enumerate(trt_names))
    gsims = rlzs_assoc.gsims_by_grp_id[src_group_id]
    result = {}  # sid, rlz.id, poe, imt, iml, trt_names -> array

    collecting_mon = monitor('collecting bins')
    arranging_mon = monitor('arranging bins')

    for site, sid in zip(sitecol, sitecol.sids):
        # edges as wanted by disagg._arrange_data_in_bins
        try:
            edges = bin_edges[sid]
        except KeyError:
            # bin_edges for a given site are missing if the site is far away
            continue

        # generate source, rupture, sites once per site
        with collecting_mon:
            bdata = disagg._collect_bins_data(
                trt_num, sources, site, curves_dict[sid], src_group_id,
                rlzs_assoc, gsims, oqparam.imtls, oqparam.poes_disagg,
                oqparam.truncation_level, oqparam.num_epsilon_bins,
                oqparam.iml_disagg, monitor)

        for (rlzi, poe, imt), iml_pne_pairs in bdata.pnes.items():
            # extract the probabilities of non-exceedance for the
            # given realization, disaggregation PoE, and IMT
            iml = iml_pne_pairs[0][0]
            probs = numpy.array([p for (i, p) in iml_pne_pairs], float)

            # bins in a format handy for hazardlib
            bins = [
                bdata.mags, bdata.dists, bdata.lons, bdata.lats, bdata.trts,
                None, probs
            ]

            # call disagg._arrange_data_in_bins
            with arranging_mon:
                key = (sid, rlzi, poe, imt, iml, trt_names)
                matrix = disagg._arrange_data_in_bins(bins,
                                                      edges + (trt_names, ))
                result[key] = numpy.array(
                    [fn(matrix) for fn in disagg.pmf_map.values()])
    return result