1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
use super::*; use std::convert::TryFrom; use deltae::DEMethod; use statistical::{mean, standard_deviation}; use std::fmt; #[derive(Debug)] pub struct DeReport { de_cgats: Cgats, de_method: DEMethod, overall: DeSummary, best_90: DeSummary, worst_10: DeSummary, } impl DeReport { pub fn new(de_cgats: &Cgats) -> Result<DeReport> { let mut de_list = DeList::try_from(de_cgats)?; let overall = DeSummary::from(&de_list); let [best_90_list, worst_10_list] = DeList::split_pct(&mut de_list, 0.9); let best_90 = DeSummary::from(&best_90_list); let worst_10 = DeSummary::from(&worst_10_list); let de_method = de_list.de_method; Ok( DeReport { de_cgats: de_cgats.clone(), de_method, overall, best_90, worst_10, } ) } } impl fmt::Display for DeReport { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "Number of Samples: {}", self.overall.sample_count)?; writeln!(f, "DE Formula: {}\n", self.de_method)?; writeln!(f, "OVERALL - ({} colors)", self.overall.sample_count)?; writeln!(f, "{}", self.overall)?; writeln!(f, "BEST 90% - ({} colors)", self.best_90.sample_count)?; writeln!(f, "{}", self.best_90)?; writeln!(f, "WORST 10% - ({} colors)", self.worst_10.sample_count)?; writeln!(f, "{}", self.worst_10)?; Ok(()) } } #[derive(Debug)] struct DeSummary { sample_count: usize, mean: Float, min: Float, max: Float, stdev: Float, } impl fmt::Display for DeSummary { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { writeln!(f, "\t{:>10}: {:0.4}", "Average DE", self.mean)?; writeln!(f, "\t{:>10}: {:0.4}", "Max DE", self.max)?; writeln!(f, "\t{:>10}: {:0.4}", "Min DE", self.min)?; writeln!(f, "\t{:>10}: {:0.4}", "StdDev DE", self.stdev) } } impl From<&DeList> for DeSummary { fn from(de_list: &DeList) -> DeSummary { DeSummary { sample_count: de_list.list.len(), mean: de_list.mean(), min: de_list.min(), max: de_list.max(), stdev: de_list.stdev(), } } } #[derive(Debug)] pub struct DeList { de_method: DEMethod, list: Vec<Float>, } impl DeList { fn mean(&self) -> Float { mean(&self.list) as Float } fn min(&self) -> Float { *self.list.iter().min_by(|a, b| a.partial_cmp(b).unwrap()).unwrap() } fn max(&self) -> Float { *self.list.iter().max_by(|a, b| a.partial_cmp(b).unwrap()).unwrap() } fn stdev(&self) -> Float { if self.list.len() <= 1 { 0.0 } else { standard_deviation(&self.list.as_slice(), None) } } fn split_pct(&mut self, pct: Float) -> [DeList; 2] { if pct <= 0.0 || pct >= 1.0 { panic!("Split must be between 0.0 and 1.0"); } let de_method = self.de_method; self.list.sort_by(|a, b| a.partial_cmp(b).unwrap()); let mut split_index = ((self.list.len() as Float) * pct) as usize; if split_index == 0 { split_index = 1; } else if split_index == self.list.len() { split_index = self.list.len() - 1 } let (best_90, worst_10) = self.list.split_at(split_index); [ DeList { de_method, list: best_90.into_iter().map(|f| *f).collect(), }, DeList { de_method, list: worst_10.into_iter().map(|f| *f).collect(), } ] } } impl std::convert::TryFrom<&Cgats> for DeList { type Error = Error; fn try_from(cgats: &Cgats) -> std::result::Result<DeList, Self::Error> { let (method_index, de_method) = cgats.de_method()?; let list = cgats.data_map.values() .filter_map(|sample| sample.values.iter().nth(method_index)) .filter_map(|cgv| cgv.float) .collect(); Ok( DeList { de_method, list, } ) } } #[test] fn de_report() -> Result<()> { let cg0 = Cgats::from_file("test_files/colorburst2.lin")?; let cg1 = Cgats::from_file("test_files/colorburst3.lin")?; let cgd = cg0.deltae(cg1, DEMethod::DE2000)?; let report = DeReport::new(&cgd)?; println!("{}", report); Ok(()) }