rotProfile2D.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. from regmaxsn.core.SWCTransforms import SWCRotate, ArgGenIterator, objFun
  2. import multiprocessing as mp
  3. import numpy as np
  4. from itertools import product
  5. import json
  6. from matplotlib import pyplot as plt
  7. import seaborn as sns
  8. plt.ion()
  9. import os
  10. from regmaxsn.core.matplotlibRCParams import mplPars
  11. sns.set(rc=mplPars)
  12. # ----------------------------------------------------------------------------------------------------------------------
  13. homeFolder = "/home/aj"
  14. dirPath = os.path.join(homeFolder,'DataAndResults/morphology/OriginalData/borstHSN2D/')
  15. #
  16. expNames = [
  17. 'HSN-fluoro01.CNG2D',
  18. 'HSN-fluoro01.CNG2DRandRot',
  19. # 'HSN-fluoro01.CNG2DRandRot1',
  20. # 'HSN-fluoro01.CNG2DRandRot2',
  21. # 'HSN-fluoro01.CNG2DRandRot3',
  22. # 'HSN-fluoro01.CNG2DRandRot4',
  23. # 'HSN-fluoro01.CNG2DRandRot5',
  24. # 'HSN-fluoro01.CNGNoiseStd42DRandRot',
  25. # 'HSN-fluoro01.CNGNoiseStd42DRandRot1',
  26. # 'HSN-fluoro01.CNGNoiseStd42DRandRot2',
  27. # 'HSN-fluoro01.CNGNoiseStd42DRandRot3',
  28. # 'HSN-fluoro01.CNGNoiseStd42DRandRot4',
  29. # 'HSN-fluoro01.CNGNoiseStd42DRandRot5',
  30. # 'HSN-fluoro02.CNG2D',
  31. # 'HSN-fluoro03.CNG',
  32. # 'HSN-fluoro04.CNG',
  33. # 'HSN-fluoro05.CNG2D',
  34. # 'HSN-fluoro06.CNG',
  35. # 'HSN-fluoro07.CNG',
  36. # 'HSN-fluoro08.CNG',
  37. # 'HSN-fluoro09.CNG',
  38. # 'HSN-fluoro10.CNG',
  39. ]
  40. refInd = 0
  41. resDir = os.path.join(homeFolder,'DataAndResults/morphology/directPixelBased/rotProfile2D/')
  42. if not os.path.isdir(resDir):
  43. os.mkdir(resDir)
  44. # ----------------------------------------------------------------------------------------------------------------------
  45. # gridSizes = [40.0, 20.0, 10.0]
  46. gridSizes = [40.0]
  47. # gridSizes = [20.0]
  48. # gridSizes = [10.0]
  49. bounds = [[0, 0], [0, 0], [-np.pi / 6, np.pi / 6]]
  50. # bounds = [[0, 0], [-np.pi / 3, np.pi / 3], [-np.pi / 3, np.pi / 3]]
  51. rotMinRes = np.deg2rad(1).round(4)
  52. nCPU = 7
  53. refSWC = os.path.join(dirPath, expNames[refInd] + '.swc')
  54. SWC2Align = os.path.join(dirPath, expNames[1] + '.swc')
  55. SWCRotCenter = np.loadtxt(SWC2Align)[:, 2:5].mean(axis=0)
  56. data = np.loadtxt(SWC2Align)[:, 2:5] - SWCRotCenter
  57. maxDist = np.linalg.norm(data, axis=1).max()
  58. pool = mp.Pool(processes=nCPU)
  59. with sns.axes_style('whitegrid'):
  60. # fig, ax = plt.subplots(nrows=3, ncols=1, figsize=(10, 8))
  61. fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(7, 5.6))
  62. with open(os.path.join(dirPath, expNames[1] + '.json')) as fle:
  63. pars = json.load(fle)
  64. actualSol = -np.rad2deg(pars['angles'][2])
  65. SWCDatas = [SWCRotate(refSWC, SWC2Align, x) for x in gridSizes]
  66. noChangeVals = [objFun(([0, 0, 0], data)) for data in SWCDatas]
  67. stepSizes = [2 * x / maxDist for x in gridSizes]
  68. for gridInd, gridSize in enumerate(gridSizes):
  69. # print('Gridsize:' + str(gridSize))
  70. stepSize = stepSizes[gridInd]
  71. # print('Stepsize: ' + str(np.rad2deg(stepSize)))
  72. bounds = np.array(bounds)
  73. boundsRoundedUp = np.sign(bounds) * np.ceil(np.abs(bounds) / stepSize) * stepSize
  74. possiblePts1D = [np.round(np.arange(x[0], x[1] + 0.9 * stepSize, stepSize), 3).tolist() for x in boundsRoundedUp]
  75. possiblePts3D = np.round(list(product(*possiblePts1D)), 3).tolist()
  76. argGen = ArgGenIterator(possiblePts3D, SWCDatas[gridInd])
  77. funcVals = pool.map(objFun, argGen)
  78. outFile = os.path.join(resDir, expNames[refInd] + expNames[1] + str(int(gridSize)) + 'rot.json')
  79. with open(outFile, 'w') as fle:
  80. json.dump({'possiblePoints3D': possiblePts3D, 'funcVals': funcVals}, fle)
  81. with sns.axes_style('whitegrid'):
  82. # ax1 = ax[gridInd]
  83. ax1 = ax
  84. ax1.plot(np.rad2deg(possiblePts1D[2]), funcVals, 'b-o', lw=6, ms=12)
  85. ybounds = (0.1 * np.floor(min(funcVals)*10), 0.1 * np.ceil(max(funcVals)*10))
  86. ax1.set_ylim(ybounds)
  87. ax1.plot([actualSol] * 2, ybounds, 'r-', lw=6)
  88. ax1.set_ylabel(r'Dissimilarity')
  89. # ax1.set_title(r'GridSize=' + str(int(gridSize)) + '$\mu$m')
  90. if not gridInd:
  91. bestSol = possiblePts3D[np.argmin(funcVals)]
  92. else:
  93. minimum = min(funcVals)
  94. if minimum > noChangeVals[gridInd]:
  95. bestSol = [0, 0, 0]
  96. else:
  97. # if True:
  98. minimzers = [y for x, y in enumerate(possiblePts3D) if funcVals[x] == minimum]
  99. prevVals = [objFun((x, SWCDatas[gridInd - 1])) for x in minimzers]
  100. bestSol = minimzers[np.argmin(prevVals)]
  101. bestVal = objFun((bestSol, SWCDatas[gridInd]))
  102. print(bestSol)
  103. ax1.set_xlabel(r'rotation about Z in degrees')
  104. fig.tight_layout()
  105. fig.canvas.draw()