Skip to content

Enhanced Result Formatting with Rich

Overview

The enhanced result formatting system provides comprehensive, user-friendly presentation of FIA analysis results with proper statistical context, confidence intervals, and reliability assessments. The system now uses the Rich Python library to create beautiful, professional terminal output with colors, tables, panels, and advanced styling.

Features

๐ŸŽฏ Statistical Rigor

  • Confidence Intervals: Automatic calculation of 95% (or configurable) confidence intervals
  • Reliability Assessment: Color-coded reliability ratings based on standard error percentages
  • Proper Statistical Context: Clear explanation of what estimates represent

๐Ÿ“Š Visual Enhancement with Rich

  • Rich Panels: Beautiful bordered panels for organizing information sections
  • Professional Tables: Styled tables with proper alignment, colors, and formatting
  • Emoji Indicators: Optional visual cues for different types of information
  • Color Coding: Syntax highlighting and color-coded reliability indicators
  • Responsive Layout: Automatically adapts to terminal width
  • Number Formatting: Proper comma-separated formatting for large numbers

๐Ÿ”ฌ Scientific Context

  • Methodology Notes: Explanation of FIA EVALIDator methodology
  • Sample Information: Plot counts and sampling context
  • Interpretation Guidance: Help users understand what results mean

Usage Examples

Basic Tree Count Query

Input: "How many live oak trees are in Texas?"

Enhanced Output:

๐ŸŒณ **FIA Tree Count Analysis Results**
==================================================

๐Ÿ“Š **Query Summary:**
   โ€ข Tree Status: Live trees
   โ€ข Land Type: Forest land
   โ€ข Tree Filter: SPCD == 802
   โ€ข Area Filter: STATECD == 48
   โ€ข Evaluation ID: 482201
   โ€ข Analysis Date: 2025-01-27 14:30

๐Ÿ“ˆ **Population Estimate:**

๐Ÿ”ข **Total Trees:** 45,234,567

๐Ÿ“Š **Statistical Precision:**
   โ€ข Standard Error: ยฑ2,261,728 trees (5.0%)
   โ€ข 95% Confidence Interval: 40,801,584 - 49,667,550
   โ€ข Reliability: ๐ŸŸข Excellent (Very reliable estimate)

๐Ÿ“ **Sample Information:**
   โ€ข Field Plots Used: 1,247
   โ€ข Average Trees per Plot: 36.3

๐Ÿ”ฌ **Methodology Notes:**
   โ€ข Population estimates use FIA EVALIDator methodology
   โ€ข Expansion factors account for plot sampling design
   โ€ข Standard errors reflect sampling uncertainty
   โ€ข 95% confidence intervals assume normal distribution

๐Ÿ’ก **Interpretation Guide:**
   โ€ข Population estimates represent total trees across the area
   โ€ข Standard errors indicate precision of estimates
   โ€ข Lower SE% = more precise estimate
   โ€ข Confidence intervals show plausible range of true values
   โ€ข Reliability ratings help assess estimate quality

Grouped Results (By Species)

Input: "Show me tree counts by species in North Carolina"

Enhanced Output:

๐ŸŒณ **FIA Tree Count Analysis Results**
==================================================

๐Ÿ“Š **Query Summary:**
   โ€ข Tree Status: Live trees
   โ€ข Land Type: Forest land
   โ€ข Evaluation ID: 372301
   โ€ข Analysis Date: 2025-01-27 14:30

๐Ÿ“ˆ **Detailed Results:**

**1. loblolly pine** (*Pinus taeda*)
   Species Code: 131
   ๐Ÿ”ข **Population Estimate:** 2,112,569,195 trees
   ๐Ÿ“Š **Standard Error:** ยฑ105,628,460 trees (5.0%)
   ๐ŸŽฏ **95% Confidence Interval:** 1,905,538,813 - 2,319,599,577 trees
   ๐ŸŸข **Reliability:** Excellent (โ‰ค5%)
   ๐Ÿ“ **Sample Size:** 3,500 plots

