plotReg-MaxS-NIOUTraj.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import os
  2. import numpy as np
  3. from regmaxsn.core.misc import parFileCheck
  4. from matplotlib import pyplot as plt
  5. from regmaxsn.core.matplotlibRCParams import mplPars
  6. import seaborn as sns
  7. import sys
  8. from functools import reduce
  9. def plotIOUTraj(parFile, parNames):
  10. plt.ion()
  11. sns.set(rc=mplPars)
  12. parsList = parFileCheck(parFile, parNames)
  13. figs = []
  14. for parInd, pars in enumerate(parsList):
  15. gridSizes = pars['gridSizes']
  16. resDir = pars['resDir']
  17. swcList = pars['swcList']
  18. expNames = [os.path.split(swc)[1][:-4] for swc in swcList]
  19. iters = sorted([int(fle[3:-4]) for fle in os.listdir(resDir) if fle.find('ref') == 0])
  20. nIter = max(iters)
  21. cols = sns.color_palette('Set2', len(gridSizes))
  22. with sns.axes_style('darkgrid'):
  23. fig, axs = plt.subplots(nrows=3, ncols=1, figsize=(14, 11.2))
  24. fig.suptitle('Job #{}'.format(parInd + 1))
  25. ax0, ax1, ax2 = axs
  26. figs.append(fig)
  27. for gridInd, gridSize in enumerate(gridSizes):
  28. print(('Doing gridSize = {} of {}'.format(gridSize, gridSizes)))
  29. nInts = []
  30. nUnions = []
  31. nIOUs = []
  32. for iterInd in range(nIter + 1):
  33. print(('Doing {}/{}'.format(iterInd + 1, nIter + 1)))
  34. alignedSWCs = [os.path.join(resDir, '{}{}.swc'.format(expName, iterInd)) for expName in expNames]
  35. indVoxs = []
  36. for aswc in alignedSWCs:
  37. aPts = np.loadtxt(aswc)[:, 2:5]
  38. aVox = np.array(aPts / gridSize, np.int32)
  39. aVoxSet = set(map(tuple, aVox))
  40. indVoxs.append(aVoxSet)
  41. aUnion = reduce(lambda x, y: x.union(y), indVoxs)
  42. aInt = reduce(lambda x, y: x.intersection(y), indVoxs)
  43. nInt = len(aInt)
  44. nUnion = len(aUnion)
  45. nIOU = 1 - nInt / float(nUnion)
  46. nInts.append(nInt)
  47. nUnions.append(nUnion)
  48. nIOUs.append(nIOU)
  49. with sns.axes_style('darkgrid'):
  50. ax0.plot(list(range(nIter + 1)), nInts, color=cols[gridInd], marker='o', ls='-', label=str(gridSize))
  51. ax0.set_xlim(-1, nIter + 2)
  52. ax0.set_xlabel('Iteration Number')
  53. ax0.set_ylabel('n(Intersection) (nI)')
  54. ax1.plot(list(range(nIter + 1)), nUnions, color=cols[gridInd], marker='o', ls='-', label=str(gridSize))
  55. ax1.set_xlim(-1, nIter + 2)
  56. ax1.set_xlabel('Iteration Number')
  57. ax1.set_ylabel('n(Union) (nU)')
  58. ax2.plot(list(range(nIter + 1)), nIOUs, color=cols[gridInd], marker='o', ls='-', label=str(gridSize))
  59. ax2.set_xlim(-1, nIter + 2)
  60. ax2.set_xlabel('Iteration Number')
  61. ax2.set_ylabel('1 - nI / nU')
  62. for fig in figs:
  63. fig.axes[0].legend()
  64. # fig.tight_layout()
  65. return figs
  66. if __name__ == '__main__':
  67. from regmaxsn.core.RegMaxSPars import RegMaxSNParNames
  68. assert len(sys.argv) == 2, 'Improper usage! Please use as \'python plotReg-MaxS-NIOUTraj.py parFile\''
  69. parFile = sys.argv[1]
  70. figs = plotIOUTraj(parFile, RegMaxSNParNames)
  71. input('Press any key to close figures and quit:')