kizzasi 0.2.1

Autoregressive General-Purpose Signal Predictor (AGSP) - Neuro-Symbolic Architecture for continuous signal streams
Documentation
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
//! Plugin system for extending Kizzasi functionality
//!
//! This module provides a trait-based plugin architecture that allows
//! users to hook into the prediction pipeline for custom preprocessing,
//! postprocessing, logging, metrics collection, and more.

use crate::error::{KizzasiError, KizzasiResult};
use scirs2_core::ndarray::Array1;
use std::any::Any;
use std::fmt;

/// Plugin execution phase
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PluginPhase {
    /// Before prediction (preprocessing)
    PreProcess,
    /// After prediction (postprocessing)
    PostProcess,
    /// On prediction error
    OnError,
    /// On state reset
    OnReset,
}

/// Context provided to plugins during execution
#[derive(Debug, Clone)]
pub struct PluginContext {
    /// Current prediction step number
    pub step: usize,
    /// Model input dimension
    pub input_dim: usize,
    /// Model output dimension
    pub output_dim: usize,
    /// Optional user data
    pub user_data: Option<String>,
}

impl PluginContext {
    /// Create a new plugin context
    pub fn new(step: usize, input_dim: usize, output_dim: usize) -> Self {
        Self {
            step,
            input_dim,
            output_dim,
            user_data: None,
        }
    }

    /// Set user data
    pub fn with_user_data(mut self, data: String) -> Self {
        self.user_data = Some(data);
        self
    }
}

/// Core plugin trait
///
/// Implement this trait to create custom plugins that hook into
/// the Kizzasi prediction pipeline.
///
/// # Example
///
/// ```rust,ignore
/// use kizzasi::plugin::{Plugin, PluginContext, PluginPhase};
///
/// struct LoggingPlugin {
///     name: String,
/// }
///
/// impl Plugin for LoggingPlugin {
///     fn name(&self) -> &str {
///         &self.name
///     }
///
///     fn on_pre_process(
///         &mut self,
///         input: &Array1<f32>,
///         ctx: &PluginContext,
///     ) -> KizzasiResult<()> {
///         println!("Step {}: Input = {:?}", ctx.step, input);
///         Ok(())
///     }
/// }
/// ```
pub trait Plugin: Send {
    /// Get the plugin name
    fn name(&self) -> &str;

    /// Get plugin description
    fn description(&self) -> &str {
        "No description"
    }

    /// Check if plugin is enabled
    fn is_enabled(&self) -> bool {
        true
    }

    /// Called before prediction (preprocessing)
    fn on_pre_process(&mut self, _input: &Array1<f32>, _ctx: &PluginContext) -> KizzasiResult<()> {
        Ok(())
    }

    /// Transform input before prediction (allows modification)
    fn transform_input(
        &mut self,
        input: Array1<f32>,
        _ctx: &PluginContext,
    ) -> KizzasiResult<Array1<f32>> {
        Ok(input)
    }

    /// Called after prediction (postprocessing)
    fn on_post_process(
        &mut self,
        _input: &Array1<f32>,
        _output: &Array1<f32>,
        _ctx: &PluginContext,
    ) -> KizzasiResult<()> {
        Ok(())
    }

    /// Transform output after prediction (allows modification)
    fn transform_output(
        &mut self,
        output: Array1<f32>,
        _ctx: &PluginContext,
    ) -> KizzasiResult<Array1<f32>> {
        Ok(output)
    }

    /// Called on prediction error
    fn on_error(&mut self, _error: &KizzasiError, _ctx: &PluginContext) -> KizzasiResult<()> {
        Ok(())
    }

    /// Called on state reset
    fn on_reset(&mut self, _ctx: &PluginContext) -> KizzasiResult<()> {
        Ok(())
    }

    /// Get plugin as Any for downcasting
    fn as_any(&self) -> &dyn Any;

    /// Get mutable plugin as Any for downcasting
    fn as_any_mut(&mut self) -> &mut dyn Any;
}

/// Plugin manager for organizing and executing plugins
pub struct PluginManager {
    plugins: Vec<Box<dyn Plugin>>,
    step_counter: usize,
}

