File size: 8,059 Bytes
6d364d3
 
 
 
e0e3f52
6d364d3
 
e0e3f52
6d364d3
 
 
 
 
 
 
 
 
 
 
 
 
e0e3f52
 
6d364d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e3f52
 
 
6d364d3
 
 
 
 
 
 
 
 
e0e3f52
 
 
 
 
 
6d364d3
 
 
 
 
e0e3f52
6d364d3
 
e0e3f52
6d364d3
 
e0e3f52
 
 
6d364d3
 
e0e3f52
 
 
6d364d3
e0e3f52
 
6d364d3
 
 
 
 
 
 
e0e3f52
6d364d3
 
e0e3f52
6d364d3
 
 
 
 
 
 
 
 
 
 
e0e3f52
 
 
 
 
 
 
 
 
 
 
 
6d364d3
 
 
 
 
 
e0e3f52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d364d3
 
 
 
 
 
 
e0e3f52
6d364d3
 
 
e0e3f52
6d364d3
 
 
 
 
e0e3f52
6d364d3
 
e0e3f52
 
 
 
 
 
 
 
 
 
6d364d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0e3f52
 
 
 
 
 
 
6d364d3
 
e0e3f52
 
 
 
 
 
 
 
 
6d364d3
 
 
 
 
e0e3f52
 
 
 
 
 
 
6d364d3
 
 
 
 
 
 
 
e0e3f52
 
6d364d3
 
e0e3f52
6d364d3
 
 
 
 
e0e3f52
 
 
6d364d3
e0e3f52
 
6d364d3
 
 
e0e3f52
 
6d364d3
e0e3f52
6d364d3
 
 
e0e3f52
 
 
 
6d364d3
 
 
e0e3f52
 
 
 
 
6d364d3
 
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
import gradio as gr
import pandas as pd
import numpy as np
import plotly.graph_objects as go
import os, random, math

# --------------------------------------------------
# Paths
# --------------------------------------------------
PATH_FINAL = "final_combined_full_table.csv"
PATH_XTEST = "X_test_target.csv"
PATH_OUTPUT = "output.csv"

GLOBE_R = 6371  # Earth radius

# --------------------------------------------------
# Utility: seeded random vector
# --------------------------------------------------
def seeded_vec(seed):
    r = random.Random(seed)
    phi = r.random() * 2 * math.pi
    cos_t = 2 * r.random() - 1
    sin_t = math.sqrt(1 - cos_t * cos_t)
    return np.array([sin_t * math.cos(phi), sin_t * math.sin(phi), cos_t])

# --------------------------------------------------
# Load data
# --------------------------------------------------
df_final = pd.read_csv(PATH_FINAL)
df_xtest = pd.read_csv(PATH_XTEST)
df_output = pd.read_csv(PATH_OUTPUT)

# --------------------------------------------------
# Synthesize positions if missing
# --------------------------------------------------
if not {"relative_position_r", "relative_position_t", "relative_position_n"}.issubset(df_final.columns):
    mags = df_final["rel_pos_mag"].fillna(1).values
    xs, ys, zs = [], [], []
    for cid, mag in zip(df_final["conjunction_id"], mags):
        v = seeded_vec(int(cid))
        v *= float(mag)
        xs.append(v[0])
        ys.append(v[1])
        zs.append(v[2])
    df_final["relative_position_r"] = xs
    df_final["relative_position_t"] = ys
    df_final["relative_position_n"] = zs

# --------------------------------------------------
# Color mapping
# --------------------------------------------------
def alert_color(level):
    L = str(level).upper()
    if "HIGH" in L:
        return "#ff1744"
    if "MEDIUM" in L:
        return "#ff9100"
    if "LOW" in L:
        return "#00e676"
    return "#9e9e9e"

df_final["_color"] = df_final["dl_score_level"].apply(alert_color)

# --------------------------------------------------
# 3D Earth
# --------------------------------------------------
def make_earth():
    u = np.linspace(0, 2 * np.pi, 72)
    v = np.linspace(0, np.pi, 36)
    u, v = np.meshgrid(u, v)
    x = GLOBE_R * np.cos(u) * np.sin(v)
    y = GLOBE_R * np.sin(u) * np.sin(v)
    z = GLOBE_R * np.cos(v)

    return go.Surface(
        x=x,
        y=y,
        z=z,
        showscale=False,
        opacity=0.90,
        colorscale=[[0, "black"], [1, "#1e3a8a"]],
    )

