Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. from math import radians, cos, sin, asin, sqrt
  2. def haversine(lon1, lat1, lon2, lat2):
  3.     """
  4.    Calculate the great circle distance between two points
  5.    on the earth (specified in decimal degrees)
  6.    """
  7.     # convert decimal degrees to radians
  8.     lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])
  9.     # haversine formula
  10.     dlon = lon2 - lon1
  11.     dlat = lat2 - lat1
  12.     a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
  13.     c = 2 * asin(sqrt(a))
  14.     km = 6367 * c
  15.     return km
  16.  
  17. import datetime
  18.  
  19. import numpy as np
  20. import pandas as pd
  21. #
  22. #import geopandas as gpd
  23. #from shapely.geometry import Point
  24. #from shapely.geometry import LineString
  25.  
  26. import matplotlib
  27. import matplotlib.pyplot as plt
  28. matplotlib.style.use('ggplot')
  29.  
  30. from pandas.tools.plotting import scatter_matrix
  31.  
  32. from DataBaseProxy import DataBaseProxy
  33. dbp = DataBaseProxy()
  34.    
  35. def process_books_df (provider, books_df):
  36.  
  37.     def get_bill (books_df, provider):
  38.        
  39.         if provider == "car2go":
  40.             books_df["bill"] = books_df["durations"].apply(lambda x: x * 0.24)
  41.         elif provider == "enjoy":
  42.             books_df["bill"] = books_df["durations"].apply(lambda x: x * 0.25)
  43.            
  44.         return books_df    
  45.    
  46.     books_df["durations"] = \
  47.         (books_df["end"] - books_df["start"])/np.timedelta64(1, 'm')
  48.     books_df["distances"] = books_df.apply\
  49.         (lambda row: haversine(row["start_lon"], row["start_lat"],
  50.                                row["end_lon"], row["end_lat"]), axis=1)
  51.     books_df = get_bill(books_df, provider)
  52.        
  53. #    books_df["geometry"] = books_df.apply\
  54. #        (lambda row: LineString([(row["start_lon"], row["start_lat"]),
  55. #                                 (row["end_lon"], row["end_lat"])]), axis=1)
  56. #    books_df = gpd.GeoDataFrame(books_df, geometry="geometry")
  57. #    books_df.crs = {"init": "epsg:4326"}
  58.    
  59.     books_df = books_df[books_df.durations < 120]
  60.     books_df = books_df[books_df.distances > 1]
  61.  
  62.     return books_df
  63.    
  64. def minmax_bill (provider, df):
  65.     process_books_df(provider,df)
  66.     riding_time(provider, df)
  67.     if provider == "car2go":
  68.         free_reservation = 20
  69.         ticket = 0.24
  70.         extra_ticket = 0.24    
  71.     elif provider == "enjoy":
  72.         free_reservation = 15
  73.         ticket = 0.25
  74.         extra_ticket = 0.10    
  75.  
  76.     indexes = df.loc[df.reservation_time > free_reservation].index
  77.     extra_minutes = df.loc[indexes, 'reservation_time'] - free_reservation
  78.     df.loc[indexes,"min_bill"] = df.loc[indexes, 'riding_time'].apply(lambda x: x * ticket) + \
  79.                                             extra_minutes.apply(lambda x: x * extra_ticket) # extra bill to res                                      
  80.     df.loc[indexes,"max_bill"] = df.loc[indexes, 'durations'].apply(lambda x: x * ticket)   # no reservation, all rentals
  81.                                          
  82.     indexes = df.loc[(df.reservation_time <= free_reservation) & (df.reservation_time > 0)].index
  83.     df.loc[indexes,"min_bill"] = df.loc[indexes, 'riding_time'].apply(lambda x: x * ticket) #                    
  84.     df.loc[indexes,"max_bill"] = df.loc[indexes, 'riding_time'].apply(lambda x: x * ticket) #
  85.    
  86.     indexes = df.loc[df.reservation_time < 0].index
  87.     df.loc[indexes,"min_bill"] = df.loc[indexes, 'riding_time'].apply(lambda x: x * ticket)
  88.     df.loc[indexes,"max_bill"] = df.loc[indexes, 'riding_time'].apply(lambda x: x * ticket)
  89.     return df
  90.    
  91. def get_duration_transit(df):
  92.     indexes = df.loc[df.fare.notnull].index
  93.     waiting_time = (df[indexes,"departure_time_google"].time() - df[indexes,"start"].time())/np.timedelta64(1, 'm')
  94.     df[indexes,"tot_duration_transit"] = df[indexes, "duration_google_transit"] + waiting_time
  95.  
  96. def riding_time (provider, df):    
  97.  
  98.    
  99.     df["reservation_time"] = df["durations"] - df["duration_driving"]
  100.     df.loc[df.reservation_time < 0, "riding_time"] = df["durations"]
  101.     df.loc[df.reservation_time > 0, "riding_time"] = df["duration_driving"]
  102.     return df
  103.    
  104. def get_books_days (city, provider, end, depth):
  105.    
  106.     start = end - datetime.timedelta(days = depth)
  107.     books_df = dbp.get_books(provider, city, start, end)
  108.    
  109.     return process_books_df(provider, books_df)
  110.  
  111. def get_books_day (city, provider, year, month, day):
  112.        
  113.     end = datetime.datetime(year, month, day, 23, 59, 59)
  114.     start = end - datetime.timedelta(days = 1)
  115.     books_df = dbp.get_books(provider, city, start, end)
  116.    
  117.     return process_books_df(provider, books_df)
  118.  
  119. #df = process_books_df("car2go", df)
  120. df = minmax_bill("car2go", df)
  121.  
  122. #books_df = get_books_day("torino", "car2go", 2016, 12, 6)
  123.  
  124. #fig, ax = plt.subplots(1,1,figsize=(10,10))
  125. #ax = scatter_matrix(df[["durations", "distances", "bill", "max_bill", "min_bill", "riding_time"]].astype("float64"),
  126. #               figsize=(10, 10), diagonal='kde')
  127. #plt.savefig("car2go" + "_books_scatter_matrix.png")
  128.  
  129. #zones = gpd.read_file("../../SHAPE/Zonizzazione.dbf")\
  130. #        .to_crs({"init": "epsg:4326"})
  131. #zones_geo = zones["geometry"]
  132.  
  133. #fig, ax = plt.subplots(1,1,figsize=(10,10))
  134. #ax = zones_geo.plot(color="white", ax=ax)
  135. ##-car2go
  136. #ax.set_xlim([7.6, 7.8])
  137. #ax.set_ylim([45.0,45.20])
  138. ##-enjoy
  139. ##ax.set_xlim([7.6, 7.8])
  140. ##ax.set_ylim([44.95,45.12])
  141. #ax = books_df.plot(ax=ax)
  142. #fig.savefig(provider + "_books_pattern.png")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement