-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path06_nfirs_cause_analysis.py
More file actions
914 lines (730 loc) · 31.4 KB
/
Copy path06_nfirs_cause_analysis.py
File metadata and controls
914 lines (730 loc) · 31.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
#!/usr/bin/env python3
"""
Step 6: NFIRS Cause Analysis
=============================
Analyzes fire causes, sprinkler effects, and building characteristics by housing type
using detailed NFIRS data.
Addresses the questions:
- Why does multifamily have 4x higher structure fire rates?
- How do fire outcomes vary by building height (number of stories)?
Usage:
python 06_nfirs_cause_analysis.py
Input:
raw_data/nfirs/*/data/fireincident.txt
raw_data/nfirs/*/data/basicincident.txt
raw_data/nfirs/*/data/structurefire.txt
raw_data/nfirs/*/data/codelookup.txt
Output:
processed_data/nfirs_austin_detailed.csv
outputs/cause_by_housing_type.csv
outputs/heat_source_by_housing.csv
outputs/sprinkler_analysis.csv
outputs/building_height_analysis.csv
outputs/chart_cause_comparison.png
outputs/chart_sprinkler_effect.png
outputs/chart_building_height.png
"""
import pandas as pd
import numpy as np
import os
import glob
import warnings
warnings.filterwarnings('ignore')
# NFIRS Code Mappings
CAUSE_IGN_MAP = {
'0': 'Other',
'1': 'Intentional',
'2': 'Unintentional',
'3': 'Equipment failure',
'4': 'Act of nature',
'5': 'Under investigation',
'U': 'Undetermined'
}
PROP_USE_CATEGORIES = {
# Residential
'419': 'Single-family',
'429': 'Multifamily',
'439': 'Boarding/rooming',
'449': 'Hotel/motel',
'459': 'Residential board and care',
'460': 'Dormitory',
'462': 'Sorority/fraternity',
'464': 'Barracks',
'400': 'Residential (general)',
}
HEAT_SOURCE_CATEGORIES = {
# Cooking
'12': 'Cooking - stove/range',
'13': 'Cooking - oven',
'14': 'Cooking - grill/hibachi',
'15': 'Cooking - deep fryer',
# Heating
'10': 'Heating - general',
'11': 'Heating - furnace/boiler',
'12': 'Heating - fireplace',
'13': 'Heating - space heater',
# Electrical
'40': 'Electrical - general',
'41': 'Electrical - wiring',
'42': 'Electrical - extension cord',
'43': 'Electrical - appliance',
'44': 'Electrical - lamp/light',
# Smoking
'61': 'Smoking - cigarette',
'62': 'Smoking - pipe/cigar',
'63': 'Smoking - match',
# Open flame
'64': 'Open flame - candle',
'65': 'Open flame - lighter',
'66': 'Open flame - match',
}
AREA_ORIGIN_CATEGORIES = {
'21': 'Bedroom',
'22': 'Living room',
'23': 'Dining room',
'24': 'Kitchen',
'25': 'Bathroom',
'26': 'Laundry',
'27': 'Garage',
'51': 'Mechanical room',
'52': 'HVAC',
'53': 'Electrical',
'91': 'Exterior - wall',
'92': 'Exterior - roof',
'93': 'Exterior - balcony/porch',
}
def load_codelookup():
"""Load code lookup table from NFIRS data"""
print("\nLoading code lookups...")
lookup_files = glob.glob("raw_data/nfirs/*/data/codelookup.txt")
if not lookup_files:
print(" Warning: No codelookup.txt found")
return {}
# Use the first one (they should all be the same)
df = pd.read_csv(lookup_files[0], sep='^', quotechar='"', dtype=str)
df.columns = ['fieldid', 'code_value', 'code_descr']
# Build lookup dictionary
lookups = {}
for field in df['fieldid'].unique():
field_df = df[df['fieldid'] == field]
lookups[field] = dict(zip(field_df['code_value'], field_df['code_descr']))
print(f" Loaded {len(lookups)} code tables")
return lookups
def extract_austin_nfirs():
"""Extract Austin (WP801) fire incidents from national NFIRS data"""
print("\nExtracting Austin NFIRS data...")
# Find all fireincident files
fire_files = glob.glob("raw_data/nfirs/*/data/fireincident.txt")
basic_files = glob.glob("raw_data/nfirs/*/data/basicincident.txt")
struct_files = glob.glob("raw_data/nfirs/*/data/structurefire.txt")
if not fire_files:
print(" Error: No fireincident.txt files found")
return None
print(f" Found {len(fire_files)} years of NFIRS data")
if struct_files:
print(f" Found {len(struct_files)} structurefire files (building characteristics)")
# Load and filter fireincident data
fire_dfs = []
for f in fire_files:
year = f.split('/')[2] # Extract year from path
print(f" Processing fireincident {year}...")
df = pd.read_csv(f, sep='^', quotechar='"', dtype=str, low_memory=False, encoding='latin-1')
# Filter to Texas WP801 (Austin area)
df_austin = df[(df['STATE'] == 'TX') & (df['FDID'] == 'WP801')]
df_austin = df_austin.copy()
df_austin['YEAR'] = year
fire_dfs.append(df_austin)
print(f" Found {len(df_austin)} Austin fire incidents")
fire_df = pd.concat(fire_dfs, ignore_index=True)
print(f" Total fireincident records: {len(fire_df)}")
# Load and filter basicincident data
basic_dfs = []
for f in basic_files:
year = f.split('/')[2]
print(f" Processing basicincident {year}...")
df = pd.read_csv(f, sep='^', quotechar='"', dtype=str, low_memory=False, encoding='latin-1')
df_austin = df[(df['STATE'] == 'TX') & (df['FDID'] == 'WP801')]
df_austin = df_austin.copy()
df_austin['YEAR'] = year
basic_dfs.append(df_austin)
print(f" Found {len(df_austin)} Austin basic incidents")
basic_df = pd.concat(basic_dfs, ignore_index=True)
print(f" Total basicincident records: {len(basic_df)}")
# Join on incident key
key_cols = ['STATE', 'FDID', 'INC_DATE', 'INC_NO', 'EXP_NO']
# Select columns from fireincident
fire_cols = key_cols + ['YEAR', 'AREA_ORIG', 'HEAT_SOURC', 'FIRST_IGN', 'CAUSE_IGN',
'FACT_IGN_1', 'HUM_FAC_1', 'EQUIP_INV', 'FIRE_SPRD',
'AES_PRES', 'AES_TYPE', 'AES_OPER', 'AES_FAIL',
'DET_ALERT', 'DET_TYPE', 'DET_OPERAT', 'DET_EFFECT']
fire_cols = [c for c in fire_cols if c in fire_df.columns]
# Select columns from basicincident
basic_cols = key_cols + ['INC_TYPE', 'PROP_USE', 'PROP_LOSS', 'CONT_LOSS']
basic_cols = [c for c in basic_cols if c in basic_df.columns]
# Merge fire + basic
merged = fire_df[fire_cols].merge(
basic_df[basic_cols],
on=key_cols,
how='inner'
)
print(f"\n Merged fire+basic dataset: {len(merged)} records")
# Load and merge structurefire data (building characteristics)
if struct_files:
struct_dfs = []
for f in struct_files:
year = f.split('/')[2]
print(f" Processing structurefire {year}...")
df = pd.read_csv(f, sep='^', quotechar='"', dtype=str, low_memory=False, encoding='latin-1')
df_austin = df[(df['STATE'] == 'TX') & (df['FDID'] == 'WP801')]
df_austin = df_austin.copy()
df_austin['YEAR'] = year
struct_dfs.append(df_austin)
print(f" Found {len(df_austin)} Austin structure fire records")
struct_df = pd.concat(struct_dfs, ignore_index=True)
print(f" Total structurefire records: {len(struct_df)}")
# Select building characteristic columns
struct_cols = key_cols + [
'STRUC_TYPE', # Structure type (1=enclosed, 2=open, etc.)
'STRUC_STAT', # Structure status (under construction, normal use, etc.)
'BLDG_ABOVE', # Number of floors above grade
'BLDG_BELOW', # Number of floors below grade
'BLDG_LGTH', # Building length in feet
'BLDG_WDTH', # Building width in feet
'TOT_SQ_FT', # Total square footage
'FIRE_ORIG', # Floor of fire origin
]
struct_cols = [c for c in struct_cols if c in struct_df.columns]
merged = merged.merge(
struct_df[struct_cols],
on=key_cols,
how='left'
)
# Report on building height data availability
if 'BLDG_ABOVE' in merged.columns:
has_height = merged['BLDG_ABOVE'].notna() & (merged['BLDG_ABOVE'] != '')
print(f" Records with building height data: {has_height.sum()} ({has_height.mean()*100:.1f}%)")
else:
print("\n Note: No structurefire.txt files found - building height data unavailable")
print(f"\n Final merged dataset: {len(merged)} records")
return merged
def classify_housing_type(df):
"""Classify incidents by housing type based on PROP_USE code"""
print("\nClassifying housing types...")
def get_housing_type(prop_use):
if pd.isna(prop_use) or prop_use == '':
return 'Unknown'
prop_use = str(prop_use).strip()
# Single-family (1-2 family dwellings)
if prop_use in ['419']:
return 'Single-family'
# Multifamily (apartments, condos)
elif prop_use in ['429']:
return 'Multifamily'
# Other residential
elif prop_use.startswith('4'):
return 'Other residential'
else:
return 'Non-residential'
df['housing_type'] = df['PROP_USE'].apply(get_housing_type)
print(" Housing type distribution:")
print(df['housing_type'].value_counts().to_string())
return df
def classify_cause(df, lookups):
"""Add human-readable cause labels"""
print("\nClassifying fire causes...")
# Cause of ignition
df['cause_label'] = df['CAUSE_IGN'].map(CAUSE_IGN_MAP).fillna('Unknown')
# Heat source category
def categorize_heat_source(code):
if pd.isna(code) or code in ['', 'UU', 'NN']:
return 'Unknown'
code = str(code).strip()[:2] # First two digits
if code in ['12', '13', '14', '15']:
return 'Cooking'
elif code in ['10', '11']:
return 'Heating'
elif code.startswith('4'):
return 'Electrical'
elif code in ['61', '62', '63']:
return 'Smoking'
elif code in ['64', '65', '66']:
return 'Open flame'
else:
return 'Other'
df['heat_source_category'] = df['HEAT_SOURC'].apply(categorize_heat_source)
# Area of origin category
def categorize_area(code):
if pd.isna(code) or code in ['', 'UU', 'NN']:
return 'Unknown'
code = str(code).strip()
if code == '24':
return 'Kitchen'
elif code in ['21', '22', '23']:
return 'Living areas'
elif code in ['25', '26']:
return 'Bathroom/Laundry'
elif code == '27':
return 'Garage'
elif code in ['51', '52', '53']:
return 'Mechanical/Electrical'
elif code.startswith('9'):
return 'Exterior'
else:
return 'Other'
df['area_category'] = df['AREA_ORIG'].apply(categorize_area)
print(" Cause distribution:")
print(df['cause_label'].value_counts().to_string())
return df
def analyze_cause_by_housing(df):
"""Analyze fire causes by housing type"""
print("\n" + "="*80)
print("CAUSE OF IGNITION BY HOUSING TYPE")
print("="*80)
# Filter to residential only
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
if len(residential) == 0:
print(" Warning: No residential incidents found")
return None
# Cross-tabulation: cause by housing type
cause_counts = pd.crosstab(
residential['housing_type'],
residential['cause_label'],
margins=True
)
# Calculate percentages
cause_pct = pd.crosstab(
residential['housing_type'],
residential['cause_label'],
normalize='index'
) * 100
print("\nCause Counts:")
print(cause_counts.to_string())
print("\nCause Percentages:")
print(cause_pct.round(1).to_string())
# Calculate ratio of multifamily to single-family for each cause
if 'Single-family' in cause_pct.index and 'Multifamily' in cause_pct.index:
print("\nMultifamily vs Single-family Comparison:")
for cause in cause_pct.columns:
sf_pct = cause_pct.loc['Single-family', cause]
mf_pct = cause_pct.loc['Multifamily', cause]
diff = mf_pct - sf_pct
print(f" {cause}: SF={sf_pct:.1f}%, MF={mf_pct:.1f}%, Diff={diff:+.1f}%")
return cause_pct
def analyze_heat_source_by_housing(df):
"""Analyze heat sources by housing type"""
print("\n" + "="*80)
print("HEAT SOURCE BY HOUSING TYPE")
print("="*80)
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
if len(residential) == 0:
return None
# Cross-tabulation
heat_pct = pd.crosstab(
residential['housing_type'],
residential['heat_source_category'],
normalize='index'
) * 100
print("\nHeat Source Percentages:")
print(heat_pct.round(1).to_string())
return heat_pct
def analyze_area_origin_by_housing(df):
"""Analyze area of origin by housing type"""
print("\n" + "="*80)
print("AREA OF ORIGIN BY HOUSING TYPE")
print("="*80)
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
if len(residential) == 0:
return None
# Cross-tabulation
area_pct = pd.crosstab(
residential['housing_type'],
residential['area_category'],
normalize='index'
) * 100
print("\nArea of Origin Percentages:")
print(area_pct.round(1).to_string())
return area_pct
def analyze_sprinkler_effect(df):
"""Analyze sprinkler presence and effectiveness"""
print("\n" + "="*80)
print("SPRINKLER ANALYSIS")
print("="*80)
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
if len(residential) == 0:
return None
# Sprinkler presence by housing type
residential['sprinkler_present'] = residential['AES_PRES'].apply(
lambda x: 'Yes' if x == 'Y' else ('No' if x == 'N' else 'Unknown')
)
sprinkler_pct = pd.crosstab(
residential['housing_type'],
residential['sprinkler_present'],
normalize='index'
) * 100
print("\nSprinkler Presence by Housing Type:")
print(sprinkler_pct.round(1).to_string())
# Sprinkler operation when present
with_sprinkler = residential[residential['AES_PRES'] == 'Y'].copy()
if len(with_sprinkler) > 0:
with_sprinkler['sprinkler_operated'] = with_sprinkler['AES_OPER'].apply(
lambda x: 'Operated' if x == 'Y' else ('Did not operate' if x == 'N' else 'Unknown')
)
operation_pct = pd.crosstab(
with_sprinkler['housing_type'],
with_sprinkler['sprinkler_operated'],
normalize='index'
) * 100
print("\nSprinkler Operation (when present):")
print(operation_pct.round(1).to_string())
# Fire spread comparison
residential['fire_spread'] = residential['FIRE_SPRD'].apply(
lambda x: {
'1': 'Confined to object',
'2': 'Confined to room',
'3': 'Confined to floor',
'4': 'Confined to building',
'5': 'Beyond building'
}.get(str(x), 'Unknown') if pd.notna(x) else 'Unknown'
)
# Compare fire spread with vs without sprinklers
print("\nFire Spread - With Sprinklers:")
with_spr = residential[residential['AES_PRES'] == 'Y']
if len(with_spr) > 0:
print(with_spr['fire_spread'].value_counts(normalize=True).mul(100).round(1).to_string())
print("\nFire Spread - Without Sprinklers:")
without_spr = residential[residential['AES_PRES'] == 'N']
if len(without_spr) > 0:
print(without_spr['fire_spread'].value_counts(normalize=True).mul(100).round(1).to_string())
return sprinkler_pct
def analyze_building_height(df):
"""Analyze fire incidents by building height (number of stories)"""
print("\n" + "="*80)
print("BUILDING HEIGHT ANALYSIS")
print("="*80)
if 'BLDG_ABOVE' not in df.columns:
print(" No building height data available (structurefire.txt not loaded)")
return None
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
if len(residential) == 0:
return None
# Convert floors above grade to numeric
residential['floors_above'] = pd.to_numeric(residential['BLDG_ABOVE'], errors='coerce')
has_data = residential['floors_above'].notna()
print(f"\n Residential records with floor data: {has_data.sum()} of {len(residential)}")
if has_data.sum() == 0:
print(" No valid building height data for residential incidents")
return None
res_with_floors = residential[has_data].copy()
# Create height categories
def categorize_height(floors):
if floors <= 2:
return '1-2 stories (low-rise)'
elif floors <= 4:
return '3-4 stories (mid-rise)'
elif floors <= 7:
return '5-7 stories (mid-rise+)'
else:
return '8+ stories (high-rise)'
res_with_floors['height_category'] = res_with_floors['floors_above'].apply(categorize_height)
# Distribution of fires by building height
print("\nFire Incidents by Building Height:")
height_dist = res_with_floors['height_category'].value_counts().sort_index()
print(height_dist.to_string())
# Height distribution by housing type
print("\nBuilding Height by Housing Type:")
height_by_type = pd.crosstab(
res_with_floors['housing_type'],
res_with_floors['height_category'],
normalize='index'
) * 100
print(height_by_type.round(1).to_string())
# Fire spread by building height
if 'FIRE_SPRD' in res_with_floors.columns:
res_with_floors['fire_spread'] = res_with_floors['FIRE_SPRD'].apply(
lambda x: {
'1': 'Confined to object',
'2': 'Confined to room',
'3': 'Confined to floor',
'4': 'Confined to building',
'5': 'Beyond building'
}.get(str(x), 'Unknown') if pd.notna(x) else 'Unknown'
)
print("\nFire Spread by Building Height:")
spread_by_height = pd.crosstab(
res_with_floors['height_category'],
res_with_floors['fire_spread'],
normalize='index'
) * 100
print(spread_by_height.round(1).to_string())
# Cause of ignition by building height
if 'cause_label' in res_with_floors.columns:
print("\nCause of Ignition by Building Height:")
cause_by_height = pd.crosstab(
res_with_floors['height_category'],
res_with_floors['cause_label'],
normalize='index'
) * 100
print(cause_by_height.round(1).to_string())
# Sprinkler presence by building height
if 'AES_PRES' in res_with_floors.columns:
res_with_floors['sprinkler_present'] = res_with_floors['AES_PRES'].apply(
lambda x: 'Yes' if x == 'Y' else ('No' if x == 'N' else 'Unknown')
)
print("\nSprinkler Presence by Building Height:")
spr_by_height = pd.crosstab(
res_with_floors['height_category'],
res_with_floors['sprinkler_present'],
normalize='index'
) * 100
print(spr_by_height.round(1).to_string())
# Property loss by building height
if 'PROP_LOSS' in res_with_floors.columns:
res_with_floors['prop_loss_num'] = pd.to_numeric(res_with_floors['PROP_LOSS'], errors='coerce')
loss_by_height = res_with_floors.groupby('height_category')['prop_loss_num'].agg(['mean', 'median', 'count'])
print("\nProperty Loss by Building Height:")
print(loss_by_height.round(0).to_string())
# Summary stats
print(f"\nMean floors above grade: {res_with_floors['floors_above'].mean():.1f}")
print(f"Median floors above grade: {res_with_floors['floors_above'].median():.0f}")
print(f"Max floors above grade: {res_with_floors['floors_above'].max():.0f}")
# Save detailed results
height_summary = res_with_floors.groupby('height_category').agg(
incident_count=('floors_above', 'count'),
mean_floors=('floors_above', 'mean'),
).sort_index()
return height_summary
def create_building_height_chart(df):
"""Create building height analysis chart"""
import matplotlib.pyplot as plt
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])].copy()
residential['floors_above'] = pd.to_numeric(residential.get('BLDG_ABOVE'), errors='coerce')
res_with_floors = residential[residential['floors_above'].notna()].copy()
if len(res_with_floors) == 0:
return
def categorize_height(floors):
if floors <= 2:
return '1-2 stories'
elif floors <= 4:
return '3-4 stories'
elif floors <= 7:
return '5-7 stories'
else:
return '8+ stories'
res_with_floors['height_category'] = res_with_floors['floors_above'].apply(categorize_height)
# Fire spread by building height
res_with_floors['fire_spread'] = res_with_floors['FIRE_SPRD'].apply(
lambda x: {
'1': 'Confined to object',
'2': 'Confined to room',
'3': 'Confined to floor',
'4': 'Confined to building',
'5': 'Beyond building'
}.get(str(x), 'Unknown') if pd.notna(x) else 'Unknown'
)
fig, axes = plt.subplots(1, 2, figsize=(14, 6))
# Left: incident count by height category
height_order = ['1-2 stories', '3-4 stories', '5-7 stories', '8+ stories']
height_counts = res_with_floors['height_category'].value_counts().reindex(height_order).fillna(0)
axes[0].bar(height_counts.index, height_counts.values, color=['#2ca02c', '#ff7f0e', '#d62728', '#9467bd'])
axes[0].set_title('Fire Incidents by Building Height')
axes[0].set_ylabel('Number of Incidents')
axes[0].tick_params(axis='x', rotation=15)
for i, v in enumerate(height_counts.values):
axes[0].text(i, v + 1, str(int(v)), ha='center', fontweight='bold')
# Right: fire spread by height
spread_by_height = pd.crosstab(
res_with_floors['height_category'],
res_with_floors['fire_spread'],
normalize='index'
) * 100
spread_by_height = spread_by_height.reindex(height_order)
spread_cols = ['Confined to object', 'Confined to room', 'Confined to floor',
'Confined to building', 'Beyond building']
spread_cols = [c for c in spread_cols if c in spread_by_height.columns]
spread_by_height[spread_cols].plot(kind='bar', stacked=True, ax=axes[1],
colormap='RdYlGn_r')
axes[1].set_title('Fire Spread by Building Height')
axes[1].set_ylabel('Percentage of Fires (%)')
axes[1].set_xlabel('')
axes[1].tick_params(axis='x', rotation=15)
axes[1].legend(title='Fire Spread', bbox_to_anchor=(1.05, 1), loc='upper left', fontsize=8)
plt.tight_layout()
plt.savefig('outputs/chart_building_height.png', dpi=150, bbox_inches='tight')
plt.close()
print(" Saved: outputs/chart_building_height.png")
def create_visualizations(df, cause_pct, heat_pct, sprinkler_pct):
"""Create comparison charts"""
print("\nCreating visualizations...")
import matplotlib.pyplot as plt
# Chart 1: Cause of Ignition Comparison
if cause_pct is not None:
fig, ax = plt.subplots(figsize=(10, 6))
cause_pct_plot = cause_pct.drop('Unknown', axis=1, errors='ignore')
cause_pct_plot = cause_pct_plot[['Unintentional', 'Intentional', 'Equipment failure', 'Undetermined']]
x = range(len(cause_pct_plot.columns))
width = 0.35
if 'Single-family' in cause_pct_plot.index and 'Multifamily' in cause_pct_plot.index:
sf_vals = cause_pct_plot.loc['Single-family'].values
mf_vals = cause_pct_plot.loc['Multifamily'].values
bars1 = ax.bar([i - width/2 for i in x], sf_vals, width, label='Single-family', color='#2ca02c')
bars2 = ax.bar([i + width/2 for i in x], mf_vals, width, label='Multifamily', color='#d62728')
ax.set_ylabel('Percentage of Fires (%)')
ax.set_title('Cause of Ignition: Single-family vs Multifamily')
ax.set_xticks(x)
ax.set_xticklabels(cause_pct_plot.columns, rotation=15, ha='right')
ax.legend()
# Add value labels
for bar in bars1:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 1,
f'{height:.0f}%', ha='center', va='bottom', fontsize=9)
for bar in bars2:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 1,
f'{height:.0f}%', ha='center', va='bottom', fontsize=9)
plt.tight_layout()
plt.savefig('outputs/chart_cause_comparison.png', dpi=150, bbox_inches='tight')
plt.close()
print(" Saved: outputs/chart_cause_comparison.png")
# Chart 2: Heat Source Comparison
if heat_pct is not None:
fig, ax = plt.subplots(figsize=(12, 6))
heat_pct_plot = heat_pct.drop('Unknown', axis=1, errors='ignore')
x = range(len(heat_pct_plot.columns))
width = 0.35
if 'Single-family' in heat_pct_plot.index and 'Multifamily' in heat_pct_plot.index:
sf_vals = heat_pct_plot.loc['Single-family'].values
mf_vals = heat_pct_plot.loc['Multifamily'].values
bars1 = ax.bar([i - width/2 for i in x], sf_vals, width, label='Single-family', color='#2ca02c')
bars2 = ax.bar([i + width/2 for i in x], mf_vals, width, label='Multifamily', color='#d62728')
ax.set_ylabel('Percentage of Fires (%)')
ax.set_title('Heat Source: Single-family vs Multifamily\n(What started the fire?)')
ax.set_xticks(x)
ax.set_xticklabels(heat_pct_plot.columns, rotation=15, ha='right')
ax.legend()
plt.tight_layout()
plt.savefig('outputs/chart_heat_source_comparison.png', dpi=150, bbox_inches='tight')
plt.close()
print(" Saved: outputs/chart_heat_source_comparison.png")
# Chart 3: Sprinkler Presence
if sprinkler_pct is not None:
fig, ax = plt.subplots(figsize=(8, 6))
sprinkler_plot = sprinkler_pct[['Yes', 'No']].copy() if 'Yes' in sprinkler_pct.columns else sprinkler_pct
x = range(len(sprinkler_plot.index))
width = 0.35
if 'Yes' in sprinkler_plot.columns and 'No' in sprinkler_plot.columns:
yes_vals = sprinkler_plot['Yes'].values
no_vals = sprinkler_plot['No'].values
bars1 = ax.bar([i - width/2 for i in x], yes_vals, width, label='Sprinkler Present', color='#1f77b4')
bars2 = ax.bar([i + width/2 for i in x], no_vals, width, label='No Sprinkler', color='#ff7f0e')
ax.set_ylabel('Percentage of Fires (%)')
ax.set_title('Sprinkler Presence by Housing Type')
ax.set_xticks(x)
ax.set_xticklabels(sprinkler_plot.index)
ax.legend()
# Add value labels
for bar in bars1:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 1,
f'{height:.0f}%', ha='center', va='bottom', fontsize=11, fontweight='bold')
for bar in bars2:
height = bar.get_height()
ax.text(bar.get_x() + bar.get_width()/2., height + 1,
f'{height:.0f}%', ha='center', va='bottom', fontsize=11, fontweight='bold')
plt.tight_layout()
plt.savefig('outputs/chart_sprinkler_by_housing.png', dpi=150, bbox_inches='tight')
plt.close()
print(" Saved: outputs/chart_sprinkler_by_housing.png")
def main():
print("\n" + "#"*60)
print("# NFIRS CAUSE ANALYSIS")
print("# Why does multifamily have 4x higher fire rates?")
print("#"*60)
# Load code lookups
lookups = load_codelookup()
# Extract Austin NFIRS data
df = extract_austin_nfirs()
if df is None or len(df) == 0:
print("\nError: No data extracted. Check that NFIRS files exist.")
return
# Classify housing type
df = classify_housing_type(df)
# Classify causes
df = classify_cause(df, lookups)
# Save detailed dataset
os.makedirs("processed_data", exist_ok=True)
os.makedirs("outputs", exist_ok=True)
df.to_csv("processed_data/nfirs_austin_detailed.csv", index=False)
print(f"\nSaved: processed_data/nfirs_austin_detailed.csv ({len(df)} records)")
# Run analyses
cause_pct = analyze_cause_by_housing(df)
heat_pct = analyze_heat_source_by_housing(df)
area_pct = analyze_area_origin_by_housing(df)
sprinkler_pct = analyze_sprinkler_effect(df)
height_summary = analyze_building_height(df)
# Save analysis outputs
if cause_pct is not None:
cause_pct.to_csv("outputs/cause_by_housing_type.csv")
print("Saved: outputs/cause_by_housing_type.csv")
if heat_pct is not None:
heat_pct.to_csv("outputs/heat_source_by_housing.csv")
print("Saved: outputs/heat_source_by_housing.csv")
if area_pct is not None:
area_pct.to_csv("outputs/area_origin_by_housing.csv")
print("Saved: outputs/area_origin_by_housing.csv")
if sprinkler_pct is not None:
sprinkler_pct.to_csv("outputs/sprinkler_by_housing.csv")
print("Saved: outputs/sprinkler_by_housing.csv")
if height_summary is not None:
height_summary.to_csv("outputs/building_height_analysis.csv")
print("Saved: outputs/building_height_analysis.csv")
# Create visualizations
create_visualizations(df, cause_pct, heat_pct, sprinkler_pct)
# Building height chart
if height_summary is not None and 'BLDG_ABOVE' in df.columns:
create_building_height_chart(df)
# Summary
print("\n" + "="*80)
print("KEY FINDINGS")
print("="*80)
residential = df[df['housing_type'].isin(['Single-family', 'Multifamily'])]
if len(residential) > 0:
# Intentional fire comparison
sf_intentional = residential[
(residential['housing_type'] == 'Single-family') &
(residential['cause_label'] == 'Intentional')
]
mf_intentional = residential[
(residential['housing_type'] == 'Multifamily') &
(residential['cause_label'] == 'Intentional')
]
sf_total = len(residential[residential['housing_type'] == 'Single-family'])
mf_total = len(residential[residential['housing_type'] == 'Multifamily'])
if sf_total > 0 and mf_total > 0:
sf_int_pct = len(sf_intentional) / sf_total * 100
mf_int_pct = len(mf_intentional) / mf_total * 100
print(f"\n1. INTENTIONAL FIRES (Arson):")
print(f" Single-family: {sf_int_pct:.1f}% of fires")
print(f" Multifamily: {mf_int_pct:.1f}% of fires")
if mf_int_pct > sf_int_pct:
print(f" -> Multifamily has {mf_int_pct/sf_int_pct:.1f}x higher arson rate")
# Sprinkler comparison
sf_sprinkler = residential[
(residential['housing_type'] == 'Single-family') &
(residential['AES_PRES'] == 'Y')
]
mf_sprinkler = residential[
(residential['housing_type'] == 'Multifamily') &
(residential['AES_PRES'] == 'Y')
]
if sf_total > 0 and mf_total > 0:
sf_spr_pct = len(sf_sprinkler) / sf_total * 100
mf_spr_pct = len(mf_sprinkler) / mf_total * 100
print(f"\n2. SPRINKLER PRESENCE:")
print(f" Single-family: {sf_spr_pct:.1f}% have sprinklers")
print(f" Multifamily: {mf_spr_pct:.1f}% have sprinklers")
print("\n" + "="*60)
print("Analysis complete. Review outputs/ for detailed results.")
print("="*60)
if __name__ == "__main__":
main()