Advertisement
Guest User

Untitled

a guest
Jan 19th, 2019
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 31.45 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import pymongo
  3. import ssl
  4. from pymongo import MongoClient
  5. import datetime
  6. import numpy as np
  7. import json
  8. from bson.son import SON
  9. import copy
  10.  
  11. class Connect:
  12. def __init__(self):
  13. self.myemotion= "I hate MOngoDB"
  14.  
  15. def connection(self):
  16. try:
  17. client = MongoClient(host='mongodb://bigdatadb.polito.it',
  18. port=27017,
  19. ssl=True,
  20. username='ictts',
  21. password='Ictts16!',
  22. authSource='carsharing',
  23. authMechanism='SCRAM-SHA-1',
  24. ssl_cert_reqs=ssl.CERT_NONE)
  25. print ("Connected successfully!!!")
  26. except pymongo.errors.ConnectionFailure :
  27. print ("Could not connect to MongoDB")
  28. db= client['carsharing']
  29. booking_enjoy = db['enjoy_PermanentBookings']
  30. parking_car2go = db['PermanentParkings']
  31. parking_enjoy = db['enjoy_PermanentParkings']
  32. booking_car2go = db['PermanentBookings']
  33. return db, parking_enjoy, parking_car2go, booking_enjoy, booking_car2go
  34.  
  35. class Filter:
  36. def __init__(self,db,startDate,endDate):
  37. self.db= db
  38. self.start=startDate
  39. self.end=endDate
  40.  
  41. def nofilter(self,city,collection):
  42. pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
  43. {"$project": {'_id': False, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] }}},
  44. {"$group" : {"_id": "$duration","count": {"$sum": 1}}},
  45. {"$sort": SON([("_id", 1)])} ]
  46. li=list(collection.aggregate(pipeline))
  47. return li
  48.  
  49. def nofilteraday(self,city,collection):
  50. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  51. {"$project": {'_id': False, "day": {"$dayOfWeek": "$init_date"},
  52. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  53. }},
  54. {"$group": {"_id": {"day":"$day","duration":"$duration"},"count": {"$sum": 1}}}]
  55. li = list(collection.aggregate(pipeline))#da fare per ogni giorno cdf output es:{'_id': {'day': 5, 'duration': 122.1}, 'count': 1},
  56. #print (li)
  57. li.sort(key=self.take_duration)
  58.  
  59. return li
  60.  
  61. def take_duration(self, element):
  62. return element["_id"]["duration"]
  63.  
  64. def nofilteraweek(self,city,collection):
  65. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  66. {"$project": {'_id': False, "week": {"$week": "$init_date"},
  67. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  68. }},
  69. {"$group": {"_id": {"week":"$week","duration":"$duration"},"count": {"$sum": 1}}}]
  70. li = list(collection.aggregate(pipeline))#da fare per ogni giorno cdf output es:{'_id': {'day': 5, 'duration': 122.1}, 'count': 1},
  71. li.sort(key=self.take_duration)
  72.  
  73. print(duration_list)
  74. return li
  75.  
  76. def filtered(self,city,collection):
  77. pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
  78. {"$project": {'_id': False, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] },#"moved" :{ "$ne": [
  79. #{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}
  80. }},
  81. {"$match":{"duration": {"$lte": 480}}},#{"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}}
  82. {"$group" : {"_id": "$duration","count": {"$sum": 1}}},
  83. {"$sort": SON([("_id", 1)])} ]
  84. li=list(collection.aggregate(pipeline))
  85. return li
  86.  
  87. def filteredUTIL(self, city, collection):
  88. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  89. {"$project": {'_id': False,
  90. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  91. "hour": {"$hour": "$init_date"}, "moved": {"$ne": [ # }} "moved" :{ "$ne": [
  92. {"$arrayElemAt": ["$origin_destination.coordinates", 0]},
  93. {"$arrayElemAt": ["$origin_destination.coordinates", 1]}]}}},
  94. {"$match": {'moved': True, "duration": {"$lte": 240, "$gte": 2}}},
  95. # {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}} {"$match":{"duration": {"$lte": 480}}}
  96. {"$group": {"_id": "$hour", "count": {"$sum": 1}}},
  97. {"$sort": SON([("_id", 1)])}]
  98. li = list(collection.aggregate(pipeline))
  99. return li
  100.  
  101. def position(self, city, collection):
  102. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  103. {"$project": {'_id': False, "hour": {"$hour": "$init_date"},
  104. "position": {"$arrayElemAt": ["$origin_destination.coordinates"]}
  105. }},
  106. {"$group": {"_id": "$hour", "position": "$position"}},
  107. {"$sort": SON([("_id", 1)])}]
  108. li = list(collection.aggregate(pipeline))
  109. print(li)
  110.  
  111. def perhour(self,city,collection):#effettivamente non è chiaro-> hour/ tot book?
  112. pipeline = [{ "$match": { "city": city,"init_time": { "$gte": self.start, "$lte": self.end} }},
  113. {"$project": {'_id': False, "hour": {"$hour": "$init_date"}, "duration": { "$divide": [ { "$subtract": ["$final_time", "$init_time"] }, 60 ] }}},
  114. {"$group": {"_id": {"hour": "$hour"}, "count": {"$sum": 1}}}]#, "duration": "$duration"
  115. li = list(collection.aggregate(pipeline))
  116. return li
  117.  
  118. def day_average(self, city, collection):
  119. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  120. {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
  121. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  122. "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
  123. {"$match": {"duration": {"$lte": 480}}},
  124. {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
  125. {"$group": {"_id": "$day", 'average':{'$avg':'$duration'}}},#'$stdDevPop':'$duration'}}},
  126. {"$sort": SON([('_id', 1)])}]
  127. li = list(collection.aggregate(pipeline))
  128. return li
  129.  
  130. def day_std(self, city, collection):
  131. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  132. {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
  133. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  134. "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
  135. {"$match": {"duration": {"$lte": 480}}},
  136. {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
  137. {"$group": {"_id": "$day", 'std': {'$stdDevPop': '$duration'}}},
  138. {"$sort": SON([('_id', 1)])}]
  139. li = list(collection.aggregate(
  140. pipeline))
  141. return li
  142.  
  143. def day_median_and_percentile(self, city, collection):
  144. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  145. {"$project": {'_id': False, "day": {"$dayOfMonth": "$init_date"},
  146. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},#
  147. "moved" :{ "$ne": [{"$arrayElemAt": [ "$origin_destination.coordinates", 0]}, {"$arrayElemAt": [ "$origin_destination.coordinates", 1]}]}}},
  148. {"$match": {"duration": {"$lte": 480}}},
  149. {"$match":{'moved':True,"duration": {"$lte": 240,"$gte":2}}},
  150. {"$group": {"_id":{'day':"$day", 'duration':'$duration'} }},
  151. {"$sort": SON([('_id', 1)])}]
  152. li = list(collection.aggregate(pipeline))
  153. temp = []
  154. median = []
  155. percentile75 = []
  156. for k in range(32):
  157. temp.clear()
  158. for i in range (len(li)):
  159. if li[i]['_id']['day'] == k and k != 0:
  160. temp.append(li[i]['_id']['duration'])
  161. if k!=0:
  162. temp.sort()
  163. median.append(temp[(len(temp)-1)//2])
  164. ind = np.int(0.75 * len(temp))
  165. percentile75.append(temp[ind])
  166. print(len(median))
  167. print(percentile75)
  168. return median, percentile75
  169.  
  170. # function to get all the rental car, filtering the ones which have position and destination at same coordinates,
  171. # parameters taked into account: time (initial and final) and coordinates (startind and ending)
  172. # this function will not be used due the long time that it takes to make each call to the database
  173. def get_car_renting(self, city, collection, start, stop):
  174. start = start
  175. stop = stop
  176. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  177. {"$project": {'_id': False, "start_hour": {"$hour": "$init_date"},
  178. "end_hour": {"$hour": "$final_date"},
  179. "moved": {"$ne": [{"$arrayElemAt": ["$origin_destination.coordinates", 0]},
  180. {"$arrayElemAt": ["$origin_destination.coordinates", 1]}]},
  181. "s" : {"$arrayElemAt": ["$origin_destination.coordinates", 0]},
  182. "e" : {"$arrayElemAt": ["$origin_destination.coordinates", 1]},
  183. 'car': "$_id"}},
  184. {"$match": {'moved': True}},
  185. {"$match" :{"e": {"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": stop}}}}},
  186. {"$match" :{"s": {"$geoWithin": {"$geometry": {"type": "Polygon", "coordinates": start}}}}},
  187. {"$group": {"_id": {"s": "$s", "e": "$e"}}}]
  188. li = list(collection.aggregate(pipeline))
  189. return li
  190.  
  191. # function to get all the parked car
  192. # parameters taked into account: time (initial and final)
  193. def get_car_position(self, city, collection):
  194. pipeline = [{"$match": {"city": city, "init_time": {"$gte": self.start, "$lte": self.end}}},
  195. {"$project": {'_id': False, "start_hour": {"$hour": "$init_date"},
  196. "end_hour": {"$hour": "$final_date"},
  197. "duration": {"$divide": [{"$subtract": ["$final_time", "$init_time"]}, 60]},
  198. "x" : {"$arrayElemAt": ["$loc.coordinates", 0]},
  199. "y" : {"$arrayElemAt": ["$loc.coordinates", 1]},
  200. 'car': "$_id"}},
  201. {"$group": {"_id": {"x": "$x", "y": "$y", 'car':"$car", "start":"$start_hour", "end":"$end_hour"}}}]
  202. li = list(collection.aggregate(pipeline))
  203. return li
  204.  
  205.  
  206.  
  207.  
  208. class Printer:
  209. def __init__(self,l1,l2,l3):
  210. self.l1 = l1
  211. self.l2 = l2
  212. self.l3 = l3
  213.  
  214. def print(self):
  215. list1=[]
  216. list11=[]
  217. list2=[]
  218. list22=[]
  219. list3 = []
  220. list33 = []
  221. length=len(self.l1)
  222. length2=len(self.l2)
  223. length3 = len(self.l3)
  224. temp=0
  225. temp2=0
  226. temp3=0
  227. plt.figure(1)
  228. for i in range(length):
  229. b=self.l1[i]
  230. list1.append(b['_id'])
  231. temp=b['count']+temp
  232. list11.append(temp)
  233. for i in range(length2):
  234. c=self.l2[i]
  235. list2.append(c['_id'])
  236. temp2 = c['count'] + temp2
  237. list22.append(temp2)
  238. for i in range(length3):
  239. d=self.l3[i]
  240. list3.append(d['_id'])
  241. temp3 = d['count'] + temp3
  242. list33.append(temp3)
  243. for i in range(len(list11)):
  244. list11[i]=list11[i]/temp
  245. for i in range(len(list22)):
  246. list22[i] = list22[i] / temp2
  247. for i in range(len(list33)):
  248. list33[i] = list33[i] / temp3
  249. #t = np.arange(0., 1.,s)
  250. plt.subplot(311)
  251. plt.grid()
  252. plt.plot(list1,list11,'c')
  253. plt.xscale('log')
  254. plt.title('CDF_Parking')#"CDFParkingBooking"
  255. plt.xlabel('Duration')
  256. plt.ylabel('Roma_Enjoy')
  257. plt.subplot(312)
  258. plt.plot(list2,list22,'b')
  259. # plt.title('CDF#nofilter')
  260. plt.xscale('log')
  261. plt.xlabel('Duration')
  262. plt.ylabel('Roma_Car2Go ')
  263. plt.grid()
  264. plt.subplot(313)
  265. plt.plot(list3, list33, 'm')
  266. # plt.title('CDF#nofilter')
  267. plt.xscale('log')
  268. plt.xlabel('Duration')
  269. plt.ylabel('Torino_Car2Go ')
  270. plt.grid()
  271. plt.show()
  272.  
  273. def printday(self):
  274. list0 = []
  275. list00 = []
  276. list1 = []
  277. list11 = []
  278. list2 = []
  279. list22 = []
  280. list3 = []
  281. list33 = []
  282. list4 = []
  283. list44 = []
  284. length = len(self.l1)
  285. temp0 = 0
  286. temp = 0
  287. temp2 = 0
  288. temp3 = 0
  289. temp4 = 0
  290. for i in range(length):
  291. b = self.l1[i]
  292. if b['_id']['week'] == 40:
  293. a = b['_id']['duration']
  294. list0.append(a)
  295. temp0 = b['count'] + temp0
  296. list00.append(temp0)
  297. elif b['_id']['week'] == 41:
  298. a = b['_id']['duration']
  299. list1.append(a)
  300. temp = b['count'] + temp
  301. list11.append(temp)
  302. elif b['_id']['week'] == 42:
  303. a = b['_id']['duration']
  304. list2.append(a)
  305. temp2 = b['count'] + temp2
  306. list22.append(temp2)
  307. elif b['_id']['week'] == 43:
  308. a = b['_id']['duration']
  309. list3.append(a)
  310. temp3 = b['count'] + temp3
  311. list33.append(temp3)
  312. elif b['_id']['week'] == 44:
  313. a = b['_id']['duration']
  314. list4.append(a)
  315. temp4 = b['count'] + temp4
  316. list44.append(temp4)
  317. for i in range(len(list00)):
  318. list00[i] = list00[i] / temp0
  319. for i in range(len(list11)):
  320. list11[i] = list11[i] / temp
  321. for i in range(len(list22)):
  322. list22[i] = list22[i] / temp2
  323. for i in range(len(list33)):
  324. list33[i] = list33[i] / temp3
  325. for i in range(len(list44)):
  326. list44[i] = list44[i] / temp4
  327. # for i in range(len(list0)+1,len(list33)):
  328. # list0.append(list0[len(list0)-1])
  329. # list00.append(1)
  330. # for i in range(len(list1)+1,len(list33)):
  331. # list1.append(list1[len(list1)-1])
  332. # list11.append(1)
  333. # for i in range(len(list2)+1, len(list33)):
  334. # list3.append(list2[len(list2)-1])
  335. # list33.append(1)
  336. # for i in range(len(list4)+1, len(list33)):
  337. # list4.append(list4[len(list4)-1])
  338. # list44.append(1)
  339. plt.figure(1)
  340. plt.grid()
  341. plt.xscale('log')
  342. plt.title('CDFperweek_Booking') # "CDFParkingBooking"
  343. plt.xlabel('Duration')
  344. plt.ylabel('Roma_Enjoy')
  345. plt.plot(list0, list00, 'k', label='40th week')
  346. plt.plot(list1, list11, 'c', label='41st week')
  347. plt.plot(list2, list22, 'b', label='42nd week')
  348. plt.plot(list3, list33, 'm', label='43rd week')
  349. plt.plot(list4, list44, 'orange', label='44th week')
  350. plt.legend(loc='upper left')
  351. plt.show()
  352.  
  353. def print_week(self):
  354. duration_list_day1 = []
  355. duration_list_day2 = []
  356. duration_list_day3 = []
  357. duration_list_day4 = []
  358. duration_list_day5 = []
  359. duration_list_day6 = []
  360. duration_list_day7 = []
  361. duration_list_day1_count = []
  362. duration_list_day2_count = []
  363. duration_list_day3_count = []
  364. duration_list_day4_count = []
  365. duration_list_day5_count = []
  366. duration_list_day6_count = []
  367. duration_list_day7_count = []
  368. days = []
  369. time = []
  370. c = []
  371. li = self.l1
  372. print(li)
  373. for k in range(8):
  374. t = 0
  375. days.clear()
  376. c.clear()
  377. for i in range(len(li)):
  378. if li[i]['_id']['day'] == k:
  379. days.append(li[i]['_id']['duration'])
  380. t = li[i]['count'] + t
  381. c.append(t)
  382. if k != 0:
  383. time.append(len(c))
  384. for i in range(len(c)):
  385. c[i] = c[i] / t
  386. print(len(c))
  387. print(time)
  388. if k == 1:
  389. duration_list_day1 = days.copy()
  390. duration_list_day1_count = c.copy()
  391. print(c)
  392. elif k == 2:
  393. duration_list_day2 = days.copy()
  394. duration_list_day2_count = c.copy()
  395. print(c)
  396. elif k == 3:
  397. duration_list_day3 = days.copy()
  398. duration_list_day3_count = c.copy()
  399. elif k == 4:
  400. duration_list_day4 = days.copy()
  401. duration_list_day4_count = c.copy()
  402. elif k == 5:
  403. duration_list_day5 = days.copy()
  404. duration_list_day5_count = c.copy()
  405. elif k == 6:
  406. duration_list_day6 = days.copy()
  407. duration_list_day6_count = c.copy()
  408. elif k == 7:
  409. duration_list_day7 = days.copy()
  410. duration_list_day7_count = c.copy()
  411. print('SUN', duration_list_day7)
  412. print(duration_list_day2)
  413. t_max = max(time)
  414. print(t_max)
  415. for k in range(1):
  416. d = t_max - time[k]
  417. for i in range(d):
  418. if k == 0:
  419. duration_list_day1.append(duration_list_day1[len(duration_list_day1) - 1])
  420. duration_list_day1_count.append(1)
  421. elif k == 1:
  422. duration_list_day2.append(duration_list_day2[len(duration_list_day2) - 1])
  423. duration_list_day2_count.append(1)
  424. elif k == 2:
  425. duration_list_day3 = (duration_list_day3[len(duration_list_day3) - 1])
  426. duration_list_day3_count.append(1)
  427. elif k == 3:
  428. duration_list_day4 = (duration_list_day4[len(duration_list_day4) - 1])
  429. duration_list_day4_count.append(1)
  430. elif k == 4:
  431. duration_list_day5 = (duration_list_day4[len(duration_list_day4) - 1])
  432. duration_list_day5_count.append(1)
  433. elif k == 5:
  434. duration_list_day6 = (duration_list_day5[len(duration_list_day5) - 1])
  435. duration_list_day6_count.append(1)
  436. elif k == 6:
  437. duration_list_day7 = (duration_list_day6[len(duration_list_day6) - 1])
  438. duration_list_day7_count.append(1)
  439. print(duration_list_day1)
  440. print(duration_list_day2)
  441. plt.figure(1)
  442. plt.xscale('log')
  443. plt.title('CDF per day')
  444. plt.plot(duration_list_day1, duration_list_day1_count, 'k', label='MON')
  445. plt.plot(duration_list_day2, duration_list_day2_count, 'c', label='TUE')
  446. plt.plot(duration_list_day3, duration_list_day3_count, 'b', label='WED')
  447. plt.plot(duration_list_day4, duration_list_day4_count, 'm', label='THU')
  448. plt.plot(duration_list_day5, duration_list_day5_count, 'orange', label='FRI')
  449. plt.plot(duration_list_day6, duration_list_day6_count, 'r', label='SAT')
  450. plt.plot(duration_list_day7, duration_list_day7_count, 'pink', label='SUN')
  451. plt.legend(loc='upper left')
  452. plt.xlabel('Duration')
  453. plt.ylabel('Turin Car2go booking')
  454. plt.show()
  455.  
  456. def print_statistics(self, l4, title):
  457. list1 = []
  458. list11 = []
  459. list2 = []
  460. list22 = []
  461. list3 = []
  462. list33 = []
  463. list4 = []
  464. list44 = []
  465. plt.figure(1)
  466. for i in range(len(self.l1)):
  467. list1.append(self.l1[i]['_id'])
  468. list11.append(self.l1[i]['average'])
  469. list2.append(self.l2[i]['_id'])
  470. list22.append(self.l2[i]['std'])
  471. list3.append(i+1)
  472. list33.append(self.l3[i])
  473. list4.append(i+1)
  474. list44.append(l4[i])
  475. plt.grid(True)
  476. plt.minorticks_on()
  477. plt.title(title)
  478. plt.xlabel('Day')
  479. plt.ylabel('Number of cars')
  480. plt.plot(list1, list11, 'c', label="Average")
  481. plt.plot(list2, list22, 'b', label="Standard Deviation")
  482. plt.plot(list3, list33, 'm', label="Median")
  483. plt.plot(list4, list44, 'pink', label="75° percentile")
  484. plt.legend(loc='upper left')
  485. plt.show()
  486.  
  487. def print_utilization(self):
  488. list1 = []
  489. list11 = []
  490. list2 = []
  491. list22 = []
  492. list3 = []
  493. list33 = []
  494. length = len(self.l1)
  495. plt.figure(1)
  496. for i in range(length):
  497. b = self.l1[i]
  498. list1.append(b['_id'])
  499. list11.append(b['count'])
  500. b = self.l2[i]
  501. list2.append(b['_id'])
  502. list22.append(b['count'])
  503. b = self.l3[i]
  504. list3.append(b['_id'])
  505. list33.append(b['count'])
  506. print(list33)
  507. plt.grid()
  508. plt.title('Filtered_utilization_Booking') # "CDFParkingBooking"
  509. plt.xlabel('Hour')
  510. plt.ylabel('#cars')
  511. plt.plot(list1, list11, 'c', label="Roma_enjoy")
  512. plt.plot(list2, list22, 'b', label="Roma_Car2Go")
  513. plt.plot(list3, list33, 'm', label="Torino_Car2GO")
  514. plt.legend(loc='upper left')
  515. plt.show()
  516.  
  517. # this function counts the parked car in different time intervals and write the data in a *.csv file
  518. # it also searches for the egdes coordinates in order to be able to calculate the grid 1 km x 1 km
  519. def print_csv(list):
  520. k = 4
  521. j = 0
  522. temp = []
  523. time_grid=[]
  524. time_grid_1kmq = []
  525. while (k<25):
  526. min_lat = 50 #45.01089
  527. min_lon = 50 #7.60684
  528. max_lat = 0 #45.18843
  529. max_lon = 0 #7.73177
  530. temp.clear()
  531.  
  532. f = open('position' + str(k)+ '.csv', mode = 'w')
  533. for i in range(len(list)):
  534. if list[i]["_id"]["start"] >= j and list[i]["_id"]["start"] < k and list[i]["_id"]["end"] >= k:
  535. f.write(str(list[i]["_id"]['x']))
  536. f.write(',')
  537. f.write(str(list[i]["_id"]['y']))
  538. f.write(',')
  539. f.write(str(list[i]["_id"]['start']))
  540. f.write(',')
  541. f.write(str(list[i]["_id"]['end']))
  542. f.write("\n")
  543. temp.append(list[i])
  544. if list[i]["_id"]['y'] < min_lat:
  545. min_lat = list[i]["_id"]['y']
  546. if list[i]["_id"]['y'] > max_lat:
  547. max_lat = list[i]["_id"]['y']
  548. if list[i]["_id"]['x'] < min_lon:
  549. min_lon = list[i]["_id"]['x']
  550. if list[i]["_id"]['x'] > max_lon:
  551. max_lon = list[i]["_id"]['x']
  552. f.close()
  553. grid, grid_1kmq, n_lat_1kmq, n_lon_1kmq = write_heatmap(min_lat, max_lat, min_lon, max_lon, k)
  554. time_grid_1kmq.append(grid_1kmq)
  555. j = k
  556. k = k + 6
  557. return grid, time_grid_1kmq, n_lat_1kmq, n_lon_1kmq
  558.  
  559. # this function uses the coordinates edges to calculate the grid
  560. # two different grids are saved: the first one with a polygon shape to feed the geojason,
  561. # the second one with only the 4 coordinates
  562. #this second grid is then ordered, in order to match the index with the number assigned the figure
  563.  
  564. def write_heatmap(min_lat, max_lat, min_lon, max_lon, n):
  565. print('max lat', max_lat)
  566. print('min lat', min_lat)
  567. print('max lon', max_lon)
  568. print('max lon', min_lon)
  569.  
  570. f = open('density' + str(n) + '.csv', mode='w')
  571. prev_lat = min_lat
  572. prev_lon = min_lon
  573. grid = []
  574. grid_1kmq = []
  575. n_lat_1kmq = int((max_lat - min_lat)//0.01)
  576. n_lon_1kmq = int((max_lon - min_lon)//0.01)
  577.  
  578. for y in range(int(10)):
  579. if y!=0:
  580. prev_lat = lat
  581. else:
  582. prev_lat = min_lat
  583. lat = min_lat + 0.01*(y+1)
  584. prev_lon = min_lon
  585. for x in range(int(n_lon_1kmq)):
  586. lon = min_lon + 0.01*(x+1)
  587. grid_1kmq.append([[[prev_lon, prev_lat], [lon,prev_lat], [lon, lat], [prev_lon, lat], [prev_lon, prev_lat]]])
  588. grid.append([prev_lon, lon, prev_lat, lat])
  589. prev_lon = lon
  590.  
  591. orderd_grid = []
  592. for i in range(10):
  593. for j in range(12):
  594. orderd_grid.append(grid_1kmq[len(grid_1kmq) - (1+i)*12 + j])
  595. return grid, orderd_grid, n_lat_1kmq, n_lon_1kmq
  596.  
  597. def write_od(n_lat_1kmq, n_lon_1kmq, grid4_coord):
  598. origin = []
  599. destination = []
  600. print(len(grid4_coord))
  601. l=0
  602. with open("data.txt", "r") as filestream:
  603. for line in filestream:
  604. if l == 0:
  605. l = 1
  606. else:
  607. current_line = line.split("\n")
  608. c = current_line[0].split(" ")
  609. print(c)
  610. origin.append(c[0].split(","))
  611. destination.append(c[1].split(","))
  612. print(n_lat_1kmq, n_lon_1kmq)
  613. print("n° celle", 120)
  614. od_matrix = np.zeros((120, 120))
  615. f = open("od_matrix.csv", mode='w')
  616.  
  617. for i in range(120):
  618. start = grid4_coord[i]
  619. print(i)
  620. for j in range(120):
  621. stop = grid4_coord[j]
  622.  
  623. for z in range(0, len(origin)):
  624. if float(origin[z][0]) > start[0] and float(origin[z][0]) < start[1] and float(origin[z][1]) > start[2] and float(origin[z][1]) < start[3]:
  625. if float(destination[z][0]) > stop[0] and float(destination[z][0]) < stop[1] and float(destination[z][1]) > stop[2] and float(destination[z][1]) < stop[3]:
  626. od_matrix[i, j] = od_matrix[i, j] + 1
  627.  
  628.  
  629. f.write(str(od_matrix[i, j]))
  630. f.write(",")
  631.  
  632. f.write("\n")
  633. f.close()
  634. plt.figure(1)
  635. plt.matshow(od_matrix)
  636. plt.show()
  637.  
  638.  
  639.  
  640.  
  641.  
  642.  
  643. if __name__ == "__main__":
  644. c=Connect()
  645. db, parking_enjoy, parking_car2go, booking_enjoy, booking_car2go = c.connection()
  646. city_rome = "Roma"
  647. city_turin = "Torino"
  648. startDate = datetime.datetime(2017, 10, 1).timestamp()
  649. endDate = datetime.datetime(2017, 10, 31, 23, 59, 59).timestamp()
  650. filter = Filter(db,startDate,endDate)
  651.  
  652. #ES 1 - CDF
  653.  
  654. #list1 = filter.nofilter(city_rome,collection1)
  655. #list2 = filter.nofilter(city_rome, collection2)
  656. #list3 = filter.nofilter(city_turin, collection2)
  657. #cdf = Printer(list1,list2,list3)
  658. #cdf.print()
  659.  
  660. # list1 = filter.perhour(city_rome, collection1)
  661. # list2 = filter.perhour(city_rome, collection2)
  662. # list3 = filter.perhour(city_turin, collection2)
  663.  
  664. # rome_car2go = filter.nofilteraday(city_rome, booking_car2go) # fare funzione apposta
  665. # rome_enjoy = filter.nofilteraday(city_rome, parking_enjoy)
  666. # turin_car2go = filter.nofilteraday(city_turin, booking_car2go)
  667. # week = Printer(rome_car2go, rome_enjoy, turin_car2go)
  668. # week.print_week()
  669.  
  670. # list1 = filter.nofilteraweek(city_rome, collection1)
  671. # list2 = filter.nofilteraweek(city_rome, collection2)
  672. # list3 = filter.nofilteraweek(city_turin, collection2)
  673. # cdf = Printer(list1,list2,list3)
  674. # cdf.printday()
  675.  
  676.  
  677. #ES 2 - Utilization
  678. # list1 = filter.filteredUTIL(city_rome, collection1)
  679. # list2 = filter.filteredUTIL(city_rome, collection2)
  680. # list3 = filter.filteredUTIL(city_turin, collection2)
  681. # cdf = Printer(list1, list2, list3)
  682. # cdf.print_utilization()
  683.  
  684. #ES 3 -Filter
  685. # list1=filter.filtered(city_rome,collection1)
  686. # list2 = filter.filtered(city_rome, collection2)
  687. # list3 = filter.filtered(city_turin, collection2)
  688. # cdf = Printer(list1, list2, list3)
  689. # cdf.print()
  690.  
  691. #ES 4 - Utilization Filtered
  692.  
  693. #ES 5 - statistics
  694. #PARKING
  695. #Rome Enjoy
  696. # avg_RE = filter.day_average(city_rome, parking_enjoy)
  697. # std_RE = filter.day_std(city_rome, parking_enjoy)
  698. # med_RE, per_RE = filter.day_median_and_percentile(city_rome, parking_enjoy)
  699. # print_RE = Printer(avg_RE, std_RE, med_RE)
  700. # print_RE.print_statistics(per_RE, 'Statistic on parking over October 2017 - Rome Enjoy')
  701. #Rome Car2Go
  702. # avg_RC = filter.day_average(city_rome, parking_car2go)
  703. # std_RC = filter.day_std(city_rome, parking_car2go)
  704. # med_RC, per_RC = filter.day_median_and_percentile(city_rome, parking_car2go)
  705. # print_RC = Printer(avg_RC, std_RC, med_RC)
  706. # print_RC.print_statistics(per_RC, 'Statistic on parking over October 2017 - Rome Car2Go')
  707. #Turin Car2Go
  708. # avg_TC = filter.day_average(city_turin, parking_car2go)
  709. # std_TC = filter.day_std(city_turin, parking_car2go)
  710. # med_TC, per_TC = filter.day_median_and_percentile(city_turin, parking_car2go)
  711. # print_TC = Printer(avg_TC, std_TC, med_TC)
  712. # print_TC.print_statistics(per_TC, 'Statistic on parking over October 2017 - Turin Car2Go')
  713. #BOOKING
  714. #Rome Enjoy
  715. # avg_RE = filter.day_average(city_rome, booking_enjoy)
  716. # std_RE = filter.day_std(city_rome, booking_enjoy)
  717. # med_RE, per_RE = filter.day_median_and_percentile(city_rome, booking_enjoy)
  718. # print_RE = Printer(avg_RE, std_RE, med_RE)
  719. # print_RE.print_statistics(per_RE, 'Statistic on booking over October 2017 - Rome Enjoy')
  720. #Rome Car2Go
  721. # avg_RC = filter.day_average(city_rome, booking_car2go)
  722. # std_RC = filter.day_std(city_rome, booking_car2go)
  723. # med_RC, per_RC = filter.day_median_and_percentile(city_rome, booking_car2go)
  724. # print_RC = Printer(avg_RC, std_RC, med_RC)
  725. # print_RC.print_statistics(per_RC, 'Statistic on booking over October 2017 - Rome Car2Go')
  726. #Turin Car2Go
  727. # avg_TC = filter.day_average(city_turin, booking_car2go)
  728. # std_TC = filter.day_std(city_turin, booking_car2go)
  729. # med_TC, per_TC = filter.day_median_and_percentile(city_turin, booking_car2go)
  730. # print_TC = Printer(avg_TC, std_TC, med_TC)
  731. # print_TC.print_statistics(per_TC, 'Statistic on booking over October 2017 - Turin Car2Go')
  732.  
  733. #ES 6 - Map
  734. #Rome Enjoy
  735.  
  736. car_position = filter.get_car_position(city_turin, parking_car2go)
  737. grid4_coord, time_grid_1kmq, n_lat_1kmq, n_lon_1kmq = print_csv(car_position)
  738. write_od(n_lat_1kmq, n_lon_1kmq, grid4_coord)
  739. exit()
  740. #list=filter.perhour(city1,collection1)
  741. #list = filter.nofilteraweek(city1, collection1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement