import pandas as pd import numpy as np import matplotlib.pyplot as plt from datetime import datetime # Read Phase 2 CSV file df = pd.read_csv('ONYX LN2 2026 PH 2.csv') # Set column names based on the actual data structure df.columns = ['Time', 'Temp', 'Alm1', 'mA', 'Alm2', 'Empty'] # Convert Time to datetime df['Time'] = pd.to_datetime(df['Time']) # Convert mA to SCFH: 4mA = 0, 20mA = 10000 # Formula: SCFH = (mA - 4) / 16 * 10000 df['SCFH'] = (df['mA'] - 4) * (10000 / 16) # Filter out negative flow rates for analysis (they might be measurement errors) df_clean = df[df['SCFH'] >= 0] # Calculate key metrics for Phase 2 peak_idx = df_clean['SCFH'].idxmax() peak_time = df_clean.loc[peak_idx, 'Time'] peak_flow = df_clean.loc[peak_idx, 'SCFH'] # Trends by day of week df_clean['DayOfWeek'] = df_clean['Time'].dt.day_name() df_clean['IsWeekend'] = df_clean['Time'].dt.dayofweek >= 5 weekend_avg = df_clean[df_clean['IsWeekend']]['SCFH'].mean() weekday_avg = df_clean[~df_clean['IsWeekend']]['SCFH'].mean() # Time of day df_clean['Hour'] = df_clean['Time'].dt.hour hourly_avg = df_clean.groupby('Hour')['SCFH'].mean() # Calculate overall statistics total_records = len(df) total_records_clean = len(df_clean) start_time = df['Time'].min() end_time = df['Time'].max() duration_days = (end_time - start_time).days # Calculate the average flow rate avg_flow = df_clean['SCFH'].mean() # Calculate variance and standard deviation std_dev = df_clean['SCFH'].std() min_flow = df_clean['SCFH'].min() max_flow = df_clean['SCFH'].max() print("=== PHASE 2 ANALYSIS RESULTS (Clean Data) ===") print(f"Duration: {start_time.strftime('%m/%d/%y')} to {end_time.strftime('%m/%d/%y')} ({duration_days} days)") print(f"Total Records: {total_records}") print(f"Clean Records: {total_records_clean}") print(f"Records with negative flow rates: {total_records - total_records_clean}") print(f"Average Flow Rate: {avg_flow:.2f} SCFH") print(f"Peak Usage: {peak_flow:.2f} SCFH at {peak_time}") print(f"Minimum Flow: {min_flow:.2f} SCFH") print(f"Maximum Flow: {max_flow:.2f} SCFH") print(f"Standard Deviation: {std_dev:.2f} SCFH") print(f"Weekend Average: {weekend_avg:.2f} SCFH") print(f"Weekday Average: {weekday_avg:.2f} SCFH") # Create a detailed hourly analysis print("\nHourly averages:") hourly_analysis = [] for hour in range(24): avg_hourly = hourly_avg.get(hour, 0) hourly_analysis.append((hour, avg_hourly)) print(f" {hour:02d}:00 - {avg_hourly:.2f} SCFH") print("\n=== COMPARISON WITH PHASE 1 ===") print("Phase 1 Results (from HTML report):") print("- Variance: -0.87% (Logger High)") print("- Peak Usage: 1062.50 SCFH on Feb 27, 2026 at 11:16 AM") print("- Weekday Average: 430.76 SCFH") print("- Weekend Average: 386.53 SCFH") print("- Data Resolution: 4.5 minutes (corrected to 4 minutes in Phase 2)") # Calculate variance between phases phase1_peak = 1062.50 phase2_peak = peak_flow peak_variance = ((phase2_peak - phase1_peak) / phase1_peak) * 100 print(f"\nPeak Usage Variance: {peak_variance:.2f}%") if abs(peak_variance) <= 1: print("✓ Peak usage variance within acceptable limits (±1%)") else: print("⚠ Peak usage variance exceeds acceptable limits (>1%)") # Create some visualizations plt.figure(figsize=(15, 10)) # Plot 1: Time series of flow rates (clean data only) plt.subplot(2, 2, 1) plt.plot(df_clean['Time'], df_clean['SCFH'], linewidth=0.8, alpha=0.7, color='blue') plt.title('LN2 Gas Consumption Over Time (Phase 2) - Clean Data') plt.xlabel('Date/Time') plt.ylabel('Flow Rate (SCFH)') plt.xticks(rotation=45) plt.grid(True, alpha=0.3) # Plot 2: Hourly average flow rates plt.subplot(2, 2, 2) hours = list(range(24)) avg_hourly = [hourly_avg.get(h, 0) for h in hours] plt.plot(hours, avg_hourly, marker='o', linewidth=2, markersize=4, color='green') plt.title('Average Flow Rate by Hour of Day') plt.xlabel('Hour of Day') plt.ylabel('Average Flow Rate (SCFH)') plt.xticks(range(0, 24, 2)) plt.grid(True, alpha=0.3) # Plot 3: Weekday vs Weekend comparison plt.subplot(2, 2, 3) days = ['Weekday', 'Weekend'] averages = [weekday_avg, weekend_avg] bars = plt.bar(days, averages, color=['#1f77b4', '#ff7f0e']) plt.title('Average Flow Rate by Day Type') plt.ylabel('Flow Rate (SCFH)') plt.grid(True, alpha=0.3, axis='y') # Add value labels on bars for bar, avg in zip(bars, averages): plt.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 10, f'{avg:.1f}', ha='center', va='bottom') # Plot 4: Flow rate distribution (clean data) plt.subplot(2, 2, 4) plt.hist(df_clean['SCFH'], bins=30, alpha=0.7, color='skyblue', edgecolor='black') plt.title('Distribution of Flow Rates (Clean Data)') plt.xlabel('Flow Rate (SCFH)') plt.ylabel('Frequency') plt.grid(True, alpha=0.3) plt.tight_layout() plt.savefig('phase2_analysis_clean.png', dpi=300, bbox_inches='tight') plt.close() print("\nAnalysis complete. Visualizations saved as 'phase2_analysis_clean.png'") # Also create a summary for the HTML report print("\n=== SUMMARY FOR HTML REPORT ===") print("Phase 2 Summary:") print(f"- Duration: {start_time.strftime('%m/%d/%y')} to {end_time.strftime('%m/%d/%y')}") print(f"- Average Flow Rate: {avg_flow:.2f} SCFH") print(f"- Peak Usage: {peak_flow:.2f} SCFH") print(f"- Weekend Average: {weekend_avg:.2f} SCFH") print(f"- Weekday Average: {weekday_avg:.2f} SCFH") # Check if data meets requirements if peak_variance <= 1: print("- Data variance within acceptable limits") else: print("- Note: Peak usage variance exceeds acceptable limits (>1%)")