I would like to extend this question to find the area of a contour in a multi-threading friendly way.
I've tried the following code:
c = cntr.Cntr(Z, X_, radial)
# Trace a contour at calc_levels
for loop in range(len(calc_levels)):
res = c.trace(calc_levels[loop])
# result is a list of arrays of vertices and path codes
# (see docs for matplotlib.path.Path)
nseg = len(res) // 2
segments = res[:nseg]
area = PolyArea(segments[0][:,0], segments[0][:,1])
def PolyArea(x,y):
return 0.5*np.abs(np.dot(x,np.roll(y,1))-np.dot(y,np.roll(x,1)))
In which I've tried adapting the code from here and where the code for PolyArea comes from this question But 1) the calculation of the area is not quite correct and 2) it slows down my multi-threaded code.
I say that the area is not correct because with the following code, which I spent a long time checking, I get completely different answers. However, this code is DEFINITELY NOT multi-threading compatible and causes the whole code to grind to a halt
fig1 = plt.figure()
ax1 = fig1.add_subplot(111)
cs = ax1.contour(Z,X_,radial, levels = calc_levels,colors='k')
for loop in range(len(calc_levels)):
vs = None
contour_ = None
contour_ = cs.collections[loop]
vs = contour_.get_paths()[0].vertices
# Compute area enclosed by vertices
features['Radial_Area_' + mystr + str(int(thisLevels[loop]*100))] = area(vs)
Is anyone able to help me either debug the first section of code, or write something different that will work better?
Thanks