Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rand crate and usage #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
150 changes: 127 additions & 23 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 4 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ Backed by a generic k-means implementation offered as a standalone library."""
default = ["app"]

# Features required for building the binary
app = [
"image",
"palette_color",
"structopt",
]
app = ["image", "palette_color", "structopt"]

# Enable `palette` color types
palette_color = ["palette", "num-traits", "fxhash"]
Expand Down Expand Up @@ -51,12 +47,12 @@ features = ["std"]
optional = true

[dependencies.rand]
version = "0.8.5"
version = "0.9.0"
default-features = false
features = ["std"]
features = ["thread_rng"]

[dependencies.rand_chacha]
version = "0.3.1"
version = "0.9.0"
default-features = false

[dependencies.structopt]
Expand Down
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
format_strings = true
reorder_imports = true
imports_granularity = "Module"
group_imports = "StdExternalCrate"
max_width = 100
21 changes: 10 additions & 11 deletions src/colors/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
use num_traits::{Float, FromPrimitive, Zero};
#[cfg(feature = "palette_color")]
use palette::{rgb::Rgb, rgb::Rgba, Lab};

use rand::Rng;

use crate::kmeans::{Calculate, Hamerly, HamerlyCentroids, HamerlyPoint};
Expand All @@ -18,7 +17,7 @@ where
for color in lab.iter() {
let mut index = 0;
let mut diff;
let mut min = core::f32::MAX;
let mut min = f32::MAX;
for (idx, cent) in centroids.iter().enumerate() {
diff = Self::difference(color, cent);
if diff < min {
Expand Down Expand Up @@ -68,9 +67,9 @@ where
#[inline]
fn create_random(rng: &mut impl Rng) -> Lab<Wp, T> {
Lab::<Wp, T>::new(
T::from_f64(rng.gen_range(0.0..=100.0)).unwrap(),
T::from_f64(rng.gen_range(-128.0..=127.0)).unwrap(),
T::from_f64(rng.gen_range(-128.0..=127.0)).unwrap(),
T::from_f64(rng.random_range(0.0..=100.0)).unwrap(),
T::from_f64(rng.random_range(-128.0..=127.0)).unwrap(),
T::from_f64(rng.random_range(-128.0..=127.0)).unwrap(),
)
}

Expand All @@ -95,7 +94,7 @@ where
for color in rgb.iter() {
let mut index = 0;
let mut diff;
let mut min = core::f32::MAX;
let mut min = f32::MAX;
for (idx, cent) in centroids.iter().enumerate() {
diff = Self::difference(color, cent);
if diff < min {
Expand Down Expand Up @@ -145,9 +144,9 @@ where
#[inline]
fn create_random(rng: &mut impl Rng) -> Rgb<S, T> {
Rgb::<S, T>::new(
T::from_f64(rng.gen_range(0.0..=1.0)).unwrap(),
T::from_f64(rng.gen_range(0.0..=1.0)).unwrap(),
T::from_f64(rng.gen_range(0.0..=1.0)).unwrap(),
T::from_f64(rng.random_range(0.0..=1.0)).unwrap(),
T::from_f64(rng.random_range(0.0..=1.0)).unwrap(),
T::from_f64(rng.random_range(0.0..=1.0)).unwrap(),
)
}

Expand Down Expand Up @@ -222,7 +221,7 @@ where
continue;
}

let mut min1 = Self::difference(val, centers.centroids.get(0).unwrap());
let mut min1 = Self::difference(val, centers.centroids.first().unwrap());
let mut min2 = f32::MAX;
let mut c1 = 0;
for j in 1..centers.centroids.len() {
Expand Down Expand Up @@ -355,7 +354,7 @@ where
continue;
}

let mut min1 = Self::difference(val, centers.centroids.get(0).unwrap());
let mut min1 = Self::difference(val, centers.centroids.first().unwrap());
let mut min2 = f32::MAX;
let mut c1 = 0;
for j in 1..centers.centroids.len() {
Expand Down
2 changes: 1 addition & 1 deletion src/kmeans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<C: Calculate> Kmeans<C> {
/// Create a new `Kmeans` struct to contain k-means results.
pub fn new() -> Self {
Kmeans {
score: core::f32::MAX,
score: f32::MAX,
centroids: Vec::new(),
indices: Vec::new(),
}
Expand Down
7 changes: 4 additions & 3 deletions src/plus_plus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rand::distributions::{Distribution, WeightedIndex};
use rand::distr::weighted::WeightedIndex;
use rand::distr::Distribution;
use rand::Rng;

/// k-means++ centroid initialization.
Expand Down Expand Up @@ -26,7 +27,7 @@ pub fn init_plus_plus<C: crate::Calculate + Clone>(
let mut weights: Vec<f32> = (0..len).map(|_| 0.0).collect();

// Choose first centroid at random, uniform sampling from input buffer
centroids.push(buf.get(rng.gen_range(0..len)).unwrap().to_owned());
centroids.push(buf.get(rng.random_range(0..len)).unwrap().to_owned());

// Pick a new centroid with weighted probability of `D(x)^2 / sum(D(x)^2)`,
// where `D(x)^2` is the distance to the closest centroid
Expand All @@ -35,7 +36,7 @@ pub fn init_plus_plus<C: crate::Calculate + Clone>(
let mut sum = 0.0;
for (b, dist) in buf.iter().zip(weights.iter_mut()) {
let mut diff;
let mut min = core::f32::MAX;
let mut min = f32::MAX;
for cent in centroids.iter() {
diff = C::difference(b, cent);
if diff < min {
Expand Down