def deaths_and_injuries_sum(self): low, med, high, unknown, all_levels = self._get_risk_model_rows() def empty_deaths(x): if x: return x.risk_model_deaths is None and x.risk_model_injuries is None def sum_deaths(x): if x: return (x.risk_model_deaths or 0) + (x.risk_model_injuries or 0) d = AttrDict({ 'low': None if empty_deaths(low) else sum_deaths(low), 'medium': None if empty_deaths(med) else sum_deaths(med), 'high': None if empty_deaths(high) else sum_deaths(high), 'unknown': None if empty_deaths(unknown) else sum_deaths(unknown), 'all': None if empty_deaths(all_levels) else sum_deaths(all_levels) }) return d
def national_risk_model_deaths_injuries_sum_quartile(self): # TODO: Need to optimize... ret = AttrDict() results = self.national_calculations.objects.filter( id=self.firedepartment.id) for numlevel, level in self.RISK_LEVELS: ret[level] = None for result in results: if result.level == numlevel: ret[level] = result.risk_model_deaths_injuries_sum_quartile return ret
def _get_risk_model_field(self, field): """ Return low/medium/high risk models """ low, med, high, unknown, all_levels = self._get_risk_model_rows() return AttrDict({ 'low': getattr(low, field, None), 'medium': getattr(med, field, None), 'high': getattr(high, field, None), 'unknown': getattr(unknown, field, None), 'all': getattr(all_levels, field, None) })
def community_fire_risk(self): return AttrDict({ 'low': self.population_metrics_rows.low.risk_model_fires_quartile, 'medium': self.population_metrics_rows.medium.risk_model_fires_quartile, 'high': self.population_metrics_rows.high.risk_model_fires_quartile, 'unknown': self.population_metrics_rows.unknown.risk_model_fires_quartile, 'all': self.population_metrics_rows.all.risk_model_fires_quartile })
def residential_fires_avg_3_years_breaks(self): """ Risk model fire count breaks for the bullet chart """ ret = AttrDict() for numlevel, level in self.RISK_LEVELS: vals = self.quartile_class.objects.get_field_stats( 'residential_fires_avg_3_years', group_by='residential_fires_avg_3_years_quartile', population_class=self.firedepartment.population_class, level=numlevel) ret[level] = [n['max'] for n in vals] return ret
def risk_model_deaths_injuries_breaks(self): """ Deaths and injuries for the bullet chart """ ret = AttrDict() for numlevel, level in self.RISK_LEVELS: vals = self.quartile_class.objects.get_field_stats( 'risk_model_deaths_injuries_sum', group_by='risk_model_deaths_injuries_sum_quartile', population_class=self.firedepartment.population_class, level=numlevel) ret[level] = [n['max'] for n in vals] return ret
def risk_model_greater_than_size_2_breaks(self): """ Size 2 or above fire breaks for the bullet chart """ ret = AttrDict() for numlevel, level in self.RISK_LEVELS: vals = self.quartile_class.objects.get_field_stats( 'risk_model_size1_percent_size2_percent_sum', group_by='risk_model_size1_percent_size2_percent_sum_quartile', population_class=self.firedepartment.population_class, level=numlevel) ret[level] = [n['max'] for n in vals] return ret
def dist_model_residential_fires_quartile_breaks(self): if not self.peers: self._get_peers() def get(df): if df is not None and not df.empty: return df.groupby(['dist_model_residential_fires_quartile' ]).max()['dist_model_score'].tolist() return AttrDict({ 'low': get(self.peers.low), 'medium': get(self.peers.medium), 'high': get(self.peers.high), 'unknown': get(self.peers.unknown), 'all': get(self.peers.all) })
def dist_model_risk_model_deaths_injuries_quartile_avg(self): if not self.peers: self._get_peers() def get(df): if df is not None and not df.empty: return df.dist_model_risk_model_deaths_injuries_quartile_avg.mean( ) return AttrDict({ 'low': get(self.peers.low), 'medium': get(self.peers.medium), 'high': get(self.peers.high), 'unknown': get(self.peers.unknown), 'all': get(self.peers.all) })
def dist_model_residential_fires_quartile(self): if not self.peers: self._get_peers() def get(df): if df is not None and not df.empty: return df.loc[ df['id'] == self.firedepartment. id].dist_model_residential_fires_quartile.values[0] return AttrDict({ 'low': get(self.peers.low), 'medium': get(self.peers.medium), 'high': get(self.peers.high), 'unknown': get(self.peers.unknown), 'all': get(self.peers.all) })
def dist_model_residential_fires_quartile_avg(self): """ Used as the "Assessment of performance score based on the number of fires." Safe Grade """ if not self.peers: self._get_peers() def get(df): if df is not None and not df.empty: return df.dist_model_residential_fires_quartile_avg.mean() return AttrDict({ 'low': get(self.peers.low), 'medium': get(self.peers.medium), 'high': get(self.peers.high), 'unknown': get(self.peers.unknown), 'all': get(self.peers.all) })
def community_death_and_injury_risk(self): return AttrDict({ 'low': self.population_metrics_rows.low. risk_model_deaths_injuries_sum_quartile, 'medium': self.population_metrics_rows.medium. risk_model_deaths_injuries_sum_quartile, 'high': self.population_metrics_rows.high. risk_model_deaths_injuries_sum_quartile, 'unknown': self.population_metrics_rows.unknown. risk_model_deaths_injuries_sum_quartile, 'all': self.population_metrics_rows.all. risk_model_deaths_injuries_sum_quartile })
def residential_structure_fire_counts(self): ret = AttrDict() for numlevel, level in self.RISK_LEVELS: v = self.firedepartment.nfirsstatistic_set.filter(metric='residential_structure_fires', level=numlevel)\ .extra(select={ 'year_max': "SELECT MAX(COUNT) FROM firestation_nfirsstatistic b WHERE b.fire_department_id = firestation_nfirsstatistic.fire_department_id AND b.year = firestation_nfirsstatistic.year and b.metric='fire_calls' and b.level = firestation_nfirsstatistic.level" })\ .extra(select={ 'year_min': "SELECT 0" }) ret[level] = [ dict(year=i.year, count=i.count, year_max=i.year_max, year_min=i.year_min) for i in v ] return ret
def dist_model_risk_model_deaths_injuries_quartile_breaks(self): """ Used as the "Assessment of performance score based on fire spread risk." Safe Grade """ if not self.peers: self._get_peers() def get(df): if df is not None and not df.empty: return df.groupby([ 'dist_model_risk_model_deaths_injuries_quartile' ]).max()['dist_model_score'].tolist() return AttrDict({ 'low': get(self.peers.low), 'medium': get(self.peers.medium), 'high': get(self.peers.high), 'unknown': get(self.peers.unknown), 'all': get(self.peers.all) })
def residential_fires_3_year_avg(self): def get(level): return self.firedepartment.nfirsstatistic_set.filter( metric='residential_structure_fires', year__gte=2010, level=level).aggregate(Avg('count')).get('count__avg') low = get(1) medium = get(2) high = get(4) unknown = get(5) all_levels = get(0) return AttrDict({ 'low': low, 'medium': medium, 'high': high, 'unknown': unknown, 'all': all_levels })
def population_metrics_rows(self): """ Returns the matching rows from the population metrics table. """ return AttrDict({ 'low': self.quartile_class.objects.filter(id=self.firedepartment.id, level=1).first(), 'medium': self.quartile_class.objects.filter(id=self.firedepartment.id, level=2).first(), 'high': self.quartile_class.objects.filter(id=self.firedepartment.id, level=4).first(), 'unknown': self.quartile_class.objects.filter(id=self.firedepartment.id, level=5).first(), 'all': self.quartile_class.objects.filter(id=self.firedepartment.id, level=0).first() })
def nfirs_deaths_and_injuries_sum(self): def get(level): return self.firedepartment.nfirsstatistic_set.filter( Q(metric='civilian_casualties') | Q(metric='firefighter_casualties'), level=level, year__gte=2010).aggregate(Avg('count')).get('count__avg') low = get(1) medium = get(2) high = get(4) unknown = get(5) all_levels = get(0) return AttrDict({ 'low': low, 'medium': medium, 'high': high, 'unknown': unknown, 'all': all_levels })
def __init__(self, firedepartment, quartile_class, national_calculations): self.firedepartment = firedepartment self.quartile_class = quartile_class self.national_calculations = national_calculations self.peers = AttrDict()