0

From this question

Finding the best trade-off point on a curve

I am using the Python solution and it works great if I run it by itself.

But I have a dictionary of these 'curves':

sse{}

{'00': [4.998436383771836,
  0.0160165895672664,
  0.004512422186993107,
  0.0013171501024742112,
  0.000788783358847752,
  0.0005498425886621068],
'67':[0.13598930783101504,
  0.04717783658889547,
  0.027547125931827038,
  0.021440839841617088,
  0.016775671441391703,
  0.013185864754748117,
  0.010318462898609907],

and so on...

if I run this it works perfectly:

curve = sse['67']
nPoints = len(curve)
allCoord = np.vstack((range(nPoints), curve)).T
np.array([range(nPoints), curve])
firstPoint = allCoord[0]
lineVec = allCoord[-1] - allCoord[0]
lineVecNorm = lineVec / np.sqrt(np.sum(lineVec**2))
vecFromFirst = allCoord - firstPoint
scalarProduct = np.sum(vecFromFirst * np.matlib.repmat(lineVecNorm, nPoints, 1), axis=1)
vecFromFirstParallel = np.outer(scalarProduct, lineVecNorm)
vecToLine = vecFromFirst - vecFromFirstParallel
distToLine = np.sqrt(np.sum(vecToLine ** 2, axis=1))
idxOfBestPoint = np.argmax(distToLine)

print(idxOfBestPoint)

but when I try to make it a looping function I get weird values that do not match when ran by itself:

for k in sse:
    curve = sse[str(k)]
    nPoints = len(curve)
    allCoord = np.vstack((range(nPoints), curve)).T
    np.array([range(nPoints), curve])
    firstPoint = allCoord[0]
    lineVeceVeceVec = allCoord[-1] - allCoord[0]
    lineVecNorm = lineVec / np.sqrt(np.sum(lineVec**2))
    vecFromFirst = allCoord - firstPoint
    scalarProduct = np.sum(vecFromFirst * np.matlib.repmat(lineVecNorm, nPoints, 1), axis=1)
    vecFromFirstParallel = np.outer(scalarProduct, lineVecNorm)
    vecToLine = vecFromFirst - vecFromFirstParallel
    distToLine = np.sqrt(np.sum(vecToLine ** 2, axis=1))
    idxOfBestPoint = np.argmax(distToLine)
    print(idxOfBestPoint, k)

It will spit out something like this:

7 57
98 11
6 45
4 50
98 91
98 00
1 62
98 79
7 48
98 12
98 38
98 23
5 37
98 56
98 67
5 25
7 46
98 22
98 49
2 47
98 41
98 78
98 35
98 68
98 14
98 24
1 0
98 42

I can't tell if there is some variable not resetting or what would cause it to fail by adding a simple loop?

To be clear it does run, but the calc is putting out '98' as the elbow on the loop, but when ran separately by itself it will be 7 for the '67' curve list, not 98.

user3486773
  • 1,174
  • 3
  • 25
  • 50
  • 1
    Can you show what the expected output is supposed to be? – lc74 Jun 17 '19 at 22:11
  • They should all be numbers less than 10. I'm trying to automatically 'find the elbow' and when all these are ran by themselves it's less than 10. it's just a large dataset so I'm not sure how to provide the exact set. I can save the dictionary to file and upload it somewhere if that helps. Just not sure where. – user3486773 Jun 17 '19 at 22:14

1 Answers1

0

You have a typo: lineVeceVeceVec = allCoord[-1] - allCoord[0]. It should be lineVec = allCoord[-1] - allCoord[0]. So what you were spitting out was the last time lineVec was assigned to a result and that last value was probably from when you ran the non-looped version.

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • Ah yes thank you! I looked at it a dozen times and just didn't catch it. My excuse is that it's Monday :) – user3486773 Jun 17 '19 at 22:37
  • @user3486773 :D. No problem. I managed to catch it by using a diff-tool. https://www.diffchecker.com/. I put in the non-looped version and looped version, skipping the initial assignment in the loop and that was the only line that was different. – rayryeng Jun 17 '19 at 22:38