File size: 3,443 Bytes
e544607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from gradio.components import Markdown, Textbox, Button
import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.svm import SVR
from sklearn.pipeline import make_pipeline
from sunpy.net import Fido
from sunpy.net import attrs as a
from sunpy.timeseries import TimeSeries




def process_data():
    # Define the time range for data retrieval
    tstart = "2015-06-21 01:00"
    tend = "2015-06-21 23:00"

    # Query and fetch GOES XRS data
    result_goes15 = Fido.search(a.Time(tstart, tend), a.Instrument("XRS"), a.goes.SatelliteNumber(15), a.Resolution("flx1s"))
    files = Fido.fetch(result_goes15)

    # Load the data into a TimeSeries
    goes_15 = TimeSeries(files, concatenate=True)

    # Extract X-ray flux and time data
    flux_data = goes_15.quantity("xrsb").value
    time_data = goes_15.time.datetime

    # Create a feature matrix with time data (as numerical values)
    X = np.array([(t - time_data[0]).total_seconds() for t in time_data]).reshape(-1, 1)

    # Split the data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(X, flux_data, test_size=0.2, random_state=42)

    # Train a linear regression model
    linear_model = LinearRegression()
    linear_model.fit(X_train, y_train)

    # Train a quadratic regression model
    quadratic_model = make_pipeline(PolynomialFeatures(degree=2), LinearRegression())
    quadratic_model.fit(X_train, y_train)

    # Train a cubic regression model
    cubic_model = make_pipeline(PolynomialFeatures(degree=3), LinearRegression())
    cubic_model.fit(X_train, y_train)

    # Train a support vector regression (SVR) model
    svr_model = SVR(kernel='linear')
    svr_model.fit(X_train, y_train)

    # Make predictions using all models
    y_pred_linear = linear_model.predict(X_test)
    y_pred_quadratic = quadratic_model.predict(X_test)
    y_pred_cubic = cubic_model.predict(X_test)
    y_pred_svr = svr_model.predict(X_test)

    # Plot the actual and predicted data from all models
    plt.figure(figsize=(12, 6))
    plt.scatter(X_test, y_test, color='blue', label='Actual Data')
    plt.plot(X_test, y_pred_linear, color='red', linewidth=2, label='Linear Prediction')
    plt.plot(X_test, y_pred_quadratic, color='green', linewidth=2, label='Quadratic Prediction')
    plt.plot(X_test, y_pred_cubic, color='orange', linewidth=2, label='Cubic Prediction')
    plt.plot(X_test, y_pred_svr, color='purple', linewidth=2, label='SVR Prediction')

    # Include solar flux data as an additional line in the plot
    plt.plot(X, flux_data, color='cyan', linestyle='dashed', label='Solar Flux')

    plt.title('GOES XRS Space Weather Forecast')
    plt.xlabel('Time (seconds since start)')
    plt.ylabel('X-ray Flux / Solar Flux')
    plt.legend()

    # Save the image
    plt.savefig('space_weather_forecast.png')

    # Display the plot
    #plt.show()
    fig = plt.figure()


process_data()

with gr.Blocks(title="Space Weather Forecast", analytics_enabled=False) as spaceml:
    gr.Markdown("# Space Weather Forecast")
    gr.Markdown("Welcome to the Space Weather Forecast!")
    with gr.Row():
        with gr.Column(scale=1):
            gradio_plot = gr.Image('space_weather_forecast.png')

spaceml.queue().launch(show_api=True, share=True)