-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshow_data.py
398 lines (331 loc) · 15.4 KB
/
show_data.py
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#%%
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks, savgol_filter
from obspy import read
import glob
#%%
def load_data(file_path):
try:
if file_path.endswith('.csv'):
df = pd.read_csv(file_path)
df.columns = ['time_abs', 'time_rel_sec', 'velocity_m_s']
elif file_path.endswith('.mseed'):
st = read(file_path)
df = pd.DataFrame({
'time_abs': st[0].times('timestamp'),
'time_rel_sec': st[0].times(),
'velocity_m_s': st[0].data
})
df['time_rel_sec'] = pd.to_numeric(df['time_rel_sec'], errors='coerce')
df['velocity_m_s'] = pd.to_numeric(df['velocity_m_s'], errors='coerce')
return df
except Exception as e:
print(f"Erreur lors du chargement du fichier {file_path}: {str(e)}")
return None
#%%
def calculate_power_and_energy(df):
try:
frequencies = np.fft.fftfreq(len(df['velocity_m_s']), d=np.mean(np.diff(df['time_rel_sec'])))
main_frequency = np.abs(frequencies[np.argmax(np.abs(np.fft.fft(df['velocity_m_s'])))])
df['power'] = (df['velocity_m_s'] ** 2) / np.sqrt(main_frequency)
df['energy'] = np.cumsum(df['power'] * np.diff(np.concatenate(([0], df['time_rel_sec']))))
df['smoothed_power'] = savgol_filter(df['power'], window_length=51, polyorder=3)
return df
except Exception as e:
print(f"Erreur lors du calcul de la puissance et de l'énergie: {str(e)}")
return None
#%%
def detect_seismic_events(df, power_threshold_factor=5, energy_threshold_factor=5, min_distance=1000):
df['smoothed_power'] = savgol_filter(df['power'], window_length=51, polyorder=3)
power_threshold = np.mean(df['smoothed_power']) + power_threshold_factor * np.std(df['smoothed_power'])
energy_rate = np.diff(df['energy']) / np.diff(df['time_rel_sec'])
energy_threshold = np.mean(energy_rate) + energy_threshold_factor * np.std(energy_rate)
power_peaks, _ = find_peaks(df['smoothed_power'], height=power_threshold, distance=min_distance)
energy_peaks, _ = find_peaks(energy_rate, height=energy_threshold, distance=min_distance)
all_peaks = sorted(set(power_peaks) | set(energy_peaks))
return all_peaks
#%%
def find_event_boundaries(df, peak, window_size=500, power_threshold_factor=0.1, energy_threshold_factor=0.1):
start_index = max(0, peak - window_size)
end_index = min(len(df), peak + window_size)
event_window = df.iloc[start_index:end_index]
power_threshold = power_threshold_factor * df['smoothed_power'].iloc[peak]
energy_threshold = energy_threshold_factor * (df['energy'].iloc[peak] - df['energy'].iloc[start_index])
# Trouver le début de l'événement
for i in range(peak, start_index, -1):
if df['smoothed_power'].iloc[i] < power_threshold and (
df['energy'].iloc[peak] - df['energy'].iloc[i]) < energy_threshold:
start = i
break
else:
start = start_index
# Trouver la fin de l'événement
for i in range(peak, end_index):
if df['smoothed_power'].iloc[i] < power_threshold and (
df['energy'].iloc[i] - df['energy'].iloc[peak]) < energy_threshold:
end = i
break
else:
end = end_index
return start, end
#%%
def plot_results(df, events):
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(15, 20), sharex=True)
ax1.plot(df['time_rel_sec'], df['velocity_m_s'], label='Vitesse', color='green')
ax1.set_ylabel('Vitesse (m/s)')
ax1.legend()
ax1.grid(True)
ax2.plot(df['time_rel_sec'], df['power'], label='Puissance', color='blue', alpha=0.5)
ax2.plot(df['time_rel_sec'], df['smoothed_power'], label='Puissance lissée', color='darkblue')
ax2.set_ylabel('Puissance (W)')
ax2.set_yscale('log')
ax2.legend()
ax2.grid(True)
ax3.plot(df['time_rel_sec'], df['energy'], label='Énergie cumulée', color='red')
ax3.set_xlabel('Temps Relatif (sec)')
ax3.set_ylabel('Énergie (J)')
ax3.legend()
ax3.grid(True)
for start, peak, end in events:
for ax in (ax1, ax2, ax3):
ax.axvline(x=df['time_rel_sec'].iloc[start], color='green', linestyle='--', alpha=0.7)
ax.axvline(x=df['time_rel_sec'].iloc[peak], color='purple', linestyle='--', alpha=0.7)
ax.axvline(x=df['time_rel_sec'].iloc[end], color='red', linestyle='--', alpha=0.7)
plt.title('Analyse des Données Sismiques')
plt.tight_layout()
plt.show()
#%%
def process_file(file_path):
print(f"Traitement du fichier : {file_path}")
df = load_data(file_path)
df = calculate_power_and_energy(df)
peaks = detect_seismic_events(df)
events = []
for peak in peaks:
start, end = find_event_boundaries(df, peak)
events.append((start, peak, end))
plot_results(df, events)
return {
'file': file_path,
'events': [(df['time_abs'].iloc[start], df['time_abs'].iloc[peak], df['time_abs'].iloc[end]) for
start, peak, end in events]
}
#%%
def process_all_files(directory):
file_patterns = ['*.csv', '*.mseed']
all_files = []
for pattern in file_patterns:
all_files.extend(glob.glob(f"{directory}/{pattern}"))
results = [process_file(file) for file in all_files]
return results
#%%
def plot_full_signal(df, events):
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(15, 20), sharex=True)
ax1.plot(df['time_rel_sec'], df['velocity_m_s'], label='Vitesse', color='green')
ax1.set_ylabel('Vitesse (m/s)')
ax1.legend()
ax1.grid(True)
ax2.plot(df['time_rel_sec'], df['power'], label='Puissance', color='blue', alpha=0.5)
ax2.plot(df['time_rel_sec'], df['smoothed_power'], label='Puissance lissée', color='darkblue')
ax2.set_ylabel('Puissance (W)')
ax2.set_yscale('log')
ax2.legend()
ax2.grid(True)
ax3.plot(df['time_rel_sec'], df['energy'], label='Énergie cumulée', color='red')
ax3.set_xlabel('Temps Relatif (sec)')
ax3.set_ylabel('Énergie (J)')
ax3.legend()
ax3.grid(True)
for start, peak, end in events:
for ax in (ax1, ax2, ax3):
ax.axvline(x=df['time_rel_sec'].iloc[start], color='green', linestyle='--', alpha=0.7)
ax.axvline(x=df['time_rel_sec'].iloc[peak], color='purple', linestyle='--', alpha=0.7)
ax.axvline(x=df['time_rel_sec'].iloc[end], color='red', linestyle='--', alpha=0.7)
plt.title('Analyse Complète des Données Sismiques')
plt.tight_layout()
plt.show()
#%%
def find_main_oscillation(df, power_threshold_factor=5, min_duration=10, max_duration=300):
try:
if len(df) == 0:
print("Le DataFrame est vide.")
return None, None
power_threshold = np.mean(df['smoothed_power']) + power_threshold_factor * np.std(df['smoothed_power'])
above_threshold = df['smoothed_power'] > power_threshold
oscillation_starts = np.where(above_threshold[:-1] == False)[0]
oscillation_ends = np.where(above_threshold[1:] == False)[0] + 1
min_length = min(len(oscillation_starts), len(oscillation_ends))
oscillation_starts = oscillation_starts[:min_length]
oscillation_ends = oscillation_ends[:min_length]
valid_oscillations = []
for start, end in zip(oscillation_starts, oscillation_ends):
if start >= len(df) or end >= len(df):
continue
duration = df['time_rel_sec'].iloc[end] - df['time_rel_sec'].iloc[start]
if min_duration <= duration <= max_duration:
amplitude = df['smoothed_power'].iloc[start:end].max()
valid_oscillations.append((start, end, amplitude, duration))
if not valid_oscillations:
print("Aucune oscillation valide trouvée.")
return None, None
# Sélectionner l'oscillation avec la plus grande amplitude
main_oscillation = max(valid_oscillations, key=lambda x: x[2])
return main_oscillation[0], main_oscillation[1] # Retourner start et end
except Exception as e:
print(f"Erreur lors de la recherche de l'oscillation principale: {str(e)}")
return None, None
#%%
def plot_main_oscillation(df, start, end):
try:
if start is None or end is None or start >= len(df) or end >= len(df) or start < 0 or end < 0:
print("Indices de début ou de fin invalides pour le tracé.")
return
start, end = min(start, end), max(start, end)
end = min(end, len(df) - 1)
event_df = df.iloc[start:end + 1]
if len(event_df) == 0:
print("Aucune donnée à tracer pour l'événement.")
return
peak = start + event_df['smoothed_power'].idxmax() - event_df.index[0]
peak = max(0, min(peak, len(event_df) - 1))
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 15), sharex=True)
ax1.plot(event_df['time_rel_sec'], event_df['velocity_m_s'], label='Vitesse', color='green')
ax1.set_ylabel('Vitesse (m/s)')
ax1.legend()
ax1.grid(True)
ax2.plot(event_df['time_rel_sec'], event_df['power'], label='Puissance', color='blue', alpha=0.5)
ax2.plot(event_df['time_rel_sec'], event_df['smoothed_power'], label='Puissance lissée', color='darkblue')
ax2.set_ylabel('Puissance (W)')
ax2.set_yscale('log')
ax2.legend()
ax2.grid(True)
ax3.plot(event_df['time_rel_sec'], event_df['energy'], label='Énergie cumulée', color='red')
ax3.set_xlabel('Temps Relatif (sec)')
ax3.set_ylabel('Énergie (J)')
ax3.legend()
ax3.grid(True)
for ax in (ax1, ax2, ax3):
ax.axvline(x=event_df['time_rel_sec'].iloc[0], color='green', linestyle='--', alpha=0.7, label='Début')
ax.axvline(x=event_df['time_rel_sec'].iloc[peak], color='purple', linestyle='--', alpha=0.7, label='Pic')
ax.axvline(x=event_df['time_rel_sec'].iloc[-1], color='red', linestyle='--', alpha=0.7, label='Fin')
duration = event_df['time_rel_sec'].iloc[-1] - event_df['time_rel_sec'].iloc[0]
plt.title(f'Oscillation Principale (Durée: {duration:.2f}s)')
plt.tight_layout()
plt.show()
except Exception as e:
print(f"Erreur lors de la création du graphique: {str(e)}")
print(f"Détails - start: {start}, end: {end}, len(df): {len(df)}")
#%%
def process_file(file_path):
print(f"Traitement du fichier : {file_path}")
df = load_data(file_path)
if df is None or len(df) == 0:
print(f"Impossible de charger ou DataFrame vide pour {file_path}")
return {'file': file_path, 'event': None}
df = calculate_power_and_energy(df)
if df is None:
print(f"Erreur lors du calcul de la puissance et de l'énergie pour {file_path}")
return {'file': file_path, 'event': None}
start, end = find_main_oscillation(df)
if start is not None and end is not None:
plot_main_oscillation(df, start, end)
return {
'file': file_path,
'event': (df['time_abs'].iloc[start], df['time_abs'].iloc[end])
}
else:
print(f"Aucune oscillation principale détectée pour {file_path}")
return {'file': file_path, 'event': None}
#%%
def plot_seismic_event(df, start, end):
try:
if start is None or end is None or start >= len(df) or end >= len(df) or start < 0 or end < 0:
print("Indices de début ou de fin invalides pour le tracé.")
return
# Assurez-vous que start est inférieur à end
start, end = min(start, end), max(start, end)
# Limitez end à la longueur du DataFrame
end = min(end, len(df) - 1)
event_df = df.iloc[start:end+1]
if len(event_df) == 0:
print("Aucune donnée à tracer pour l'événement.")
return
# Trouvez le pic dans l'intervalle de l'événement
peak = start + event_df['smoothed_power'].idxmax() - event_df.index[0]
peak = max(0, min(peak, len(event_df) - 1)) # Assurez-vous que peak est dans les limites
fig, (ax1, ax2, ax3) = plt.subplots(3, 1, figsize=(12, 15), sharex=True)
ax1.plot(event_df['time_rel_sec'], event_df['velocity_m_s'], label='Vitesse', color='green')
ax1.set_ylabel('Vitesse (m/s)')
ax1.legend()
ax1.grid(True)
ax2.plot(event_df['time_rel_sec'], event_df['power'], label='Puissance', color='blue', alpha=0.5)
ax2.plot(event_df['time_rel_sec'], event_df['smoothed_power'], label='Puissance lissée', color='darkblue')
ax2.set_ylabel('Puissance (W)')
ax2.set_yscale('log')
ax2.legend()
ax2.grid(True)
ax3.plot(event_df['time_rel_sec'], event_df['energy'], label='Énergie cumulée', color='red')
ax3.set_xlabel('Temps Relatif (sec)')
ax3.set_ylabel('Énergie (J)')
ax3.legend()
ax3.grid(True)
for ax in (ax1, ax2, ax3):
ax.axvline(x=event_df['time_rel_sec'].iloc[0], color='green', linestyle='--', alpha=0.7, label='Début')
ax.axvline(x=event_df['time_rel_sec'].iloc[peak], color='purple', linestyle='--', alpha=0.7, label='Pic')
ax.axvline(x=event_df['time_rel_sec'].iloc[-1], color='red', linestyle='--', alpha=0.7, label='Fin')
plt.title(f'Événement Sismique Principal (Début: {event_df["time_rel_sec"].iloc[0]:.2f}s, Fin: {event_df["time_rel_sec"].iloc[-1]:.2f}s)')
plt.tight_layout()
plt.show()
except Exception as e:
print(f"Erreur lors de la création du graphique: {str(e)}")
print(f"Détails - start: {start}, end: {end}, len(df): {len(df)}")
#%%
def process_file(file_path):
print(f"Traitement du fichier : {file_path}")
df = load_data(file_path)
if df is None or len(df) == 0:
print(f"Impossible de charger ou DataFrame vide pour {file_path}")
return {'file': file_path, 'event': None}
df = calculate_power_and_energy(df)
if df is None:
print(f"Erreur lors du calcul de la puissance et de l'énergie pour {file_path}")
return {'file': file_path, 'event': None}
start, end = find_main_oscillation(df)
if start is not None and end is not None:
plot_seismic_event(df, start, end)
return {
'file': file_path,
'event': (df['time_abs'].iloc[start], df['time_abs'].iloc[end])
}
else:
print(f"Aucun événement sismique principal détecté pour {file_path}")
return {'file': file_path, 'event': None}
#%%
def process_all_files(directory):
file_patterns = ['*.csv', '*.mseed']
all_files = []
for pattern in file_patterns:
all_files.extend(glob.glob(f"{directory}/{pattern}"))
results = []
for file in all_files:
try:
result = process_file(file)
results.append(result)
except Exception as e:
print(f"Erreur lors du traitement du fichier {file}: {str(e)}")
return results
# Exemple d'utilisation
directory = '.'
results = process_all_files(directory)
# Afficher les résultats
for result in results:
print(f"\nFichier: {result['file']}")
if result['event']:
start, end = result['event']
print(f"Événement sismique principal détecté:")
print(f" Début: {start}")
print(f" Fin: {end}")
else:
print("Aucun événement sismique principal détecté.")