plotReg-MaxS-NIOUTraj.py 3.2 KB

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