**2. red maple** (*Acer rubrum*)
   Species Code: 316
   ๐Ÿ”ข **Population Estimate:** 1,933,632,940 trees
   ๐Ÿ“Š **Standard Error:** ยฑ96,681,647 trees (5.0%)
   ๐ŸŽฏ **95% Confidence Interval:** 1,744,137,692 - 2,123,128,188 trees
   ๐ŸŸข **Reliability:** Excellent (โ‰ค5%)
   ๐Ÿ“ **Sample Size:** 3,500 plots

๐Ÿ“‹ **Summary Statistics:**
   โ€ข Total Entries: 2
   โ€ข Combined Population: 4,046,202,135 trees
   โ€ข Plot Sample Size: 3,500 plots

Reliability Assessment

The system automatically assesses estimate reliability based on standard error percentages:

Rating SE Range Indicator Interpretation
๐ŸŸข Excellent โ‰ค5% Green Very reliable estimate
๐ŸŸก Good 5-10% Yellow Reliable estimate
๐ŸŸ  Fair 10-20% Orange Moderately reliable estimate
๐Ÿ”ด Poor >20% Red Use with caution

Configuration Options

Formatter Styles with Rich

from pyfia.ai.result_formatter import create_result_formatter

# Enhanced style with Rich (default) - beautiful panels, tables, and colors
formatter = create_result_formatter("enhanced", use_rich=True)

# Simple style with Rich - clean panels without emojis
formatter = create_result_formatter("simple", use_rich=True)

# Scientific style with Rich - 99% confidence intervals, professional styling
formatter = create_result_formatter("scientific", use_rich=True)

# Fallback to plain text (when Rich unavailable or disabled)
formatter = create_result_formatter("enhanced", use_rich=False)

Rich-Specific Features

# Control terminal width for Rich output
formatter = create_result_formatter("enhanced", console_width=120)

# Rich formatting automatically provides:
# - Colored panels with borders
# - Professional tables with alignment
# - Responsive layout
# - Syntax highlighting
# - Visual hierarchy

Custom Configuration

from pyfia.ai.result_formatter import FIAResultFormatter

# Custom confidence level with Rich
formatter = FIAResultFormatter(
    confidence_level=0.90,  # 90% CI
    use_rich=True,
    console_width=100
)

# Disable emojis but keep Rich styling
formatter = FIAResultFormatter(
    include_emojis=False,
    use_rich=True
)

# Scientific configuration with Rich
formatter = FIAResultFormatter(
    include_emojis=False,
    confidence_level=0.99,
    use_rich=True
)

Rich Output Examples

Single Result with Rich Panels

When Rich is enabled, single results are displayed in beautiful bordered panels:

โ•ญโ”€ ๐ŸŒณ Query Summary โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚                                                                โ”‚
โ”‚  Tree Status: Live trees                                       โ”‚
โ”‚  Land Type: Forest land                                        โ”‚
โ”‚  Area Filter: STATECD == 37                                    โ”‚
โ”‚  Evaluation ID: 372301                                         โ”‚
โ”‚  Analysis Date: 2025-06-25 18:43                               โ”‚
โ”‚                                                                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

โ•ญโ”€ ๐Ÿ“Š Population Analysis Results โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
โ”‚                                                                โ”‚
โ”‚  Population Estimate: 2,112,569,195 trees                     โ”‚
โ”‚                                                                โ”‚
โ”‚  Statistical Precision:                                        โ”‚
โ”‚  โ€ข Standard Error: ยฑ105,628,460 trees (5.0%)                  โ”‚
โ”‚  โ€ข 95% Confidence Interval: 1,905,537,413 - 2,319,600,977     โ”‚
โ”‚  โ€ข Reliability: ๐ŸŸข Excellent (Very reliable estimate)          โ”‚
โ”‚                                                                โ”‚
โ”‚  Sample Information:                                           โ”‚
โ”‚  โ€ข Field Plots Used: 3,500                                     โ”‚
โ”‚  โ€ข Average Trees per Plot: 603,591.2                           โ”‚
โ”‚                                                                โ”‚
โ•ฐโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฏ

Grouped Results with Rich Tables

Multiple species are displayed in professional tables:

                      ๐ŸŒณ Tree Population Analysis
โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ณโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”“
โ”ƒ Species               โ”ƒ    Population โ”ƒ Standard Error โ”ƒ Reliability  โ”ƒ
โ”กโ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ•‡โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”โ”ฉ
โ”‚ loblolly pine         โ”‚ 2,112,569,195 โ”‚   ยฑ105,628,460 โ”‚ ๐ŸŸข Excellent โ”‚
โ”‚ (Pinus taeda)         โ”‚               โ”‚         (5.0%) โ”‚              โ”‚
โ”‚ red maple             โ”‚ 1,933,632,940 โ”‚    ยฑ96,681,647 โ”‚ ๐ŸŸข Excellent โ”‚
โ”‚ (Acer rubrum)         โ”‚               โ”‚         (5.0%) โ”‚              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Integration with AI Agent

The enhanced formatting is automatically used by the AI agent when available:

from pyfia.ai.agent import FIAAgent

agent = FIAAgent("path/to/database.duckdb")
response = agent.query("How many pine trees are in Oregon?")
# Automatically uses enhanced formatting

Fallback Behavior

If the enhanced formatter is not available, the system gracefully falls back to simple formatting:

Tree Count Results:

Species: loblolly pine (Pinus taeda)
Total Population: 2,112,569,195 trees
Standard Error: 105,628,460
Standard Error %: 5.0%

(Statistically valid population estimate using FIA methodology)

Technical Implementation

Key Components

  1. FIAResultFormatter: Main formatter class with configurable options
  2. Reliability Assessment: Automatic quality evaluation based on SE%
  3. Confidence Intervals: Statistical calculation with proper bounds
  4. Visual Enhancement: Emoji mapping and structured layout
  5. Fallback System: Graceful degradation when formatter unavailable

Statistical Calculations

  • Confidence Intervals: Uses appropriate z-scores (1.96 for 95%, 2.576 for 99%)
  • Lower Bound Constraint: Ensures confidence intervals don't go below zero
  • Reliability Thresholds: Based on FIA accuracy standards

Error Handling

  • Import Safety: Graceful handling when dependencies unavailable
  • Data Validation: Checks for required columns and valid values
  • NaN Handling: Proper treatment of missing statistical values

Best Practices

For Users

  1. Pay attention to reliability ratings - Green is best, red requires caution
  2. Use confidence intervals - They show the uncertainty in estimates
  3. Consider sample size - More plots generally mean better estimates
  4. Understand methodology - Read the methodology notes for context

For Developers

  1. Always provide fallback - Simple formatting should always work
  2. Validate input data - Check for required columns before formatting
  3. Handle edge cases - Zero estimates, missing SE values, etc.
  4. Test different scenarios - Single results, grouped results, edge cases

Future Enhancements

Planned Features

  • Comparison Formatting: Side-by-side comparisons of different estimates
  • Trend Analysis: Formatting for temporal comparisons
  • Export Options: PDF, CSV, and other format exports
  • Interactive Elements: Expandable sections and drill-down capabilities

Visualization Integration

  • Chart Generation: Automatic creation of bar charts and plots
  • Map Integration: Geographic visualization of spatial results
  • Statistical Plots: Confidence interval visualizations

Examples for Different Estimate Types

Volume Estimates

๐Ÿ“Š Volume Analysis Results
โ€ข Net Volume: 2,659.03 cu ft/acre (ยฑ39.89, 1.5% SE)
โ€ข Gross Volume: 2,692.80 cu ft/acre (ยฑ40.39, 1.5% SE)
โ€ข ๐ŸŸข Excellent reliability for both estimates

Biomass Estimates

๐ŸŒฟ Biomass Analysis Results
โ€ข Aboveground: 69.7 tons/acre (ยฑ1.05, 1.5% SE)
โ€ข Carbon Content: 32.8 tons/acre (ยฑ0.49, 1.5% SE)
โ€ข ๐ŸŸข Excellent reliability - suitable for carbon accounting

Area Estimates

๐Ÿ—บ๏ธ Forest Area Analysis
โ€ข Total Forest: 18,592,940 acres (ยฑ117,647, 0.63% SE)
โ€ข Timberland: 17,854,302 acres (ยฑ125,181, 0.70% SE)
โ€ข ๐ŸŸข Excellent reliability - meets FIA accuracy standards

This enhanced formatting system transforms raw statistical output into user-friendly, scientifically rigorous presentations that help users understand both the results and their reliability.