(
log_means: &[f64],
log_dispersions: &[f64],
batch_col: &polars::prelude::Column,
n_bins: usize,
)
| 200 | } |
| 201 | |
| 202 | pub fn _normalize_by_batch( |
| 203 | log_means: &[f64], |
| 204 | log_dispersions: &[f64], |
| 205 | batch_col: &polars::prelude::Column, |
| 206 | n_bins: usize, |
| 207 | ) -> anyhow::Result<Vec<f64>> { |
| 208 | let mut unique_batches = Vec::new(); |
| 209 | if let DataType::String = batch_col.dtype() { |
| 210 | unique_batches = batch_col.str()?.iter().flatten().collect::<Vec<&str>>(); |
| 211 | } |
| 212 | |
| 213 | let mut norm_dispersions = vec![0.0; log_means.len()]; |
| 214 | for batch in unique_batches { |
| 215 | let batch_mask: Vec<bool> = batch_col |
| 216 | .str()? |
| 217 | .into_iter() |
| 218 | .map(|x| x == Some(batch)) |
| 219 | .collect(); |
| 220 | |
| 221 | let batch_size = batch_mask.iter().filter(|&&x| x).count(); |
| 222 | |
| 223 | if batch_size > 0 { |
| 224 | let batch_norm = normalize_per_bin( |
| 225 | &log_means |
| 226 | .iter() |
| 227 | .zip(batch_mask.iter()) |
| 228 | .filter(|(_, &mask)| mask) |
| 229 | .map(|(&x, _)| x) |
| 230 | .collect::<Vec<_>>(), |
| 231 | &log_dispersions |
| 232 | .iter() |
| 233 | .zip(batch_mask.iter()) |
| 234 | .filter(|(_, &mask)| mask) |
| 235 | .map(|(&x, _)| x) |
| 236 | .collect::<Vec<_>>(), |
| 237 | n_bins, |
| 238 | )?; |
| 239 | |
| 240 | let mut j = 0; |
| 241 | for i in 0..log_means.len() { |
| 242 | if batch_mask[i] { |
| 243 | norm_dispersions[i] = batch_norm[j]; |
| 244 | j += 1; |
| 245 | } |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | Ok(norm_dispersions) |
| 250 | } |
| 251 | |
| 252 | pub fn fit_svr(x: &[f64], y: &[f64]) -> anyhow::Result<(Vec<f64>, Vec<f64>)> { |
| 253 | let n = x.len(); |
nothing calls this directly
no test coverage detected