Skip to content

Commit

Permalink
bugfix in testcase + clippy + gnuplot
Browse files Browse the repository at this point in the history
  • Loading branch information
Pardoxa committed Dec 6, 2023
1 parent cf2aa70 commit 5da4e34
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
24 changes: 16 additions & 8 deletions src/examples/coin_flips.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<R> HasRng<R> for CoinFlipSequence<R>
}

#[cfg(test)]
#[cfg(features="replica_exchange")]
#[cfg(feature="replica_exchange")]
mod tests{
use super::*;
use rand::SeedableRng;
Expand Down Expand Up @@ -274,7 +274,7 @@ mod tests{
1,
NonZeroUsize::new(1999).unwrap(),
NonZeroUsize::new(2).unwrap(),
0.000003
0.000001
).unwrap();

let rewl1 = rewl_builder1.greedy_build(|e| Some(e.head_count()));
Expand All @@ -296,7 +296,7 @@ mod tests{
1,
NonZeroUsize::new(1999).unwrap(),
NonZeroUsize::new(2).unwrap(),
0.000003
0.000001
).unwrap();

let mut rewl2 = rewl_builder2.greedy_build(|e| Some(e.head_count()));
Expand All @@ -307,6 +307,7 @@ mod tests{
rewl_slice.par_iter_mut()
.for_each(|rewl| rewl.simulate_until_convergence(|e| Some(e.head_count())));


rewl_slice.iter()
.for_each(
|r|
Expand All @@ -315,7 +316,7 @@ mod tests{
.for_each(|w| println!("rewl replica_frac {}", w.replica_exchange_frac()));
}
);
let steps: usize = rewl_slice
let steps: u64 = rewl_slice
.iter()
.flat_map(|r|
r.walkers()
Expand All @@ -336,10 +337,10 @@ mod tests{
let mut rees_slice: Vec<_> = rewl_slice.into_iter()
.map(|r| r.into_rees())
.collect();

rees_slice.par_iter_mut()
.for_each(|rees| rees.simulate_until_convergence(|e| Some(e.head_count()), |_,_, _|{}));
let steps: usize = rees_slice

let steps: u64 = rees_slice
.iter()
.flat_map(|r|
r.walkers()
Expand All @@ -348,7 +349,7 @@ mod tests{
).sum();
println!("Ges steps rees {}", steps);

let prob_rees = rees::merged_log_prob(&rees_slice).unwrap();
let prob_rees = rees::merged_log_prob_rees(&rees_slice).unwrap();

let mut max_ln_difference_rewl = f64::NEG_INFINITY;
let mut max_difference_rewl = f64::NEG_INFINITY;
Expand All @@ -360,7 +361,14 @@ mod tests{
let mut max_difference_rees = f64::NEG_INFINITY;
let mut frac_difference_max_rees = f64::NEG_INFINITY;
let mut frac_difference_min_rees = f64::INFINITY;
for (index, ((val_sim1, val_sim2), val_true)) in prob.0.into_iter().zip(prob_rees.0).zip(ln_prob_true).enumerate()

let iter = prob.0.
into_iter()
.zip(prob_rees.0)
.zip(ln_prob_true)
.enumerate();

for (index, ((val_sim1, val_sim2), val_true)) in iter
{
println!("{} {} {} {}", index, val_sim1, val_sim2, val_true);

Expand Down
37 changes: 32 additions & 5 deletions src/heatmap/gnuplot.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use{
std::{
use std::{
fmt,
io::Write,
convert::From,
borrow::*
}
};
borrow::*,
path::Path
};

#[cfg(feature = "serde_support")]
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -293,6 +292,34 @@ impl GnuplotSettings {

self.terminal.finish(&mut writer)
}

/// Same as write_heatmap but it assumes that the heatmap
/// matrix is available in the file "heatmap"
pub fn write_heatmap_external_matrix<W, P>(
&self,
mut writer: W,
matrix_width: usize,
matrix_height: usize,
matrix_path: P
) -> std::io::Result<()>
where W: Write,
P: AsRef<Path>
{
self.write_heatmap_helper1(
&mut writer,
matrix_width,
matrix_height
)?;

writeln!(
writer,
"splot \"{}\" matrix with image t \"{}\" ",
matrix_path.as_ref().to_string_lossy(),
&self.title
)?;

self.terminal.finish(&mut writer)
}
}

impl Default for GnuplotSettings{
Expand Down
10 changes: 5 additions & 5 deletions src/histogram/binning/binning_int_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,21 +100,21 @@ macro_rules! impl_binning {
impl Binning<$t> for paste!{[<FastBinning $t:upper>]} {
fn get_bin_len(&self) -> usize
{
let len = self.bins_m1();
let _len = self.bins_m1();
todo!()
}

fn get_bin_index<V: Borrow<$t>>(&self, val: V) -> Option<usize>{
fn get_bin_index<V: Borrow<$t>>(&self, _val: V) -> Option<usize>{
todo!()
}

/// Does a value correspond to a valid bin?
fn is_inside<V: Borrow<$t>>(&self, val: V) -> bool{
fn is_inside<V: Borrow<$t>>(&self, _val: V) -> bool{
todo!()
}

/// Opposite of `is_inside`
fn not_inside<V: Borrow<$t>>(&self, val: V) -> bool{
fn not_inside<V: Borrow<$t>>(&self, _val: V) -> bool{
todo!()
}

Expand All @@ -130,7 +130,7 @@ macro_rules! impl_binning {
/// # calculates some sort of absolute distance to the nearest valid bin
/// * any invalid numbers (like NAN or INFINITY) should have the highest distance possible
/// * if a value corresponds to a valid bin, the distance should be zero
fn distance<V: Borrow<$t>>(&self, val: V) -> f64{
fn distance<V: Borrow<$t>>(&self, _val: V) -> f64{
todo!{}
}

Expand Down
33 changes: 17 additions & 16 deletions src/rees/rees.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,6 @@ where Ensemble: Send + Sync + MarkovChain<S, Res>,
walker
};


walker
.par_iter_mut()
.zip(self.extra.par_iter_mut())
Expand Down Expand Up @@ -719,10 +718,10 @@ where Ensemble: Send + Sync + MarkovChain<S, Res>,
/// there is no overlap between neighboring intervals
#[deprecated(since="0.2.0", note="will be removed in future releases. Use new method 'derivative_merged_log_prob_and_aligned' or consider using 'average_merged_log_probability_and_align' instead")]
#[allow(deprecated)]
pub fn merged_log_prob(&self) -> Result<(Hist, Vec<f64>), HistErrors>
pub fn merged_log_prob_rees(&self) -> Result<(Hist, Vec<f64>), HistErrors>
where Hist: HistogramCombine
{
let (mut log_prob, e_hist) = self.merged_log_probability()?;
let (mut log_prob, e_hist) = self.merged_log_probability_rees()?;

norm_ln_prob(&mut log_prob);

Expand Down Expand Up @@ -818,7 +817,7 @@ where Ensemble: Send + Sync + MarkovChain<S, Res>,

#[deprecated(since="0.2.0", note="will be removed in future releases. Use new method 'derivative_merged_log_prob_and_aligned' or consider using 'average_merged_log_probability_and_align' instead")]
#[allow(deprecated)]
fn merged_log_probability(&self) -> Result<(Vec<f64>, Hist), HistErrors>
fn merged_log_probability_rees(&self) -> Result<(Vec<f64>, Hist), HistErrors>
where Hist: HistogramCombine
{
let (merge_points, alignment, log_prob, e_hist) = self.merged_log_probability_helper()?;
Expand Down Expand Up @@ -856,7 +855,7 @@ where Ensemble: Send + Sync + MarkovChain<S, Res>,
/// ## Errors
/// * will return `HistErrors::EmptySlice` if the `rees` slice is empty
/// * will return other HistErrors if the intervals have no overlap
pub fn merged_log_prob<Extra, Ensemble, R, Hist, Energy, S, Res>(
pub fn merged_log_prob_rees<Extra, Ensemble, R, Hist, Energy, S, Res>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>]
) -> Result<(Vec<f64>, Hist), HistErrors>
where Hist: Histogram + HistogramVal<Energy> + HistogramCombine + Send + Sync,
Expand Down Expand Up @@ -903,11 +902,13 @@ where Hist: HistogramVal<Energy> + HistogramCombine + Histogram + Send + Sync,
/// ## Errors
/// * will return `HistErrors::EmptySlice` if the `rees` slice is empty
/// * will return other HistErrors if the intervals have no overlap
pub fn merged_log10_prob<Extra, Ensemble, R, Hist, Energy, S, Res>(rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>]) -> Result<(Vec<f64>, Hist), HistErrors>
pub fn merged_log10_prob_rees<Extra, Ensemble, R, Hist, Energy, S, Res>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>]
) -> Result<(Vec<f64>, Hist), HistErrors>
where Hist: Histogram + HistogramVal<Energy> + HistogramCombine + Send + Sync,
Energy: PartialOrd
{
let mut res = merged_log_prob(rees)?;
let mut res = merged_log_prob_rees(rees)?;
ln_to_log10(&mut res.0);
Ok(res)
}
Expand All @@ -921,7 +922,7 @@ where Hist: Histogram + HistogramVal<Energy> + HistogramCombine + Send + Sync,
/// ## Errors
/// * will return `HistErrors::EmptySlice` if the `rees` slice is empty
/// * will return other HistErrors if the intervals have no overlap
pub fn merged_log_probability_and_align<Ensemble, R, Hist, Energy, S, Res, Extra>(
pub fn merged_log_probability_and_align_rees<Ensemble, R, Hist, Energy, S, Res, Extra>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>]
) -> GluedResult<Hist>
where Hist: Histogram + HistogramCombine + HistogramVal<Energy> + Send + Sync,
Expand All @@ -944,13 +945,13 @@ where Hist: Histogram + HistogramCombine + HistogramVal<Energy> + Send + Sync,
}

/// # Merge probability density of multiple rees simulations
/// * similar to [merged_log_probability_and_align]
/// * similar to [merged_log_probability_and_align_rees]
///
/// The difference is, that this function will ignore the specified walkers,
/// therefore `ignore` should be a slice of indices, which are to be ignored.
/// The slice does not have to be sorted in any way, though duplicate indices
/// and indices which are out of bounds will be ignored for the ignore list
pub fn merged_log_probability_and_align_ignore<Ensemble, R, Hist, Energy, S, Res, Extra>(
pub fn merged_log_probability_and_align_ignore_rees<Ensemble, R, Hist, Energy, S, Res, Extra>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>],
ignore: &[usize]
) -> GluedResult<Hist>
Expand All @@ -976,32 +977,32 @@ where Hist: Histogram + HistogramCombine + HistogramVal<Energy> + Send + Sync,
}

/// # Merge probability density of multiple rees simulations
/// * same as [merged_log_probability_and_align](`crate::rees::merged_log_probability_and_align`)
/// * same as [merged_log_probability_and_align_rees](`crate::rees::merged_log_probability_and_align_rees`)
/// but all logarithms are now base 10
pub fn merged_log10_probability_and_align<Ensemble, R, Hist, Energy, S, Res, Extra>(
pub fn merged_log10_probability_and_align_rees<Ensemble, R, Hist, Energy, S, Res, Extra>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>]
) -> GluedResult<Hist>
where Hist: Histogram + HistogramCombine + HistogramVal<Energy> + Send + Sync,
Energy: PartialOrd
{
merged_log10_probability_and_align_ignore(rees, &[])
merged_log10_probability_and_align_ignore_rees(rees, &[])
}

/// # Merge probability density of multiple rees simulations
/// * similar to [merged_log10_probability_and_align]
/// * similar to [merged_log10_probability_and_align_rees]
///
/// The difference is, that this function will ignore the specified walkers,
/// therefore `ignore` should be a slice of indices, which are to be ignored.
/// The slice does not have to be sorted in any way, though duplicate indices
/// and indices which are out of bounds will be ignored for the ignore list
pub fn merged_log10_probability_and_align_ignore<Ensemble, R, Hist, Energy, S, Res, Extra>(
pub fn merged_log10_probability_and_align_ignore_rees<Ensemble, R, Hist, Energy, S, Res, Extra>(
rees: &[Rees<Extra, Ensemble, R, Hist, Energy, S, Res>],
ignore: &[usize]
) -> GluedResult<Hist>
where Hist: Histogram + HistogramCombine + HistogramVal<Energy> + Send + Sync,
Energy: PartialOrd
{
let mut res = merged_log_probability_and_align_ignore(rees, ignore)?;
let mut res = merged_log_probability_and_align_ignore_rees(rees, ignore)?;
ln_to_log10(&mut res.1);
res.2.par_iter_mut()
.for_each(|slice| ln_to_log10(slice));
Expand Down
3 changes: 2 additions & 1 deletion src/rees/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,8 @@ where Hist: HistogramVal<Energy>,
},
_ => {
self.count_rejected();
e.undo_steps_quiet(&self.markov_steps);
e.steps_rejected(&self.markov_steps);
e.undo_steps_quiet(&self.markov_steps);
}
}

Expand All @@ -377,6 +377,7 @@ where Hist: HistogramVal<Energy>,
extra_fn(self, &mut e, extra);

}

#[cfg(feature = "sweep_time_optimization")]
{
self.duration = start.elapsed();
Expand Down

0 comments on commit 5da4e34

Please sign in to comment.