def compare_travel_data_sets(self): # get copied travel data csv copied_travel_data_location = os.path.join(self.destination, "opus_matsim", "tmp") if not os.path.exists(copied_travel_data_location): raise StandardError("Travel data not found: %s" % copied_travel_data_location) logger.log_status("Get copied travel data: %s" % copied_travel_data_location) # convert travel data csv into travel data set matrix in_storage = csv_storage(storage_location=copied_travel_data_location) table_name = "travel_data" travel_data_attribute = "single_vehicle_to_work_travel_cost" travel_data_set = TravelDataDataset(in_storage=in_storage, in_table_name=table_name) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=999) # get exsisting travel data set and convert it also into travel data set matrix year = self.run_config["base_year"] + 2 attribute_cache = AttributeCache(cache_directory=self.run_config["cache_directory"]) cache_storage = attribute_cache.get_flt_storage_for_year(year) existing_travel_data_set = TravelDataDataset(in_storage=cache_storage, in_table_name=table_name) existing_travel_data_attribute_mat = existing_travel_data_set.get_attribute_as_matrix( travel_data_attribute, fill=999 ) from numpy import savetxt # for debugging savetxt(os.path.join(self.destination, "origin_travel_data.txt"), travel_data_attribute_mat, fmt="%f") savetxt(os.path.join(self.destination, "existing_travel_data"), existing_travel_data_attribute_mat, fmt="%f") # compare both data set matices compare = travel_data_attribute_mat == existing_travel_data_attribute_mat # return result return compare.all()
def compare_travel_data_sets(self): # get copied travel data csv copied_travel_data_location = os.path.join( self.destination, 'opus_matsim', 'tmp') if not os.path.exists(copied_travel_data_location): raise StandardError('Travel data not found: %s' % copied_travel_data_location) logger.log_status('Get copied travel data: %s' % copied_travel_data_location) # convert travel data csv into travel data set matrix in_storage = csv_storage(storage_location = copied_travel_data_location) table_name = "travel_data" travel_data_attribute = 'single_vehicle_to_work_travel_cost' travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=999) # get exsisting travel data set and convert it also into travel data set matrix year = self.run_config['base_year']+2 attribute_cache = AttributeCache(cache_directory=self.run_config['cache_directory']) cache_storage = attribute_cache.get_flt_storage_for_year(year) existing_travel_data_set = TravelDataDataset( in_storage=cache_storage, in_table_name=table_name ) existing_travel_data_attribute_mat = existing_travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=999) from numpy import savetxt # for debugging savetxt( os.path.join(self.destination, 'origin_travel_data.txt'), travel_data_attribute_mat , fmt="%f") savetxt( os.path.join(self.destination, 'existing_travel_data') , existing_travel_data_attribute_mat, fmt="%f") # compare both data set matices compare = travel_data_attribute_mat == existing_travel_data_attribute_mat # return result return compare.all()
def test_run(self): print "Entering test run" logger.log_status("Loading travel data: %s" % self.travel_data_source) # get travel data as an attribute marix in_storage = csv_storage(storage_location=self.travel_data_source_dir) table_name = "travel_data" travel_data_attribute = "single_vehicle_to_work_travel_cost" travel_data_set = TravelDataDataset(in_storage=in_storage, in_table_name=table_name) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=0) # determine location to store and read attribute matrix location1 = os.path.join(self.tempDir, "attrib_matrix1.txt") location2 = os.path.join(self.tempDir, "attrib_matrix2.txt") # store attribute matrix savetxt(location1, travel_data_attribute_mat, fmt="%i") savetxt(location2, travel_data_attribute_mat, fmt="%i") # read attribute matrix matrix1 = genfromtxt(location1) matrix2 = genfromtxt(location2) # compare both matices result = matrix1 == matrix2 self.assertTrue(result.all()) print "Leaving test run"
def test_run(self): print "Entering test run" logger.log_status('Loading travel data: %s' % self.travel_data_source) # get travel data as an attribute marix in_storage = csv_storage(storage_location=self.travel_data_source_dir) table_name = "travel_data" travel_data_attribute = 'single_vehicle_to_work_travel_cost' travel_data_set = TravelDataDataset(in_storage=in_storage, in_table_name=table_name) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix( travel_data_attribute, fill=0) # determine location to store and read attribute matrix location1 = os.path.join(self.tempDir, 'attrib_matrix1.txt') location2 = os.path.join(self.tempDir, 'attrib_matrix2.txt') # store attribute matrix savetxt(location1, travel_data_attribute_mat, fmt="%i") savetxt(location2, travel_data_attribute_mat, fmt="%i") # read attribute matrix matrix1 = genfromtxt(location1) matrix2 = genfromtxt(location2) # compare both matices result = (matrix1 == matrix2) self.assertTrue(result.all()) print "Leaving test run"
def test_run(self): print "Entering test run" path = paths.get_opus_home_path('opus_matsim', 'tmp') # check if travel data exsits travel_data = os.path.join( path, "travel_data.csv" ) if not os.path.exists(travel_data): print "Travel Data not found!!!" sys.exit() in_storage = csv_storage(storage_location = path) table_name = "travel_data" travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) origin_zones = travel_data_set.get_attribute_as_column(self.origin_zone_id) l = numpy.atleast_1d(origin_zones).tolist() origin_list = set(l) # removes duplicates and sorts the list in ascending order # destination_list = len(origin_list) * self.cbd # creates a list that contains the zone id of the cbd an has the same length as "origin_list" # set high travel costs for all origin to cbd pairs for id in origin_list: travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, self.high_travel_cost, id, self.cbd) # adjust cbd to cbd travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, 0.0, self.cbd, self.cbd) # adjust prefered zone to cbd travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, self.low_travel_cost, self.preferential_zone, self.cbd) w = travel_data_set.get_index_by_origin_and_destination_ids(110, 129) x = travel_data_set.get_index_by_origin_and_destination_ids(129, 129) y = travel_data_set.get_index_by_origin_and_destination_ids(20, 129) z = travel_data_set.get_index_by_origin_and_destination_ids(20, 20) print w print x print y print z origin_zones = travel_data_set.get_attribute_as_column(self.origin_zone_id) destination_zones = travel_data_set.get_attribute_as_column(self.destination_zone_id) my_travel_data_attr_mat = travel_data_set.get_attribute_as_matrix('travel_data.single_vehicle_to_work_travel_cost', fill=999) my_travel_data_attr_mat[origin_zones, destination_zones] = 1.03 cbd_ids = where(origin_zones == 129) print "Leaving test run"
def test_run(self): print "Entering test run" path = os.path.join(os.environ['OPUS_HOME'], 'opus_matsim/tmp') # check if travel data exsits travel_data = os.path.join( path, "travel_data.csv" ) if not os.path.exists(travel_data): print "Travel Data not found!!!" sys.exit() in_storage = csv_storage(storage_location = path) table_name = "travel_data" travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) origin_zones = travel_data_set.get_attribute_as_column(self.origin_zone_id) l = numpy.atleast_1d(origin_zones).tolist() origin_list = set(l) # removes duplicates and sorts the list in ascending order # destination_list = len(origin_list) * self.cbd # creates a list that contains the zone id of the cbd an has the same length as "origin_list" # set high travel costs for all origin to cbd pairs for id in origin_list: travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, self.high_travel_cost, id, self.cbd) # adjust cbd to cbd travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, 0.0, self.cbd, self.cbd) # adjust prefered zone to cbd travel_data_set.set_values_of_one_attribute_with_od_pairs(self.travel_data_attribute, self.low_travel_cost, self.preferential_zone, self.cbd) w = travel_data_set.get_index_by_origin_and_destination_ids(110, 129) x = travel_data_set.get_index_by_origin_and_destination_ids(129, 129) y = travel_data_set.get_index_by_origin_and_destination_ids(20, 129) z = travel_data_set.get_index_by_origin_and_destination_ids(20, 20) print w print x print y print z origin_zones = travel_data_set.get_attribute_as_column(self.origin_zone_id) destination_zones = travel_data_set.get_attribute_as_column(self.destination_zone_id) my_travel_data_attr_mat = travel_data_set.get_attribute_as_matrix('travel_data.single_vehicle_to_work_travel_cost', fill=999) my_travel_data_attr_mat[origin_zones, destination_zones] = 1.03 cbd_ids = where(origin_zones == 129) print "Leaving test run"
def test_run(self): print "Entering test run" # This test checks if the pre-computed MATSim travel data # is loaded correctly into UrbanSim. Therefore the UrbanSim # travel data matrix is converted into a numpy array and two # predefined values are checked whether they match with MATSim data. # # Here an example: # my_list = [[1,2,3], # [4,5,6], # [7,8,9]] # # my_list[0][1] should be = 2 # my_list[2][2] should be = 9 table_name = 'travel_data' travel_data_attribute = 'single_vehicle_to_work_travel_cost' # location of pre-calculated MATSim travel costs in_storage = csv_storage(storage_location = self.input_directory) # create travel data set (travel costs) travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=31) # converting from numpy array into a 2d list travel_list = numpy.atleast_2d(travel_data_attribute_mat).tolist() # get two values for validation value1 = int(travel_list[1][1]) # should be = 0 value2 = int(travel_list[2][1]) # should be = 120 logger.log_status('First validation value should be 0. Current value is %i' % value1) logger.log_status('Second validation value should be 120. Current value is %i' % value2) self.assertTrue( value1 == 0 ) self.assertTrue( value2 == 120 ) # self.dump_travel_list(travel_list) # for debugging print "Leaving test run"
def test_run(self): print "Entering test run" # This test loads an exising travel data as a TravelDataSet (numpy array) # and accesses single (pre-known) values to validate the conversion process # (numpy array into standard python list). # # Here an example: # my_list = [[1,2,3], # [4,5,6], # [7,8,9]] # # my_list[0][1] should be = 2 # my_list[2][2] should be = 9 table_name = 'travel_data' travel_data_attribute = 'single_vehicle_to_work_travel_cost' # location of pre-calculated MATSim travel costs in_storage = csv_storage(storage_location = self.input_directory) # create travel data set (travel costs) travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=31) # converting from numpy array into a 2d list travel_list = numpy.atleast_2d(travel_data_attribute_mat).tolist() # get two values for validation value1 = int(travel_list[1][1]) # should be = 0 value2 = int(travel_list[2][1]) # should be = 120 logger.log_status('First validation value should be 0. Current value is %i' % value1) logger.log_status('Second validation value should be 120. Current value is %i' % value2) self.assertTrue( value1 == 0 ) self.assertTrue( value2 == 120 ) # self.dump_travel_list(travel_list) # for debugging print "Leaving test run"
def get_travel_cost_matrix(self): ''' Returns the pre-calculated MATSim travel costs as 2d list ''' # get sensitivity test path test_dir_path = test_dir.__path__[0] input_directory = os.path.join( test_dir_path, 'data', 'travel_cost') print "input_directory: %s" % input_directory # check source file if not os.path.exists( input_directory ): print 'File not found! %s' % input_directory sys.exit() table_name = 'travel_data' travel_data_attribute = 'single_vehicle_to_work_travel_cost' # location of pre-calculated MATSim travel costs in_storage = csv_storage(storage_location = input_directory) # create travel data set (travel costs) travel_data_set = TravelDataDataset( in_storage=in_storage, in_table_name=table_name ) travel_data_attribute_mat = travel_data_set.get_attribute_as_matrix(travel_data_attribute, fill=999) # travel cost matris as 2d array travel_list = numpy.atleast_2d(travel_data_attribute_mat).tolist() return travel_list