From 57ccf8ff716416f7dbcba7f42a5e4369cea2fea0 Mon Sep 17 00:00:00 2001 From: Nigel Delaney Date: Fri, 19 Aug 2022 16:16:22 -0700 Subject: [PATCH] feat!: Update `strum` and `ordered-float` dependencies and change From into TryFrom for NotNan. (#491) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update `strum` crates Figure we'll need to change eventually. * Upgrade Ordered Float ordered-float ditched their From methods for NotNan and instead made them TryFrom (https://github.com/reem/rust-ordered-float/commit/39f76bb4910eeb4ad5c43f4111fefc550c258460). This change makes sense because From trait implementations generally shouldn’t panic, which the implementation did if the f64 was a NaN, and so TryFrom is more appropriate. This commit accounts for this change in the upgrade * Remove Deprecated Function Call `ndarray` has renamed `genrows()` to `rows()` * Formatting Fix * Update CHANGELOG * `From -> `TryFrom` The conversion from LogProb is not guaranteed to succeed, so we use `TryFrom` instead of `From`, as per the Rust docs: ```Note: The From trait must not fail. The From trait is intended for perfect conversions. If the conversion can fail or is not perfect, use TryFrom.``` Note this does change the API. * Apply suggestions from code review Co-authored-by: Adam Azarchs * Update CHANGELOG.md * Update Cargo.toml * Fix build issue. Co-authored-by: Adam Azarchs Co-authored-by: Johannes Köster --- CHANGELOG.md | 1 + Cargo.toml | 6 ++--- src/pattern_matching/pssm/mod.rs | 2 +- src/stats/probs/adaptive_integration.rs | 32 ++++++++++++------------- src/stats/probs/mod.rs | 10 ++++---- 5 files changed, 26 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b831ac79b..901458ec2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). + ## [0.41.0](https://www.github.com/rust-bio/rust-bio/compare/v0.40.0...v0.41.0) (2022-03-30) diff --git a/Cargo.toml b/Cargo.toml index 5fa08b276..0c062b622 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ serde_derive = "1.0" approx = ">=0.3, <0.6" custom_derive = "0.1" newtype_derive = "0.1" -ordered-float = "1.0" +ordered-float = "2.0" regex = { version = "1.3", default-features = false, features = ["std", "perf"] } multimap = ">=0.6, <0.9" fxhash = "0.2" @@ -45,8 +45,8 @@ statrs = ">= 0.11, < 0.16" bio-types = ">=0.11.0" pest = { version = "2", optional = true } pest_derive = { version = "2", optional = true } -strum = ">= 0.16, < 0.24" -strum_macros = ">= 0.16, < 0.24" +strum = ">= 0.16, < 0.25" +strum_macros = ">= 0.16, < 0.25" getset = ">=0.0.9, <0.2" enum-map = ">=0.6.4, <2" triple_accel = ">=0.3, <0.5" diff --git a/src/pattern_matching/pssm/mod.rs b/src/pattern_matching/pssm/mod.rs index 5569c3058..06771b1e1 100644 --- a/src/pattern_matching/pssm/mod.rs +++ b/src/pattern_matching/pssm/mod.rs @@ -296,7 +296,7 @@ pub trait Motif { let bits = Self::get_bits(); let scores = self.get_scores(); let mut tot = 0.0; - for row in scores.genrows() { + for row in scores.rows() { tot += bits - ent(row.iter()); } tot diff --git a/src/stats/probs/adaptive_integration.rs b/src/stats/probs/adaptive_integration.rs index 4555d8617..795b6e488 100644 --- a/src/stats/probs/adaptive_integration.rs +++ b/src/stats/probs/adaptive_integration.rs @@ -5,7 +5,7 @@ use std::cmp; use std::collections::HashMap; -use std::convert::Into; +use std::convert::{Into, TryFrom}; use std::hash::Hash; use std::{ fmt::Debug, @@ -43,7 +43,7 @@ use ordered_float::NotNan; /// ); /// abs_diff_eq!(integral.exp(), 0.682, epsilon=0.01); /// ``` -pub fn ln_integrate_exp( +pub fn ln_integrate_exp( mut density: F, min_point: T, max_point: T, @@ -57,10 +57,11 @@ where + Div, Output = T> + Mul + Into - + From + + TryFrom + Ord + Debug + Hash, + E: Debug, F: FnMut(T) -> LogProb, f64: From, { @@ -97,34 +98,31 @@ where left = middle.unwrap(); } } + // After that loop, we are guaranteed that middle.is_some(). + let middle = middle.unwrap(); + let first_middle = first_middle.unwrap(); // METHOD: add additional grid point in the initially abandoned arm if middle < first_middle { - grid_point( - middle_grid_point(first_middle.unwrap(), max_point), - &mut probs, - ); + grid_point(middle_grid_point(first_middle, max_point), &mut probs); } else { - grid_point( - middle_grid_point(min_point, first_middle.unwrap()), - &mut probs, - ); + grid_point(middle_grid_point(min_point, first_middle), &mut probs); } // METHOD additionally investigate small interval around the optimum for point in linspace( cmp::max( - middle.unwrap() - (max_resolution.into() * 3.0).into(), + T::try_from(middle.into() - max_resolution.into() * 3.0).unwrap(), min_point, ) .into(), - middle.unwrap().into(), + middle.into(), 4, ) .take(3) .chain( linspace( - middle.unwrap().into(), + middle.into(), cmp::min( - middle.unwrap() + (max_resolution.into() * 3.0).into(), + T::try_from(middle.into() + max_resolution.into() * 3.0).unwrap(), max_point, ) .into(), @@ -132,7 +130,7 @@ where ) .skip(1), ) { - grid_point(point.into(), &mut probs); + grid_point(T::try_from(point).unwrap(), &mut probs); } let sorted_grid_points: Vec = probs.keys().sorted().map(|point| (*point).into()).collect(); @@ -140,7 +138,7 @@ where // METHOD: // Step 2: integrate over grid points visited during the binary search. LogProb::ln_trapezoidal_integrate_grid_exp::( - |_, g| *probs.get(&T::from(g)).unwrap(), + |_, g| *probs.get(&T::try_from(g).unwrap()).unwrap(), &sorted_grid_points, ) } diff --git a/src/stats/probs/mod.rs b/src/stats/probs/mod.rs index eda74fe28..dc0c1c7c0 100644 --- a/src/stats/probs/mod.rs +++ b/src/stats/probs/mod.rs @@ -10,6 +10,7 @@ pub mod adaptive_integration; pub mod cdf; pub mod errors; +use std::convert::TryFrom; use std::f64; use std::iter; use std::mem; @@ -18,7 +19,7 @@ use std::ops::{Add, AddAssign, Div, Mul, Sub, SubAssign}; use itertools::Itertools; use itertools_num::linspace; use num_traits::{Float, Zero}; -use ordered_float::NotNan; +use ordered_float::{FloatIsNan, NotNan}; use crate::utils::FastExp; @@ -401,9 +402,10 @@ impl From> for LogProb { } } -impl From for NotNan { - fn from(p: LogProb) -> NotNan { - NotNan::from(*p) +impl TryFrom for NotNan { + type Error = FloatIsNan; + fn try_from(p: LogProb) -> Result { + NotNan::new(*p) } }