Última atividade 1727711760

rebin_example.py Bruto
1from mantid.simpleapi import *
2import matplotlib.pyplot as plt
3import numpy as np
4
5
6def 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
38if __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()