randomlyTransform.py 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Ajayrama Kumaraswamy, 2017, LMU Munich
  2. """
  3. Description: This script is used to randomly transform a list of SWCs. Sets of rotation, translation and
  4. scaling transform parameters are drawn independently from uniform distributions with specified
  5. bounds. Each transform is then applied with to each SWC in the list.
  6. The transform parameters and the order of application are as below:
  7. (i) scaling centering at the centroid and along XYZ axes. Values must be in (0, 1]
  8. (ii) Euler angles in radians, XYZ rotations about centroid
  9. (iii) translations in micrometers along XYZ axes
  10. Usage: python <path to file>randomlyTransform.py
  11. Action: Transforms SWCs using N sets of independently drawn transform paramters.
  12. For every SWC with name in expNames and in the folder dirPath, N number of correspondingly
  13. transformed SWC files with names <swc name><suffix><transform set #>.swc are created in the folder
  14. outPath.
  15. For each such SWC, a json file is also created with the input parameters, i.e.,
  16. swc being transformed, euler angles, scalings and translations.
  17. Usage guidelines: Edit the variables dirPath, expNames, outPath, N, suffix, transBounds, rotBounds, scaleBounds and
  18. run this script.
  19. """
  20. import numpy as np
  21. import os
  22. from regmaxsn.core.transforms import compose_matrix
  23. from regmaxsn.core.swcFuncs import transSWC_rotAboutPoint
  24. import json
  25. # ----------------------------------------------------------------------------------------------------------------------
  26. temp = os.path.split(__file__)[0]
  27. dirPath = os.path.join(os.path.split(temp)[0], 'TestFiles')
  28. expNames = [
  29. 'HSN-fluoro01.CNG',
  30. # 'HSN-fluoro01.CNGNoiseStd1',
  31. # 'HSN-fluoro01.CNGNoiseStd2',
  32. # 'HSN-fluoro01.CNGNoiseStd3',
  33. # 'HSN-fluoro01.CNGNoiseStd4',
  34. # 'HSN-fluoro01.CNGNoiseStd5',
  35. ]
  36. outPath = dirPath
  37. N = 1
  38. # ----------------------------------------------------------------------------------------------------------------------
  39. # suffix = 'RandTrans'
  40. #
  41. # transBounds = [-20, 20]
  42. # rotBounds = [-np.pi / 6, np.pi / 6]
  43. # scaleBounds = [0.5, 1 / 0.5]
  44. # ----------------------------------------------------------------------------------------------------------------------
  45. # suffix = 'RandRot'
  46. #
  47. # transBounds = [0, 0]
  48. # rotBounds = [-np.pi / 6, np.pi / 6]
  49. # scaleBounds = [1, 1]
  50. # ----------------------------------------------------------------------------------------------------------------------
  51. # suffix = 'RandScale'
  52. #
  53. # transBounds = [0, 0]
  54. # rotBounds = [0, 0]
  55. # scaleBounds = [0.5, 1 / 0.5]
  56. # ----------------------------------------------------------------------------------------------------------------------
  57. suffix = 'RandTranslate'
  58. transBounds = [-20, 20]
  59. rotBounds = [0, 0]
  60. scaleBounds = [1, 1]
  61. # ----------------------------------------------------------------------------------------------------------------------
  62. for ind in range(N):
  63. translation = np.random.rand(3) * (transBounds[1] - transBounds[0]) + transBounds[0]
  64. scale = np.random.rand(3) * (scaleBounds[1] - scaleBounds[0]) + scaleBounds[0]
  65. rots = np.random.rand(3) * (rotBounds[1] - rotBounds[0]) + rotBounds[0]
  66. rotMat = compose_matrix(angles=rots, scale=scale)[:3, :3]
  67. for expName in expNames:
  68. inFile = os.path.join(dirPath, expName + '.swc')
  69. outFile = os.path.join(outPath, expName + suffix + str(ind) + '.swc')
  70. rotCenter = np.loadtxt(inFile)[:, 2:5].mean(axis=0)
  71. transSWC_rotAboutPoint(inFile, rotMat, translation, outFile, rotCenter)
  72. with open(os.path.join(outPath, expName + suffix + str(ind) + '.json'), 'w') as fle:
  73. toWrite = {'inFile': inFile,
  74. 'translation': translation.tolist(), 'angles': rots.tolist(), 'scale': scale.tolist(),
  75. 'comments': 'rotation and scaling about inFile centroid'}
  76. json.dump(toWrite, fle)