projectToRef.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import sys
  2. from regmaxsn.core.swcFuncs import getPCADetails, writeSWC_numpy
  3. from regmaxsn.core.RegMaxSPars import DensitySaveParNames
  4. from regmaxsn.core.misc import parFileCheck
  5. import numpy as np
  6. import os
  7. def projectToRef(parFile, sourceRefSWC, targetRefSWC, outDir):
  8. if not os.path.isdir(outDir):
  9. os.makedirs(outDir)
  10. parsList = parFileCheck(parFile, DensitySaveParNames)
  11. refData = np.loadtxt(targetRefSWC)[:, 2:5]
  12. refEvecs, thrash1 = getPCADetails(None, center=True, data=refData)
  13. refMean = refData.mean(axis=0)
  14. refDataNoMean = refData - refMean
  15. intRefData = np.loadtxt(sourceRefSWC)[:, 2:5]
  16. intRefEvecs, thrash2 = getPCADetails(None, center=True, data=intRefData)
  17. intRefMean = intRefData.mean(axis=0)
  18. intRefDataNoMean = intRefData - intRefMean
  19. correlation = np.dot(intRefDataNoMean.T, refDataNoMean)
  20. u, s, v = np.linalg.svd(correlation)
  21. temp = np.dot(v, u.T)
  22. for pars in parsList:
  23. resFile = pars['resFile']
  24. resDataAll = np.loadtxt(resFile)
  25. resData = resDataAll[:, 2:5]
  26. resDataNoMean = resData - intRefMean
  27. # temp2 = np.dot(axesExchangeMat, intRefEvecs.T)
  28. # temp = np.dot(refEvecs, temp2)
  29. # temp1 = np.diag(np.sign(np.diag(temp)))
  30. # #
  31. # temp = np.dot(refEvecs, np.dot(axesExchangeMat, np.dot(temp1, intRefEvecs.T)))
  32. resDataTargetProj = np.dot(temp.T, resDataNoMean.T).T
  33. outSWCData = resDataAll.copy()
  34. outSWCData[:, 2:5] = resDataTargetProj + refMean
  35. resFileStub = os.path.split(resFile)[1][:-4]
  36. outSWC = os.path.join(outDir, "{}.swc".format(resFileStub))
  37. writeSWC_numpy(outSWC, outSWCData)
  38. if __name__ == "__main__":
  39. assert len(sys.argv) == 5, "Improper Usage! Please use as:\n" \
  40. "python {} <parFile> <source refSWC> <target refSWC> <outDir>".format(sys.argv[0])
  41. parFile = sys.argv[1]
  42. sourceRefSWC = sys.argv[2]
  43. targetRefSWC = sys.argv[3]
  44. outDir = sys.argv[4]
  45. projectToRef(parFile, sourceRefSWC, targetRefSWC, outDir)