pairwiseDistanceStatsNN.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import os
  2. import json
  3. import numpy as np
  4. import pandas as pd
  5. from statsmodels.sandbox.descstats import sign_test
  6. from scipy.spatial import cKDTree
  7. from multiprocessing import cpu_count
  8. homeFolder = os.path.expanduser('~')
  9. def pairwiseDistanceStats(parFile):
  10. with open(parFile) as fle:
  11. parsList = json.load(fle)
  12. transErrs = pd.DataFrame()
  13. signCloserThanSmalledVoxelSizeDF = pd.DataFrame()
  14. allSizes = []
  15. allThreshs = []
  16. passCount = 0
  17. passCountNGT = 0
  18. for parInd, par in enumerate(parsList):
  19. refSWC = par['refSWC']
  20. resFile = par['resFile']
  21. testName = resFile[:-4]
  22. thresh = par['gridSizes'][-1]
  23. print('Doing ' + repr((refSWC, resFile)))
  24. refPts = np.loadtxt(refSWC)[:, 2:5]
  25. testPtsFull = np.loadtxt(resFile)
  26. testPts = testPtsFull[:, 2:5]
  27. refKDTree = cKDTree(refPts, compact_nodes=True, leafsize=100)
  28. minDists = refKDTree.query(testPts, n_jobs=cpu_count() - 1)[0]
  29. minDists[minDists == np.inf] = 1000
  30. transErrs = transErrs.append(pd.DataFrame({'Pairwise Distance in $\mu$m': minDists,
  31. 'Exp. Name': testName,
  32. "Node ID": testPtsFull[:, 0]}),
  33. ignore_index=True)
  34. t, p = sign_test(minDists, thresh)
  35. print(minDists.shape)
  36. oneSidedP = 0.5 * p
  37. signCloserThanSmallestVoxelSize = t < 0 and oneSidedP < 0.01
  38. if signCloserThanSmallestVoxelSize:
  39. passCount += 1
  40. notSignFartherThanSVS = not (t > 0 and oneSidedP < 0.01)
  41. if notSignFartherThanSVS:
  42. passCountNGT += 1
  43. tempDict = {"Job Number": parInd, "resFile": resFile, "refSWC": refSWC,
  44. "t Statistic": t, "One Sided p value": oneSidedP,
  45. "Pairwise distance significantly smaller than than {}".format(thresh):
  46. signCloserThanSmallestVoxelSize}
  47. signCloserThanSmalledVoxelSizeDF = signCloserThanSmalledVoxelSizeDF.append(tempDict,
  48. ignore_index=True)
  49. print("Jobs with "
  50. "pairwise distance "
  51. "significantly smaller "
  52. "than lowest voxel size: {} out of {}".format(passCount, len(parsList)))
  53. print("Jobs with "
  54. "pairwise distance "
  55. "not significantly greater "
  56. "than lowest voxel size: {} out of {}".format(passCountNGT, len(parsList)))
  57. allEqualSizeThresh = (allSizes.count(allSizes[0]) == len(allSizes)) and \
  58. (allThreshs.count(allThreshs[0]) == len(allThreshs))
  59. nodeWiseSignCloserThanSmallestVoxelSize = []
  60. nodeWiseNotSignLargerThanSmallestVoxelSize = []
  61. if allEqualSizeThresh:
  62. transErrsGBNodeID = transErrs.groupby("Node ID")
  63. for nodeInd, (node, nodeDistsDF) in enumerate(transErrsGBNodeID):
  64. print("Doing node {}, {} of {}".format(node, nodeInd, len(transErrsGBNodeID.indices)))
  65. t, p = sign_test(nodeDistsDF['Pairwise Distance in $\mu$m'].astype(float), allThreshs[0])
  66. oneSidedP = 0.5 * p
  67. signCloserThanSmallestVoxelSize = t < 0 and oneSidedP < 0.01
  68. nodeWiseSignCloserThanSmallestVoxelSize.append(signCloserThanSmallestVoxelSize)
  69. print("Jobs with "
  70. "pairwise distance "
  71. "significantly smaller "
  72. "than lowest voxel size: {} out of {}".format(passCount, len(parsList)))
  73. print("Jobs with "
  74. "pairwise distance "
  75. "not significantly larger "
  76. "than lowest voxel size: {} out of {}".format(sum(nodeWiseNotSignLargerThanSmallestVoxelSize), len(parsList)))
  77. print("Nodes with pairwise distance "
  78. "significantly smaller "
  79. "than lowest voxel size: {} out of {}".format(sum(nodeWiseSignCloserThanSmallestVoxelSize),
  80. len(nodeWiseSignCloserThanSmallestVoxelSize)))
  81. # ----------------------------------------------------------------------------------------------------------------------
  82. if __name__ == '__main__':
  83. import sys
  84. assert len(sys.argv) == 2, 'Improper usage! Please use as \'python plotPairwiseDistance.py parFile\''
  85. parFile = sys.argv[1]
  86. figs = pairwiseDistanceStats(parFile)