Four Edge Patterns Exercise

Four Edge Patterns Exercise

animation
analytics-ml
edge
patterns

Try It Yourself

Scenario: Industrial motor monitoring with vibration sensors at 1 kHz sampling.

Pattern 1: Filter

def filter_pattern(vibration_g, threshold=5.0):
    """Send only threshold violations"""
    if vibration_g > threshold:
        return {"alert": True, "value": vibration_g, "pattern": "filter"}
    return None  # Don't transmit normal readings

# Test: 8 samples, 3 exceed threshold (5.0)
readings = [2.1, 3.5, 2.8, 6.2, 4.1, 7.8, 3.2, 5.9]
alerts = [filter_pattern(r) for r in readings if filter_pattern(r)]
print(f"Filter: {len(readings)} readings -> {len(alerts)} alerts")

Pattern 2: Aggregate

import statistics

def aggregate_pattern(readings_1sec):
    """Compute 1-second statistics from 1000 Hz samples"""
    return {
        "min": min(readings_1sec),
        "max": max(readings_1sec),
        "avg": statistics.mean(readings_1sec),
        "stddev": statistics.stdev(readings_1sec),
        "pattern": "aggregate"
    }

# Test: 1000 samples -> 4 values = 250x reduction
samples_1khz = [2.1 + (i * 0.01) for i in range(1000)]
summary = aggregate_pattern(samples_1khz)
print(f"Aggregate: 1000 samples -> 4 values = 250x reduction")

Pattern 3: Infer (simplified)

def infer_pattern(vibration_rms, threshold_trained=4.5):
    """Simplified ML inference: anomaly score based on trained threshold"""
    anomaly_score = vibration_rms / threshold_trained
    if anomaly_score > 1.2:  # 20% above normal
        return {"anomaly": True, "score": round(anomaly_score, 2),
                "pattern": "infer"}
    return None  # Normal operation

# Test: 5.8 g RMS is above trained baseline of 4.5 g
result = infer_pattern(5.8)
print(f"Infer: {result}")

Pattern 4: Store-Forward

class StoreForwardBuffer:
    def __init__(self, max_size_mb=100):
        self.buffer = []
        self.max_size = max_size_mb * 1024 * 1024

    def store(self, reading):
        self.buffer.append(reading)
        if len(self.buffer) > 1000:  # Simplified FIFO
            self.buffer.pop(0)

    def forward(self, network_available):
        if network_available and self.buffer:
            print(f"Forwarding {len(self.buffer)} buffered readings")
            self.buffer.clear()
            return True
        return False

# Test: buffer during outage, then forward
buffer = StoreForwardBuffer()
for i in range(50):
    buffer.store({"reading": i})

buffer.forward(network_available=False)  # No sync
print(f"Buffer size: {len(buffer.buffer)} readings")
buffer.forward(network_available=True)   # Sync successful
print(f"After sync: {len(buffer.buffer)} readings")

What to Observe:

  • Filter can drastically reduce transmissions when threshold violations are rare, but it loses routine trend information.
  • Aggregate preserves statistics while achieving 250x reduction
  • Infer requires trained model but enables intelligent detection
  • Store-Forward ensures no data loss during outages

Extension Challenge: Combine patterns: Filter + Aggregate. Send hourly aggregates for normal operation, immediate alerts for threshold violations.

Back to the chapter