File size: 9,771 Bytes
2cd3970
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
"""
Integration module for scenario comparison functionality in the HVAC calculator.

This module provides UI components and integration code for the scenario comparison
functionality in both the cooling and heating calculators.
"""

import streamlit as st
import pandas as pd
import matplotlib.pyplot as plt
from utils.scenario_manager import ScenarioManager

def add_scenario_ui(calculator_type):
    """
    Add scenario saving UI to the calculator.
    
    Args:
        calculator_type (str): Type of calculator ('cooling' or 'heating')
    
    Returns:
        bool: True if a scenario was saved, False otherwise
    """
    st.subheader("Save Current Calculation as Scenario")
    
    # Check if results exist
    results_key = f"{calculator_type}_results"
    if results_key not in st.session_state or not st.session_state[results_key]:
        st.warning("Please complete a calculation before saving a scenario.")
        return False
    
    # Get form data key
    form_data_key = f"{calculator_type}_form_data"
    
    # Create input fields for scenario name and description
    scenario_name = st.text_input("Scenario Name", f"My {calculator_type.capitalize()} Scenario")
    scenario_description = st.text_area("Scenario Description", "Description of this scenario...")
    
    # Save button
    if st.button("Save Scenario"):
        if not scenario_name:
            st.error("Please provide a name for the scenario.")
            return False
        
        # Initialize scenario manager
        scenario_manager = ScenarioManager()
        
        # Save the scenario
        try:
            path = scenario_manager.save_scenario(
                scenario_name,
                scenario_description,
                calculator_type,
                st.session_state[form_data_key],
                st.session_state[results_key]
            )
            
            st.success(f"Scenario saved successfully: {scenario_name}")
            return True
        except Exception as e:
            st.error(f"Error saving scenario: {str(e)}")
            return False
    
    return False

def scenario_comparison_page():
    """
    Create a page for comparing saved scenarios.
    """
    st.title("Scenario Comparison")
    
    # Initialize scenario manager
    scenario_manager = ScenarioManager()
    
    # Get available scenarios
    all_scenarios = scenario_manager.get_available_scenarios()
    
    if not all_scenarios:
        st.warning("No saved scenarios found. Please save some scenarios first.")
        return
    
    # Filter by calculator type
    calculator_type = st.radio(
        "Calculator Type",
        ["cooling", "heating", "all"],
        format_func=lambda x: x.capitalize() if x != "all" else "All"
    )
    
    filtered_scenarios = all_scenarios
    if calculator_type != "all":
        filtered_scenarios = [s for s in all_scenarios if s["calculator_type"] == calculator_type]
    
    if not filtered_scenarios:
        st.warning(f"No {calculator_type} scenarios found.")
        return
    
    # Create a DataFrame for display
    scenarios_df = pd.DataFrame([
        {
            "Name": s["name"],
            "Description": s["description"],
            "Type": s["calculator_type"].capitalize(),
            "Date": s["timestamp"]
        }
        for s in filtered_scenarios
    ])
    
    st.subheader("Available Scenarios")
    st.dataframe(scenarios_df)
    
    # Select scenarios to compare
    st.subheader("Select Scenarios to Compare")
    st.info("Select at least two scenarios to compare. The first selected scenario will be used as the base scenario.")
    
    # Create a mapping of display names to paths
    scenario_options = {f"{s['name']} ({s['calculator_type'].capitalize()})": s["path"] for s in filtered_scenarios}
    
    # Multi-select for scenarios
    selected_scenario_names = st.multiselect(
        "Select Scenarios",
        options=list(scenario_options.keys())
    )
    
    if len(selected_scenario_names) < 2:
        st.warning("Please select at least two scenarios to compare.")
        return
    
    # Get the paths for selected scenarios
    selected_scenario_paths = [scenario_options[name] for name in selected_scenario_names]
    
    # Compare button
    if st.button("Compare Selected Scenarios"):
        # Compare scenarios
        comparison = scenario_manager.compare_scenarios(selected_scenario_paths)
        
        if "error" in comparison:
            st.error(comparison["error"])
            return
        
        # Generate charts
        charts = scenario_manager.generate_comparison_charts(comparison)
        
        # Display comparison
        scenario_manager.display_comparison_in_streamlit(comparison, charts)
        
        # Add export options
        st.subheader("Export Comparison")
        
        # Export as CSV
        comparison_data = []
        
        # Add total loads
        for load in comparison["total_loads"]:
            row = {
                "Scenario": load["name"],
                "Metric": "Total Load",
                "Value": load["total_load_kw"],
                "Unit": "kW"
            }
            comparison_data.append(row)
            
            row = {
                "Scenario": load["name"],
                "Metric": "Recommended Size",
                "Value": load["recommended_size_kw"],
                "Unit": "kW"
            }
            comparison_data.append(row)
        
        # Add breakdown percentages
        for breakdown in comparison["breakdown"]:
            for category in breakdown:
                if category != "name":
                    row = {
                        "Scenario": breakdown["name"],
                        "Metric": f"{category.capitalize()} Percentage",
                        "Value": breakdown[category],
                        "Unit": "%"
                    }
                    comparison_data.append(row)
        
        # Add differences
        base_scenario = comparison["scenarios"][0]
        for scenario_name, diff in comparison["differences"].items():
            row = {
                "Scenario": scenario_name,
                "Metric": f"Absolute Difference from {base_scenario}",
                "Value": diff["absolute_diff_kw"],
                "Unit": "kW"
            }
            comparison_data.append(row)
            
            row = {
                "Scenario": scenario_name,
                "Metric": f"Percentage Difference from {base_scenario}",
                "Value": diff["percentage_diff"],
                "Unit": "%"
            }
            comparison_data.append(row)
        
        # Create DataFrame
        export_df = pd.DataFrame(comparison_data)
        
        # Convert to CSV
        csv = export_df.to_csv(index=False)
        
        # Create download button
        st.download_button(
            label="Download Comparison as CSV",
            data=csv,
            file_name="scenario_comparison.csv",
            mime="text/csv"
        )

