addRandomNoise.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. # Ajayrama Kumaraswamy, 2017, LMU Munich
  2. """
  3. Description: This script is used to adding spherical gaussian noise of different standard deviations
  4. to the points of a list of SWCs.
  5. Usage: python <path to file>addRandomNoise.py
  6. Action: For each SWC in baseSWCs, and each value of standard deviation in noiseStds, draws a sample of
  7. the same size as the number of points in the SWC from the same zero mean Gaussian distribution of
  8. the specified standard deviation, adds it to the points and writes it to
  9. <swc Name>NoiseStd<standard deviation>.swc in the folder outPath
  10. Usage guidelines: Edit the variables dirPath, expNames, outPath, noiseStds and run this script.
  11. """
  12. import numpy as np
  13. import os
  14. from regmaxsn.core.swcFuncs import readSWC_numpy, writeSWC_numpy
  15. # ----------------------------------------------------------------------------------------------------------------------
  16. temp = os.path.split(__file__)[0]
  17. dirPath = os.path.join(os.path.split(temp)[0], 'TestFiles')
  18. expNames = [
  19. 'HSN-fluoro01.CNG',
  20. ]
  21. outPath = dirPath
  22. noiseStds = list(range(1, 6))
  23. # ----------------------------------------------------------------------------------------------------------------------
  24. baseSWCs = [os.path.join(dirPath, expName + '.swc') for expName in expNames]
  25. for baseSWC in baseSWCs:
  26. expName = os.path.split(baseSWC)[1][:-4]
  27. headr, data = readSWC_numpy(baseSWC)
  28. for std in noiseStds:
  29. noise = np.random.normal(0, std, (data.shape[0], 3))
  30. noisyData = data.copy()
  31. noisyData[:, 2:5] = data[:, 2:5] + noise
  32. outFile = os.path.join(outPath, expName + 'NoiseStd' + str(std) + '.swc')
  33. writeSWC_numpy(outFile, noisyData, headr)
  34. # ----------------------------------------------------------------------------------------------------------------------