Skip to content

Commit 600786c

Browse files
committed
allow for tying and fixing
1 parent f5f1138 commit 600786c

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

scripts/Engineering/texture/TextureUtils.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -396,31 +396,38 @@ def rerun_fit_with_new_ws(
396396
if "InputWorkspace" in k:
397397
md_fit_kwargs[k] = new_ws.name()
398398

399-
# mdf.freeAll()
400399
bg_ties = []
400+
new_func = MultiDomainFunction()
401401
for idom in range(mdf.nFunctions()):
402402
comp = mdf[idom]
403403
peak = comp[0]
404404
bg = comp[1]
405-
peak.freeAll()
405+
# create fresh peak as ties are causing problems
406+
new_peak = FunctionFactory.Instance().createPeakFunction(peak.name())
407+
[new_peak.setParameter(param, peak.getParameterValue(param)) for param in ("I", "X0", "A", "B", "S")]
406408
# update constraints around new values
407409
intens = max(peak.getParameterValue("I"), 1)
408410
x0 = peak.getParameterValue("X0")
409-
peak.addConstraints(f"{max(intens / 2, 1e-6)}<I<{intens * 2}")
410-
peak.addConstraints(f"{x0 - x0_move}<X0<{x0 + x0_move}")
411+
new_peak.addConstraints(f"{max(intens / 2, 1e-6)}<I<{intens * 2}")
412+
new_peak.addConstraints(f"{x0 - x0_move}<X0<{x0 + x0_move}")
411413
if tie_background and idom > 0:
412414
for ipar_bg in range(bg.nParams()):
413415
par = bg.getParamName(ipar_bg)
414416
bg_ties.append(f"f{idom}.f1.{par}=f0.f1.{par}")
415417
for param in parameters_to_fix:
416-
peak.fixParameter(param)
418+
new_peak.fixParameter(param)
419+
420+
comp_func = CompositeFunctionWrapper(FunctionWrapper(new_peak), FunctionWrapper(bg), NumDeriv=True)
421+
new_func.add(comp_func.function)
422+
new_func.setDomainIndex(idom, idom)
423+
key_suffix = f"_{idom}" if idom > 0 else ""
424+
new_func.setMatrixWorkspace(new_ws, idom, md_fit_kwargs["StartX" + key_suffix], md_fit_kwargs["EndX" + key_suffix])
417425

418426
# if ties are required add them
419-
func = f"{str(mdf)};ties=({','.join(bg_ties)})" if tie_background else mdf
427+
func = f"{str(new_func)};ties=({','.join(bg_ties)})" if tie_background else new_func
420428

421429
return Fit(
422430
Function=func,
423-
CostFunction="Unweighted least squares",
424431
Output=f"fit_{new_ws.name()}",
425432
MaxIterations=iters,
426433
**fit_kwargs,
@@ -437,8 +444,8 @@ def fit_all_peaks(
437444
i_over_sigma_thresh: float = 2.0,
438445
nan_replacement: Optional[str] = "zeros",
439446
no_fit_value_dict: Optional[dict] = None,
440-
smooth_vals: Sequence[int] = (15, 5),
441-
tied_bkgs: Sequence[bool] = (False, False),
447+
smooth_vals: Sequence[int] = (3, 2),
448+
tied_bkgs: Sequence[bool] = (False, True),
442449
final_fit_raw: bool = False,
443450
parameters_to_tie: Sequence[str] = ("A", "B"),
444451
subsequent_fit_param_fix: Sequence[str] = ("A", "B"),
@@ -536,7 +543,7 @@ def fit_all_peaks(
536543
)
537544

538545
# perform subsequent fits
539-
while len(fit_wss) > 0:
546+
while len(fit_wss) - 1 > fit_num:
540547
fit_num += 1
541548
mdf = fit_object.Function.function
542549
fit_ws = fit_wss[fit_num]
@@ -602,7 +609,6 @@ def fit_all_peaks(
602609
row += [default_vals[p], np.nan]
603610
table_vals[ispec] = row
604611
if nan_replacement:
605-
print("Nan replaced")
606612
table_vals = replace_nans(table_vals, nan_replacement)
607613
for i, row in enumerate(table_vals):
608614
out_tab.addRow([i] + list(row))

0 commit comments

Comments
 (0)