content-extractor-rl 0.1.0

RL-based article extraction from HTML using Deep Q-Networks and heuristic fallback
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
//! Training visualization and plotting using plotters library
// ============================================================================
// FILE: crates/content-extractor-rl/src/plotting.rs
// ============================================================================

use crate::{Result, training::TrainingMetrics};
use plotters::prelude::*;
use std::path::Path;
use tracing::info;

/// Plot configuration
pub struct PlotConfig {
    pub width: u32,
    pub height: u32,
    pub dpi: u32,
}

impl Default for PlotConfig {
    fn default() -> Self {
        Self {
            width: 1600,
            height: 1200,
            dpi: 150,
        }
    }
}

/// Training plots generator
pub struct TrainingPlotter {
    config: PlotConfig,
}

impl TrainingPlotter {
    /// Create new plotter with default config
    pub fn new() -> Self {
        Self {
            config: PlotConfig::default(),
        }
    }

    /// Create plotter with custom config
    pub fn with_config(config: PlotConfig) -> Self {
        Self { config }
    }

    /// Generate comprehensive training plots
    pub fn plot_training_results(&self, metrics: &TrainingMetrics, output_path: &Path) -> Result<()> {
        info!("Generating training plots to: {}", output_path.display());

        let root = BitMapBackend::new(
            output_path,
            (self.config.width, self.config.height)
        ).into_drawing_area();

        root.fill(&WHITE)
            .map_err(|e| crate::ExtractionError::ModelError(format!("Plot fill error: {}", e)))?;

        // Split into 2x2 grid
        let areas = root.split_evenly((2, 2));

        // Plot 1: Episode Rewards
        self.plot_rewards(&areas[0], &metrics.episode_rewards)?;

        // Plot 2: Episode Quality
        self.plot_quality(&areas[1], &metrics.episode_qualities)?;

        // Plot 3: Reward Distribution
        self.plot_reward_distribution(&areas[2], &metrics.episode_rewards)?;

        // Plot 4: Quality Distribution
        self.plot_quality_distribution(&areas[3], &metrics.episode_qualities)?;

        root.present()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Plot present error: {}", e)))?;

        info!("Training plots saved successfully");
        Ok(())
    }

