-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupverify.go
712 lines (674 loc) · 23.4 KB
/
groupverify.go
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
package main
import (
"fmt"
"math"
"os"
"sort"
)
const (
ReportFormatMarkdown int = 1
ReportFormatHtml int = 2
)
const markdownStart string = "# Cliquetool Group Verification Results\n\n"
const htmlStart string = "<html><body><h1>Cliquetool Group Verification Results</h1>\n"
const DegreeCutoff float64 = 50.0 // 50%
const GroupOverlapPercentError float64 = 80.0
type GroupCount struct {
group *Group
cnt int
}
type GroupReportErrorCount struct {
reportText string
errorSize int
groupSize int
}
func linkCountForNode(db *NodeDb, id int) int {
if id < len(db.nodes) {
return len(db.nodes[id].links)
} else {
return 0
}
}
// Function callback for sort.Slie on a group error report:
// sort from highest to lowest
func cmpGroupReportErrors(a GroupReportErrorCount, b GroupReportErrorCount) bool {
if a.errorSize < b.errorSize {
return true
} else if a.errorSize > b.errorSize {
return false
}
return true
}
// Function callback for sort.Slize on a group error report:
// sort from highest to lowest
func cmpGroupReportSizes(a GroupReportErrorCount, b GroupReportErrorCount) bool {
if a.groupSize < b.groupSize {
return false
} else if a.groupSize > b.groupSize {
return true
}
return false
}
// function callback for sort.Slize sort from highest to lowest
func cmpGroupCounts(a *GroupCount, b *GroupCount) bool {
perA := float64(a.cnt) / float64(len(a.group.ids))
perB := float64(b.cnt) / float64(len(b.group.ids))
if perA < perB {
return true
} else if perA > perB {
return false
} else {
// secondary sort is number of links
if a.cnt < b.cnt {
return true
} else if a.cnt > b.cnt {
return false
}
}
return false
}
// Add the link into the existing NodeCount array.
func addGroupToGroupCount(groupCount []GroupCount, group *Group, grpCount int) []GroupCount {
found := false
cnt := len(groupCount)
ret := groupCount
/* search for group in existing array */
for j := 0; j < cnt && !found; j++ {
if ret[j].group.groupName == group.groupName {
ret[j].cnt += grpCount
found = true
}
}
if !found {
gc := GroupCount{group: group, cnt: grpCount}
ret = append(ret, gc)
}
return ret
}
// Verify the groups found for a set of nodes and generate a report in either
// markdown or html.
func verifyGroupDb(nodeDb *NodeDb, groupDb *GroupDb, reportOutputPath string, reportFormat int, reportSortBySize bool) error {
var f *os.File
if len(reportOutputPath) == 0 {
f = os.Stdout
} else {
var err error
f, err = os.Create(reportOutputPath)
if err != nil {
return err
}
defer f.Close()
}
if reportFormat == ReportFormatMarkdown {
f.WriteString(markdownStart)
} else {
f.WriteString(htmlStart)
}
/* update node group counts */
updateNodeGroupCounts(nodeDb, groupDb)
writeGroupSummaryReport(nodeDb, groupDb, f, reportFormat)
reports := make([]GroupReportErrorCount, 0)
for i := 0; i < len(groupDb.groups); i++ {
ret := verifyGroupStruct(nodeDb, groupDb, &groupDb.groups[i], f, reportFormat)
//if ret.errorSize > 0 {
/* add to our array */
reports = append(reports, *ret)
//}
}
if reportSortBySize {
// sort results, showing largest groups first
sort.Slice(reports[:], func(i, j int) bool {
return cmpGroupReportSizes(reports[i], reports[j])
})
} else {
// sort results, showing questionable groups first
sort.Slice(reports[:], func(i, j int) bool {
return cmpGroupReportErrors(reports[i], reports[j])
})
}
/* now write to file the sorted group reports */
for i := 0; i < len(reports); i++ {
f.WriteString(reports[i].reportText)
}
if reportFormat == ReportFormatMarkdown {
f.WriteString(fmt.Sprintf("\n\nGenerated by cliquetool v%s\n", CliqueToolVersion))
} else {
f.WriteString(fmt.Sprintf("\n<hr/><p>Generated by cliquetool v%s\n", CliqueToolVersion))
}
return nil
}
// Write a report of group size count
func writeGroupSummaryReport(nodeDb *NodeDb, groupDb *GroupDb, fp *os.File, reportFormat int) {
/* update group stats (if needed) */
maxSize := 0
for i := 0; i < len(groupDb.groups); i++ {
updateGroupStats(nodeDb, &groupDb.groups[i])
maxSize = maxOfTwoInts(maxSize, len(groupDb.groups[i].ids))
}
cnt := make([]int, maxSize+1)
maxCnt := 0
for i := 0; i < len(groupDb.groups); i++ {
l := len(groupDb.groups[i].ids)
cnt[l]++
maxCnt = maxOfTwoInts(maxCnt, l)
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "## Group Size Summary\n")
fmt.Fprintf(fp, "| %15s | %15s | %15s |\n", "Group Size", "Count (=)", "Count (>=)")
fmt.Fprintf(fp, "| %15s | %15s | %15s |\n", "---", "---:", "---:")
} else {
fmt.Fprintf(fp, "<table><thead><tr><th colspan\"4\">Group Size Summary</th></tr>\n")
fmt.Fprintf(fp, "<tr><th>%s</th><th>%s</th><th>%s</th></tr></thead>\n",
"Group Size", "Count(=)", "Count(>=)")
}
sum := 0
for i := maxSize; i >= 3; i-- {
if cnt[i] > 0 {
sum += cnt[i]
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "| %15d | %15d | %15d |\n", i, cnt[i], sum)
} else {
fmt.Fprintf(fp, "<tr><td>%10d</td><td>%10d</td><td>%10d</td></tr>\n", i, cnt[i], sum)
}
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "\n")
} else {
fmt.Fprintf(fp, "</table>\n<hr>\n")
}
// How many valid nodes (some Id numbers may have been skipped)
totalNumNodes := 0
for i := 0; i < len(nodeDb.nodes); i++ {
if len(nodeDb.nodes[i].links) > 0 {
totalNumNodes++
}
}
// Generate a table that shows a density distribution. For example,
// how many groups in the 0 - 10% range and so on.
//
densities := make([]int, 11)
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "## Group Density Summary\n")
fmt.Fprintf(fp, "| %15s | %15s | %15s |\n", "Group Density", "Count (=)", "Count (>=)")
fmt.Fprintf(fp, "| %15s | %15s | %15s |\n", "---", "---:", "---:")
} else {
fmt.Fprintf(fp, "<table><thead><tr><th colspan\"3\">Group Density Summary</th></tr>\n")
fmt.Fprintf(fp, "<tr><th>%s</th><th>%s</th><th>%s</th></tr></thead>\n",
"Group Density", "Count(=)", "Count(>=)")
}
// count up the number for each bucket.
// 0 = (0.00 - 0.099), 1 = (0.10-0.199)
maxCnt = 0
for i := 0; i < len(groupDb.groups); i++ {
binNum := int(math.Floor(groupDb.groups[i].density * 10.0))
if binNum >= 0 && binNum <= 10 {
densities[binNum]++
if densities[binNum] > maxCnt {
maxCnt = densities[binNum]
}
}
}
sum = 0
for i := 10; i >= 0; i-- {
sum += densities[i]
var temp string
if i < 10 {
temp = fmt.Sprintf("%d0.0-%d9.9%%", i, i)
} else {
temp = "100%"
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "| %15s | %15d | %15d |\n", temp, densities[i], sum)
} else {
fmt.Fprintf(fp, "<tr><td>%s</td><td>%d</td><td>%d</td></tr>\n",
temp, densities[i], sum)
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "\n")
} else {
fmt.Fprintf(fp, "</table>\n<hr/>\n")
}
// Now generate a table that shows how many groups each node is in.
// (80% are in 1 group, 10% are in 2 groups, etc.)
maxGroupCount := 0
for i := 0; i < len(nodeDb.nodes); i++ {
if nodeDb.nodes[i].numGroups >= maxGroupCount {
maxGroupCount = nodeDb.nodes[i].numGroups
}
}
groupSizeCount := make([]int, maxGroupCount+1)
for i := 0; i < len(nodeDb.nodes); i++ {
if len(nodeDb.nodes[i].links) > 0 {
numGroupsForNode := nodeDb.nodes[i].numGroups
groupSizeCount[numGroupsForNode]++
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "## Node Group Membership Counts\n")
fmt.Fprintf(fp, "| %15s | %15s |\n", "No. Groups", "No. Nodes")
fmt.Fprintf(fp, "| %15s | %15s |\n", "---:", "---:")
temp := fmt.Sprintf(" %d (%.2f%%)",
(totalNumNodes - groupSizeCount[0]),
100.0*(float64(totalNumNodes)-
float64(groupSizeCount[0]))/float64(totalNumNodes))
fmt.Fprintf(fp, "| %15s | %15s |\n", "1+ Groups", temp)
temp = fmt.Sprintf("%d (%.2f%%)",
groupSizeCount[0],
100.0*float64(groupSizeCount[0])/float64(totalNumNodes))
fmt.Fprintf(fp, "| %15s | %15s |\n", "No Groups", temp)
} else {
fmt.Fprintf(fp, "<table><thead><tr><th colspan\"2\">Node Group Membership Counts</th></tr>\n")
fmt.Fprintf(fp, "<tr><th>%s</th><th>%s</th></tr></thead>\n",
"No. Groups", "No. Nodes")
fmt.Fprintf(fp, "<tr class=\"totals\"><td>1 or More Groups</td><td>%d (%.2f%%)</td></tr>\n",
(totalNumNodes - groupSizeCount[0]),
100.0*(float64(totalNumNodes)-
float64(groupSizeCount[0]))/float64(totalNumNodes))
fmt.Fprintf(fp, "<tr class=\"totals\"><td>No Groups</td><td>%d (%.2f%%)</td></tr>\n",
groupSizeCount[0],
100.0*float64(groupSizeCount[0])/float64(totalNumNodes))
}
for i := 1; i < maxGroupCount || i < 3; i++ {
if i > maxGroupCount || i > len(groupSizeCount)-1 {
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "| %15d | %15s |\n", i, "0 (0.00%)")
} else {
fmt.Fprintf(fp, "<tr><td>%d</td><td>%d (%.2f%%)</td></tr>\n",
i, 0, 0.0)
}
} else if groupSizeCount[i] > 0 {
if reportFormat == ReportFormatMarkdown {
temp := fmt.Sprintf("%d (%.2f%%)",
groupSizeCount[i],
100.0*float64(groupSizeCount[i])/float64(totalNumNodes))
fmt.Fprintf(fp, "| %15d | %15s |\n", i, temp)
} else {
fmt.Fprintf(fp, "<tr><td>%d</td><td>%d (%.2f%%)</td></tr>\n",
i, groupSizeCount[i], 100.0*float64(groupSizeCount[i])/
float64(totalNumNodes))
}
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "\n")
} else {
fmt.Fprintf(fp, "</table>\n\n")
}
// Print a table of the nodes with the most links that did not
// end up in any group.
nodeCount := make([]NodeCount, 0)
nNodes := 0
for i := 0; i < len(nodeDb.nodes); i++ {
if len(nodeDb.nodes[i].links) >= 4 && nodeDb.nodes[i].numGroups == 0 {
nc := NodeCount{id: nodeDb.nodes[i].id, cnt: len(nodeDb.nodes[i].links)}
nodeCount = append(nodeCount, nc)
nNodes++
}
}
sort.Slice(nodeCount[:], func(i, j int) bool {
return cmpNodeCountLinkCounts(nodeCount[i], nodeCount[j])
})
/* show the top 40 or all with a node count of 4 or more... */
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "## Ungrouped Nodes\n")
if nNodes == 0 {
fmt.Fprintf(fp, "None\n")
} else {
fmt.Fprintf(fp, "| %30s | %15s |\n", "NodeId", "No. Links")
fmt.Fprintf(fp, "| %30s | %15s |\n", "---:", "---:")
}
} else {
if nNodes == 0 {
fmt.Fprintf(fp, "None\n")
} else {
fmt.Fprintf(fp, "<table class=\"ungroupednodes\"><thead><tr><th colspan\"2\">Ungrouped Nodes</th></tr>\n")
fmt.Fprintf(fp, "<tr><th>%s</th><th>%s</th></tr></thead>\n",
"NodeId", "No. Links")
}
}
if nNodes > 0 {
for i := 0; i < nNodes && i < 40; i++ {
//don't show nodes with less than 4 links
if nodeCount[i].cnt < 4 && i > 20 {
break
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "| %30s | %15d |\n",
nodeNameAbbr(nodeDb.nodes[nodeCount[i].id], 30),
nodeCount[i].cnt)
} else {
fmt.Fprintf(fp, "<tr><td>%s</td><td>%d</td></tr>\n",
nodeDb.nodes[nodeCount[i].id].externalName, nodeCount[i].cnt)
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "\n")
} else {
fmt.Fprintf(fp, "</table>\n\n")
}
}
// Print up a table that shows a one-line summary of each group
// that includes: name, size, group links, all links for nodes in group,
// density.
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "## Group Statistics\n")
fmt.Fprintf(fp, "| %25s | %8s | %12s | %12s | %9s |\n", "Name", "Size", "Group Links",
"Total Links", "Density")
fmt.Fprintf(fp, "| %25s | %8s | %12s | %12s | %9s |\n", "---", "---:", "---:", "---:", "---:")
} else {
fmt.Fprintf(fp, "<table><thead><tr><th colspan\"5\">Group Statistics</th></tr>\n")
fmt.Fprintf(fp, "<tr><th>%s</th><th>%s</th><th>%s</th></tr></thead>",
"Name", "Size", "Group Links")
}
for i := 0; i < len(groupDb.groups); i++ {
group := &groupDb.groups[i]
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "| %25s | %8d | %12d | %12d | %9.2f |\n", group.groupName, len(group.ids),
group.groupLinks,
group.totalNodeLinks, 100.0*group.density)
} else {
fmt.Fprintf(fp, "<tr><td>%s</td><td>%d</td><td>%d</td><td>%d</td><td>%0.2f%%</td></tr>\n",
group.groupName, len(group.ids),
group.groupLinks, group.totalNodeLinks, 100.0*group.density)
}
}
if reportFormat == ReportFormatMarkdown {
fmt.Fprintf(fp, "\n")
} else {
fmt.Fprintf(fp, "</table>\n\n")
}
}
// Run some verification tools and gather some statistics about a group.
// If the groupDb parameter is non-nil, then we will also check
// to see if the group is related in any way to other groups
// (subset, superset, overlap, etc.)
func verifyGroupStruct(db *NodeDb, groupDb *GroupDb, group *Group,
fp *os.File, reportFormat int) *GroupReportErrorCount {
totalLinkCnt := 0
minLinksInGroup := -1 // min inner-group links for any node
maxMissing := 10
externalAve := 0.0
var ret GroupReportErrorCount
ret.groupSize = len(group.ids)
if reportFormat == ReportFormatMarkdown {
ret.reportText += fmt.Sprintf("## Summary For Group: %s\n", group.groupName)
ret.reportText += fmt.Sprintf("| %25s | %14s | %16s | %10s |\n", "NodeID", "Group Links",
"Grp/Total Lnks", "No. Groups")
ret.reportText += fmt.Sprintf("| %25s | %14s | %16s | %10s |\n", "---", "---:", "---:", "---:")
} else {
ret.reportText += fmt.Sprintf("<table><thead><tr><th colspan\"6\">Group %s</th></tr>\n", group.groupName)
ret.reportText += fmt.Sprintf("<tr><th>%s</th><th colspan=\"2\">%s</th><th colspan=\"2\">%s</th><th>%s</th></tr></thead>",
"NodeID", "Group Links", "Grp/Total Lnks", "No. Groups")
}
for i := 0; i < len(group.ids); i++ {
// count how many in this group this node links to
linkCnt := 0
allNodeLinkCnt := linkCountForNode(db, group.ids[i])
for j := 0; j < len(group.ids); j++ {
if i != j {
if nodeLinksTo(*db, group.ids[i], group.ids[j]) {
linkCnt++
}
}
}
if linkCnt < minLinksInGroup || minLinksInGroup < 0 {
minLinksInGroup = linkCnt
}
totalLinkCnt += linkCnt
temp := fmt.Sprintf("%d/%d %6.2f%%", linkCnt, len(group.ids)-1,
100.0*float64(linkCnt)/float64(len(group.ids)-1))
temp2 := fmt.Sprintf("%d/%d %6.2f%%", linkCnt, allNodeLinkCnt,
100.0*float64(linkCnt)/float64(allNodeLinkCnt))
if reportFormat == ReportFormatMarkdown {
line := fmt.Sprintf("| %25s | %14s | %16s | %10d\n",
nodeNameAbbr(db.nodes[group.ids[i]], 18), temp, temp2,
db.nodes[group.ids[i]].numGroups)
ret.reportText += line
} else {
line := fmt.Sprintf(
"<tr><td>%s</td>%s%s<td>%d</td></tr>\n",
db.nodes[group.ids[i]].externalName,
temp, temp2, db.nodes[group.ids[i]].numGroups)
ret.reportText += line
}
if linkCnt == 0 {
ret.errorSize = 100
}
externalAve += 100.0 *
float64(linkCnt) / float64(allNodeLinkCnt)
}
groupAve := 100.0 * float64(totalLinkCnt) /
float64((len(group.ids)-1)*len(group.ids))
var line string
if reportFormat == ReportFormatMarkdown {
temp := fmt.Sprintf("%d/%d %6.2f%%", totalLinkCnt,
(len(group.ids)-1)*len(group.ids), groupAve)
temp2 := fmt.Sprintf("AVE %6.2f%%", externalAve/float64(len(group.ids)))
line = fmt.Sprintf("| %25s | %14s | %16s | %10s | \n", "TOTALS", temp, temp2, "-")
} else {
temp := fmt.Sprintf("<td>%d/%d</td><td>%.2f%%</td>", totalLinkCnt,
(len(group.ids)-1)*len(group.ids), groupAve)
temp2 := fmt.Sprintf("<td>AVE</td><td>%.2f%%</td>",
externalAve/float64(len(group.ids)))
line = fmt.Sprintf("<tr class=\"totals\"><td>%s</td>%s%s<td>-</td></tr>\n",
"TOTALS", temp, temp2)
}
ret.reportText += line
/*
* Now, we want to find if there are any possible group members that
* might have been missed. We do this by counting up all the links
* that the group members have. Then, we sort the list to find
* out which non-group member is linked to the most.
* If any of these have more than the least-linked within the
* group, we mark it with either a '!' or '+'.
*/
nodeCount := make([]NodeCount, 0)
for i := 0; i < len(group.ids); i++ {
/* get all links for node id group.ids[i] */
nodeId := group.ids[i]
for j := 0; j < len(db.nodes[nodeId].links); j++ {
/* Make sure this link does not reference a current group node */
if !isInArray(group.ids, db.nodes[nodeId].links[j]) {
nodeCount = addLinkToNodeCount(db, nodeCount, db.nodes[nodeId].links[j])
}
}
}
if len(nodeCount) == 0 {
if reportFormat == ReportFormatMarkdown {
ret.reportText += "### No other possible group nodes found\n"
} else {
ret.reportText += "<tr><td colspan=\"6\">No other possible group nodes found</td></tr>\n"
}
} else {
// Sort the nodes by number of links to it in the group.
// Secondary sort is the number of total links (less is better).
sort.Slice(nodeCount[:], func(i, j int) bool {
return cmpNodeCountLinkCounts(nodeCount[i], nodeCount[j])
})
// print either top 5 or all that have more than min links
if reportFormat == ReportFormatMarkdown {
ret.reportText += "### Potential group nodes not included\n" +
fmt.Sprintf("| %25s | %14s | %16s | %10s |\n", "NodeID", "Group Links",
"Grp/Total Lnks", "No. Groups") +
fmt.Sprintf("| %25s | %14s | %16s | %10s |\n", "---", "---:", "---:", "---:")
} else {
ret.reportText += "<tr><td colspan=\"6\">Potential group nodes not included</td></tr>\n"
}
for i := 0; i < len(nodeCount); i++ {
ch := ' '
// count how many in this group this node links to
linkCnt := 0
allNodeLinkCnt := linkCountForNode(db, nodeCount[i].id)
for j := 0; j < len(group.ids); j++ {
if nodeLinksTo(*db, nodeCount[i].id, group.ids[j]) {
linkCnt++
}
}
degree := 100.0 * float64(linkCnt) / float64(len(group.ids))
//ieRatio := 100.0 * float64(linkCnt) / float64(allNodeLinkCnt)
// Show node and continue if any of the following:
// - degree is >= DEGREE_CUTOFF
// - this is one of the first 5 nodes in the list
if i >= 5 && degree < DegreeCutoff {
continue
}
// Only show 10 candidates if we are still showing nodes that SHOULD
// be considered for inclusion in the group */
if linkCnt >= minLinksInGroup && i >= maxMissing {
if reportFormat == ReportFormatMarkdown {
ret.reportText += "| more... |\n"
} else {
ret.reportText += "<tr><td colspan=\"6\" class=\"str\">[more...]</td></tr>\n"
}
break
}
// Use some special characters to make searching through the output
// for important stuff a little easier.
// Use '+' to denote that this entry is linked to more group nodes
// than the least-linked group member is.
// Use '!' to denote the above AND the ratio of links to the group
// to total group nodes is higher than the average for the whole
// group. (Seems like this shouldn't happen to me...)
if linkCnt >= minLinksInGroup && degree >= groupAve {
ret.errorSize = linkCnt - minLinksInGroup
ch = '!'
} else if linkCnt >= minLinksInGroup {
ch = '+'
} else if degree >= DegreeCutoff {
// TODO: replace DegreeCutoff with value from ParamTable
ch = '+'
}
if reportFormat == ReportFormatMarkdown {
temp := fmt.Sprintf("%d/%d %6.2f%%", linkCnt, len(group.ids), degree)
temp2 := fmt.Sprintf("%d/%d %6.2f%%", linkCnt, allNodeLinkCnt,
100.0*float64(linkCnt)/float64(allNodeLinkCnt))
ret.reportText +=
fmt.Sprintf("| %c%24s | %14s | %16s | %10d |\n", ch,
nodeNameAbbr(db.nodes[nodeCount[i].id], 17), temp, temp2,
db.nodes[nodeCount[i].id].numGroups)
} else {
temp := fmt.Sprintf("<td>%d/%d</td><td>%.2f%%</td>", linkCnt,
len(group.ids), degree)
temp2 := fmt.Sprintf("<td>%d/%d</td><td>%.2f%%</td>", linkCnt,
allNodeLinkCnt,
100.0*float64(linkCnt)/float64(allNodeLinkCnt))
ret.reportText +=
fmt.Sprintf("<tr><td>%s</td>%s%s<td>%d</td></tr>\n",
db.nodes[nodeCount[i].id].externalName, temp, temp2,
db.nodes[nodeCount[i].id].numGroups)
}
}
}
// See if this group is connected (subset, superset, overlap) to
// any groups specified in the GroupDb passed in.
// The GroupDb passed may or may not include this group, so we
// may find this exact group in the GroupDb.
if len(nodeCount) > 0 {
groupCount := make([]GroupCount, 0)
for i := 0; i < len(groupDb.groups) && groupDb.groups[i].groupName != group.groupName; i++ {
group2 := &groupDb.groups[i]
j := countSharedNodes(group, group2)
// Technically, j=1 would be one common member in two groups, but
// this generates too much info to look at.
if j >= 3 {
// If we are comparing groups within the same group db, don't
// compare a group against itself.
if group.groupName == group2.groupName {
groupCount =
addGroupToGroupCount(groupCount, group2, j)
}
}
}
if len(groupCount) > 0 {
numSupergroups := 0
numRelated := 0
// now sort groupCount to put groups with most overlap first
sort.Slice(groupCount[:], func(i, j int) bool {
return cmpGroupCounts(&groupCount[i], &groupCount[j])
})
for i := 0; i < len(groupCount); i++ {
foundSuspectSubset := false
if groupCount[i].cnt == len(group.ids) {
if numSupergroups == 0 {
if reportFormat == ReportFormatMarkdown {
ret.reportText += "### This group is a subset of:\n" +
fmt.Sprintf("| %25s | %14s |\n", "---:", "---") +
fmt.Sprintf("| %25s | %14s |\n", "Shared/Total", "Group")
} else {
ret.reportText += "<tr><th colspan=\"6\">This group is a subset of</th></tr>\n" +
"<tr><th>Shared/Total</th><th colspan=\"5\">Group</th></tr>\n" +
fmt.Sprintf("<tr><td>%18s</td><td>%s</td></tr>\n", "Shared/Total", "Group")
}
}
numSupergroups++
if !foundSuspectSubset {
ret.errorSize += 100
foundSuspectSubset = true
}
} else {
if !foundSuspectSubset &&
groupCount[i].cnt == len(groupCount[i].group.ids) {
ret.errorSize += 100
foundSuspectSubset = true
}
if numRelated == 0 {
if reportFormat == ReportFormatMarkdown {
ret.reportText += "### Related groups (3+ common nodes):\n" +
fmt.Sprintf("| %25s | %14s |\n", "---:", "---") +
fmt.Sprintf("| %25s | %14s |\n", "Shared/Total", "Group")
} else {
ret.reportText += "<tr><th colspan=\"6\">Related groups (3+ common nodes)</th></tr>\n" +
fmt.Sprintf("<tr><th>%s</th><th colspan=\"5\">%s</th></tr>\n",
"Shared/Total", "Group")
}
}
numRelated++
}
temp := fmt.Sprintf("%d/%d (%5.2f%%)", groupCount[i].cnt,
len(groupCount[i].group.ids),
100.0*float64(groupCount[i].cnt)/
float64(len(groupCount[i].group.ids)))
ch := ' '
if len(groupCount[i].group.ids) == groupCount[i].cnt {
ch = '='
}
if reportFormat == ReportFormatMarkdown {
ret.reportText += fmt.Sprintf("| %c%24s | %14s |\n", ch,
temp, groupCount[i].group.groupName)
} else {
ret.reportText += fmt.Sprintf("<tr>><td>%c%s</td><td colspan=\"5\" class=\"str\">%s</td></tr>\n",
ch, temp, groupCount[i].group.groupName)
}
if 100.0*float64(groupCount[i].cnt)/
float64(len(groupCount[i].group.ids)) >=
GroupOverlapPercentError {
/* found another group that is a subset of this one */
ret.errorSize++
}
if i == 9 && len(groupCount) >= 10 {
/* only display 10... */
if reportFormat == ReportFormatMarkdown {
ret.reportText += "[more...]\n"
} else {
ret.reportText += "<tr><td colspan=\"6\" class=\"str\">[more...]</td></tr>\n"
}
break
}
}
} else {
if reportFormat == ReportFormatMarkdown {
ret.reportText += "No related groups\n"
} else {
ret.reportText += "<tr><td colspan=\"6\" class=\"str\">No related groups</td></tr>\n"
}
}
}
if reportFormat == ReportFormatMarkdown {
ret.reportText += "\n"
} else {
ret.reportText += "</table><br/><br/>\n\n"
}
return &ret
}