Skip to content
Snippets Groups Projects
Commit 022310ec authored by FangJiangyi's avatar FangJiangyi
Browse files

Add Region Generation Template

parent b5c091cb
Branches master
No related tags found
No related merge requests found
import pandas as pd
from UCTB.preprocess.RegionGenerator import RegionGenerator
from UCTB.preprocess.dataset_helper import convert_uctb_data
# NYC
# initialize the configuration of the RegionGenerator
spatial_range = []
area_limit = 1
region_generator = RegionGenerator(spatial_range=spatial_range,area_limit=area_limit)
# regions are created in self.regions
region_generator.partition(method='grid')
service_record_filepath = 'trip_record.csv'
df = pd.read_csv(service_record_filepath)
# service records are binded in self.demand_matrix
region_generator.bind(df,method='location_based')
# cluster elements to acquire regions with better charateristics
regions,demand_matrix = region_generator.aggregate(cluster_method='node_swapping')
time_fitness = 5
time_range = ['2013-01-01','2014-01-01']
uctb_data = convert_uctb_data(regions,demand_matrix,time_fitness,time_range)
import logging
import numpy as np
def grid_partition():
# To be implemented
pass
def hexagon_partition():
# To be implemented
pass
def roadnetwork_partition():
# To be implemented
pass
def location_bind():
# To be implemented
pass
def async_fluid():
# To be implemented
pass
def node_swapping():
# To be implemented
pass
class RegionGenerator():
'''
This class is used to generate regions and create demand matrix.
Regions will be stored into self.regions and demand matrix will be stored into self.demand_matrix
This class should be instantiated before NodeTrafficLoader is instantiated.
Args:
spatial_range: list([lat_min,lat_max,lon_min,lon_max])
area_limit: int (All regions should be smaller than)
'''
partition_func_dict = {
'grid':grid_partition,
'hexagon':hexagon_partition,
'road_network':roadnetwork_partition
}
bind_func_dict = {
'location':location_bind
}
cluster_func_dict = {
'async_fluid':async_fluid,
'node_swapping':node_swapping
}
def __init__(self,spatial_range,area_limit) -> None:
self.lat_min = spatial_range[0]
self.lat_max = spatial_range[1]
self.lon_min = spatial_range[2]
self.lon_max = spatial_range[3]
self.area_limit = area_limit
def partition(self,method,**params) -> any:
if method not in self.partition_func_dict:
logging.error(f"Unsupported method of partition: {method}. Skipping.")
else:
self.regions = self.partition_func_dict[method](**params)
def bind(self,df,method,**params) -> any:
if method not in self.bind_func_dict:
logging.error(f"Unsupported method of bind: {method}. Skipping.")
else:
self.demand_matrix = self.bind_func_dict[method](**params)
pass
def aggregate(self,cluster_method,merge_way='sum',**params) -> any:
if cluster_method not in self.partition_func_dict:
logging.error(f"Unsupported method of aggregation: {cluster_method}. Skipping.")
else:
regions2clusters,_ = self.cluster_func_dict[cluster_method](self.regions,**params)
num_clusters = max(regions2clusters)
new_demand_matrix = np.zeros([self.demand_matrix.shape[0],num_clusters])
for i in range(num_clusters):
if merge_way == 'sum':
new_demand_matrix[:,i] = np.sum(self.demand_matrix[:,np.nonzero(regions2clusters==i)[0]])
elif merge_way == 'average':
new_demand_matrix[:,i] = np.average(self.demand_matrix[:,np.nonzero(regions2clusters==i)[0]])
else:
raise KeyError('not implemented')
new_regions = None # aggregate regions in to new regions (should be a single geometry but not collections of geometry)
return new_regions,new_demand_matrix
......@@ -74,4 +74,9 @@ def build_uctb_dataset(traffic_node, time_fitness, node_station_info, time_range
pkl_file_name = os.path.join(output_dir, '{}_{}.pkl'.format(dataset_name, city))
with open(pkl_file_name, 'wb') as f:
pickle.dump(dataset, f)
\ No newline at end of file
pickle.dump(dataset, f)
def convert_uctb_data():
# To be implemented
pass
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment