def test_example_tiny_smart_loading(self): subfolder = "ExampleTiny" # big data from classical v2 folder = join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=False) incidence_mat, travel_times_mat = attrs # left, right, _, u_turn = AngleProcessor.get_turn_categorical_matrices() self._first_example_common_data_checks(travel_times_mat, incidence_mat, obs_mat)
def test_compare_optim_methods(self): subfolder = "ExampleTinyModifiedObs" # big data from classical v2 folder = join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=True) import awkward1 as ak obs_mat = obs_mat.toarray() obs_record = ak.from_numpy(obs_mat) incidence_mat, travel_times_mat, angle_cts_mat = attrs left, _, _, u_turn = AngleProcessor.get_turn_categorical_matrices( angle_cts_mat, incidence_mat) data_list = [travel_times_mat, left, u_turn] network_data_struct = ModelDataStruct(data_list, incidence_mat) # network_data_struct.add_second_travel_time_for_testing() optimiser = optimisers.LineSearchOptimiser( optimisers.OptimHessianType.BFGS, max_iter=4) RecursiveLogitModelEstimation.zeros_error_override = False model = RecursiveLogitModelEstimation(network_data_struct, optimiser, observations_record=obs_record, initial_beta=-15) m1_ll_out, m1_grad_out = model.get_log_likelihood() optimiser2 = optimisers.ScipyOptimiser(method='newton-cg') model2 = RecursiveLogitModelEstimation(network_data_struct, optimiser2, observations_record=obs_record, initial_beta=-15) m2_ll_out, m2_grad_out = model2.get_log_likelihood() assert np.allclose(m2_ll_out, m1_ll_out) assert np.allclose(m2_grad_out, m1_grad_out) beta1 = model.solve_for_optimal_beta() beta2 = model2.solve_for_optimal_beta(verbose=True) m1_ll_out, m1_grad_out = model.get_log_likelihood() m2_ll_out, m2_grad_out = model2.get_log_likelihood() print(m1_ll_out, m2_ll_out) print(m1_grad_out, m2_grad_out) assert np.allclose(beta1, beta2, 0.34657) RecursiveLogitModelEstimation.zeros_error_override = None
def test_example_tiny_modified_awkward_array(self): subfolder = "ExampleTinyModifiedObs" # big data from classical v2 folder = join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=True) import awkward1 as ak obs_mat = obs_mat.toarray() obs_record = ak.from_numpy(obs_mat) incidence_mat, travel_times_mat, angle_cts_mat = attrs left, _, _, u_turn = AngleProcessor.get_turn_categorical_matrices( angle_cts_mat, incidence_mat) # incidence matrix which only has nonzero travel times # - rather than what is specified in file t_time_incidence = (travel_times_mat > 0).astype('int').todok() self._tiny_modified_common_data_checks(travel_times_mat, left, u_turn, t_time_incidence, incidence_mat, obs_record)
def test_example_tiny_modified_sparse(self): # TODO shouldn't be using this data - data is just confusing/ unclear, nothing inherently # wrong # Now is a bad example as @42f564e9 results in this test case being illegal valued subfolder = "ExampleTinyModifiedObs" # big data from classical v2 folder = join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=True) incidence_mat, travel_times_mat, angle_cts_mat = attrs left, _, _, u_turn = AngleProcessor.get_turn_categorical_matrices( angle_cts_mat, incidence_mat) # incidence matrix which only has nonzero travel times # - rather than what is specified in file t_time_incidence = (travel_times_mat > 0).astype('int').todok() self._tiny_modified_common_data_checks(travel_times_mat, left, u_turn, t_time_incidence, incidence_mat, obs_mat)
def test_example_tiny_modified_awkward_array_in_expected_format(self): """Test's awkward array input obs format when it is actually zero indexed and ragged data, not square. See that output is consistent in this case""" subfolder = "ExampleTinyModifiedObs" # big data from classical v2 folder = join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=True) import awkward1 as ak obs_mat = obs_mat.toarray() obs_list_raw = obs_mat.tolist() # we know that obs mat is square and 1 indexed with zero for padding (sparse originally) obs_conv = [[(i - 1) for i in row if i != 0] for row in obs_list_raw] obs_record = ak.from_iter(obs_conv) incidence_mat, travel_times_mat, angle_cts_mat = attrs left, _, _, u_turn = AngleProcessor.get_turn_categorical_matrices( angle_cts_mat, incidence_mat) # incidence matrix which only has nonzero travel times # - rather than what is specified in file t_time_incidence = (travel_times_mat > 0).astype('int').todok() self._tiny_modified_common_data_checks(travel_times_mat, left, u_turn, t_time_incidence, incidence_mat, obs_record)
# np.seterr(all='raise') # all='print') np.set_printoptions(precision=12, suppress=True) np.set_printoptions(edgeitems=10, linewidth=180) np.core.arrayprint._line_width = 200 # import warnings # warnings.simplefilter("error") time_io_start = time.time() # subfolder ="ExampleTutorial"# "ExampleTutorial" from classical logicv2 # subfolder = "ExampleTiny" subfolder = "ExampleTinyModifiedObs" print(os.getcwd()) folder = os.path.join("Datasets", subfolder) obs_mat, attrs = load_standard_path_format_csv(folder, delim=" ", angles_included=True) incidence_mat, travel_times_mat, angle_cts_mat = attrs left, _, _, u_turn = AngleProcessor.get_turn_categorical_matrices( angle_cts_mat, incidence_mat) # incidence matrix which only has nonzero travel times - rather than what is specified in file t_time_incidence = (travel_times_mat > 0).astype('int').todok() data_list = [travel_times_mat, left, u_turn, t_time_incidence] network_data_struct = ModelDataStruct(data_list, incidence_mat) # # network_data_struct, obs_mat = ModelDataStruct.from_directory( # folder, add_angles=True, angle_type='comparison', delim=" ") # # print(network_data_struct.data_array) # # print(network_data_struct.data_array)