From 3307a9eab5a26d28b05054082ace56762a04b5cd Mon Sep 17 00:00:00 2001 From: Jiekang Tian Date: Sat, 21 Nov 2026 14:47:09 +0800 Subject: [PATCH] fix(afm): calibrate force curves and fit in place --- crates/app/src/ui/canvas/interactions.rs | 10 +- crates/app/src/ui/command_exec.rs | 2 + crates/app/src/ui/commands.rs | 30 +- crates/app/src/ui/commands/identity.rs | 24 +- crates/app/src/ui/commands/ribbon.rs | 2 + crates/app/src/ui/commands/roster.rs | 12 +- crates/app/src/ui/commands/types.rs | 52 +++ crates/app/src/ui/commands_afm_tests.rs | 163 ++++++++ crates/app/src/ui/ribbon/layout.rs | 1 + crates/app/src/ui/tools/afm_fit.rs | 386 ++++++++++++++++++ crates/app/src/ui/tools/curve_fit.rs | 8 +- crates/app/src/ui/tools/mod.rs | 11 +- crates/app/src/ui/tools/task_card.rs | 11 +- crates/app/src/ui/tools/task_card_tabs.rs | 6 +- crates/core/src/actions/app_impl/apply.rs | 3 + crates/core/src/actions/app_impl/revert.rs | 5 + crates/core/src/actions/app_impl/validate.rs | 6 + crates/core/src/actions/mod.rs | 8 + crates/core/src/project/convert_views.rs | 28 +- crates/core/src/project/dto.rs | 2 + crates/core/src/project/lineage_convert.rs | 2 + crates/core/src/state/afm.rs | 98 ++++- crates/core/src/state/afm_fit.rs | 298 ++++++++++++++ crates/core/src/state/afm_fit_output.rs | 170 ++++++++ crates/core/src/state/axis_overrides.rs | 2 +- crates/core/src/state/lineage.rs | 2 + crates/core/src/state/mod.rs | 3 + crates/core/src/state/plot_object.rs | 62 ++- crates/core/src/state/ui_state.rs | 2 + crates/core/src/state/ui_state/task_dock.rs | 7 +- crates/core/tests/afm_canvas.rs | 15 + crates/figure/src/display_gain.rs | 17 + crates/io/src/nanoscope.rs | 38 +- crates/io/src/nanoscope_calibration.rs | 38 +- crates/io/src/nanoscope_force_axis.rs | 56 +++ crates/io/src/nanoscope_tests.rs | 46 +++ .../content/docs/guides/supported-formats.md | 26 +- .../docs/zh-cn/guides/supported-formats.md | 18 +- 38 files changed, 1607 insertions(+), 63 deletions(-) create mode 100644 crates/app/src/ui/commands_afm_tests.rs create mode 100644 crates/app/src/ui/tools/afm_fit.rs create mode 100644 crates/core/src/state/afm_fit.rs create mode 100644 crates/core/src/state/afm_fit_output.rs create mode 100644 crates/io/src/nanoscope_force_axis.rs diff --git a/crates/app/src/ui/canvas/interactions.rs b/crates/app/src/ui/canvas/interactions.rs index eeaf373f..bb33571c 100644 --- a/crates/app/src/ui/canvas/interactions.rs +++ b/crates/app/src/ui/canvas/interactions.rs @@ -33,6 +33,11 @@ pub(crate) fn handle_selection_drag( plot: PlotRect, ui: &Ui, ) { + if app.doc.datasets[dataset].as_afm().is_some() + && app.afm_force_plot_dataset(ci, object_id).is_none() + { + return; + } if app.plot_rejects_legacy_selection(ci, object_id) && app.plot_interaction_descriptor(ci, object_id).is_none() { @@ -138,7 +143,10 @@ pub(crate) fn finish_selection_drag( unit: None, source_stream: None, }); - let unit = if app.doc.datasets[dataset].as_mass_spec().is_some() { + let unit = if app.doc.datasets[dataset].as_afm().is_some() { + app.session.status = format!("Selected AFM X range: {:.3}-{:.3}.", x.min, x.max); + return; + } else if app.doc.datasets[dataset].as_mass_spec().is_some() { "min" } else { "ppm" diff --git a/crates/app/src/ui/command_exec.rs b/crates/app/src/ui/command_exec.rs index 217c6e4f..12182db4 100644 --- a/crates/app/src/ui/command_exec.rs +++ b/crates/app/src/ui/command_exec.rs @@ -322,6 +322,8 @@ fn execute_inner( CommandId::LineFit => reveal_tool_group(app, Tool::LineFit, ToolGroup::LineFit), CommandId::RunPeakFit => run_peak_fit(app, ctx), CommandId::CurveFit => open_curve_fit(app), + CommandId::FitAfmForce(branch) => super::tools::open_afm_force_fit_task(app, branch), + CommandId::AfmFit(command) => super::tools::run_afm_fit_command(app, command), CommandId::RunCurveFit => run_curve_fit(app), CommandId::Statistics => open_statistics(app), CommandId::ChartType => open_chart_type(app), diff --git a/crates/app/src/ui/commands.rs b/crates/app/src/ui/commands.rs index ee1bc046..fc56511f 100644 --- a/crates/app/src/ui/commands.rs +++ b/crates/app/src/ui/commands.rs @@ -5,7 +5,7 @@ use plotium_core::layout::{Align, Distribute, GutterPreset, SpacingMode}; use plotium_core::properties::PropertyStep; #[cfg(test)] use plotium_core::state::WorkflowTab; -use plotium_core::state::{Dataset, PlotiumApp, Tool, ToolGroup}; +use plotium_core::state::{AfmForceBranch, Dataset, PlotiumApp, Tool, ToolGroup}; mod craft; mod identity; use identity::command_identity; @@ -23,7 +23,7 @@ pub use ribbon::{Applicability, RibbonPlacement}; use roster::command_ids; pub(crate) const REPOSITORY_URL: &str = "https://github.com/nmrtist/plotium"; mod types; -pub use types::{CommandDescriptor, CommandExecutionClass, CommandId}; +pub use types::{AfmFitCommand, CommandDescriptor, CommandExecutionClass, CommandId}; pub fn catalog(app: &PlotiumApp) -> Vec { command_ids(app.session.recent_files.len()) @@ -70,6 +70,7 @@ pub fn describe(app: &PlotiumApp, id: CommandId) -> CommandDescriptor { }) }; let has_selectable_analysis_trace = || has_trace() && !is_time_nmr(); + let afm = || dataset().and_then(Dataset::as_afm); let has_generic_peak_fit_trace = || has_trace() && (is_frequency_nmr() || is_table()); let range = || active_dataset.and_then(|di| app.analysis_range_for(di)); let is_series = || dataset().is_some_and(Dataset::supports_region_analysis); @@ -329,6 +330,7 @@ pub fn describe(app: &PlotiumApp, id: CommandId) -> CommandDescriptor { ), CommandId::SelectRange => requires( has_selectable_analysis_trace() + || afm().is_some_and(|afm| afm.data.forces.is_some()) || dataset().is_some_and(|dataset| { dataset.tool_groups().contains(&ToolGroup::MassSpectrometry) }), @@ -443,6 +445,26 @@ pub fn describe(app: &PlotiumApp, id: CommandId) -> CommandDescriptor { ) }), CommandId::CurveFit => requires(is_table(), "Select a data table before fitting curves."), + CommandId::AfmFit(command) => super::tools::afm_fit_gate(app, command), + CommandId::FitAfmForce(branch) => requires( + afm().is_some(), + "Select an AFM dataset before fitting a force curve.", + ).and_then(|()| { + requires( + afm().is_some_and(|afm| afm.data.forces.is_some()), + "Import an AFM dataset containing force curves.", + ) + }).and_then(|()| { + requires( + afm().is_some_and(|afm| afm.force_branch_sample_count(branch) >= 3), + "Select an AFM dataset with at least three samples in this branch.", + ) + }).and_then(|()| { + requires( + afm().and_then(|data| app.afm_fit_target(data.resource_id, branch)).is_some(), + "Show an AFM force plot before fitting.", + ) + }), CommandId::RunCurveFit => { requires(is_table(), "Select a data table before running Curve Fit.").and_then(|()| { requires( @@ -585,6 +607,7 @@ pub fn describe(app: &PlotiumApp, id: CommandId) -> CommandDescriptor { Applicability::Always => true, Applicability::LineAlignmentOnly => app.trace_alignment_target().is_some(), Applicability::TableOnly => is_table(), + Applicability::AfmOnly => afm().is_some(), Applicability::XasOnly => dataset().and_then(Dataset::as_xas).is_some(), Applicability::SeriesOnly => is_series(), Applicability::Homonuclear2dOnly => dataset() @@ -607,6 +630,9 @@ pub fn describe(app: &PlotiumApp, id: CommandId) -> CommandDescriptor { } } #[cfg(test)] +#[path = "commands_afm_tests.rs"] +mod afm_tests; +#[cfg(test)] #[path = "commands_alignment_tests.rs"] mod alignment_tests; #[cfg(test)] diff --git a/crates/app/src/ui/commands/identity.rs b/crates/app/src/ui/commands/identity.rs index a94186dc..f06e5ba7 100644 --- a/crates/app/src/ui/commands/identity.rs +++ b/crates/app/src/ui/commands/identity.rs @@ -8,7 +8,7 @@ use egui_phosphor::regular as icon; use plotium_core::actions::ZOrder; use plotium_core::layout::{Align, Distribute, GutterPreset, SpacingMode}; use plotium_core::properties::PropertyStep; -use plotium_core::state::{PanelLayout, PlotiumApp, Tool}; +use plotium_core::state::{AfmForceBranch, PanelLayout, PlotiumApp, Tool}; use super::CommandId; @@ -290,7 +290,26 @@ pub(super) fn command_identity( ), CommandId::RunPeakFit => plain("Run Peak Fit", Some(icon::PLAY)), CommandId::CurveFit => plain("Fit Curves", Some(icon::FUNCTION)), + CommandId::FitAfmForce(branch) => plain( + match branch { + AfmForceBranch::Approach => "Fit Approach", + AfmForceBranch::Retract => "Fit Retract", + }, + Some(icon::FUNCTION), + ), CommandId::RunCurveFit => plain("Run Fit", Some(icon::PLAY)), + CommandId::AfmFit(command) => { + let (label, _, icon) = command.identity(); + let checked = (command == super::AfmFitCommand::ToggleOverlay).then(|| { + app.session + .ui + .afm_fit_task + .as_ref() + .and_then(|draft| app.afm_fit_result(draft.target)) + .is_some_and(|fit| fit.shown) + }); + (label.into(), Some(icon), checked) + } CommandId::Statistics => plain("Statistics", Some(icon::CHART_BAR)), CommandId::ChartType => plain("Chart Type…", Some(icon::CHART_SCATTER)), CommandId::FigureTypography => plain("Figure Typography…", Some(icon::TEXT_AA)), @@ -599,7 +618,10 @@ fn simple_stable_id(id: CommandId) -> &'static str { CommandId::LineFit => "analysis.line_fit", CommandId::RunPeakFit => "analysis.run_peak_fit", CommandId::CurveFit => "analysis.curve_fit", + CommandId::FitAfmForce(AfmForceBranch::Approach) => "analysis.afm_fit_approach", + CommandId::FitAfmForce(AfmForceBranch::Retract) => "analysis.afm_fit_retract", CommandId::RunCurveFit => "analysis.run_curve_fit", + CommandId::AfmFit(command) => command.identity().1, CommandId::Statistics => "analysis.statistics", CommandId::ChartType => "figure.chart_type", CommandId::FigureTypography => "figure.typography", diff --git a/crates/app/src/ui/commands/ribbon.rs b/crates/app/src/ui/commands/ribbon.rs index e06eb16d..323588e6 100644 --- a/crates/app/src/ui/commands/ribbon.rs +++ b/crates/app/src/ui/commands/ribbon.rs @@ -18,6 +18,7 @@ pub enum Applicability { Always, LineAlignmentOnly, TableOnly, + AfmOnly, XasOnly, SeriesOnly, Homonuclear2dOnly, @@ -99,6 +100,7 @@ pub(super) fn ribbon_placement(id: CommandId) -> Option { Applicability::ToolGroup(ToolGroup::LineFit), ), CommandId::CurveFit | CommandId::RunCurveFit => (Analyze, "Curve Fit", 0, TableOnly), + CommandId::FitAfmForce(_) => (Analyze, "AFM Force Fit", 0, Applicability::AfmOnly), CommandId::Statistics => (Analyze, "Statistics", 0, TableOnly), CommandId::Integrate | CommandId::Multiplets => ( Analyze, diff --git a/crates/app/src/ui/commands/roster.rs b/crates/app/src/ui/commands/roster.rs index 1c01f10e..b57b8575 100644 --- a/crates/app/src/ui/commands/roster.rs +++ b/crates/app/src/ui/commands/roster.rs @@ -5,9 +5,10 @@ use plotium_core::export::ExportFormat; use plotium_core::layout::{Align, Distribute, GutterPreset, SpacingMode}; use plotium_core::properties::PropertyStep; +use plotium_core::state::AfmForceBranch; use super::helpers::tool_commands; -use super::{CommandId, ZOrder}; +use super::{AfmFitCommand, CommandId, ZOrder}; /// The full command roster; only the recent-files arm depends on app state. pub(super) fn command_ids(recent_files: usize) -> Vec { @@ -126,6 +127,15 @@ pub(super) fn command_ids(recent_files: usize) -> Vec { CommandId::LineFit, CommandId::RunPeakFit, CommandId::CurveFit, + CommandId::FitAfmForce(AfmForceBranch::Approach), + CommandId::FitAfmForce(AfmForceBranch::Retract), + CommandId::AfmFit(AfmFitCommand::Run), + CommandId::AfmFit(AfmFitCommand::ToggleOverlay), + CommandId::AfmFit(AfmFitCommand::ZoomRange), + CommandId::AfmFit(AfmFitCommand::Duplicate), + CommandId::AfmFit(AfmFitCommand::ResidualPlot), + CommandId::AfmFit(AfmFitCommand::DataSheet), + CommandId::AfmFit(AfmFitCommand::ShowSource), CommandId::RunCurveFit, CommandId::Statistics, CommandId::ChartType, diff --git a/crates/app/src/ui/commands/types.rs b/crates/app/src/ui/commands/types.rs index a99df0c4..739ef80e 100644 --- a/crates/app/src/ui/commands/types.rs +++ b/crates/app/src/ui/commands/types.rs @@ -1,5 +1,55 @@ use super::*; +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AfmFitCommand { + Run, + ToggleOverlay, + ZoomRange, + Duplicate, + ResidualPlot, + DataSheet, + ShowSource, +} + +impl AfmFitCommand { + pub(super) const fn identity(self) -> (&'static str, &'static str, &'static str) { + use egui_phosphor::regular as icon; + match self { + Self::Run => ("Run AFM Fit", "analysis.afm_fit_run", icon::PLAY), + Self::ToggleOverlay => ( + "Show Fit on Source Plot", + "analysis.afm_fit_overlay", + icon::EYE, + ), + Self::ZoomRange => ( + "Zoom to Fit Range", + "analysis.afm_fit_zoom", + icon::MAGNIFYING_GLASS_PLUS, + ), + Self::Duplicate => ( + "Duplicate Plot with Fit", + "analysis.afm_fit_duplicate", + icon::COPY, + ), + Self::ResidualPlot => ( + "Create Residual Plot", + "analysis.afm_fit_residual", + icon::CHART_LINE, + ), + Self::DataSheet => ( + "Create Fit Data Sheet", + "analysis.afm_fit_sheet", + icon::TABLE, + ), + Self::ShowSource => ( + "Show AFM Source Plot", + "analysis.afm_fit_source", + icon::ARROW_LEFT, + ), + } + } +} + #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum CommandId { NewProject, @@ -121,6 +171,8 @@ pub enum CommandId { LineFit, RunPeakFit, CurveFit, + FitAfmForce(AfmForceBranch), + AfmFit(AfmFitCommand), RunCurveFit, Statistics, ChartType, diff --git a/crates/app/src/ui/commands_afm_tests.rs b/crates/app/src/ui/commands_afm_tests.rs new file mode 100644 index 00000000..dd3f936c --- /dev/null +++ b/crates/app/src/ui/commands_afm_tests.rs @@ -0,0 +1,163 @@ +use super::*; +use plotium_core::actions::Action; +use plotium_core::state::{ + AfmDataset, AnalysisSelection, AxisRange, DEFAULT_CANVAS_SIZE_MM, TaskDockTab, +}; +use plotium_io::{AfmData, AfmForceSet, AfmScale}; +use std::sync::Arc; + +fn insert_afm(forces: Option) -> PlotiumApp { + let mut app = PlotiumApp::new_with_settings(plotium_core::settings::Settings::default()); + let data = AfmDataset::load(AfmData { + images: Vec::new(), + forces, + source: "synthetic.spm".into(), + import_warnings: Vec::new(), + }); + app.execute_action(Action::insert_dataset_with_default_canvas( + &app, + Dataset::Afm(Box::new(data)), + "AFM".into(), + DEFAULT_CANVAS_SIZE_MM, + )); + app +} + +#[test] +fn afm_fit_group_is_visible_but_disabled_without_force_data() { + let app = insert_afm(None); + for branch in [AfmForceBranch::Approach, AfmForceBranch::Retract] { + let command = describe(&app, CommandId::FitAfmForce(branch)); + assert!(!command.enabled); + assert!(command.disabled_reason.unwrap().contains("force curves")); + assert_eq!(command.ribbon.unwrap().group, "AFM Force Fit"); + } +} + +#[test] +fn afm_fit_command_uses_the_selected_branch_and_range() { + let mut app = insert_afm(Some(AfmForceSet { + grid_width: 1, + grid_height: 1, + samples_per_curve: 12, + raw: Arc::from([5, 7, 9, 11, 13, 15, 20, 17, 14, 11, 8, 5]), + signal_scale: AfmScale { + multiplier: 1.0, + offset: 0.0, + unit: "nm".into(), + }, + sample_period_s: None, + z_positions: Some(Arc::from([0., 1., 2., 3., 4., 5., 0., 1., 2., 3., 4., 5.])), + display_order: Arc::from((0..12).collect::>()), + approach_samples: 6, + deflection_sensitivity_m_per_v: None, + spring_constant_n_per_m: Some(1.0), + })); + let source = app.doc.datasets[0].resource_id(); + let canvas = &app.doc.canvases[0]; + app.session.ui.analysis_selection = Some(AnalysisSelection { + dataset: source, + canvas: canvas.resource_id, + object: canvas.objects[0].id, + x_range: AxisRange::new(1.0, 4.0), + y_range: None, + field: None, + unit: None, + source_stream: None, + }); + + let ctx = egui::Context::default(); + assert!(describe(&app, CommandId::SelectRange).enabled); + let command = CommandId::FitAfmForce(AfmForceBranch::Retract); + assert!( + catalog(&app) + .iter() + .any(|entry| entry.id == command && entry.enabled) + ); + execute_without_clipboard(command, &mut app, &ctx); + assert_eq!(app.active_dataset(), Some(0)); + assert_eq!(app.session.ui.task_dock_active, Some(TaskDockTab::CurveFit)); + let draft = app.session.ui.afm_fit_task.as_ref().unwrap(); + assert_eq!(draft.model, plotium_analysis::models::LINEAR_ID); + let target = draft.target; + let viewport = app.afm_fit_plot(target).unwrap().viewport.clone(); + let active = app.session.active_canvas; + let run = |app: &mut PlotiumApp, command| { + execute_without_clipboard(CommandId::AfmFit(command), app, &ctx); + }; + run(&mut app, AfmFitCommand::Run); + let result = app.afm_fit_result(target).unwrap(); + assert!(result.converged); + assert_eq!(result.rows.len(), 4); + for ([x, y, predicted], [expected_x, expected_y]) in + result + .rows + .iter() + .zip([[1., 17.], [2., 14.], [3., 11.], [4., 8.]]) + { + assert_eq!(*x, expected_x); + assert!((y - expected_y).abs() < 1e-12); + assert!((predicted - y).abs() < 1e-6); + } + assert_eq!(app.doc.datasets.len(), 1); + assert_eq!(app.doc.canvases.len(), 1); + assert_eq!(app.session.active_canvas, active); + assert!(app.session.ui.sheet_open.is_none()); + let plot = app.afm_fit_plot(target).unwrap(); + assert_eq!(plot.viewport, viewport); + assert_eq!(plot.figure().series.len(), 3); + assert_eq!(plot.figure().series[2].name, "Retract fit"); + app.undo(); + assert!(app.afm_fit_result(target).is_none()); + assert_eq!(app.afm_fit_plot(target).unwrap().figure().series.len(), 2); + app.redo(); + + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("afm.plotium"); + plotium_core::project::save_project(&app, &path, true).unwrap(); + let reopened = plotium_core::project::load_project(&path).unwrap(); + assert!(reopened.afm_fit_result(target).unwrap().converged); + assert_eq!( + reopened.afm_fit_plot(target).unwrap().figure().series.len(), + 3 + ); + + run(&mut app, AfmFitCommand::ToggleOverlay); + assert_eq!(app.afm_fit_plot(target).unwrap().figure().series.len(), 2); + run(&mut app, AfmFitCommand::Duplicate); + assert_eq!(app.doc.datasets.len(), 1); + assert_eq!(app.session.active_canvas, Some(1)); + assert_eq!( + app.doc.canvases[1].objects[0] + .plot() + .unwrap() + .figure() + .series + .len(), + 3 + ); + assert!(!app.afm_fit_result(target).unwrap().shown); + run(&mut app, AfmFitCommand::ResidualPlot); + assert_eq!(app.session.active_canvas, Some(2)); + assert_eq!( + app.doc.canvases[2].objects[0] + .plot() + .unwrap() + .figure() + .series[0] + .name, + "Residual" + ); + assert!(app.session.ui.sheet_open.is_none()); + run(&mut app, AfmFitCommand::DataSheet); + assert_eq!(app.doc.canvases.len(), 3); + assert_eq!(app.session.ui.sheet_open, Some(2)); + assert_eq!( + app.doc.datasets[2] + .as_table() + .unwrap() + .series_bindings + .len(), + 3 + ); +} diff --git a/crates/app/src/ui/ribbon/layout.rs b/crates/app/src/ui/ribbon/layout.rs index 0c483cbf..17194815 100644 --- a/crates/app/src/ui/ribbon/layout.rs +++ b/crates/app/src/ui/ribbon/layout.rs @@ -405,6 +405,7 @@ pub(super) fn group_order(tab: WorkflowTab, group: &str) -> u8 { "Overlay", "Peak Fit", "Curve Fit", + "AFM Force Fit", "Statistics", "Interpret", ], diff --git a/crates/app/src/ui/tools/afm_fit.rs b/crates/app/src/ui/tools/afm_fit.rs new file mode 100644 index 00000000..171a017a --- /dev/null +++ b/crates/app/src/ui/tools/afm_fit.rs @@ -0,0 +1,386 @@ +use super::task_card::{self, TaskCardGeometry}; +use crate::ui::commands::{self, AfmFitCommand as Fit, CommandId}; +use egui::Ui; +use plotium_analysis::fit_model::{FitModelDefinition, FitSolver, RobustLoss, WeightMode}; +use plotium_analysis::models::{LINEAR_ID, builtin_models}; +use plotium_core::state::{ + AfmFitDraft, AfmForceBranch, AfmForceFit, FrameRef, PlotiumApp, TaskDockTab, +}; + +const TAB: TaskDockTab = TaskDockTab::CurveFit; + +pub(in crate::ui) fn open_task(app: &mut PlotiumApp, branch: AfmForceBranch) { + let Some(source) = app.active_dataset().and_then(|di| app.doc.datasets.get(di)) else { + return; + }; + let Some(target) = app.afm_fit_target(source.resource_id(), branch) else { + return; + }; + let result = app.afm_fit_result(target); + app.session.ui.afm_fit_task = Some(AfmFitDraft { + target, + model: result.map_or_else(|| LINEAR_ID.into(), |fit| fit.model.id.clone()), + options: result.map_or_else(Default::default, |fit| fit.options.clone()), + overlay: result.is_none_or(|fit| fit.shown), + error: None, + }); + match plotium_core::fit_model_library::FitModelLibrary::load() { + Ok(library) => app.session.ui.fit_custom_models = library.models, + Err(error) => { + app.session.ui.fit_custom_models.clear(); + app.session.status = format!("Could not load custom fit models: {error}"); + } + } + app.session.ui.curve_fit_task_dataset = None; + app.session.ui.curve_fit_task_collapsed = false; + app.session.ui.open_task_tab(TAB); +} + +pub(in crate::ui) fn afm_fit_gate(app: &PlotiumApp, command: Fit) -> Result<(), &'static str> { + let draft = app + .session + .ui + .afm_fit_task + .as_ref() + .ok_or("Open AFM Force Fit first.")?; + app.afm_fit_plot(draft.target) + .ok_or("Open AFM Force Fit on an available source plot.")?; + if matches!(command, Fit::Run | Fit::ShowSource) { + return Ok(()); + } + let result = app + .afm_fit_result(draft.target) + .ok_or("Run AFM Fit to create a result.")?; + if matches!(command, Fit::ToggleOverlay | Fit::Duplicate) && !result.converged { + return Err("Run a converged fit before displaying its fitted curve."); + } + Ok(()) +} + +pub(in crate::ui) fn run_command(app: &mut PlotiumApp, command: Fit) { + let Some(draft) = app.session.ui.afm_fit_task.clone() else { + return; + }; + let target = draft.target; + let result = match command { + Fit::Run => app + .run_afm_fit(target, &draft.model, draft.options, draft.overlay) + .map(|()| { + if app.session.active_canvas != app.doc.canvas_index(target.canvas) { + show_source(app, target); + } + }), + Fit::ToggleOverlay => app.toggle_afm_fit_overlay(target).map(|()| { + let shown = app.afm_fit_result(target).is_some_and(|fit| fit.shown); + if let Some(task) = &mut app.session.ui.afm_fit_task { + task.overlay = shown; + } + }), + Fit::ZoomRange => app.zoom_to_afm_fit_range(target).map(|()| { + if app.session.active_canvas != app.doc.canvas_index(target.canvas) { + show_source(app, target); + } + }), + Fit::Duplicate => app.duplicate_afm_fit_plot(target), + Fit::ResidualPlot => app.create_afm_fit_output(target, true), + Fit::DataSheet => app.create_afm_fit_output(target, false), + Fit::ShowSource => { + show_source(app, target); + Ok(()) + } + }; + if let Some(task) = &mut app.session.ui.afm_fit_task { + task.error = result.as_ref().err().cloned(); + } + if let Err(error) = result { + app.session.status = error; + } +} + +fn show_source(app: &mut PlotiumApp, target: plotium_core::state::AfmFitTarget) { + if let Some(ci) = app.doc.canvas_index(target.canvas) { + app.doc.canvases[ci].selected_object = Some(target.object); + app.reveal_board_frame(FrameRef::Page(ci)); + } +} + +pub(super) fn render_task(app: &mut PlotiumApp, host: &mut Ui) { + if !task_card::is_active(app, TAB) { + return; + } + let Some(target) = app + .session + .ui + .afm_fit_task + .as_ref() + .map(|draft| draft.target) + else { + return; + }; + let collapsed = app.session.ui.curve_fit_task_collapsed; + let TaskCardGeometry { + pos, + width, + body_height, + } = task_card::geometry(app, host, TAB, 310.0, collapsed); + let area_id = task_card::area_id(TAB); + let dark = host.visuals().dark_mode; + let mut close = false; + let mut collapse = false; + task_card::area(host, area_id, pos).show(host.ctx(), |ui| { + ui.set_width(width); + crate::ui::card_frame(dark, egui::Margin::ZERO).show(ui, |ui| { + task_card::header( + ui, + area_id, + "AFM Force Fit", + Some(target.branch.name().to_owned()), + |ui| { + close = crate::ui::panel_chrome::close(ui, "Close AFM Force Fit").clicked(); + collapse = + crate::ui::panel_chrome::collapse(ui, collapsed, "AFM Force Fit").clicked(); + }, + ); + if task_card::tab_bar(app, TAB, ui) { + ui.separator(); + } + if !collapsed { + ui.separator(); + task_card::sized_body(ui, body_height, |ui| { + egui::ScrollArea::vertical() + .auto_shrink([false, false]) + .show(ui, |ui| body(app, ui)); + }); + } + }); + if !collapsed { + task_card::resize_handles(app, ui, area_id, TAB, width, body_height); + } + }); + if close { + app.session.ui.close_task_tab(TAB); + } else if collapse { + app.session.ui.curve_fit_task_collapsed = !collapsed; + } +} + +fn body(app: &mut PlotiumApp, ui: &mut Ui) { + let Some(target) = app + .session + .ui + .afm_fit_task + .as_ref() + .map(|draft| draft.target) + else { + return; + }; + let Some(source) = app + .afm_fit_plot(target) + .and_then(|plot| plot.primary_dataset()) + .and_then(|id| app.doc.dataset_by_id(id)) + else { + ui.small("Source plot unavailable. Open AFM Force Fit from a force curve."); + return; + }; + ui.weak(source.display_name()); + if app.session.active_canvas != app.doc.canvas_index(target.canvas) { + command(app, ui, Fit::ShowSource, false); + } + if let Some(range) = app.afm_fit_selection(target) { + ui.label(format!("Selected X: {:.4} to {:.4}", range.min, range.max)); + } else { + ui.label("Range: Entire branch"); + } + let Some(draft) = &mut app.session.ui.afm_fit_task else { + return; + }; + let models = builtin_models() + .iter() + .chain(&app.session.ui.fit_custom_models) + .filter(|model| model.independent_variables.len() == 1 && model.responses.len() == 1); + let model_name = |model: &FitModelDefinition| model.name.as_str().to_owned(); + ui.horizontal(|ui| { + ui.label("Model"); + let selected = models + .clone() + .find(|model| model.id == draft.model) + .map(model_name) + .unwrap_or_else(|| draft.model.clone()); + egui::ComboBox::from_id_salt("afm_fit_model") + .selected_text(selected) + .show_ui(ui, |ui| { + for model in models { + ui.selectable_value(&mut draft.model, model.id.clone(), &model.name); + } + }); + }); + ui.checkbox(&mut draft.overlay, "Overlay on source plot"); + ui.collapsing("Fit Options", |ui| { + ui.horizontal_wrapped(|ui| { + ui.label("Solver"); + egui::ComboBox::from_id_salt("afm_fit_solver") + .selected_text(match draft.options.solver { + FitSolver::BoundedTrustRegion => "Bounded trust region", + FitSolver::LevenbergMarquardt => "Levenberg-Marquardt", + }) + .show_ui(ui, |ui| { + ui.selectable_value( + &mut draft.options.solver, + FitSolver::BoundedTrustRegion, + "Bounded trust region", + ); + ui.selectable_value( + &mut draft.options.solver, + FitSolver::LevenbergMarquardt, + "Levenberg-Marquardt", + ); + }); + }); + ui.horizontal_wrapped(|ui| { + let choices = [ + (WeightMode::Auto, "Auto"), + (WeightMode::Equal, "Equal"), + (WeightMode::Relative, "Relative"), + (WeightMode::Poisson, "Poisson"), + ]; + let selected = choices + .iter() + .find(|(mode, _)| *mode == draft.options.weights) + .map_or("Custom", |(_, label)| *label); + ui.label("Weights"); + egui::ComboBox::from_id_salt("afm_fit_weights") + .selected_text(selected) + .show_ui(ui, |ui| { + for (mode, label) in choices { + ui.selectable_value(&mut draft.options.weights, mode, label); + } + }); + }); + ui.horizontal_wrapped(|ui| { + ui.label("Robust loss"); + let choices = [ + (RobustLoss::None, "None"), + (RobustLoss::Huber(1.0), "Huber"), + (RobustLoss::SoftL1(1.0), "Soft-L1"), + (RobustLoss::Cauchy(1.0), "Cauchy"), + ]; + let selected = choices + .iter() + .find(|(value, _)| *value == draft.options.robust_loss) + .map_or("Custom", |(_, label)| *label); + egui::ComboBox::from_id_salt("afm_fit_loss") + .selected_text(selected) + .show_ui(ui, |ui| { + for (value, label) in choices { + ui.selectable_value(&mut draft.options.robust_loss, value, label); + } + }); + }); + ui.horizontal(|ui| { + ui.label("Starts"); + ui.add(egui::DragValue::new(&mut draft.options.multi_start).range(1..=32)); + }); + }); + command(app, ui, Fit::Run, true); + if let Some(error) = app + .session + .ui + .afm_fit_task + .as_ref() + .and_then(|task| task.error.as_ref()) + { + ui.colored_label(ui.visuals().error_fg_color, error); + } + ui.separator(); + if let Some(result) = app.afm_fit_result(target).cloned() { + summary(ui, &result); + ui.horizontal_wrapped(|ui| { + command(app, ui, Fit::ZoomRange, false); + ui.menu_button("Output", |ui| { + for choice in [ + Fit::ToggleOverlay, + Fit::Duplicate, + Fit::ResidualPlot, + Fit::DataSheet, + ] { + if command(app, ui, choice, false) { + ui.close(); + } + } + }); + }); + } else { + ui.weak("No result. Run AFM Fit."); + } +} + +fn summary(ui: &mut Ui, result: &AfmForceFit) { + let range = result.range(); + crate::typography::headline_label(ui, &result.model.name); + ui.small(format!( + "{:.4} to {:.4} {} | {} points", + range.min, + range.max, + result.x_unit, + result.rows.len() + )); + if result.converged { + ui.small("Converged"); + } else { + ui.colored_label(ui.visuals().warn_fg_color, "Did not converge"); + } + for p in &result.parameters { + let value = p.standard_error.map_or_else( + || format!("{:.5}", p.value), + |error| super::curve_fit::fmt_val_sigma(p.value, error), + ); + if result.model.id == LINEAR_ID && p.name == "a" { + ui.label(format!("Intercept (a): {value} {}", result.y_unit)); + } else if result.model.id == LINEAR_ID && p.name == "b" { + let unit = if result.x_unit.is_empty() { + format!("{}/sample", result.y_unit) + } else { + format!("{}/{}", result.y_unit, result.x_unit) + }; + ui.label(format!("Slope (b): {value} {unit}")); + } else { + ui.label(format!("{}: {value}", p.name)); + } + } + if let Some(r_squared) = result.r_squared { + ui.small(format!("R²: {r_squared:.5}")); + } + for notice in &result.notices { + ui.colored_label(ui.visuals().warn_fg_color, notice); + } +} + +fn command(app: &mut PlotiumApp, ui: &mut Ui, choice: Fit, primary: bool) -> bool { + let id = CommandId::AfmFit(choice); + let command = commands::describe(app, id); + let label = if choice == Fit::ToggleOverlay { + format!( + "{} {}", + if command.checked == Some(true) { + egui_phosphor::regular::CHECK + } else { + " " + }, + command.label + ) + } else { + format!("{} {}", command.icon.unwrap_or(""), command.label) + }; + let mut button = egui::Button::new(label); + if primary { + button = button.fill(ui.visuals().selection.bg_fill); + } + let clicked = ui + .add_enabled(command.enabled, button) + .on_disabled_hover_text(command.disabled_reason.unwrap_or_default()) + .clicked(); + if clicked { + commands::execute_without_clipboard(id, app, ui.ctx()); + } + clicked +} diff --git a/crates/app/src/ui/tools/curve_fit.rs b/crates/app/src/ui/tools/curve_fit.rs index 444aeef3..56074798 100644 --- a/crates/app/src/ui/tools/curve_fit.rs +++ b/crates/app/src/ui/tools/curve_fit.rs @@ -30,6 +30,7 @@ pub(crate) fn open_task(app: &mut PlotiumApp, di: usize) { return; } ensure_curve_fit_state(app, di); + app.session.ui.afm_fit_task = None; app.session.ui.curve_fit_task_dataset = Some(di); app.session.ui.open_task_tab(TaskDockTab::CurveFit); } @@ -750,10 +751,13 @@ pub(super) fn run_curve_fit(app: &mut PlotiumApp, di: usize) { } } -/// Captured b encoding takes priority over acquisition gradient constants; -/// delay rulers use inversion recovery, other tables a mono-exponential. fn default_fit_model(table: &TableDataset) -> &'static str { use plotium_analysis::models; + if table.lineage.as_ref().is_some_and(|lineage| { + lineage.kind == plotium_core::state::DerivationKind::AfmForceBranchTable + }) { + return models::LINEAR_ID; + } if table.meta.diffusion_input.is_some() { return models::DIFFUSION_B_ID; } diff --git a/crates/app/src/ui/tools/mod.rs b/crates/app/src/ui/tools/mod.rs index 08e6c71a..9fa0ec32 100644 --- a/crates/app/src/ui/tools/mod.rs +++ b/crates/app/src/ui/tools/mod.rs @@ -1,6 +1,7 @@ //! Renders a single `ToolGroup` for the active dataset; new data domains plug //! in here without touching the sidebar or the toolbar. +mod afm_fit; mod craft; mod cursors; mod curve_fit; @@ -35,6 +36,10 @@ use slice::slice_group; use symmetry::symmetry_group; pub(super) use line_fit::line_fit_shape_id; + +pub(super) use afm_fit::{ + afm_fit_gate, open_task as open_afm_force_fit_task, run_command as run_afm_fit_command, +}; pub(super) use xps::open_workbench as open_xps_workbench; #[derive(Clone, Copy, Default)] @@ -76,7 +81,11 @@ pub(crate) fn render_region_task(app: &mut PlotiumApp, ui: &mut Ui) { } pub(crate) fn render_curve_fit_task(app: &mut PlotiumApp, ui: &mut Ui) { - curve_fit::render_task(app, ui); + if app.session.ui.afm_fit_task.is_some() { + afm_fit::render_task(app, ui); + } else { + curve_fit::render_task(app, ui); + } } pub(crate) fn render_statistics_task(app: &mut PlotiumApp, ui: &mut Ui) { diff --git a/crates/app/src/ui/tools/task_card.rs b/crates/app/src/ui/tools/task_card.rs index 5a15690e..c0bd92e5 100644 --- a/crates/app/src/ui/tools/task_card.rs +++ b/crates/app/src/ui/tools/task_card.rs @@ -115,7 +115,7 @@ fn card_width(host: egui::Rect, tab: TaskDockTab, preferred_width: f32) -> f32 { .min(canvas_preserving_maximum) } -fn area_id(tab: TaskDockTab) -> Id { +pub(super) fn area_id(tab: TaskDockTab) -> Id { match tab { TaskDockTab::Dosy => Id::new("dosy_task_card"), TaskDockTab::Processing => Id::new("processing_task_card"), @@ -195,7 +195,14 @@ pub(crate) fn visible_area_id(app: &PlotiumApp) -> Option { .ui .curve_fit_task_dataset .filter(|dataset| Some(*dataset) == active) - .map(|_| Id::new("curve_fit_task_card")), + .map(|_| area_id(TaskDockTab::CurveFit)) + .or_else(|| { + app.session + .ui + .afm_fit_task + .as_ref() + .map(|_| area_id(TaskDockTab::CurveFit)) + }), TaskDockTab::Statistics => app .session .ui diff --git a/crates/app/src/ui/tools/task_card_tabs.rs b/crates/app/src/ui/tools/task_card_tabs.rs index 6c17f53e..68f2081f 100644 --- a/crates/app/src/ui/tools/task_card_tabs.rs +++ b/crates/app/src/ui/tools/task_card_tabs.rs @@ -52,7 +52,11 @@ pub(in crate::ui::tools) fn tab_bar( TaskDockTab::CurveFit, icon::CHART_LINE_UP, "Fit", - app.session.ui.curve_fit_task_dataset, + app.session.ui.curve_fit_task_dataset.or_else(|| { + let target = app.session.ui.afm_fit_task.as_ref()?.target; + app.doc + .dataset_index(app.afm_fit_plot(target)?.primary_dataset()?) + }), ), ( TaskDockTab::Statistics, diff --git a/crates/core/src/actions/app_impl/apply.rs b/crates/core/src/actions/app_impl/apply.rs index 4d355f85..8895ea7c 100644 --- a/crates/core/src/actions/app_impl/apply.rs +++ b/crates/core/src/actions/app_impl/apply.rs @@ -67,6 +67,9 @@ impl PlotiumApp { } } Action::SetProtein { after, .. } => self.doc.protein = after.clone(), + Action::SetAfmForceFits { target, after, .. } => { + self.apply_afm_force_fits(*target, after)?; + } Action::InstallScientificState { after, .. } => { self.install_processing_snapshots(after)? } diff --git a/crates/core/src/actions/app_impl/revert.rs b/crates/core/src/actions/app_impl/revert.rs index 5243b495..bed3a51e 100644 --- a/crates/core/src/actions/app_impl/revert.rs +++ b/crates/core/src/actions/app_impl/revert.rs @@ -30,6 +30,11 @@ impl PlotiumApp { } } Action::SetProtein { before, .. } => self.doc.protein = before.clone(), + Action::SetAfmForceFits { target, before, .. } => { + if let Err(error) = self.apply_afm_force_fits(*target, before) { + self.session.status = error.to_string(); + } + } Action::InstallScientificState { before, .. } => { if let Err(error) = self.install_processing_snapshots(before) { self.session.status = error.to_string(); diff --git a/crates/core/src/actions/app_impl/validate.rs b/crates/core/src/actions/app_impl/validate.rs index dc8f42e3..ac46801a 100644 --- a/crates/core/src/actions/app_impl/validate.rs +++ b/crates/core/src/actions/app_impl/validate.rs @@ -350,6 +350,12 @@ pub(super) fn validate_action( } } } + Action::SetAfmForceFits { target, .. } if app.afm_fit_plot(*target).is_none() => { + return Err(ActionApplyError::StaleTarget(format!( + "AFM plot {}", + target.object + ))); + } Action::SetCurveBinding { canvas, object, .. } => { if app .doc diff --git a/crates/core/src/actions/mod.rs b/crates/core/src/actions/mod.rs index 3b3a68a6..4b0fa80f 100644 --- a/crates/core/src/actions/mod.rs +++ b/crates/core/src/actions/mod.rs @@ -14,6 +14,7 @@ use plotium_processing::xrd::XrdProcessing; use plotium_processing::{AxisPipeline, Params2D, Preset2D}; mod app_impl; +pub(crate) use app_impl::ActionApplyError; pub(crate) mod deletion; pub use deletion::DeletionChange; mod arrange; @@ -242,6 +243,11 @@ pub enum Action { before: CanvasViewport, after: CanvasViewport, }, + SetAfmForceFits { + target: crate::state::AfmFitTarget, + before: Vec>, + after: Vec>, + }, SetXViewportLinks { canvas: usize, before: Vec, @@ -695,6 +701,7 @@ impl Action { Self::SetChartType { .. } => "chart type", Self::SetStackSpec { .. } => "stack setting", Self::SetPlotIntent { .. } => "plot intent", + Self::SetAfmForceFits { .. } => "AFM force fit", Self::SetCanvasSize { .. } | Self::SetPageLayout { .. } => "page layout", _ => "edit", } @@ -730,6 +737,7 @@ impl Action { Self::SetPanelMeta { before, after, .. } => before == after, Self::SetObjectFlags { before, after, .. } => before == after, Self::SetPlotIntent { before, after, .. } => before == after, + Self::SetAfmForceFits { before, after, .. } => before == after, // Inserting or removing a bookmark always changes the list. Self::BoardViewInsert { .. } | Self::BoardViewRemove { .. } => false, Self::SetCurveBinding { before, after, .. } => before.binding == after.binding, diff --git a/crates/core/src/project/convert_views.rs b/crates/core/src/project/convert_views.rs index 28d784b7..eda7d38d 100644 --- a/crates/core/src/project/convert_views.rs +++ b/crates/core/src/project/convert_views.rs @@ -130,6 +130,7 @@ pub fn canvas_to_view( viewport: None, axis_overrides: None, intent: None, + afm_force_fits: Vec::new(), text: None, shape: None, image: None, @@ -162,6 +163,7 @@ pub fn canvas_to_view( }, Dataset::Table(_) => "line_plot", Dataset::Electrophysiology(_) => "line_plot", + Dataset::Afm(_) if plot.chart.type_id == "afm_force_curve" => "line_plot", Dataset::Afm(_) => "heatmap", Dataset::MassSpec(_) => "line_plot", Dataset::Xrd(_) => "line_plot", @@ -199,6 +201,7 @@ pub fn canvas_to_view( viewport: Some(ViewportDto::from_viewport(&plot.viewport)), axis_overrides: AxisOverridesDto::from_overrides(&plot.axis_overrides), intent: (!plot.intent.is_empty()).then(|| plot.intent.clone()), + afm_force_fits: plot.afm_force_fits().to_vec(), ..base(kind) }) } @@ -428,7 +431,7 @@ pub fn view_to_canvas( preserve_aspect: image.preserve_aspect, }) } - "line_plot" | "contour_plot" | "stack_plot" | "plot" => { + "line_plot" | "contour_plot" | "stack_plot" | "heatmap" | "plot" => { let resolve = |input: &str| { recipe_to_dataset.get(input).copied().ok_or_else(|| { ProjectError::Invalid(format!( @@ -548,18 +551,14 @@ pub fn view_to_canvas( .as_ref() .map(|dto| projections_from_dto(dto, recipe_to_dataset, &app.doc.datasets)) .unwrap_or_default(); - // A snapshot is a picture of a figure the document could still - // produce. When the dataset's selected DOSY map did not survive - // the load, it no longer can: replaying the snapshot would leave - // the saved contours on the canvas while the load report says the - // stack is being shown. Rebuild from the restored document - // instead, so what is drawn and what is reported agree. + // Missing DOSY maps invalidate snapshots; AFM overlays are restored from fit results. let map_unavailable = app.doc.datasets[di] .as_nmr2d() .is_some_and(|dataset| dataset.missing_selected_map_note().is_some()); + let rebuild = map_unavailable || !view_object.afm_force_fits.is_empty(); let mut figure = match &view_object.snapshot { - Some(snapshot) if !map_unavailable => read_json(zip, &snapshot.figure) - .unwrap_or_else(|_| { + Some(snapshot) if !rebuild => { + read_json(zip, &snapshot.figure).unwrap_or_else(|_| { app.build_object_figure( display_owner, &binding, @@ -568,7 +567,8 @@ pub fn view_to_canvas( &projections, size_mm, ) - }), + }) + } _ => app.build_object_figure( display_owner, &binding, @@ -592,11 +592,8 @@ pub fn view_to_canvas( axis_overrides.annotations.validate().map_err(|e| { ProjectError::Invalid(format!("view {view_id} object {}: {e}", view_object.id)) })?; - // Must track the branch actually taken above: a snapshot has axis - // overrides and the viewport already baked in, a rebuilt figure - // does not, so a bypassed snapshot has to be treated as absent - // here or the rebuilt figure would lose both. - let snapshot_backed = view_object.snapshot.is_some() && !map_unavailable; + // Only a replayed snapshot has axis overrides baked in. + let snapshot_backed = view_object.snapshot.is_some() && !rebuild; let derived_axes = if snapshot_backed { let derived = app.build_object_figure( display_owner, @@ -644,6 +641,7 @@ pub fn view_to_canvas( )) })?; } + plot.set_afm_force_fits(view_object.afm_force_fits.clone()); CanvasObjectKind::Plot(Box::new(plot)) } other => { diff --git a/crates/core/src/project/dto.rs b/crates/core/src/project/dto.rs index 2e427569..ac9ab0ae 100644 --- a/crates/core/src/project/dto.rs +++ b/crates/core/src/project/dto.rs @@ -409,6 +409,8 @@ pub struct ViewCanvasObject { /// The author's stated purpose and claim of a plot; absent when unset. #[serde(default, skip_serializing_if = "Option::is_none")] pub intent: Option, + #[serde(default, skip_serializing_if = "Vec::is_empty")] + pub afm_force_fits: Vec>, #[serde(default, skip_serializing_if = "Option::is_none")] pub text: Option, #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/crates/core/src/project/lineage_convert.rs b/crates/core/src/project/lineage_convert.rs index 53f6af64..80e7c911 100644 --- a/crates/core/src/project/lineage_convert.rs +++ b/crates/core/src/project/lineage_convert.rs @@ -16,6 +16,7 @@ pub(super) fn derivation_kind_to_str(kind: DerivationKind) -> &'static str { DerivationKind::IvTable => "iv_table", DerivationKind::StatisticsTable => "statistics_table", DerivationKind::RelationalTransform => "relational_transform", + DerivationKind::AfmForceBranchTable => "afm_force_branch_table", } } @@ -34,6 +35,7 @@ fn derivation_kind_from_str(value: &str) -> Result { "iv_table" => Ok(DerivationKind::IvTable), "statistics_table" => Ok(DerivationKind::StatisticsTable), "relational_transform" => Ok(DerivationKind::RelationalTransform), + "afm_force_branch_table" => Ok(DerivationKind::AfmForceBranchTable), other => Err(ProjectError::Invalid(format!( "unknown dataset derivation kind {other}" ))), diff --git a/crates/core/src/state/afm.rs b/crates/core/src/state/afm.rs index 710753b5..071d070f 100644 --- a/crates/core/src/state/afm.rs +++ b/crates/core/src/state/afm.rs @@ -2,6 +2,22 @@ use super::*; use plotium_figure::{AxisFrame, ColormapId, Contour, ContourStyle, HeatmapGrid, Series}; use std::sync::Arc; +#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] +pub enum AfmForceBranch { + Approach, + Retract, +} + +impl AfmForceBranch { + pub const fn name(self) -> &'static str { + match self { + Self::Approach => "Approach", + Self::Retract => "Retract", + } + } +} + #[derive(Clone)] pub struct AfmDataset { pub resource_id: DatasetId, @@ -140,8 +156,7 @@ impl AfmDataset { } }) }; - let displacement_factor = physical_length_factor(&forces.signal_scale.unit) - .or(forces.deflection_sensitivity_m_per_v); + let displacement_factor = deflection_length_factor(forces); let spring_constant = forces .spring_constant_n_per_m .filter(|value| value.is_finite() && *value > 0.0); @@ -164,11 +179,8 @@ impl AfmDataset { }) .collect(); let split = forces.approach_samples.min(ordered.len()); - let x_label = if forces.z_positions.is_some() { - "Z" - } else { - "Time" - }; + let (x_name, x_unit) = force_x_quantity(forces); + let x_label = axis_label(x_name, x_unit); let y_label = if displacement_factor.is_some() && spring_constant.is_some() { "Force (nN)" } else if displacement_factor.is_some() { @@ -191,16 +203,28 @@ impl AfmDataset { Axis::new(x_label, x_min, x_max), Axis::new(y_label, y_min, y_max), ); - figure - .series - .push(Series::line("Approach", ordered[..split].to_vec())); + figure.series.push(Series::line( + AfmForceBranch::Approach.name(), + ordered[..split].to_vec(), + )); figure.series.push( - Series::line("Retract", ordered[split..].to_vec()) + Series::line(AfmForceBranch::Retract.name(), ordered[split..].to_vec()) .colored(Color::rgb(0xd1, 0x24, 0x2a)), ); Some(figure) } + pub fn force_branch_sample_count(&self, branch: AfmForceBranch) -> usize { + let Some(forces) = &self.data.forces else { + return 0; + }; + let approach = forces.approach_samples.min(forces.samples_per_curve); + match branch { + AfmForceBranch::Approach => approach, + AfmForceBranch::Retract => forces.samples_per_curve - approach, + } + } + fn image_for_field(&self, field: FieldId) -> Option<&plotium_io::AfmImageChannel> { self.data .images @@ -211,6 +235,58 @@ impl AfmDataset { } } +impl PlotiumApp { + pub fn afm_force_plot_dataset(&self, canvas: usize, object: ObjectId) -> Option { + let plot = self.doc.canvases.get(canvas)?.object(object)?.plot()?; + let dataset = plot.primary_dataset()?; + (plot.chart.type_id == "afm_force_curve" + && self.doc.dataset_by_id(dataset)?.as_afm().is_some()) + .then_some(dataset) + } +} + +fn axis_label(name: &str, unit: &str) -> String { + if unit.is_empty() { + name.into() + } else { + format!("{name} ({unit})") + } +} + +pub(super) fn force_x_quantity(forces: &plotium_io::AfmForceSet) -> (&'static str, &'static str) { + if forces.z_positions.is_some() { + ("Z", "nm") + } else if forces.sample_period_s.is_some() { + ("Time", "s") + } else { + ("Sample index", "") + } +} + +pub(super) fn force_y_quantity(forces: &plotium_io::AfmForceSet) -> (&'static str, &str) { + if deflection_length_factor(forces).is_none() { + ("Signal", &forces.signal_scale.unit) + } else if forces + .spring_constant_n_per_m + .is_some_and(|v| v.is_finite() && v > 0.0) + { + ("Force", "nN") + } else { + ("Deflection", "nm") + } +} + +fn deflection_length_factor(forces: &plotium_io::AfmForceSet) -> Option { + let unit = forces.signal_scale.unit.split('/').next()?.trim(); + physical_length_factor(unit) + .or_else(|| { + unit.eq_ignore_ascii_case("V") + .then_some(forces.deflection_sensitivity_m_per_v) + .flatten() + }) + .filter(|value| value.is_finite() && *value > 0.0) +} + fn physical_length_factor(unit: &str) -> Option { let normalized = unit .split('/') diff --git a/crates/core/src/state/afm_fit.rs b/crates/core/src/state/afm_fit.rs new file mode 100644 index 00000000..df66bb77 --- /dev/null +++ b/crates/core/src/state/afm_fit.rs @@ -0,0 +1,298 @@ +use super::*; +use plotium_analysis::fit_model::{FitDataset, FitModelDefinition, FitOptions, fit_model}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::sync::Arc; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct AfmFitTarget { + pub canvas: CanvasId, + pub object: ObjectId, + pub source: DatasetId, + pub branch: AfmForceBranch, +} + +#[derive(Clone)] +pub struct AfmFitDraft { + pub target: AfmFitTarget, + pub model: String, + pub options: FitOptions, + pub overlay: bool, + pub error: Option, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct AfmFitParameter { + pub name: String, + pub value: f64, + pub standard_error: Option, +} + +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct AfmForceFit { + pub source: DatasetId, + pub branch: AfmForceBranch, + pub model: FitModelDefinition, + pub options: FitOptions, + pub shown: bool, + pub converged: bool, + pub parameters: Vec, + pub r_squared: Option, + pub notices: Vec, + pub x_name: String, + pub x_unit: String, + pub y_name: String, + pub y_unit: String, + /// Each row stores X, observation, and prediction, in acquisition order. + pub rows: Vec<[f64; 3]>, +} + +impl AfmForceFit { + pub fn range(&self) -> AxisRange { + self.rows.iter().fold( + AxisRange { + min: f64::INFINITY, + max: f64::NEG_INFINITY, + }, + |range, [x, _, _]| AxisRange { + min: range.min.min(*x), + max: range.max.max(*x), + }, + ) + } +} + +impl PlotiumApp { + pub(crate) fn apply_afm_force_fits( + &mut self, + target: AfmFitTarget, + fits: &[Arc], + ) -> Result<(), crate::actions::ActionApplyError> { + let plot = self + .doc + .canvas_index(target.canvas) + .and_then(|ci| self.doc.canvases[ci].object_mut(target.object)) + .and_then(CanvasObject::plot_mut) + .ok_or_else(|| { + crate::actions::ActionApplyError::StaleTarget(format!("AFM plot {}", target.object)) + })?; + plot.set_afm_force_fits(fits.to_vec()); + Ok(()) + } + pub fn afm_fit_target( + &self, + source: DatasetId, + branch: AfmForceBranch, + ) -> Option { + let on_page = |ci: usize| { + let canvas = self.doc.canvases.get(ci)?; + let valid = |object| { + canvas.object(object).is_some_and(|item| item.visible) + && self.afm_force_plot_dataset(ci, object) == Some(source) + }; + let object = canvas + .active_plot_object_id() + .filter(|&id| valid(id)) + .or_else(|| { + canvas + .objects + .iter() + .map(|item| item.id) + .find(|&id| valid(id)) + })?; + Some(AfmFitTarget { + canvas: canvas.resource_id, + object, + source, + branch, + }) + }; + self.session + .active_canvas + .and_then(on_page) + .or_else(|| (0..self.doc.canvases.len()).find_map(on_page)) + } + + pub fn afm_fit_plot(&self, target: AfmFitTarget) -> Option<&PlotObject> { + let ci = self.doc.canvas_index(target.canvas)?; + (self.afm_force_plot_dataset(ci, target.object)? == target.source).then_some(())?; + self.doc.canvases[ci].object(target.object)?.plot() + } + + pub fn afm_fit_result(&self, target: AfmFitTarget) -> Option<&Arc> { + let plot = self.afm_fit_plot(target)?; + let source = plot.primary_dataset()?; + plot.afm_force_fits() + .iter() + .find(|fit| fit.source == source && fit.branch == target.branch) + } + + pub fn afm_fit_selection(&self, target: AfmFitTarget) -> Option { + let selection = self.session.ui.analysis_selection.as_ref()?; + (selection.canvas == target.canvas + && selection.object == target.object + && self.afm_fit_plot(target)?.primary_dataset() == Some(selection.dataset)) + .then_some(selection.x_range) + } + + pub fn run_afm_fit( + &mut self, + target: AfmFitTarget, + model_id: &str, + options: FitOptions, + shown: bool, + ) -> Result<(), String> { + let plot = self + .afm_fit_plot(target) + .ok_or("Select a force curve before fitting.")?; + let source = plot + .primary_dataset() + .ok_or("The force curve source is unavailable.")?; + let afm = self + .doc + .dataset_by_id(source) + .and_then(Dataset::as_afm) + .ok_or("The AFM source is unavailable.")?; + let forces = afm + .data + .forces + .as_ref() + .ok_or("The AFM source has no force curves.")?; + let figure = afm + .field_catalog + .id_for_key("afm.force_curve") + .and_then(|field| afm.force_figure(field)) + .ok_or("The force curve is unavailable.")?; + let range = self.afm_fit_selection(target); + let branch = figure + .series + .iter() + .find(|s| s.name == target.branch.name()) + .ok_or("The selected AFM branch is unavailable.")?; + let (x, y): (Vec<_>, Vec<_>) = branch + .points + .iter() + .copied() + .filter(|[x, _]| range.is_none_or(|r| *x >= r.min && *x <= r.max)) + .map(|[x, y]| (x, y)) + .unzip(); + if x.len() < 3 { + return Err( + "Select a range with at least three AFM samples, or clear the range.".into(), + ); + } + if x.iter().chain(&y).any(|v| !v.is_finite()) || x.iter().all(|v| *v == x[0]) { + return Err("Choose an AFM range with finite samples and distinct X values.".into()); + } + let model = super::app_impl_analysis::resolve_table_fit_model(model_id, false)?; + let ([input], [response]) = ( + model.independent_variables.as_slice(), + model.responses.as_slice(), + ) else { + return Err("Choose a model with one independent variable and one response.".into()); + }; + let data = FitDataset { + id: target.branch.name().into(), + inputs: BTreeMap::from([(input.id.clone(), x.clone())]), + responses: BTreeMap::from([(response.id.clone(), y)]), + sigmas: BTreeMap::new(), + constants: BTreeMap::new(), + }; + let result = fit_model(model, vec![data], &[], options) + .map_err(|error| format!("AFM fit failed: {error}"))?; + let (x_name, x_unit) = super::afm::force_x_quantity(forces); + let (y_name, y_unit) = super::afm::force_y_quantity(forces); + let rows = result + .points + .iter() + .map(|p| { + x.get(p.row) + .map(|&v| [v, p.observed, p.predicted]) + .filter(|row| { + row.iter().all(|v| v.is_finite()) && (row[1] - row[2]).is_finite() + }) + .ok_or("The AFM fit returned a non-finite prediction.") + }) + .collect::, _>>()?; + if result.parameters.iter().any(|p| !p.value.is_finite()) { + return Err("The AFM fit returned a non-finite parameter.".into()); + } + let parameters = result + .parameters + .iter() + .map(|p| AfmFitParameter { + name: p.parameter.clone(), + value: p.value, + standard_error: p.standard_error.is_finite().then_some(p.standard_error), + }) + .collect(); + let mut after = plot.afm_force_fits().to_vec(); + // Refit only replaces this source and branch. + after.retain(|fit| !(fit.source == source && fit.branch == target.branch)); + after.push(Arc::new(AfmForceFit { + source, + branch: target.branch, + model: result.model, + options: result.options, + shown, + converged: result.converged, + parameters, + r_squared: result + .statistics + .r_squared + .is_finite() + .then_some(result.statistics.r_squared), + notices: result.notices, + x_name: x_name.into(), + x_unit: x_unit.into(), + y_name: y_name.into(), + y_unit: y_unit.into(), + rows, + })); + self.change_afm_fits(target, after)?; + self.session.status = if result.converged { + format!( + "Fitted {} ({} points).", + target.branch.name(), + result.points.len() + ) + } else { + "AFM fit did not converge. Inspect the estimates before using them.".into() + }; + Ok(()) + } + + pub fn toggle_afm_fit_overlay(&mut self, target: AfmFitTarget) -> Result<(), String> { + let fit = self.afm_fit_result(target).ok_or("Run AFM Fit first.")?; + let mut after = self + .afm_fit_plot(target) + .ok_or("The source plot is unavailable.")? + .afm_force_fits() + .to_vec(); + for stored in &mut after { + if Arc::ptr_eq(stored, fit) { + let stored = Arc::make_mut(stored); + stored.shown = !stored.shown; + } + } + self.change_afm_fits(target, after) + } + + fn change_afm_fits( + &mut self, + target: AfmFitTarget, + after: Vec>, + ) -> Result<(), String> { + let before = self + .afm_fit_plot(target) + .ok_or("The source plot is unavailable.")? + .afm_force_fits() + .to_vec(); + self.try_execute_action(crate::actions::Action::SetAfmForceFits { + target, + before, + after, + }) + .map_err(|error| error.to_string()) + } +} diff --git a/crates/core/src/state/afm_fit_output.rs b/crates/core/src/state/afm_fit_output.rs new file mode 100644 index 00000000..84af95af --- /dev/null +++ b/crates/core/src/state/afm_fit_output.rs @@ -0,0 +1,170 @@ +use super::*; +use crate::actions::Action; +use std::sync::Arc; + +impl PlotiumApp { + pub fn duplicate_afm_fit_plot(&mut self, target: AfmFitTarget) -> Result<(), String> { + let fit = self + .afm_fit_result(target) + .filter(|fit| fit.converged) + .ok_or("Run a converged AFM fit before duplicating the plot.")?; + let ci = self + .doc + .canvas_index(target.canvas) + .ok_or("The source canvas is unavailable.")?; + let source = &self.doc.canvases[ci]; + let mut object = source + .object(target.object) + .cloned() + .ok_or("The source plot is unavailable.")?; + let name = format!("{} - {} Fit", object.name, fit.branch.name()); + let mut page = CanvasDocument::new( + name, + [ + object.frame.width / MM_TO_PT, + object.frame.height / MM_TO_PT, + ], + ); + page.background = source.background; + object.id = page.allocate_object_id(); + object.frame.x = 0.0; + object.frame.y = 0.0; + object.locked = false; + let mut fit = fit.clone(); + Arc::make_mut(&mut fit).shown = true; + object + .plot_mut() + .ok_or("The source plot is unavailable.")? + .set_afm_force_fits(vec![fit]); + let object_id = object.id; + page.objects.push(object); + page.create_panel_for_plot(object_id) + .ok_or("Could not create the AFM fit panel.")?; + let index = self.doc.canvases.len(); + self.try_execute_action(Action::InsertCanvas { + index, + canvas: Box::new(page), + active_before: self.session.active_canvas, + activate: true, + auto_place: true, + }) + .map_err(|error| error.to_string())?; + self.reveal_board_frame(FrameRef::Page(index)); + Ok(()) + } + + pub fn create_afm_fit_output( + &mut self, + target: AfmFitTarget, + residual_plot: bool, + ) -> Result<(), String> { + let fit = self + .afm_fit_result(target) + .ok_or("Run AFM Fit before creating an output.")?; + let source = fit.source; + let name = format!( + "{} - {} {}", + self.doc + .dataset_by_id(source) + .ok_or("The AFM source is unavailable.")? + .display_name(), + fit.branch.name(), + if residual_plot { + "Residuals" + } else { + "Fit Data" + }, + ); + let column = |name: &str, value: fn([f64; 3]) -> f64| FloatSeries { + name: name.into(), + unit: fit.y_unit.clone(), + values: fit + .rows + .iter() + .copied() + .map(|row| Some(value(row))) + .collect(), + uncertainty: None, + fit: None, + }; + let mut series = vec![column("Residual", |[_, observed, predicted]| { + observed - predicted + })]; + if !residual_plot { + series.insert(0, column(&fit.y_name, |[_, observed, _]| observed)); + series.insert(1, column("Predicted", |[_, _, predicted]| predicted)); + } + let mut table = materialized_float_series_table( + ( + fit.x_name.clone(), + fit.x_unit.clone(), + fit.rows.iter().map(|[x, _, _]| Some(*x)).collect(), + ), + series, + "plotium.afm.fit-output.v1", + ) + .map_err(|error| format!("Could not create AFM fit output: {error}"))?; + table.name = Some(name.clone()); + table.lineage = Some(DatasetLineage::new( + DerivationKind::AfmForceBranchTable, + [source], + )); + if residual_plot { + let index = self.doc.canvases.len(); + let action = Action::insert_dataset_with_default_canvas( + self, + Dataset::Table(Box::new(table)), + name, + DEFAULT_CANVAS_SIZE_MM, + ); + self.try_execute_action(action) + .map_err(|error| error.to_string())?; + self.reveal_board_frame(FrameRef::Page(index)); + } else { + let size = table.board_rect_pt(); + table.board_pos = next_board_frame_pos(self, [size.width, size.height]); + let id = table.resource_id; + self.try_execute_action(Action::CreateTableData { + dataset: Box::new(Dataset::Table(Box::new(table))), + }) + .map_err(|error| error.to_string())?; + let index = self + .doc + .dataset_index(id) + .ok_or("The AFM fit table is unavailable.")?; + self.reveal_board_frame(FrameRef::Sheet(index)); + self.session.ui.sheet_open = Some(index); + } + Ok(()) + } + + pub fn zoom_to_afm_fit_range(&mut self, target: AfmFitTarget) -> Result<(), String> { + let fit = self + .afm_fit_result(target) + .ok_or("Run AFM Fit before zooming to its range.")?; + let ci = self + .doc + .canvas_index(target.canvas) + .ok_or("The source canvas is unavailable.")?; + let plot = self + .afm_fit_plot(target) + .ok_or("The source plot is unavailable.")?; + let mut viewport = plot.viewport.clone(); + let range = fit.range(); + if range.min >= range.max || !range.min.is_finite() || !range.max.is_finite() { + return Err("The fitted range is invalid.".into()); + } + viewport.view_x = range.clamp_in(viewport.full_x, plot.figure().x.scale); + if viewport.auto_y { + viewport.refresh_auto_y(plot.figure()); + } + self.try_execute_action(Action::set_object_viewport( + ci, + target.object, + plot.viewport.clone(), + viewport, + )) + .map_err(|error| error.to_string())?; + Ok(()) + } +} diff --git a/crates/core/src/state/axis_overrides.rs b/crates/core/src/state/axis_overrides.rs index 15131203..8934be79 100644 --- a/crates/core/src/state/axis_overrides.rs +++ b/crates/core/src/state/axis_overrides.rs @@ -223,7 +223,7 @@ impl CanvasViewport { } } - fn refresh_auto_y(&mut self, fig: &Figure) { + pub(super) fn refresh_auto_y(&mut self, fig: &Figure) { self.view_y = visible_y_range_on(fig, self.view_x, YAxisSide::Left).unwrap_or(self.full_y); self.view_y2 = if self.auto_y2 { self.full_y2 diff --git a/crates/core/src/state/lineage.rs b/crates/core/src/state/lineage.rs index 09976dcd..f828c3ff 100644 --- a/crates/core/src/state/lineage.rs +++ b/crates/core/src/state/lineage.rs @@ -21,6 +21,7 @@ pub enum DerivationKind { IvTable, StatisticsTable, RelationalTransform, + AfmForceBranchTable, } impl DerivationKind { @@ -39,6 +40,7 @@ impl DerivationKind { Self::IvTable => "IV table", Self::StatisticsTable => "Statistics table", Self::RelationalTransform => "Table transform", + Self::AfmForceBranchTable => "AFM force branch", } } } diff --git a/crates/core/src/state/mod.rs b/crates/core/src/state/mod.rs index a7f6122a..33f7eba3 100644 --- a/crates/core/src/state/mod.rs +++ b/crates/core/src/state/mod.rs @@ -19,6 +19,8 @@ use plotium_processing::{ }; mod afm; +mod afm_fit; +mod afm_fit_output; mod app_impl; mod app_impl_align; mod app_impl_analysis; @@ -204,6 +206,7 @@ mod xrd; mod xrd_axis; pub use afm::*; +pub use afm_fit::*; pub use app_impl::*; pub use app_impl_align::*; pub use app_impl_analysis::validate_ilt_params; diff --git a/crates/core/src/state/plot_object.rs b/crates/core/src/state/plot_object.rs index 9a1745ea..e041a0cc 100644 --- a/crates/core/src/state/plot_object.rs +++ b/crates/core/src/state/plot_object.rs @@ -1,8 +1,9 @@ use super::{ - AxisOverrides, AxisProjections, CanvasViewport, ChartSpec, DataBinding, DatasetId, DerivedAxes, - PlotIntent, SeriesId, StackSpec, + AfmForceFit, AxisOverrides, AxisProjections, CanvasViewport, ChartSpec, DataBinding, DatasetId, + DerivedAxes, PlotIntent, SeriesId, StackSpec, }; use plotium_figure::{Figure, FigureTypography}; +use std::sync::Arc; use std::sync::atomic::{AtomicU64, Ordering}; static NEXT_FIGURE_GEOMETRY_GENERATION: AtomicU64 = AtomicU64::new(1); @@ -34,6 +35,8 @@ pub struct PlotObject { pub axis_overrides: AxisOverrides, /// What the author wants the plot to show; never read while drawing. pub intent: PlotIntent, + afm_force_fits: Vec>, + afm_overlay_count: usize, /// Axis presentation emitted by the latest figure build, before author /// overrides are applied. Derived property defaults read this artifact. derived_axes: DerivedAxes, @@ -76,6 +79,8 @@ impl PlotObject { projections, axis_overrides, intent: PlotIntent::default(), + afm_force_fits: Vec::new(), + afm_overlay_count: 0, derived_axes, figure, figure_geometry_generation: next_figure_geometry_generation(), @@ -119,6 +124,8 @@ impl PlotObject { projections, axis_overrides, intent: PlotIntent::default(), + afm_force_fits: Vec::new(), + afm_overlay_count: 0, derived_axes, figure, figure_geometry_generation: next_figure_geometry_generation(), @@ -150,6 +157,55 @@ impl PlotObject { &self.figure } + pub fn afm_force_fits(&self) -> &[Arc] { + &self.afm_force_fits + } + + pub(crate) fn set_afm_force_fits(&mut self, fits: Vec>) { + self.afm_force_fits = fits; + self.refresh_afm_overlays(); + self.figure_geometry_generation = next_figure_geometry_generation(); + } + + fn refresh_afm_overlays(&mut self) { + use plotium_figure::{Color, LineStyle, Series}; + let base_len = self + .figure + .series + .len() + .saturating_sub(self.afm_overlay_count); + self.figure.series.truncate(base_len); + self.afm_overlay_count = 0; + if self.chart.type_id != "afm_force_curve" { + return; + } + let source = self.primary_dataset(); + for fit in self + .afm_force_fits + .iter() + .filter(|fit| fit.converged && fit.shown && Some(fit.source) == source) + { + let color = match fit.branch { + super::AfmForceBranch::Approach => Color::rgb(0x00, 0x87, 0x72), + super::AfmForceBranch::Retract => Color::rgb(0xaf, 0x68, 0x17), + }; + let mut overlay = Series::line( + format!("{} fit", fit.branch.name()), + fit.rows + .iter() + .map(|[x, _, predicted]| [*x, *predicted]) + .collect(), + ) + .colored(color); + overlay.width = 1.5; + overlay.appearance.line_style = LineStyle::Dashed; + match self.figure.push_unamplified_series(overlay) { + Ok(()) => self.afm_overlay_count += 1, + Err(error) => eprintln!("Could not draw the AFM fit overlay: {error}"), + } + } + } + /// The x direction this plot draws outside a shared-x stack. pub fn natural_x_reversed(&self) -> bool { self.natural_x_reversed @@ -262,6 +318,8 @@ impl PlotObject { figure.x.reversed = reversed; } self.figure = figure; + self.afm_overlay_count = 0; + self.refresh_afm_overlays(); self.make_legend_room(); self.figure_geometry_generation = next_figure_geometry_generation(); } diff --git a/crates/core/src/state/ui_state.rs b/crates/core/src/state/ui_state.rs index 38aee638..e46518b3 100644 --- a/crates/core/src/state/ui_state.rs +++ b/crates/core/src/state/ui_state.rs @@ -246,6 +246,7 @@ pub struct UiState { pub region_task_collapsed: bool, /// Data table whose Curve Fit workflow is shown in the canvas task card. pub curve_fit_task_dataset: Option, + pub afm_fit_task: Option, /// Whether the Curve Fit task card is reduced to its one-line summary. pub curve_fit_task_collapsed: bool, /// Data table whose Statistics workflow is shown in the canvas task card. @@ -470,6 +471,7 @@ impl Default for UiState { region_task_dataset: None, region_task_collapsed: false, curve_fit_task_dataset: None, + afm_fit_task: None, curve_fit_task_collapsed: false, stat_task_dataset: None, stat_task_collapsed: false, diff --git a/crates/core/src/state/ui_state/task_dock.rs b/crates/core/src/state/ui_state/task_dock.rs index 959070ba..5817eede 100644 --- a/crates/core/src/state/ui_state/task_dock.rs +++ b/crates/core/src/state/ui_state/task_dock.rs @@ -33,6 +33,7 @@ impl UiState { } TaskDockTab::CurveFit => { self.curve_fit_task_dataset = None; + self.afm_fit_task = None; self.curve_fit_task_collapsed = false; } TaskDockTab::Statistics => { @@ -59,7 +60,10 @@ impl UiState { self.processing_task_dataset.is_some(), ), (TaskDockTab::Regions, self.region_task_dataset.is_some()), - (TaskDockTab::CurveFit, self.curve_fit_task_dataset.is_some()), + ( + TaskDockTab::CurveFit, + self.curve_fit_task_dataset.is_some() || self.afm_fit_task.is_some(), + ), (TaskDockTab::Statistics, self.stat_task_dataset.is_some()), (TaskDockTab::Protein, self.protein_open), (TaskDockTab::Craft, self.craft_task_dataset.is_some()), @@ -78,6 +82,7 @@ impl UiState { self.region_task_dataset = None; self.region_task_collapsed = false; self.curve_fit_task_dataset = None; + self.afm_fit_task = None; self.curve_fit_task_collapsed = false; self.stat_task_dataset = None; self.stat_task_collapsed = false; diff --git a/crates/core/tests/afm_canvas.rs b/crates/core/tests/afm_canvas.rs index 861500eb..4e5bf457 100644 --- a/crates/core/tests/afm_canvas.rs +++ b/crates/core/tests/afm_canvas.rs @@ -78,6 +78,21 @@ fn force_only_gui_insertion_builds_a_nonempty_force_curve() { assert!((plot.figure().series[0].points[0][1] - 0.1).abs() < 1.0e-12); } +#[test] +fn map_and_force_plots_survive_project_roundtrip() { + let app = insert(afm_dataset(true)); + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("afm.plotium"); + plotium_core::project::save_project(&app, &path, false).unwrap(); + let reopened = plotium_core::project::load_project(&path).unwrap(); + let objects = &reopened.doc.canvases[0].objects; + let map = objects[0].plot().unwrap(); + let force = objects[1].plot().unwrap(); + assert!(map.figure().heatmap.is_some()); + assert_eq!(force.figure().series.len(), 2); + assert_eq!(force.figure().x.label, "Z (nm)"); +} + #[test] fn map_and_force_gui_insertion_builds_side_by_side_plots() { let app = insert(afm_dataset(true)); diff --git a/crates/figure/src/display_gain.rs b/crates/figure/src/display_gain.rs index c84bf69a..5fec381a 100644 --- a/crates/figure/src/display_gain.rs +++ b/crates/figure/src/display_gain.rs @@ -9,6 +9,23 @@ pub(crate) fn unit_gain() -> f64 { } impl Figure { + /// Append a scientific series to a figure that may already be amplified. + pub fn push_unamplified_series(&mut self, mut series: Series) -> Result<(), &'static str> { + let gain = self.display_gain; + if gain != 1.0 { + let source: Vec<_> = series.points.iter().map(|point| point[1]).collect(); + for (point, &original) in series.points.iter_mut().zip(&source) { + point[1] = amplify(original, series.display_baseline, gain); + if original.is_finite() && !point[1].is_finite() { + return Err("This display gain would exceed the numeric range."); + } + } + series.unamplified_y = Some(std::sync::Arc::new(source)); + } + self.series.push(series); + Ok(()) + } + /// Recover runtime source coordinates only after complete geometry equality. /// Project snapshots carry the materialized geometry, not this cache. pub fn inherit_display_gain_sources(&mut self, reference: &Self) { diff --git a/crates/io/src/nanoscope.rs b/crates/io/src/nanoscope.rs index a0758de2..0cee537c 100644 --- a/crates/io/src/nanoscope.rs +++ b/crates/io/src/nanoscope.rs @@ -22,8 +22,11 @@ use companion::{companion_candidates, geometries_match}; mod calibration; use calibration::{ calibrated_si_value, first_number, is_physical_deflection_unit, normalize_afm_unit, - physical_unit, sensitivity_scale, value_with_si, + physical_unit, sensitivity_scale, }; +#[path = "nanoscope_force_axis.rs"] +mod force_axis_details; +use force_axis_details::standalone_force_axis; #[derive(Debug, Clone)] struct Entry { @@ -69,7 +72,7 @@ pub fn is_nanoscope(path: &Path) -> bool { return false; }; let text = String::from_utf8_lossy(&prefix[..read]).to_ascii_lowercase(); - text.contains("\\*file list") || text.contains("\\*ciao") + text.contains("\\*file list") || text.contains("\\*force file list") || text.contains("\\*ciao") } pub fn load(path: &Path) -> Result { @@ -211,7 +214,14 @@ fn parse( } } - let forces = if let Some(candidate) = force_candidates.into_iter().max_by_key(|v| v.2) { + let forces = if let Some(candidate) = force_candidates.into_iter().max_by_key(|v| { + // Standalone ramps can store the same signal twice, once per X view. + let ramp = format == DataFormat::Afm(AfmFormat::BrukerNanoScopeSpm) + && v.0 + .value(&["X Data Type"]) + .is_some_and(|axis| axis.eq_ignore_ascii_case("Ramp")); + (v.2, ramp) + }) { match parse_force(bytes, candidate, &globals, format) { Ok(force) => Some(force), Err(error) @@ -356,7 +366,7 @@ fn parse_force( .checked_mul(samples_per_curve) .and_then(|value| value.checked_mul(word)) .ok_or_else(|| invalid("force block dimensions overflow"))?; - if expected != length { + if samples_per_curve == 0 || expected != length { return Err(invalid( "force block length does not match curve dimensions", )); @@ -372,13 +382,17 @@ fn parse_force( samples_per_curve, frame_direction(section), ); - let sample_period_s = global_number(globals, &["Sample period", "PeakForce period"]) - .map(|value| value_with_si(value, "s")); + let sample_period_s = global_value(globals, &["Sample period", "PeakForce period"]) + .and_then(calibration::sample_period_seconds); let frequency = global_number(globals, &["Peak Force Frequency", "PeakForce Frequency"]); let amplitude = global_number(globals, &["Peak Force Amplitude", "PeakForce Amplitude"]); let sync = global_number(globals, &["Sync Distance QNM"]).unwrap_or(0.0); let (display_order, approach_samples, z_positions) = - force_axis(samples_per_curve, format, frequency, amplitude, sync); + if format == DataFormat::Afm(AfmFormat::BrukerNanoScopeSpm) && pixels == 1 { + standalone_force_axis(section, globals, samples_per_curve)? + } else { + force_axis(samples_per_curve, format, frequency, amplitude, sync) + }; let deflection_sensitivity_m_per_v = global_value( globals, &[ @@ -389,8 +403,10 @@ fn parse_force( ], ) .and_then(|value| calibrated_si_value(value, "m/v")); - let spring_constant_n_per_m = - global_number(globals, &["Spring Constant"]).filter(|value| *value > 0.0); + let spring_constant_n_per_m = section + .number(&["Spring Constant"]) + .or_else(|| global_number(globals, &["Spring Constant"])) + .filter(|value| value.is_finite() && *value > 0.0); Ok(AfmForceSet { grid_width, grid_height, @@ -406,6 +422,8 @@ fn parse_force( }) } +type ForceAxis = (Vec, usize, Option>); + fn replace_force_sentinels(raw: &mut [i32], samples_per_curve: usize) { if samples_per_curve < 2 { return; @@ -477,7 +495,7 @@ fn force_axis( frequency: Option, amplitude: Option, sync: f64, -) -> (Vec, usize, Option>) { +) -> ForceAxis { if format != DataFormat::Afm(AfmFormat::BrukerPeakForceCapture) || frequency.is_none() || amplitude.is_none() diff --git a/crates/io/src/nanoscope_calibration.rs b/crates/io/src/nanoscope_calibration.rs index fb6364d6..2bc35b68 100644 --- a/crates/io/src/nanoscope_calibration.rs +++ b/crates/io/src/nanoscope_calibration.rs @@ -50,9 +50,41 @@ pub(super) fn is_physical_deflection_unit(unit: &str) -> bool { .any(|physical| lower == *physical || lower.starts_with(&format!("{physical}/"))) } -pub(super) fn value_with_si(value: f64, unit: &str) -> f64 { - let _ = unit; - value +/// The quantity after the hard scale is the display value, not the units/LSB. +pub(super) fn display_quantity(value: &str) -> Option<(f64, &str)> { + let mut quantity = value + .rsplit_once(')') + .map_or(value, |(_, tail)| tail) + .split_whitespace(); + Some((quantity.next()?.parse().ok()?, quantity.next()?)) +} + +pub(super) fn sample_period_seconds(value: &str) -> Option { + let (magnitude, unit) = display_quantity(value).or_else(|| Some((value.parse().ok()?, "s")))?; + let factor = match unit.to_ascii_lowercase().replace(['µ', 'μ'], "u").as_str() { + "s" => 1.0, + "ms" => 1e-3, + "us" => 1e-6, + "ns" => 1e-9, + _ => return None, + }; + let period = magnitude * factor; + (period.is_finite() && period > 0.0).then_some(period) +} + +pub(super) fn length_factor_nm(unit: &str) -> Option { + match unit + .to_ascii_lowercase() + .replace(['µ', 'μ', '~'], "u") + .as_str() + { + "m" => Some(1e9), + "mm" => Some(1e6), + "um" => Some(1e3), + "nm" => Some(1.0), + "pm" => Some(1e-3), + _ => None, + } } fn parenthesized(value: &str) -> Option<&str> { diff --git a/crates/io/src/nanoscope_force_axis.rs b/crates/io/src/nanoscope_force_axis.rs new file mode 100644 index 00000000..cf4a2dec --- /dev/null +++ b/crates/io/src/nanoscope_force_axis.rs @@ -0,0 +1,56 @@ +use super::*; + +pub(super) fn standalone_force_axis( + section: &Section, + globals: &HashMap, + samples: usize, +) -> Result { + // NanoScope lists retract then extend counts, but stores extend first. + let approach = if let Some([retract, extend]) = section + .value(&["Samps/line", "Samples/line"]) + .or_else(|| global_value(globals, &["Samps/line"])) + .and_then(|value| { + let mut counts = value.split_whitespace(); + Some([ + counts.next()?.parse::().ok()?, + counts.next()?.parse().ok()?, + ]) + }) { + if extend.checked_add(retract) != Some(samples) { + return Err(invalid( + "force branch sample counts do not match the data block", + )); + } + extend + } else { + samples / 2 + }; + let z_positions = section + .value(&["X Data Type"]) + .filter(|axis| axis.eq_ignore_ascii_case("Ramp")) + .and_then(|_| section.value(&["Ramp Size"])) + .and_then(|value| { + let (magnitude, unit) = calibration::display_quantity(value)?; + let nm = if unit.eq_ignore_ascii_case("V") { + let reference = bracketed(value)?; + let (sensitivity, length_unit) = + sensitivity_scale(globals.get(&reference.to_ascii_lowercase())?)?; + magnitude * sensitivity * calibration::length_factor_nm(&length_unit)? + } else { + magnitude * calibration::length_factor_nm(unit)? + }; + let nm = nm.abs(); + (nm.is_finite() && nm > 0.0).then(|| { + // Both branches are stored closest first. This is relative Z, + // not tip-sample separation or indentation. + [approach, samples - approach] + .into_iter() + .flat_map(|count| { + (0..count) + .map(move |i| nm * (i as f64 / count.saturating_sub(1).max(1) as f64)) + }) + .collect() + }) + }); + Ok(((0..samples).collect(), approach, z_positions)) +} diff --git a/crates/io/src/nanoscope_tests.rs b/crates/io/src/nanoscope_tests.rs index 1cc77e73..4dd8c4b4 100644 --- a/crates/io/src/nanoscope_tests.rs +++ b/crates/io/src/nanoscope_tests.rs @@ -51,6 +51,52 @@ fn deflection_error_raster_remains_an_image_channel() { assert!(data.forces.is_none()); } +fn standalone_force_views() -> Vec { + let mut header = String::from( + "\\*Force file list\n\\*Scanner list\n\\@Sens. Zsens: V 10.0 nm/V\n\\@Sens. DeflSens: V 20.0 nm/V\n\\*Ciao force list\n\\@Sample Period: V (0.1 us/LSB) 6.0 us\n", + ); + for (axis, offset) in [("Ramp", 1024), ("Time", 1052)] { + header.push_str(&format!( + "\\*Ciao force image list\n\\Data offset: {offset}\n\\Data length: 28\n\\Bytes/pixel: 4\n\\Samps/line: 3 4\n\\Spring Constant: 0.7\n\\X Data Type: {axis}\n\\@4:Image Data: S [DeflectionError] \"Deflection Error\"\n\\@4:Z scale: V [Sens. DeflSens] (0.01 V/LSB) 2.5 V\n\\@4:Ramp Size: V [Sens. Zsens] (0.005 V/LSB) 9.0 V\n", + )); + } + header.push_str("\\*File list end\n\u{1a}"); + let mut bytes = header.into_bytes(); + assert!(bytes.len() <= 1024); + bytes.resize(1024, 0); + for value in 1_i32..=14 { + bytes.extend_from_slice(&value.to_le_bytes()); + } + bytes +} + +#[test] +fn standalone_ramp_prefers_z_view_and_calibrates_both_branches() { + let bytes = standalone_force_views(); + let (data, warnings) = parse( + &bytes, + Path::new("ramp.spm"), + DataFormat::Afm(AfmFormat::BrukerNanoScopeSpm), + ) + .unwrap(); + assert!(warnings.is_empty()); + assert!(data.images.is_empty()); + let force = data.forces.unwrap(); + assert_eq!( + force.curve_raw(0, 0), + Some([1, 2, 3, 4, 5, 6, 7].as_slice()) + ); + assert_eq!(force.approach_samples, 4); + assert_eq!( + force.z_positions.as_deref(), + Some([0.0, 30.0, 60.0, 90.0, 0.0, 45.0, 90.0].as_slice()), + ); + assert_eq!(force.sample_period_s, Some(6.0e-6)); + assert_eq!(force.spring_constant_n_per_m, Some(0.7)); + assert_eq!(force.signal_scale.unit, "nm"); + assert!((force.signal_scale.apply(5) - 1.0).abs() < 1e-12); +} + #[test] fn malformed_optional_force_block_does_not_abort_valid_images() { let mut header = "\\*File list diff --git a/docs/src/content/docs/guides/supported-formats.md b/docs/src/content/docs/guides/supported-formats.md index f8d38a02..6e533010 100644 --- a/docs/src/content/docs/guides/supported-formats.md +++ b/docs/src/content/docs/guides/supported-formats.md @@ -253,11 +253,27 @@ See [XPS](/guides/xps/) for energy conversion, processing, and fitting. Plotium imports NanoScope `.spm` images, force curves, and force-volume grids, plus PeakForce Capture `.pfc` data cubes. Image channels plot as maps at the recorded scan size, in the file's physical units, with the aspect ratio locked. -Force curves plot as separate approach and retract branches; when the file -records a deflection sensitivity, the vertical axis is deflection in -nanometres, otherwise the curve stays in the unit stored in the file. Plotium -shows the acquired data as is — it does not infer a contact point, indentation, -or modulus, and does not fit a contact-mechanics model. +Force curves plot as separate Approach and Retract branches. With calibrated +deflection and a spring constant, the vertical axis is force in nanonewtons; +without the spring constant it is deflection in nanometres. If the signal +cannot be converted to deflection, it stays in its stored unit. For standalone +ramps, Plotium uses the calibrated relative Z ramp when available, then +recorded sample time, then sample index. + +Use **Analyze → AFM Force Fit → Fit Approach** or **Fit Retract**. To fit +part of a branch, use **Analyze → Analysis Range** and drag across the force +plot. The default model is **Linear**. **Run AFM Fit** overlays the prediction +within the fitted interval on the source plot without opening a new page or +sheet. The fitted points, parameters, and convergence status stay with the +plot when you save the project. + +Clear **Overlay on source plot** to run a fit without changing what the plot +shows. After fitting, **Output** lets you toggle the overlay, duplicate the +plot with the fit, create a residual plot, or open a new sheet containing +observed, predicted, and residual values. **Zoom to Fit Range** magnifies +the fitted interval on the source plot. Z is piezo displacement, not +tip-sample separation or indentation; this fit does not infer a contact +point or calculate a modulus. A PeakForce Capture file usually has an AllImages `.spm` export saved beside it. Plotium finds that companion, checks that its image grid matches the force diff --git a/docs/src/content/docs/zh-cn/guides/supported-formats.md b/docs/src/content/docs/zh-cn/guides/supported-formats.md index 0ba39dca..9c887799 100644 --- a/docs/src/content/docs/zh-cn/guides/supported-formats.md +++ b/docs/src/content/docs/zh-cn/guides/supported-formats.md @@ -197,10 +197,20 @@ CasaXPS `.txt` 按八行结构头识别,而不是仅按扩展名识别。其 Plotium 可导入 NanoScope `.spm` 图像、力曲线与 Force Volume 网格,以及 PeakForce Capture `.pfc` 数据立方体。图像通道按文件记录的扫描尺寸和物理单位 -绘制成地图,并锁定纵横比。力曲线按 approach 与 retract 两段分别绘制;文件中 -记录了偏转灵敏度时,纵轴为以纳米计的偏转量,否则曲线保持文件中存储的单位。 -Plotium 按采集原样显示数据——不会推断接触点、压痕或模量,也不会拟合接触力学 -模型。 +绘制成地图,并锁定纵横比。力曲线按 Approach(接近)与 Retract(回撤)两段 +分别绘制。具备偏转标定和弹簧常数时,纵轴为以纳牛计的力;仅具备偏转标定时, +纵轴为以纳米计的偏转量;无法转换为偏转量时,保留信号原始单位。独立斜坡曲线 +优先使用标定后的相对 Z 位移,其次使用采样时间,最后使用采样序号。 + +选择 **Analyze → AFM Force Fit → Fit Approach** 或 **Fit Retract**。只拟合局部时, +用 **Analyze → Analysis Range** 在力曲线图上拖选区间。默认模型为 **Linear**。 +点击 **Run AFM Fit**,拟合曲线会叠加在原图的拟合区间内,不会打开新页面或表格。 +拟合点、参数和收敛状态随图表一同保存到项目。 + +取消勾选 **Overlay on source plot** 可只计算结果,不改变原图显示。拟合后,可在 +**Output** 菜单中切换原图叠加、复制带拟合线的图、创建残差图,或打开包含观测值、 +预测值和残差的新表格。**Zoom to Fit Range** 可在原图上放大拟合区间。Z 是压电 +执行器位移,不是针尖与样品间距或压痕深度;该拟合不推断接触点,也不计算模量。 PeakForce Capture 文件旁通常保存着一个 AllImages `.spm` 导出文件。Plotium 会 找到这个配套文件,核对其图像网格与力网格一致后,把两者作为一个数据集导入;