def add_scenario_comparison_to_app(app_module):
    """
    Add scenario comparison functionality to the main app.
    
    Args:
        app_module: The main app module to modify
    """
    # Add scenario comparison page to the app
    app_module.pages["scenario_comparison"] = scenario_comparison_page
    
    # Update the main function to include the scenario comparison page
    original_main = app_module.main
    
    def new_main():
        st.sidebar.title("HVAC Load Calculator")
        
        # Add attribution
        st.sidebar.markdown("""
        **Created by:** Dr. Majed Abuseif
        
        This tool was created to facilitate HVAC calculation and understanding for Deakin University students, but has been enhanced to cover wider aspects to allow professionals and energy and HVAC enthusiasts to use it.
        """)
        
        # Add page selection
        page = st.sidebar.radio(
            "Select Calculator",
            ["Cooling Load Calculator", "Heating Load Calculator", "Scenario Comparison"]
        )
        
        if page == "Cooling Load Calculator":
            app_module.cooling_calculator()
        elif page == "Heating Load Calculator":
            app_module.heating_calculator()
        elif page == "Scenario Comparison":
            scenario_comparison_page()
    
    # Replace the main function
    app_module.main = new_main
    
    return app_module

def add_scenario_ui_to_calculator(calculator_module, calculator_type):
    """
    Add scenario saving UI to a calculator module.
    
    Args:
        calculator_module: The calculator module to modify
        calculator_type (str): Type of calculator ('cooling' or 'heating')
    """
    # Find the results section in the calculator
    if calculator_type == "cooling":
        original_results_form = calculator_module.results_form
        
        def new_results_form(ref_data):
            # Call the original results form
            original_results_form(ref_data)
            
            # Add scenario UI
            st.markdown("---")
            add_scenario_ui(calculator_type)
        
        # Replace the results form
        calculator_module.results_form = new_results_form
    
    elif calculator_type == "heating":
        original_results_form = calculator_module.results_form
        
        def new_results_form(ref_data):
            # Call the original results form
            original_results_form(ref_data)
            
            # Add scenario UI
            st.markdown("---")
            add_scenario_ui(calculator_type)
        
        # Replace the results form
        calculator_module.results_form = new_results_form
    
    return calculator_module