compareRegPerfOPSInt.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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-000047_Standardized',
  20. 'Trh-F-000047.CNG',
  21. 'Trh-M-000143.CNG',
  22. 'Trh-F-000092.CNG',
  23. 'Trh-F-700009.CNG',
  24. 'Trh-M-000013.CNG',
  25. 'Trh-M-000146.CNG',
  26. 'Trh-M-100009.CNG',
  27. 'Trh-F-000019.CNG',
  28. 'Trh-M-000081.CNG',
  29. 'Trh-M-900003.CNG',
  30. 'Trh-F-200035.CNG',
  31. 'Trh-F-200015.CNG',
  32. 'Trh-M-000040.CNG',
  33. 'Trh-M-600023.CNG',
  34. 'Trh-M-100048.CNG',
  35. 'Trh-M-700019.CNG',
  36. 'Trh-F-100009.CNG',
  37. 'Trh-M-400000.CNG',
  38. 'Trh-M-000067.CNG',
  39. 'Trh-M-000114.CNG',
  40. 'Trh-M-100018.CNG',
  41. 'Trh-M-000141.CNG',
  42. 'Trh-M-900019.CNG',
  43. 'Trh-M-800002.CNG'
  44. ]
  45. case1 = {'resDirs': {
  46. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangOPSInt"),
  47. # "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangOPSInt"),
  48. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangOPSInt"),
  49. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangOPSInt"),
  50. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangOPSInt"),
  51. "Standardized": os.path.join(homeFolder, "Registered", "chiangOPSInt",),
  52. },
  53. 'initRef': "Trh-F-000047\n-Standardized",
  54. 'expNameLambdas': {
  55. "PCA": lambda x: x,
  56. # "blastneuron": lambda x: x,
  57. "PCA + RobartsICP": lambda x: x,
  58. "Reg-MaxS": lambda x: x,
  59. "Reg-MaxS-N": lambda x: x,
  60. "Standardized": standardizedExpNameLambda}
  61. }
  62. case2 = {'resDirs': {
  63. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangOPSInt"),
  64. # "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangOPSInt_Trh-F-000047.CNG"),
  65. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangOPSInt_Trh-F-000047.CNG"),
  66. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangOPSInt_Trh-F-000047.CNG"),
  67. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangOPSInt_Trh-F-000047.CNG"),
  68. "Standardized": os.path.join(homeFolder, "Registered", "chiangOPSInt",),
  69. },
  70. 'initRef': "Trh-F-000047",
  71. 'expNameLambdas': {
  72. "PCA": lambda x: x,
  73. # "blastneuron": lambda x: x,
  74. "PCA + RobartsICP": lambda x: x,
  75. "Reg-MaxS": lambda x: x,
  76. "Reg-MaxS-N": lambda x: x,
  77. "Standardized": standardizedExpNameLambda}
  78. }
  79. case3 = {'resDirs': {
  80. "PCA": os.path.join(homeFolder, "PCA-Based", "chiangOPSInt_Trh-M-900003.CNG"),
  81. # "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangOPSInt_Trh-M-900003.CNG"),
  82. "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangOPSInt_Trh-M-900003.CNG"),
  83. "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangOPSInt_Trh-M-900003.CNG"),
  84. "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangOPSInt_Trh-M-900003.CNG"),
  85. "Standardized": os.path.join(homeFolder, "Registered", "chiangOPSInt",),
  86. },
  87. 'initRef': "Trh-M-900003",
  88. 'expNameLambdas': {
  89. "PCA": lambda x:x,
  90. # "blastneuron": lambda x:x,
  91. "PCA + RobartsICP": lambda x:x,
  92. "Reg-MaxS": lambda x:x,
  93. "Reg-MaxS-N": lambda x:x,
  94. "Standardized": standardizedExpNameLambda}
  95. }
  96. # case4 = {'resDirs': {
  97. # "PCA": os.path.join(homeFolder, "PCA-Based", "chiangOPSInt_Trh-M-100018.CNG"),
  98. # "blastneuron": os.path.join(homeFolder, "BlastNeuron", "chiangOPSInt_Trh-M-100018.CNG"),
  99. # "PCA + RobartsICP": os.path.join(homeFolder, "RobartsICP", "chiangOPSInt_Trh-M-100018.CNG"),
  100. # "Reg-MaxS": os.path.join(homeFolder, "Reg-MaxS", "chiangOPSInt_Trh-M-100018.CNG"),
  101. # "Reg-MaxS-N": os.path.join(homeFolder, "Reg-MaxS-N", "chiangOPSInt_Trh-M-100018.CNG"),
  102. # "Standardized": os.path.join(homeFolder, "Registered", "chiangOPSInt",),
  103. # },
  104. # 'initRef': "Trh-M-100018",
  105. # 'expNameLambdas': {
  106. # "PCA": lambda x:x,
  107. # "blastneuron": lambda x:x,
  108. # "PCA + RobartsICP": lambda x:x,
  109. # "Reg-MaxS": lambda x:x,
  110. # "Reg-MaxS-N": lambda x:x,
  111. # "Standardized": standardizedExpNameLambda}
  112. # }
  113. # cases = [case1, case2, case3, case4]
  114. cases = [case1, case2, case3]
  115. voxelSize = 10
  116. def saveData(outXLFile):
  117. metricsDF = pd.DataFrame()
  118. maxDistStatsDF = pd.DataFrame()
  119. runTimesDF = pd.DataFrame()
  120. for case in cases:
  121. resDirs = case["resDirs"]
  122. initRef = case["initRef"]
  123. expNameLambdas = case['expNameLambdas']
  124. for (resDirLabel, resDir) in resDirs.iteritems():
  125. outFiles = []
  126. expNameLambda = expNameLambdas[resDirLabel]
  127. for expName in expNames:
  128. outFile = os.path.join(resDir, "{}.swc".format(expNameLambda(expName)))
  129. if os.path.isfile(outFile):
  130. outFiles.append(outFile)
  131. else:
  132. print("{} not found. Ignoring it.".format(outFile))
  133. if outFiles:
  134. print("Collecting data for resDirLabel={}, initRef={}".format(resDirLabel, initRef))
  135. metric = occupancyEMD(outFiles, voxelSize)
  136. if resDirLabel == "Reg-MaxS-N":
  137. finalRef = os.path.join(resDir, "finalRef.swc")
  138. initialRef = os.path.join(resDir, "ref-1.swc")
  139. runtime = os.stat(finalRef).st_mtime - os.stat(initialRef).st_mtime
  140. else:
  141. outFileModTimes = [os.stat(outFile).st_mtime for outFile in outFiles]
  142. outFileModTimesSorted = sorted(outFileModTimes)
  143. nFiles = float(len(outFiles))
  144. runtime = (outFileModTimesSorted[-1] - outFileModTimesSorted[0]) * nFiles / (nFiles - 1)
  145. tempDict = {"Initial Reference": initRef,
  146. "Occupancy Based Dissimilarity Measure": metric,
  147. "Total runtime (s)": runtime,
  148. "Method": resDirLabel}
  149. metricsDF = metricsDF.append(tempDict, ignore_index=True)
  150. # crdMaxDistsStatsDFFull = maxDistStats(outFiles)
  151. # aggDictRename = {"mean": "mean of \nmaximum distances",
  152. # "std": "standard deviation of \nmaximum distances"}
  153. # crdMaxDistsStatsDF = crdMaxDistsStatsDFFull.loc[:, ("maximum distance", "source point")]
  154. # crdMaxDistsStatsDF.set_index("source point", inplace=True)
  155. # crdMaxDistMeanStd = crdMaxDistsStatsDF.groupby("source point")["maximum distance"]\
  156. # .aggregate([np.mean, np.std])
  157. # crdMaxDistMeanStd = crdMaxDistMeanStd.rename(columns=aggDictRename)
  158. # tempDF = crdMaxDistMeanStd.reset_index()
  159. # tempDF['Initial Reference'] = initRef
  160. # tempDF['Method'] = resDirLabel
  161. # maxDistStatsDF = maxDistStatsDF.append(tempDF, ignore_index=True)
  162. else:
  163. print("No usable SWCs found in {}".format(resDir))
  164. metricsDF.to_excel(outXLFile)
  165. def plotData(inFile):
  166. [darkblue, green, red, violet, yellow, lightblue] = sns.color_palette()
  167. metricsDF = pd.read_excel(inFile)
  168. fig1, ax1 = plt.subplots(figsize=(14, 11.2))
  169. sns.barplot(data=metricsDF, x="Initial Reference",
  170. y="Occupancy Based Dissimilarity Measure", hue="Method",
  171. ax=ax1, hue_order=["PCA", "PCA + RobartsICP", "BlastNeuron",
  172. "Reg-MaxS", "Reg-MaxS-N", "Standardized"],
  173. palette=[red, violet, yellow, lightblue, darkblue, green])
  174. ax1.legend(loc='best', ncol=3)
  175. ax1.set_ylabel("Occupancy Based Dissimilarity Measure")
  176. temp = ax1.get_ylim()
  177. ax1.set_ylim(temp[0], temp[1] + 2)
  178. # fig2, ax2 = plt.subplots(figsize=(14, 11.2))
  179. # sns.boxplot(data=maxDistStatsDF, x="Initial Reference", y="mean of \nmaximum distances",
  180. # hue="Method", whis=np.inf,
  181. # ax=ax2, hue_order=["PCA", "blastneuron","PCA + RobartsICP",
  182. # "Reg-MaxS", "Reg-MaxS-N", "Standardized"])
  183. # ax2.legend(loc="best", ncol=3)
  184. #
  185. # fig3, ax3 = plt.subplots(figsize=(14, 11.2))
  186. # sns.boxplot(data=maxDistStatsDF, x="Initial Reference", y="standard deviation of \nmaximum distances",
  187. # hue="Method", whis=np.inf,
  188. # ax=ax3, hue_order=["PCA", "blastneuron","PCA + RobartsICP",
  189. # "Reg-MaxS", "Reg-MaxS-N", "Standardized"])
  190. # ax3.legend(loc="best", ncol=3)
  191. # for fig in [fig1, fig2, fig3]:
  192. for fig in [fig1]:
  193. fig.tight_layout()
  194. return fig1
  195. if __name__ == "__main__":
  196. assert len(sys.argv) == 3, "Improper Usage! Please use as:\n" \
  197. "python {fName} save <outFile> or python {fName} plot <inFile>".format(fName=sys.argv[0])
  198. if sys.argv[1] == "save":
  199. saveData(sys.argv[2])
  200. elif sys.argv[1] == "plot":
  201. fig = plotData(sys.argv[2])
  202. else:
  203. raise(ValueError("Improper Usage! Please use as:\n"
  204. "python {fName} save <outFile> or python {fName} plot <inFile>".format(fName=sys.argv[0])))