impl PluginManager {
    /// Create a new plugin manager
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
            step_counter: 0,
        }
    }

    /// Add a plugin
    pub fn add_plugin(&mut self, plugin: Box<dyn Plugin>) {
        self.plugins.push(plugin);
    }

    /// Remove a plugin by name
    pub fn remove_plugin(&mut self, name: &str) -> Option<Box<dyn Plugin>> {
        if let Some(pos) = self.plugins.iter().position(|p| p.name() == name) {
            Some(self.plugins.remove(pos))
        } else {
            None
        }
    }

    /// Get a plugin by name
    pub fn get_plugin(&self, name: &str) -> Option<&dyn Plugin> {
        self.plugins
            .iter()
            .find(|p| p.name() == name)
            .map(|p| p.as_ref())
    }

    /// Get a mutable plugin by name
    pub fn get_plugin_mut(&mut self, name: &str) -> Option<&mut Box<dyn Plugin>> {
        self.plugins.iter_mut().find(|p| p.name() == name)
    }

    /// Execute pre-process hooks
    pub fn execute_pre_process(
        &mut self,
        input: &Array1<f32>,
        input_dim: usize,
        output_dim: usize,
    ) -> KizzasiResult<()> {
        let ctx = PluginContext::new(self.step_counter, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                plugin.on_pre_process(input, &ctx)?;
            }
        }
        Ok(())
    }

    /// Transform input through all plugins
    pub fn transform_input(
        &mut self,
        mut input: Array1<f32>,
        input_dim: usize,
        output_dim: usize,
    ) -> KizzasiResult<Array1<f32>> {
        let ctx = PluginContext::new(self.step_counter, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                input = plugin.transform_input(input, &ctx)?;
            }
        }
        Ok(input)
    }

    /// Execute post-process hooks
    pub fn execute_post_process(
        &mut self,
        input: &Array1<f32>,
        output: &Array1<f32>,
        input_dim: usize,
        output_dim: usize,
    ) -> KizzasiResult<()> {
        let ctx = PluginContext::new(self.step_counter, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                plugin.on_post_process(input, output, &ctx)?;
            }
        }
        self.step_counter += 1;
        Ok(())
    }

    /// Transform output through all plugins
    pub fn transform_output(
        &mut self,
        mut output: Array1<f32>,
        input_dim: usize,
        output_dim: usize,
    ) -> KizzasiResult<Array1<f32>> {
        let ctx = PluginContext::new(self.step_counter, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                output = plugin.transform_output(output, &ctx)?;
            }
        }
        Ok(output)
    }

    /// Execute error hooks
    pub fn execute_on_error(
        &mut self,
        error: &KizzasiError,
        input_dim: usize,
        output_dim: usize,
    ) -> KizzasiResult<()> {
        let ctx = PluginContext::new(self.step_counter, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                plugin.on_error(error, &ctx)?;
            }
        }
        Ok(())
    }

    /// Execute reset hooks
    pub fn execute_on_reset(&mut self, input_dim: usize, output_dim: usize) -> KizzasiResult<()> {
        let ctx = PluginContext::new(0, input_dim, output_dim);
        for plugin in &mut self.plugins {
            if plugin.is_enabled() {
                plugin.on_reset(&ctx)?;
            }
        }
        self.step_counter = 0;
        Ok(())
    }

    /// Get number of plugins
    pub fn len(&self) -> usize {
        self.plugins.len()
    }

    /// Check if empty
    pub fn is_empty(&self) -> bool {
        self.plugins.is_empty()
    }

    /// List all plugin names
    pub fn plugin_names(&self) -> Vec<&str> {
        self.plugins.iter().map(|p| p.name()).collect()
    }
}

impl Default for PluginManager {
    fn default() -> Self {
        Self::new()
    }
}

impl fmt::Debug for PluginManager {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PluginManager")
            .field("plugin_count", &self.plugins.len())
            .field("step_counter", &self.step_counter)
            .field("plugins", &self.plugin_names())
            .finish()
    }
}

// ============================================================================
// Built-in Plugins
// ============================================================================

/// Logging plugin that prints prediction information
pub struct LoggingPlugin {
    name: String,
    enabled: bool,
}

impl LoggingPlugin {
    /// Create a new logging plugin
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            enabled: true,
        }
    }

    /// Enable or disable the plugin
    pub fn set_enabled(&mut self, enabled: bool) {
        self.enabled = enabled;
    }
}

impl Plugin for LoggingPlugin {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Logs prediction inputs and outputs"
    }

    fn is_enabled(&self) -> bool {
        self.enabled
    }

    fn on_pre_process(&mut self, input: &Array1<f32>, ctx: &PluginContext) -> KizzasiResult<()> {
        println!("[{}] Step {}: Input = {:?}", self.name, ctx.step, input);
        Ok(())
    }

    fn on_post_process(
        &mut self,
        _input: &Array1<f32>,
        output: &Array1<f32>,
        ctx: &PluginContext,
    ) -> KizzasiResult<()> {
        println!("[{}] Step {}: Output = {:?}", self.name, ctx.step, output);
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

/// Statistics collection plugin
pub struct StatsPlugin {
    name: String,
    enabled: bool,
    prediction_count: usize,
    total_input_magnitude: f32,
    total_output_magnitude: f32,
}

impl StatsPlugin {
    /// Create a new statistics plugin
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            enabled: true,
            prediction_count: 0,
            total_input_magnitude: 0.0,
            total_output_magnitude: 0.0,
        }
    }

    /// Get statistics
    pub fn stats(&self) -> (usize, f32, f32) {
        (
            self.prediction_count,
            if self.prediction_count > 0 {
                self.total_input_magnitude / self.prediction_count as f32
            } else {
                0.0
            },
            if self.prediction_count > 0 {
                self.total_output_magnitude / self.prediction_count as f32
            } else {
                0.0
            },
        )
    }

    /// Reset statistics
    pub fn reset_stats(&mut self) {
        self.prediction_count = 0;
        self.total_input_magnitude = 0.0;
        self.total_output_magnitude = 0.0;
    }
}

