Skip to content

Commit

Permalink
custom plot navigation methods
Browse files Browse the repository at this point in the history
	modified:   README.md
	modified:   src/egui_plot_stuff/egui_horizontal_line.rs
	modified:   src/egui_plot_stuff/egui_vertical_line.rs
	modified:   src/histoer/histo1d/histogram1d.rs
	modified:   src/histoer/histo1d/plot_settings.rs
	modified:   src/histoer/histo2d/histogram2d.rs
	modified:   src/histoer/histo2d/projections.rs
	modified:   src/histoer/histogrammer.rs
  • Loading branch information
alconley committed Nov 13, 2024
1 parent 1c27969 commit a86ccf5
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 50 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,13 @@ Keybinds (cursor must be in the plot):
- Data is evaluated at the bin center for the background marker.
- O: Peak find
- Peak find settings are located in the context menu.
- Use with caution, lol.
- F: Fit Gaussians
- Settings and results can be found in the context menu.
- Requires 2 region markers. Data will be evaluated between the markers.
- Multiple Gaussians can be fitted together when multiple peak markers are between the region markers. By default, all peaks have the same standard deviation. This can be changed by checking the "Equal Standard Deviation" button. The position can also be locked if needed.
- If no peak markers are between the region markers, the program will assume there is only 1 peak approximately at the max value in the data.
- If there is no background fit when the fit button is clicked, the selected background model will be used and fitted. This can result in longer and potentially incorrect fits due to more parameters being fit. A fix would be adjusting the initial guess', min, and max value or fiting the background with the background markers.
- The lmfit fit report, fit stats, and fit lines can be viewed in the Fits menu.

- S: Store Fit
- Saves the fit.
- Can save/load the fit to/from a file in the context menu.
Expand Down
4 changes: 3 additions & 1 deletion src/egui_plot_stuff/egui_horizontal_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct EguiHorizontalLine {
pub stroke_rgb: Rgb,

pub interactive_dragging: bool,
pub mid_point_radius: f32,

#[serde(skip)]
pub is_dragging: bool,
Expand All @@ -44,6 +45,7 @@ impl Default for EguiHorizontalLine {
color_rgb: Rgb::from_color32(Color32::BLUE),
stroke_rgb: Rgb::from_color32(Color32::BLUE),
interactive_dragging: true,
mid_point_radius: 3.0,
is_dragging: false,
}
}
Expand Down Expand Up @@ -89,7 +91,7 @@ impl EguiHorizontalLine {
let mid_point = egui_plot::Points::new(mid_point_pos)
.color(self.color)
.highlight(self.highlighted)
.radius(3.0)
.radius(self.mid_point_radius)
.id(Id::new(self.name.clone()));

plot_ui.points(mid_point);
Expand Down
4 changes: 3 additions & 1 deletion src/egui_plot_stuff/egui_vertical_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub struct EguiVerticalLine {
pub stroke_rgb: Rgb,

pub interactive_dragging: bool,
pub mid_point_radius: f32,

#[serde(skip)]
pub is_dragging: bool,
Expand All @@ -44,6 +45,7 @@ impl Default for EguiVerticalLine {
color_rgb: Rgb::from_color32(Color32::BLUE),
stroke_rgb: Rgb::from_color32(Color32::BLUE),
interactive_dragging: true,
mid_point_radius: 3.0,
is_dragging: false,
}
}
Expand Down Expand Up @@ -89,7 +91,7 @@ impl EguiVerticalLine {
let mid_point = egui_plot::Points::new(mid_point_pos)
.color(self.color)
.highlight(self.highlighted)
.radius(3.0)
.radius(self.mid_point_radius)
.id(Id::new(self.name.clone()));

plot_ui.points(mid_point);
Expand Down
27 changes: 22 additions & 5 deletions src/histoer/histo1d/histogram1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,7 @@ impl Histogram {
self.plot_settings.cursor_position = None;
}

// if self.plot_settings.egui_settings.limit_scrolling {
self.limit_scrolling(plot_ui);
// }
}

pub fn draw_other_histograms(
Expand Down Expand Up @@ -298,7 +296,8 @@ impl Histogram {

pub fn render(&mut self, ui: &mut egui::Ui) {
// Display progress bar while hist is being filled
self.plot_settings.progress_ui(ui);
// disabled since the row calculation is done in chucks
// self.plot_settings.progress_ui(ui);

self.update_line_points(); // Ensure line points are updated for projections
self.keybinds(ui); // Handle interactive elements
Expand All @@ -323,9 +322,23 @@ impl Histogram {
let plot_response = plot.show(ui, |plot_ui| {
self.draw(plot_ui);

// if progress is updating, turn on the auto bounds
if self.plot_settings.progress.is_some() {
plot_ui.set_auto_bounds(Vec2b::new(true, true));
let y_max = self.bins.iter().max().cloned().unwrap_or(0) as f64;
let mut plot_bounds = plot_ui.plot_bounds();
plot_bounds.extend_with_y(y_max * 1.1);
plot_ui.set_plot_bounds(plot_bounds);
}

if self.plot_settings.egui_settings.reset_axis {
// let y_min = self.bins.iter().min().cloned().unwrap_or(0) as f64;
// let y_max = self.bins.iter().max().cloned().unwrap_or(0) as f64;
// let plot_bounds = egui_plot::PlotBounds::from_min_max(
// [self.range.0 * 1.1, y_min * 1.1],
// [self.range.1 * 1.1, y_max * 1.1],
// );
// plot_ui.set_plot_bounds(plot_bounds);
plot_ui.auto_bounds();
self.plot_settings.egui_settings.reset_axis = false;
}

if self.plot_settings.cursor_position.is_some() {
Expand All @@ -334,6 +347,10 @@ impl Histogram {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(1.1, 1.0));
} else if delta_pos.y < 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(0.9, 1.0));
} else if delta_pos.x > 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(1.1, 1.0));
} else if delta_pos.x < 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(0.9, 1.0));
}
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/histoer/histo1d/plot_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,14 @@ impl PlotSettings {
self.markers.interactive_dragging(response);
}

pub fn progress_ui(&mut self, ui: &mut egui::Ui) {
if let Some(progress) = self.progress {
ui.add(
egui::ProgressBar::new(progress)
.show_percentage()
.animate(true)
.text(format!("{:.0}%", progress * 100.0)),
);
}
}
// pub fn progress_ui(&mut self, ui: &mut egui::Ui) {
// if let Some(progress) = self.progress {
// ui.add(
// egui::ProgressBar::new(progress)
// .show_percentage()
// .animate(true)
// .text(format!("{:.0}%", progress * 100.0)),
// );
// }
// }
}
26 changes: 26 additions & 0 deletions src/histoer/histo2d/histogram2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ impl Histogram2D {
self.plot_settings.recalculate_image = false;
}

