farthestPointStats.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from regmaxsn.core.swcFuncs import readSWC_numpy, resampleSWC
  2. from scipy.spatial import ConvexHull
  3. import pandas as pd
  4. from scipy.spatial.distance import cdist
  5. def maxDistStats(swcFiles):
  6. swcHullVerts = []
  7. swcDatas = []
  8. swcDataSeries =[]
  9. swcHullVertsSeries = []
  10. maxDistStatsDF = pd.DataFrame()
  11. for swcFile in swcFiles:
  12. totalLen, swcDataWithR = resampleSWC(swcFile, 0.5)
  13. swcData = swcDataWithR[:, :3]
  14. swcDatas.append(swcData)
  15. swcDataSeries.append(pd.Series([tuple(x) for x in swcData]))
  16. hull = ConvexHull(swcData)
  17. swcHullVert = swcData[hull.vertices, :]
  18. swcHullVerts.append(swcHullVert)
  19. swcHullVertsSeries.append(pd.Series([tuple(x) for x in swcHullVert]))
  20. for swcInd1, swcData1 in enumerate(swcDatas):
  21. swcInds = range(len(swcFiles))
  22. swcInds.remove(swcInd1)
  23. swcDataSeries1 = swcDataSeries[swcInd1]
  24. for swcInd2 in swcInds:
  25. swcHullVert2 = swcHullVerts[swcInd2]
  26. swcHullVertSeries2 = swcHullVertsSeries[swcInd2]
  27. distMat = cdist(swcData1, swcHullVert2)
  28. maxDistsInds = distMat.argmax(axis=1)
  29. tempDF = pd.DataFrame()
  30. tempDF["source point"] = swcDataSeries1
  31. tempDF["maximum distance"] = distMat.max(axis=1)
  32. tempDF["destination farthest point"] = swcHullVertSeries2[maxDistsInds].values
  33. tempDF["destination SWC"] = swcFiles[swcInd2]
  34. maxDistStatsDF = maxDistStatsDF.append(tempDF, ignore_index=True)
  35. return maxDistStatsDF