Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Christnowx
Gephi
提交
fe24632e
G
Gephi
项目概览
Christnowx
/
Gephi
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gephi
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
fe24632e
编写于
4月 01, 2016
作者:
E
Eduardo Ramos
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix #1419 Getting null pointer error when trying to calculate modularity
Fix #713 Modularity Calculation Throws Exception On Empty Graph
上级
c2046112
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
103 addition
and
76 deletion
+103
-76
modules/StatisticsPlugin/src/main/java/org/gephi/statistics/plugin/Modularity.java
...src/main/java/org/gephi/statistics/plugin/Modularity.java
+88
-72
modules/StatisticsPlugin/src/test/java/org/gephi/statistics/plugin/ModularityNGTest.java
...st/java/org/gephi/statistics/plugin/ModularityNGTest.java
+15
-4
未找到文件。
modules/StatisticsPlugin/src/main/java/org/gephi/statistics/plugin/Modularity.java
浏览文件 @
fe24632e
...
...
@@ -133,26 +133,27 @@ public class Modularity implements Statistics, LongTask {
Graph
graph
;
double
[]
weights
;
double
graphWeightSum
;
Li
nkedLi
st
<
ModEdge
>[]
topology
;
Li
nkedLi
st
<
Community
>
communities
;
List
<
ModEdge
>[]
topology
;
List
<
Community
>
communities
;
int
N
;
HashMap
<
Integer
,
Community
>
invMap
;
CommunityStructure
(
Graph
h
graph
)
{
this
.
graph
=
h
graph
;
N
=
h
graph
.
getNodeCount
();
CommunityStructure
(
Graph
graph
)
{
this
.
graph
=
graph
;
N
=
graph
.
getNodeCount
();
invMap
=
new
HashMap
<>();
nodeConnectionsWeight
=
new
HashMap
[
N
];
nodeConnectionsCount
=
new
HashMap
[
N
];
nodeCommunities
=
new
Community
[
N
];
map
=
new
HashMap
<>();
topology
=
new
Linked
List
[
N
];
communities
=
new
Linked
List
<>();
topology
=
new
Array
List
[
N
];
communities
=
new
Array
List
<>();
int
index
=
0
;
weights
=
new
double
[
N
];
for
(
Node
node
:
h
graph
.
getNodes
())
{
for
(
Node
node
:
graph
.
getNodes
())
{
map
.
put
(
node
,
index
);
nodeCommunities
[
index
]
=
new
Community
(
this
);
nodeConnectionsWeight
[
index
]
=
new
HashMap
<>();
nodeConnectionsCount
[
index
]
=
new
HashMap
<>();
weights
[
index
]
=
0
;
...
...
@@ -167,34 +168,47 @@ public class Modularity implements Statistics, LongTask {
}
}
for
(
Node
node
:
h
graph
.
getNodes
())
{
for
(
Node
node
:
graph
.
getNodes
())
{
int
node_index
=
map
.
get
(
node
);
topology
[
node_index
]
=
new
LinkedList
<>();
for
(
Edge
edge
:
hgraph
.
getEdges
(
node
))
{
Node
neighbor
=
hgraph
.
getOpposite
(
node
,
edge
);
topology
[
node_index
]
=
new
ArrayList
<>();
Set
<
Node
>
uniqueNeighbors
=
new
HashSet
<>(
graph
.
getNeighbors
(
node
).
toCollection
());
for
(
Node
neighbor
:
uniqueNeighbors
)
{
if
(
node
==
neighbor
)
{
continue
;
}
int
neighbor_index
=
map
.
get
(
neighbor
);
float
weight
=
1
;
if
(
useWeight
)
{
weight
=
(
float
)
edge
.
getWeight
(
graph
.
getView
());
float
weight
=
0
;
//Sum all parallel edges weight:
for
(
Edge
edge
:
graph
.
getEdges
(
node
,
neighbor
))
{
if
(
useWeight
)
{
weight
+=
edge
.
getWeight
(
graph
.
getView
());
}
else
{
weight
+=
1
;
}
}
//Finally add a single edge with the summed weight of all parallel edges:
//Fixes issue #1419 Getting null pointer error when trying to calculate modularity
weights
[
node_index
]
+=
weight
;
Modularity
.
ModEdge
me
=
new
ModEdge
(
node_index
,
neighbor_index
,
weight
);
topology
[
node_index
].
add
(
me
);
Community
adjCom
=
nodeCommunities
[
neighbor_index
];
nodeConnectionsWeight
[
node_index
].
put
(
adjCom
,
weight
);
nodeConnectionsCount
[
node_index
].
put
(
adjCom
,
1
);
nodeCommunities
[
node_index
].
connectionsWeight
.
put
(
adjCom
,
weight
);
nodeCommunities
[
node_index
].
connectionsCount
.
put
(
adjCom
,
1
);
nodeConnectionsWeight
[
neighbor_index
].
put
(
nodeCommunities
[
node_index
],
weight
);
nodeConnectionsCount
[
neighbor_index
].
put
(
nodeCommunities
[
node_index
],
1
);
nodeCommunities
[
neighbor_index
].
connectionsWeight
.
put
(
nodeCommunities
[
node_index
],
weight
);
nodeCommunities
[
neighbor_index
].
connectionsCount
.
put
(
nodeCommunities
[
node_index
],
1
);
Community
nodeCom
=
nodeCommunities
[
node_index
];
nodeCom
.
connectionsWeight
.
put
(
adjCom
,
weight
);
nodeCom
.
connectionsCount
.
put
(
adjCom
,
1
);
nodeConnectionsWeight
[
neighbor_index
].
put
(
nodeCom
,
weight
);
nodeConnectionsCount
[
neighbor_index
].
put
(
nodeCom
,
1
);
adjCom
.
connectionsWeight
.
put
(
nodeCom
,
weight
);
adjCom
.
connectionsCount
.
put
(
nodeCom
,
1
);
graphWeightSum
+=
weight
;
}
...
...
@@ -206,7 +220,7 @@ public class Modularity implements Statistics, LongTask {
}
private
void
addNodeTo
(
int
node
,
Community
to
)
{
to
.
add
(
n
ew
Integer
(
node
)
);
to
.
add
(
n
ode
);
nodeCommunities
[
node
]
=
to
;
for
(
ModEdge
e
:
topology
[
node
])
{
...
...
@@ -276,8 +290,7 @@ public class Modularity implements Statistics, LongTask {
}
}
private
void
removeNodeFrom
(
int
node
,
Community
from
)
{
private
void
removeNodeFromItsCommunity
(
int
node
)
{
Community
community
=
nodeCommunities
[
node
];
for
(
ModEdge
e
:
topology
[
node
])
{
int
neighbor
=
e
.
target
;
...
...
@@ -334,18 +347,17 @@ public class Modularity implements Statistics, LongTask {
}
}
from
.
remove
(
new
Integer
(
node
)
);
community
.
remove
(
node
);
}
private
void
moveNodeTo
(
int
node
,
Community
to
)
{
Community
from
=
nodeCommunities
[
node
];
removeNodeFrom
(
node
,
from
);
removeNodeFromItsCommunity
(
node
);
addNodeTo
(
node
,
to
);
}
private
void
zoomOut
()
{
int
M
=
communities
.
size
();
LinkedList
<
ModEdge
>[]
newTopology
=
new
Linked
List
[
M
];
ArrayList
<
ModEdge
>[]
newTopology
=
new
Array
List
[
M
];
int
index
=
0
;
nodeCommunities
=
new
Community
[
M
];
nodeConnectionsWeight
=
new
HashMap
[
M
];
...
...
@@ -355,7 +367,8 @@ public class Modularity implements Statistics, LongTask {
Community
com
=
communities
.
get
(
i
);
nodeConnectionsWeight
[
index
]
=
new
HashMap
<>();
nodeConnectionsCount
[
index
]
=
new
HashMap
<>();
newTopology
[
index
]
=
new
LinkedList
<>();
newTopology
[
index
]
=
new
ArrayList
<>();
nodeCommunities
[
index
]
=
new
Community
(
com
);
Set
<
Community
>
iter
=
com
.
connectionsWeight
.
keySet
();
double
weightSum
=
0
;
...
...
@@ -406,7 +419,7 @@ public class Modularity implements Statistics, LongTask {
double
weightSum
;
CommunityStructure
structure
;
Li
nkedLi
st
<
Integer
>
nodes
;
List
<
Integer
>
nodes
;
HashMap
<
Modularity
.
Community
,
Float
>
connectionsWeight
;
HashMap
<
Modularity
.
Community
,
Integer
>
connectionsCount
;
...
...
@@ -418,7 +431,7 @@ public class Modularity implements Statistics, LongTask {
structure
=
com
.
structure
;
connectionsWeight
=
new
HashMap
<>();
connectionsCount
=
new
HashMap
<>();
nodes
=
new
Linked
List
<>();
nodes
=
new
Array
List
<>();
//mHidden = pCom.mHidden;
}
...
...
@@ -426,7 +439,7 @@ public class Modularity implements Statistics, LongTask {
this
.
structure
=
structure
;
connectionsWeight
=
new
HashMap
<>();
connectionsCount
=
new
HashMap
<>();
nodes
=
new
Linked
List
<>();
nodes
=
new
Array
List
<>();
}
public
void
seed
(
int
node
)
{
...
...
@@ -435,13 +448,13 @@ public class Modularity implements Statistics, LongTask {
}
public
boolean
add
(
int
node
)
{
nodes
.
add
Last
(
new
Integer
(
node
)
);
nodes
.
add
(
node
);
weightSum
+=
structure
.
weights
[
node
];
return
true
;
}
public
boolean
remove
(
int
node
)
{
boolean
result
=
nodes
.
remove
(
new
Integer
(
node
)
);
boolean
result
=
nodes
.
remove
(
(
Integer
)
node
);
weightSum
-=
structure
.
weights
[
node
];
if
(
nodes
.
isEmpty
())
{
structure
.
communities
.
remove
(
this
);
...
...
@@ -452,29 +465,33 @@ public class Modularity implements Statistics, LongTask {
@Override
public
void
execute
(
GraphModel
graphModel
)
{
Graph
h
graph
=
graphModel
.
getUndirectedGraphVisible
();
execute
(
h
graph
);
Graph
graph
=
graphModel
.
getUndirectedGraphVisible
();
execute
(
graph
);
}
public
void
execute
(
Graph
h
graph
)
{
public
void
execute
(
Graph
graph
)
{
isCanceled
=
false
;
hgraph
.
readLock
();
structure
=
new
Modularity
.
CommunityStructure
(
hgraph
);
int
[]
comStructure
=
new
int
[
hgraph
.
getNodeCount
()];
graph
.
readLock
();
HashMap
<
String
,
Double
>
computedModularityMetrics
=
computeModularity
(
hgraph
,
structure
,
comStructure
,
resolution
,
isRandomized
,
useWeight
);
structure
=
new
Modularity
.
CommunityStructure
(
graph
);
int
[]
comStructure
=
new
int
[
graph
.
getNodeCount
()];
modularity
=
computedModularityMetrics
.
get
(
"modularity"
);
modularityResolution
=
computedModularityMetrics
.
get
(
"modularityResolution"
);
if
(
graph
.
getNodeCount
()
>
0
)
{
//Fixes issue #713 Modularity Calculation Throws Exception On Empty Graph
HashMap
<
String
,
Double
>
computedModularityMetrics
=
computeModularity
(
graph
,
structure
,
comStructure
,
resolution
,
isRandomized
,
useWeight
);
modularity
=
computedModularityMetrics
.
get
(
"modularity"
);
modularityResolution
=
computedModularityMetrics
.
get
(
"modularityResolution"
);
}
else
{
modularity
=
0
;
modularityResolution
=
0
;
}
saveValues
(
comStructure
,
h
graph
,
structure
);
saveValues
(
comStructure
,
graph
,
structure
);
h
graph
.
readUnlock
();
graph
.
readUnlock
();
}
protected
HashMap
<
String
,
Double
>
computeModularity
(
Graph
h
graph
,
CommunityStructure
theStructure
,
int
[]
comStructure
,
protected
HashMap
<
String
,
Double
>
computeModularity
(
Graph
graph
,
CommunityStructure
theStructure
,
int
[]
comStructure
,
double
currentResolution
,
boolean
randomized
,
boolean
weighted
)
{
isCanceled
=
false
;
Progress
.
start
(
progress
);
...
...
@@ -486,7 +503,7 @@ public class Modularity implements Statistics, LongTask {
HashMap
<
String
,
Double
>
results
=
new
HashMap
<>();
if
(
isCanceled
)
{
h
graph
.
readUnlockAll
();
graph
.
readUnlockAll
();
return
results
;
}
boolean
someChange
=
true
;
...
...
@@ -508,13 +525,13 @@ public class Modularity implements Statistics, LongTask {
localChange
=
true
;
}
if
(
isCanceled
)
{
h
graph
.
readUnlockAll
();
graph
.
readUnlockAll
();
return
results
;
}
}
someChange
=
localChange
||
someChange
;
if
(
isCanceled
)
{
h
graph
.
readUnlockAll
();
graph
.
readUnlockAll
();
return
results
;
}
}
...
...
@@ -524,11 +541,11 @@ public class Modularity implements Statistics, LongTask {
}
}
fillComStructure
(
h
graph
,
theStructure
,
comStructure
);
double
[]
degreeCount
=
fillDegreeCount
(
h
graph
,
theStructure
,
comStructure
,
nodeDegrees
,
weighted
);
fillComStructure
(
graph
,
theStructure
,
comStructure
);
double
[]
degreeCount
=
fillDegreeCount
(
graph
,
theStructure
,
comStructure
,
nodeDegrees
,
weighted
);
double
computedModularity
=
finalQ
(
comStructure
,
degreeCount
,
h
graph
,
theStructure
,
totalWeight
,
1
.,
weighted
);
double
computedModularityResolution
=
finalQ
(
comStructure
,
degreeCount
,
h
graph
,
theStructure
,
totalWeight
,
currentResolution
,
weighted
);
double
computedModularity
=
finalQ
(
comStructure
,
degreeCount
,
graph
,
theStructure
,
totalWeight
,
1
.,
weighted
);
double
computedModularityResolution
=
finalQ
(
comStructure
,
degreeCount
,
graph
,
theStructure
,
totalWeight
,
currentResolution
,
weighted
);
results
.
put
(
"modularity"
,
computedModularity
);
results
.
put
(
"modularityResolution"
,
computedModularityResolution
);
...
...
@@ -536,12 +553,12 @@ public class Modularity implements Statistics, LongTask {
return
results
;
}
Community
updateBestCommunity
(
CommunityStructure
theStructure
,
int
i
,
double
currentResolution
)
{
private
Community
updateBestCommunity
(
CommunityStructure
theStructure
,
int
node_id
,
double
currentResolution
)
{
double
best
=
0
.;
Community
bestCommunity
=
null
;
Set
<
Community
>
iter
=
theStructure
.
nodeConnectionsWeight
[
i
].
keySet
();
Set
<
Community
>
iter
=
theStructure
.
nodeConnectionsWeight
[
node_id
].
keySet
();
for
(
Community
com
:
iter
)
{
double
qValue
=
q
(
i
,
com
,
theStructure
,
currentResolution
);
double
qValue
=
q
(
node_id
,
com
,
theStructure
,
currentResolution
);
if
(
qValue
>
best
)
{
best
=
qValue
;
bestCommunity
=
com
;
...
...
@@ -550,8 +567,7 @@ public class Modularity implements Statistics, LongTask {
return
bestCommunity
;
}
int
[]
fillComStructure
(
Graph
hgraph
,
CommunityStructure
theStructure
,
int
[]
comStructure
)
{
// int[] comStructure = new int[hgraph.getNodeCount()];
private
int
[]
fillComStructure
(
Graph
graph
,
CommunityStructure
theStructure
,
int
[]
comStructure
)
{
int
count
=
0
;
for
(
Community
com
:
theStructure
.
communities
)
{
...
...
@@ -566,37 +582,37 @@ public class Modularity implements Statistics, LongTask {
return
comStructure
;
}
double
[]
fillDegreeCount
(
Graph
h
graph
,
CommunityStructure
theStructure
,
int
[]
comStructure
,
double
[]
nodeDegrees
,
boolean
weighted
)
{
private
double
[]
fillDegreeCount
(
Graph
graph
,
CommunityStructure
theStructure
,
int
[]
comStructure
,
double
[]
nodeDegrees
,
boolean
weighted
)
{
double
[]
degreeCount
=
new
double
[
theStructure
.
communities
.
size
()];
for
(
Node
node
:
h
graph
.
getNodes
())
{
for
(
Node
node
:
graph
.
getNodes
())
{
int
index
=
theStructure
.
map
.
get
(
node
);
if
(
weighted
)
{
degreeCount
[
comStructure
[
index
]]
+=
nodeDegrees
[
index
];
}
else
{
degreeCount
[
comStructure
[
index
]]
+=
h
graph
.
getDegree
(
node
);
degreeCount
[
comStructure
[
index
]]
+=
graph
.
getDegree
(
node
);
}
}
return
degreeCount
;
}
private
double
finalQ
(
int
[]
struct
,
double
[]
degrees
,
Graph
h
graph
,
private
double
finalQ
(
int
[]
struct
,
double
[]
degrees
,
Graph
graph
,
CommunityStructure
theStructure
,
double
totalWeight
,
double
usedResolution
,
boolean
weighted
)
{
double
res
=
0
;
double
[]
internal
=
new
double
[
degrees
.
length
];
for
(
Node
n
:
h
graph
.
getNodes
())
{
for
(
Node
n
:
graph
.
getNodes
())
{
int
n_index
=
theStructure
.
map
.
get
(
n
);
for
(
Edge
edge
:
h
graph
.
getEdges
(
n
))
{
Node
neighbor
=
h
graph
.
getOpposite
(
n
,
edge
);
for
(
Edge
edge
:
graph
.
getEdges
(
n
))
{
Node
neighbor
=
graph
.
getOpposite
(
n
,
edge
);
if
(
n
==
neighbor
)
{
continue
;
}
int
neigh_index
=
theStructure
.
map
.
get
(
neighbor
);
if
(
struct
[
neigh_index
]
==
struct
[
n_index
])
{
if
(
weighted
)
{
internal
[
struct
[
neigh_index
]]
+=
edge
.
getWeight
(
h
graph
.
getView
());
internal
[
struct
[
neigh_index
]]
+=
edge
.
getWeight
(
graph
.
getView
());
}
else
{
internal
[
struct
[
neigh_index
]]++;
}
...
...
@@ -610,13 +626,13 @@ public class Modularity implements Statistics, LongTask {
return
res
;
}
private
void
saveValues
(
int
[]
struct
,
Graph
h
graph
,
CommunityStructure
theStructure
)
{
Table
nodeTable
=
h
graph
.
getModel
().
getNodeTable
();
private
void
saveValues
(
int
[]
struct
,
Graph
graph
,
CommunityStructure
theStructure
)
{
Table
nodeTable
=
graph
.
getModel
().
getNodeTable
();
Column
modCol
=
nodeTable
.
getColumn
(
MODULARITY_CLASS
);
if
(
modCol
==
null
)
{
modCol
=
nodeTable
.
addColumn
(
MODULARITY_CLASS
,
"Modularity Class"
,
Integer
.
class
,
new
Integer
(
0
)
);
modCol
=
nodeTable
.
addColumn
(
MODULARITY_CLASS
,
"Modularity Class"
,
Integer
.
class
,
0
);
}
for
(
Node
n
:
h
graph
.
getNodes
())
{
for
(
Node
n
:
graph
.
getNodes
())
{
int
n_index
=
theStructure
.
map
.
get
(
n
);
n
.
setAttribute
(
modCol
,
struct
[
n_index
]);
}
...
...
modules/StatisticsPlugin/src/test/java/org/gephi/statistics/plugin/ModularityNGTest.java
浏览文件 @
fe24632e
...
...
@@ -250,22 +250,33 @@ public class ModularityNGTest {
undirectedGraph
.
addNode
(
node7
);
undirectedGraph
.
addNode
(
node8
);
Edge
edge12
=
graphModel
.
factory
().
newEdge
(
node1
,
node2
,
0
,
10
.
f
,
false
);
//Test 3 parallel edges summing weight = 10
//Related issue ==> #1419 Getting null pointer error when trying to calculate modularity
Edge
edge12_1
=
graphModel
.
factory
().
newEdge
(
node1
,
node2
,
0
,
2
.
f
,
false
);
Edge
edge12_2
=
graphModel
.
factory
().
newEdge
(
node1
,
node2
,
0
,
5
.
f
,
false
);
Edge
edge12_3
=
graphModel
.
factory
().
newEdge
(
node1
,
node2
,
0
,
3
.
f
,
false
);
Edge
edge23
=
graphModel
.
factory
().
newEdge
(
node2
,
node3
,
false
);
Edge
edge34
=
graphModel
.
factory
().
newEdge
(
node3
,
node4
,
0
,
10
.
f
,
false
);
Edge
edge45
=
graphModel
.
factory
().
newEdge
(
node4
,
node5
,
false
);
Edge
edge56
=
graphModel
.
factory
().
newEdge
(
node5
,
node6
,
0
,
10
.
f
,
false
);
Edge
edge67
=
graphModel
.
factory
().
newEdge
(
node6
,
node7
,
false
);
Edge
edge78
=
graphModel
.
factory
().
newEdge
(
node7
,
node8
,
0
,
10
.
f
,
false
);
//Test 2 parallel edges summing weight = 10
Edge
edge78_1
=
graphModel
.
factory
().
newEdge
(
node7
,
node8
,
0
,
5
.
f
,
false
);
Edge
edge78_2
=
graphModel
.
factory
().
newEdge
(
node7
,
node8
,
0
,
5
.
f
,
false
);
Edge
edge81
=
graphModel
.
factory
().
newEdge
(
node8
,
node1
,
false
);
undirectedGraph
.
addEdge
(
edge12
);
undirectedGraph
.
addEdge
(
edge12_1
);
undirectedGraph
.
addEdge
(
edge12_2
);
undirectedGraph
.
addEdge
(
edge12_3
);
undirectedGraph
.
addEdge
(
edge23
);
undirectedGraph
.
addEdge
(
edge34
);
undirectedGraph
.
addEdge
(
edge45
);
undirectedGraph
.
addEdge
(
edge56
);
undirectedGraph
.
addEdge
(
edge67
);
undirectedGraph
.
addEdge
(
edge78
);
undirectedGraph
.
addEdge
(
edge78_1
);
undirectedGraph
.
addEdge
(
edge78_2
);
undirectedGraph
.
addEdge
(
edge81
);
UndirectedGraph
hgraph
=
graphModel
.
getUndirectedGraph
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录