pairwiseDistanceStats.py 4.3 KB

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