compareRegPerfRAL_min5.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. import os
  2. import pandas as pd
  3. import seaborn as sns
  4. from matplotlib import pyplot as plt
  5. from regmaxsn.core.matplotlibRCParams import mplPars
  6. from regmaxsn.core.occupancyBasedMeasure import occupancyEMD
  7. from regmaxsn.core.farthestPointStats import maxDistStats
  8. import numpy as np
  9. import sys
  10. plt.ion()
  11. sns.set(rc=mplPars)
  12. def standardizedExpNameLambda(x):
  13. if x.endswith("_Standardized"):
  14. return x[:-len("_Standardized")]
  15. else:
  16. return x
  17. homeFolder = "/home/aj/DataAndResults/morphology/"
  18. expNames = [
  19. "Trh-F-700063_Standardized",
  20. "Trh-F-700063.CNG",
  21. "Trh-F-500050.CNG",
  22. "Trh-F-500106.CNG",
  23. "Trh-M-500051.CNG",
  24. "Trh-F-600071.CNG",
  25. "Trh-F-500093.CNG",
  26. "Trh-F-500148.CNG",
  27. "Trh-F-500154.CNG",
  28. "Trh-F-700018.CNG",
  29. ]
  30. case1 = {'resDirs': {
  31. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangRAL"),
  32. "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangRAL"),
  33. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangRAL"),
  34. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangRAL"),
  35. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangRAL_min5"),
  36. "Standardized": os.path.join(homeFolder, "Registered", "chiangRAL",),
  37. },
  38. 'initRef': "Trh-F-700063-Standardized",
  39. 'expNameLambdas': {
  40. "PCA": lambda x: x,
  41. "blastneuron": lambda x: x,
  42. "PCA + RobartsICP": lambda x: x,
  43. "Reg-MaxS": lambda x: x,
  44. "Reg-MaxS-N": lambda x: x,
  45. "Standardized": standardizedExpNameLambda}
  46. }
  47. case2 = {'resDirs': {
  48. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangRAL_Trh-F-700063.CNG"),
  49. "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangRAL_Trh-F-700063.CNG"),
  50. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangRAL_Trh-F-700063.CNG"),
  51. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangRAL_Trh-F-700063.CNG"),
  52. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangRAL_Trh-F-700063.CNG_min5"),
  53. "Standardized": os.path.join(homeFolder, "Registered", "chiangRAL"),
  54. },
  55. 'initRef': "Trh-F-700063",
  56. 'expNameLambdas': {
  57. "PCA": lambda x: x,
  58. "blastneuron": lambda x: x,
  59. "PCA + RobartsICP": lambda x: x,
  60. "Reg-MaxS": lambda x: x,
  61. "Reg-MaxS-N": lambda x: x,
  62. "Standardized": standardizedExpNameLambda}
  63. }
  64. case3 = {'resDirs': {
  65. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangRAL_Trh-F-700018.CNG"),
  66. "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangRAL_Trh-F-700018.CNG"),
  67. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangRAL_Trh-F-700018.CNG"),
  68. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangRAL_Trh-F-700018.CNG"),
  69. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangRAL_Trh-F-700018.CNG_min5"),
  70. "Standardized": os.path.join(homeFolder, "Registered", "chiangRAL"),
  71. },
  72. 'initRef': "Trh-F-700018",
  73. 'expNameLambdas': {
  74. "PCA": lambda x: x,
  75. "blastneuron": lambda x: x,
  76. "PCA + RobartsICP": lambda x: x,
  77. "Reg-MaxS": lambda x: x,
  78. "Reg-MaxS-N": lambda x: x,
  79. "Standardized": standardizedExpNameLambda}
  80. }
  81. case4 = {'resDirs': {
  82. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangRAL_Trh-M-500051.CNG"),
  83. "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangRAL_Trh-M-500051.CNG"),
  84. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangRAL_Trh-M-500051.CNG"),
  85. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangRAL_Trh-M-500051.CNG"),
  86. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangRAL_Trh-M-500051.CNG_min5"),
  87. "Standardized": os.path.join(homeFolder, "Registered", "chiangRAL"),
  88. },
  89. 'initRef': "Trh-M-500051",
  90. 'expNameLambdas': {
  91. "PCA": lambda x: x,
  92. "blastneuron": lambda x: x,
  93. "PCA + RobartsICP": lambda x: x,
  94. "Reg-MaxS": lambda x: x,
  95. "Reg-MaxS-N": lambda x: x,
  96. "Standardized": standardizedExpNameLambda}
  97. }
  98. cases = [case1, case2, case3, case4]
  99. voxelSize = 10
  100. def saveData(outXLFile):
  101. metricsDF = pd.DataFrame()
  102. maxDistStatsDF = pd.DataFrame()
  103. for case in cases:
  104. resDirs = case["resDirs"]
  105. initRef = case["initRef"]
  106. expNameLambdas = case['expNameLambdas']
  107. for (resDirLabel, resDir) in resDirs.iteritems():
  108. outFiles = []
  109. expNameLambda = expNameLambdas[resDirLabel]
  110. for expName in expNames:
  111. outFile = os.path.join(resDir, "{}.swc".format(expNameLambda(expName)))
  112. if os.path.isfile(outFile):
  113. outFiles.append(outFile)
  114. else:
  115. print("{} not found. Ignoring it.".format(outFile))
  116. if outFiles:
  117. print("Collecting data for resDirLabel={}, initRef={}".format(resDirLabel, initRef))
  118. metric = occupancyEMD(outFiles, voxelSize)
  119. tempDict = {"Initial Reference": initRef,
  120. "Occupancy Based Dissimilarity Measure": metric,
  121. "Method": resDirLabel}
  122. metricsDF = metricsDF.append(tempDict, ignore_index=True)
  123. # crdMaxDistsStatsDFFull = maxDistStats(outFiles)
  124. # aggDictRename = {"mean": "mean of \nmaximum distances",
  125. # "std": "standard deviation of \nmaximum distances"}
  126. # crdMaxDistsStatsDF = crdMaxDistsStatsDFFull.loc[:, ("maximum distance", "source point")]
  127. # crdMaxDistsStatsDF.set_index("source point", inplace=True)
  128. # crdMaxDistMeanStd = crdMaxDistsStatsDF.groupby("source point")["maximum distance"]\
  129. # .aggregate([np.mean, np.std])
  130. # crdMaxDistMeanStd = crdMaxDistMeanStd.rename(columns=aggDictRename)
  131. # tempDF = crdMaxDistMeanStd.reset_index()
  132. # tempDF['Initial Reference'] = initRef
  133. # tempDF['Method'] = resDirLabel
  134. # maxDistStatsDF = maxDistStatsDF.append(tempDF, ignore_index=True)
  135. else:
  136. print("No usable SWCs found in {}".format(resDir))
  137. metricsDF.to_excel(outXLFile)
  138. def plotData(inFile):
  139. metricsDF = pd.read_excel(inFile)
  140. fig1, ax1 = plt.subplots(figsize=(14, 11.2))
  141. sns.barplot(data=metricsDF, x="Initial Reference",
  142. y="Occupancy Based Dissimilarity Measure", hue="Method",
  143. ax=ax1, hue_order=["PCA", "blastneuron","PCA + RobartsICP",
  144. "Reg-MaxS", "Reg-MaxS-N", "Standardized"])
  145. ax1.legend(loc='best', ncol=3)
  146. ax1.set_ylabel("Occupancy Based Dissimilarity Measure")
  147. # fig2, ax2 = plt.subplots(figsize=(14, 11.2))
  148. # sns.boxplot(data=maxDistStatsDF, x="Initial Reference", y="mean of \nmaximum distances",
  149. # hue="Method", whis=np.inf,
  150. # ax=ax2, hue_order=["PCA", "blastneuron","PCA + RobartsICP",
  151. # "Reg-MaxS", "Reg-MaxS-N", "Standardized"])
  152. # ax2.legend(loc="best", ncol=3)
  153. #
  154. # fig3, ax3 = plt.subplots(figsize=(14, 11.2))
  155. # sns.boxplot(data=maxDistStatsDF, x="Initial Reference", y="standard deviation of \nmaximum distances",
  156. # hue="Method", whis=np.inf,
  157. # ax=ax3, hue_order=["PCA", "blastneuron","PCA + RobartsICP",
  158. # "Reg-MaxS", "Reg-MaxS-N", "Standardized"])
  159. # ax3.legend(loc="best", ncol=3)
  160. # for fig in [fig1, fig2, fig3]:
  161. for fig in [fig1]:
  162. fig.tight_layout()
  163. return fig1
  164. if __name__ == "__main__":
  165. assert len(sys.argv) == 3, "Improper Usage! Please use as:\n" \
  166. "python {fName} save <outFile> or python {fName} plot <inFile>".format(fName=sys.argv[0])
  167. if sys.argv[1] == "save":
  168. saveData(sys.argv[2])
  169. elif sys.argv[1] == "plot":
  170. fig = plotData(sys.argv[2])
  171. else:
  172. raise(ValueError("Improper Usage! Please use as:\n"
  173. "python {fName} save <outFile> or python {fName} plot <inFile>".format(fName=sys.argv[0])))