/Project-Management/ILL/Wilcke_Report/Algorithms/LampRebin.py

https://github.com/mantidproject/documents · Python · 111 lines · 89 code · 16 blank · 6 comment · 15 complexity · b4f7d192ec0f4ac2d4b3d06a7612091e MD5 · raw file

  1. from mantid import config
  2. from mantid.kernel import *
  3. from mantid.api import *
  4. import numpy
  5. import math
  6. def GetDict(InputWorkspace, Spectrum, Start, BinWidth, End):
  7. # Get the data
  8. datax = InputWorkspace.readX(Spectrum)
  9. datay = InputWorkspace.readY(Spectrum)
  10. # Get the start and end index
  11. start_i = 0
  12. end_i = len(datax)
  13. for i in range(0, len(datax)):
  14. if datax[i]>Start:
  15. start_i = i
  16. break
  17. for i in range(len(datax), start_i, -1):
  18. if datax[i-1]<End:
  19. end_i = i
  20. break
  21. # Create end data arrays
  22. endx = numpy.arange(Start, End+BinWidth, BinWidth)
  23. endy = numpy.empty(len(endx))
  24. ind = start_i
  25. dic = {}
  26. # Iterate through the x values
  27. for v in range(0, len(endx)):
  28. value = endx[v] # get value at index
  29. dif = 0 # variable to get closest x value
  30. found = False # variable to know when to break
  31. start = ind # variable to start where last loop left off
  32. for i in range(start, end_i): #iterate through original xvalues
  33. temp = math.fabs((datax[i] - value)) # get difference
  34. if (dif == 0) | (temp<dif): # only select the lowest difference
  35. dif = temp
  36. ind = i # at the end of the loop, should contain the index of the closest x value in the original x data
  37. if (round(temp) == 0) & (found == False): #once you've got the right integer value, you've "found" the value
  38. found = True
  39. if (round(temp) !=0) & (found == True): #if you've found and it's not longer the right integer, you've gone too far
  40. break
  41. dic[v] = ind
  42. return dic
  43. def TestRebin(Input, Spectrum, Start, Bin, End, Dic):
  44. # Get the data
  45. datax = Input.readX(Spectrum)
  46. datae = Input.readE(Spectrum)
  47. datay = Input.readY(Spectrum)
  48. # Create end data arrays
  49. endx = numpy.arange(Start, End+Bin, Bin)
  50. endy = numpy.empty(len(endx)-1)
  51. ende = numpy.empty(len(endy))
  52. for i in range(0, len(endy)):
  53. try:
  54. endy[i] = datay[Dic[i]] #look the value up in the provided dictionnary
  55. ende[i] = datae[Dic[i]]
  56. except:
  57. endy[i] = 0
  58. ende[i] = 0
  59. output = WorkspaceFactory.create("Workspace2D", NVectors=1, XLength=len(endx), YLength=len(endy))
  60. output.setX(0, endx)
  61. output.setY(0, endy)
  62. output.setE(0, ende)
  63. return output
  64. class LampRebin(PythonAlgorithm):
  65. def PyInit(self):
  66. self.declareProperty(WorkspaceProperty(name='InputWorkspace', defaultValue='', direction=Direction.Input))
  67. self.declareProperty(IntArrayProperty('Spectra'))
  68. self.declareProperty('StartBin',0, IntMandatoryValidator())
  69. self.declareProperty('BinWidth', 1.0, FloatMandatoryValidator())
  70. self.declareProperty('EndBin', 0, IntMandatoryValidator())
  71. self.declareProperty(WorkspaceProperty(name="OutputWorkspace", defaultValue="",direction=Direction.Output))
  72. def PyExec(self):
  73. ws = self.getProperty('InputWorkspace').value
  74. spectra = self.getProperty('Spectra').value
  75. if len(spectra)==0:
  76. spectra = numpy.arange(0, ws.getNumberHistograms())
  77. start = self.getProperty('StartBin').value
  78. bin = self.getProperty('BinWidth').value
  79. end = self.getProperty('EndBin').value
  80. lines = ws.getNumberHistograms()
  81. length = int((end-start)/bin) + 1
  82. for i in range(0, ws.getNumberHistograms()): # get first non-monitor index
  83. if not ws.getDetector(i).isMonitor():
  84. break
  85. Dic = GetDict(ws, i, start, bin, end) #get dictionnary for first non monitor index
  86. output_ws = WorkspaceFactory.create(ws, NVectors=lines, XLength=length, YLength=length-1)
  87. prog_reporter = Progress(self,start=0.0,end=1.0, nreports=len(spectra))
  88. for i in spectra:
  89. data = TestRebin(ws,i, start, bin, end, Dic)
  90. output_ws.setX(i, data.readX(0))
  91. output_ws.setY(i, data.readY(0))
  92. output_ws.setE(i, data.readE(0))
  93. prog_reporter.report()
  94. self.setProperty('OutputWorkspace', output_ws)
  95. AlgorithmFactory.subscribe(LampRebin)