def main(): option_parser, opts, args = parse_command_line_parameters(**script_info) data, comments = parse_mapping_file_to_dict(opts.input_path) column_headers = [] if ',' not in opts.column: column_data = [] column_name = opts.column for i in data: if column_name not in data[i]: raise ValueError( "No column: '%s' in the mapping file. Existing columns are: %s" % (column_name, data[i].keys())) try: column_data.append(float(data[i][opts.column])) except ValueError: raise ValueError( "All the values in the column '%s' must be numeric but '%s' has '%s'" % (column_name, i, data[i][column_name])) column_headers.append(i) dtx_mtx = compute_distance_matrix_from_metadata(column_data) else: latitudes = [] longitudes = [] try: latitude, longitude = opts.column.split(',') except ValueError: raise ValueError( "This script accepts a maximum of 2 colums separated by comma and you passed: %s" % (opts.column)) for i in data: if latitude not in data[i] or longitude not in data[i]: raise ValueError( "One of these columns or both do not exist: '%s' or '%s' in the mapping file. Existing columns are: %s" % (latitude, longitude, data[i].keys())) try: latitudes.append(float(data[i][latitude])) longitudes.append(float(data[i][longitude])) except ValueError: raise ValueError( "All the values in the columnd '%s' & '%s' must be numeric but '%s' has '%s'" % (latitude, longitude, i, data[i][column_name])) column_headers.append(i) dtx_mtx = calculate_dist_vincenty(latitudes, longitudes) dtx_txt = format_distance_matrix(column_headers, dtx_mtx) outfilepath = os.path.join(opts.output_fp) f = open(outfilepath, 'w') f.write(dtx_txt) f.close()
def test_compute_distance_matrix_from_metadata_int(self): """ distance calculations on ints should throw no errors""" exp_out = array([[0, 0, 92, 9096, 9992, 9894, 18898, 18898, 18898], [0, 0, 92, 9096, 9992, 9894, 18898, 18898, 18898], [92, 92, 0, 9188, 10084, 9986, 18990, 18990, 18990], [9096, 9096, 9188, 0, 896, 798, 9802, 9802, 9802], [9992, 9992, 10084, 896, 0, 98, 8906, 8906, 8906], [9894, 9894, 9986, 798, 98, 0, 9004, 9004, 9004], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0]]) res_out = compute_distance_matrix_from_metadata(self.DOB) self.assertFloatEqual(exp_out, res_out)
def test_compute_distance_matrix_from_metadata_floats(self): """ distance calculations on floats should throw no errors""" # testing floats exp_out = array([[0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], [0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], [0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5], [0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4], [0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3], [0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2], [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1], [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.]]) res_out = compute_distance_matrix_from_metadata(self.Float_Col) self.assertFloatEqual(exp_out, res_out)
def test_compute_distance_matrix_from_metadata_int(self): """ distance calculations on ints should throw no errors""" exp_out = array([[0, 0, 92, 9096, 9992, 9894, 18898, 18898, 18898], [0, 0, 92, 9096, 9992, 9894, 18898, 18898, 18898], [92, 92, 0, 9188, 10084, 9986, 18990, 18990, 18990], [9096, 9096, 9188, 0, 896, 798, 9802, 9802, 9802], [9992, 9992, 10084, 896, 0, 98, 8906, 8906, 8906], [9894, 9894, 9986, 798, 98, 0, 9004, 9004, 9004], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0], [18898, 18898, 18990, 9802, 8906, 9004, 0, 0, 0]]) res_out = compute_distance_matrix_from_metadata(self.DOB) assert_almost_equal(exp_out, res_out)
def test_compute_distance_matrix_from_metadata_floats(self): """ distance calculations on floats should throw no errors""" # testing floats exp_out = array([[0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8], [0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], [0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5, 0.6], [0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4, 0.5], [0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3, 0.4], [0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2, 0.3], [0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1, 0.2], [0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0., 0.1], [0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.]]) res_out = compute_distance_matrix_from_metadata(self.Float_Col) assert_almost_equal(exp_out, res_out)