    /// Plot episode rewards over time with moving average
    fn plot_rewards<DB: DrawingBackend>(
        &self,
        area: &DrawingArea<DB, plotters::coord::Shift>,
        rewards: &[f32],
    ) -> Result<()>
    where
        DB::ErrorType: 'static,
    {
        if rewards.is_empty() {
            return Ok(());
        }

        let max_episodes = rewards.len();
        let max_reward = rewards.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let min_reward = rewards.iter().copied().fold(f32::INFINITY, f32::min);

        let mut chart = ChartBuilder::on(area)
            .caption("Episode Rewards", ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(50)
            .build_cartesian_2d(0..max_episodes, min_reward..max_reward)
            .map_err(|e| crate::ExtractionError::ModelError(format!("Chart build error: {}", e)))?;

        chart.configure_mesh()
            .x_desc("Episode")
            .y_desc("Reward")
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Mesh error: {}", e)))?;

        // Plot raw rewards
        chart.draw_series(LineSeries::new(
            rewards.iter().enumerate().map(|(i, &r)| (i, r)),
            &BLUE.mix(0.5),
        ))
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
            .label("Raw")
            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], BLUE));

        // Plot moving average
        if rewards.len() > 100 {
            let window = rewards.len().min(100);
            let moving_avg = self.calculate_moving_average(rewards, window);

            chart.draw_series(LineSeries::new(
                moving_avg.into_iter()
                    .enumerate()
                    .map(|(i, avg)| (i + window - 1, avg)),
                &RED,
            ))
                .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
                .label(format!("MA({})", window))
                .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));
        }

        chart.configure_series_labels()
            .background_style(WHITE.mix(0.8))
            .border_style(BLACK)
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Legend error: {}", e)))?;

        Ok(())
    }

    /// Plot episode quality over time with moving average
    fn plot_quality<DB: DrawingBackend>(
        &self,
        area: &DrawingArea<DB, plotters::coord::Shift>,
        qualities: &[f32],
    ) -> Result<()>
    where
        DB::ErrorType: 'static,
    {
        if qualities.is_empty() {
            return Ok(());
        }

        let max_episodes = qualities.len();
        let max_quality = qualities.iter().copied().fold(f32::NEG_INFINITY, f32::max);

        let mut chart = ChartBuilder::on(area)
            .caption("Episode Quality", ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(50)
            .build_cartesian_2d(0..max_episodes, 0.0..max_quality.max(1.0))
            .map_err(|e| crate::ExtractionError::ModelError(format!("Chart build error: {}", e)))?;

        chart.configure_mesh()
            .x_desc("Episode")
            .y_desc("Quality Score")
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Mesh error: {}", e)))?;

        // Plot raw quality
        chart.draw_series(LineSeries::new(
            qualities.iter().enumerate().map(|(i, &q)| (i, q)),
            &GREEN.mix(0.5),
        ))
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
            .label("Raw")
            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], GREEN));

        // Plot moving average
        if qualities.len() > 100 {
            let window = qualities.len().min(100);
            let moving_avg = self.calculate_moving_average(qualities, window);

            chart.draw_series(LineSeries::new(
                moving_avg.into_iter()
                    .enumerate()
                    .map(|(i, avg)| (i + window - 1, avg)),
                &RED,
            ))
                .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
                .label(format!("MA({})", window))
                .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));
        }

        chart.configure_series_labels()
            .background_style(WHITE.mix(0.8))
            .border_style(BLACK)
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Legend error: {}", e)))?;

        Ok(())
    }

    /// Plot reward distribution histogram
    fn plot_reward_distribution<DB: DrawingBackend>(
        &self,
        area: &DrawingArea<DB, plotters::coord::Shift>,
        rewards: &[f32],
    ) -> Result<()>
    where
        DB::ErrorType: 'static,
    {
        if rewards.is_empty() {
            return Ok(());
        }

        let max_reward = rewards.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let min_reward = rewards.iter().copied().fold(f32::INFINITY, f32::min);

        // Calculate histogram
        let n_bins = 50;
        let bin_width = (max_reward - min_reward) / n_bins as f32;
        let mut histogram = vec![0usize; n_bins];

        for &reward in rewards {
            let bin = ((reward - min_reward) / bin_width).floor() as usize;
            let bin = bin.min(n_bins - 1);
            histogram[bin] += 1;
        }

        let max_count = *histogram.iter().max().unwrap_or(&1);

        let mut chart = ChartBuilder::on(area)
            .caption("Reward Distribution", ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(50)
            .build_cartesian_2d(min_reward..max_reward, 0..max_count)
            .map_err(|e| crate::ExtractionError::ModelError(format!("Chart build error: {}", e)))?;

        chart.configure_mesh()
            .x_desc("Reward")
            .y_desc("Frequency")
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Mesh error: {}", e)))?;

        // Draw histogram bars
        chart.draw_series(
            histogram.iter().enumerate().map(|(i, &count)| {
                let x0 = min_reward + i as f32 * bin_width;
                let x1 = x0 + bin_width;
                Rectangle::new([(x0, 0), (x1, count)], BLUE.mix(0.7).filled())
            })
        )
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?;

        // Draw mean line
        let mean = rewards.iter().sum::<f32>() / rewards.len() as f32;
        chart.draw_series(LineSeries::new(
            vec![(mean, 0), (mean, max_count)],
            RED.stroke_width(2),
        ))
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
            .label(format!("Mean: {:.3}", mean))
            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));

        chart.configure_series_labels()
            .background_style(WHITE.mix(0.8))
            .border_style(BLACK)
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Legend error: {}", e)))?;

        Ok(())
    }

    /// Plot quality distribution histogram
    fn plot_quality_distribution<DB: DrawingBackend>(
        &self,
        area: &DrawingArea<DB, plotters::coord::Shift>,
        qualities: &[f32],
    ) -> Result<()>
    where
        DB::ErrorType: 'static,
    {
        if qualities.is_empty() {
            return Ok(());
        }

        let max_quality = qualities.iter().copied().fold(f32::NEG_INFINITY, f32::max);
        let min_quality = qualities.iter().copied().fold(f32::INFINITY, f32::min);

        // Calculate histogram
        let n_bins = 50;
        let bin_width = (max_quality - min_quality).max(0.01) / n_bins as f32;
        let mut histogram = vec![0usize; n_bins];

        for &quality in qualities {
            let bin = ((quality - min_quality) / bin_width).floor() as usize;
            let bin = bin.min(n_bins - 1);
            histogram[bin] += 1;
        }

        let max_count = *histogram.iter().max().unwrap_or(&1);

        let mut chart = ChartBuilder::on(area)
            .caption("Quality Distribution", ("sans-serif", 30).into_font())
            .margin(10)
            .x_label_area_size(30)
            .y_label_area_size(50)
            .build_cartesian_2d(min_quality..max_quality.max(1.0), 0..max_count)
            .map_err(|e| crate::ExtractionError::ModelError(format!("Chart build error: {}", e)))?;

        chart.configure_mesh()
            .x_desc("Quality Score")
            .y_desc("Frequency")
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Mesh error: {}", e)))?;

        // Draw histogram bars
        chart.draw_series(
            histogram.iter().enumerate().map(|(i, &count)| {
                let x0 = min_quality + i as f32 * bin_width;
                let x1 = x0 + bin_width;
                Rectangle::new([(x0, 0), (x1, count)], GREEN.mix(0.7).filled())
            })
        )
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?;

        // Draw mean line
        let mean = qualities.iter().sum::<f32>() / qualities.len() as f32;
        chart.draw_series(LineSeries::new(
            vec![(mean, 0), (mean, max_count)],
            RED.stroke_width(2),
        ))
            .map_err(|e| crate::ExtractionError::ModelError(format!("Series error: {}", e)))?
            .label(format!("Mean: {:.3}", mean))
            .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], RED));

        chart.configure_series_labels()
            .background_style(WHITE.mix(0.8))
            .border_style(BLACK)
            .draw()
            .map_err(|e| crate::ExtractionError::ModelError(format!("Legend error: {}", e)))?;

        Ok(())
    }

    /// Calculate moving average
    fn calculate_moving_average(&self, data: &[f32], window: usize) -> Vec<f32> {
        let mut result = Vec::with_capacity(data.len() - window + 1);

        for i in window - 1..data.len() {
            let sum: f32 = data[i - window + 1..=i].iter().sum();
            result.push(sum / window as f32);
        }

        result
    }

    /// Generate plot periodically during training
    pub fn plot_intermediate(&self, metrics: &TrainingMetrics, output_path: &Path, episode: usize) -> Result<()> {
        let timestamped_path = output_path.parent().unwrap().join(
            format!("training_plot_ep{}.png", episode)
        );

        self.plot_training_results(metrics, &timestamped_path)
    }
}

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

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

    #[test]
    fn test_plot_generation() {
        let temp_dir = TempDir::new().unwrap();
        let plot_path = temp_dir.path().join("test_plot.png");

        let metrics = TrainingMetrics {
            episode_rewards: (0..100).map(|i| (i as f32 * 0.01) - 0.5).collect(),
            episode_qualities: (0..100).map(|i| i as f32 * 0.01).collect(),
            episode_losses: vec![],
            best_avg_quality: 0.9,
        };

        let plotter = TrainingPlotter::new();
        plotter.plot_training_results(&metrics, &plot_path).unwrap();

        assert!(plot_path.exists());
    }
}