Spaces:
Runtime error
Runtime error
Commit
·
e544607
1
Parent(s):
1138be8
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from gradio.components import Markdown, Textbox, Button
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from sklearn.model_selection import train_test_split
|
| 6 |
+
from sklearn.linear_model import LinearRegression
|
| 7 |
+
from sklearn.preprocessing import PolynomialFeatures
|
| 8 |
+
from sklearn.svm import SVR
|
| 9 |
+
from sklearn.pipeline import make_pipeline
|
| 10 |
+
from sunpy.net import Fido
|
| 11 |
+
from sunpy.net import attrs as a
|
| 12 |
+
from sunpy.timeseries import TimeSeries
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
def process_data():
|
| 18 |
+
# Define the time range for data retrieval
|
| 19 |
+
tstart = "2015-06-21 01:00"
|
| 20 |
+
tend = "2015-06-21 23:00"
|
| 21 |
+
|
| 22 |
+
# Query and fetch GOES XRS data
|
| 23 |
+
result_goes15 = Fido.search(a.Time(tstart, tend), a.Instrument("XRS"), a.goes.SatelliteNumber(15), a.Resolution("flx1s"))
|
| 24 |
+
files = Fido.fetch(result_goes15)
|
| 25 |
+
|
| 26 |
+
# Load the data into a TimeSeries
|
| 27 |
+
goes_15 = TimeSeries(files, concatenate=True)
|
| 28 |
+
|
| 29 |
+
# Extract X-ray flux and time data
|
| 30 |
+
flux_data = goes_15.quantity("xrsb").value
|
| 31 |
+
time_data = goes_15.time.datetime
|
| 32 |
+
|
| 33 |
+
# Create a feature matrix with time data (as numerical values)
|
| 34 |
+
X = np.array([(t - time_data[0]).total_seconds() for t in time_data]).reshape(-1, 1)
|
| 35 |
+
|
| 36 |
+
# Split the data into training and testing sets
|
| 37 |
+
X_train, X_test, y_train, y_test = train_test_split(X, flux_data, test_size=0.2, random_state=42)
|
| 38 |
+
|
| 39 |
+
# Train a linear regression model
|
| 40 |
+
linear_model = LinearRegression()
|
| 41 |
+
linear_model.fit(X_train, y_train)
|
| 42 |
+
|
| 43 |
+
# Train a quadratic regression model
|
| 44 |
+
quadratic_model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())
|
| 45 |
+
quadratic_model.fit(X_train, y_train)
|
| 46 |
+
|
| 47 |
+
# Train a cubic regression model
|
| 48 |
+
cubic_model = make_pipeline(PolynomialFeatures(degree=3), LinearRegression())
|
| 49 |
+
cubic_model.fit(X_train, y_train)
|
| 50 |
+
|
| 51 |
+
# Train a support vector regression (SVR) model
|
| 52 |
+
svr_model = SVR(kernel='linear')
|
| 53 |
+
svr_model.fit(X_train, y_train)
|
| 54 |
+
|
| 55 |
+
# Make predictions using all models
|
| 56 |
+
y_pred_linear = linear_model.predict(X_test)
|
| 57 |
+
y_pred_quadratic = quadratic_model.predict(X_test)
|
| 58 |
+
y_pred_cubic = cubic_model.predict(X_test)
|
| 59 |
+
y_pred_svr = svr_model.predict(X_test)
|
| 60 |
+
|
| 61 |
+
# Plot the actual and predicted data from all models
|
| 62 |
+
plt.figure(figsize=(12, 6))
|
| 63 |
+
plt.scatter(X_test, y_test, color='blue', label='Actual Data')
|
| 64 |
+
plt.plot(X_test, y_pred_linear, color='red', linewidth=2, label='Linear Prediction')
|
| 65 |
+
plt.plot(X_test, y_pred_quadratic, color='green', linewidth=2, label='Quadratic Prediction')
|
| 66 |
+
plt.plot(X_test, y_pred_cubic, color='orange', linewidth=2, label='Cubic Prediction')
|
| 67 |
+
plt.plot(X_test, y_pred_svr, color='purple', linewidth=2, label='SVR Prediction')
|
| 68 |
+
|
| 69 |
+
# Include solar flux data as an additional line in the plot
|
| 70 |
+
plt.plot(X, flux_data, color='cyan', linestyle='dashed', label='Solar Flux')
|
| 71 |
+
|
| 72 |
+
plt.title('GOES XRS Space Weather Forecast')
|
| 73 |
+
plt.xlabel('Time (seconds since start)')
|
| 74 |
+
plt.ylabel('X-ray Flux / Solar Flux')
|
| 75 |
+
plt.legend()
|
| 76 |
+
|
| 77 |
+
# Save the image
|
| 78 |
+
plt.savefig('space_weather_forecast.png')
|
| 79 |
+
|
| 80 |
+
# Display the plot
|
| 81 |
+
#plt.show()
|
| 82 |
+
fig = plt.figure()
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
process_data()
|
| 86 |
+
|
| 87 |
+
with gr.Blocks(title="Space Weather Forecast", analytics_enabled=False) as spaceml:
|
| 88 |
+
gr.Markdown("# Space Weather Forecast")
|
| 89 |
+
gr.Markdown("Welcome to the Space Weather Forecast!")
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column(scale=1):
|
| 92 |
+
gradio_plot = gr.Image('space_weather_forecast.png')
|
| 93 |
+
|
| 94 |
+
spaceml.queue().launch(show_api=True, share=True)
|
| 95 |
+
|
| 96 |
+
|