Skip to content

Commit

Permalink
feat: add log crate; make print statement a debug log
Browse files Browse the repository at this point in the history
  • Loading branch information
lily-mosquitoes committed Jul 22, 2023
1 parent 211a3e8 commit 2071911
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 37 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "image-recovery"
version = "0.1.0"
version = "0.1.1"
edition = "2021"
authors = ["Lílian Ferreira de Freitas <lily.mosquitoes@gmail.com>", "Emilia L. K. Blåsten <emilia.blasten@iki.fi>"]
description = "Image recovery algorithms, implemented in Rust."
Expand All @@ -11,6 +11,7 @@ documentation = "https://docs.rs/image-recovery"
categories = ["mathematics"]

[dependencies]
log = "0.4"
image = "0.24"
ndarray = { version = "0.15", features = ["matrixmultiply-threading"] }

Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,16 @@
#![feature(test)]
extern crate test;

mod _impl;
pub mod array_ops;
pub mod img;
pub mod utils;
pub mod solvers;
mod _impl;
#[cfg(test)] mod tests;
#[cfg(test)]
mod tests;
pub mod utils;

pub use ndarray::Array2;
pub use image;
pub use ndarray::Array2;

/// struct representing an RGB image as 3 matrices,
/// one for each color channel
Expand Down
55 changes: 34 additions & 21 deletions src/solvers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@

//! Implementation of algorithms for image recovery.
use ndarray::Array2;
use crate::{
RgbMatrices,
array_ops::{Derivative, Power},
utils,
utils, RgbMatrices,
};
use ndarray::Array2;

