forked from QuantCrimAtLeeds/PredictCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample2.py
More file actions
62 lines (46 loc) · 1.66 KB
/
Copy pathexample2.py
File metadata and controls
62 lines (46 loc) · 1.66 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
# Import libraries we need
import csv
import open_cp.data
import geopandas as gpd
import pyproj
import dateutil.parser
import open_cp.scripted as scripted
import datetime
# Load the input data; see `scripted_intro.md`
def row_to_datetime(row):
datetime_string = row[1]
return dateutil.parser.parse(datetime_string)
proj = pyproj.Proj({"init" : "epsg:2790"})
def row_to_coords(row):
x = float(row[5])
y = float(row[4])
return proj(x, y)
def load_points():
with open("example.csv") as file:
reader = csv.reader(file)
header = next(reader)
# Assume the header is correct
times = []
xcoords = []
ycoords = []
for row in reader:
times.append(row_to_datetime(row))
x, y = row_to_coords(row)
xcoords.append(x)
ycoords.append(y)
# Maybe `times` is not sorted.
times, xcoords, ycoords = open_cp.data.order_by_time(times, xcoords, ycoords)
return open_cp.data.TimedPoints.from_coords(times, xcoords, ycoords)
def load_geometry():
frame = gpd.read_file("SouthSide")
return frame.geometry[0]
# Perform the predictions; see `scripted_intro.md`
with scripted.Data(load_points, load_geometry,
start=datetime.datetime(2016,1,1)) as state:
time_range = scripted.TimeRange(datetime.datetime(2016,10,1),
datetime.datetime(2017,1,1), datetime.timedelta(days=1))
state.add_prediction(scripted.NaiveProvider, time_range)
state.score(scripted.HitRateEvaluator)
state.score(scripted.HitCountEvaluator)
state.process(scripted.HitRateSave("rates.csv"))
state.process(scripted.HitCountSave("counts.csv"))