-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerate_vectors.py
412 lines (321 loc) · 14.4 KB
/
generate_vectors.py
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
import datetime
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pytz
import scipy.ndimage
import scipy.stats
# weather station data
WS_DTYPE = [("x", np.float),
("y", np.float),
("station_name", np.str_, 64),
("stn_id", np.int),
("climate_identifier", np.int),
("id", np.str_, 64),
("local_date", "datetime64[ms]"),
("province_code", np.str_, 2),
("local_year", np.float),
("local_month", np.float),
("local_day", np.float),
("mean_temperature", np.float),
("mean_temperature_flag", np.str_, 1),
("min_temperature", np.float),
("min_temperature_flag", np.str_, 1),
("max_temperature", np.float),
("max_temperature_flag", np.str_, 1),
("total_precipitation", np.float),
("total_precipitation_flag", np.str_, 1),
("total_rain", np.float),
("total_rain_flag", np.str_, 1),
("total_snow", np.float),
("total_snow_flag", np.str_, 1),
("snow_on_ground", np.float),
("snow_on_ground_flag", np.str_, 1),
("direction_max_gust", np.float),
("direction_max_gust_flag", np.str_, 1),
("speed_max_gust", np.float),
("speed_max_gust_flag", np.str_, 1),
("cooling_degree_days", np.float),
("cooling_degree_days_flag", np.str_, 1),
("heating_degree_days", np.float),
("heating_degree_days_flag", np.str_, 1),
("min_rel_humidity", np.float),
("min_rel_humidity_flag", np.str_, 1),
("max_rel_humidity", np.float),
("max_rel_humidity_flag", np.str_, 1)]
OV_DTYPE = [("liberal", np.float), ("conservative", np.float), ("ndp", np.float),
("green", np.float), ("bloc", np.float), ("other", np.float)]
# TODO try "total_snow", "total_rain" from other nearest station if non-existent
# features to extract
VARS = ["min_temperature", "mean_temperature", "max_temperature",
"total_precipitation"]#, "snow_on_ground"]
def angular_separation(ra1, dec1, ra2, dec2):
"""Calculates the angular separation between two celestial objects.
Parameters
----------
ra1 : float, array
Right ascension of the first source in degrees.
dec1 : float, array
Declination of the first source in degrees.
ra2 : float, array
Right ascension of the second source in degrees.
dec2 : float, array
Declination of the second source in degrees.
Returns
-------
angle : float, array
Angle between the two sources in degrees, where 0 corresponds
to the positive Dec axis.
angular_separation : float, array
Angular separation between the two sources in degrees.
Notes
-----
The angle between sources is calculated with the Pythagorean theorem
and is used later to calculate the uncertainty in the event position
in the direction of the known source.
Calculating the angular separation using spherical geometry gives
poor accuracy for small (< 1 degree) separations, and using the
Pythagorean theorem fails for large separations (> 1 degrees).
Transforming the spherical geometry cosine formula into one that
uses haversines gives the best results, see e.g. [1]_. This gives:
.. math:: \mathrm{hav} d = \mathrm{hav} \Delta\delta +
\cos\delta_1 \cos\delta_2 \mathrm{hav} \Delta\\alpha
Where we use the identity
:math:`\mathrm{hav} \\theta = \sin^2(\\theta/2)` in our
calculations.
The calculation might give inaccurate results for antipodal
coordinates, but we do not expect such calculations here..
The source angle (or bearing angle) :math:`\\theta` from a point
A(ra1, dec1) to a point B(ra2, dec2), defined as the angle in
clockwise direction from the positive declination axis can be
calculated using:
.. math:: \\tan(\\theta) = (\\alpha_2 - \\alpha_1) /
(\delta_2 - \delta_1)
In NumPy :math:`\\theta` can be calculated using the arctan2
function. Note that for negative :math:`\\theta` a factor :math:`2\pi`
needs to be added. See also the documentation for arctan2.
References
----------
.. [1] Sinnott, R. W. 1984, Sky and Telescope, 68, 158
Examples
--------
>>> print(angular_separation(200.478971, 55.185900, 200.806433, 55.247994))
(79.262937451490941, 0.19685681276638525)
>>> print(angular_separation(0., 20., 180., 20.))
(90.0, 140.0)
"""
# convert decimal degrees to radians
deg2rad = np.pi / 180
ra1 = ra1 * deg2rad
dec1 = dec1 * deg2rad
ra2 = ra2 * deg2rad
dec2 = dec2 * deg2rad
# delta works
dra = ra1 - ra2
ddec = dec1 - dec2
# haversine formula
hav = np.sin(ddec / 2.0) ** 2 + np.cos(dec1) * np.cos(dec2) \
* np.sin(dra / 2.0) ** 2
angular_separation = 2 * np.arcsin(np.sqrt(hav))
# angle in the clockwise direction from the positive dec axis
# note the minus signs in front of `dra` and `ddec`
source_angle = np.arctan2(-dra, -ddec)
source_angle[source_angle < 0] += 2 * np.pi
# convert radians back to decimal degrees
return source_angle / deg2rad, angular_separation / deg2rad
def calculate_moments(ts):
"""Calculate the moments of a timeseries and the number of outliers."""
mean = np.nanmean(ts)
std = np.nanstd(ts)
#skew = scipy.stats.skew(ts, nan_policy="omit")
#kurt = scipy.stats.kurtosis(ts, nan_policy="omit")
noutliers = np.sum(np.logical_or(ts < -3 * std, ts > 3 * std))
d_high_outliers = np.abs(np.nanpercentile(ts, 99) - mean)
d_low_outliers = np.abs(np.nanpercentile(ts, 1) - mean)
return mean, std, noutliers, d_high_outliers, d_low_outliers
def nan_helper(x):
"""Helper to handle indices and logical indices of NaN arrays."""
return np.isnan(x), lambda y: y.nonzero()[0]
class WSData():
"""Canadian weather station data."""
def __init__(self, fname):
self.data = np.genfromtxt(fname, dtype=WS_DTYPE,
delimiter=",", skip_header=1)
self.unique_stations, self.unique_stations_idx = \
np.unique(self.data["stn_id"], return_index=True)
def find_nearest_station(self, lon, lat, remove_station = False):
"""Find the station ID of the weather station nearest to some
coordinate."""
angle, sep = angular_separation(
self.data[self.unique_stations_idx]["x"],
self.data[self.unique_stations_idx]["y"], lon, lat)
if not remove_station:
nearest_idx = np.argsort(sep)[0]
else:
nearest_idx = np.argsort(sep)[1]
#print("")
#print("Nearest weather station is {:.2f} degrees away..".format(
# sep[nearest_idx]))
#print("")
return self.unique_stations[nearest_idx]
def interpolate(self,station_data,VARS,lon,lat):
for var in VARS:
# remove NaNs by interpolation
nans, y = nan_helper(station_data[var])
#TODO bad practise because don't understand the error
try: station_data[var][nans] = np.interp(y(nans), y(~nans),
station_data[var][~nans])
# Recursively remove station from self and call interpolate again
except ValueError:
station = self.find_nearest_station(lon, lat, remove_station=True)
station_data = self.data[self.data["stn_id"] == station]
station_data = self.interpolate(station_data,VARS,lon,lat)
return station_data
def generate_input_vector(self, lon, lat, date):
"""Retrieve data for a given riding and election.
Parameters
----------
lon : float
Longitude, in degrees.
lat : float
Latitude, in degrees.
date : :obj:datetime
Date.
Returns
-------
vector : array_like
Data vector for model.
"""
# no of variables x (2 time ranges +
# (4 moments + 3 outlier statistics) x 6 time ranges
#vector = np.empty(len(VARS) * 5)
#vector = np.empty(len(VARS) * (2 * 1 + 5 * 5))
vector = np.empty(68)
i = 0
station = self.find_nearest_station(lon, lat)
station_data = self.data[self.data["stn_id"] == station]
#print("station data", station_data)
# TODO add results from smoothed timeseries
# TODO add previous election result
# TODO add direction max gust (decomposed in x, y)
# Interpolate between missing data. If too much data is missing, try different station
station_data = self.interpolate(station_data,VARS,lon,lat)
# Add moments of variables at a given period to vector
for var in VARS:
baseline = np.nanmedian(station_data[var])
#print("station data nans", station_data)
for timerange in [0, -1,-7,-30, -365]:
if timerange == 0:
# election day
mask = station_data["local_date"] == date
station_time_data = station_data[mask]
# print(station_data["local_date"])
# print( np.sum(mask),date, type(date))
#print('mask',mask, 'station_time_data' ,station_time_data )
elif timerange == -1:
# day before election day
mask = station_data["local_date"] \
== date + datetime.timedelta(days=timerange)
station_time_data = station_data[mask]
elif timerange > -366:
# week, month or year before election day
mask = np.logical_and(station_data["local_date"] <= date,
station_data["local_date"] > date \
+ datetime.timedelta(days=timerange))
station_time_data = station_data[mask]
elif timerange == -730:
# 2 years before election day
mask = np.logical_and(station_data["local_date"] <= date \
+ datetime.timedelta(days=-365),
station_data["local_date"] > date \
+ datetime.timedelta(days=timerange))
station_time_data = station_data[mask]
elif timerange == -1095:
# 3 years before election day
mask = np.logical_and(station_data["local_date"] <= date \
+ datetime.timedelta(days=-730),
station_data["local_date"] > date \
+ datetime.timedelta(days=timerange))
station_time_data = station_data[mask]
else:
# 4 years before election day
mask = np.logical_and(station_data["local_date"] <= date \
+ datetime.timedelta(days=-1095),
station_data["local_date"] > date \
+ datetime.timedelta(days=timerange))
station_time_data = station_data[mask]
# smooth time-series
#ts = scipy.ndimage.gaussian_filter1d(station_time_data[var],
# 10.0, order=0)
ts = station_time_data[var] - baseline
if len(ts) == 0:
#print("I give up")
continue
# print('ts',ts)
if len(ts) == 1:
vector[i] = ts[0]
i += 1
else:
moments = calculate_moments(ts)
vector[i:i+5] = moments
i += 5
return vector
class ElectionResults():
"""Quebec election results."""
def __init__(self, fname):
self.df = pd.read_csv(fname, encoding="utf-8")
if __name__ == "__main__":
#lon = -70.
#lat = 44.
#election_date = datetime.datetime(2019, 4, 30)
weather_data = WSData("data/weather_mctavish.csv")
#vector = weather_data.generate_input_vector(lon, lat, election_date)
er = ElectionResults("data/voteResult.csv")
for row in er.df.T:
election = er.df.iloc[row,:]
lon = float(election["lon"])
lat = float(election["lat"])
y, m, d = election["Election Date"].split("-")
election_date = datetime.datetime(int(y), int(m), int(d))
input_vector = weather_data.generate_input_vector(lon, lat, election_date)
#print(input_vector)
output_vector = np.empty(6)
output_vector[0] = float(election["Liberal"])
output_vector[1] = float(election["Conservative"])
output_vector[2] = float(election["NDP"])
output_vector[3] = float(election["Green"])
output_vector[4] = float(election["Bloc"])
output_vector[5] = float(election["Other"])
#print(output_vector)
break
def get_input_output():
#lon = -70.
#lat = 44.
#election_date = datetime.datetime(2019, 4, 30)
weather_data = WSData("data/climate_daily_combined.csv")
#vector = weather_data.generate_input_vector(lon, lat, election_date)
er = ElectionResults("data/voteResultQc.csv")
X=[]
Y=[]
#print(weather_data.unique_stations)
#print(weather_data.unique_stations_idx)
for row in er.df.T:
election = er.df.iloc[row,:]
#print(election)
lon = float(election["lon"])
lat = float(election["lat"])
y, m, d = election["Election Date"].split("-")
election_date = datetime.datetime(int(y), int(m), int(d))
input_vector = weather_data.generate_input_vector(lon, lat, election_date)
#print(input_vector)
output_vector = np.empty(6)
output_vector[0] = float(election["Liberal"])
output_vector[1] = float(election["Conservative"])
output_vector[2] = float(election["NDP"])
output_vector[3] = float(election["Green"])
output_vector[4] = float(election["Bloc"])
output_vector[5] = float(election["Other"])
X.append(input_vector)
Y.append(output_vector)
#print(output_vector)
return np.array(X),np.array(Y)