Skip to content

Commit

Permalink
median durations by sev
Browse files Browse the repository at this point in the history
  • Loading branch information
dschep committed Jan 10, 2025
1 parent e9ff346 commit 171d05d
Showing 1 changed file with 45 additions and 2 deletions.
47 changes: 45 additions & 2 deletions process.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import statistics
import sys
from datetime import datetime

import fiona
import h3
Expand Down Expand Up @@ -48,8 +49,7 @@
lng, lat = feat.geometry.coordinates
cell = h3.latlng_to_cell(lat, lng, RESOLUTION)
writer.writerow(
[cell]
+ list([feat.properties[f] for f in FIELDS if f != "h3_cell"])
[cell] + list([feat.properties[f] for f in FIELDS if f != "h3_cell"])
)

# Compute basic metadata
Expand Down Expand Up @@ -118,11 +118,54 @@
open("docs/mode_severity.geojson", "w"),
)


def format_quote(raw_note: str) -> str:
note = raw_note.strip('"').strip()
return f'"{note}"'


# durations by severity
durations_by_sev = {
1: {},
2: {},
3: {},
4: {},
}
for feat in feats:
lng, lat = feat.geometry.coordinates
cell = h3.latlng_to_cell(lat, lng, RESOLUTION)

sev = EXTENTS.index(feat.properties["to_what_extent_did_you_lose_wat"])
if sev == 0:
continue
if feat.properties["when_did_you_lose_water"]:
duration = datetime.fromisoformat(
feat.properties["CreationDate"]
) - datetime.fromisoformat(feat.properties["when_did_you_lose_water"])
if feat.properties["when_did_you_regain_water"]:
duration = datetime.fromisoformat(
feat.properties["when_did_you_regain_water"]
) - datetime.fromisoformat(feat.properties["when_did_you_lose_water"])
duration = duration.total_seconds() / 60 / 60
durations_by_sev[sev].setdefault(cell, []).append(duration)
for sev, durations_by_cell in durations_by_sev.items():
json.dump(
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": h3.cells_to_geo([cell]),
"properties": {
"duration": statistics.median(durations),
},
}
for cell, durations in durations_by_cell.items()
],
},
open(f"docs/sev{sev}_median_duration.geojson", "w"),
)

# read rows from selected-notes.csv
with open("docs/selected-notes.csv", "r") as notes:
reader = csv.reader(notes)
Expand Down

0 comments on commit 171d05d

Please sign in to comment.