Skip to content

Commit 9b55563

Browse files
committed
Add community metrics with Modularity&Conductance
1 parent 703a85b commit 9b55563

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Community Metrics
2+
3+
CALL gds.conductance.stream(
4+
$dependencies_projection + '-cleaned', {
5+
relationshipWeightProperty: $dependencies_projection_weight_property
6+
,communityProperty: $dependencies_projection_write_property
7+
})
8+
YIELD community AS communityId, conductance
9+
WITH collect({communityId: communityId, conductance: conductance}) AS conductances
10+
CALL gds.modularity.stream(
11+
$dependencies_projection + '-cleaned', {
12+
relationshipWeightProperty: $dependencies_projection_weight_property
13+
,communityProperty: $dependencies_projection_write_property
14+
})
15+
YIELD communityId, modularity
16+
WITH conductances
17+
,collect({communityId: communityId, modularity: modularity}) AS modularities
18+
MATCH (member)
19+
WHERE member[$dependencies_projection_write_property] IS NOT NULL
20+
AND $dependencies_projection_node IN LABELS(member)
21+
WITH conductances
22+
,modularities
23+
,member[$dependencies_projection_write_property] AS communityId
24+
,coalesce(member.fqn, member.fileName, member.name) AS memberName
25+
,coalesce(member.name, replace(last(split(member.fileName, '/')), '.jar', '')) AS shortMemberName
26+
WITH conductances
27+
,modularities
28+
,communityId
29+
,count(DISTINCT memberName) AS memberCount
30+
,collect(DISTINCT shortMemberName) AS shortMemberNames
31+
,collect(DISTINCT memberName) AS memberNames
32+
,reduce(memberConductance = 0, conductance IN conductances |
33+
CASE conductance.communityId WHEN communityId THEN conductance.conductance
34+
ELSE memberConductance END) AS conductance
35+
,reduce(memberModularity = 0, modularity IN modularities |
36+
CASE modularity.communityId WHEN communityId THEN modularity.modularity
37+
ELSE memberModularity END) AS modularity
38+
RETURN communityId
39+
,conductance
40+
,modularity
41+
,memberCount
42+
,shortMemberNames[0..9] AS someMemberNamesShort
43+
,memberNames[0..9] AS someMemberNames
44+
ORDER BY communityId ASCENDING

scripts/reports/CommunityCsv.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ detectCommunitiesWithLouvain() {
114114
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
115115
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
116116

117-
queryCommunitiyModularities "${@}" "${writePropertyName}"
117+
calculateCommunityMetrics "${@}" "${writePropertyName}"
118118
}
119119

120120
# Community Detection using the Leiden Algorithm
@@ -160,7 +160,7 @@ detectCommunitiesWithLeiden() {
160160
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
161161
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
162162

163-
queryCommunitiyModularities "${@}" "${writePropertyName}"
163+
calculateCommunityMetrics "${@}" "${writePropertyName}"
164164
}
165165

166166
# Community Detection using the Weakly Connected Components Algorithm
@@ -196,6 +196,8 @@ detectCommunitiesWithWeaklyConnectedComponents() {
196196
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_9_Write_Mutated.cypher" "${@}" "${writePropertyName}"
197197
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
198198
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
199+
200+
calculateCommunityMetrics "${@}" "${writePropertyName}"
199201
}
200202

201203
# Community Detection using the Label Propagation Algorithm
@@ -231,6 +233,8 @@ detectCommunitiesWithLabelPropagation() {
231233
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_9_Write_Mutated.cypher" "${@}" "${writePropertyName}"
232234
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
233235
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
236+
237+
calculateCommunityMetrics "${@}" "${writePropertyName}"
234238
}
235239

236240
# Community Detection using the K-Core Decomposition Algorithm
@@ -265,6 +269,8 @@ detectCommunitiesWithKCoreDecomposition() {
265269
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_9_Write_Mutated.cypher" "${@}" "${writePropertyName}"
266270
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
267271
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
272+
273+
calculateCommunityMetrics "${@}" "${writePropertyName}"
268274
}
269275

270276
# Community Detection using the Approximate Maximum k-cut Algorithm
@@ -301,9 +307,11 @@ detectCommunitiesWithApproximateMaximumKCut() {
301307
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_9_Write_Mutated.cypher" "${@}" "${writePropertyName}"
302308
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_10_Delete_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
303309
execute_cypher "${PROJECTION_CYPHER_DIR}/Dependencies_11_Add_Label.cypher" "${@}" "${writePropertyName}" "${writeLabelName}"
310+
311+
calculateCommunityMetrics "${@}" "${writePropertyName}"
304312
}
305313

306-
# Query modularity for Leiden communities
314+
# Calculates community metrics including "Modularity" and "Conductance"
307315
#
308316
# Required Parameters:
309317
# - dependencies_projection=...
@@ -312,7 +320,7 @@ detectCommunitiesWithApproximateMaximumKCut() {
312320
# Name of the property that contains the communitiy id
313321
# - dependencies_projection_weight_property=...
314322
# Name of the node property that contains the dependency weight. Example: "weight"
315-
queryCommunitiyModularities() {
323+
calculateCommunityMetrics() {
316324
local COMMUNITY_DETECTION_CYPHER_DIR="${CYPHER_DIR}/Community_Detection"
317325

318326
local nodeLabel
@@ -322,7 +330,7 @@ queryCommunitiyModularities() {
322330
propertyName=$( extractQueryParameter "dependencies_projection_write_property" "${@}")
323331

324332
# Stream to CSV
325-
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_7d_Modularity_Members.cypher" "${@}" > "${FULL_REPORT_DIRECTORY}/${nodeLabel}_${propertyName}_Communities_Modularities.csv"
333+
execute_cypher "${COMMUNITY_DETECTION_CYPHER_DIR}/Community_Detection_9_Community_Metrics.cypher" "${@}" > "${FULL_REPORT_DIRECTORY}/${nodeLabel}_${propertyName}_Community_Metrics.csv"
326334
}
327335

328336
# Write modularity for Leiden communities

0 commit comments

Comments
 (0)