plotCHBasedMetricVsAverageRotScaleTransform.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import pandas as pd
  2. from regmaxsn.core.maxDistanceBasedMetric import maxDistEMD
  3. import pathlib2
  4. import json
  5. import numpy as np
  6. import itertools
  7. from matplotlib import pyplot as plt
  8. import seaborn as sns
  9. from regmaxsn.core.matplotlibRCParams import mplPars
  10. import sys
  11. dirPath = pathlib2.Path("/media/ajay/ADATA_HD720/Ginjang/DataAndResults/morphology/OriginalData/Tests")
  12. expName = 'HSN-fluoro01.CNG'
  13. swcSetSize = 20
  14. N = 500
  15. resDir = pathlib2.Path("/media/ajay/ADATA_HD720/Ginjang/DataAndResults/morphology/CHBasedMetricTests")
  16. scaleDF = pd.DataFrame()
  17. rotDF = pd.DataFrame()
  18. transDF = pd.DataFrame()
  19. suffixes = ("RandRotY", "RandScaleY", "RandTranslateY")
  20. dfs = [rotDF, scaleDF, transDF]
  21. labels = ("Rotation (degrees)", "Scale", "Translation (um)")
  22. jsonKeys = ("angles", "scale", "translation")
  23. parInds = (1, 1, 1)
  24. figs = []
  25. def saveData():
  26. for suffixInd, suffix in enumerate(suffixes):
  27. print("Doing {}".format(labels[suffixInd]))
  28. parSWCDict = {}
  29. for ind in range(N):
  30. label = labels[suffixInd]
  31. jsonKey = jsonKeys[suffixInd]
  32. parInd = parInds[suffixInd]
  33. outFile = str(dirPath / '{}{}{}.swc'.format(expName, suffix, ind))
  34. transJSONFile = str(dirPath / '{}{}{}.json'.format(expName, suffix, ind))
  35. with open(transJSONFile, "r") as fle:
  36. transJSON = json.load(fle)
  37. jsonPars = transJSON[jsonKey]
  38. parSWCDict[jsonPars[1]] = outFile
  39. allPars = parSWCDict.keys()
  40. allParsSorted = np.sort(allPars)
  41. maxStepSize = int(np.floor(float(N) / float(swcSetSize)))
  42. baseSet = np.arange(0, swcSetSize)
  43. for stepSize in range(1, maxStepSize + 1):
  44. print("Doing StepSize {}/{}".format(stepSize, maxStepSize))
  45. windowSlideSize = int(stepSize * swcSetSize / 2)
  46. windowStarts = range(0, N - stepSize * swcSetSize + 1, windowSlideSize)
  47. for windowStart in windowStarts:
  48. print("Doing Window start {}/{}".format(windowStart, windowStarts))
  49. pars = allParsSorted[windowStart + stepSize * baseSet]
  50. swcFiles = [parSWCDict[par] for par in pars]
  51. metric = maxDistEMD(swcFiles)
  52. if suffix == "RandRotY":
  53. pars = np.rad2deg(pars)
  54. tempDict = {"mean of {}".format(label): np.mean(pars),
  55. "std of {}".format(label): np.std(pars),
  56. "metric": metric}
  57. dfs[suffixInd] = dfs[suffixInd].append(tempDict, ignore_index=True)
  58. outFile = str(resDir / "metricVs{}.xlsx".format(suffix))
  59. dfs[suffixInd].to_excel(outFile)
  60. def plotData():
  61. sns.set(rc=mplPars)
  62. figs = []
  63. for suffixInd, suffix in enumerate(suffixes):
  64. label = labels[suffixInd]
  65. dfXL = str(resDir / "metricVs{}.xlsx".format(suffix))
  66. df = pd.read_excel(dfXL)
  67. df["mean of {}".format(label)] = df["mean of {}".format(label)].apply(lambda x: round(x, 3))
  68. df["std of {}".format(label)] = df["std of {}".format(label)].apply(lambda x: round(x, 3))
  69. df2Plot = df.pivot(index="mean of {}".format(label),
  70. columns="std of {}".format(label),
  71. values="metric")
  72. fig, ax = plt.subplots(figsize=(14, 11.2))
  73. sns.heatmap(data=df2Plot, ax=ax)
  74. ax.set_xticklabels(ax.get_xticklabels(), rotation="vertical")
  75. ax.set_yticklabels(ax.get_yticklabels(), rotation="horizontal")
  76. fig.tight_layout()
  77. outFile = str(resDir / "metricVs{}.png".format(suffix))
  78. fig.savefig(outFile, dpi=150)
  79. figs.append(fig)
  80. if __name__ == "__main__":
  81. assert len(sys.argv) == 2, 'Improper usage! Please use as \'python {arg} save\' or' \
  82. '\'python {arg} plot'.format(arg=sys.argv[0])
  83. if sys.argv[1] == "save":
  84. saveData()
  85. elif sys.argv[1] == "plot":
  86. plotData()
  87. else:
  88. raise ValueError