1818import re
1919import json
2020import copy
21- import logging
2221import functools
2322import collections
2423import numpy
@@ -140,7 +139,7 @@ def groupby_id(self):
140139 ddic = AccumDict (accum = AccumDict (accum = []))
141140 dic = AccumDict (accum = [])
142141 for rf in self :
143- dic [rf .id , rf . peril ].append (rf )
142+ dic [rf .id , getattr ( rf , ' peril' , 'groundshaking' ) ].append (rf )
144143 for (riskid , peril ), rfs in dic .items ():
145144 ddic [riskid ][peril ] = group_by_lt (rfs )
146145 num_perils = {riskid : len (ddic [riskid ]) for riskid in ddic }
@@ -525,33 +524,6 @@ class ValidationError(Exception):
525524 pass
526525
527526
528- # TODO: if the consequence tag is different from "taxonomy", pass
529- # the values of the exposure for that tag
530- def check_consequences (fname , taxonomies , perils ):
531- """
532- Check that the taxonomy field (if any) and the peril field (if any)
533- in the consequence file are consistent with the expected taxonomies
534- and perils
535- """
536- df = pandas .read_csv (fname )
537- if 'taxonomy' in df .columns :
538- csq_taxonomies = set (df ['taxonomy' ])
539- extra = csq_taxonomies - taxonomies
540- missing = taxonomies - csq_taxonomies
541- if not csq_taxonomies & taxonomies :
542- raise InvalidFile (f'{ fname } : no matching taxonomies' )
543- elif missing :
544- raise InvalidFile (f'{ fname } : missing taxonomies { missing } ' )
545- elif extra :
546- # tested in event_based_damage/case_15
547- logging .warning (f'In { fname } there are extra taxonomies missing '
548- f'in the exposure: { extra } ' )
549- if 'peril' in df .columns :
550- for line , peril in enumerate (df ['peril' ], 1 ):
551- if peril not in perils :
552- raise InvalidFile (f'{ fname } : unknown { peril = } at { line = } ' )
553-
554-
555527class CompositeRiskModel (collections .abc .Mapping ):
556528 """
557529 A container (riskid, kind) -> riskmodel
@@ -624,12 +596,27 @@ def set_tmap(self, tmap_df, taxidx):
624596 else :
625597 csq_files .append (fnames )
626598 for fname in csq_files :
627- check_consequences (fname , set (taxidx ), self .perils )
628- for byname , coeffs in self .consdict .items ():
629- # reduce the consdict to the taxonomies in the exposure
630- self .consdict [byname ] = {taxidx [taxo ]: arr
631- for taxo , arr in coeffs .items ()
632- if taxo in taxidx }
599+ df = pandas .read_csv (fname )
600+ if 'peril' in df .columns :
601+ for line , peril in enumerate (df ['peril' ], 1 ):
602+ if peril not in self .perils :
603+ raise InvalidFile (
604+ f'{ fname } : unknown { peril = } at { line = } ' )
605+
606+ cfs = '\n ' .join (csq_files )
607+ df = self .tmap_df
608+ for peril in self .perils :
609+ for byname , coeffs in self .consdict .items ():
610+ # ex. byname = "losses_by_taxonomy"
611+ if len (coeffs ):
612+ for per , risk_id , weight in zip (
613+ df .peril , df .risk_id , df .weight ):
614+ if (per == '*' or per == peril ) and risk_id != '?' :
615+ try :
616+ coeffs [risk_id ][peril ]
617+ except KeyError :
618+ raise InvalidFile (
619+ f'Missing { risk_id = } , { peril = } in\n { cfs } ' )
633620
634621 def check_risk_ids (self , inputs ):
635622 """
@@ -639,7 +626,8 @@ def check_risk_ids(self, inputs):
639626 for riskfunc in self .risklist :
640627 ids_by_kind [riskfunc .kind ].add (riskfunc .id )
641628 kinds = tuple (ids_by_kind ) # vulnerability, fragility, ...
642- fnames = [fname for kind , fname in inputs .items () if kind .endswith (kinds )]
629+ fnames = [fname for kind , fname in inputs .items ()
630+ if kind .endswith (kinds )]
643631 if len (ids_by_kind ) > 1 :
644632 k = next (iter (ids_by_kind ))
645633 base_ids = set (ids_by_kind .pop (k ))
@@ -687,7 +675,6 @@ def compute_csq(self, assets, dd5, tmap_df, oq):
687675 :returns: a dict consequence_name, loss_type -> array[P, A, E]
688676 """
689677 # by construction all assets have the same taxonomy
690- taxi = assets [0 ]['taxonomy' ]
691678 P , A , E , _L , _D = dd5 .shape
692679 csq = AccumDict (accum = numpy .zeros ((P , A , E )))
693680 for byname , coeffs in self .consdict .items ():
@@ -703,7 +690,7 @@ def compute_csq(self, assets, dd5, tmap_df, oq):
703690 else : # assume one weigth per peril
704691 [w ] = df [df .peril == peril ].weight
705692 coeff = (dd5 [pi , :, :, li , 1 :] @
706- coeffs [taxi ][peril ][lt ] * w )
693+ coeffs [risk_id ][peril ][lt ] * w )
707694 cAE = scientific .consequence (
708695 consequence , assets , coeff , lt , oq .time_event )
709696 csq [consequence , li ][pi ] += cAE
0 commit comments