let (scroll, _pointer_down, _modifiers) = ui.input(|i| {
let scroll = i.events.iter().find_map(|e| match e {
egui::Event::MouseWheel {
unit: _,
delta,
modifiers: _,
} => Some(*delta),
_ => None,
});
(scroll, i.pointer.primary_down(), i.modifiers)
});

let mut plot = egui_plot::Plot::new(self.name.clone());
plot = self.plot_settings.egui_settings.apply_to_plot(plot);

Expand All @@ -238,6 +250,20 @@ impl Histogram2D {

let plot_response = plot.show(ui, |plot_ui| {
self.draw(plot_ui);

if self.plot_settings.cursor_position.is_some() {
if let Some(delta_pos) = scroll {
if delta_pos.y > 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(1.1, 1.1));
} else if delta_pos.y < 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(0.9, 0.9));
} else if delta_pos.x > 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(1.1, 1.1));
} else if delta_pos.x < 0.0 {
plot_ui.zoom_bounds_around_hovered(egui::Vec2::new(0.9, 0.9));
}
}
}
});

plot_response.response.context_menu(|ui| {
Expand Down
36 changes: 5 additions & 31 deletions src/histoer/histo2d/projections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ impl Histogram2D {
let bins = self.x_projection(min_y, max_y);
x_histogram.bins = bins.clone();
x_histogram.original_bins = bins;
x_histogram.plot_settings.egui_settings.reset_axis = true;

self.plot_settings.projections.x_projection = Some(x_histogram);

Expand Down Expand Up @@ -192,57 +193,30 @@ impl Projections {
y_projection: None,
y_projection_line_1: EguiVerticalLine {
name: "Y Projection Line 1".to_string(),
mid_point_radius: 5.0,
..EguiVerticalLine::default()
},
y_projection_line_2: EguiVerticalLine {
name: "Y Projection Line 2".to_string(),
mid_point_radius: 5.0,
..EguiVerticalLine::default()
},

add_x_projection: false,
x_projection: None,
x_projection_line_1: EguiHorizontalLine {
name: "X Projection Line 1".to_string(),
mid_point_radius: 5.0,
..EguiHorizontalLine::default()
},
x_projection_line_2: EguiHorizontalLine {
name: "X Projection Line 2".to_string(),
mid_point_radius: 5.0,
..EguiHorizontalLine::default()
},
}
}

// this pops out a new window with the y projection

// fn show_y_projection(&mut self, ui: &mut egui::Ui) {
// if self.add_y_projection && self.y_projection.is_some() {
// ui.ctx().show_viewport_immediate(
// egui::ViewportId::from_hash_of("Y Projection".to_string()),
// egui::ViewportBuilder::default()
// .with_title(self.y_projection.as_ref().unwrap().name.clone())
// .with_inner_size([600.0, 400.0]),
// |ctx, class| {
// assert!(
// class == egui::ViewportClass::Immediate,
// "This egui backend doesn't support multiple viewports"
// );

// egui::CentralPanel::default().show(ctx, |ui| {
// if let Some(histogram) = &mut self.y_projection {
// histogram.render(ui);
// }
// });

// if ctx.input(|i| i.viewport().close_requested()) {
// // Tell parent viewport that we should not show next frame:
// self.y_projection = None;
// self.add_y_projection = false;
// }
// },
// );
// }
// }

fn show_y_projection(&mut self, ui: &mut egui::Ui) {
if self.add_y_projection && self.y_projection.is_some() {
let name = if let Some(histogram) = &self.y_projection {
Expand Down
10 changes: 10 additions & 0 deletions src/histoer/histogrammer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ impl Histogrammer {
if passes_all_cuts {
hist.fill(value);
}

hist.plot_settings.progress = Some(
(row_start as f32 + index as f32)
/ (df.height() as f32),
);
}
}
}
Expand Down Expand Up @@ -549,6 +554,11 @@ impl Histogrammer {
hist.plot_settings.recalculate_image = true;
}

for hist in &hist1d_map {
let mut hist = hist.0.lock().unwrap();
hist.plot_settings.progress = None;
}

println!("\tProcessed rows {} to {}", row_start, row_start + height);
}

Expand Down

0 comments on commit a86ccf5

Please sign in to comment.