Skip to content

Commit daa4864

Browse files
committed
python format
1 parent 829508e commit daa4864

1 file changed

Lines changed: 43 additions & 42 deletions

File tree

source/utils/profiler_simplified/plot_json.py

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@
1414

1515
ranks = []
1616

17+
1718
def getJsonKey(key):
1819
unit = "MB"
19-
if (key == "dataSize"):
20+
if key == "dataSize":
2021
jsonKey = "transport_0"
21-
elif (key == "metadataSize"):
22+
elif key == "metadataSize":
2223
jsonKey = "transport_1"
2324
else:
2425
if key.endswith("_mus"):
2526
jsonKey = key
2627
else:
27-
jsonKey = key+"_mus";
28+
jsonKey = key + "_mus"
2829
unit = "Sec"
2930
return jsonKey, unit
30-
31+
32+
3133
def getValue(obj, jsonKey):
3234
unitSize = 1000000
33-
35+
3436
if "_mus" in jsonKey:
3537
w = obj.get(jsonKey)
3638
elif "transport" in jsonKey:
@@ -40,26 +42,27 @@ def getValue(obj, jsonKey):
4042
w = 0
4143
unitSize = 1048576
4244
else:
43-
print ("Unable to process: ", jsonKey)
45+
print("Unable to process: ", jsonKey)
4446

45-
if (w):
46-
return float(w/unitSize)
47+
if w:
48+
return float(w / unitSize)
4749
else:
4850
return 0
49-
51+
5052

5153
def retrieve(data, key):
5254
values = []
5355
jsonKey, unit = getJsonKey(key)
5456
for obj in data:
55-
values.append( getValue(obj, jsonKey) )
57+
values.append(getValue(obj, jsonKey))
5658
return unit, values
57-
59+
60+
5861
def plotKey(key):
5962
global json_files
6063
global ranks
6164

62-
label=key
65+
label = key
6366
for filename in json_files:
6467
plt.figure(figsize=(10, 4))
6568
try:
@@ -68,35 +71,35 @@ def plotKey(key):
6871
except Exception as e:
6972
print(f"Skipping {filename}: {e}")
7073
continue
71-
72-
label_name = filename;
73-
if (len(filename) > 100):
74-
label_name = os.path.basename(filename);
75-
if isinstance(key, str):
74+
75+
label_name = filename
76+
if len(filename) > 100:
77+
label_name = os.path.basename(filename)
78+
if isinstance(key, str):
7679
unit, values = retrieve(data, key)
7780
if len(ranks) == 0:
7881
ranks = list(range(0, len(values)))
79-
plt.ylabel(label + " (" + unit + ")")
80-
plt.plot(ranks, values, marker="o", linestyle='--', label=label_name)
82+
plt.ylabel(label + " (" + unit + ")")
83+
plt.plot(ranks, values, marker="o", linestyle="--", label=label_name)
8184

8285
if isinstance(key, (list, tuple)) and all(isinstance(item, str) for item in key):
8386
label = ""
8487
val = np.array([])
8588
for item in key:
8689
unit, values = retrieve(data, item)
87-
if (len(val) == 0):
88-
val = np.array(values);
90+
if len(val) == 0:
91+
val = np.array(values)
8992
label = item
9093
if len(ranks) == 0:
91-
ranks = list(range(0, len(values)))
94+
ranks = list(range(0, len(values)))
9295
else:
93-
val += values;
94-
label += "+"+item;
95-
plt.plot(ranks, val, marker="o", linestyle='--', label=label_name+"_"+label)
96-
#plt.ylabel(label + " (" + unit + ")") ## label can be tooo long
97-
plt.ylabel( "(" + unit + ")")
96+
val += values
97+
label += "+" + item
98+
plt.plot(ranks, val, marker="o", linestyle="--", label=label_name + "_" + label)
99+
# plt.ylabel(label + " (" + unit + ")") ## label can be tooo long
100+
plt.ylabel("(" + unit + ")")
98101
plt.xlabel("Ranks")
99-
plt.title(" Values Across Ranks")
102+
plt.title(" Values Across Ranks")
100103
plt.xticks(ranks)
101104
plt.grid(True)
102105
plt.tight_layout()
@@ -110,19 +113,19 @@ def plotKey(key):
110113
print(f"Figure saved to {output_path}")
111114

112115
plt.show()
113-
116+
114117

115118
def getProfilerComponents(key, data):
116119
# pattern = re.compile(r"^abc_[^_]+_mus$")
117120
pattern = re.compile(rf"^{key}_[^_]+_mus$")
118121

119122
matches = []
120-
123+
121124
for obj in data:
122125
if isinstance(obj, dict):
123126
for key in obj:
124127
if pattern.match(key):
125-
matches.append(key[:-4]) # remove _mus
128+
matches.append(key[:-4]) # remove _mus
126129
break
127130

128131
return matches
@@ -134,27 +137,25 @@ def getProfilerComponents(key, data):
134137
## plot metadata size written on each rank
135138
plotKey("metadataSize")
136139

137-
## plot end step times on each rank
140+
## plot end step times on each rank
138141
plotKey("ES")
139-
## plot PDW/PP times on each rank
142+
## plot PDW/PP times on each rank
140143
plotKey(["PDW", "PP"])
141-
## plot indepent time blocks on each rank
144+
## plot indepent time blocks on each rank
142145
plotKey(["ES", "PDW", "PP", "BS", "DC"])
143146

144147

145-
146148
######################################################
147149
## one can replace ES with other tags of interest. ##
148150
## we will find all next level subcomponents ##
149151
## and plot out if there is any valid entries ##
150-
######################################################
152+
######################################################
151153

152-
if (len(json_files) == 1):
153-
print ("Exploring different ES components ")
154-
testFile=json_files[0];
154+
if len(json_files) == 1:
155+
print("Exploring different ES components ")
156+
testFile = json_files[0]
155157
with open(testFile) as f:
156158
data = json.load(f)
157-
m=getProfilerComponents("ES", data)
158-
if (len(m) > 0):
159+
m = getProfilerComponents("ES", data)
160+
if len(m) > 0:
159161
plotKey(m)
160-

0 commit comments

Comments
 (0)