Skip to content

Commit ea3a628

Browse files
committed
Simplify maintainability script
1 parent 1642d45 commit ea3a628

File tree

3 files changed

+62
-85
lines changed

3 files changed

+62
-85
lines changed

benchmarks/maintainability/config.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ modeling_interfaces:
77
linopy:
88
github: PyPSA/linopy
99
include_paths: ["linopy"]
10-
pyomo_core:
10+
pyomo_no_contrib:
1111
github: Pyomo/pyomo
12-
include_paths: ["pyomo/core"]
12+
include_paths: ["pyomo"]
13+
exclude_paths: ["pyomo/contrib"]
14+
pyomo:
15+
github: Pyomo/pyomo
16+
include_paths: ["pyomo"]
1317
cvxpy:
1418
github: cvxpy/cvxpy
1519
include_paths:
1620
- "cvxpy"
1721
exclude_paths:
1822
- "cvxpy/cvxcore/include/Eigen"
19-
- "cvxpy/cvxcore/python"
23+
- "cvxpy/cvxcore/python" # mostly auto-generated or third-party code
2024
jump:
2125
github: jump-dev/JuMP.jl
2226
include_paths: ["src"]
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
modeling_interface,Python,C++,Julia,total,factor
2-
pyoframe,2614,,,2614,1.0
3-
pulp,7793,,,7793,3.0
4-
linopy,8088,,,8088,3.1
5-
jump,,,12258,12258,4.7
6-
pyoptinterface,5338,17981,,23319,8.9
7-
cvxpy,27306,1300,,28606,10.9
8-
pyomo_core,28753,,,28753,11.0
1+
modeling_interface,Python,Julia,C/C++,total,factor
2+
pyoframe,2614,,0,2614,1.0
3+
pulp,7793,,0,7793,3.0
4+
linopy,8095,,0,8095,3.1
5+
jump,,12258,0,12258,4.7
6+
pyoptinterface,5338,,17981,23319,8.9
7+
cvxpy,27456,,1300,28756,11.0
8+
pyomo_no_contrib,82344,,0,82344,31.5
9+
pyomo,151503,,6925,158428,60.6

benchmarks/maintainability/run.py

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,57 @@
88
import yaml
99

1010

11-
def check_installed():
12-
try:
13-
subprocess.run(
14-
["git", "--version"],
15-
check=True,
16-
stdout=subprocess.DEVNULL,
17-
stderr=subprocess.DEVNULL,
18-
)
19-
except subprocess.CalledProcessError as e:
20-
raise RuntimeError(
21-
"Git is not installed. Please install Git to run this script."
22-
) from e
11+
def main():
12+
cwd = Path(__file__).parent
2313

24-
try:
25-
subprocess.run(
26-
["cloc", "--version"],
27-
check=True,
28-
stdout=subprocess.DEVNULL,
29-
stderr=subprocess.DEVNULL,
14+
# Read config.yaml
15+
with open(cwd / "config.yaml") as file:
16+
config = yaml.safe_load(file)
17+
18+
results = pl.DataFrame()
19+
20+
for modeling_interface, mi_config in config["modeling_interfaces"].items():
21+
github = mi_config["github"].lower()
22+
results = pl.concat(
23+
[
24+
results,
25+
measure_lines_of_code(
26+
github,
27+
cwd / "downloads" / github.partition("/")[2],
28+
include_paths=mi_config["include_paths"],
29+
exclude_paths=mi_config.get("exclude_paths", []),
30+
include_exts=config["valid_extensions"],
31+
exclude_dirs=config["always_exclude_dirs"],
32+
).with_columns(modeling_interface=pl.lit(modeling_interface)),
33+
],
34+
how="diagonal",
3035
)
31-
except subprocess.CalledProcessError as e:
32-
raise RuntimeError(
33-
"cloc is not installed. Please install cloc to run this script."
34-
) from e
36+
37+
results = results.with_columns(
38+
(
39+
pl.col("C++").fill_null(0)
40+
+ pl.col("C/C++ Header").fill_null(0)
41+
+ pl.col("C").fill_null(0)
42+
).alias("C/C++")
43+
).drop("C/C++ Header", "C", "C++")
44+
results = results.select(
45+
"modeling_interface",
46+
*[col for col in results.columns if col not in ("modeling_interface", "SUM")],
47+
total="SUM",
48+
).sort("total")
49+
results = results.with_columns(
50+
factor=(
51+
pl.col("total")
52+
/ results.filter(modeling_interface="pyoframe")["total"].item()
53+
).round(1)
54+
)
55+
print(results)
56+
57+
results.write_csv(cwd / "results.csv")
3558

3659

3760
def measure_lines_of_code(
38-
github,
61+
github: str,
3962
downloads_dir: Path,
4063
*,
4164
include_paths: list[str],
@@ -47,7 +70,6 @@ def measure_lines_of_code(
4770
url = "https://github.com/" + github
4871
subprocess.run(["git", "clone", url, downloads_dir], check=True)
4972

50-
# run cloc
5173
cmd = ["cloc"]
5274
cmd += include_paths
5375
if exclude_paths:
@@ -81,55 +103,5 @@ def measure_lines_of_code(
81103
return df
82104

83105

84-
def main():
85-
check_installed()
86-
87-
cwd = Path(__file__).parent
88-
89-
# Read config.yaml
90-
with open(cwd / "config.yaml") as file:
91-
config = yaml.safe_load(file)
92-
93-
always_exclude_dirs = config["always_exclude_dirs"]
94-
valid_extensions = config["valid_extensions"]
95-
96-
results = pl.DataFrame()
97-
98-
for modeling_interface, mi_config in config["modeling_interfaces"].items():
99-
results = pl.concat(
100-
[
101-
results,
102-
measure_lines_of_code(
103-
mi_config["github"],
104-
cwd / "downloads" / modeling_interface,
105-
include_paths=mi_config["include_paths"],
106-
exclude_paths=mi_config.get("exclude_paths", []),
107-
include_exts=valid_extensions,
108-
exclude_dirs=always_exclude_dirs,
109-
).with_columns(modeling_interface=pl.lit(modeling_interface)),
110-
],
111-
how="diagonal",
112-
)
113-
114-
# reorder columns
115-
results = results.with_columns(
116-
(pl.col("C++") + pl.col("C/C++ Header")).alias("C++")
117-
).drop("C/C++ Header")
118-
results = results.select(
119-
"modeling_interface",
120-
*[col for col in results.columns if col not in ("modeling_interface", "SUM")],
121-
total="SUM",
122-
).sort("total")
123-
results = results.with_columns(
124-
factor=(
125-
pl.col("total")
126-
/ results.filter(modeling_interface="pyoframe")["total"].item()
127-
).round(1)
128-
)
129-
130-
print(results)
131-
results.write_csv(cwd / "results.csv")
132-
133-
134106
if __name__ == "__main__":
135107
main()

0 commit comments

Comments
 (0)