Plots the learning curve from an optimization result.
(r: &OptResult<f64>, w: &Window)
| 298 | /// Plots the learning curve from an optimization result. |
| 299 | /// |
| 300 | pub fn plot_learning_curve(r: &OptResult<f64>, w: &Window) -> Result<(String, String), &'static str> { |
| 301 | |
| 302 | let errors = r.fvals.iter().map(|&(_, ref y)| y).cloned().collect::<Vec<f64>>(); |
| 303 | |
| 304 | let mut prfx = "/tmp/".to_string(); |
| 305 | prfx.extend(thread_rng().gen_ascii_chars().take(16)); |
| 306 | |
| 307 | let script_file = prfx.clone() + ".m"; |
| 308 | let image_file = prfx + ".png"; |
| 309 | |
| 310 | let r = builder() |
| 311 | .add_vector("y = $$", &errors) |
| 312 | .add("x = 1:size(y, 2)") |
| 313 | .add("plot(x, y, 'linewidth', 2)") |
| 314 | .add("grid on") |
| 315 | .add("title('learning curve')") |
| 316 | .add("xlabel('iteration')") |
| 317 | .add("ylabel('error')") |
| 318 | .add(&("print -r100 -dpng '".to_string() + &image_file + "'")) |
| 319 | .run(&script_file); |
| 320 | |
| 321 | match r { |
| 322 | Ok(_) => { |
| 323 | let img = RgbImage::from_file(&image_file); |
| 324 | match img { |
| 325 | Some(i) => { |
| 326 | w.show_image(&i); |
| 327 | Ok((script_file, image_file)) |
| 328 | }, |
| 329 | _ => Err("Could not load image.") |
| 330 | } |
| 331 | }, |
| 332 | _ => Err("Could not run octave.") |
| 333 | } |
| 334 | } |
no test coverage detected