FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
HydroModPy/evaluation/generate_report.py at dev · HydroModPy/HydroModPy · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
HydroModPy
/
HydroModPy
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
11
Code
Issues
18
Pull requests
1
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
HydroModPy
/
evaluation
/
generate_report.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
585 lines (512 loc) · 22.2 KB
Breadcrumbs
HydroModPy
/
evaluation
/
generate_report.py
Copy path
File metadata and controls
585 lines (512 loc) · 22.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
from
__future__
import
annotations
import
argparse
import
json
import
sys
from
collections
import
defaultdict
from
pathlib
import
Path
from
typing
import
Any
REPO_ROOT
=
Path
(
__file__
).
resolve
().
parents
[
1
]
if
str
(
REPO_ROOT
)
not
in
sys
.
path
:
sys
.
path
.
insert
(
0
,
str
(
REPO_ROOT
))
try
:
import
pandas
as
pd
except
ImportError
as
exc
:
# pragma: no cover - optional dependency
pd
=
None
PANDAS_ERROR
=
exc
else
:
PANDAS_ERROR
=
None
try
:
import
matplotlib
.
pyplot
as
plt
except
ImportError
as
exc
:
# pragma: no cover - optional dependency
plt
=
None
MPL_ERROR
=
exc
else
:
MPL_ERROR
=
None
from
evaluation
.
_utils
import
resolve_repository_root
from
evaluation
.
cohesion
import
compute_cohesion
from
evaluation
.
coupling
import
compute_cbo
from
evaluation
.
extract_architecture
import
collect_architecture
from
evaluation
.
radon_metrics
import
compute_radon_metrics
from
evaluation
.
repository_summary
import
summarize_repository
TOP_N_PACKAGES
=
15
TOP_N_MODULES
=
20
TOP_N_BOXPLOT_PACKAGES
=
10
PACKAGE_GROUP_DEPTH
=
1
def
load_json
(
path
:
Path
)
->
dict
[
str
,
Any
]:
if
not
path
.
exists
():
return
{}
return
json
.
loads
(
path
.
read_text
(
encoding
=
"utf-8"
))
def
package_summary_frame
(
summary
:
dict
[
str
,
Any
])
->
pd
.
DataFrame
:
packages
=
summary
.
get
(
"packages"
)
or
[]
if
packages
:
return
pd
.
DataFrame
(
packages
)
files
=
pd
.
DataFrame
(
summary
.
get
(
"files"
, []))
if
files
.
empty
:
return
pd
.
DataFrame
()
if
"package"
not
in
files
.
columns
:
files
[
"package"
]
=
files
[
"module"
].
apply
(
lambda
name
:
name
.
rsplit
(
"."
,
1
)[
0
]
if
"."
in
name
else
name
)
grouped
=
files
.
groupby
(
"package"
,
dropna
=
False
).
agg
(
file_count
=
(
"path"
,
"count"
),
module_count
=
(
"module"
,
"nunique"
),
public_module_count
=
(
"public"
,
"sum"
)
if
"public"
in
files
.
columns
else
(
"module"
,
"count"
),
class_count
=
(
"classes"
,
"sum"
)
if
"classes"
in
files
.
columns
else
(
"module"
,
"count"
),
method_count
=
(
"methods"
,
"sum"
)
if
"methods"
in
files
.
columns
else
(
"module"
,
"count"
),
function_count
=
(
"functions"
,
"sum"
)
if
"functions"
in
files
.
columns
else
(
"module"
,
"count"
),
line_count
=
(
"lines"
,
"sum"
)
if
"lines"
in
files
.
columns
else
(
"module"
,
"count"
),
)
return
grouped
.
reset_index
()
def
top_modules_frame
(
summary
:
dict
[
str
,
Any
])
->
pd
.
DataFrame
:
top_modules
=
summary
.
get
(
"top_modules"
)
or
[]
if
top_modules
:
return
pd
.
DataFrame
(
top_modules
)
files
=
pd
.
DataFrame
(
summary
.
get
(
"files"
, []))
if
files
.
empty
:
return
pd
.
DataFrame
()
if
"public"
in
files
.
columns
:
files
=
files
[
files
[
"public"
]]
if
files
.
empty
:
files
=
pd
.
DataFrame
(
summary
.
get
(
"files"
, []))
return
files
.
sort_values
([
"lines"
,
"module"
],
ascending
=
[
False
,
True
]).
head
(
TOP_N_MODULES
)
def
flatten_coupling
(
data
:
dict
[
str
,
Any
])
->
list
[
dict
[
str
,
Any
]]:
rows
:
list
[
dict
[
str
,
Any
]]
=
[]
for
file_info
in
data
.
get
(
"files"
, []):
module
=
file_info
.
get
(
"module"
)
package
=
file_info
.
get
(
"package"
)
public
=
file_info
.
get
(
"public"
)
for
cls
in
file_info
.
get
(
"classes"
, []):
rows
.
append
(
{
"module"
:
module
,
"package"
:
package
,
"public"
:
public
,
"path"
:
file_info
.
get
(
"path"
),
**
cls
,
}
)
return
rows
def
flatten_cohesion
(
data
:
dict
[
str
,
Any
])
->
list
[
dict
[
str
,
Any
]]:
rows
:
list
[
dict
[
str
,
Any
]]
=
[]
for
file_info
in
data
.
get
(
"files"
, []):
module
=
file_info
.
get
(
"module"
)
package
=
file_info
.
get
(
"package"
)
public
=
file_info
.
get
(
"public"
)
for
cls
in
file_info
.
get
(
"classes"
, []):
rows
.
append
(
{
"module"
:
module
,
"package"
:
package
,
"public"
:
public
,
"path"
:
file_info
.
get
(
"path"
),
**
cls
,
}
)
return
rows
def
_module_to_package
(
module
:
str
,
depth
:
int
|
None
=
None
)
->
str
:
package
=
module
.
rsplit
(
"."
,
1
)[
0
]
if
"."
in
module
else
module
if
depth
is
not
None
:
package
=
"."
.
join
(
package
.
split
(
"."
)[:
depth
])
or
package
return
package
def
build_package_dependency_frame
(
architecture
:
pd
.
DataFrame
|
dict
[
str
,
Any
],
depth
:
int
|
None
=
None
)
->
pd
.
DataFrame
:
if
isinstance
(
architecture
,
dict
):
architecture
=
pd
.
DataFrame
(
architecture
.
get
(
"modules"
, []))
if
architecture
.
empty
or
"module"
not
in
architecture
.
columns
:
return
pd
.
DataFrame
()
edge_weights
:
dict
[
tuple
[
str
,
str
],
int
]
=
defaultdict
(
int
)
for
_
,
module_info
in
architecture
.
iterrows
():
source_module
=
module_info
.
get
(
"module"
)
if
not
source_module
:
continue
source_package
=
_module_to_package
(
source_module
,
depth
)
for
dependency
in
module_info
.
get
(
"internal_dependencies"
)
or
[]:
target_package
=
_module_to_package
(
dependency
,
depth
)
if
source_package
and
target_package
and
source_package
!=
target_package
:
edge_weights
[(
source_package
,
target_package
)]
+=
1
rows
=
[
{
"source"
:
source
,
"target"
:
target
,
"weight"
:
weight
}
for
(
source
,
target
),
weight
in
sorted
(
edge_weights
.
items
())
]
return
pd
.
DataFrame
(
rows
)
def
public_only
(
frame
:
pd
.
DataFrame
)
->
pd
.
DataFrame
:
if
frame
.
empty
:
return
frame
if
"public"
in
frame
.
columns
:
public_frame
=
frame
[
frame
[
"public"
].
fillna
(
False
)]
if
not
public_frame
.
empty
:
return
public_frame
return
frame
def
package_group_column
(
frame
:
pd
.
DataFrame
,
depth
:
int
,
source_col
:
str
=
"package"
)
->
pd
.
Series
:
if
frame
.
empty
or
source_col
not
in
frame
.
columns
:
return
pd
.
Series
(
dtype
=
object
)
return
(
frame
[
source_col
]
.
fillna
(
""
)
.
astype
(
str
)
.
apply
(
lambda
value
:
"."
.
join
(
value
.
split
(
"."
)[:
depth
])
or
value
)
)
def
select_package_order
(
counts
:
pd
.
Series
,
top_n
:
int
,
pinned
:
list
[
str
])
->
list
[
str
]:
ordered
=
counts
.
sort_values
(
ascending
=
False
)
selected
=
list
(
ordered
.
head
(
top_n
).
index
)
for
package
in
pinned
:
if
package
in
ordered
.
index
and
package
not
in
selected
:
selected
.
append
(
package
)
return
selected
def
dependency_cycles_text
(
graph
:
Any
,
n
:
int
=
5
)
->
str
:
# Strongly connected components with more than one node are exactly the
# packages caught in an import cycle. This is O(V+E) via Tarjan's
# algorithm, unlike enumerating every simple cycle which can blow up
# combinatorially on a real dependency graph.
import
networkx
as
nx
cycle_groups
=
[
sorted
(
group
)
for
group
in
nx
.
strongly_connected_components
(
graph
)
if
len
(
group
)
>
1
]
if
not
cycle_groups
:
return
""
lines
=
[
" <-> "
.
join
(
group
)
for
group
in
cycle_groups
[:
n
]]
return
"Cycles de dependances:
\n
"
+
"
\n
"
.
join
(
lines
)
def
top_outliers_text
(
frame
:
pd
.
DataFrame
,
value_col
:
str
,
label_cols
:
list
[
str
],
n
:
int
=
5
)
->
str
:
if
frame
.
empty
or
value_col
not
in
frame
.
columns
:
return
""
subset
=
frame
.
dropna
(
subset
=
[
value_col
])
if
subset
.
empty
:
return
""
subset
=
subset
.
nlargest
(
n
,
value_col
)
lines
:
list
[
str
]
=
[]
for
_
,
row
in
subset
.
iterrows
():
label
=
"::"
.
join
(
str
(
row
[
col
])
for
col
in
label_cols
if
col
in
row
and
pd
.
notna
(
row
[
col
]))
lines
.
append
(
f"
{
label
}
=
{
row
[
value_col
]:.1f
}
"
)
return
"Valeurs extremes:
\n
"
+
"
\n
"
.
join
(
lines
)
def
explode_complexity_frame
(
radon
:
pd
.
DataFrame
)
->
pd
.
DataFrame
:
if
radon
.
empty
or
"complexity"
not
in
radon
.
columns
:
return
pd
.
DataFrame
()
rows
:
list
[
dict
[
str
,
Any
]]
=
[]
for
_
,
row
in
radon
.
iterrows
():
for
block
in
row
.
get
(
"complexity"
, [])
or
[]:
complexity
=
block
.
get
(
"complexity"
)
if
isinstance
(
complexity
, (
int
,
float
)):
rows
.
append
(
{
"module"
:
row
.
get
(
"module"
),
"package"
:
row
.
get
(
"package"
),
"public"
:
row
.
get
(
"public"
,
False
),
"path"
:
row
.
get
(
"path"
),
"name"
:
block
.
get
(
"name"
),
"complexity"
:
float
(
complexity
),
}
)
return
pd
.
DataFrame
(
rows
)
def
write_excel
(
output_path
:
Path
,
frames
:
tuple
[
Any
, ...])
->
None
:
if
pd
is
None
:
raise
SystemExit
(
f"pandas is required:
{
PANDAS_ERROR
}
"
)
output_path
.
parent
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
with
pd
.
ExcelWriter
(
output_path
,
engine
=
"openpyxl"
)
as
writer
:
sheet_names
=
[
"summary"
,
"radon"
,
"coupling"
,
"cohesion"
,
"architecture"
]
for
sheet_name
,
frame
in
zip
(
sheet_names
,
frames
,
strict
=
False
):
frame
.
to_excel
(
writer
,
sheet_name
=
sheet_name
,
index
=
False
)
def
write_charts
(
output_dir
:
Path
,
frames
:
tuple
[
Any
, ...],
package_depth
:
int
=
PACKAGE_GROUP_DEPTH
,
pinned_packages
:
list
[
str
]
|
None
=
None
,
)
->
None
:
if
plt
is
None
or
pd
is
None
:
return
pinned_packages
=
pinned_packages
or
[]
output_dir
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
summary
,
radon
,
coupling
,
cohesion
,
architecture
=
frames
if
not
summary
.
empty
and
"package"
in
summary
.
columns
:
summary_grouped
=
summary
.
copy
()
summary_grouped
[
"package_group"
]
=
package_group_column
(
summary_grouped
,
package_depth
)
columns
=
[
col
for
col
in
[
"line_count"
,
"class_count"
,
"function_count"
]
if
col
in
summary_grouped
.
columns
]
if
columns
:
grouped
=
summary_grouped
.
groupby
(
"package_group"
,
as_index
=
True
)[
columns
].
sum
()
rank_col
=
"line_count"
if
"line_count"
in
columns
else
columns
[
0
]
order
=
select_package_order
(
grouped
[
rank_col
],
TOP_N_PACKAGES
,
pinned_packages
)
top_packages
=
grouped
.
reindex
(
order
)
ax
=
top_packages
[
columns
].
plot
(
kind
=
"bar"
,
figsize
=
(
14
,
7
))
ax
.
set_title
(
"Top packages by size"
)
ax
.
set_xlabel
(
"Package"
)
ax
.
set_ylabel
(
"Count"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"summary.png"
,
dpi
=
200
)
plt
.
close
()
top_modules
=
summary
.
attrs
.
get
(
"top_modules"
,
pd
.
DataFrame
())
if
(
not
top_modules
.
empty
and
"lines"
in
top_modules
.
columns
and
"module"
in
top_modules
.
columns
):
ax
=
top_modules
.
set_index
(
"module"
)[
"lines"
].
plot
(
kind
=
"bar"
,
figsize
=
(
14
,
7
))
ax
.
set_title
(
"Top public modules by lines"
)
ax
.
set_xlabel
(
"Module"
)
ax
.
set_ylabel
(
"LOC"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"top_modules.png"
,
dpi
=
200
)
plt
.
close
()
if
not
radon
.
empty
:
complexity
=
explode_complexity_frame
(
radon
)
if
not
complexity
.
empty
:
ax
=
complexity
[
"complexity"
].
plot
(
kind
=
"hist"
,
bins
=
20
,
figsize
=
(
10
,
6
),
title
=
"Cyclomatic complexity distribution"
)
ax
.
set_xlabel
(
"Complexity"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"complexity_distribution.png"
,
dpi
=
200
)
plt
.
close
()
complexity
[
"package_group"
]
=
package_group_column
(
complexity
,
package_depth
)
package_counts
=
complexity
.
groupby
(
"package_group"
).
size
()
package_order
=
select_package_order
(
package_counts
,
TOP_N_BOXPLOT_PACKAGES
,
pinned_packages
)
if
package_order
:
data
=
[
complexity
.
loc
[
complexity
[
"package_group"
]
==
package
,
"complexity"
]
.
dropna
()
.
tolist
()
for
package
in
package_order
]
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
14
,
7
))
ax
.
boxplot
(
data
,
tick_labels
=
package_order
,
showmeans
=
True
)
ax
.
set_title
(
"Cyclomatic complexity by package"
)
ax
.
set_ylabel
(
"Complexity"
)
ax
.
tick_params
(
axis
=
"x"
,
rotation
=
30
)
complexity_subset
=
complexity
[
complexity
[
"package_group"
].
isin
(
package_order
)]
outliers_text
=
top_outliers_text
(
complexity_subset
,
"complexity"
, [
"module"
,
"name"
]
)
if
outliers_text
:
ax
.
text
(
1.02
,
0.98
,
outliers_text
,
transform
=
ax
.
transAxes
,
fontsize
=
7
,
va
=
"top"
,
ha
=
"left"
,
bbox
=
dict
(
boxstyle
=
"round"
,
facecolor
=
"white"
,
edgecolor
=
"gray"
,
alpha
=
0.9
),
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"complexity_boxplot.png"
,
dpi
=
200
,
bbox_inches
=
"tight"
)
plt
.
close
(
fig
)
if
"maintainability_index"
in
radon
.
columns
:
series
=
radon
[
"maintainability_index"
].
dropna
()
if
not
series
.
empty
:
ax
=
series
.
plot
(
kind
=
"hist"
,
bins
=
20
,
figsize
=
(
10
,
6
),
title
=
"Maintainability Index distribution"
,
)
ax
.
set_xlabel
(
"Maintainability Index"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"maintainability.png"
,
dpi
=
200
)
plt
.
close
()
if
not
coupling
.
empty
and
"cbo"
in
coupling
.
columns
:
coupling
=
public_only
(
coupling
).
copy
()
cbo_values
=
coupling
[
"cbo"
].
dropna
()
if
not
cbo_values
.
empty
:
ax
=
cbo_values
.
plot
(
kind
=
"hist"
,
bins
=
20
,
figsize
=
(
10
,
6
),
title
=
"CBO distribution"
)
ax
.
set_xlabel
(
"CBO"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"coupling_distribution.png"
,
dpi
=
200
)
plt
.
close
()
coupling
[
"package_group"
]
=
package_group_column
(
coupling
,
package_depth
)
packages
=
coupling
.
groupby
(
"package_group"
).
size
()
order
=
select_package_order
(
packages
,
TOP_N_BOXPLOT_PACKAGES
,
pinned_packages
)
if
order
:
data
=
[
coupling
.
loc
[
coupling
[
"package_group"
]
==
package
,
"cbo"
].
dropna
().
tolist
()
for
package
in
order
]
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
14
,
7
))
ax
.
boxplot
(
data
,
tick_labels
=
order
,
showmeans
=
True
)
ax
.
set_title
(
"CBO by package"
)
ax
.
set_ylabel
(
"CBO"
)
ax
.
tick_params
(
axis
=
"x"
,
rotation
=
30
)
coupling_subset
=
coupling
[
coupling
[
"package_group"
].
isin
(
order
)]
outliers_text
=
top_outliers_text
(
coupling_subset
,
"cbo"
, [
"module"
,
"class"
])
if
outliers_text
:
ax
.
text
(
1.02
,
0.98
,
outliers_text
,
transform
=
ax
.
transAxes
,
fontsize
=
7
,
va
=
"top"
,
ha
=
"left"
,
bbox
=
dict
(
boxstyle
=
"round"
,
facecolor
=
"white"
,
edgecolor
=
"gray"
,
alpha
=
0.9
),
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"coupling_boxplot.png"
,
dpi
=
200
,
bbox_inches
=
"tight"
)
plt
.
close
(
fig
)
if
not
cohesion
.
empty
and
"lcom"
in
cohesion
.
columns
:
cohesion
=
public_only
(
cohesion
).
copy
()
lcom_values
=
cohesion
[
"lcom"
].
dropna
()
if
not
lcom_values
.
empty
:
ax
=
lcom_values
.
plot
(
kind
=
"hist"
,
bins
=
20
,
figsize
=
(
10
,
6
),
title
=
"LCOM distribution"
)
ax
.
set_xlabel
(
"LCOM"
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"cohesion_distribution.png"
,
dpi
=
200
)
plt
.
close
()
cohesion
[
"package_group"
]
=
package_group_column
(
cohesion
,
package_depth
)
packages
=
cohesion
.
groupby
(
"package_group"
).
size
()
order
=
select_package_order
(
packages
,
TOP_N_BOXPLOT_PACKAGES
,
pinned_packages
)
if
order
:
data
=
[
cohesion
.
loc
[
cohesion
[
"package_group"
]
==
package
,
"lcom"
].
dropna
().
tolist
()
for
package
in
order
]
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
14
,
7
))
ax
.
boxplot
(
data
,
tick_labels
=
order
,
showmeans
=
True
)
ax
.
set_title
(
"LCOM by package"
)
ax
.
set_ylabel
(
"LCOM"
)
ax
.
tick_params
(
axis
=
"x"
,
rotation
=
30
)
cohesion_subset
=
cohesion
[
cohesion
[
"package_group"
].
isin
(
order
)]
outliers_text
=
top_outliers_text
(
cohesion_subset
,
"lcom"
, [
"module"
,
"class"
])
if
outliers_text
:
ax
.
text
(
1.02
,
0.98
,
outliers_text
,
transform
=
ax
.
transAxes
,
fontsize
=
7
,
va
=
"top"
,
ha
=
"left"
,
bbox
=
dict
(
boxstyle
=
"round"
,
facecolor
=
"white"
,
edgecolor
=
"gray"
,
alpha
=
0.9
),
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"cohesion_boxplot.png"
,
dpi
=
200
,
bbox_inches
=
"tight"
)
plt
.
close
(
fig
)
architecture_graph
=
build_package_dependency_frame
(
architecture
,
package_depth
)
if
not
architecture_graph
.
empty
:
packages
=
sorted
(
set
(
architecture_graph
[
"source"
]).
union
(
set
(
architecture_graph
[
"target"
]))
)
index
=
{
package
:
i
for
i
,
package
in
enumerate
(
packages
)}
matrix
=
[[
0
for
_
in
packages
]
for
_
in
packages
]
for
_
,
edge
in
architecture_graph
.
iterrows
():
matrix
[
index
[
edge
[
"source"
]]][
index
[
edge
[
"target"
]]]
+=
int
(
edge
[
"weight"
])
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
12
,
10
))
image
=
ax
.
imshow
(
matrix
,
cmap
=
"Blues"
,
aspect
=
"auto"
)
ax
.
set_title
(
"Package dependency heatmap"
)
ax
.
set_xticks
(
range
(
len
(
packages
)))
ax
.
set_yticks
(
range
(
len
(
packages
)))
ax
.
set_xticklabels
(
packages
,
rotation
=
90
,
fontsize
=
7
)
ax
.
set_yticklabels
(
packages
,
fontsize
=
7
)
fig
.
colorbar
(
image
,
ax
=
ax
,
fraction
=
0.046
,
pad
=
0.04
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"architecture_package_heatmap.png"
,
dpi
=
200
)
plt
.
close
(
fig
)
try
:
import
networkx
as
nx
except
ImportError
:
return
graph
=
nx
.
DiGraph
()
for
_
,
edge
in
architecture_graph
.
iterrows
():
graph
.
add_edge
(
edge
[
"source"
],
edge
[
"target"
],
weight
=
edge
[
"weight"
])
if
graph
.
number_of_nodes
()
>
0
:
fig
,
ax
=
plt
.
subplots
(
figsize
=
(
14
,
10
))
positions
=
nx
.
spring_layout
(
graph
,
seed
=
42
)
nx
.
draw_networkx
(
graph
,
positions
,
ax
=
ax
,
node_size
=
400
,
font_size
=
7
,
arrows
=
True
,
edge_color
=
"#888888"
,
)
ax
.
set_title
(
"Package architecture graph"
)
ax
.
axis
(
"off"
)
cycles_text
=
dependency_cycles_text
(
graph
)
if
cycles_text
:
ax
.
text
(
1.02
,
0.98
,
cycles_text
,
transform
=
ax
.
transAxes
,
fontsize
=
7
,
va
=
"top"
,
ha
=
"left"
,
bbox
=
dict
(
boxstyle
=
"round"
,
facecolor
=
"#fff3cd"
,
edgecolor
=
"#856404"
,
alpha
=
0.9
),
)
plt
.
tight_layout
()
plt
.
savefig
(
output_dir
/
"architecture_package_graph.png"
,
dpi
=
200
,
bbox_inches
=
"tight"
)
plt
.
close
(
fig
)
def
_build_frames
(
summary
:
dict
[
str
,
Any
],
radon
:
dict
[
str
,
Any
],
coupling
:
dict
[
str
,
Any
],
cohesion
:
dict
[
str
,
Any
],
architecture
:
dict
[
str
,
Any
],
)
->
tuple
[
Any
,
Any
,
Any
,
Any
,
Any
]:
summary_frame
=
package_summary_frame
(
summary
)
summary_frame
.
attrs
[
"top_modules"
]
=
top_modules_frame
(
summary
)
return
(
summary_frame
,
pd
.
DataFrame
(
radon
.
get
(
"files"
, [])),
pd
.
DataFrame
(
flatten_coupling
(
coupling
)),
pd
.
DataFrame
(
flatten_cohesion
(
cohesion
)),
pd
.
DataFrame
(
architecture
.
get
(
"modules"
, [])),
)
def
build_frames_from_repo
(
root
:
Path
):
summary
=
summarize_repository
(
root
)
radon
=
compute_radon_metrics
(
root
)
coupling
=
compute_cbo
(
root
)
cohesion
=
compute_cohesion
(
root
)
architecture
=
collect_architecture
(
root
)
architecture
=
architecture
.
get
(
"data"
,
architecture
)
return
_build_frames
(
summary
,
radon
,
coupling
,
cohesion
,
architecture
)
def
build_frames
(
input_dir
:
Path
):
if
pd
is
None
:
raise
SystemExit
(
f"pandas is required:
{
PANDAS_ERROR
}
"
)
summary
=
load_json
(
input_dir
/
"summary.json"
)
radon
=
load_json
(
input_dir
/
"radon.json"
)
coupling
=
load_json
(
input_dir
/
"coupling.json"
)
cohesion
=
load_json
(
input_dir
/
"cohesion.json"
)
architecture
=
load_json
(
input_dir
/
"architecture.json"
)
return
_build_frames
(
summary
,
radon
,
coupling
,
cohesion
,
architecture
)
def
main
()
->
None
:
parser
=
argparse
.
ArgumentParser
(
description
=
"Generate quality report"
)
parser
.
add_argument
(
"--repo"
,
type
=
str
)
parser
.
add_argument
(
"--output-dir"
,
type
=
Path
,
required
=
True
)
parser
.
add_argument
(
"--excel"
,
type
=
Path
,
default
=
None
)
parser
.
add_argument
(
"--package-depth"
,
type
=
int
,
default
=
PACKAGE_GROUP_DEPTH
,
help
=
"Number of dotted segments used to group packages in charts (1 = top-level package)"
,
)
parser
.
add_argument
(
"--pin-package"
,
action
=
"append"
,
default
=
None
,
help
=
"Package (at --package-depth granularity) to always include in the top-N charts, can be repeated"
,
)
args
=
parser
.
parse_args
()
if
pd
is
None
:
raise
SystemExit
(
f"pandas is required:
{
PANDAS_ERROR
}
"
)
with
resolve_repository_root
(
None
,
args
.
repo
,
None
)
as
root
:
frames
=
build_frames_from_repo
(
root
)
args
.
output_dir
.
mkdir
(
parents
=
True
,
exist_ok
=
True
)
names
=
[
"summary"
,
"radon"
,
"coupling"
,
"cohesion"
,
"architecture"
]
for
name
,
frame
in
zip
(
names
,
frames
,
strict
=
False
):
frame
.
to_csv
(
args
.
output_dir
/
f"
{
name
}
.csv"
,
index
=
False
)
excel_path
=
args
.
excel
if
args
.
excel
is
not
None
else
args
.
output_dir
/
"evaluation.xlsx"
write_excel
(
excel_path
,
frames
)
write_charts
(
args
.
output_dir
/
"charts"
,
frames
,
args
.
package_depth
,
args
.
pin_package
)
if
__name__
==
"__main__"
:
main
()
Back
|
FazBrowse Home
|
New Git URL