onokoro-python-lib
Loading...
Searching...
No Matches
strasse_log_checker.py
1"""!
2@file test
3@version 1
4@author Fumutaka ENDO
5@date 2025-06-28T18:50:58+09:00
6@brief log viewer
7"""
8import pandas as pd
9import glob
10import plotly.express as px
11import plotly.graph_objects as go
12from plotly.subplots import make_subplots
13import pytz
14import sys
15import os
16import argparse
17
18def parse_temperature_log(file_path):
19 """!
20 @brief parse strasse log file
21
22 @param efile_path input file path
23
24 @return DataFrame
25 """
26 data_lines = []
27
28 in_comment_block = False
29 with open(file_path, 'r') as f:
30 for line in f:
31 line = line.strip()
32 # コメントブロックのトグル
33 if line.startswith("///"):
34 in_comment_block = not in_comment_block
35 continue
36 if in_comment_block or line == '':
37 continue
38 data_lines.append(line)
39
40 # データをタブで分割してDataFrame化
41 df = pd.DataFrame([line.split("\t") for line in data_lines])
42
43 # 列名付与(1列目: timestamp、以降 val_1, val_2, ...)
44 df.columns = ["timestamp"] + [f"val_{i}" for i in range(1, len(df.columns))]
45
46 return df
47
48def check_log_onokoro57(input_path=None):
49 """!
50 @brief read and plot logs
51
52 @param input_path input file path
53 """
54 # ログファイルをすべて読み込む
55 log_files = sorted(glob.glob(input_path)) # 例:カレントディレクトリにある .txt ファイルすべて
56
57 all_data = pd.concat([parse_temperature_log(f) for f in log_files], ignore_index=True)
58
59 new_columns = ["Timestamp","Target Holder (bottom)","Target Holder (1/3)","Target Holder (top)","Cold Head (stage 1)","Feed-Through (Supply)","Feed-Through (Return)","2nd stage CH (SD)","2nd stage CH (PT)","Setpoint(1)","PT02 (Cryostat filling line)","PT01 (Buffer Tank)","Target Chamber","Cryostat Chamber","Gas System Pump","heater output 1","heater output 2","Cold Valve (ON=100%/OFF=0%)","Compressor (ON=100%/OFF=0%)","H2 Filling (ON=100%/OFF=0%)"]
60
61 all_data.columns = new_columns
62
63 # 数値列を float に変換(非数は NaN)
64 for col in all_data.columns[1:]:
65 all_data[col] = pd.to_numeric(all_data[col], errors='coerce')
66
67 all_data['Timestamp'] = all_data['Timestamp'].str.replace("h/", ":", regex=False)\
68 .str.replace("m/", ":", regex=False)\
69 .str.replace("s", "", regex=False)
70
71 all_data['Timestamp'] = pd.to_datetime(all_data['Timestamp'], format="%Y/%m/%d_%H:%M:%S")
72 # 1. まずドイツ時間をローカライズ(夏時間あり:Europe/Berlin)
73 all_data['Timestamp'] = all_data['Timestamp'].dt.tz_localize('Europe/Berlin')
74 # 2. 日本時間(JST)に変換
75 all_data['Timestamp'] = all_data['Timestamp'].dt.tz_convert('Asia/Tokyo')
76
77
78 # x軸列(timestampなど)
79 x_col = all_data.columns[0]
80
81 plot_y_labels = ["Temperatures [K]", "Pressures [bar]", "tatus / heater output [%]"]
82
83 # 描画する列グループ(例:2〜10列、11〜15列、16〜20列)
84 groups = [
85 all_data.columns[2:11],
86 all_data.columns[11:16],
87 all_data.columns[16:21],
88 ]
89
90 # サブプロットを作成(行数 = グループ数)
91 fig = make_subplots(rows=len(groups), cols=1, shared_xaxes=False, vertical_spacing=0.05)
92
93 # 各グループを描画
94 for i, group in enumerate(groups):
95 for col in group:
96 fig.add_trace(
97 go.Scatter(
98 x=all_data[x_col],
99 y=all_data[col],
100 mode='lines',
101 name=col,
102 showlegend=(i >= 0) # 最初の行だけに凡例を表示
103 ),
104 row=i + 1,
105 col=1
106 )
107 fig.update_yaxes(title_text=f"{plot_y_labels[i]}", row=i + 1, col=1)
108
109 # レイアウト調整
110 fig.update_layout(
111 height=300 * len(groups),
112 title_text="STRASSE log viewr",
113 legend=dict(
114 orientation="v",
115 x=1.05,
116 y=1,
117 xanchor='left',
118 yanchor='top'
119 )
120 )
121
122 fig.show()
123
124def check_log():
125 """!
126 @brief log plot method for uv command
127
128 CLI argument:
129 @arg input files path for loading
130 @arg experiment-number experiment number
131 """
132 parser = argparse.ArgumentParser()
133 parser.add_argument("-in","--input", help="files path for loading", type=str, default="./data/onokoro57/logs/*.txt")
134 parser.add_argument("-en","--experiment-number", help="experiment number", type=int, default=57)
135 args = parser.parse_args()
136
137 input_path: str = args.input
138 experiment_number: int = args.experiment_number
139
140 if experiment_number == 57:
141 check_log_onokoro57(input_path)
142 else:
143 print('experiment number does not exist')