diff --git a/ohmpi/plots.py b/ohmpi/plots.py new file mode 100644 index 0000000000000000000000000000000000000000..a4b03ac767af5ac7e7b7f47e736ddbdd800c4b32 --- /dev/null +++ b/ohmpi/plots.py @@ -0,0 +1,64 @@ +import matplotlib.pyplot as plt +import numpy as np +from utils import parse_log +from datetime import datetime +import matplotlib + +def plot_exec_log(exec_log,names=None): + time, process_id, tag, msg = parse_log(exec_log) + events = msg[tag == 'EVENT'] + + category, name, state, time = np.empty(events.shape[0]).astype(str), np.empty(events.shape[0]).astype(str), \ + np.empty(events.shape[0]).astype(str), np.empty(events.shape[0]).astype(str) + + for i, event in enumerate(events): + # print(event.split("\t")[3]) + # print('o',datetime.strptime('2023-06-16 10:04:54.222336','%Y-%m-%d %H:%M:%S.%f')) + category[i] = event.split("\t")[0] + name[i] = event.split("\t")[1] + state[i] = event.split("\t")[2] + time[i] = event.split("\t")[3].replace('\n','') #datetime.strptime(event.split("\t")[3],'%Y-%m-%d %H:%M:%S.%f') + time = time.astype(np.datetime64) + state = state[time.argsort()] + category = category[time.argsort()] + name = name[time.argsort()] + time = np.sort(time) + + if names is None: + names = dict.fromkeys(np.unique(category)) + for cat in np.unique(category): + names[cat] = np.array(np.unique(name[category == cat])) + + fig, axarr = plt.subplots(len(names.keys()),sharex=True) + print(axarr) + if not isinstance(axarr,np.ndarray): + print('no') + axarr = np.array([axarr]) + for i,cat in enumerate(names.keys()): + print(cat) + y=0 + for j,n in enumerate(names[cat]): + cmap = matplotlib.cm.get_cmap('tab20') + colors = [cmap(c/len(names[cat])) for c in range(len(names[cat]))] + event_ids = np.where((name==n) & (category==cat))[0] + y+=1 + axarr[i].set_title(cat) + label=True + for k,id in enumerate(event_ids[:-1]): + # print(state[event_ids[k]]) + if state[event_ids[k]] == 'begin' and state[event_ids[k+1]] == 'end': + if label: + axarr[i].fill_betweenx([y,y+1],time[event_ids[k]],time[event_ids[k+1]],color=colors[j],label=n) + label=False + else: + axarr[i].fill_betweenx([y, y + 1], time[event_ids[k]], time[event_ids[k + 1]], color=colors[j]) + ylabels = names[cat] + ylabelpos = np.arange(len(names[cat]))+1.5 + + axarr[i].set_yticks(ylabelpos) + axarr[i].set_yticklabels(ylabels) + axarr[i].legend() + plt.show() + + +plot_exec_log('logs/exec.log') diff --git a/ohmpi/utils.py b/ohmpi/utils.py index 3150e93f63ae2417e3210f2c85fa0601e5c30b68..8645e7ad49b1e8148c33752bfe860f60561ebfec 100644 --- a/ohmpi/utils.py +++ b/ohmpi/utils.py @@ -2,7 +2,7 @@ import io import os import shutil import collections.abc - +import numpy as np def update_dict(d, u): """Updates a dictionary by adding elements to collection items associated to existing keys @@ -59,4 +59,30 @@ def change_config(config_file, verbose=True): print(f.read()) except Exception as error: - print(f'Could not change config file to {pwd}/{config_file}:\n{error}') \ No newline at end of file + print(f'Could not change config file to {pwd}/{config_file}:\n{error}') + +def parse_log(log): + msg_started = False + msg_tmp = '' + with open(log, "r") as file: + time, process_id, msg, tag = [], [], [], [] + for i,line in enumerate(file): + if len(line.split(" | ")) > 1: + time.append(line.split(" | ")[0]) + process_id.append(line.split(" | ")[1]) + msg.append(":".join(line.split(" | ")[2].split(":")[1:])) + tag.append(line.split(" | ")[2].split(":")[0]) + elif "{" in line or msg_started: + msg_tmp = msg_tmp + line + print(msg_tmp) + msg_started = True + if "}" in line: + msg[-1] = msg[-1] + msg_tmp + msg_tmp = '' + msg_started = False + time = np.array(time) + process_id = np.array(process_id) + tag = np.array(tag) + msg = np.array(msg) + + return time, process_id, tag, msg \ No newline at end of file