yuanpeng revised this gist . Go to revision
1 file changed, 55 insertions
rebin_example.py(file created)
| @@ -0,0 +1,55 @@ | |||
| 1 | + | from mantid.simpleapi import * | |
| 2 | + | import matplotlib.pyplot as plt | |
| 3 | + | import numpy as np | |
| 4 | + | ||
| 5 | + | ||
| 6 | + | def rebin_data(datax, datay, xmin, xmax, xbin): | |
| 7 | + | """Rebin input data with specified binning params | |
| 8 | + | ||
| 9 | + | Parameters: | |
| 10 | + | datax (numpy.ndarray): Input data X array | |
| 11 | + | datay (numpy.ndarray): Input data Y array | |
| 12 | + | xmin (float): X Min for rebinning | |
| 13 | + | xmax (float): X Max for rebinning | |
| 14 | + | xbin (float): Bin size for rebinning | |
| 15 | + | ||
| 16 | + | Returns: | |
| 17 | + | tuple: Rebinned data X and Y arrays | |
| 18 | + | """ | |
| 19 | + | wksp = CreateWorkspace( | |
| 20 | + | DataX=datax, | |
| 21 | + | DataY=datay, | |
| 22 | + | NSpec=1, | |
| 23 | + | Distribution=True | |
| 24 | + | ) | |
| 25 | + | ||
| 26 | + | Rebin( | |
| 27 | + | InputWorkspace=wksp, | |
| 28 | + | OutputWorkspace="wksp_rebin", | |
| 29 | + | Params=f"{xmin}, {xbin}, {xmax}" | |
| 30 | + | ) | |
| 31 | + | ||
| 32 | + | datax_rebinned = mtd["wksp_rebin"].extractX()[0] | |
| 33 | + | datay_rebinned = mtd["wksp_rebin"].extractY()[0] | |
| 34 | + | ||
| 35 | + | return (datax_rebinned, datay_rebinned) | |
| 36 | + | ||
| 37 | + | ||
| 38 | + | if __name__ == "__main__": | |
| 39 | + | # Dummy data of a simple Gaussian distribution | |
| 40 | + | mu = 0 | |
| 41 | + | sigma = 1 | |
| 42 | + | amplitude = 1 | |
| 43 | + | ||
| 44 | + | datax = np.linspace(-5, 5, 100) | |
| 45 | + | datay = amplitude * np.exp(-((datax - mu) ** 2) / (2 * sigma ** 2)) | |
| 46 | + | ||
| 47 | + | rebinned_data = rebin_data(datax, datay, -4., 4., .2) | |
| 48 | + | ||
| 49 | + | # Plot the original and rebinned data together | |
| 50 | + | plt.plot(datax, datay, label="Original Data") | |
| 51 | + | plt.plot(rebinned_data[0], rebinned_data[1], label="Rebinned Data") | |
| 52 | + | plt.xlabel('x') | |
| 53 | + | plt.ylabel('y') | |
| 54 | + | plt.legend() | |
| 55 | + | plt.show() | |
Newer
Older