Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scenario_risk as a library #3484

Merged
merged 8 commits into from Mar 5, 2018
Merged

scenario_risk as a library #3484

merged 8 commits into from Mar 5, 2018

Conversation

micheles
Copy link
Contributor

Fixing the example in #3127 to work with the current master:

import numpy
from openquake.commonlib import readinput
from openquake.calculators import getters
from openquake.risklib import riskinput


def main(job_ini):
    oq = readinput.get_oqparam(job_ini)
    # extract the sites and the assets from the exposure
    sitecol, assetcol = readinput.get_sitecol_assetcol(oq)
    # filter the sites with the rupture
    rupture, sitecol = readinput.get_rupture_sitecol(oq, sitecol)
    # build the dictionary rlzs_by_gsim
    rlzs_by_gsim = readinput.get_rlzs_by_gsim(oq)
    # read the full risk model
    riskmodel = readinput.get_risk_model(oq)
    riskmodel.taxonomy = assetcol.tagcol.taxonomy
    # extract the minimum intensity dictionary
    min_iml = riskmodel.get_min_iml()
    # build the gmfgetter
    gmfgetter = getters.GmfGetter(
        rlzs_by_gsim, [rupture], sitecol.complete, oq.imtls,
        [min_iml[imt] for imt in oq.imtls], oq.maximum_distance,
        oq.truncation_level, oq.get_correl_model())
    gmfgetter.init()
    # build the epsilons
    eps = riskinput.make_eps(
        assetcol, oq.number_of_ground_motion_fields,
        oq.master_seed, oq.asset_correlation)
    # build the riskinput object
    rinput = riskinput.RiskInput(gmfgetter, assetcol.assets_by_site(), eps)
    R = len(rlzs_by_gsim)  # number of GSIMs
    L = len(oq.lti)  # number of loss types
    A = len(assetcol)  # number of assets
    I = oq.insured_losses + 1  # insured losses
    # compute average losses for each asset, averaging on the events
    avglosses = numpy.zeros((A, L, I, R))
    for out in riskmodel.gen_outputs(rinput):
        # out has shape (L, A, E, I)
        for a, asset in enumerate(out.assets):
            mean = out.array[:, a, :, :].mean(axis=1)  # on the events
            avglosses[asset.ordinal, :, :, out.rlzi] += mean
    # compute total by summing on all assets
    print(avglosses.sum(axis=0))  # shape (L, I, R)


if __name__ == '__main__':
    import sys
    main(sys.argv[1])

@micheles micheles added this to the Engine 3.0.0 milestone Feb 28, 2018
@micheles micheles requested a review from raoanirudh March 5, 2018 13:40
@micheles micheles merged commit 2d84926 into master Mar 5, 2018
@micheles micheles deleted the get_sitecol_assetcol branch March 5, 2018 15:15
Copy link
Member

@raoanirudh raoanirudh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I get the following error trying to run scenario_risk/case_master:

Traceback (most recent call last):
  File "scenario_risk.py", line 14, in <module>
    rlzs_by_gsim = readinput.get_rlzs_by_gsim(oq)
  File "../oq-engine/openquake/commonlib/readinput.py", line 379, in get_rlzs_by_gsim
    dic[gsim_by_trt['*']] = [rlzi]
KeyError: '*'

@micheles
Copy link
Contributor Author

micheles commented Mar 5, 2018

Uhm... I wonder why the engine case_master is working ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants