0

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

Community
  • 1
  • 1
jlt199
  • 2,349
  • 6
  • 23
  • 43

1 Answers1

1

Sorry, I wasn't comparing like with like. Now looking at the same file I see that the calculated areas are the same! So the 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)))

Does work how I thought it should and is much faster than the other code.

I will delete this post later giving those who are looking at time to see my answer

jlt199
  • 2,349
  • 6
  • 23
  • 43
  • If you can extent this to a [mcve], you can as well keep it, as it may be useful for others. If you decide not to produce a minimal example, you can delete it, because it will not help much. – ImportanceOfBeingErnest Mar 21 '17 at 00:21