| | """ |
| | Synthetic Data Generator: Industrial Equipment Sensor Anomaly Detection |
| | |
| | Generates realistic multivariate sensor data from a simulated manufacturing |
| | plant with 5 equipment units. Embeds 4 distinct anomaly types at ~4% rate |
| | with realistic noise, missing values, temporal correlations, and concept drift. |
| | |
| | Reproducible with fixed random seeds. |
| | """ |
| |
|
| | import numpy as np |
| | import pandas as pd |
| | from datetime import datetime, timedelta |
| |
|
| | SEED = 42 |
| | np.random.seed(SEED) |
| |
|
| | |
| | |
| | |
| | N_EQUIPMENT = 5 |
| | EQUIPMENT_IDS = [f"EQ-{str(i).zfill(3)}" for i in range(1, N_EQUIPMENT + 1)] |
| | START_DATE = datetime(2024, 1, 1) |
| | READINGS_PER_EQUIPMENT = 10000 |
| | TOTAL_ROWS = N_EQUIPMENT * READINGS_PER_EQUIPMENT |
| | ANOMALY_RATE_TARGET = 0.04 |
| |
|
| | |
| | EQUIPMENT_PROFILES = { |
| | "EQ-001": {"temp_base": 72, "vib_base": 0.15, "press_base": 150, "rpm_base": 3000, "flow_base": 45}, |
| | "EQ-002": {"temp_base": 68, "vib_base": 0.12, "press_base": 155, "rpm_base": 3200, "flow_base": 48}, |
| | "EQ-003": {"temp_base": 75, "vib_base": 0.18, "press_base": 145, "rpm_base": 2800, "flow_base": 42}, |
| | "EQ-004": {"temp_base": 70, "vib_base": 0.14, "press_base": 152, "rpm_base": 3100, "flow_base": 46}, |
| | "EQ-005": {"temp_base": 73, "vib_base": 0.16, "press_base": 148, "rpm_base": 2900, "flow_base": 44}, |
| | } |
| |
|
| | |
| | OPERATING_MODES = ["startup", "normal", "high_load", "cooldown", "idle"] |
| | MODE_DURATION_RANGE = { |
| | "startup": (15, 45), |
| | "normal": (120, 480), |
| | "high_load": (30, 180), |
| | "cooldown": (20, 60), |
| | "idle": (30, 120), |
| | } |
| | MODE_MULTIPLIERS = { |
| | "startup": {"temp": 0.85, "vib": 1.3, "press": 0.9, "rpm": 0.7, "flow": 0.6}, |
| | "normal": {"temp": 1.0, "vib": 1.0, "press": 1.0, "rpm": 1.0, "flow": 1.0}, |
| | "high_load": {"temp": 1.25, "vib": 1.5, "press": 1.15, "rpm": 1.2, "flow": 1.3}, |
| | "cooldown": {"temp": 0.9, "vib": 0.8, "press": 0.95, "rpm": 0.5, "flow": 0.4}, |
| | "idle": {"temp": 0.7, "vib": 0.3, "press": 0.85, "rpm": 0.1, "flow": 0.05}, |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | ANOMALY_TYPES = ["thermal_runaway", "bearing_degradation", "pressure_leak", "sensor_malfunction"] |
| |
|
| |
|
| | def generate_operating_mode_sequence(n_steps, rng): |
| | """Generate a realistic sequence of operating modes.""" |
| | modes = [] |
| | current_mode = "startup" |
| | steps_remaining = rng.integers(*MODE_DURATION_RANGE[current_mode]) |
| |
|
| | for _ in range(n_steps): |
| | modes.append(current_mode) |
| | steps_remaining -= 1 |
| | if steps_remaining <= 0: |
| | |
| | if current_mode == "startup": |
| | current_mode = "normal" |
| | elif current_mode == "normal": |
| | current_mode = rng.choice(["normal", "high_load", "cooldown"], p=[0.6, 0.3, 0.1]) |
| | elif current_mode == "high_load": |
| | current_mode = rng.choice(["normal", "high_load", "cooldown"], p=[0.4, 0.3, 0.3]) |
| | elif current_mode == "cooldown": |
| | current_mode = rng.choice(["idle", "normal", "startup"], p=[0.4, 0.4, 0.2]) |
| | elif current_mode == "idle": |
| | current_mode = rng.choice(["startup", "idle"], p=[0.7, 0.3]) |
| | steps_remaining = rng.integers(*MODE_DURATION_RANGE[current_mode]) |
| |
|
| | return modes |
| |
|
| |
|
| | def generate_base_sensor_data(n_steps, profile, modes, rng): |
| | """Generate correlated base sensor readings with mode-dependent behavior.""" |
| | t = np.arange(n_steps) |
| |
|
| | |
| | ambient_cycle = 3.0 * np.sin(2 * np.pi * t / 1440) + 0.5 * np.sin(2 * np.pi * t / 720) |
| |
|
| | |
| | age_factor = 1.0 + 0.0001 * t |
| |
|
| | temp_mult = np.array([MODE_MULTIPLIERS[m]["temp"] for m in modes]) |
| | vib_mult = np.array([MODE_MULTIPLIERS[m]["vib"] for m in modes]) |
| | press_mult = np.array([MODE_MULTIPLIERS[m]["press"] for m in modes]) |
| | rpm_mult = np.array([MODE_MULTIPLIERS[m]["rpm"] for m in modes]) |
| | flow_mult = np.array([MODE_MULTIPLIERS[m]["flow"] for m in modes]) |
| |
|
| | |
| | temp_base = profile["temp_base"] * temp_mult * age_factor + ambient_cycle |
| | vib_base = profile["vib_base"] * vib_mult * age_factor |
| | press_base = profile["press_base"] * press_mult |
| | rpm_base = profile["rpm_base"] * rpm_mult |
| | flow_base = profile["flow_base"] * flow_mult |
| |
|
| | |
| | temp = temp_base + rng.normal(0, 0.8, n_steps) |
| | vibration = vib_base + rng.normal(0, 0.01, n_steps) |
| | pressure = press_base + rng.normal(0, 1.5, n_steps) |
| | rpm = rpm_base + rng.normal(0, 15, n_steps) |
| | flow_rate = flow_base + rng.normal(0, 0.8, n_steps) |
| |
|
| | |
| | |
| | power = 0.012 * rpm + 0.3 * flow_rate + rng.normal(0, 0.5, n_steps) |
| | |
| | coolant_temp = np.convolve(temp, np.ones(10) / 10, mode='same') - 5 + rng.normal(0, 0.3, n_steps) |
| | |
| | acoustic_db = 40 + 80 * vibration + 0.005 * rpm + rng.normal(0, 1.0, n_steps) |
| | |
| | oil_viscosity = 100 - 0.4 * temp + rng.normal(0, 0.5, n_steps) |
| | |
| | humidity = 45 + 10 * np.sin(2 * np.pi * t / 1440 + 1.5) + rng.normal(0, 2, n_steps) |
| |
|
| | return { |
| | "temperature_c": temp, |
| | "vibration_mm_s": np.clip(vibration, 0, None), |
| | "pressure_kpa": pressure, |
| | "motor_rpm": np.clip(rpm, 0, None), |
| | "flow_rate_lpm": np.clip(flow_rate, 0, None), |
| | "power_consumption_kw": np.clip(power, 0, None), |
| | "coolant_temp_c": coolant_temp, |
| | "acoustic_level_db": np.clip(acoustic_db, 20, 120), |
| | "oil_viscosity_cst": np.clip(oil_viscosity, 10, 150), |
| | "humidity_pct": np.clip(humidity, 10, 95), |
| | "ambient_temp_c": 22 + ambient_cycle + rng.normal(0, 0.3, n_steps), |
| | } |
| |
|
| |
|
| | def inject_anomalies(sensors, modes, n_steps, rng): |
| | """Inject 4 types of anomalies into sensor data.""" |
| | is_anomaly = np.zeros(n_steps, dtype=int) |
| | anomaly_type = np.full(n_steps, "normal", dtype=object) |
| |
|
| | |
| | target_anomaly_count = int(n_steps * ANOMALY_RATE_TARGET) |
| | injected = 0 |
| |
|
| | |
| | used = np.zeros(n_steps, dtype=bool) |
| |
|
| | |
| | n_thermal = int(target_anomaly_count * 0.30) |
| | thermal_injected = 0 |
| | attempts = 0 |
| | while thermal_injected < n_thermal and attempts < 500: |
| | attempts += 1 |
| | window_len = rng.integers(15, 41) |
| | start = rng.integers(100, n_steps - window_len - 10) |
| | if used[start:start + window_len].any(): |
| | continue |
| | |
| | if modes[start] not in ("normal", "high_load"): |
| | continue |
| |
|
| | ramp = np.linspace(0, 1, window_len) ** 1.5 |
| | magnitude = rng.uniform(15, 35) |
| |
|
| | sensors["temperature_c"][start:start + window_len] += magnitude * ramp |
| | |
| | lag = min(5, window_len // 3) |
| | sensors["coolant_temp_c"][start + lag:start + window_len] += magnitude * 0.6 * ramp[:window_len - lag] |
| | |
| | sensors["oil_viscosity_cst"][start:start + window_len] -= magnitude * 0.3 * ramp |
| | |
| | sensors["power_consumption_kw"][start:start + window_len] += 2 * ramp |
| |
|
| | is_anomaly[start:start + window_len] = 1 |
| | anomaly_type[start:start + window_len] = "thermal_runaway" |
| | used[start:start + window_len] = True |
| | thermal_injected += window_len |
| |
|
| | |
| | n_bearing = int(target_anomaly_count * 0.25) |
| | bearing_injected = 0 |
| | attempts = 0 |
| | while bearing_injected < n_bearing and attempts < 500: |
| | attempts += 1 |
| | window_len = rng.integers(5, 21) |
| | start = rng.integers(100, n_steps - window_len - 10) |
| | if used[start:start + window_len].any(): |
| | continue |
| |
|
| | |
| | vib_increase = rng.uniform(0.3, 0.8) |
| | noise_amp = rng.uniform(0.05, 0.15) |
| | sensors["vibration_mm_s"][start:start + window_len] += vib_increase + noise_amp * rng.standard_normal(window_len) |
| | |
| | sensors["acoustic_level_db"][start:start + window_len] += 15 + 5 * rng.standard_normal(window_len) |
| | |
| | sensors["motor_rpm"][start:start + window_len] += rng.normal(0, 30, window_len) |
| |
|
| | is_anomaly[start:start + window_len] = 1 |
| | anomaly_type[start:start + window_len] = "bearing_degradation" |
| | used[start:start + window_len] = True |
| | bearing_injected += window_len |
| |
|
| | |
| | n_pressure = int(target_anomaly_count * 0.25) |
| | pressure_injected = 0 |
| | attempts = 0 |
| | while pressure_injected < n_pressure and attempts < 500: |
| | attempts += 1 |
| | window_len = rng.integers(20, 61) |
| | start = rng.integers(100, n_steps - window_len - 10) |
| | if used[start:start + window_len].any(): |
| | continue |
| | if modes[start] not in ("normal", "high_load"): |
| | continue |
| |
|
| | ramp = np.linspace(0, 1, window_len) |
| | pressure_drop = rng.uniform(8, 25) |
| |
|
| | sensors["pressure_kpa"][start:start + window_len] -= pressure_drop * ramp |
| | |
| | sensors["flow_rate_lpm"][start:start + window_len] += 3 * ramp |
| | |
| | sensors["power_consumption_kw"][start:start + window_len] += 1.5 * ramp |
| |
|
| | is_anomaly[start:start + window_len] = 1 |
| | anomaly_type[start:start + window_len] = "pressure_leak" |
| | used[start:start + window_len] = True |
| | pressure_injected += window_len |
| |
|
| | |
| | |
| | |
| | n_sensor_mal = int(target_anomaly_count * 0.20) |
| | sensor_mal_injected = 0 |
| | attempts = 0 |
| | while sensor_mal_injected < n_sensor_mal and attempts < 500: |
| | attempts += 1 |
| | window_len = rng.integers(10, 30) |
| | start = rng.integers(100, n_steps - window_len - 10) |
| | if used[start:start + window_len].any(): |
| | continue |
| |
|
| | |
| | target_sensor = rng.choice([ |
| | "temperature_c", "vibration_mm_s", "pressure_kpa", "motor_rpm" |
| | ]) |
| |
|
| | |
| | original = sensors[target_sensor][start:start + window_len].copy() |
| | noise_scale = np.std(sensors[target_sensor]) * rng.uniform(2, 5) |
| |
|
| | |
| | erratic = original + noise_scale * rng.standard_normal(window_len) |
| | |
| | if rng.random() > 0.5: |
| | stuck_start = rng.integers(0, max(1, window_len // 2)) |
| | stuck_len = rng.integers(3, min(10, window_len - stuck_start)) |
| | erratic[stuck_start:stuck_start + stuck_len] = original[stuck_start] |
| |
|
| | sensors[target_sensor][start:start + window_len] = erratic |
| |
|
| | |
| | |
| | is_anomaly[start:start + window_len] = 1 |
| | anomaly_type[start:start + window_len] = "sensor_malfunction" |
| | used[start:start + window_len] = True |
| | sensor_mal_injected += window_len |
| |
|
| | return is_anomaly, anomaly_type |
| |
|
| |
|
| | def add_missing_values(df, rng): |
| | """Add realistic missing value patterns.""" |
| | n = len(df) |
| | sensor_cols = [ |
| | "temperature_c", "vibration_mm_s", "pressure_kpa", "motor_rpm", |
| | "flow_rate_lpm", "power_consumption_kw", "coolant_temp_c", |
| | "acoustic_level_db", "oil_viscosity_cst", "humidity_pct" |
| | ] |
| |
|
| | |
| | for col in sensor_cols: |
| | mask = rng.random(n) < rng.uniform(0.008, 0.025) |
| | df.loc[mask, col] = np.nan |
| |
|
| | |
| | n_blocks = rng.integers(15, 30) |
| | for _ in range(n_blocks): |
| | block_sensor = rng.choice(sensor_cols) |
| | block_start = rng.integers(0, n - 20) |
| | block_len = rng.integers(5, 16) |
| | df.loc[block_start:block_start + block_len, block_sensor] = np.nan |
| |
|
| | |
| | for col in sensor_cols[:5]: |
| | missing_idx = df[df[col].isna()].index |
| | for idx in missing_idx: |
| | if rng.random() < 0.15: |
| | neighbor = rng.choice([c for c in sensor_cols if c != col]) |
| | if idx in df.index: |
| | df.loc[idx, neighbor] = np.nan |
| |
|
| | return df |
| |
|
| |
|
| | def add_concept_drift(sensors, n_steps, rng): |
| | """Add subtle concept drift — baseline shifts partway through.""" |
| | drift_point = rng.integers(n_steps // 3, 2 * n_steps // 3) |
| | drift_ramp = np.zeros(n_steps) |
| | ramp_len = rng.integers(500, 1500) |
| | end_point = min(drift_point + ramp_len, n_steps) |
| | drift_ramp[drift_point:end_point] = np.linspace(0, 1, end_point - drift_point) |
| | drift_ramp[end_point:] = 1.0 |
| |
|
| | |
| | sensors["temperature_c"] += drift_ramp * rng.uniform(1.5, 3.5) |
| | sensors["pressure_kpa"] -= drift_ramp * rng.uniform(1.0, 3.0) |
| |
|
| | return sensors |
| |
|
| |
|
| | def generate_equipment_data(eq_id, profile, rng): |
| | """Generate complete data for one equipment unit.""" |
| | n = READINGS_PER_EQUIPMENT |
| |
|
| | |
| | timestamps = [START_DATE + timedelta(minutes=i) for i in range(n)] |
| |
|
| | |
| | modes = generate_operating_mode_sequence(n, rng) |
| |
|
| | |
| | sensors = generate_base_sensor_data(n, profile, modes, rng) |
| |
|
| | |
| | sensors = add_concept_drift(sensors, n, rng) |
| |
|
| | |
| | is_anomaly, anomaly_type = inject_anomalies(sensors, modes, n, rng) |
| |
|
| | |
| | equipment_age_hours = np.arange(n) / 60 + rng.uniform(500, 5000) |
| | last_maintenance_hours = np.zeros(n) |
| | maintenance_interval = rng.integers(2000, 4000) |
| | last_maint = 0 |
| | for i in range(n): |
| | if (i - last_maint) > maintenance_interval: |
| | last_maint = i |
| | maintenance_interval = rng.integers(2000, 4000) |
| | last_maintenance_hours[i] = (i - last_maint) / 60 |
| |
|
| | |
| | data = { |
| | "timestamp": timestamps, |
| | "equipment_id": eq_id, |
| | "operating_mode": modes, |
| | **sensors, |
| | "equipment_age_hours": np.round(equipment_age_hours, 1), |
| | "hours_since_maintenance": np.round(last_maintenance_hours, 1), |
| | "is_anomaly": is_anomaly, |
| | "anomaly_type": anomaly_type, |
| | } |
| |
|
| | return pd.DataFrame(data) |
| |
|
| |
|
| | def add_leakage_features(df, rng): |
| | """ |
| | Add features that look useful but contain target leakage. |
| | These should be identified and removed by a careful agent. |
| | """ |
| | n = len(df) |
| |
|
| | |
| | |
| | df["rolling_anomaly_rate"] = ( |
| | df.groupby("equipment_id")["is_anomaly"] |
| | .transform(lambda x: x.rolling(60, center=True, min_periods=1).mean()) |
| | ) |
| | |
| | df["rolling_anomaly_rate"] += rng.normal(0, 0.005, n) |
| | df["rolling_anomaly_rate"] = df["rolling_anomaly_rate"].clip(0, 1) |
| |
|
| | |
| | |
| | df["maintenance_priority_score"] = rng.uniform(0, 30, n) |
| | anomaly_mask = df["is_anomaly"] == 1 |
| | df.loc[anomaly_mask, "maintenance_priority_score"] += rng.uniform(20, 50, anomaly_mask.sum()) |
| |
|
| | |
| | def map_alert(atype): |
| | if atype == "normal": |
| | return rng.choice(["NONE", "TEMP_WARN", "VIB_WARN"], p=[0.92, 0.04, 0.04]) |
| | elif atype == "thermal_runaway": |
| | return rng.choice(["TEMP_WARN", "TEMP_CRIT"], p=[0.3, 0.7]) |
| | elif atype == "bearing_degradation": |
| | return rng.choice(["VIB_WARN", "VIB_CRIT"], p=[0.4, 0.6]) |
| | elif atype == "pressure_leak": |
| | return rng.choice(["PRESS_WARN", "PRESS_CRIT"], p=[0.5, 0.5]) |
| | elif atype == "sensor_malfunction": |
| | return rng.choice(["SENSOR_ERR", "NONE"], p=[0.6, 0.4]) |
| | return "NONE" |
| |
|
| | df["alert_code"] = df["anomaly_type"].apply(map_alert) |
| |
|
| | return df |
| |
|
| |
|
| | def main(): |
| | rng = np.random.default_rng(SEED) |
| |
|
| | all_data = [] |
| | for eq_id, profile in EQUIPMENT_PROFILES.items(): |
| | print(f"Generating data for {eq_id}...") |
| | eq_rng = np.random.default_rng(rng.integers(0, 2**31)) |
| | eq_data = generate_equipment_data(eq_id, profile, eq_rng) |
| | all_data.append(eq_data) |
| |
|
| | df = pd.concat(all_data, ignore_index=True) |
| |
|
| | |
| | df = df.sort_values(["timestamp", "equipment_id"]).reset_index(drop=True) |
| |
|
| | |
| | df.insert(0, "reading_id", [f"R{str(i).zfill(6)}" for i in range(len(df))]) |
| |
|
| | |
| | df = add_leakage_features(df, rng) |
| |
|
| | |
| | df = add_missing_values(df, rng) |
| |
|
| | |
| | print(f"\nDataset shape: {df.shape}") |
| | print(f"Anomaly rate: {df['is_anomaly'].mean():.4f}") |
| | print(f"Anomaly type distribution:") |
| | print(df["anomaly_type"].value_counts()) |
| | print(f"\nMissing values per column:") |
| | print(df.isnull().sum()[df.isnull().sum() > 0]) |
| |
|
| | |
| | df.to_csv("/home/claude/data.csv", index=False) |
| | print(f"\nSaved to /home/claude/data.csv") |
| |
|
| | return df |
| |
|
| |
|
| | if __name__ == "__main__": |
| | df = main() |
| |
|