CodingMaster24/SolarPanelAnalysisPage
1
1import streamlit as st
2import numpy as np
3import pandas as pd
4import matplotlib.pyplot as plt
5import seaborn as sns
6import warnings
7from sklearn.preprocessing import MinMaxScaler
8from tensorflow.keras.models import Sequential
9from tensorflow.keras.layers import Dense, LSTM, InputLayer
10from sklearn.model_selection import train_test_split
11
12# Ignore warnings
13warnings.filterwarnings('ignore')
14
15# Page Navigation
16PAGES = {
17 "Solar Panel Analysis": "solar_panel_analysis",
18 "About": "about"
19}
20
21page = st.sidebar.selectbox("Select a page", options=list(PAGES.keys()))
22if page == "About":
23 st.title("About Solar Panel Data Analysis")
24 st.write("""
25 This project uses machine learning techniques to forecast solar panel daily energy generation data using historical data.
26 Users can either upload their own datasets or use the default datasets provided from GitHub. The app uses neural networks
27 (LSTM or Dense) to make predictions about the solar panel energy yield based on previous data.
28 """)
29 st.write("### How the Model Works")
30 st.write("""
31 The model learns the patterns in the solar power generation data by using a neural network architecture.
32 Users can select the type of neural network (LSTM or Dense) and adjust hyperparameters like epochs and batch size.
33 """)
34 st.write("### Project Description")
35 st.write("""
36 This project is developed by the LetsBreakItDown, with the goal of promoting green and sustainable energy. It focuses on solar power generation and how we can use artificial intelligence (AI) to manage and optimize the energy produced by solar panels. The main motive behind this project is to explore how green AI can help create smarter, more efficient systems for using renewable energy, like solar power.
37
38 The project aims to contribute to green energy management by predicting and improving solar power output. It is part of the larger effort to reduce reliance on fossil fuels and move towards a cleaner, more sustainable way of using energy.
39
40 By using machine learning and AI, the project helps us understand how we can better manage and use the energy generated by solar panels, making our environment greener and supporting sustainable energy for the future.
41 """)
42 st.stop()
43
44# Main page: Solar Panel Analysis
45if page == "Solar Panel Analysis":
46 # Streamlit UI for file upload or default dataset selection
47 st.title("Solar Power Data Analysis")
48 st.write("### Solar Plant 1 Generation Data")
49
50 # Option for uploading files or using default datasets
51 data_source = st.radio("Choose Data Source", ("Use Default GitHub Data", "Upload Your Own Files"))
52
53 if data_source == "Use Default GitHub Data":
54 # Default GitHub URLs for datasets
55 gen_1_url = "https://raw.githubusercontent.com/Sivatech24/SolarPanelDataAnalysis/256b8a98839900c42f44ee5edd14d57f18997a8d/Jupyter%20Notebook/DataSet/SolarPower/Plant_1_Generation_Data.csv"
56 sens_1_url = "https://raw.githubusercontent.com/Sivatech24/SolarPanelDataAnalysis/256b8a98839900c42f44ee5edd14d57f18997a8d/Jupyter%20Notebook/DataSet/SolarPower/Plant_1_Weather_Sensor_Data.csv"
57
58 # Load the data from GitHub links
59 gen_1 = pd.read_csv(gen_1_url)
60 sens_1 = pd.read_csv(sens_1_url)
61 st.write("#### Plant Generation Data Head")
62 st.write(gen_1.head())
63 st.write("#### Plant Generation Data Description")
64 st.write(gen_1.describe())
65
66 else:
67 # File uploader for custom dataset
68 gen_1_file = st.file_uploader("Upload the Solar Generation Data (CSV)", type=["csv"])
69 sens_1_file = st.file_uploader("Upload the Weather Sensor Data (CSV)", type=["csv"])
70
71 if gen_1_file is not None and sens_1_file is not None:
72 # Load the uploaded files
73 gen_1 = pd.read_csv(gen_1_file)
74 sens_1 = pd.read_csv(sens_1_file)
75 st.write("#### Plant Generation Data Head")
76 st.write(gen_1.head())
77 st.write("#### Plant Generation Data Description")
78 st.write(gen_1.describe())
79 else:
80 st.warning("Please upload both files to proceed with the analysis.")
81
82 # Preprocessing and feature extraction
83 gen_1['DATE_TIME'] = pd.to_datetime(gen_1['DATE_TIME'], format='%d-%m-%Y %H:%M')
84 gen_1.set_index('DATE_TIME', inplace=True)
85 gen_1 = gen_1[['DAILY_YIELD']] # We focus on forecasting DAILY_YIELD
86
87 # Normalize the data (MinMax Scaling)
88 scaler = MinMaxScaler(feature_range=(0, 1))
89 scaled_data = scaler.fit_transform(gen_1.values)
90
91 # Create train/test data
92 train_size = int(len(scaled_data) * 0.8)
93 train_data, test_data = scaled_data[:train_size], scaled_data[train_size:]
94
95 # Convert data to sequences for neural network (Sliding window approach)
96 def create_dataset(data, time_step=1):
97 X, y = [], []
98 for i in range(len(data) - time_step):
99 X.append(data[i:i + time_step, 0])
100 y.append(data[i + time_step, 0])
101 return np.array(X), np.array(y)
102
103 time_step = 60 # Use the last 60 data points to predict the next
104 X_train, y_train = create_dataset(train_data, time_step)
105 X_test, y_test = create_dataset(test_data, time_step)
106
107 # Reshape input data for the models (samples, time steps, features)
108 X_train_lstm = X_train.reshape(X_train.shape[0], X_train.shape[1], 1)
109 X_test_lstm = X_test.reshape(X_test.shape[0], X_test.shape[1], 1)
110 X_train_dense = X_train.reshape(X_train.shape[0], X_train.shape[1])
111 X_test_dense = X_test.reshape(X_test.shape[0], X_test.shape[1])
112
113 # Add scrollbars to adjust epochs and batch size
114 epochs = st.slider('Select number of epochs', min_value=10, max_value=100, value=50, step=10)
115 batch_size = st.slider('Select batch size', min_value=16, max_value=128, value=32, step=16)
116
117 # Function to build LSTM model
118 def build_lstm_model(input_shape):
119 model = Sequential()
120 model.add(InputLayer(input_shape=input_shape)) # Input layer for LSTM
121 model.add(LSTM(units=50, return_sequences=True)) # First LSTM layer
122 model.add(LSTM(units=50, return_sequences=False)) # Second LSTM layer
123 model.add(Dense(units=1)) # Output layer for forecasting
124 model.compile(optimizer='adam', loss='mean_squared_error')
125 return model
126
127 # Function to build Dense neural network model
128 def build_dense_model(input_shape):
129 model = Sequential([
130 Dense(256, activation='relu', input_shape=(input_shape,)), # Input layer with 256 neurons
131 Dense(256, activation='relu'), # Second hidden layer with 256 neurons
132 Dense(128, activation='relu'), # Third hidden layer with 128 neurons
133 Dense(64, activation='relu'), # Fourth hidden layer with 64 neurons
134 Dense(32, activation='relu'), # Fifth hidden layer with 32 neurons
135 Dense(16, activation='relu'), # Sixth hidden layer with 16 neurons
136 Dense(8, activation='relu'), # Seventh hidden layer with 8 neurons
137 Dense(1, activation='sigmoid') # Output layer with 1 output for forecasting using Sigmoid
138 ])
139 model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])
140 return model
141
142 # Choose between LSTM and Dense model
143 model_type = st.selectbox("Choose model type", ["LSTM", "Dense Neural Network"])
144
145 if model_type == "LSTM":
146 # Build and train LSTM model
147 model = build_lstm_model((X_train_lstm.shape[1], 1))
148 st.write(f"Training the LSTM model with {epochs} epochs and batch size {batch_size}...")
149 history = model.fit(X_train_lstm, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test_lstm, y_test), verbose=1)
150 elif model_type == "Dense Neural Network":
151 # Build and train Dense neural network model
152 model = build_dense_model(X_train_dense.shape[1])
153 st.write(f"Training the Dense neural network model with {epochs} epochs and batch size {batch_size}...")
154 history = model.fit(X_train_dense, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test_dense, y_test), verbose=1)
155
156 # Predict on the test set
157 st.write("Making Predictions...")
158 predicted_yield = model.predict(X_test_dense if model_type == "Dense Neural Network" else X_test_lstm)
159
160 # Invert scaling to get actual values
161 predicted_yield = scaler.inverse_transform(predicted_yield)
162 y_test_actual = scaler.inverse_transform(y_test.reshape(-1, 1))
163
164 # Plotting the results
165 fig, ax = plt.subplots(figsize=(15, 5))
166 ax.plot(y_test_actual, label='True Daily Yield', color='navy')
167 ax.plot(predicted_yield, label='Predicted Daily Yield', color='green')
168 ax.legend()
169 ax.set_title(f'Solar Power Forecast using {model_type} Model', fontsize=17)
170 st.pyplot(fig)
171
172 # Show model training loss curve
173 fig2, ax2 = plt.subplots(figsize=(15, 5))
174 ax2.plot(history.history['loss'], label='Training Loss', color='blue')
175 ax2.plot(history.history['val_loss'], label='Validation Loss', color='orange')
176 ax2.legend()
177 ax2.set_title(f'{model_type} Model Loss Curve', fontsize=17)
178 st.pyplot(fig2)
179
180 # Display final model summary
181 st.write("Final Model Summary:")
182 st.text(model.summary())
183 