-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisualizations.py
More file actions
103 lines (87 loc) · 3.58 KB
/
Copy pathvisualizations.py
File metadata and controls
103 lines (87 loc) · 3.58 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
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
import matplotlib.pyplot as plt
def plot_resampled_data(df, year, month, output_dir="", title="Resampled Data", sample_columns=None):
"""
Plots time series data for selected columns after resampling.
"""
if sample_columns is None:
sample_columns = df.select_dtypes(include='number').columns[:3] # First 3 numeric columns
plt.figure(figsize=(14, 6))
for col in sample_columns:
plt.plot(df.index, df[col], label=col)
plt.title(f"{title} ({month}/{year})")
plt.xlabel("Time")
plt.ylabel("Value")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{output_dir}/resampled_data_{year}_{month}.png")
# plt.show()
def compare_raw_and_resampled(raw_df, resampled_df, col_index, year, month, output_dir="", title=None):
"""
Plots original vs resampled values for a given sensor column.
"""
if col_index >= len(raw_df.columns):
print(f"[SKIP] Column index {col_index} out of range")
return
sensor = raw_df.columns[col_index]
plt.figure(figsize=(14, 6))
plt.plot(raw_df.index, raw_df[sensor], label='Original', alpha=0.5)
plt.plot(resampled_df.index, resampled_df[sensor], label='Resampled', linewidth=2)
plt.title(title or f"{sensor} - Original vs Resampled ({month}/{year})")
plt.xlabel("Time")
plt.ylabel("Sensor Value")
plt.legend()
plt.grid(True)
plt.tight_layout()
# plt.show()
plt.savefig(f"{output_dir}/{sensor}_comparison_{year}_{month}.png")
def plot_rolling_stat(df, col_index, year, month, stats=('mean', 'std', 'min', 'max'), output_dir="", window=3):
if col_index >= len(df.columns):
print(f"[SKIP] Column index {col_index} out of range")
return
column = df.columns[col_index]
plt.figure(figsize=(14, 6))
plt.plot(df.index, df[column], label='Original', alpha=0.4)
rolling_cols = [f"{column}_roll_{stat}" for stat in stats]
# Check existence
missing = [col for col in rolling_cols if col not in df.columns]
if missing:
print(f"[SKIP] Missing columns: {missing}")
return
# plt.plot(df.index, df[column].rolling(window).mean(), label=f'Rolling Mean ({window})', linewidth=2)
# plt.figure(figsize=(14, 6))
for col in rolling_cols:
plt.plot(df.index, df[col], label=col, linewidth=2)
plt.title(f"{col} ({month}/{year})")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{output_dir}/{col}_{year}_{month}.png")
# plt.show()
def plot_fft_features(df, col_index, year, month, output_dir="", num_coeffs=3):
"""
Plots FFT features for a single sensor.
Parameters:
- df: DataFrame that contains FFT features.
- base_column_name: name of the sensor (e.g., 'sensor1').
- num_coeffs: number of FFT coefficients you extracted per window.
"""
if col_index >= len(df.columns):
print(f"[SKIP] Column index {col_index} out of range")
return
base_col = df.columns[col_index]
fft_cols = [f"{base_col}_fft_{i}" for i in range(num_coeffs)]
if not all(col in df.columns for col in fft_cols):
print(f"Missing FFT columns for {base_col}. Found: {[col for col in fft_cols if col in df.columns]}")
return
plt.figure(figsize=(14, 6))
for col in fft_cols:
plt.plot(df.index, df[col], label=col)
plt.title(f"FFT Features for {base_col} ({month}/{year})")
plt.xlabel("Time Window")
plt.ylabel("Magnitude")
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.savefig(f"{output_dir}/{base_col}_fft_{year}_{month}.png")
# plt.show()