Skip to content

Commit

Permalink
added test data for HB1A
Browse files Browse the repository at this point in the history
  • Loading branch information
bingli621 committed Aug 13, 2024
1 parent 1415e5c commit d2fda83
Show file tree
Hide file tree
Showing 174 changed files with 30,719 additions and 99 deletions.
Binary file modified .DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions scripts/HoV6Sn6_contour.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import matplotlib.pyplot as plt
from tavi.data.tavi import TAVI

tavi = TAVI()

tavi_file_name = "./test_data/tavi_test_exp1031.h5"
tavi.new_tavi_file(tavi_file_name)

nexus_file_name = "./test_data/nexus_exp1031.h5"
tavi.load_nexus_data_from_disk(nexus_file_name)


dataset = tavi.data["IPTS32912_HB1A_exp1031"]

print(len(dataset))
# 001 s1 scans
scan_list = [dataset[f"scan{i:04}"] for i in range(90, 121, 5)]

sg1 = tavi.generate_scan_group(signals=scan_list, signal_axes=("persistent_field", "s1", "detector"))
contour1 = sg1.generate_contour(rebin_steps=(None, None))
sg1.plot_contour(contour1, cmap="turbo")


plt.show()
1 change: 1 addition & 0 deletions scripts/test_data_conversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_open_tavi_file(tavi):
tavi = TAVI()
# test_conversion(424)
# test_conversion(710)
test_conversion(1031)
test_load_nexus_to_new_tavi(tavi)

# test_open_exsiting_tavi()
15 changes: 12 additions & 3 deletions scripts/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,18 @@ def test_fit_scan(tavi):

x, y, xerr, yerr, xlabel, ylabel, title, label = curve1
f1 = Fit(x=x, y=y, fit_range=(0.0, 4))
f1.add_background()
f1.add_signal()
f1.add_signal(model="Gaussian", values=(0, None, None), vary=(False, True, True))
f1.add_background(values=(0.7,))
f1.add_signal(
values=(None, 3.5, None),
vary=(True, True, True),
)
f1.add_signal(
model="Gaussian",
values=(None, 0, None),
vary=(True, False, True),
mins=(0, 0, 0.1),
maxs=(None, 0.1, 0.3),
)
f1.perform_fit()

p1.plot_curve(*curve1)
Expand Down
Binary file modified src/.DS_Store
Binary file not shown.
Binary file modified src/tavi/.DS_Store
Binary file not shown.
123 changes: 65 additions & 58 deletions src/tavi/data/fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def __init__(self, x, y, err=None, fit_range=None):
y (list)
err (list | None)
fit_range (tuple)
NUM_PTS (int): number of points for the fit curve
"""
self.NUM_PTS = 100
self.range = fit_range
Expand All @@ -55,17 +56,16 @@ def __init__(self, x, y, err=None, fit_range=None):
self.x.max(),
num=self.NUM_PTS,
)
self.y_plot = None

self.background_models = []
self.signal_models = []
self.pars = Parameters()
self.num_backgrounds = 0
self.num_signals = 0
self.FIT_STATUS = None
self.chisqr = 0
self.fit_result = None

self.PLOT_SEPARATELY = False
self.y_plot = None
self.best_values = None

def add_background(
self,
Expand Down Expand Up @@ -95,31 +95,37 @@ def add_background(
else:
prefix = ""
model = Fit.models[model](prefix=prefix, nan_policy="propagate")
num_params = len(model.param_names)

param_names = model.param_names
# guess initials
pars = model.guess(self.y, x=self.x)
if values is None:
values = []
for idx, param in enumerate(model.param_names):
values.append(pars[param].value)
if vary is None:
vary = [True] * num_params
if mins is None:
mins = [None] * num_params
if maxs is None:
maxs = [None] * num_params
if exprs is None:
exprs = [None] * num_params

for idx, param in enumerate(model.param_names):
self.pars.add(
param,
value=pars[param].value,
vary=vary[idx],
min=mins[idx],
max=maxs[idx],
expr=exprs[idx],
)

# overwrite with user input
if values is not None:
for idx, v in enumerate(values):
if v is not None:
pars[param_names[idx]].set(value=v)

if vary is not None:
for idx, v in enumerate(vary):
if v is not None:
pars[param_names[idx]].set(vary=v)

if mins is not None:
for idx, v in enumerate(mins):
if v is not None:
pars[param_names[idx]].set(min=v)
if maxs is not None:
for idx, v in enumerate(maxs):
if v is not None:
pars[param_names[idx]].set(max=v)

if exprs is not None:
for idx, v in enumerate(exprs):
if v is not None:
pars[param_names[idx]].set(expr=v)

for param_name in param_names:
self.pars.add(pars[param_name])

self.background_models.append(model)

Expand All @@ -132,7 +138,7 @@ def add_signal(
maxs=None,
exprs=None,
):
"""Set the model for background
"""Set the model for signal
Args:
model (str): Constant, Linear, Quadratic, Polynomial,
Expand All @@ -145,34 +151,37 @@ def add_signal(
self.num_signals += 1
prefix = f"s{self.num_signals}_"
model = Fit.models[model](prefix=prefix, nan_policy="propagate")

param_names = model.param_names
# guess initials
pars = model.guess(self.y, x=self.x)

num_params = len(model.param_names)
if values is None:
values = []
for idx, param in enumerate(model.param_names):
values.append(pars[param].value)
if vary is None:
vary = [True] * num_params
if mins is None:
mins = [None] * num_params
if maxs is None:
maxs = [None] * num_params
if exprs is None:
exprs = [None] * num_params

# pars = model.guess(self.y, x=self.x)
for idx, param in enumerate(model.param_names):
self.pars.add(
param,
value=values[idx],
vary=vary[idx],
min=mins[idx],
max=maxs[idx],
expr=exprs[idx],
)

# overwrite with user input
if values is not None:
for idx, v in enumerate(values):
if v is not None:
pars[param_names[idx]].set(value=v)

if vary is not None:
for idx, v in enumerate(vary):
if v is not None:
pars[param_names[idx]].set(vary=v)

if mins is not None:
for idx, v in enumerate(mins):
if v is not None:
pars[param_names[idx]].set(min=v)
if maxs is not None:
for idx, v in enumerate(maxs):
if v is not None:
pars[param_names[idx]].set(max=v)

if exprs is not None:
for idx, v in enumerate(exprs):
if v is not None:
pars[param_names[idx]].set(expr=v)

for param_name in param_names:
self.pars.add(pars[param_name])
self.signal_models.append(model)

def perform_fit(self):
Expand All @@ -186,10 +195,8 @@ def perform_fit(self):
else:
out = model.fit(self.y, self.pars, x=self.x, weights=self.err)

self.chisqr = out.chisqr
self.FIT_STATUS = out.success
self.result = out

# self.y_plot = model.eval(self.pars, x=self.x_plot)
self.y_plot = model.eval(out.params, x=self.x_plot)
self.best_values = out.best_values
print(out.fit_report(min_correl=0.25))
2 changes: 0 additions & 2 deletions src/tavi/data/scan_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def __init__(
signal_axes=(None, None, None),
background_axes=(None, None, None),
):

self.signals = signals
self.backgrounds = backgrounds
self.signal_axes = list(signal_axes)
Expand Down Expand Up @@ -165,7 +164,6 @@ def plot_waterfall(self, contour_plot, shifts=None, ylim=None, xlim=None, fmt="o
fig, ax = plt.subplots()
shift = 0
for i in range(num):

if np.isnan(z[:, i]).all(): # all nan
continue
else:
Expand Down
Loading

0 comments on commit d2fda83

Please sign in to comment.