Skip to content

Commit e16000f

Browse files
committed
fix: run ruff formatter on time_layers.py
1 parent 2519b25 commit e16000f

1 file changed

Lines changed: 42 additions & 41 deletions

File tree

validation/time_layers.py

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,17 @@
1212
Hardware should be reported in the paper caption alongside these results.
1313
"""
1414

15-
import time
1615
import platform
16+
import time
17+
1718
import numpy as np
1819

1920
# ── Configuration ───────────────────────────────────────────────────────────
20-
L = 2048 # window length (matches Table VII)
21-
N_ITER = 1000 # iterations per layer
22-
FS = 12000 # Hz (CWRU sampling rate)
23-
RPM = 1797 # CWRU drive-end shaft speed
24-
N_WARMUP = 50 # warmup iterations (excluded from timing)
21+
L = 2048 # window length (matches Table VII)
22+
N_ITER = 1000 # iterations per layer
23+
FS = 12000 # Hz (CWRU sampling rate)
24+
RPM = 1797 # CWRU drive-end shaft speed
25+
N_WARMUP = 50 # warmup iterations (excluded from timing)
2526

2627
np.random.seed(42)
2728

@@ -63,11 +64,14 @@ def time_function(func, n_iter=N_ITER, n_warmup=N_WARMUP):
6364
def time_cnn():
6465
try:
6566
import os
67+
6668
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
6769
import tensorflow as tf
70+
6871
tf.config.set_visible_devices([], 'GPU') # force CPU
6972

7073
from cansyd.perception.cnn import build_cnn
74+
7175
model = build_cnn(input_shape=(L, 1), num_classes=10)
7276
x = signal.reshape(1, L, 1)
7377

@@ -85,10 +89,10 @@ def time_cnn():
8589
times = np.array(times)
8690
return float(times.mean()), float(times.std())
8791
except ImportError:
88-
print(" [SKIP] TensorFlow not installed — CNN timing unavailable")
92+
print(' [SKIP] TensorFlow not installed — CNN timing unavailable')
8993
return None, None
9094
except Exception as e:
91-
print(f" [SKIP] CNN timing failed: {e}")
95+
print(f' [SKIP] CNN timing failed: {e}')
9296
return None, None
9397

9498

@@ -113,10 +117,7 @@ def time_causal():
113117
treatment = signal_kurtosis(signals_batch)
114118

115119
def run():
116-
analyze_causal(
117-
treatment, labels_batch, conditions_batch,
118-
domain='timing_benchmark'
119-
)
120+
analyze_causal(treatment, labels_batch, conditions_batch, domain='timing_benchmark')
120121

121122
# Causal layer uses 1000 permutations internally — time for 1 call
122123
# and report as per-batch (divide by N_BATCH for per-sample)
@@ -161,60 +162,60 @@ def run():
161162
# Main
162163
# ============================================================================
163164
if __name__ == '__main__':
164-
print("=" * 65)
165-
print("CANSYD Per-Layer Inference Timing Benchmark")
166-
print("=" * 65)
167-
print(f" Window length (L): {L}")
168-
print(f" Iterations: {N_ITER}")
169-
print(f" Warmup: {N_WARMUP}")
170-
print(f" Platform: {platform.processor()}")
171-
print(f" Python: {platform.python_version()}")
172-
print(f" CPU: {platform.machine()}")
165+
print('=' * 65)
166+
print('CANSYD Per-Layer Inference Timing Benchmark')
167+
print('=' * 65)
168+
print(f' Window length (L): {L}')
169+
print(f' Iterations: {N_ITER}')
170+
print(f' Warmup: {N_WARMUP}')
171+
print(f' Platform: {platform.processor()}')
172+
print(f' Python: {platform.python_version()}')
173+
print(f' CPU: {platform.machine()}')
173174
print()
174175

175176
results = {}
176177

177-
print("[1/5] CNN (1D-Conv + softmax)...")
178+
print('[1/5] CNN (1D-Conv + softmax)...')
178179
m, s = time_cnn()
179180
if m is not None:
180181
results['1. CNN (1D-Conv + softmax)'] = (m, s)
181-
print(f" {m:.2f} ± {s:.2f} ms")
182+
print(f' {m:.2f} ± {s:.2f} ms')
182183

183-
print("[2/5] Physics (FFT + envelope)...")
184+
print('[2/5] Physics (FFT + envelope)...')
184185
m, s = time_physics()
185186
results['2. Physics (FFT + envelope)'] = (m, s)
186-
print(f" {m:.2f} ± {s:.2f} ms")
187+
print(f' {m:.2f} ± {s:.2f} ms')
187188

188-
print("[3/5] Causal (ATE + permutation)...")
189+
print('[3/5] Causal (ATE + permutation)...')
189190
m, s = time_causal()
190191
results['3a. Causal (ATE + permutation)'] = (m, s)
191-
print(f" {m:.2f} ± {s:.2f} ms/sample")
192+
print(f' {m:.2f} ± {s:.2f} ms/sample')
192193

193-
print("[4/5] Counterfactual (sensitivity)...")
194+
print('[4/5] Counterfactual (sensitivity)...')
194195
m, s = time_counterfactual()
195196
results['3b. Counterfactual (abduction)'] = (m, s)
196-
print(f" {m:.2f} ± {s:.2f} ms")
197+
print(f' {m:.2f} ± {s:.2f} ms')
197198

198-
print("[5/5] Consensus router...")
199+
print('[5/5] Consensus router...')
199200
m, s = time_consensus()
200201
results['4. Consensus router'] = (m, s)
201-
print(f" {m:.4f} ± {s:.4f} ms")
202+
print(f' {m:.4f} ± {s:.4f} ms')
202203

203204
# ── Summary Table ───────────────────────────────────────────────────────
204205
print()
205-
print("=" * 65)
206-
print("RESULTS (for Table VII)")
207-
print("=" * 65)
208-
print(f"{'Layer':<35} {'Time (ms)':>12} {'± std':>10}")
209-
print("-" * 60)
206+
print('=' * 65)
207+
print('RESULTS (for Table VII)')
208+
print('=' * 65)
209+
print(f'{"Layer":<35} {"Time (ms)":>12} {"± std":>10}')
210+
print('-' * 60)
210211

211212
total_mean = 0.0
212213
for layer, (m, s) in results.items():
213-
print(f" {layer:<33} {m:>10.2f} ±{s:>7.2f}")
214+
print(f' {layer:<33} {m:>10.2f} ±{s:>7.2f}')
214215
total_mean += m
215216

216-
print("-" * 60)
217-
print(f" {'TOTAL':<33} {total_mean:>10.2f}")
217+
print('-' * 60)
218+
print(f' {"TOTAL":<33} {total_mean:>10.2f}')
218219
print()
219-
print("Copy these values into Table VII of main.tex.")
220-
print("Report hardware in the table caption.")
220+
print('Copy these values into Table VII of main.tex.')
221+
print('Report hardware in the table caption.')

0 commit comments

Comments
 (0)