# Test CI Analysis Functions # ========================== # # Script to test CI data loading and analysis functions cat("Testing CI Analysis Functions...\n") cat("================================\n") # Load functions source("ci_analysis_functions.R") # Test 1: Load field boundaries cat("\n1. Testing field boundary loading...\n") field_boundaries <- load_field_boundaries() if (!is.null(field_boundaries)) { cat("✓ Field boundaries loaded successfully\n") cat(" Number of fields:", nrow(field_boundaries), "\n") cat(" Columns:", paste(names(field_boundaries), collapse = ", "), "\n") cat(" CRS:", st_crs(field_boundaries)$input, "\n") } else { cat("✗ Failed to load field boundaries\n") } # Test 2: Load current CI cat("\n2. Testing current CI loading...\n") current_ci <- load_current_ci() if (!is.null(current_ci)) { cat("✓ Current CI loaded successfully\n") cat(" Dimensions:", dim(current_ci), "\n") cat(" CRS:", crs(current_ci), "\n") cat(" Value range:", round(global(current_ci, range, na.rm = TRUE)[,1], 3), "\n") cat(" Mean CI:", round(global(current_ci, mean, na.rm = TRUE)[1,1], 3), "\n") } else { cat("✗ Failed to load current CI\n") } # Test 3: Load previous CI cat("\n3. Testing previous CI loading...\n") previous_ci <- load_previous_ci() if (!is.null(previous_ci)) { cat("✓ Previous CI loaded successfully\n") cat(" Dimensions:", dim(previous_ci), "\n") cat(" Value range:", round(global(previous_ci, range, na.rm = TRUE)[,1], 3), "\n") cat(" Mean CI:", round(global(previous_ci, mean, na.rm = TRUE)[1,1], 3), "\n") } else { cat("✗ Failed to load previous CI\n") } # Test 4: Calculate change cat("\n4. Testing CI change calculation...\n") if (!is.null(current_ci) && !is.null(previous_ci)) { ci_change <- calculate_ci_change(current_ci, previous_ci) if (!is.null(ci_change)) { cat("✓ CI change calculated successfully\n") cat(" Change range:", round(global(ci_change, range, na.rm = TRUE)[,1], 3), "\n") cat(" Mean change:", round(global(ci_change, mean, na.rm = TRUE)[1,1], 3), "\n") } else { cat("✗ Failed to calculate CI change\n") } } else { cat("⚠ Skipping change calculation - need both current and previous CI\n") } # Test 5: Create test map cat("\n5. Testing interactive map creation...\n") if (!is.null(current_ci)) { # Set to plot mode for testing tmap_mode("plot") test_map <- create_interactive_ci_map( current_ci, field_boundaries, title = "Test CI Map" ) if (!is.null(test_map)) { cat("✓ Interactive map created successfully\n") print(test_map) } else { cat("✗ Failed to create interactive map\n") } } else { cat("⚠ Skipping map test - no CI data available\n") } # Test 6: Field statistics cat("\n6. Testing field statistics calculation...\n") if (!is.null(current_ci) && !is.null(field_boundaries)) { field_stats <- calculate_field_ci_stats(current_ci, field_boundaries) if (!is.null(field_stats)) { cat("✓ Field statistics calculated successfully\n") cat(" Number of fields with stats:", nrow(field_stats), "\n") cat(" Sample statistics:\n") print(head(field_stats, 3)) } else { cat("✗ Failed to calculate field statistics\n") } } else { cat("⚠ Skipping field stats - need both CI data and field boundaries\n") } cat("\n=== Test Summary ===\n") cat("CI Analysis functions testing complete!\n") # Reset tmap mode tmap_mode("view")