/// single channel denoising algorithm.
///
Expand All @@ -42,7 +41,15 @@ use crate::{
///
/// `max_iter` and `convergence_threshold` bound the runtime of the
/// algorithm, i.e. it runs until `convergence_threshold < norm(current - previous) / norm(previous)` or `max_iter` is hit.
pub fn denoise(input: &Array2<f64>, lambda: f64, mut tau: f64, mut sigma: f64, gamma: f64, max_iter: u32, convergence_threshold: f64) -> Array2<f64> {
pub fn denoise(
input: &Array2<f64>,
lambda: f64,
mut tau: f64,
mut sigma: f64,
gamma: f64,
max_iter: u32,
convergence_threshold: f64,
) -> Array2<f64> {
// primal variable (two copies, for storing value of iteration n-1)
let mut current = input.to_owned();
let mut previous: Array2<f64>;
Expand All @@ -68,16 +75,15 @@ pub fn denoise(input: &Array2<f64>, lambda: f64, mut tau: f64, mut sigma: f64, g
let mut iter: u32 = 1;
loop {
// update the dual variable
(dual_a, dual_b) = utils
::ball_projection(&f(dual_a, current_bar.dx(), sigma), &f(dual_b, current_bar.dy(), sigma));
(dual_a, dual_b) = utils::ball_projection(
&f(dual_a, current_bar.dx(), sigma),
&f(dual_b, current_bar.dy(), sigma),
);

// update the primal variable
// save it first
previous = current.to_owned();
current = weighted_average(&current - (tau *
k_star(&dual_a, &dual_b)),
tau,
lambda);
current = weighted_average(&current - (tau * k_star(&dual_a, &dual_b)), tau, lambda);

// update theta
theta = 1_f64 / (1_f64 + (2_f64 * gamma * tau));
Expand All @@ -92,8 +98,8 @@ pub fn denoise(input: &Array2<f64>, lambda: f64, mut tau: f64, mut sigma: f64, g
// check for convergence or max_iter iterations
let c = norm(&(&current - &previous)) / norm(&previous);
if c < convergence_threshold || iter >= max_iter {
println!("returned at iter n {}", iter);
break
log::debug!("returned at iter n {}", iter);
break;
}
iter += 1;
}
Expand Down Expand Up @@ -121,7 +127,15 @@ pub fn denoise(input: &Array2<f64>, lambda: f64, mut tau: f64, mut sigma: f64, g
///
/// `max_iter` and `convergence_threshold` bound the runtime of the
/// algorithm, i.e. it runs until `convergence_threshold < norm(current - previous) / norm(previous)` or `max_iter` is hit.
pub fn denoise_multichannel(input: &RgbMatrices, lambda: f64, mut tau: f64, mut sigma: f64, gamma: f64, max_iter: u32, convergence_threshold: f64) -> RgbMatrices {
pub fn denoise_multichannel(
input: &RgbMatrices,
lambda: f64,
mut tau: f64,
mut sigma: f64,
gamma: f64,
max_iter: u32,
convergence_threshold: f64,
) -> RgbMatrices {
// primal variable (two copies, for storing value of iteration n-1)
let mut current = input.to_owned();
let mut previous: RgbMatrices;
Expand All @@ -147,16 +161,15 @@ pub fn denoise_multichannel(input: &RgbMatrices, lambda: f64, mut tau: f64, mut
let mut iter: u32 = 1;
loop {
// update the dual variable
(dual_a, dual_b) = utils
::ball_projection_multichannel(&f(dual_a, current_bar.dx(), sigma), &f(dual_b, current_bar.dy(), sigma));
(dual_a, dual_b) = utils::ball_projection_multichannel(
&f(dual_a, current_bar.dx(), sigma),
&f(dual_b, current_bar.dy(), sigma),
);

// update the primal variable
// save it first
previous = current.to_owned();
current = weighted_average(&current - (tau *
k_star(&dual_a, &dual_b)),
tau,
lambda);
current = weighted_average(&current - (tau * k_star(&dual_a, &dual_b)), tau, lambda);

// update theta
theta = 1_f64 / (1_f64 + (2_f64 * gamma * tau));
Expand All @@ -171,8 +184,8 @@ pub fn denoise_multichannel(input: &RgbMatrices, lambda: f64, mut tau: f64, mut
// check for convergence or max_iter iterations
let c = norm(&(&current - &previous)) / norm(&previous);
if c < convergence_threshold || iter >= max_iter {
println!("returned at iter n {}", iter);
break
log::debug!("returned at iter n {}", iter);
break;
}
iter += 1;
}
Expand Down
19 changes: 8 additions & 11 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,12 @@

//! Utility functions for matrices (`ndarray::Array2<f64>`).
use crate::{array_ops::Power, RgbMatrices};
use ndarray::Array2;
use crate::{
RgbMatrices,
array_ops::{Power},
};

/// length of vectors given two matrices, unchecked for size.
pub fn len_of_vectors(a: &Array2<f64>, b: &Array2<f64>) -> Array2<f64> {
(a.squared() + b.squared())
.map(|x| x.sqrt())
(a.squared() + b.squared()).map(|x| x.sqrt())
}

/// length of vectors given two RGB matrices, unchecked for size.
Expand All @@ -47,16 +43,17 @@ pub fn len_of_vectors_multichannel(a: &RgbMatrices, b: &RgbMatrices) -> Array2<f

/// the projection of vectors from two matrices into a 2D ball (-1, 1), unchecked for size.
pub fn ball_projection(a: &Array2<f64>, b: &Array2<f64>) -> (Array2<f64>, Array2<f64>) {
let max = len_of_vectors(a, b)
.map(|x| 1_f64.max(*x));
let max = len_of_vectors(a, b).map(|x| 1_f64.max(*x));

(a / &max, b / &max)
}

/// the projection of vectors from two RGB matrices into a 2D ball (-1, 1), unchecked for size.
pub fn ball_projection_multichannel(a: &RgbMatrices, b: &RgbMatrices) -> (RgbMatrices, RgbMatrices) {
let max = len_of_vectors_multichannel(a, b)
.map(|x| 1_f64.max(*x));
pub fn ball_projection_multichannel(
a: &RgbMatrices,
b: &RgbMatrices,
) -> (RgbMatrices, RgbMatrices) {
let max = len_of_vectors_multichannel(a, b).map(|x| 1_f64.max(*x));

(a / &max, b / &max)
}

0 comments on commit 2071911

Please # to comment.