1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
//! Golden Pipeline dialog for configuring and applying the golden pipeline transformation.
use crate::Kinetics::experimental_kinetics::column_provenance::{
ColumnTransformKind, QualityStatus,
};
use crate::Kinetics::experimental_kinetics::exp_engine_api::GoldenPipelineConfig;
use crate::Kinetics::experimental_kinetics::experiment_series_main::ExperimentThermalMode;
use crate::Kinetics::experimental_kinetics::math::ndarray_statistics::FilterQualityReport;
use crate::gui::experimental_kinetics_gui::model::PlotModel;
use eframe::egui;
use log::info;
/// State for the golden pipeline dialog.
#[derive(Clone, Debug)]
pub struct GoldenPipelineDialogState {
pub open: bool,
pub config: GoldenPipelineConfig,
pub last_error: Option<String>,
pub quality_report: Option<FilterQualityReport>,
pub quality_note: Option<String>,
}
impl Default for GoldenPipelineDialogState {
fn default() -> Self {
Self {
open: false,
config: GoldenPipelineConfig::default(),
last_error: None,
quality_report: None,
quality_note: None,
}
}
}
impl GoldenPipelineDialogState {
/// Open the dialog with default config.
pub fn open(&mut self) {
self.open = true;
self.config = GoldenPipelineConfig::default();
self.last_error = None;
self.quality_report = None;
self.quality_note = None;
}
fn find_savitzky_golay_report(
model: &PlotModel,
experiment_id: &str,
) -> Option<FilterQualityReport> {
let experiment = model.series.get_experiment_by_id(experiment_id).ok()?;
for column in experiment.dataset.list_of_columns() {
let Some(provenance) = experiment.dataset.column_provenance(&column) else {
continue;
};
for step in provenance.steps.iter().rev() {
if matches!(&step.kind, ColumnTransformKind::SavitzkyGolay { .. }) {
if let Some(quality) = &step.quality {
if matches!(&quality.status, QualityStatus::Computed) {
if let Some(report) = &quality.report {
return Some(report.clone());
}
}
}
}
}
}
None
}
/// Show the dialog window.
pub fn show(&mut self, ctx: &egui::Context, model: &mut PlotModel) {
if !self.open {
return;
}
let mut dialog_open = self.open;
let mut should_apply = false;
let mut should_close = false;
egui::Window::new("Golden Pipeline")
.open(&mut dialog_open)
.resizable(true)
.default_size([500.0, 600.0])
.show(ctx, |ui| {
ui.heading("Golden Pipeline Configuration");
if let Ok(id) = model.get_experiment_by_selected_curve() {
if let Ok(experiment) = model.series.get_experiment_by_id(&id) {
if experiment.meta.resolved_thermal_mode() == ExperimentThermalMode::Isothermal {
if let Some(temperature) = experiment.meta.isothermal_temperature {
ui.label(format!(
"Isothermal input: setpoint {temperature:.3} K; measured T is optional and dT/dt will not be generated."
));
} else {
ui.colored_label(
egui::Color32::YELLOW,
"Isothermal metadata has no setpoint temperature.",
);
}
} else {
ui.label("Non-isothermal input: a bound measured temperature column is required; the pipeline generates dT/dt.");
}
}
}
ui.separator();
egui::ScrollArea::vertical().show(ui, |ui| {
// Time cuts
ui.collapsing("Time cuts", |ui| {
ui.horizontal(|ui| {
ui.label("Cut before time (optional):");
if let Some(ref mut val) = self.config.time_cut_before {
ui.add(egui::DragValue::new(val).speed(0.1));
} else {
if ui.button("Set").clicked() {
self.config.time_cut_before = Some(0.0);
}
}
if self.config.time_cut_before.is_some() && ui.button("Clear").clicked()
{
self.config.time_cut_before = None;
}
});
ui.horizontal(|ui| {
ui.label("Cut after time (optional):");
if let Some(ref mut val) = self.config.time_cut_after {
ui.add(egui::DragValue::new(val).speed(0.1));
} else {
if ui.button("Set").clicked() {
self.config.time_cut_after = Some(0.0);
}
}
if self.config.time_cut_after.is_some() && ui.button("Clear").clicked()
{
self.config.time_cut_after = None;
}
});
});
// Savitzky–Golay smoothing
ui.collapsing("Savitzky–Golay Smoothing", |ui| {
ui.horizontal(|ui| {
ui.label("Window size:");
ui.add(
egui::DragValue::new(&mut self.config.sav_gol_config.window_size)
.speed(1)
.range(3..=101),
);
});
ui.horizontal(|ui| {
ui.label("Polynomial degree:");
ui.add(
egui::DragValue::new(&mut self.config.sav_gol_config.poly_degree)
.speed(1)
.range(1..=10),
);
});
ui.horizontal(|ui| {
ui.label("Derivative order:");
ui.add(
egui::DragValue::new(&mut self.config.sav_gol_config.deriv)
.speed(1)
.range(0..=5),
);
});
ui.horizontal(|ui| {
ui.label("Delta (step):");
ui.add(
egui::DragValue::new(&mut self.config.sav_gol_config.delta)
.speed(0.01),
);
});
});
// Averaging time for conversion
ui.collapsing("Conversion", |ui| {
ui.horizontal(|ui| {
ui.label("Averaging time (for conversion) - IN SECONDS:");
ui.add(
egui::DragValue::new(&mut self.config.averaging_time).speed(0.1),
);
});
});
// Spline resampling
ui.collapsing("Spline Resampling", |ui| {
ui.horizontal(|ui| {
ui.label("Number of points:");
ui.add(
egui::DragValue::new(&mut self.config.spline_config.n_points)
.speed(1)
.range(10..=10000),
);
});
ui.horizontal(|ui| {
ui.label("Number of internal knots:");
ui.add(
egui::DragValue::new(
&mut self.config.spline_config.n_internal_points,
)
.speed(1)
.range(10..=100000),
);
});
ui.horizontal(|ui| {
ui.label("Spline degree:");
ui.add(
egui::DragValue::new(&mut self.config.spline_config.degree)
.speed(1)
.range(1..=5),
);
});
});
// Output options
ui.collapsing("Output Options", |ui| {
ui.checkbox(
&mut self.config.save_to_new_experiment,
"Save to new experiment",
);
ui.checkbox(&mut self.config.del_old_experiment, "Delete old experiment");
});
});
ui.separator();
if let Some(err) = &self.last_error {
ui.colored_label(egui::Color32::RED, err);
}
if let Some(report) = &self.quality_report {
ui.separator();
ui.strong("Savitzky-Golay mass smoothing quality");
ui.label(format!(
"Compared: {} -> {}",
report.column_raw, report.column_filtered
));
egui::Grid::new("golden_pipeline_quality_report")
.num_columns(2)
.striped(true)
.show(ui, |ui| {
ui.label("First-difference roughness ratio");
ui.label(format!("{:.6}", report.roughness_ratio_1st));
ui.end_row();
ui.label("Second-difference roughness ratio");
ui.label(format!("{:.6}", report.roughness_ratio_2nd));
ui.end_row();
ui.label("RMSE vs input (mass units)");
ui.label(format!("{:.6}", report.rmse));
ui.end_row();
ui.label("Normalized RMSE");
ui.label(format!("{:.6}", report.normalized_rmse));
ui.end_row();
ui.label("Correlation");
ui.label(format!("{:.6}", report.correlation));
ui.end_row();
});
ui.label(
"Roughness ratios below 1 mean smoother differences; these metrics compare with the input, not with an unknown true signal.",
);
}
if let Some(note) = &self.quality_note {
ui.label(note);
}
ui.horizontal(|ui| {
if ui.button("Close").clicked() {
should_close = true;
}
if ui.button("Calculate").clicked() {
should_apply = true;
}
});
});
if should_close {
dialog_open = false;
}
self.open = dialog_open;
if should_apply {
info!("Applying golden pipeline with config: {:?}", self.config);
self.last_error = None;
self.quality_report = None;
self.quality_note = None;
// Get selected experiment ID
match model.get_experiment_by_selected_curve() {
Ok(id) => {
let output_id = if self.config.save_to_new_experiment {
format!("proceeded_{id}")
} else {
id.clone()
};
match model.apply_golden_pipeline(&id, self.config.clone()) {
Ok(()) => {
self.quality_report =
Self::find_savitzky_golay_report(model, &output_id);
if self.quality_report.is_none() {
self.quality_note = Some(
"Pipeline completed, but no comparable SG quality report was recorded."
.to_string(),
);
}
ctx.request_repaint();
let _ = model.push_message(&format!(
"Golden Pipeline completed for '{output_id}'"
));
info!("Golden pipeline applied successfully.");
}
Err(e) => {
let message = format!("Golden Pipeline failed: {e:?}");
self.last_error = Some(message.clone());
let _ = model.push_message(&message);
}
}
}
Err(e) => {
let message = format!("Golden Pipeline could not start: {e:?}");
self.last_error = Some(message.clone());
let _ = model.push_message(&message);
}
}
}
}
}