Skip to content

Examples DataAnalysis

Truong Giang Vu edited this page Feb 14, 2026 · 2 revisions

Example: Data Analysis with Polars

Demo: Collect Revit data β†’ Analyze with Polars β†’ Visualize results
Script: data_analysis_script.py
Time: 2-3 minutes


What It Does

Complete workflow combining all three modules:

  1. CodeExecute - Auto-install Polars via PEP 723
  2. Logging - Structured output with syntax highlighting
  3. Visualization - Display outliers in 3D view

Key Pattern: PEP 723 Dependencies

# /// script
# dependencies = [
#     "polars==1.38.1",
# ]
# ///

When you click Execute:

  • CodeExecute checks if polars is installed
  • If missing, UV resolver installs it automatically (~5 seconds)
  • Script runs with full access to Polars DataFrame API

Key Pattern: Revit Data Collection

from Autodesk.Revit.DB import FilteredElementCollector, Wall

doc = __revit__.ActiveUIDocument.Document
walls = FilteredElementCollector(doc).OfClass(Wall).ToElements()

data = []
for wall in walls:
    data.append({
        "Id": wall.Id.IntegerValue,
        "Name": wall.Name,
        "Area": wall.get_Parameter(BuiltInParameter.HOST_AREA_COMPUTED).AsDouble(),
    })

Output in Trace Panel:

Collecting wall data...
Collected 127 walls

Key Pattern: DataFrame Analysis

import polars as pl

df = pl.DataFrame(data)

# Statistical analysis
stats = df.select(pl.col("Area").describe())
print(stats)

# Find outliers (area > 2 std deviations)
mean_area = df["Area"].mean()
std_area = df["Area"].std()
outliers = df.filter(pl.col("Area") > mean_area + 2 * std_area)

Output in Trace Panel:

Wall Area Statistics:
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ statisticβ”‚ value      β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ count    β”‚ 127.0      β”‚
β”‚ mean     β”‚ 245.8      β”‚
β”‚ std      β”‚ 78.3       β”‚
β”‚ min      β”‚ 12.5       β”‚
β”‚ max      β”‚ 892.1      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Found 3 outlier walls

Key Pattern: Geometry Visualization

# Get walls from outlier IDs
outlier_ids = [ElementId(id) for id in outliers["Id"]]
outlier_walls = [doc.GetElement(id) for id in outlier_ids]

# Print geometry β†’ auto-visualized in 3D
for wall in outlier_walls:
    curve = wall.Location.Curve
    print(f"Wall {wall.Id}: Area={wall_area:.1f} sq ft")
    print(curve)  # Appears in 3D view with color

Result:

  • Outlier walls highlighted in 3D view
  • Trace Panel shows IDs + areas
  • Clear visual feedback for analysis

Try It Yourself

  1. Open RevitDevTool panel
  2. Navigate to CodeExecute tab
  3. Load folder: source/RevitDevTool.PythonDemo/commands/
  4. Click data_analysis_script.py
  5. Watch dependencies install (first run only)
  6. See results in Trace Panel + 3D view

Full source: data_analysis_script.py


Related Examples

Clone this wiki locally