def handle(self, *args, **options): vals = FireDepartment.objects.filter(dist_model_score__isnull=True) dists = [] with PostgresBackend(dict(host='localhost')) as backend: for fd in vals: results = backend.get_firespread_counts(query=RESIDENTIAL_FIRES_BY_FDID_STATE, query_params=(fd.fdid, fd.state)) ahs_building_size = ahs_building_areas(fd.fdid, fd.state) if ahs_building_size is not None: results.update(dict(building_area_draw=ahs_building_size)) dist = DIST(floor_extent=False, **results) try: fd.dist_model_score = dist.gibbs_sample() except ZeroDivisionError: continue #print 'Updating {0} with the new DIST score: {1}.'.format(fd.name, fd.dist_model_score) if not options.get('dry_run'): fd.save() dists.append((fd.id, fd.dist_model_score)) print 'Updated dist scores: ', dists
def update_performance_score(id, dry_run=False): """ Updates department performance scores. """ try: cursor = connections['nfirs'].cursor() fd = FireDepartment.objects.get(id=id) except (ConnectionDoesNotExist, FireDepartment.DoesNotExist): return cursor.execute(RESIDENTIAL_FIRES_BY_FDID_STATE, (fd.fdid, fd.state)) results = dictfetchall(cursor) old_score = fd.dist_model_score counts = dict(object_of_origin=0, room_of_origin=0, floor_of_origin=0, building_of_origin=0, beyond=0) for result in results: if result['fire_sprd'] == '1': counts['object_of_origin'] += result['count'] if result['fire_sprd'] == '2': counts['room_of_origin'] += result['count'] if result['fire_sprd'] == '3': counts['floor_of_origin'] += result['count'] if result['fire_sprd'] == '4': counts['building_of_origin'] += result['count'] if result['fire_sprd'] == '5': counts['beyond'] += result['count'] ahs_building_size = ahs_building_areas(fd.fdid, fd.state) if ahs_building_size is not None: counts['building_area_draw'] = ahs_building_size response_times = response_time_distributions.get('{0}-{1}'.format(fd.fdid, fd.state)) if response_times: counts['arrival_time_draw'] = LogNormalDraw(*response_times, multiplier=60) try: dist = DIST(floor_extent=False, **counts) fd.dist_model_score = dist.gibbs_sample() except (NotEnoughRecords, ZeroDivisionError): fd.dist_model_score = None print 'updating fdid: {2} from: {0} to {1}.'.format(old_score, fd.dist_model_score, fd.id) if dry_run: return fd.save()
def test_dist_import(self): floor_extent = False results = {'floor_of_origin': 126896L, 'beyond': 108959L, 'object_of_origin': 383787L, 'room_of_origin': 507378L, 'building_of_origin': 529300L} dist = DIST(floor_extent=floor_extent, **results) self.assertAlmostEqual(dist.gibbs_sample(), 32.0, delta=4)