KiThe 0.3.4

A numerical suite for chemical kinetics and thermodynamics, combustion, heat and mass transfer,chemical engeneering. Work in progress. Advices and contributions will be appreciated
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! Аналитический слой `TGAExperiment`.
//!
//! Здесь живут сглаживание, фильтры, ресемплинг, oneframeplot helpers,
//! статистика по окнам и вспомогательные wrappers для работы с осями.
//! Модуль оставлен плоским намеренно: здесь важнее читабельность API,
//! чем ещё одна прослойка абстракции.

use super::*;

impl TGAExperiment {
    //======================================================================
    // SMOOTHING AND FILTERING
    //======================================================================
    pub fn smooth_columns(
        self,
        cols: &[&str],
        strategy: crate::Kinetics::experimental_kinetics::exp_kinetics_smooth_filter::SmoothStrategy,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.smooth_columns(cols, strategy)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn rolling_mean(self, col_name: &str, window: usize) -> Self {
        let dataset = self.dataset.rolling_mean(col_name, window);
        Self {
            dataset,
            meta: self.meta,
        }
    }

    pub fn rolling_mean_as(self, col_name: &str, window: usize, out_col: Option<&str>) -> Self {
        let dataset = self.dataset.rolling_mean_as(col_name, window, out_col);
        Self {
            dataset,
            meta: self.meta,
        }
    }

    pub fn hampel_filter(
        self,
        col: &str,
        window: usize,
        n_sigma: f64,
        strategy: crate::Kinetics::experimental_kinetics::exp_kinetics_smooth_filter::HampelStrategy,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.hampel_filter(col, window, n_sigma, strategy)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn hampel_filter_as(
        self,
        col: &str,
        window: usize,
        n_sigma: f64,
        strategy: crate::Kinetics::experimental_kinetics::exp_kinetics_smooth_filter::HampelStrategy,
        out_col: Option<&str>,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .hampel_filter_as(col, window, n_sigma, strategy, out_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn hampel_filter_null_safe(
        self,
        col: &str,
        window: usize,
        n_sigma: f64,
        strategy: crate::Kinetics::experimental_kinetics::exp_kinetics_smooth_filter::HampelStrategy,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .hampel_filter_null_safe(col, window, n_sigma, strategy)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn hampel_filter_null_safe_as(
        self,
        col: &str,
        window: usize,
        n_sigma: f64,
        strategy: crate::Kinetics::experimental_kinetics::exp_kinetics_smooth_filter::HampelStrategy,
        out_col: Option<&str>,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .hampel_filter_null_safe_as(col, window, n_sigma, strategy, out_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn sg_filter_column(
        self,
        col: &str,
        window: usize,
        poly_order: usize,
        deriv: usize,
        delta: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .sg_filter_column(col, window, poly_order, deriv, delta)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn sg_filter_column_as(
        self,
        col: &str,
        window: usize,
        poly_order: usize,
        deriv: usize,
        delta: f64,
        out_col: Option<&str>,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .sg_filter_column_as(col, window, poly_order, deriv, delta, out_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lowess_smooth_columns(
        self,
        time_col: &str,
        columns: &[&str],
        config: LowessConfig,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .lowess_smooth_columns(time_col, columns, config)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lowess_smooth_column(
        self,
        time_col: &str,
        column: &str,
        config: LowessConfig,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .lowess_smooth_column(time_col, column, config)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lowess_smooth_column_as(
        self,
        time_col: &str,
        column: &str,
        out_column: Option<&str>,
        config: LowessConfig,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .lowess_smooth_column_as(time_col, column, out_column, config)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lowess_smooth_columns_as(
        self,
        time_col: &str,
        columns: &[&str],
        out_columns: &[Option<&str>],
        config: LowessConfig,
    ) -> Result<Self, TGADomainError> {
        let dataset =
            self.dataset
                .lowess_smooth_columns_as(time_col, columns, out_columns, config)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn spline_resample_columns_as(
        self,
        time_col: &str,
        new_time_col: &str,
        columns: &[&str],
        out_columns: &[Option<&str>],
        n_points: usize,
        kind: SplineKind,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.spline_resample_columns_as(
            time_col,
            new_time_col,
            columns,
            out_columns,
            n_points,
            kind,
        )?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn spline_resample_columns(
        self,
        time_col: &str,
        new_time_col: &str,
        columns: &[&str],
        n_points: usize,
        kind: SplineKind,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.spline_resample_columns(
            time_col,
            new_time_col,
            columns,
            n_points,
            kind,
        )?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lsq_spline_resample_columns(
        self,
        time_col: &str,
        new_time_col: &str,
        columns: &[&str],
        n_points: usize,
    ) -> Result<Self, TGADomainError> {
        let dataset =
            self.dataset
                .lsq_spline_resample_columns(time_col, new_time_col, columns, n_points)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn lsq_spline_resample_columns_as(
        mut self,
        time_col: &str,
        new_time_col: &str,
        columns: &[&str],
        out_columns: &[Option<&str>],
        n_points: usize,
        degree: usize,
        n_internal_knots: usize,
        solver: SolverKind,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.lsq_spline_resample_columns_as(
            time_col,
            new_time_col,
            columns,
            out_columns,
            n_points,
            degree,
            n_internal_knots,
            solver,
        )?;
        self.dataset = dataset;
        Ok(self)
    }

    //======================================================================
    // GUI / ONEFRAMEPLOT HELPERS
    //======================================================================
    pub fn set_oneframeplot_x(self, x_col: &str) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.set_oneframeplot_x(x_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn oneframeplot_axis_name(&self, axis: XY) -> Result<String, TGADomainError> {
        self.dataset.oneframeplot_axis_name(axis)
    }

    pub fn set_oneframeplot_y(self, y_col: &str) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.set_oneframeplot_y(y_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn sample_oneframeplot(
        &self,
        range: Option<ViewRange>,
        max_points: usize,
    ) -> Result<PlotSeries, TGADomainError> {
        self.dataset.sample_oneframeplot(range, max_points)
    }

    pub fn sample_column(
        &self,
        col_name: &str,
        range: Option<(f64, f64)>,
        n_points: usize,
    ) -> Result<Vec<f64>, TGADomainError> {
        self.dataset.sample_column(col_name, range, n_points)
    }

    pub fn spline_resample_oneframeplot(
        self,
        new_time_col: &str,
        n_points: usize,
        kind: SplineKind,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .spline_resample_oneframeplot(new_time_col, n_points, kind)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn spline_resample_oneframeplot_as(
        self,
        new_time_col: &str,
        n_points: usize,
        kind: SplineKind,
        out_col: Option<&str>,
    ) -> Result<Self, TGADomainError> {
        let dataset =
            self.dataset
                .spline_resample_oneframeplot_as(new_time_col, n_points, kind, out_col)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn with_x_or_y<F>(self, axis: XY, op: F) -> Result<Self, TGADomainError>
    where
        F: FnOnce(TGADataset, &str) -> Result<TGADataset, TGADomainError>,
    {
        let dataset = self.dataset.with_x_or_y(axis, op)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn with_x_and_y<F>(self, op: F) -> Result<Self, TGADomainError>
    where
        F: FnOnce(TGADataset, &str, &str) -> Result<TGADataset, TGADomainError>,
    {
        let dataset = self.dataset.with_x_and_y(op)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn cut_before_x_or_y(self, axis: XY, start_value: f64) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.cut_before_x_or_y(axis, start_value)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn cut_after_x_or_y(self, axis: XY, end_value: f64) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.cut_after_x_or_y(axis, end_value)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn cut_range_x_or_y(self, axis: XY, from: f64, to: f64) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.cut_range_x_or_y(axis, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn cut_range_inverse_x_or_y(
        self,
        axis: XY,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.cut_range_inverse_x_or_y(axis, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn min_distance_to_oneframeplot_point(
        &self,
        point: (f64, f64),
    ) -> Result<f64, TGADomainError> {
        self.dataset.min_distance_to_oneframeplot_point(point)
    }

    pub fn sample_columns(
        &self,
        time_col: &str,
        value_cols: &[&str],
        range: Option<ViewRange>,
        max_points: usize,
    ) -> Result<Vec<PlotSeries>, TGADomainError> {
        self.dataset
            .sample_columns(time_col, value_cols, range, max_points)
    }

    pub fn list_of_columns(&self) -> Vec<String> {
        self.dataset.list_of_columns()
    }

    pub fn get_axis_as_vec(&self, axis: XY) -> Result<Vec<f64>, TGADomainError> {
        self.dataset.get_axis_as_vec(axis)
    }

    pub fn get_x_as_vec(&self) -> Result<Vec<f64>, TGADomainError> {
        self.dataset.get_x_as_vec()
    }

    pub fn get_y_as_vec(&self) -> Result<Vec<f64>, TGADomainError> {
        self.dataset.get_y_as_vec()
    }

    pub fn get_plotseries(&self) -> Result<PlotSeries, TGADomainError> {
        self.dataset.get_plotseries()
    }

    pub fn plot_xy_ranges(&self) -> Result<Ranges, TGADomainError> {
        self.dataset.plot_xy_ranges()
    }

    pub fn get_time_col(&self) -> Result<String, TGADomainError> {
        self.dataset.get_time_col()
    }

    pub fn get_mass_col(&self) -> Result<String, TGADomainError> {
        self.dataset.get_mass_col()
    }

    pub fn get_temperature_col(&self) -> Result<String, TGADomainError> {
        self.dataset.get_temperature_col()
    }

    //======================================================================
    // STATS / COLUMN HELPERS
    //======================================================================
    pub fn mean_on_interval(
        &self,
        value_col: &str,
        time_col: &str,
        from: f64,
        to: f64,
    ) -> Result<f64, TGADomainError> {
        Ok(self
            .dataset
            .mean_on_interval(value_col, time_col, from, to)?)
    }

    pub fn mean_on_interval_on_own_range(
        &self,
        column: &str,
        from: f64,
        to: f64,
    ) -> Result<f64, TGADomainError> {
        Ok(self
            .dataset
            .mean_on_interval_on_own_range(column, from, to)?)
    }

    pub fn mean_on_column(&self, column: &str) -> Result<f64, TGADomainError> {
        Ok(self.dataset.mean_on_column(column)?)
    }

    pub fn take_column(&mut self, column_name: &str) -> Option<String> {
        self.dataset.take_column(column_name)
    }

    pub fn list_of_columns_to_recalc(&mut self) -> Vec<String> {
        self.dataset.list_of_columns_to_recalc()
    }

    pub fn drop_nulls(&mut self) -> Result<(), TGADomainError> {
        self.dataset.drop_nulls()
    }

    pub fn apply_golden_pipeline(
        self,
        config: GoldenPipelineConfig,
    ) -> Result<(Self, Vec<String>), TGADomainError> {
        let (dataset, vec_of_new) = self.dataset.apply_golden_pipeline(config)?;
        let exp = Self {
            dataset,
            meta: self.meta.clone(),
        };
        Ok((exp, vec_of_new))
    }

    pub fn get_column_by_nature(&self, nature: ColumnNature) -> Option<String> {
        self.dataset.get_column_by_nature(nature)
    }

    pub fn get_columns_by_nature(&self, nature: Vec<ColumnNature>) -> Vec<Option<String>> {
        self.dataset.get_columns_by_nature(nature)
    }

    /// Returns `true` when the experiment has at least one reversible snapshot.
    pub fn can_undo(&self) -> bool {
        self.dataset.can_undo()
    }

    /// Revert the last dataset mutation for this experiment.
    pub fn undo_last(mut self) -> Result<Self, TGADomainError> {
        self.dataset.undo_last()?;
        Ok(self)
    }

    pub fn offset_y_column_in_range_by_x_reference(
        self,
        offset: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .offset_y_column_in_range_by_x_reference(offset, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn offset_y_column_in_its_range(
        self,
        offset: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .offset_y_column_in_its_range(offset, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn offset_x_column_in_its_range(
        self,
        offset: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .offset_x_column_in_its_range(offset, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn scale_y_column_in_range_by_x_reference(
        self,
        scale: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self
            .dataset
            .scale_y_column_in_range_by_x_reference(scale, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn scale_y_column_in_its_range(
        self,
        scale: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.scale_y_column_in_its_range(scale, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn scale_x_column_in_its_range(
        self,
        scale: f64,
        from: f64,
        to: f64,
    ) -> Result<Self, TGADomainError> {
        let dataset = self.dataset.scale_x_column_in_its_range(scale, from, to)?;
        Ok(Self {
            dataset,
            meta: self.meta,
        })
    }

    pub fn history_of_operations(&self) -> Vec<OperationRecord> {
        self.dataset
            .history_of_operations
            .vector_of_operations
            .clone()
    }

    pub fn operations_on_column(&self, col: &str) -> Vec<OperationRecord> {
        self.dataset.operations_on_column(col)
    }

    pub fn get_column_history(&self, col: &str) -> ColumnHistory {
        self.dataset.get_column_history(col)
    }

    /// Human-readable provenance chain for one column.
    pub fn column_provenance_text(&self, col: &str) -> Option<String> {
        self.dataset.column_provenance_text(col)
    }

    pub fn validate_for_kinetics(&self) -> Result<(), TGADomainError> {
        self.dataset
            .validate_required_columns(&["temperature", "alpha", "dalpha_dt"])
    }
}