Michael Yuan commited on
Commit ·
1410ba5
1
Parent(s): 870452c
Refactor barplot df generation code
Browse files
app.py
CHANGED
|
@@ -9,27 +9,31 @@ DATASET_REPO = "uclanecl/NECL_GPUs"
|
|
| 9 |
USER_BLACKLIST = ["gdm", "necl", "ncel", "root"]
|
| 10 |
|
| 11 |
|
| 12 |
-
def
|
| 13 |
-
df =
|
| 14 |
-
|
|
|
|
| 15 |
df["users"] = df["users"].apply(
|
| 16 |
-
lambda user_list:
|
| 17 |
-
[u for u in user_list if u.strip() and u.strip() not in USER_BLACKLIST] or ["Free"]
|
| 18 |
-
)
|
| 19 |
)
|
| 20 |
-
|
|
|
|
| 21 |
new_df = []
|
| 22 |
-
for row in df.
|
|
|
|
| 23 |
gpu_users_num = len(row["users"])
|
| 24 |
-
|
|
|
|
| 25 |
new_row = row.copy()
|
|
|
|
|
|
|
| 26 |
new_row["count"] = 1 / gpu_users_num
|
| 27 |
new_row["username"] = username
|
|
|
|
| 28 |
new_df.append(new_row)
|
|
|
|
| 29 |
df = pd.DataFrame(new_df)
|
| 30 |
|
| 31 |
-
# Drop duplicate user columns
|
| 32 |
-
df = df.drop(columns=["users"])
|
| 33 |
return df
|
| 34 |
|
| 35 |
|
|
@@ -57,16 +61,16 @@ def plot():
|
|
| 57 |
if not df_list:
|
| 58 |
return None, pd.DataFrame()
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
last_update_time = max(
|
| 65 |
last_update_time = datetime.fromisoformat(last_update_time).strftime("%Y-%m-%d %H:%M:%S")
|
| 66 |
|
| 67 |
# Create the bar plot
|
| 68 |
fig = px.bar(
|
| 69 |
-
|
| 70 |
x="count",
|
| 71 |
y="server",
|
| 72 |
color="username",
|
|
@@ -74,27 +78,27 @@ def plot():
|
|
| 74 |
color_discrete_map={
|
| 75 |
"Free": "black",
|
| 76 |
},
|
| 77 |
-
text=
|
| 78 |
)
|
| 79 |
# fig.update_layout(
|
| 80 |
# yaxis={"categoryorder": "array", "categoryarray": dfs["server"].unique()[::-1]},
|
| 81 |
# )
|
| 82 |
fig.update_traces(textposition="inside", insidetextanchor="middle")
|
| 83 |
|
| 84 |
-
return fig,
|
| 85 |
|
| 86 |
except Exception as e:
|
| 87 |
print(f"Global error in plot: {e}")
|
| 88 |
return None, pd.DataFrame()
|
| 89 |
|
| 90 |
|
| 91 |
-
def
|
| 92 |
-
fig,
|
| 93 |
-
return fig,
|
| 94 |
|
| 95 |
|
| 96 |
demo = gr.Interface(
|
| 97 |
-
fn=
|
| 98 |
inputs=[],
|
| 99 |
outputs=[
|
| 100 |
gr.Plot(label="GPU Status", elem_classes="plotcss"),
|
|
|
|
| 9 |
USER_BLACKLIST = ["gdm", "necl", "ncel", "root"]
|
| 10 |
|
| 11 |
|
| 12 |
+
def get_plot_df(df):
|
| 13 |
+
df["users"] = df["users"].str.split(",")
|
| 14 |
+
|
| 15 |
+
# Clean users list with "Free" as the empty placeholder value
|
| 16 |
df["users"] = df["users"].apply(
|
| 17 |
+
lambda user_list: [u for u in user_list if u not in USER_BLACKLIST] or ["Free"]
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
+
|
| 20 |
+
# Construct a new DataFrame for the barplot
|
| 21 |
new_df = []
|
| 22 |
+
for row in df.iterrows():
|
| 23 |
+
users_list = row["users"]
|
| 24 |
gpu_users_num = len(row["users"])
|
| 25 |
+
|
| 26 |
+
for username in users_list:
|
| 27 |
new_row = row.copy()
|
| 28 |
+
new_row.drop(labels="users")
|
| 29 |
+
|
| 30 |
new_row["count"] = 1 / gpu_users_num
|
| 31 |
new_row["username"] = username
|
| 32 |
+
|
| 33 |
new_df.append(new_row)
|
| 34 |
+
|
| 35 |
df = pd.DataFrame(new_df)
|
| 36 |
|
|
|
|
|
|
|
| 37 |
return df
|
| 38 |
|
| 39 |
|
|
|
|
| 61 |
if not df_list:
|
| 62 |
return None, pd.DataFrame()
|
| 63 |
|
| 64 |
+
df = pd.concat(df_list, ignore_index=True)
|
| 65 |
+
df = df.fillna({"fan_speed": "N/A", "users": ""})
|
| 66 |
+
df = get_plot_df(df)
|
| 67 |
|
| 68 |
+
last_update_time = max(df["query_time"])
|
| 69 |
last_update_time = datetime.fromisoformat(last_update_time).strftime("%Y-%m-%d %H:%M:%S")
|
| 70 |
|
| 71 |
# Create the bar plot
|
| 72 |
fig = px.bar(
|
| 73 |
+
df,
|
| 74 |
x="count",
|
| 75 |
y="server",
|
| 76 |
color="username",
|
|
|
|
| 78 |
color_discrete_map={
|
| 79 |
"Free": "black",
|
| 80 |
},
|
| 81 |
+
text=df["index"].astype(str) + "<br>" + df["username"].astype(str),
|
| 82 |
)
|
| 83 |
# fig.update_layout(
|
| 84 |
# yaxis={"categoryorder": "array", "categoryarray": dfs["server"].unique()[::-1]},
|
| 85 |
# )
|
| 86 |
fig.update_traces(textposition="inside", insidetextanchor="middle")
|
| 87 |
|
| 88 |
+
return fig, df
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
print(f"Global error in plot: {e}")
|
| 92 |
return None, pd.DataFrame()
|
| 93 |
|
| 94 |
|
| 95 |
+
def generate_interface():
|
| 96 |
+
fig, df = plot()
|
| 97 |
+
return fig, df
|
| 98 |
|
| 99 |
|
| 100 |
demo = gr.Interface(
|
| 101 |
+
fn=generate_interface,
|
| 102 |
inputs=[],
|
| 103 |
outputs=[
|
| 104 |
gr.Plot(label="GPU Status", elem_classes="plotcss"),
|