forked from se-sic/coronet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util-networks-covariates.R
1801 lines (1613 loc) · 96.2 KB
/
util-networks-covariates.R
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## This file is part of coronet, which is free software: you
## can redistribute it and/or modify it under the terms of the GNU General
## Public License as published by the Free Software Foundation, version 2.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License along
## with this program; if not, write to the Free Software Foundation, Inc.,
## 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
##
## Copyright 2017 by Felix Prasse <[email protected]>
## Copyright 2018-2019 by Claus Hunsen <[email protected]>
## Copyright 2018-2019 by Thomas Bock <[email protected]>
## Copyright 2021, 2023 by Thomas Bock <[email protected]>
## Copyright 2018-2019 by Klara Schlüter <[email protected]>
## Copyright 2018 by Jakob Kronawitter <[email protected]>
## Copyright 2020 by Christian Hechtl <[email protected]>
## Copyright 2021 by Johannes Hostert <[email protected]>
## Copyright 2022 by Niklas Schneider <[email protected]>
## Copyright 2022 by Jonathan Baumann <[email protected]>
## All Rights Reserved.
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Libraries ---------------------------------------------------------------
requireNamespace("logging") # for logging
requireNamespace("parallel") # for parallel computation
requireNamespace("plyr") # for ldply function
requireNamespace("igraph") # networks
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Utility functions -------------------------------------------------------
#' Utility function to compute vertex attributes for a list of network
#'
#' Important: This function only works for lists of networks which have timestamps used in their range names.
#'
#' This method is a wrapper combining the steps of splitting the project data and calculating the attribute.
#'
#' Important: If a vertex attribute can have multiple values per vertex, the \code{compute.attr} function
#' has to wrap all attribute vectors of a single vertex into a list. In that case, also the
#' \code{default.value} needs to be put into a list. By that, this function can differentiate between
#' attributes having multiple values and attributes having, at most, one value.
#'
#' @param list.of.networks The list of networks to add vertex attributes to
#' @param project.data The entire project data
#' @param attr.name The name of the attribute to add
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value
#' @param compute.attr The function to compute the attribute to add. Must return a named list
#' with the names being the name of the vertex.
#'
#' @return A list of networks with the added attribute
split.and.add.vertex.attribute = function(list.of.networks, project.data, attr.name,
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value, compute.attr, list.attributes = FALSE) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
net.to.range.list = split.data.by.networks(list.of.networks, project.data, aggregation.level)
nets.with.attr = add.vertex.attribute(net.to.range.list, attr.name, default.value, compute.attr, list.attributes)
return(nets.with.attr)
}
#' Utility function to compute vertex attributes for a list of network-to-range tuples.
#'
#' Important: If a vertex attribute can have multiple values per vertex, the \code{compute.attr} function
#' has to wrap all attribute vectors of a single vertex into a list. In that case, also the
#' \code{default.value} needs to be put into a list. By that, this function can differentiate between
#' attributes having multiple values and attributes having, at most, one value.
#'
#' @param net.to.range.list A list containing tuples with networks and corresponding range data.
#' @param attr.name The name of the attribute to add
#' @param default.value The default value to add if a vertex has no matching value
#' @param compute.attr The function to compute the attribute to add. Must return a named list
#' with the names being the name of the vertex.
#'
#' @return A list of networks with the added attribute
add.vertex.attribute = function(net.to.range.list, attr.name, default.value, compute.attr, list.attributes = FALSE) {
nets.with.attr = mapply(
names(net.to.range.list), net.to.range.list,
SIMPLIFY = FALSE, FUN = function(range, net.to.range) {
current.network = net.to.range[["network"]]
range.data = net.to.range[["data"]]
attrs.by.vertex.name = compute.attr(range, range.data, current.network)
## catch the primitive case that the list of given attributes is empty or not set
if (length(attrs.by.vertex.name) == 0 || is.null(attrs.by.vertex.name)) {
## construct list of attribute name and type
attributes = structure(class(default.value), names = attr.name)
## add the vertex attribute (default value = 'NA')
net.with.attr = add.attributes.to.network(current.network, "vertex", attributes)
## overwrite set vertex attribute with 'default.value', given the case that there are indeed vertices
## in the current network
net.with.attr = igraph::set.vertex.attribute(net.with.attr, attr.name, value = default.value)
## return immediately
return(net.with.attr)
}
get.or.default = function(name, data, default) {
if (name %in% names(data)) {
return(data[[name]])
} else {
return(default)
}
}
attributes = lapply(igraph::V(current.network)$name,
function(x) get.or.default(x, attrs.by.vertex.name, default.value))
## simplify the list of attributes to a vector if all its elements are just vectors (not lists)
if (length(attributes) > 0 && !any(sapply(attributes, is.list))) {
attributes = unlist(attributes)
}
## otherwise, the list of attributes contains lists, so we can only remove the outermost list
else if (!list.attributes) {
attributes = unlist(attributes, recursive = FALSE)
}
net.with.attr = igraph::set.vertex.attribute(current.network, attr.name, value = attributes)
return(net.with.attr)
}
)
return (nets.with.attr)
}
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Author network functions ------------------------------------------------
#' Add count-based attribute based on \code{count.method}
#'
#' Note: This is a helper function for all other functions adding a count-based
#' vertex attribute.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#' @param commit.count.method The method reference for counting the data
#' @param name.column The name of the column which contains the author information
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.count.helper = function(list.of.networks, project.data, name,
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L, count.method, name.column) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
nets.with.attr = split.and.add.vertex.attribute(
list.of.networks, project.data, name, aggregation.level, default.value,
function(range, range.data, net) {
count.df = count.method(range.data)[c(name.column, "freq")]
if (!is.data.frame(count.df)) {
return(list())
}
count.list = structure(count.df[["freq"]], names = count.df[[name.column]])
return(count.list)
}
)
return(nets.with.attr)
}
## * Commit count ----------------------------------------------------------
#' Add commit-count attribute based on author name
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count = function(list.of.networks, project.data, name = "commit.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.author.commit.count, "author.name"
)
return(nets.with.attr)
}
#' Add commit-count attribute based on author name where author is not committer
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count.author.not.committer"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count.not.committer = function(list.of.networks, project.data,
name = "commit.count.author.not.committer",
aggregation.level = c("range", "cumulative",
"all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.committer.not.author.commit.count, "author.name"
)
return(nets.with.attr)
}
#' Add commit-count attribute based on comitter name
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count.committer"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count.committer = function(list.of.networks, project.data,
name = "commit.count.committer",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.committer.commit.count, "committer.name"
)
return(nets.with.attr)
}
#' Add commit-count attribute based on comitter name where committer is not author
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count.committer.not.author"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count.committer.not.author = function(list.of.networks, project.data,
name = "commit.count.committer.not.author",
aggregation.level = c("range", "cumulative",
"all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.committer.not.author.commit.count, "committer.name"
)
return(nets.with.attr)
}
#' Add commit-count attribute based on committer name where the committer equals the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count.committer.and.author"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count.committer.and.author = function(list.of.networks, project.data,
name = "commit.count.committer.and.author",
aggregation.level = c("range", "cumulative",
"all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.committer.and.author.commit.count, "committer.name"
)
return(nets.with.attr)
}
#' Add commit-count attribute based on commits where the person represented by the vertex is the committer
#' or the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "commit.count.committer.or.author"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.commit.count.committer.or.author = function(list.of.networks, project.data,
name = "commit.count.committer.or.author",
aggregation.level = c("range", "cumulative",
"all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.committer.or.author.commit.count, "name"
)
return(nets.with.attr)
}
#' Add unique artifact count attribute
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "artifact.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.artifact.count = function(list.of.networks, project.data, name = "artifact.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
nets.with.attr = split.and.add.vertex.attribute(
list.of.networks, project.data, name, aggregation.level, default.value,
function(range, range.data, net) {
lapply(range.data$group.artifacts.by.data.column("commits", "author.name"),
function(x) {
length(unique(x[["artifact"]]))
}
)
}
)
return(nets.with.attr)
}
## * Mail count ----------------------------------------------------------
#' Add mail-count attribute based on the total number of mails sent,
#' where the person represented by the vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "mail.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.mail.count = function(list.of.networks, project.data,
name = "mail.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.author.mail.count, "author.name"
)
return(nets.with.attr)
}
#' Add mail-thread-count attribute based on the number of mail threads participated in,
#' where the person represented by the vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "mail.thread.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.mail.thread.count = function(list.of.networks, project.data,
name = "mail.thread.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L) {
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, get.author.mail.thread.count, "author.name"
)
return(nets.with.attr)
}
## * Issue / PR count --------------------------------------------------------------
#' Add issue-count attribute based on the number of issues participated in,
#' where the person represented by the vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add. You might want to change this [default: "issue.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#' @param issue.type The issue kind,see \code{preprocess.issue.data} [default: "all"]
#' @param use.unfiltered.data whether to use unfiltered issue data (see \code{preprocess.issue.data}) [default: FALSE]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.issue.count = function(list.of.networks, project.data,
name = "issue.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L, issue.type = c("all", "pull.requests", "issues"),
use.unfiltered.data = FALSE) {
issue.type = match.arg(issue.type)
if (missing(name) && identical(issue.type, "pull.requests")) {
name = "pull.request.count"
}
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level, default.value,
function(data) {
return(get.author.issue.count(data, type = issue.type,use.unfiltered.data = use.unfiltered.data))
},
"author.name"
)
return(nets.with.attr)
}
#' Add issue-count attribute based on the number of issues participated in by commenting,
#' where the person represented by the vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "issues.commented.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#' @param issue.type The issue kind,see \code{preprocess.issue.data} [default: "all"]
#' @param use.unfiltered.data whether to use unfiltered issue data (see \code{preprocess.issue.data}) [default: FALSE]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.issues.commented.count = function(list.of.networks, project.data,
name = "issues.commented.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L, issue.type = c("all", "pull.requests",
"issues"),
use.unfiltered.data = FALSE) {
issue.type = match.arg(issue.type)
if (missing(name) && identical(issue.type, "pull.requests")) {
name = "pull.requests.commented.count"
}
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, function(data) {
return(get.author.issues.commented.in.count(data, type = issue.type,
use.unfiltered.data = use.unfiltered.data))
},
"author.name"
)
return(nets.with.attr)
}
#' Add issue-count attribute based on the number of issues created,
#' where the person represented by the vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "issue.creation.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#' @param issue.type The issue kind,see \code{preprocess.issue.data} [default: "all"]
#' @param use.unfiltered.data whether to use unfiltered issue data (see \code{preprocess.issue.data}).
#' Note that filtered data may not contain issue creation events.
#' [default: TRUE]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.issue.creation.count = function(list.of.networks, project.data,
name = "issue.creation.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L, issue.type = c("all", "pull.requests",
"issues"),
use.unfiltered.data = TRUE) {
issue.type = match.arg(issue.type)
if (missing(name) && identical(issue.type, "pull.requests")) {
name = "pull.request.creation.count"
}
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, function(data) {
return(get.author.issues.created.count(data, type = issue.type, use.unfiltered.data = use.unfiltered.data))
}, "author.name"
)
return(nets.with.attr)
}
#' Add issue-comments-count attribute based on the number of comments in issues, where the person represented by the
#' vertex is the author.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "issue.comment.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#' @param issue.type The issue kind,see \code{preprocess.issue.data} [default: "all"]
#' @param use.unfiltered.data whether to use unfiltered issue data (see \code{preprocess.issue.data}) [default: FALSE]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.issue.comment.count = function(list.of.networks, project.data,
name = "issue.comment.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative",
"project.all.ranges",
"complete"),
default.value = 0L, issue.type = c("all", "pull.requests",
"issues"),
use.unfiltered.data = FALSE) {
issue.type = match.arg(issue.type)
if (missing(name) && identical(issue.type, "pull.requests")) {
name = "pull.request.comment.count"
}
nets.with.attr = add.vertex.attribute.count.helper(
list.of.networks, project.data, name, aggregation.level,
default.value, function(data) {
return(get.author.issue.comment.count(data, type = issue.type, use.unfiltered.data = use.unfiltered.data))
}, "author.name"
)
return(nets.with.attr)
}
## * Meta-data -------------------------------------------------------------
#' Add author email attribute
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "author.email"]
#' @param default.value The default value to add if a vertex has no matching value [default: NA]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.email = function(list.of.networks, project.data, name = "author.email",
default.value = NA) {
nets.with.attr = split.and.add.vertex.attribute(
list.of.networks, project.data, name, "complete", default.value,
function(range, range.data, net) {
authors = range.data$get.authors()
author.to.mail = structure(names = authors[["author.name"]],
authors[["author.email"]])
return(author.to.mail)
}
)
return(nets.with.attr)
}
## * Activity --------------------------------------------------------------
#' Add first activity attribute.
#'
#' @param list.of.networks The network list.
#' @param project.data The project data.
#' @param activity.types The kinds of activity to use as basis: One or more of \code{mails}, \code{commits} and
#' \code{issues}. [default: c("mails", "commits", "issues")]
#' @param name The attribute name to add. [default: "first.activity"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "complete"]
#' @param default.value The default value to add if a vertex has no matching value. [default: NA].
#' @param combine.all.activity.types Flag indicating that one value, computed over all given
#' \code{activity.types} is of interest (instead of one value per type).
#' [default: FALSE]
#'
#' @return A list of networks with the added attribute.
add.vertex.attribute.author.first.activity = function(list.of.networks, project.data,
activity.types = c("mails", "commits", "issues"),
name = "first.activity",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = NA,
combine.activity.types = FALSE) {
aggregation.level = match.arg.or.default(aggregation.level, default = "complete")
parsed.activity.types = match.arg.or.default(activity.types, several.ok = TRUE)
## make sure that the default value contains a tzone attribute (even if the default value is NA)
default.value = get.date.from.string(default.value)
## the given default.value is interpreted as default per author and type.
type.default = default.value
## the default value appended to vertices where no data is available is structured
## and named analogously to the vertex attributes containing available data.
vertex.default = default.value
if (!combine.activity.types) {
vertex.default = rep(list(vertex.default), length(parsed.activity.types))
names(vertex.default) = parsed.activity.types
}
compute.attr = function(range, range.data, net) {
data = get.first.activity.data(range.data, parsed.activity.types, type.default)
## If configured, find minimum over all activity types per author, for example:
## data
## list(authorA = list(mails = 1, commits = 2), authorB = list(mails = 3, commits = 3))
## yields
## list(authorA = list(all.activities = 1), authorB = list(all.activities = 3))
if (combine.activity.types) {
data = parallel::mclapply(data, function(item.list) {
min.value = min(do.call(base::c, item.list), na.rm = TRUE)
return(list(all.activities = min.value))
})
}
return(data)
}
nets.with.attr = split.and.add.vertex.attribute(list.of.networks, project.data, name, aggregation.level,
vertex.default, compute.attr, list.attributes = TRUE)
return(nets.with.attr)
}
#' Add active-ranges attribute
#'
#' Notice: One vertex can be active in multiple ranges, therefore there may be a vector of ranges as
#' active-ranges attribute.
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "active.ranges"]
#' @param activity.types The kinds of activity to use as basis: One or more of \code{mails}, \code{commits} and
#' \code{issues}. [default: c("mails", "commits", "issues")]
#' @param default.value The default value to add if a vertex has no matching value [default: list()]
#' @param combine.activity.types Flag indicating that one value, computed over all given
#' \code{activity.types} is of interest (instead of one value per type).
#' [default: FALSE]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.active.ranges = function(list.of.networks, project.data, name = "active.ranges",
activity.types = c("mails", "commits", "issues"),
default.value = list(),
combine.activity.types = FALSE) {
net.to.range.list = split.data.by.networks(list.of.networks, project.data, "range")
parsed.activity.types = match.arg.or.default(activity.types, several.ok = TRUE)
## the given default.value is interpreted as default per author and type.
type.default = default.value
compute.attr = function(range, range.data, net) {
data = get.active.ranges.data(parsed.activity.types, net.to.range.list, type.default)
## combine the active ranges of all attributes to one list if desired
if (combine.activity.types) {
data = lapply(data, function(person) {
flattened.person = (list("all.activity.types" = as.list(unique(unlist(person)))))
return(list(flattened.person))
})
}
return(data)
}
## the default value appended to vertices where no data is available is structured
## and named analogously to the vertex attributes containing available data.
vertex.default = default.value
if (combine.activity.types) {
vertex.default = list(default.value)
names(vertex.default) = c("all.activity.types")
} else {
vertex.default = rep(list(default.value), length(parsed.activity.types))
names(vertex.default) = parsed.activity.types
}
vertex.default = list(vertex.default)
nets.with.attr = add.vertex.attribute(net.to.range.list, name, vertex.default, compute.attr)
return(nets.with.attr)
}
## * Role ------------------------------------------------------------------
#' Add author role attribute, while using the classification method
#' \code{get.author.class.by.type} to provide the attributes
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "author.role"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param type The type of author classification. One of \code{"network.degree"},
#' \code{"network.eigen"}, \code{"commit.count"}, and \code{"loc.count"}.
#' @param default.value The default value to add if a vertex has no matching value [default: NA]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.role.simple = function(list.of.networks, project.data, name = "author.role",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
type = c("network.degree", "network.eigen",
"commit.count", "loc.count"),
default.value = NA) {
classification.function = function(network, range.data) {
classification = get.author.class.by.type(network, range.data, type)
return(classification)
}
nets.with.attr = add.vertex.attribute.author.role.function(
list.of.networks, project.data, classification.function, name,
aggregation.level, default.value
)
return(nets.with.attr)
}
#' Add author role attribute using a specified classification function
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param classification.result A name of an author-classification function. Must return a tuple
#' of two lists containing the authors named "core" and "peripheral".
#' See the functions \code{get.author.class.*}.
#' @param name The attribute name to add [default: "author.role"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: NA]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.role.function = function(list.of.networks, project.data, classification.function,
name = "author.role",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = NA) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
net.to.range.list = split.data.by.networks(list.of.networks, project.data, aggregation.level)
classification.results = lapply(
net.to.range.list,
function(net.to.range) {
author.class = classification.function(net.to.range[["network"]], net.to.range[["data"]])
return(author.class)
}
)
nets.with.attr = add.vertex.attribute.author.role(
list.of.networks, classification.results, name, default.value
)
return(nets.with.attr)
}
#' Add author role attribute by classification results
#'
#' Important: The lists \code{list.of.networks} and \code{classification.results} needs to be of the same
#' length for this to work properly.
#'
#' @param list.of.networks The network list
#' @param classification.results A list of author-classification results. Each item needs to contain
#' a tuple of two lists containing the authors named "core" and "peripheral"
#' (see the functions \code{get.author.class.*}).
#' The list needs to be of the same length as \code{list.of.networks} and use
#' the same names.
#' @param name The attribute name to add [default: "author.role"]
#' @param default.value The default value to add if a vertex has no matching value [default: NA]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.author.role = function(list.of.networks, classification.results,
name = "author.role", default.value = NA) {
if (length(list.of.networks) != length(classification.results) ||
!identical(names(list.of.networks), names(classification.results))) {
logging::logwarn(paste("Adding author-classification vertex attribute: The classification",
"results do not match with the list of networks. Please see the",
"documentation of the function 'add.vertex.attribute.author.role'."))
}
## provide a list whose elements only contain the network as no data is needed here
net.list = lapply(list.of.networks, function(net) {
n = list()
n[["network"]] = net
return(n)
})
nets.with.attr = add.vertex.attribute(
net.list, name, default.value,
function(range, range.data, net) {
classification = classification.results[[range]]
author.class = plyr::ldply(classification, .id = NA)
author.to.role = structure(author.class[[".id"]], names = author.class[["author.name"]])
return(author.to.role)
}
)
return(nets.with.attr)
}
## / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
## Artifact network functions ----------------------------------------------
## * Commit-based metrics --------------------------------------------------
#' Add the count of unique editors (i.e., authors) that worked on an artifact
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "editor.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param editor.definition Determines, who is counted as editor of an artifact (one ore more of
#' \code{c("author", "committer")}). [default: "author"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.artifact.editor.count = function(list.of.networks, project.data, name = "editor.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
editor.definition = c("author", "committer"),
default.value = 0L) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
## match editor definitions to column name in commit dataframe
if (missing(editor.definition)) {
editor.definition = "author"
} else {
editor.definition = match.arg.or.default(editor.definition, choices = c("author", "committer"),
several.ok = TRUE)
}
editor.definition = paste0(editor.definition, ".name")
nets.with.attr = split.and.add.vertex.attribute(
list.of.networks, project.data, name, aggregation.level, default.value,
function(range, range.data, net) {
vertex.attributes = lapply(range.data$group.authors.by.data.column("commits", "artifact"),
function(artifact.commits) {
editor.count = length(unique(unlist(artifact.commits[editor.definition])))
return(editor.count)
}
)
return(vertex.attributes)
}
)
return(nets.with.attr)
}
#' Add the amount of times the artifact was changed
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "change.count"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "range"]
#' @param default.value The default value to add if a vertex has no matching value [default: 0L]
#'
#' @return A list of networks with the added attribute
add.vertex.attribute.artifact.change.count = function(list.of.networks, project.data, name = "change.count",
aggregation.level = c("range", "cumulative", "all.ranges",
"project.cumulative", "project.all.ranges",
"complete"),
default.value = 0L) {
aggregation.level = match.arg.or.default(aggregation.level, default = "range")
nets.with.attr = split.and.add.vertex.attribute(
list.of.networks, project.data, name, aggregation.level, default.value,
function(range, range.data, net) {
artifact.to.commit = get.key.to.value.from.df(range.data$get.commits(), "artifact", "hash")
artifact.change.count = lapply(artifact.to.commit, function(x) {
length(unique(x[["hash"]]))
})
return(artifact.change.count)
}
)
return(nets.with.attr)
}
#' Add the first occurrence of the artifact
#'
#' @param list.of.networks The network list
#' @param project.data The project data
#' @param name The attribute name to add [default: "first.occurrence"]
#' @param aggregation.level Determines the data to use for the attribute calculation.
#' One of \code{"range"}, \code{"cumulative"}, \code{"all.ranges"},
#' \code{"project.cumulative"}, \code{"project.all.ranges"}, and
#' \code{"complete"}. See \code{split.data.by.networks} for
#' more details. [default: "complete"]
#' @param default.value The default value to add if a vertex has no matching value [default: NA]
#'
#' @return A list of networks with the added attribute