@@ -5387,35 +5387,26 @@ def apply(self, population):
53875387 # Make some summary statistics
53885388 # Get the dataframe and isolate the important information
53895389 df = population .props
5390- # dump dataframe each month if population size is large (used to find the minimum viable population size)
5391- thoseininjuries = df .loc [df .rt_road_traffic_inc ]
5390+ population_with_injuries = df .loc [df .rt_road_traffic_inc ]
53925391 # ================================= Injury severity ===========================================================
5393- sev = thoseininjuries ['rt_inj_severity' ]
5394- rural_injuries = df .loc [df .rt_road_traffic_inc & ~ df .li_urban ]
5395- if len (rural_injuries ) > 0 :
5396- percent_sev_rural = \
5397- len (rural_injuries .loc [rural_injuries ['rt_inj_severity' ] == 'severe' ]) / len (rural_injuries )
5398- else :
5399- percent_sev_rural = 'none_injured'
5400- urban_injuries = df .loc [df .rt_road_traffic_inc & df .li_urban ]
5401- if len (urban_injuries ) > 0 :
5402- percent_sev_urban = \
5403- len (urban_injuries .loc [urban_injuries ['rt_inj_severity' ] == 'severe' ]) / len (urban_injuries )
5404- else :
5405- percent_sev_urban = 'none_injured'
5406- severity , severitycount = np .unique (sev , return_counts = True )
5407- if 'mild' in severity :
5408- idx = np .where (severity == 'mild' )
5409- self .totmild += len (idx )
5410- if 'severe' in severity :
5411- idx = np .where (severity == 'severe' )
5412- self .totsevere += len (idx )
5392+ population_subsets_with_injuries = {
5393+ "rural" : population_with_injuries .loc [~ population_with_injuries .li_urban ],
5394+ "urban" : population_with_injuries .loc [population_with_injuries .li_urban ],
5395+ }
5396+ proportion_severely_injured = {
5397+ label : (
5398+ len (pop_subset .loc [pop_subset ['rt_inj_severity' ] == 'severe' ])
5399+ / len (pop_subset )
5400+ ) if len (pop_subset ) > 0 else "none_injured"
5401+ for label , pop_subset in population_subsets_with_injuries .items ()
5402+ }
5403+ self .totmild += (population_with_injuries .rt_inj_severity == "mild" ).sum ()
5404+ self .totsevere += (population_with_injuries .rt_inj_severity == "severe" ).sum ()
54135405 dict_to_output = {
54145406 'total_mild_injuries' : self .totmild ,
5415- ''
5416- '_severe_injuries' : self .totsevere ,
5417- 'Percent_severe_rural' : percent_sev_rural ,
5418- 'Percent_severe_urban' : percent_sev_urban
5407+ 'total_severe_injuries' : self .totsevere ,
5408+ 'proportion_severe_rural' : proportion_severely_injured ["rural" ],
5409+ 'proportion_severe_urban' : proportion_severely_injured ["urban" ],
54195410 }
54205411 logger .info (key = 'injury_severity' ,
54215412 data = dict_to_output ,
0 commit comments