impl Plugin for StatsPlugin {
    fn name(&self) -> &str {
        &self.name
    }

    fn description(&self) -> &str {
        "Collects prediction statistics"
    }

    fn is_enabled(&self) -> bool {
        self.enabled
    }

    fn on_post_process(
        &mut self,
        input: &Array1<f32>,
        output: &Array1<f32>,
        _ctx: &PluginContext,
    ) -> KizzasiResult<()> {
        self.prediction_count += 1;
        self.total_input_magnitude += input.iter().map(|x| x.abs()).sum::<f32>();
        self.total_output_magnitude += output.iter().map(|x| x.abs()).sum::<f32>();
        Ok(())
    }

    fn on_reset(&mut self, _ctx: &PluginContext) -> KizzasiResult<()> {
        self.reset_stats();
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }

    fn as_any_mut(&mut self) -> &mut dyn Any {
        self
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_plugin_manager_creation() {
        let manager = PluginManager::new();
        assert_eq!(manager.len(), 0);
        assert!(manager.is_empty());
    }

    #[test]
    fn test_add_remove_plugin() {
        let mut manager = PluginManager::new();

        let plugin = Box::new(LoggingPlugin::new("test_logger"));
        manager.add_plugin(plugin);

        assert_eq!(manager.len(), 1);
        assert!(!manager.is_empty());

        let removed = manager.remove_plugin("test_logger");
        assert!(removed.is_some());
        assert_eq!(manager.len(), 0);
    }

    #[test]
    fn test_logging_plugin() {
        let mut plugin = LoggingPlugin::new("test");
        assert_eq!(plugin.name(), "test");
        assert!(plugin.is_enabled());

        let input = Array1::from_vec(vec![0.1, 0.2, 0.3]);
        let ctx = PluginContext::new(0, 3, 3);

        plugin.on_pre_process(&input, &ctx).unwrap();
        plugin.on_post_process(&input, &input, &ctx).unwrap();
    }

    #[test]
    fn test_stats_plugin() {
        let mut plugin = StatsPlugin::new("stats");

        let input = Array1::from_vec(vec![1.0, 2.0, 3.0]);
        let output = Array1::from_vec(vec![0.5, 1.0, 1.5]);
        let ctx = PluginContext::new(0, 3, 3);

        plugin.on_post_process(&input, &output, &ctx).unwrap();

        let (count, avg_in, avg_out) = plugin.stats();
        assert_eq!(count, 1);
        assert_eq!(avg_in, 6.0); // 1 + 2 + 3
        assert_eq!(avg_out, 3.0); // 0.5 + 1.0 + 1.5
    }

    #[test]
    fn test_plugin_manager_execution() {
        let mut manager = PluginManager::new();
        manager.add_plugin(Box::new(StatsPlugin::new("stats")));

        let input = Array1::from_vec(vec![0.1, 0.2]);
        let output = Array1::from_vec(vec![0.3, 0.4]);

        manager.execute_pre_process(&input, 2, 2).unwrap();
        manager.execute_post_process(&input, &output, 2, 2).unwrap();

        let plugin = manager.get_plugin("stats").unwrap();
        let stats_plugin = plugin.as_any().downcast_ref::<StatsPlugin>().unwrap();
        let (count, _, _) = stats_plugin.stats();
        assert_eq!(count, 1);
    }

    #[test]
    fn test_plugin_reset() {
        let mut manager = PluginManager::new();
        manager.add_plugin(Box::new(StatsPlugin::new("stats")));

        let input = Array1::from_vec(vec![0.1]);
        let output = Array1::from_vec(vec![0.2]);

        manager.execute_post_process(&input, &output, 1, 1).unwrap();
        manager.execute_on_reset(1, 1).unwrap();

        let plugin = manager.get_plugin("stats").unwrap();
        let stats_plugin = plugin.as_any().downcast_ref::<StatsPlugin>().unwrap();
        let (count, _, _) = stats_plugin.stats();
        assert_eq!(count, 0);
    }
}