File size: 3,536 Bytes
f8d9c8a
4c16235
f8d9c8a
 
4c16235
 
 
 
 
a555792
 
626c449
4c16235
 
 
f5f591a
4d9a2f5
e904c97
4c16235
 
 
 
 
e904c97
 
 
 
 
 
 
4c16235
4d9a2f5
4c16235
 
f731ddb
4c16235
 
 
 
e904c97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c16235
 
 
 
 
a555792
4c16235
 
 
 
 
 
 
e904c97
 
 
 
 
 
 
 
 
 
 
 
 
 
3273528
 
 
 
 
 
 
 
 
4c16235
e916724
4c16235
e55813a
4c16235
 
 
f5f591a
e904c97
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
import asyncio
import random
from datetime import datetime, timedelta

import numpy as np
import pandas as pd

from .downtime import machine_errors

TOOLS_COUNT = 2

async def generate_data(state):
    """
    Generate synthetic production data for a manufacturing process.
    """
    current_time = state["date"] if state["date"] else datetime.now()
    part_id = state["part_id"] if state["part_id"] else 0

    non_compliance_rates = {
        1: 0.05,
        2: 0.10,
    }

    if 'raw_df' not in state['data']:
        state['data']['raw_df'] = pd.DataFrame(columns=[
            "Part ID", "Timestamp", "Position", "Orientation", "Tool ID",
            "Compliance", "Event", "Error Code", "Error Description",
            "Downtime Start", "Downtime End"
        ])

    for _ in range(1000):
        if not state["running"]:
            break

        if random.random() < 0.01:     # 0.005
            error_key = random.choice(list(machine_errors.keys()))
            error = machine_errors[error_key]
            downtime = error["downtime"]

            new_row = pd.DataFrame([{
                "Part ID": "N/A",
                "Timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
                "Position": "N/A",
                "Orientation": "N/A",
                "Tool ID": "N/A",
                "Compliance": "N/A",
                "Event": "Machine Error",
                "Error Code": error_key,
                "Error Description": error["description"],
                "Downtime Start": current_time.strftime("%Y-%m-%d %H:%M:%S"),
                "Downtime End": (current_time + downtime).strftime("%Y-%m-%d %H:%M:%S")
            }])

            state['data']['raw_df'] = pd.concat([state['data']['raw_df'], new_row], ignore_index=True)

            current_time += downtime
        else:
            position = np.random.normal(loc=0.4, scale=0.03)
            orientation = np.random.normal(loc=0.4, scale=0.06)
            tool_id = (part_id % TOOLS_COUNT) + 1

            if random.random() < non_compliance_rates[tool_id]:
                position = np.random.normal(loc=0.4, scale=0.2)
                orientation = np.random.normal(loc=0.4, scale=0.3)

            compliance = 'OK' if (0.3 <= position <= 0.5) and (0.2 <= orientation <= 0.6) else 'NOK'

            new_row = pd.DataFrame([{
                "Part ID": part_id,
                "Timestamp": current_time.strftime("%Y-%m-%d %H:%M:%S"),
                "Position": round(position, 4),
                "Orientation": round(orientation, 4),
                "Tool ID": tool_id,
                "Compliance": compliance,
                "Event": "N/A",
                "Error Code": "N/A",
                "Error Description": "N/A",
                "Downtime Start": "N/A",
                "Downtime End": "N/A"
            }])

            if (
                    (not new_row.empty and not new_row.isna().all().all())
                and \
                    (not state['data']['raw_df'].empty and not state['data']['raw_df'].isna().all().all())
            ):
                state['data']['raw_df'] = pd.concat([state['data']['raw_df'], new_row], ignore_index=True)

            elif not new_row.empty and not new_row.isna().all().all():
                state['data']['raw_df'] = new_row.copy()

            #print(f"- part {part_id} data generated")
            part_id += 1
            await asyncio.sleep(0.2)

        current_time += timedelta(seconds=1)

    state["date"] = current_time
    state["part_id"] = part_id