Feature/grace wasserstein uncertainty bootstrap - #2
Draft
graxia0322 wants to merge 135 commits into
Draft
Conversation
…ngle formulation first
…isting RODOS extraction logic (filename-reading)
…metadata from INHALER files
…eping the skip for RODOS files
…aler data but leave rodos unchanged
…ressure drop combo to calculate w1
- Converted plotting functions 1–3 to fully namespaced calls (ggplot2::, dplyr::, stringr::, etc.) - Removed reliance on attached tidyverse/viridis packages - Standardized on base pipe (|>) for consistency with core analysis scripts - Preserved function behavior and outputs; refactor only - Sets pattern for remaining visualization functions
…exporter - Introduce plot_reference_vs_test() core ggplot builder with input validation - Add export_pairwise_condition_pdfs() to save per-formulation × condition PDFs - Standardize naming/labels and keep fully namespaced dplyr/ggplot2 usage
- Remove invalid cur_column() usage inside if_all() - Filter test conditions programmatically across condition_cols
Ensure manual color scales are keyed to recoded curve labels so reference and test distributions render with intended colors instead of default grey.
…ate-first pooling - Implement Exporter 2 to generate one PDF per formulation - Overlay pooled reference (RODOS) with all available INHALER test conditions - Match replicate-first pooling logic used in Wasserstein calculations (script 02) - Use deterministic condition labeling and stable color mapping - Add defensive input checks and verbose status messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Edited 01_data_import.r, 02_wasserstein_core.r, 03_visualization.r and wasserstein_bootstrap_analysis.r to encompass up to 3x3x3 formulation x device resistance x pressure drop inhaler data in each script in a flexible manner. Key features of each script:
01_data_import.R
Data Structure:
RODOS: Traditional folder-based structure (formulation folders with replicate CSVs)
INHALER: Flat folder with metadata embedded in CSV headers
Automatic Metadata Extraction:
Device Resistance: Auto-detects "low", "medium", "high" from Device column (case-insensitive)
Pressure Drop: Extracts numeric values from pressure_drop/pressure-drop columns
Formulation ID: From folder names (RODOS) or formulation_id column (INHALER)
Replicates: From filename pattern (RODOS) or auto-assigned by timestamp (INHALER)
Flexible Factor Handling:
Works with any combination: 3×1×1, 3×2×3, 3×3×3, etc.
Handles missing factors (e.g., single device level)
Validates all extracted metadata with informative warnings
Smart Replicate Assignment:
INHALER: Groups by formulation + device + pressure, then assigns rep1, rep2, rep3 by timestamp
RODOS: Extracts from filename using regex pattern
Output:
data_v2/tidy/standardized_data_with_conditions.csv
02_wasserstein_core.R
Nested Looping for All Conditions:
Outer loop: Formulations
Inner loop: Device × Pressure combinations
Calculates W1 for each unique condition vs formulation-specific RODOS reference
Correct Pooling Methodology (pool + average across particle size grid):
RODOS: Pools ALL replicates once per formulation (reference is condition-independent)
INHALER: Pools replicates separately for each resistance×pressure drop combo
Follows correct statistical approach: pool first, then calculate W1
Flexible Condition Filtering:
pool_replicate_cdfs() accepts optional device_resistance and pressure_drop parameters
When NULL, pools across all conditions (for RODOS)
When specified, filters to specific condition (for INHALER)
Comprehensive Validation:
Reports W1 results with device/pressure identifiers
Validates data availability for each condition
Output:
results_v2/wasserstein_results.csv
03_visualization.R
Dynamic Facet Grid Generation:
Auto-creates device_resistance (rows) × pressure_drop (columns) facet grids
Repeats RODOS reference curve in every facet for comparison
Scales plot dimensions automatically based on number of facets
Intelligent Factor Ordering:
Device: Natural order (low → medium → high) if those levels exist
Pressure: Numeric sorting (1 kPa → 2 kPa → 4 kPa)
Falls back to alphabetical if non-standard levels
Four Plot Types:
Individual Formulation PDFs: One PDF per formulation showing all device×pressure grids
Overlay PDF: All INHALER conditions colored by formulation
CDF by Device: Faceted by device resistance levels
CDF by Pressure: Faceted by pressure drop levels
W1 by Device/Pressure: Bar charts faceted by experimental factors
Output:
figures_v2/*_comparison_faceted.pdf (one per formulation)
figures_v2/all_inhaler_overlay.pdf
figures_v2/CDF_by_device.pdf
figures_v2/CDF_by_pressure.pdf
figures_v2/W1_by_device.pdf
figures_v2/W1_by_pressure.pdf
wasserstein_bootstrap_analysis.R
Factor-Aware Bootstrap Resampling:
get_replicate_cdfs() accepts device_resistance and pressure_drop filters
Bootstraps each device×pressure combination separately
Generates uncertainty estimates for all conditions, not just formulations
Three-Way Effect-to-Noise Analysis:
Formulation Effect: Variability across formulations (held device/pressure constant)
Device Effect: Variability across device levels (held formulation/pressure constant)
Pressure Effect: Variability across pressure levels (held formulation/device constant)
Each ratio compares effect magnitude to bootstrap measurement noise
Defensive Factor Checking:
Detects if factors exist in data before calculating effect-to-noise ratios
Skips device/pressure effects if only 1 level present (e.g., 3×1×1 design)
Works from simple (3×1×1) to complex (3×3×3) designs
Flexible Faceted Visualizations:
Bootstrap Analysis PDF: Shows CI, SE, and precision for all conditions with device×pressure facets
Effect-Noise Analysis PDF:
Auto-adjusts plot dimensions based on number of facets
Smart Faceting Logic:
Only facets when factors have >1 level
Uses facet_grid() for 2D layouts when both device and pressure vary
Uses facet_wrap() for 1D layouts when only one factor varies
No faceting for simple formulation-only designs
Output:
results_v2/bootstrap_results.csv (W1 mean, SD, 95% CI for each condition)
results_v2/effect_noise_ratios.csv (3 rows: formulation, device, pressure effects)
figures_v2/bootstrap_analysis.pdf (faceted bootstrap diagnostics)
figures_v2/effect_noise_analysis.pdf (multi-panel effect-to-noise summary)