# --------------------------------------------------
# Build 3D Visualization
# --------------------------------------------------
def build_orbit_plot(orbit, frame, highlight):
    sub = df_final.copy()

    if orbit != "ALL":
        sub = sub[sub["orbit_regime"] == orbit]

    sub = sub[sub["conjunction_id"] % 200 == frame]

    SCALE = 4.5

    fig = go.Figure()
    fig.add_trace(make_earth())

    xs = GLOBE_R + sub["relative_position_r"].astype(float) * SCALE
    ys = GLOBE_R + sub["relative_position_t"].astype(float) * SCALE
    zs = GLOBE_R + sub["relative_position_n"].astype(float) * SCALE

    fig.add_trace(
        go.Scatter3d(
            x=xs,
            y=ys,
            z=zs,
            mode="markers",
            marker=dict(size=3, color=sub["_color"], opacity=0.95),
            text=sub["conjunction_id"].astype(str),
            hoverinfo="text",
            name="Conjunction Events",
        )
    )

    if highlight:
        try:
            cid = int(highlight)
            t = df_final[df_final["conjunction_id"] == cid]
            if not t.empty:
                xs2 = GLOBE_R + t["relative_position_r"].astype(float) * SCALE
                ys2 = GLOBE_R + t["relative_position_t"].astype(float) * SCALE
                zs2 = GLOBE_R + t["relative_position_n"].astype(float) * SCALE

                fig.add_trace(
                    go.Scatter3d(
                        x=xs2,
                        y=ys2,
                        z=zs2,
                        mode="lines+markers",
                        line=dict(width=6, color="yellow"),
                        marker=dict(size=6, color="yellow"),
                        name=f"Track {cid}",
                    )
                )
        except:
            pass

    fig.update_layout(
        scene=dict(
            xaxis=dict(visible=False),
            yaxis=dict(visible=False),
            zaxis=dict(visible=False),
        ),
        template="plotly_dark",
        height=650,
        margin=dict(l=0, r=0, t=40, b=0),
    )

    return fig

# --------------------------------------------------
# Top alerts table
# --------------------------------------------------
def top_alerts(n):
    cols = [
        "conjunction_id",
        "orbit_regime",
        "dl_score_fixed",
        "dl_score_level",
        "ppo_action_name",
        "final_mode_fixed",
    ]
    if n <= 0:
        return pd.DataFrame()
    return df_final.sort_values("dl_score_fixed", ascending=False)[cols].head(n)

# --------------------------------------------------
# Inspector
# --------------------------------------------------
def inspect_event(cid):
    try:
        cid = int(cid)
    except:
        return pd.DataFrame(), "Invalid ID"

    row = df_final[df_final["conjunction_id"] == cid]
    if row.empty:
        return pd.DataFrame(), "Not Found"

    r = row.iloc[0]
    txt = f"""
Orbit Regime: {r.get('orbit_regime')}
Miss Distance: {r.get('miss_distance')}
DL Score: {r.get('dl_score_fixed')} ({r.get('dl_score_level')})
PPO Action: {r.get('ppo_action_name')}
Final Mode: {r.get('final_mode_fixed')}
"""
    return row, txt

# --------------------------------------------------
# Custom Dark Mode CSS
# --------------------------------------------------
dark_css = """
body { background-color: #111 !important; color: white !important; }
.gradio-container { background-color: #111 !important; }
label, input, textarea { color: white !important; }
"""

# --------------------------------------------------
# UI
# --------------------------------------------------
orbit_list = ["ALL"] + sorted(df_final["orbit_regime"].dropna().unique().tolist())

with gr.Blocks(title="Space Collision Dashboard") as demo:

    gr.HTML(f"<style>{dark_css}</style>")

    gr.Markdown(
        "<h1 style='text-align:center;color:white;'>🚀 Space Collision Dashboard — Premium Dark Mode</h1>"
    )

    with gr.Row():
        with gr.Column(scale=2):
            orbit = gr.Dropdown(orbit_list, label="Orbit Regime")
            frame = gr.Slider(0, 199, value=0, step=1, label="Frame Index")
            highlight = gr.Textbox(label="Highlight Conjunction ID")
            plot = gr.Plot(label="3D Orbit Visualizer")

            btn_plot = gr.Button("Render 3D View")
            btn_plot.click(
                fn=build_orbit_plot,
                inputs=[orbit, frame, highlight],
                outputs=plot,
            )

        with gr.Column(scale=1):
            gr.Markdown("### Alert Statistics")

            high = int((df_final["dl_score_level"] == "HIGH").sum())
            med = int((df_final["dl_score_level"] == "MEDIUM").sum())
            low = int((df_final["dl_score_level"] == "LOW").sum())

            gr.Markdown(
                f"""
- **High Alerts:** {high}
- **Medium Alerts:** {med}
- **Low Alerts:** {low}
"""
            )

            gr.Markdown("### Top Alerts Table")
            top_n = gr.Slider(5, 50, value=10, step=5, label="Top N Alerts")
            top_table = gr.Dataframe()

            btn_top = gr.Button("Load Top Alerts")
            btn_top.click(fn=top_alerts, inputs=top_n, outputs=top_table)

            gr.Markdown("### Event Inspector")
            cid_box = gr.Textbox(label="Enter Conjunction ID")
            inspect_table = gr.Dataframe()
            inspect_text = gr.Textbox(label="Details")

            btn_insp = gr.Button("Inspect")
            btn_insp.click(
                fn=inspect_event, inputs=cid_box, outputs=[inspect_table, inspect_text]
            )

demo.launch()