Files
AlarmAnalysis/test_duration_fix.py
andy f08a1a9bf5 Initial commit: alarm analysis project
Python project for analyzing alarm data from building monitoring systems.
Includes alarm analyzer, plotting, tests, and source data files.
2026-02-26 09:03:54 -05:00

81 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify the fix for alarm duration calculation
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from alarm_analyzer import AlarmAnalyzer
def test_duration_calculation():
"""
Test the updated duration calculation with sample data
"""
print("Testing updated duration calculation...")
# Use the existing files
csv_file = "CardinalAlarmsDec25.csv"
xlsx_file = "SensorReport Cardinal 2025-12-23_processed.xlsx"
if not os.path.exists(csv_file):
print(f"CSV file {csv_file} not found. Creating a small test file...")
# Create a minimal test file
test_data = """Alarm_Id,Sensor_Id,Date,Description,LogTime
1,1001,2025-12-01 00:01:00.000,"Lo Warning: 68.0<=68.0F ",2025-12-01 00:01:01.077
2,1001,2025-12-01 00:05:00.000,"Lo Alarm: 67.5<=68.0F ",2025-12-01 00:05:01.077
3,1001,2025-12-01 00:10:00.000,"Normal 68.2F ",2025-12-01 00:10:01.077
4,1002,2025-12-01 00:02:00.000,"Error: Comm Loss Error 20.4>=20 min.",2025-12-01 00:02:01.077
5,1002,2025-12-01 00:07:00.000,"Hi Alarm: 70.0>=68.0F ",2025-12-01 00:07:01.077
6,1002,2025-12-01 00:12:00.000,"Normal 69.5F ",2025-12-01 00:12:01.077"""
with open(csv_file, 'w') as f:
f.write(test_data)
# Create analyzer instance
analyzer = AlarmAnalyzer(csv_file, xlsx_file)
try:
# Load data
alarm_data, sensor_data = analyzer.load_data()
print(f"Loaded {len(alarm_data)} alarm records")
# Categorize alarms
categorized_data = analyzer.categorize_alarms()
print("Categorized alarms successfully")
# Pair events and calculate durations
paired_events = analyzer.pair_events_and_calculate_durations()
if paired_events is not None and len(paired_events) > 0:
print(f"Created {len(paired_events)} paired events")
print("\nFirst few paired events:")
print(paired_events[['Sensor_Id', 'Alarm_Type', 'Start_Time', 'End_Time', 'Duration_Minutes', 'End_Reason']].head(10))
# Check if End_Reason column exists
if 'End_Reason' in paired_events.columns:
print(f"\nEnd reason distribution:")
print(paired_events['End_Reason'].value_counts())
else:
print("ERROR: End_Reason column not found in paired events")
# Check for transitions
if 'End_Reason' in paired_events.columns:
transitions = paired_events[paired_events['End_Reason'].str.contains('Transition', na=False)]
if len(transitions) > 0:
print(f"\nFound {len(transitions)} alarm condition transitions:")
print(transitions[['Sensor_Id', 'Alarm_Type', 'Start_Description', 'End_Description', 'Duration_Minutes', 'End_Reason']])
else:
print("\nNo alarm condition transitions found in this sample.")
else:
print("No paired events created")
print("Test completed successfully!")
except Exception as e:
print(f"Error during test: {e}")
import traceback
traceback.print_exc()
if __name__ == "__main__":
test_duration_calculation()