tool-competition-av
tool-competition-av copied to clipboard
Fix calculation of direction coverage
It seems that the pipeline uses the following direction_coverage function for the road analysis.
I found two issues:
- first
n_bins
is 25 by default, but should be 35, so it's 36 bins in total. The calculated data is thus split differently than the comment indicates - second, the code uses
np.arccos(dot_product)
for the calculation of the angle. However,np.arccos
produces values in the range [0, π] (i.e. [0, 180] degrees). In other words, this function calculates the smallest angle betweenTHE_NORTH
and the segment, independent of the side.
I suggest the following code to fix it (note, it uses numpy throughout and is thus faster than the previous method).
def get_thetas(xs, ys, skip=1):
"""Transform x,y coordinates of points and return each segment's offset from x-axis in the range [np.pi, np.pi]"""
xdiffs = xs[1:] - xs[0:-1]
ydiffs = ys[1:] - ys[0:-1]
thetas = np.arctan2(ydiffs, xdiffs)
return thetas
def dir_cov(the_test, n_bins=36):
np_arr = np.array(the_test)
thetas, kappas = get_thetas(np_arr[:,0], np_arr[:,1])
coverage_buckets = np.linspace(-np.pi, np.pi, num=n_bins)
covered_elements = set(np.digitize(thetas, coverage_buckets))
return len(covered_elements) / len(coverage_buckets)
According to my tests on the data, this behaviour should be correct.