ysn-rfd/text-dataset-tiny-code-script-py-format
USED of tahamajs/medicine_ds_persian for .parquet file USED of Alijafarixcs2/persian-it-llama2-2k for .parquet file USED of Abirate/english_quotes for .jsonl file NEW FILES (05/12/2025) NEW FILES (12/26/2025) NEW FILES (02/15/2026)
31.6k
1import numpy as np
2import matplotlib.pyplot as plt
3from matplotlib.animation import FuncAnimation
4from mpl_toolkits.mplot3d import Axes3D, art3d
5import random
6
7# Parameters
8WORLD_SIZE = 100000
9AIRCRAFT_COUNT = 100
10RADAR_RANGE = 70000
11RADAR_ALTITUDE_LIMIT = 20000 # max altitude radar covers in meters
12SCAN_SPEED = 2.0 # degrees per frame
13BEAM_WIDTH = 5.0 # degrees width of radar beam
14TRACK_LENGTH = 20 # length of tail/track for aircrafts
15MAX_ACCELERATION = 5 # m/s^2 max change in velocity per frame
16
17# Aircraft types with properties
18AIRCRAFT_TYPES = {
19 'commercial': {'rcs_range': (10, 20), 'color': 'cyan', 'size': 30},
20 'military': {'rcs_range': (5, 12), 'color': 'red', 'size': 40},
21 'drone': {'rcs_range': (1, 4), 'color': 'yellow', 'size': 20},
22 'unknown': {'rcs_range': (0.5, 2), 'color': 'magenta', 'size': 25}
23}
24
25# Event Class with motion
26class MovingEvent3D:
27 def __init__(self, evt_type, center, radius, altitude, velocity):
28 self.type = evt_type
29 self.center = np.array(center, dtype=float)
30 self.radius = radius
31 self.altitude = altitude
32 self.velocity = np.array(velocity, dtype=float)
33 self.active = True
34
35 def update(self):
36 self.center += self.velocity
37 # Bounce inside world bounds for x,y
38 for i in [0, 1]:
39 if self.center[i] < 0 or self.center[i] > WORLD_SIZE:
40 self.velocity[i] = -self.velocity[i]
41 self.center[i] = np.clip(self.center[i], 0, WORLD_SIZE)
42 # Bounce altitude inside radar altitude limit
43 if self.altitude < 0 or self.altitude > RADAR_ALTITUDE_LIMIT:
44 self.velocity[2] = -self.velocity[2]
45 self.altitude = np.clip(self.altitude, 0, RADAR_ALTITUDE_LIMIT)
46 # Random on/off toggle for event activity
47 if random.random() < 0.001:
48 self.active = not self.active
49
50def generate_moving_events_3d():
51 events = []
52 for _ in range(4):
53 evt_type = random.choice(['storm', 'no-fly-zone', 'jamming', 'interference'])
54 center = np.random.uniform(0, WORLD_SIZE, 2)
55 altitude = np.random.uniform(0, RADAR_ALTITUDE_LIMIT)
56 radius = {'storm': 15000, 'no-fly-zone': 10000, 'jamming': 8000, 'interference':12000}[evt_type]
57 velocity = np.random.uniform(-50, 50, 3)
58 events.append(MovingEvent3D(evt_type, center, radius, altitude, velocity))
59 return events
60
61world_events = generate_moving_events_3d()
62
63# Generate aircrafts with altitude, track history, type and variable velocity
64def generate_aircraft_3d():
65 aircrafts = []
66 for i in range(AIRCRAFT_COUNT):
67 ac_type = random.choices(list(AIRCRAFT_TYPES.keys()), weights=[0.5,0.3,0.15,0.05])[0]
68 rcs_min, rcs_max = AIRCRAFT_TYPES[ac_type]['rcs_range']
69 ac = {
70 'id': i,
71 'type': ac_type,
72 'position': np.array([*np.random.uniform(0, WORLD_SIZE, 2), np.random.uniform(0, RADAR_ALTITUDE_LIMIT)]),
73 'velocity': np.random.uniform(-50, 50, 3),
74 'rcs': random.uniform(rcs_min, rcs_max),
75 'callsign': f"{ac_type[:2].upper()}{i:03}",
76 'emergency': random.random() < 0.03,
77 'track': [],
78 'acceleration': np.zeros(3),
79 }
80 aircrafts.append(ac)
81 return aircrafts
82
83aircrafts = generate_aircraft_3d()
84radar_angle = [0]
85radar_pos = np.array([WORLD_SIZE/2, WORLD_SIZE/2, 0])
86paused = [False]
87
88def is_event_active_3d(pos):
89 for evt in world_events:
90 if evt.active:
91 d_xy = np.linalg.norm(pos[:2] - evt.center)
92 dz = abs(pos[2] - evt.altitude)
93 if d_xy < evt.radius and dz < evt.radius / 2:
94 return evt.type
95 return None
96
97def detect_3d(ac, radar_pos):
98 delta = ac['position'] - radar_pos
99 rng = np.linalg.norm(delta)
100 if rng > RADAR_RANGE or ac['position'][2] > RADAR_ALTITUDE_LIMIT:
101 return False
102 bearing = (np.degrees(np.arctan2(delta[1], delta[0])) + 360) % 360
103 diff = abs((bearing - radar_angle[0] + 180) % 360 - 180)
104 if diff > BEAM_WIDTH / 2:
105 return False
106 evt = is_event_active_3d(ac['position'])
107 snr_val = 20 - 20*np.log10(rng + 1) + ac['rcs']
108 if evt == 'jamming':
109 snr_val -= 50
110 elif evt == 'storm':
111 snr_val -= 15
112 elif evt == 'interference':
113 snr_val -= 25
114 prob = 1 / (1 + np.exp(-(snr_val - 10)))
115 # Introduce random detection noise
116 noise = np.random.normal(0, 0.1)
117 return np.random.rand() < (prob + noise)
118
119# Setup plot
120fig = plt.figure(figsize=(14, 10))
121ax = fig.add_subplot(111, projection='3d')
122ax.set_xlim(0, WORLD_SIZE)
123ax.set_ylim(0, WORLD_SIZE)
124ax.set_zlim(0, RADAR_ALTITUDE_LIMIT)
125ax.set_facecolor('black')
126
127# Scatter for different types of aircrafts (dynamic update)
128all_scatter = ax.scatter([], [], [], c=[], s=[], label='Aircraft')
129detected_scatter = ax.scatter([], [], [], c='lime', s=60, label='Detected')
130emergency_scatter = ax.scatter([], [], [], c='orange', s=80, marker='^', label='Emergency')
131radar_sweep_line, = ax.plot([], [], [], c='cyan', linewidth=3, label='Radar Sweep')
132
133# Track lines for aircrafts
134track_lines = [ax.plot([], [], [], c='white', alpha=0.3, linewidth=1)[0] for _ in range(AIRCRAFT_COUNT)]
135
136event_spheres = []
137event_colors = {'storm':'blue', 'no-fly-zone':'yellow', 'jamming':'magenta', 'interference':'purple'}
138
139def plot_sphere(center, radius, color):
140 u = np.linspace(0, 2*np.pi, 20)
141 v = np.linspace(0, np.pi, 20)
142 x = center[0] + radius * np.outer(np.cos(u), np.sin(v))
143 y = center[1] + radius * np.outer(np.sin(u), np.sin(v))
144 z = center[2] + radius * np.outer(np.ones(np.size(u)), np.cos(v))
145 return ax.plot_surface(x, y, z, color=color, alpha=0.15)
146
147for evt in world_events:
148 sphere = plot_sphere(np.array([*evt.center, evt.altitude]), evt.radius, event_colors[evt.type])
149 event_spheres.append(sphere)
150
151# Radar range circle on ground
152radar_circle = plt.Circle((radar_pos[0], radar_pos[1]), RADAR_RANGE, color='cyan', alpha=0.1)
153ax.add_patch(radar_circle)
154art3d.pathpatch_2d_to_3d(radar_circle, z=0, zdir="z")
155
156def update(frame):
157 if paused[0]:
158 return
159
160 # بهروزرسانی زاویه رادار
161 radar_angle[0] = (radar_angle[0] + 1) % 360
162
163 all_pos = []
164 all_colors = []
165 all_sizes = []
166
167 detected_pos = []
168 emergency_pos = []
169
170 for ac in aircrafts:
171 # محدود کردن سرعت
172 v_mag = np.linalg.norm(ac['velocity'])
173 max_speed = 250 # m/s
174 if v_mag > max_speed:
175 ac['velocity'] = (ac['velocity'] / v_mag) * max_speed
176
177 # بهروزرسانی موقعیت
178 ac['position'] += ac['velocity']
179
180 # برخورد به دیوارههای جهان
181 for i in [0, 1]:
182 if ac['position'][i] < 0 or ac['position'][i] > WORLD_SIZE:
183 ac['velocity'][i] = -ac['velocity'][i]
184 ac['position'][i] = np.clip(ac['position'][i], 0, WORLD_SIZE)
185 if ac['position'][2] < 0 or ac['position'][2] > RADAR_ALTITUDE_LIMIT:
186 ac['velocity'][2] = -ac['velocity'][2]
187 ac['position'][2] = np.clip(ac['position'][2], 0, RADAR_ALTITUDE_LIMIT)
188
189 # ثبت رد حرکت
190 ac['track'].append(ac['position'].copy())
191 if len(ac['track']) > TRACK_LENGTH:
192 ac['track'].pop(0)
193
194 all_pos.append(ac['position'])
195 all_colors.append(AIRCRAFT_TYPES[ac['type']]['color'])
196 all_sizes.append(AIRCRAFT_TYPES[ac['type']]['size'])
197
198 if detect_3d(ac, radar_pos):
199 detected_pos.append(ac['position'])
200 if ac['emergency']:
201 emergency_pos.append(ac['position'])
202
203 # تبدیل به np.array
204 all_pos = np.array(all_pos)
205 detected_pos = np.array(detected_pos)
206 emergency_pos = np.array(emergency_pos)
207
208 # آپدیت scatter کل هواپیماها
209 if len(all_pos) > 0:
210 all_scatter._offsets3d = (all_pos[:,0], all_pos[:,1], all_pos[:,2])
211 all_scatter.set_color(all_colors)
212 all_scatter.set_sizes(all_sizes)
213 else:
214 all_scatter._offsets3d = ([], [], [])
215 all_scatter.set_color([])
216 all_scatter.set_sizes([])
217
218 # آپدیت scatter هواپیماهای تشخیص داده شده
219 if len(detected_pos) > 0:
220 detected_scatter._offsets3d = (detected_pos[:,0], detected_pos[:,1], detected_pos[:,2])
221 detected_scatter.set_sizes([60]*len(detected_pos))
222 else:
223 detected_scatter._offsets3d = ([], [], [])
224 detected_scatter.set_sizes([])
225
226 # آپدیت scatter هواپیماهای اضطراری
227 if len(emergency_pos) > 0:
228 emergency_scatter._offsets3d = (emergency_pos[:,0], emergency_pos[:,1], emergency_pos[:,2])
229 emergency_scatter.set_sizes([80]*len(emergency_pos))
230 else:
231 emergency_scatter._offsets3d = ([], [], [])
232 emergency_scatter.set_sizes([])
233
234 # بهروزرسانی خطوط رد حرکت
235 for i, ac in enumerate(aircrafts):
236 if len(ac['track']) >= 2:
237 track_arr = np.array(ac['track'])
238 track_lines[i].set_data(track_arr[:,0], track_arr[:,1])
239 track_lines[i].set_3d_properties(track_arr[:,2])
240 else:
241 track_lines[i].set_data([], [])
242 track_lines[i].set_3d_properties([])
243
244 # بهروزرسانی خط اسکن رادار
245 angle_rad = np.radians(radar_angle[0])
246 x = [radar_pos[0], radar_pos[0] + RADAR_RANGE * np.cos(angle_rad)]
247 y = [radar_pos[1], radar_pos[1] + RADAR_RANGE * np.sin(angle_rad)]
248 z = [0, 0]
249 radar_sweep_line.set_data(x, y)
250 radar_sweep_line.set_3d_properties(z)
251
252 ax.set_title(f"3D Radar Simulation - Scan Angle: {radar_angle[0]:.1f}°")
253
254
255def on_key(event):
256 if event.key == ' ':
257 paused[0] = not paused[0]
258
259fig.canvas.mpl_connect('key_press_event', on_key)
260
261ani = FuncAnimation(fig, update, interval=50)
262plt.legend(loc='upper right')
263plt.show()
264 