Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
SamRWest committed Feb 20, 2024
1 parent e12dfe6 commit dc07fae
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 215 deletions.
36 changes: 9 additions & 27 deletions utils/dd_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ def parse_parameter_values_from_file(
while data[index].strip() == "":
index += 1

param_name = data[index].replace(
" ' '/", ""
) # param_name is followed by this pattern
param_name = data[index].replace(" ' '/", "") # param_name is followed by this pattern

index += 1
param_data = []
Expand All @@ -64,9 +62,7 @@ def parse_parameter_values_from_file(
attributes = words[0].split(".")
attributes = [a if " " in a else a.strip("'") for a in attributes]
else:
raise ValueError(
f"Unexpected number of spaces in parameter value setting: {data[index]}"
)
raise ValueError(f"Unexpected number of spaces in parameter value setting: {data[index]}")

value = words[-1]
param_data.append([*attributes, value])
Expand Down Expand Up @@ -106,9 +102,7 @@ def parse_parameter_values_from_file(
text = words[1]
set_data.add(tuple([*attributes, text]))
else:
raise ValueError(
f"Unexpected number of spaces in set value setting: {data[index]}"
)
raise ValueError(f"Unexpected number of spaces in set value setting: {data[index]}")

index += 1

Expand Down Expand Up @@ -140,17 +134,11 @@ def save_data_with_headers(
try:
columns = headers_data[param_name]
except KeyError:
raise ValueError(
f"Could not find mapping for {param_name} in mapping file."
)
raise ValueError(f"Could not find mapping for {param_name} in mapping file.")
for row in param_data:
if len(row) != len(columns):
raise ValueError(
f"Mismatched number of columns for param {param_name} between data ({len(row)}) and mapping ({len(columns)})"
)
df = pd.DataFrame(
data=np.asarray(param_data)[:, 0 : len(columns)], columns=columns
)
raise ValueError(f"Mismatched number of columns for param {param_name} between data ({len(row)}) and mapping ({len(columns)})")
df = pd.DataFrame(data=np.asarray(param_data)[:, 0 : len(columns)], columns=columns)
df.to_csv(os.path.join(save_dir, param_name + ".csv"), index=False)

return
Expand All @@ -171,9 +159,7 @@ def generate_headers_by_attr() -> Dict[str, List[str]]:
return headers_by_attr


def convert_dd_to_tabular(
basedir: str, output_dir: str, headers_by_attr: Dict[str, List[str]]
) -> None:
def convert_dd_to_tabular(basedir: str, output_dir: str, headers_by_attr: Dict[str, List[str]]) -> None:
dd_files = [p for p in Path(basedir).rglob("*.dd")]

all_sets = defaultdict(list)
Expand Down Expand Up @@ -219,12 +205,8 @@ def convert_dd_to_tabular(

def main(arg_list: None | list[str] = None):
args_parser = argparse.ArgumentParser()
args_parser.add_argument(
"input_dir", type=str, help="Input directory containing .dd files."
)
args_parser.add_argument(
"output_dir", type=str, help="Output directory to save the .csv files in."
)
args_parser.add_argument("input_dir", type=str, help="Input directory containing .dd files.")
args_parser.add_argument("output_dir", type=str, help="Output directory to save the .csv files in.")
args = args_parser.parse_args(arg_list)
convert_dd_to_tabular(args.input_dir, args.output_dir, generate_headers_by_attr())

Expand Down
29 changes: 7 additions & 22 deletions utils/run_benchmarks.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def run_gams_gdxdiff(
return "Error: dd_files not in benchmark"

# Copy GAMS scaffolding
scaffolding_folder = path.join(
path.dirname(path.realpath(__file__)), "..", "xl2times", "gams_scaffold"
)
scaffolding_folder = path.join(path.dirname(path.realpath(__file__)), "..", "xl2times", "gams_scaffold")
shutil.copytree(scaffolding_folder, out_folder, dirs_exist_ok=True)
# Create link to TIMES source
if not path.exists(path.join(out_folder, "source")):
Expand Down Expand Up @@ -307,9 +305,7 @@ def run_all_benchmarks(
# The rest of this script checks regressions against main
# so skip it if we're already on main
repo = git.Repo(".") # pyright: ignore
origin = (
repo.remotes.origin if "origin" in repo.remotes else repo.remotes[0]
) # don't assume remote is called 'origin'
origin = repo.remotes.origin if "origin" in repo.remotes else repo.remotes[0] # don't assume remote is called 'origin'
origin.fetch("main")
if "main" not in repo.heads:
repo.create_head("main", origin.refs.main).set_tracking_branch(origin.refs.main)
Expand All @@ -332,9 +328,7 @@ def run_all_benchmarks(
result = parse_result(f.readlines()[-1])
# Use a fake runtime and GAMS result
results_main.append((benchmark["name"], 999, "--", *result))
print(
f"Skipped running on main. Using results from {path.join(benchmarks_folder, 'out-main')}"
)
print(f"Skipped running on main. Using results from {path.join(benchmarks_folder, 'out-main')}")

else:
if repo.is_dirty():
Expand Down Expand Up @@ -396,23 +390,17 @@ def run_all_benchmarks(
runtime_change = our_time - main_time

print(f"Total runtime: {our_time:.2f}s (main: {main_time:.2f}s)")
print(
f"Change in runtime (negative == faster): {runtime_change:+.2f}s ({100 * runtime_change / main_time:+.1f}%)"
)
print(f"Change in runtime (negative == faster): {runtime_change:+.2f}s ({100 * runtime_change / main_time:+.1f}%)")

our_correct = df["Correct"].sum()
main_correct = df["M Correct"].sum()
correct_change = our_correct - main_correct
print(
f"Change in correct rows (higher == better): {correct_change:+d} ({100 * correct_change / main_correct:+.1f}%)"
)
print(f"Change in correct rows (higher == better): {correct_change:+d} ({100 * correct_change / main_correct:+.1f}%)")

our_additional_rows = df["Additional"].sum()
main_additional_rows = df["M Additional"].sum()
additional_change = our_additional_rows - main_additional_rows
print(
f"Change in additional rows: {additional_change:+d} ({100 * additional_change / main_additional_rows:+.1f}%)"
)
print(f"Change in additional rows: {additional_change:+d} ({100 * additional_change / main_additional_rows:+.1f}%)")

if len(accu_regressions) + len(addi_regressions) + len(time_regressions) > 0:
print()
Expand Down Expand Up @@ -511,10 +499,7 @@ def run_all_benchmarks(
verbose=args.verbose,
debug=args.debug,
)
print(
f"Ran {args.run} in {runtime:.2f}s. {acc}% ({cor} correct, {add} additional).\n"
f"GAMS: {gms}"
)
print(f"Ran {args.run} in {runtime:.2f}s. {acc}% ({cor} correct, {add} additional).\n" f"GAMS: {gms}")
else:
run_all_benchmarks(
benchmarks_folder,
Expand Down
Loading

0 comments on commit dc07fae

Please sign in to comment.