-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample-streams.py
More file actions
40 lines (31 loc) · 1.47 KB
/
example-streams.py
File metadata and controls
40 lines (31 loc) · 1.47 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
#### This code searches for an optimal subset of streams, aggregated over a fixed set of locations
#### Ideas for exploratory data analysis:
#### 1. Try different sets of locations (very small vs. very large)
from ccss import *
from ccss import greedy_streams as search_streams
f = open(opts.output,"w")
out = csv.writer(f)
out.writerow("predict R Dopt S Xn Yn elapsed".split())
matched = "all"
fields = 'type, tract, area, date'
if opts.areas != "all":
print "Matching areas", opts.areas
data = select(input, areas=opts.areas, fields=fields, start_date=start_date, end_date=end_date) #input[match_areas(input, opts.areas)]
matched = opts.areas
elif opts.tracts != "all":
print "Matching tracts", opts.tracts
data = select(input, tracts=opts.tracts, fields=fields, start_date=start_date, end_date=end_date) #input[match_tracts(input, opts.tracts)]
matched = opts.tracts
else:
data = select(input, fields, start_date=start_date, end_date=end_date) #, input
with mytimer:
R, Dopt = search_streams(data, np.array(streams))
Xn = np.sum(match_streams(data,Dopt))
Yn = np.sum(match_streams(data,[opts.predict]))
print "\n\n-------------------- In area", matched
print "Correlation = %.05f"%R
print "for predicting", opts.predict, "with these leading indicators:", ', '.join(Dopt)
print "# of events in X:", Xn
print "# of events in Y:", Yn
out.writerow([opts.predict, R, Dopt, matched, Xn, Yn, mytimer.elapsed])
print "search complete, output written to %s"%(opts.output)