Skip to content

Commit

Permalink
added x and y projections to 2d histograms and the ability to rebin 1…
Browse files Browse the repository at this point in the history
…d histograms

	modified:   Cargo.lock
	modified:   src/app.rs
	modified:   src/egui_plot_stuff/egui_image.rs
	modified:   src/egui_plot_stuff/egui_line.rs
	modified:   src/egui_plot_stuff/egui_polygon.rs
	modified:   src/egui_plot_stuff/mod.rs
	modified:   src/histoer/histogram1d.rs
	modified:   src/histoer/histogram2d.rs
	modified:   src/lib.rs
  • Loading branch information
alconley committed Jun 22, 2024
1 parent 122e0ef commit b9956aa
Show file tree
Hide file tree
Showing 9 changed files with 331 additions and 170 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ impl NATApp {
Default::default()
}

fn ui(&mut self, ui: &mut egui::Ui) {
self.tree.ui(&mut self.behavior, ui);
}
// fn ui(&mut self, ui: &mut egui::Ui) {
// self.tree.ui(&mut self.behavior, ui);
// }

fn add_histograms_to_tree(&mut self) {
// let mut panes = self.processer.histogrammer.get_histogram1d_panes();
Expand Down
7 changes: 3 additions & 4 deletions src/egui_plot_stuff/egui_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl Default for EguiImage {

impl EguiImage {
pub fn heatmap(name: String, range_x: [f64; 2], range_y: [f64; 2]) -> Self {

let image = EguiImage::default();

EguiImage {
Expand All @@ -68,7 +67,7 @@ impl EguiImage {
..image
}
}

// Convert ColorImage to ImageData (Byte array)
fn to_image_data(&self, color_image: ColorImage) -> ImageData {
let width = color_image.size[0];
Expand All @@ -77,7 +76,7 @@ impl EguiImage {
for pixel in color_image.pixels.iter() {
rgba_data.extend_from_slice(&pixel.to_array());
}

ImageData::Color(ColorImage::from_rgba_unmultiplied([width, height], &rgba_data).into())
}

Expand All @@ -91,7 +90,7 @@ impl EguiImage {
egui::Vec2::new(self.image_width, self.image_height)
}

pub fn get_plot_image_from_texture(&mut self, ui: &mut egui::Ui) -> Option<egui_plot::PlotImage> {
pub fn get_plot_image_from_texture(&mut self) -> Option<egui_plot::PlotImage> {
if let Some(texture) = &self.texture {
Some(PlotImage::new(
texture,
Expand Down
8 changes: 4 additions & 4 deletions src/egui_plot_stuff/egui_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ impl Default for EguiLine {
log_x: false,
name: "Line".to_string(),
highlighted: false,
stroke: Stroke::new(1.0, Color32::LIGHT_BLUE),
stroke: Stroke::new(1.0, Color32::from_rgb(120, 47, 64)),
width: 2.0,
color: Color32::BLACK,
color: Color32::from_rgb(120, 47, 64),
reference_fill: false,
fill: 0.0,
style: Some(LineStyle::Solid),
style_length: 15.0,
points: vec![],
color_rgb: Rgb::from_color32(Color32::LIGHT_BLUE),
stroke_rgb: Rgb::from_color32(Color32::LIGHT_BLUE),
color_rgb: Rgb::from_color32(Color32::from_rgb(120, 47, 64)),
stroke_rgb: Rgb::from_color32(Color32::from_rgb(120, 47, 64)),
}
}
}
Expand Down
43 changes: 24 additions & 19 deletions src/egui_plot_stuff/egui_polygon.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use egui::{Color32, DragValue, Slider, Stroke, Ui};
use egui_plot::{Polygon, LineStyle, PlotPoints, PlotUi};
use egui_plot::{LineStyle, PlotPoints, PlotUi, Polygon};

use crate::egui_plot_stuff::colors::{Rgb, COLOR_OPTIONS};

Expand Down Expand Up @@ -65,22 +65,27 @@ impl EguiPolygon {
}
}

pub fn keybinds(&mut self, ui: &mut egui::Ui, cursor_position: Option<egui_plot::PlotPoint>) {
if let Some(_cursor_position) = cursor_position {
if ui.input(|i| i.key_pressed(egui::Key::C)) {
self.draw = !self.draw;
self.interactive = !self.interactive;
log::info!("{} Polygon -> Draw: {}, Interactive: {}", self.name, self.draw, self.interactive);
}

if ui.input(|i| i.key_pressed(egui::Key::Delete))
|| ui.input(|i| i.key_pressed(egui::Key::Backspace))
{
self.clear_vertices();
log::info!("{} Polygon -> Vertices Cleared", self.name);
}
}
}
// pub fn keybinds(&mut self, ui: &mut egui::Ui, cursor_position: Option<egui_plot::PlotPoint>) {
// if let Some(_cursor_position) = cursor_position {
// if ui.input(|i| i.key_pressed(egui::Key::C)) {
// self.draw = !self.draw;
// self.interactive = !self.interactive;
// log::info!(
// "{} Polygon -> Draw: {}, Interactive: {}",
// self.name,
// self.draw,
// self.interactive
// );
// }

// if ui.input(|i| i.key_pressed(egui::Key::Delete))
// || ui.input(|i| i.key_pressed(egui::Key::Backspace))
// {
// self.clear_vertices();
// log::info!("{} Polygon -> Vertices Cleared", self.name);
// }
// }
// }

pub fn add_vertex(&mut self, x: f64, y: f64) {
self.vertices.push([x, y]);
Expand Down Expand Up @@ -112,12 +117,12 @@ impl EguiPolygon {
}

plot_ui.polygon(polygon);
plot_ui.points(vertices_points);
plot_ui.points(vertices_points);
}
}

pub fn menu_button(&mut self, ui: &mut Ui) {
ui.menu_button(format!("{}", self.name), |ui| {
ui.menu_button(self.name.to_string(), |ui| {
ui.vertical(|ui| {
ui.text_edit_singleline(&mut self.name);
ui.checkbox(&mut self.draw, "Draw Polygon");
Expand Down
2 changes: 1 addition & 1 deletion src/egui_plot_stuff/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ pub mod egui_horizontal_line;
pub mod egui_image;
pub mod egui_line;
// pub mod egui_points;
pub mod egui_vertical_line;
pub mod egui_polygon;
pub mod egui_vertical_line;
82 changes: 67 additions & 15 deletions src/histoer/histogram1d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,24 @@ pub struct PlotSettings {
egui_settings: EguiPlotSettings,
stats_info: bool,
markers: EguiFitMarkers,
rebin_factor: usize,
}

impl Default for PlotSettings {
fn default() -> Self {
PlotSettings {
cursor_position: None,
egui_settings: EguiPlotSettings::default(),
stats_info: false,
markers: EguiFitMarkers::new(),
rebin_factor: 1,
}
}
}

impl PlotSettings {
pub fn settings_ui(&mut self, ui: &mut egui::Ui) {
ui.vertical(|ui| {
ui.label("Plot Settings:");
ui.separator();
ui.checkbox(&mut self.stats_info, "Show Statitics");
self.egui_settings.menu_button(ui);
self.markers.menu_button(ui);
});
ui.checkbox(&mut self.stats_info, "Show Statitics");
self.egui_settings.menu_button(ui);
self.markers.menu_button(ui);
}
}

Expand All @@ -46,23 +42,23 @@ pub struct Histogram {
pub line: EguiLine,
pub plot_settings: PlotSettings,
pub fits: Fits,
pub original_bins: Vec<u32>,
}

// new(name.to_string(), egui::Color32::LIGHT_BLUE)
impl Histogram {
// Create a new Histogram with specified min, max, and number of bins
pub fn new(name: &str, number_of_bins: usize, range: (f64, f64)) -> Self {
let mut line = EguiLine::new(egui::Color32::LIGHT_BLUE);
line.name = name.to_string();

Histogram {
name: name.to_string(),
bins: vec![0; number_of_bins],
range,
bin_width: (range.1 - range.0) / number_of_bins as f64,
line,
line: EguiLine {
name: name.to_string(),
..Default::default()
},
plot_settings: PlotSettings::default(),
fits: Fits::new(),
original_bins: vec![0; number_of_bins],
}
}

Expand All @@ -72,10 +68,44 @@ impl Histogram {
let index = ((value - self.range.0) / self.bin_width) as usize;
if index < self.bins.len() {
self.bins[index] += 1;
self.original_bins[index] += 1;
}
}
}

// Rebin the histogram according to the rebin factor
fn rebin(&mut self) {
let rebin_factor = self.plot_settings.rebin_factor;
let new_bin_count = self.original_bins.len() / rebin_factor;
let mut new_bins = vec![0; new_bin_count];

for (i, &count) in self.original_bins.iter().enumerate() {
let new_index = i / rebin_factor;
new_bins[new_index] += count;
}

self.bins = new_bins;
self.bin_width = (self.range.1 - self.range.0) / new_bin_count as f64;
self.update_line_points();
}

// Compute the possible rebin factors based on the initial number of bins
fn possible_rebin_factors(&self) -> Vec<usize> {
let mut factors = vec![];
factors.push(1);
let mut factor = 1;
while self.original_bins.len() % (factor * 2) == 0 {
factor *= 2;
factors.push(factor);
}

// remove the last factor if it is the same as the number of bins
if factors.last() == Some(&self.original_bins.len()) {
factors.pop();
}
factors
}

// Convert histogram bins to line points
fn update_line_points(&mut self) {
self.line.points = self
Expand Down Expand Up @@ -383,6 +413,28 @@ impl Histogram {
self.plot_settings.settings_ui(ui);
self.fits.fit_context_menu_ui(ui);
self.keybinds_ui(ui);

ui.separator();
ui.heading("Rebin");

let possible_factors = self.possible_rebin_factors();

ui.label("Rebin Factor:");

ui.horizontal_wrapped(|ui| {
for &factor in &possible_factors {
if ui
.selectable_label(
self.plot_settings.rebin_factor == factor,
format!("{}", factor),
)
.clicked()
{
self.plot_settings.rebin_factor = factor;
self.rebin();
}
}
});
}

// Renders the histogram using egui_plot
Expand Down
Loading

0 comments on commit b9956aa

Please sign in to comment.