-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_plot.py
More file actions
65 lines (52 loc) · 2.18 KB
/
utils_plot.py
File metadata and controls
65 lines (52 loc) · 2.18 KB
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
import matplotlib.pyplot as plt
import os
from pyotdr.read import sorparse
TRAZAS_DIR = r"C:\MLpractica3\TRAZAS\Centrales\Chiminangos\CABLE 1"
def generate_plot(id_cable, id_hilo, output_path):
# Find the sor file
filename = f"{id_hilo}.sor"
file_path = os.path.join(TRAZAS_DIR, filename)
if not os.path.exists(file_path):
# Intentar buscar variaciones del nombre si existe
files = [f for f in os.listdir(TRAZAS_DIR) if f.startswith(f"{id_hilo}")]
if files:
file_path = os.path.join(TRAZAS_DIR, files[0])
else:
return None
status, results, tracedata = sorparse(file_path)
if status != "ok":
return None
if not isinstance(tracedata, list):
return None
distances = []
losses = []
for line in tracedata:
try:
parts = line.strip().split('\t')
if len(parts) == 2:
distances.append(float(parts[0]))
losses.append(float(parts[1]))
except ValueError:
pass
if not distances:
return None
plt.figure(figsize=(10, 5))
plt.plot(distances, losses, color='#00a2e8', linewidth=1.5)
plt.title(f"OTDR Trace - Cable {id_cable}, Hilo {id_hilo}", fontsize=14, pad=15)
plt.xlabel("Distance (km)", fontsize=12)
plt.ylabel("Attenuation (dB)", fontsize=12)
plt.grid(True, linestyle='--', alpha=0.7)
plt.gca().invert_yaxis() # In OTDR plots, usually lower values (more loss) go down, or just regular.
# Actually OTDR plots show backscatter power (dB) on Y axis. Higher is more power.
# The data usually shows dB values. Typical OTDR plot has 0 at top and goes down, or just standard.
# Pyotdr parses it as loss or backscatter. Let's just plot it normally first.
plt.gca().invert_yaxis() # OTDRs usually have power decreasing downwards.
# Mark key events if we have them
key_events = results.get('KeyEvents', {}).get('events', [])
for ev in key_events:
dist = float(ev.get('distance', 0))
plt.axvline(x=dist, color='red', linestyle=':', alpha=0.5)
plt.tight_layout()
plt.savefig(output_path, dpi=150)
plt.close()
return output_path