File size: 2,018 Bytes
626c449
3273528
05a4c82
f8d9c8a
 
 
05a4c82
 
 
 
 
 
 
 
 
 
 
3273528
05a4c82
 
c587d34
05a4c82
626c449
3273528
 
 
 
c587d34
05a4c82
3273528
 
 
 
 
 
 
 
 
 
 
 
626c449
 
 
05a4c82
3273528
05a4c82
 
c587d34
 
 
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
import asyncio
from concurrent.futures import ThreadPoolExecutor

import numpy as np


def stats_metrics(data, column, usl, lsl):
    rolling_mean = data[column].expanding().mean()
    rolling_std = data[column].expanding().std()
    cp = (usl - lsl) / (6 * rolling_std)
    cpk = np.minimum(
        (usl - rolling_mean) / (3 * rolling_std),
        (rolling_mean - lsl) / (3 * rolling_std)
    )
    cpk[rolling_std == 0] = 0
    return rolling_mean, rolling_std, cp, cpk

def process_unique_tool(tool, tool_data):
    tool_data['pos_rolling_mean'], tool_data['pos_rolling_std'], tool_data['pos_rolling_cp'], tool_data['pos_rolling_cpk'] = stats_metrics(tool_data, 'Position', 0.5, 0.3)
    tool_data['ori_rolling_mean'], tool_data['ori_rolling_std'], tool_data['ori_rolling_cp'], tool_data['ori_rolling_cpk'] = stats_metrics(tool_data, 'Orientation', 0.6, 0.2)
    return tool, tool_data

async def tools_metrics(raw_data):
    filtered_data = raw_data[raw_data['Tool ID'] != 'N/A']
    tools = filtered_data['Tool ID'].unique()

    loop = asyncio.get_running_loop()
    metrics = {}

    with ThreadPoolExecutor() as executor:
        tasks = [
            loop.run_in_executor(
                executor,
                process_unique_tool,
                tool,
                filtered_data[filtered_data['Tool ID'] == tool].copy()
            )
            for tool in tools
        ]

        results = await asyncio.gather(*tasks)

    for tool, tool_data in results:
        metrics[f"tool_{tool}"] = tool_data

    all_tools_data = filtered_data.copy()
    all_tools_data['pos_rolling_mean'], all_tools_data['pos_rolling_std'], all_tools_data['pos_rolling_cp'], all_tools_data['pos_rolling_cpk'] = stats_metrics(all_tools_data, 'Position', 0.5, 0.3)
    all_tools_data['ori_rolling_mean'], all_tools_data['ori_rolling_std'], all_tools_data['ori_rolling_cp'], all_tools_data['ori_rolling_cpk'] = stats_metrics(all_tools_data, 'Orientation', 0.6, 0.2)
    metrics['all'] = all_tools_data

    return metrics