rebin_example.py
· 1.4 KiB · Python
原始文件
from mantid.simpleapi import *
import matplotlib.pyplot as plt
import numpy as np
def rebin_data(datax, datay, xmin, xmax, xbin):
"""Rebin input data with specified binning params
Parameters:
datax (numpy.ndarray): Input data X array
datay (numpy.ndarray): Input data Y array
xmin (float): X Min for rebinning
xmax (float): X Max for rebinning
xbin (float): Bin size for rebinning
Returns:
tuple: Rebinned data X and Y arrays
"""
wksp = CreateWorkspace(
DataX=datax,
DataY=datay,
NSpec=1,
Distribution=True
)
Rebin(
InputWorkspace=wksp,
OutputWorkspace="wksp_rebin",
Params=f"{xmin}, {xbin}, {xmax}"
)
datax_rebinned = mtd["wksp_rebin"].extractX()[0]
datay_rebinned = mtd["wksp_rebin"].extractY()[0]
return (datax_rebinned, datay_rebinned)
if __name__ == "__main__":
# Dummy data of a simple Gaussian distribution
mu = 0
sigma = 1
amplitude = 1
datax = np.linspace(-5, 5, 100)
datay = amplitude * np.exp(-((datax - mu) ** 2) / (2 * sigma ** 2))
rebinned_data = rebin_data(datax, datay, -4., 4., .2)
# Plot the original and rebinned data together
plt.plot(datax, datay, label="Original Data")
plt.plot(rebinned_data[0], rebinned_data[1], label="Rebinned Data")
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
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() |