Skip to content

Commit

Permalink
modified: src/fitter/fitter.rs
Browse files Browse the repository at this point in the history
	modified:   src/fitter/linear.rs
	modified:   src/histoer/histogram1d.rs
	modified:   src/lazyframer.rs
  • Loading branch information
alconley committed May 25, 2024
1 parent 8ab6fc2 commit 7546212
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 114 deletions.
68 changes: 31 additions & 37 deletions src/fitter/fitter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
use super::gaussian::GaussianFitter;
use super::linear::LinearFitter;

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum FitModel {
Gaussian(Vec<f64>), // put the inital peak locations in here
Linear,
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum FitResult {
Gaussian(GaussianFitter),
Linear(LinearFitter),
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct BackgroundFitter {
pub x_data: Vec<f64>,
Expand All @@ -19,15 +31,14 @@ impl BackgroundFitter {
}
}

pub fn fit(&mut self) {
pub fn fit(&mut self) {
match self.model {
FitModel::Gaussian(_) => {
eprintln!("Gaussian background fitting not yet implemented");
}
FitModel::Linear => {

// check x and y data are the same length
if self.x_data.len()!= self.y_data.len() {
if self.x_data.len() != self.y_data.len() {
eprintln!("x_data and y_data must have the same length");
return;
}
Expand All @@ -36,31 +47,34 @@ impl BackgroundFitter {
linear_fitter.perform_linear_fit();

Check warning on line 47 in src/fitter/fitter.rs

View workflow job for this annotation

GitHub Actions / Check

unused `Result` that must be used

Check warning on line 47 in src/fitter/fitter.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused `Result` that must be used

self.result = Some(FitResult::Linear(linear_fitter));

}
}
}

}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum FitModel {
Gaussian(Vec<f64>), // put the inital peak locations in here
Linear,
}
pub fn draw(&self, plot_ui: &mut egui_plot::PlotUi) {
// Draw the fit lines
if let Some(fit) = &self.result {
match fit {
FitResult::Gaussian(fit) => {
let color = egui::Color32::from_rgb(255, 0, 255); // purple
fit.draw(plot_ui, color);
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub enum FitResult {
Gaussian(GaussianFitter),
Linear(LinearFitter),
FitResult::Linear(fit) => {
let color = egui::Color32::GREEN;
fit.draw(plot_ui, color);
}
}
}
}
}

#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Fitter {
pub x_data: Vec<f64>,
pub y_data: Vec<f64>,
pub y_err: Option<Vec<f64>>,
pub background: Option<Background>,
pub background: Option<BackgroundFitter>,
pub model: FitModel,
pub result: Option<FitResult>,
}
Expand All @@ -82,24 +96,6 @@ impl Fitter {
// Perform the fit based on the model
match &self.model {
FitModel::Gaussian(peak_markers) => {

if self.background.is_some() {
// if background is linear, preform linear fit and subtract from data
match &self.background.as_ref().unwrap().model {
FitModel::Linear => {
let mut fit = LinearFitter::new(
self.background.as_ref().unwrap().x_data.clone(),
self.background.as_ref().unwrap().y_data.clone(),
);
}

FitModel::Gaussian(_) => {
// TODO: implement this
}
}
}


// Perform Gaussian fit
let mut fit = GaussianFitter::new(
self.x_data.clone(),
Expand All @@ -111,12 +107,11 @@ impl Fitter {

self.result = Some(FitResult::Gaussian(fit));
}


FitModel::Linear => {
// Perform Linear fit
let mut fit = LinearFitter::new(self.x_data.clone(), self.y_data.clone());

fit.perform_linear_fit();

Check warning on line 115 in src/fitter/fitter.rs

View workflow job for this annotation

GitHub Actions / Check

unused `Result` that must be used

Check warning on line 115 in src/fitter/fitter.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unused `Result` that must be used

self.result = Some(FitResult::Linear(fit));
Expand All @@ -142,5 +137,4 @@ impl Fitter {
}
}
}

}
18 changes: 11 additions & 7 deletions src/fitter/linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ pub struct Parameters {
pub intercept: f64,
}


#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct LinearFitter {
x_data: Vec<f64>,
y_data: Vec<f64>,
fit_params: Option<Parameters>,
fit_line: Option<Vec<(f64, f64)>>
fit_line: Option<Vec<(f64, f64)>>,
}

impl LinearFitter {
Expand Down Expand Up @@ -78,7 +77,11 @@ impl LinearFitter {
pub fn get_fit_line(&mut self) {
if let Some(params) = &self.fit_params {
let x_min = self.x_data.iter().cloned().fold(f64::INFINITY, f64::min);
let x_max = self.x_data.iter().cloned().fold(f64::NEG_INFINITY, f64::max);
let x_max = self
.x_data
.iter()
.cloned()
.fold(f64::NEG_INFINITY, f64::max);

let y_min = params.slope * x_min + params.intercept;
let y_max = params.slope * x_max + params.intercept;
Expand All @@ -89,15 +92,16 @@ impl LinearFitter {

pub fn draw(&self, plot_ui: &mut egui_plot::PlotUi, color: egui::Color32) {
if let Some(fit_line) = &self.fit_line {
let plot_points: Vec<egui_plot::PlotPoint> = fit_line
.iter()
.map(|(x, y)| egui_plot::PlotPoint::new(*x, *y))
.collect();

let plot_points: Vec<egui_plot::PlotPoint> = fit_line.iter().map(|(x, y)| egui_plot::PlotPoint::new(*x, *y)).collect();

let line = egui_plot::Line::new(egui_plot::PlotPoints::Owned(plot_points))
.color(color)
.stroke(egui::Stroke::new(1.0, color));

plot_ui.line(line);
}
}

}
}
Loading

0 comments on commit 7546212

Please sign in to comment.