-
Notifications
You must be signed in to change notification settings - Fork 38
/
plot_results.py
245 lines (216 loc) · 7.66 KB
/
plot_results.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
"""Save the number of trainable parameter and inference speed of all available models."""
# =============================================================================
# Copyright 2021 Henrique Morimitsu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# =============================================================================
import argparse
import logging
from pathlib import Path
from typing import Optional, Tuple, Union
import numpy as np
import pandas as pd
import plotly.express as px
from ptlflow.utils.utils import config_logging
config_logging()
def _init_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser()
parser.add_argument(
"--models",
type=str,
nargs="+",
default=None,
help=(
"List of model names to be loaded from the results. If not provided, all models will be loaded."
),
)
parser.add_argument(
"--exclude_models",
type=str,
nargs="+",
default=None,
help=("Optional list of model names that will not be loaded from the results."),
)
parser.add_argument(
"--metrics_csv_path",
type=str,
default=None,
help=("Path to a csv file with the metrics results."),
)
parser.add_argument(
"--benchmark_csv_path",
type=str,
default=None,
help=("Path to a csv file with the benchmark results."),
)
parser.add_argument(
"--plot_axes",
type=str,
nargs=2,
default=None,
required=True,
help=(
"Name of two measured parameters to create a scatter plot. It must correspond to a column name of the provide CSV files."
),
)
parser.add_argument(
"--checkpoint_names",
type=str,
nargs="+",
default=("things",),
help=(
"Name of checkpoints to be included in the final outputs. The names must be substrings of the values in "
"the file from --metrics_csv_path."
),
)
parser.add_argument(
"--output_path",
type=str,
default=str(Path("outputs/plots")),
help=("Path to a directory where the outputs will be saved."),
)
parser.add_argument(
"--log_x",
action="store_true",
help="If set, the X-axis of the plot will be in log-scale.",
)
parser.add_argument(
"--log_y",
action="store_true",
help="If set, the Y-axis of the plot will be in log-scale.",
)
return parser
def save_plot(
output_dir: Union[str, Path],
df: pd.DataFrame,
log_x: bool,
log_y: bool,
) -> None:
"""Create a plot of the results and save to disk.
Parameters
----------
output_dir : Union[str, Path]
Path to the directory where the plot will be saved.
df : pd.DataFrame
A DataFrame with the benchmark results.
log_x : bool
If set, the X-axis is plot in log scale.
log_y : bool
If set, the Y-axis is plot in log scale.
"""
df = df.dropna()
xkey, ykey = list(df.columns)[-2:]
symbol_sequence = [
"circle",
"diamond",
"square",
"x",
"cross",
"pentagon",
"triangle-up",
]
fig = px.scatter(
df,
x=xkey,
y=ykey,
color="checkpoint" if "checkpoint" in list(df.columns) else "model",
symbol="model",
log_x=log_x,
log_y=log_y,
title=f"{xkey} x {ykey}",
symbol_sequence=symbol_sequence,
)
fig.update_traces(
marker={"size": 20, "line": {"width": 2, "color": "DarkSlateGrey"}},
selector={"mode": "markers"},
)
fig.update_layout(title_font_size=30)
out_name = f"plot-{xkey}-{ykey}.html".replace("/", "_").replace("\\", "_")
out_path = Path(output_dir) / out_name
fig.write_html(out_path)
logging.info("Saved plot between %s and %s at: %s", xkey, ykey, out_path)
def get_available_axes(benchmark_df, metrics_df):
axes_names_to_source = {}
if benchmark_df is not None:
axes_names_to_source.update({c: "benchmark" for c in benchmark_df.columns})
if metrics_df is not None:
axes_names_to_source.update({c: "metrics" for c in metrics_df.columns})
return axes_names_to_source
def load_dataframe(args):
assert (args.benchmark_csv_path is not None) or (args.metrics_csv_path is not None)
benchmark_df = None
if args.benchmark_csv_path is not None:
benchmark_df = pd.read_csv(args.benchmark_csv_path)
benchmark_df.rename(
columns={c: c.lower() for c in benchmark_df.columns}, inplace=True
)
metrics_df = None
if args.benchmark_csv_path is not None:
metrics_df = pd.read_csv(args.metrics_csv_path)
metrics_df.rename(
columns={c: c.lower() for c in metrics_df.columns}, inplace=True
)
axes_names_to_source = get_available_axes(benchmark_df, metrics_df)
assert (
args.plot_axes[0] in axes_names_to_source
), f"{args.plot_axes[0]} is not a valid axis name. The valid names are: {axes_names_to_source.keys()}"
assert (
args.plot_axes[1] in axes_names_to_source
), f"{args.plot_axes[1]} is not a valid axis name. The valid names are: {axes_names_to_source.keys()}"
axes_sources = [axes_names_to_source[a] for a in args.plot_axes]
unique_sources = list(set(axes_sources))
base_columns = ["model"]
if len(args.checkpoint_names) > 1 or args.checkpoint_names[0] == "all":
assert (
len(unique_sources) == 1 and unique_sources[0] == "metrics"
), "Using all or more than one argument for --checkpoint_names is only supported if both --plot_axes are from the metrics CSV"
base_columns += ["checkpoint"]
if len(unique_sources) == 1:
if unique_sources[0] == "benchmark":
df = benchmark_df
if unique_sources[0] == "metrics":
df = metrics_df
df = df[df["checkpoint"] == args.checkpoint_names[0]]
else:
metrics_df = metrics_df[metrics_df["checkpoint"] == args.checkpoint_names[0]]
df = pd.merge(metrics_df, benchmark_df, "inner", "model")
df = df[base_columns + args.plot_axes]
available_model_names = list(df["model"])
if args.models is not None:
invalid_model_names = [n for n in args.models if n not in available_model_names]
if len(invalid_model_names) > 0:
logging.warning(
"The following requested models cannot be found in the csvs and will not included in the plot: %s",
", ".join(invalid_model_names),
)
model_names = [n for n in args.models if n in available_model_names]
else:
if args.exclude_models is None:
model_names = available_model_names
else:
model_names = [
n for n in available_model_names if n not in args.exclude_models
]
df = df[df["model"].isin(model_names)]
return df
if __name__ == "__main__":
parser = _init_parser()
args = parser.parse_args()
df = load_dataframe(args)
Path(args.output_path).mkdir(parents=True, exist_ok=True)
save_plot(
args.output_path,
df,
args.log_x,
args.log_y,
)