(* LSMM_CR - ElBanna editorial revision v4 *)
(* Base: original Wolfram Community notebook v2.1 *)
(* Three amendments:
1. Disjoint train/test
2. Matched comparison M0, MH, MV
3. Experimental force and predictions overlaid
*)
ClearAll["Global`*"];
url = "https://zenodo.org/records/20133263/files/LSMM_CR.zip?download=1";
zip = URLDownload[url];
dir = CreateDirectory[];
ExtractArchive[zip, dir];
getData[i_Integer] := Module[
{raw, d, t, x, f, dx0, dx, sgn, cand, mins,
a, b, c, tr, te, thr = 0.005},
raw = Import[
FileNameJoin[
{dir, "LSMM_CR",
"CR_S" <> ToString[i] <> "_CyclicCompressionTension.txt"}],
"TSV"
];
d = Select[Rest[raw], VectorQ[#, NumericQ] &];
{t, x, f} = Transpose[d];
dx0 = Differences[x]/Differences[t];
dx = Join[{First[dx0]}, dx0];
sgn = Sign[dx];
Do[
If[sgn[[k]] == 0, sgn[[k]] = sgn[[k - 1]]],
{k, 2, Length[sgn]}
];
cand = Select[
Range[2, Length[x]],
sgn[[# - 1]] < 0 &&
sgn[[#]] > 0 &&
x[[#]] <= 0.8 * Min[x] &
];
mins = Take[cand, UpTo[3]];
If[Length[mins] < 3,
Print["ERROR: three compression minima not found in CR_S", i];
Return[$Failed]
];
{a, b, c} = mins;
tr = Select[Range[a, b], Abs[dx[[#]]] >= thr &];
te = Select[Range[b + 1, c], Abs[dx[[#]]] >= thr &];
If[Intersection[tr, te] =!= {},
Print["ERROR: overlapping train/test in sample ", i];
Return[$Failed]
];
<|
"x" -> x,
"f" -> f,
"sgn" -> sgn,
"dx" -> dx,
"t" -> t,
"tr" -> tr,
"te" -> te,
"mins" -> mins
|>
];
Clear[analyze];
analyze[d_Association, i_Integer] := Module[
{x = d["x"], f = d["f"], t = d["t"],
dx = d["dx"], sgn = d["sgn"],
tr = d["tr"], te = d["te"], z,
p0, pp, pm, pred0, predh, predv,
matTr, matTe, coef, scale,
positive, negative, mse0, mseh, msev, basis},
If[Length[tr] < 9 || Length[te] < 1,
Print["ERROR: too few train/test rows: ", i];
Return[$Failed]
];
positive = Select[tr, sgn[[#]] > 0 &];
negative = Select[tr, sgn[[#]] < 0 &];
If[Min[Length[positive], Length[negative]] < 4,
Print["ERROR: insufficient training points per branch: ", i];
Return[$Failed]
];
p0 = Fit[
Transpose[{x[[tr]], f[[tr]]}],
Table[z^k, {k, 0, 3}], z
];
pp = Fit[
Transpose[{x[[positive]], f[[positive]]}],
Table[z^k, {k, 0, 3}], z
];
pm = Fit[
Transpose[{x[[negative]], f[[negative]]}],
Table[z^k, {k, 0, 3}], z
];
pred0 = (N[p0 /. z -> #] &) /@ x[[te]];
predh = MapThread[
If[#2 > 0,
N[pp /. z -> #1],
N[pm /. z -> #1]] &,
{x[[te]], sgn[[te]]}
];
scale = Max[Abs[dx[[tr]]]];
If[scale <= 0,
Print["ERROR: zero training velocity: ", i];
Return[$Failed]
];
basis[xx_, vv_] := Join[
{1., xx, xx^2, xx^3},
(vv/scale) {1., xx, xx^2, xx^3}
];
matTr = MapThread[basis, {x[[tr]], dx[[tr]]}];
matTe = MapThread[basis, {x[[te]], dx[[te]]}];
coef = LeastSquares[matTr, f[[tr]]];
predv = matTe . coef;
mse0 = Mean[(f[[te]] - pred0)^2];
mseh = Mean[(f[[te]] - predh)^2];
msev = Mean[(f[[te]] - predv)^2];
<|
"Sample" -> i,
"NTrain" -> Length[tr],
"NTest" -> Length[te],
"Disjoint" -> (Intersection[tr, te] === {}),
"MSE0" -> mse0,
"MSEH" -> mseh,
"MSEV" -> msev,
"MHgainPercent" -> 100 (1 - mseh/mse0),
"MVgainPercent" -> 100 (1 - msev/mse0),
"MVbeatsMH" -> (msev < mseh),
"TimeTest" -> t[[te]],
"XTest" -> x[[te]],
"FTest" -> f[[te]],
"Pred0" -> pred0,
"PredH" -> predh,
"PredV" -> predv
|>
];
data = Table[getData[i], {i, 1, 10}];
If[!AllTrue[data, AssociationQ],
Print["STOP: one or more samples failed original cycle selection."];
Abort[]
];
results = MapIndexed[
analyze[#1, First[#2]] &,
data
];
If[
!AllTrue[results, AssociationQ] ||
!AllTrue[results, #["Disjoint"] &],
Print["STOP: incomplete fits or non-disjoint split."];
Abort[]
];
summary = Prepend[
(Lookup[#,
{"Sample", "NTrain", "NTest",
"MSE0", "MSEH", "MSEV",
"MHgainPercent", "MVgainPercent",
"MVbeatsMH"}] & /@ results),
{"Sample", "Train", "Test",
"MSE0 [N^2]", "MSEH [N^2]", "MSEV [N^2]",
"MH gain [%]", "MV gain [%]", "MV < MH"}
];
Print[Grid[summary, Frame -> All, Alignment -> Left]];
Print["M0 vs MH wins: ", Count[(#["MSEH"] < #["MSE0"] & /@ results), True], "/10"];
Print["MH vs MV wins: ", Count[Lookup[results, "MVbeatsMH"], True], "/10"];
Print["Median MH MSE reduction (%): ", Median[Lookup[results, "MHgainPercent"]]];
Print["Median MV MSE reduction (%): ", Median[Lookup[results, "MVgainPercent"]]];
makePlot[r_Association, key_, xlab_, title_] := Module[
{absc = r[key],
f = r["FTest"],
p0 = r["Pred0"],
ph = r["PredH"],
pv = r["PredV"]},
ListLinePlot[
{
Transpose[{absc, f}],
Transpose[{absc, p0}],
Transpose[{absc, ph}],
Transpose[{absc, pv}]
},
PlotLegends -> {"Experimental", "M0", "MH", "MV"},
AxesLabel -> {xlab, "Force [N]"},
PlotLabel -> title,
PlotRange -> All,
ImageSize -> Large
]
];
Do[
Print[makePlot[results[[i]], "TimeTest", "Time [s]", "CR_S" <> ToString[i] <> " - held-out force vs time"]];
Print[makePlot[results[[i]], "XTest", "Displacement [mm]", "CR_S" <> ToString[i] <> " - held-out force-displacement"]];
,
{i, 1, 10}
];
(* Base: original Wolfram Community notebook v2.1 *)
(* Three amendments:
1. Disjoint train/test
2. Matched comparison M0, MH, MV
3. Experimental force and predictions overlaid
*)
ClearAll["Global`*"];
url = "https://zenodo.org/records/20133263/files/LSMM_CR.zip?download=1";
zip = URLDownload[url];
dir = CreateDirectory[];
ExtractArchive[zip, dir];
getData[i_Integer] := Module[
{raw, d, t, x, f, dx0, dx, sgn, cand, mins,
a, b, c, tr, te, thr = 0.005},
raw = Import[
FileNameJoin[
{dir, "LSMM_CR",
"CR_S" <> ToString[i] <> "_CyclicCompressionTension.txt"}],
"TSV"
];
d = Select[Rest[raw], VectorQ[#, NumericQ] &];
{t, x, f} = Transpose[d];
dx0 = Differences[x]/Differences[t];
dx = Join[{First[dx0]}, dx0];
sgn = Sign[dx];
Do[
If[sgn[[k]] == 0, sgn[[k]] = sgn[[k - 1]]],
{k, 2, Length[sgn]}
];
cand = Select[
Range[2, Length[x]],
sgn[[# - 1]] < 0 &&
sgn[[#]] > 0 &&
x[[#]] <= 0.8 * Min[x] &
];
mins = Take[cand, UpTo[3]];
If[Length[mins] < 3,
Print["ERROR: three compression minima not found in CR_S", i];
Return[$Failed]
];
{a, b, c} = mins;
tr = Select[Range[a, b], Abs[dx[[#]]] >= thr &];
te = Select[Range[b + 1, c], Abs[dx[[#]]] >= thr &];
If[Intersection[tr, te] =!= {},
Print["ERROR: overlapping train/test in sample ", i];
Return[$Failed]
];
<|
"x" -> x,
"f" -> f,
"sgn" -> sgn,
"dx" -> dx,
"t" -> t,
"tr" -> tr,
"te" -> te,
"mins" -> mins
|>
];
Clear[analyze];
analyze[d_Association, i_Integer] := Module[
{x = d["x"], f = d["f"], t = d["t"],
dx = d["dx"], sgn = d["sgn"],
tr = d["tr"], te = d["te"], z,
p0, pp, pm, pred0, predh, predv,
matTr, matTe, coef, scale,
positive, negative, mse0, mseh, msev, basis},
If[Length[tr] < 9 || Length[te] < 1,
Print["ERROR: too few train/test rows: ", i];
Return[$Failed]
];
positive = Select[tr, sgn[[#]] > 0 &];
negative = Select[tr, sgn[[#]] < 0 &];
If[Min[Length[positive], Length[negative]] < 4,
Print["ERROR: insufficient training points per branch: ", i];
Return[$Failed]
];
p0 = Fit[
Transpose[{x[[tr]], f[[tr]]}],
Table[z^k, {k, 0, 3}], z
];
pp = Fit[
Transpose[{x[[positive]], f[[positive]]}],
Table[z^k, {k, 0, 3}], z
];
pm = Fit[
Transpose[{x[[negative]], f[[negative]]}],
Table[z^k, {k, 0, 3}], z
];
pred0 = (N[p0 /. z -> #] &) /@ x[[te]];
predh = MapThread[
If[#2 > 0,
N[pp /. z -> #1],
N[pm /. z -> #1]] &,
{x[[te]], sgn[[te]]}
];
scale = Max[Abs[dx[[tr]]]];
If[scale <= 0,
Print["ERROR: zero training velocity: ", i];
Return[$Failed]
];
basis[xx_, vv_] := Join[
{1., xx, xx^2, xx^3},
(vv/scale) {1., xx, xx^2, xx^3}
];
matTr = MapThread[basis, {x[[tr]], dx[[tr]]}];
matTe = MapThread[basis, {x[[te]], dx[[te]]}];
coef = LeastSquares[matTr, f[[tr]]];
predv = matTe . coef;
mse0 = Mean[(f[[te]] - pred0)^2];
mseh = Mean[(f[[te]] - predh)^2];
msev = Mean[(f[[te]] - predv)^2];
<|
"Sample" -> i,
"NTrain" -> Length[tr],
"NTest" -> Length[te],
"Disjoint" -> (Intersection[tr, te] === {}),
"MSE0" -> mse0,
"MSEH" -> mseh,
"MSEV" -> msev,
"MHgainPercent" -> 100 (1 - mseh/mse0),
"MVgainPercent" -> 100 (1 - msev/mse0),
"MVbeatsMH" -> (msev < mseh),
"TimeTest" -> t[[te]],
"XTest" -> x[[te]],
"FTest" -> f[[te]],
"Pred0" -> pred0,
"PredH" -> predh,
"PredV" -> predv
|>
];
data = Table[getData[i], {i, 1, 10}];
If[!AllTrue[data, AssociationQ],
Print["STOP: one or more samples failed original cycle selection."];
Abort[]
];
results = MapIndexed[
analyze[#1, First[#2]] &,
data
];
If[
!AllTrue[results, AssociationQ] ||
!AllTrue[results, #["Disjoint"] &],
Print["STOP: incomplete fits or non-disjoint split."];
Abort[]
];
summary = Prepend[
(Lookup[#,
{"Sample", "NTrain", "NTest",
"MSE0", "MSEH", "MSEV",
"MHgainPercent", "MVgainPercent",
"MVbeatsMH"}] & /@ results),
{"Sample", "Train", "Test",
"MSE0 [N^2]", "MSEH [N^2]", "MSEV [N^2]",
"MH gain [%]", "MV gain [%]", "MV < MH"}
];
Print[Grid[summary, Frame -> All, Alignment -> Left]];
Print["M0 vs MH wins: ", Count[(#["MSEH"] < #["MSE0"] & /@ results), True], "/10"];
Print["MH vs MV wins: ", Count[Lookup[results, "MVbeatsMH"], True], "/10"];
Print["Median MH MSE reduction (%): ", Median[Lookup[results, "MHgainPercent"]]];
Print["Median MV MSE reduction (%): ", Median[Lookup[results, "MVgainPercent"]]];
makePlot[r_Association, key_, xlab_, title_] := Module[
{absc = r[key],
f = r["FTest"],
p0 = r["Pred0"],
ph = r["PredH"],
pv = r["PredV"]},
ListLinePlot[
{
Transpose[{absc, f}],
Transpose[{absc, p0}],
Transpose[{absc, ph}],
Transpose[{absc, pv}]
},
PlotLegends -> {"Experimental", "M0", "MH", "MV"},
AxesLabel -> {xlab, "Force [N]"},
PlotLabel -> title,
PlotRange -> All,
ImageSize -> Large
]
];
Do[
Print[makePlot[results[[i]], "TimeTest", "Time [s]", "CR_S" <> ToString[i] <> " - held-out force vs time"]];
Print[makePlot[results[[i]], "XTest", "Displacement [mm]", "CR_S" <> ToString[i] <> " - held-out force-displacement"]];
,
{i, 1, 10}
];