-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTimeForce.java
More file actions
491 lines (403 loc) · 17.2 KB
/
Copy pathTimeForce.java
File metadata and controls
491 lines (403 loc) · 17.2 KB
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
/*
Copyright 2008-2011 Gephi
Authors : Mathieu Jacomy <mathieu.jacomy@gmail.com>
Website : http://www.gephi.org
This file is part of Gephi.
DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
Copyright 2011 Gephi Consortium. All rights reserved.
The contents of this file are subject to the terms of either the GNU
General Public License Version 3 only ("GPL") or the Common
Development and Distribution License("CDDL") (collectively, the
"License"). You may not use this file except in compliance with the
License. You can obtain a copy of the License at
http://gephi.org/about/legal/license-notice/
or /cddl-1.0.txt and /gpl-3.0.txt. See the License for the
specific language governing permissions and limitations under the
License. When distributing the software, include this License Header
Notice in each file and include the License files at
/cddl-1.0.txt and /gpl-3.0.txt. If applicable, add the following below the
License Header, with the fields enclosed by brackets [] replaced by
your own identifying information:
"Portions Copyrighted [year] [name of copyright owner]"
If you wish your version of this file to be governed by only the CDDL
or only the GPL Version 3, indicate your decision by adding
"[Contributor] elects to include this software in this distribution
under the [CDDL or GPL Version 3] license." If you do not indicate a
single choice of license, a recipient has the option to distribute
your version of this file under either the CDDL, the GPL Version 3 or
to extend the choice of license to its licensees as provided above.
However, if you add GPL Version 3 code and therefore, elected the GPL
Version 3 license, then the option applies only if the new code is
made subject to such option by the copyright holder.
Contributor(s): Wouter Spekkink
Contribution: Most of the code is from the original Force Atlas 2 source code
Some changes were made to change the way that nodes are laid out in the x-axis
The x-axis layout is now based on a variable that should be submitted by the
user. The approach used to implement this was inspired by the source dode
of the GeoLayout plugin.
Portions Copyrighted 2011 Gephi Consortium.
*/
package org.wouterspekkink.eventgraphlayout;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.gephi.data.attributes.api.AttributeColumn;
import org.gephi.data.attributes.api.AttributeController;
import org.gephi.data.attributes.api.AttributeModel;
import org.gephi.data.attributes.api.AttributeRow;
import org.gephi.graph.api.Edge;
import org.gephi.graph.api.Graph;
import org.gephi.graph.api.GraphModel;
import org.gephi.graph.api.Node;
import org.gephi.layout.spi.Layout;
import org.gephi.layout.spi.LayoutBuilder;
import org.gephi.layout.spi.LayoutProperty;
import org.gephi.ui.propertyeditor.NodeColumnNumbersEditor;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.wouterspekkink.eventgraphlayout.ForceFactory.AttractionForce;
import org.wouterspekkink.eventgraphlayout.ForceFactory.RepulsionForce;
/**
*
* @author wouterspekkink
*/
public class TimeForce implements Layout {
private GraphModel graphModel;
private Graph graph;
private final TimeForceBuilder layoutBuilder;
private double jitterTolerance;
private double scalingRatio;
private double orderScale;
private AttributeColumn order;
private double gravity;
private double speed;
private boolean strongGravityMode;
private int threadCount;
private int currentThreadCount;
private ExecutorService pool;
private boolean vertical;
private boolean center;
public TimeForce(TimeForceBuilder layoutBuilder) {
this.layoutBuilder = layoutBuilder;
this.threadCount = Math.min(4, Math.max(1, Runtime.getRuntime().availableProcessors() - 1));
resetPropertiesValues();
}
@Override
public void initAlgo() {
speed = 1.;
graph = graphModel.getGraphVisible();
graph.readLock();
Node[] nodes = graph.getNodes().toArray();
// Initialise layout data
for (Node n : nodes) {
if (n.getNodeData().getLayoutData() == null || !(n.getNodeData().getLayoutData() instanceof TimeForceLayoutData)) {
TimeForceLayoutData nLayout = new TimeForceLayoutData();
n.getNodeData().setLayoutData(nLayout);
}
TimeForceLayoutData nLayout = n.getNodeData().getLayoutData();
nLayout.mass = 1 + graph.getDegree(n);
nLayout.old_dx = 0;
nLayout.old_dy = 0;
nLayout.dx = 0;
nLayout.dy = 0;
}
pool = Executors.newFixedThreadPool(threadCount);
currentThreadCount = threadCount;
}
@Override
public void goAlgo() {
// Initialize graph data
if (graphModel == null) {
return;
}
graph = graphModel.getGraphVisible();
graph.readLock();
Node[] nodes = graph.getNodes().toArray();
Edge[] edges = graph.getEdges().toArray();
Vector<Node> validNodes = new Vector<Node>();;
Vector<Node> unvalidNodes = new Vector<Node>();;
for (Node n : nodes) {
AttributeRow row = (AttributeRow) n.getNodeData().getAttributes();
if(row.getValue(order)!=null){
validNodes.add(n);
} else {
unvalidNodes.add(n);
}
}
// Initialise layout data
for (Node n : nodes) {
if (n.getNodeData().getLayoutData() == null || !(n.getNodeData().getLayoutData() instanceof TimeForceLayoutData)) {
TimeForceLayoutData nLayout = new TimeForceLayoutData();
n.getNodeData().setLayoutData(nLayout);
}
TimeForceLayoutData nLayout = n.getNodeData().getLayoutData();
nLayout.mass = 1 + graph.getDegree(n);
nLayout.dx = 0;
nLayout.dy = 0;
nLayout.old_dx = nLayout.dx;
nLayout.old_dy = nLayout.dy;
}
// Repulsion (and gravity)
// NB: Muti-threaded
RepulsionForce Repulsion = ForceFactory.builder.buildRepulsion(getScalingRatio());
int taskCount = 8 * currentThreadCount; // The threadPool Executor Service will manage the fetching of tasks and threads.
// We make more tasks than threads because some tasks may need more time to compute.
ArrayList<Future> threads = new ArrayList();
for (int t = taskCount; t > 0; t--) {
int from = (int) Math.floor(nodes.length * (t - 1) / taskCount);
int to = (int) Math.floor(nodes.length * t / taskCount);
Future future = pool.submit(new NodesThread(nodes, from, to, getGravity(), (isStrongGravityMode()) ? (ForceFactory.builder.getStrongGravity(getScalingRatio())) : (Repulsion), getScalingRatio(), Repulsion));
threads.add(future);
}
for (Future future : threads) {
try {
future.get();
} catch (InterruptedException ex) {
Exceptions.printStackTrace(ex);
} catch (ExecutionException ex) {
Exceptions.printStackTrace(ex);
}
}
//Attraction
AttractionForce Attraction = ForceFactory.builder.buildAttraction(1);
for (Edge e : edges) {
Attraction.apply(e.getSource(), e.getTarget(), 1);
}
// Auto adjust speed
double totalSwinging = 0d; // How much irregular movement
double totalEffectiveTraction = 0d; // Hom much useful movement
for (Node n : nodes) {
TimeForceLayoutData nLayout = n.getNodeData().getLayoutData();
if (!n.getNodeData().isFixed()) {
double swinging = Math.sqrt(Math.pow(nLayout.old_dx - nLayout.dx, 2) + Math.pow(nLayout.old_dy - nLayout.dy, 2));
totalSwinging += nLayout.mass * swinging; // If the node has a burst change of direction, then it's not converging.
totalEffectiveTraction += nLayout.mass * 0.5 * Math.sqrt(Math.pow(nLayout.old_dx + nLayout.dx, 2) + Math.pow(nLayout.old_dy + nLayout.dy, 2));
}
}
// We want that swingingMovement < tolerance * convergenceMovement
double targetSpeed = getJitterTolerance() * getJitterTolerance() * totalEffectiveTraction / totalSwinging;
// But the speed shoudn't rise too much too quickly, since it would make the convergence drop dramatically.
double maxRise = 0.5; // Max rise: 50%
speed = speed + Math.min(targetSpeed - speed, maxRise * speed);
// Apply Forces
for (Node n: validNodes) {
AttributeRow row = (AttributeRow) n.getNodeData().getAttributes();
TimeForceLayoutData nLayout = n.getNodeData().getLayoutData();
if (!n.getNodeData().isFixed()) {
// Adaptive auto-speed: the speed of each node is lowered
// when the node swings.
double swinging = Math.sqrt((nLayout.old_dx - nLayout.dx) * (nLayout.old_dx - nLayout.dx) + (nLayout.old_dy - nLayout.dy) * (nLayout.old_dy - nLayout.dy));
//double factor = speed / (1f + Math.sqrt(speed * swinging));
double factor = speed / (1f + speed * Math.sqrt(swinging));
double x = n.getNodeData().x() + nLayout.dx * factor;
double y = n.getNodeData().y() + nLayout.dy * factor;
double averageX = 0;
double averageY = 0;
float ord = getFloatValue(n, order);
ord = ord * (float) orderScale;
n.getNodeData().setX(ord);
if (vertical == true) {
n.getNodeData().setY((float) y);
} else {
n.getNodeData().setY(n.getNodeData().y());
}
if (center == true) {
averageX += x;
averageY += y;
averageX = averageX/nodes.length;
averageY = averageY/nodes.length;
x = n.getNodeData().x() - averageX;
y = n.getNodeData().y() - averageY;
n.getNodeData().setX((float) x);
n.getNodeData().setY((float) y);
}
}
}
graph.readUnlockAll();
}
public float getFloatValue(Node node, AttributeColumn column) {
return ((Number) node.getNodeData().getAttributes().getValue(column.getIndex())).floatValue();
}
@Override
public boolean canAlgo() {
return graphModel != null && order != null;
}
@Override
public void endAlgo() {
for (Node n : graph.getNodes()) {
n.getNodeData().setLayoutData(null);
}
pool.shutdown();
graph.readUnlockAll();
}
@Override
public LayoutProperty[] getProperties() {
List<LayoutProperty> properties = new ArrayList<LayoutProperty>();
final String TIMEFORCE = "Time Force Layout";
try {
properties.add(LayoutProperty.createProperty(
this, Double.class,
"Scale of Order",
TIMEFORCE,
"Determines the separation of the nodes on the x-axis",
"getOrderScale", "setOrderScale"));
properties.add(LayoutProperty.createProperty(
this, AttributeColumn.class,
"Order",
TIMEFORCE,
"Selects the attribute that indicates the order of events",
"getOrder", "setOrder", NodeColumnNumbersEditor.class));
properties.add(LayoutProperty.createProperty(
this, Boolean.class,
"Set Vertical Force",
TIMEFORCE,
"Used to push unconnected groups of nodes away from each other",
"isVertical", "setVertical"));
properties.add(LayoutProperty.createProperty(
this, Double.class,
"Vertical Scale",
TIMEFORCE,
"Sets the strength of the vertical force",
"getScalingRatio", "setScalingRatio"));
properties.add(LayoutProperty.createProperty(
this, Boolean.class,
"Strong Gravity Mode",
TIMEFORCE,
"Sets the strong gravity mode",
"isStrongGravityMode", "setStrongGravityMode"));
properties.add(LayoutProperty.createProperty(
this, Double.class,
"Gravity",
TIMEFORCE,
"Pulls nodes to origin of the y-axis. Prevents islands from drifting away.",
"getGravity", "setGravity"));
properties.add(LayoutProperty.createProperty(
this, Double.class,
"Jitter Tolerance",
TIMEFORCE,
"How much swiging you allow.",
"getJitterTolerance", "setJitterTolerance"));
properties.add(LayoutProperty.createProperty(
this, Integer.class,
"Threads",
TIMEFORCE,
"Possibility to use more threads if your cores can handle it.",
"getThreadsCount", "setThreadsCount"));
properties.add(LayoutProperty.createProperty(
this, Boolean.class,
"Center",
TIMEFORCE,
"Centers the graph",
"isCenter", "setCenter"));
} catch (Exception e) {
e.printStackTrace();
}
return properties.toArray(new LayoutProperty[0]);
}
@Override
public void resetPropertiesValues() {
AttributeModel attModel = Lookup.getDefault().lookup(AttributeController.class).getModel();
for (AttributeColumn c : attModel.getNodeTable().getColumns()) {
if(c.getId().equalsIgnoreCase("order")
|| c.getId().equalsIgnoreCase("ord")
|| c.getTitle().equalsIgnoreCase("order")
|| c.getTitle().equalsIgnoreCase("ord")) {
order = c;
} else {
order = null;
}
}
int nodesCount = 0;
if (graphModel != null) {
nodesCount = graphModel.getGraphVisible().getNodeCount();
}
setOrderScale(10.0);
// Tuning
setScalingRatio(1.0);
setStrongGravityMode(false);
setGravity(1.);
setVertical(false);
setCenter(false);
// Performance
if (nodesCount >= 50000) {
setJitterTolerance(10d);
} else if (nodesCount >= 5000) {
setJitterTolerance(1d);
} else {
setJitterTolerance(0.3d);
}
setThreadsCount(2);
}
@Override
public LayoutBuilder getBuilder() {
return layoutBuilder;
}
@Override
public void setGraphModel(GraphModel graphModel) {
this.graphModel = graphModel;
// Trick: reset here to take the profile of the graph in account for default values
resetPropertiesValues();
}
public Double getJitterTolerance() {
return jitterTolerance;
}
public void setJitterTolerance(Double jitterTolerance) {
this.jitterTolerance = jitterTolerance;
}
public Double getScalingRatio() {
return scalingRatio;
}
public void setScalingRatio(Double scalingRatio) {
this.scalingRatio = scalingRatio;
}
public Boolean isStrongGravityMode() {
return strongGravityMode;
}
public void setStrongGravityMode(Boolean strongGravityMode) {
this.strongGravityMode = strongGravityMode;
}
public Boolean isVertical() {
return vertical;
}
public void setVertical(Boolean vertical) {
this.vertical = vertical;
}
public Boolean isCenter() {
return center;
}
public void setCenter(Boolean center) {
this.center = center;
}
public Double getGravity() {
return gravity;
}
public void setGravity(Double gravity) {
this.gravity = gravity;
}
public Integer getThreadsCount() {
return threadCount;
}
public void setThreadsCount(Integer threadCount) {
if (threadCount < 1) {
setThreadsCount(1);
} else {
this.threadCount = threadCount;
}
}
public Double getOrderScale() {
return orderScale;
}
public void setOrderScale(Double orderScale) {
this.orderScale = orderScale;
}
public AttributeColumn getOrder() {
return order;
}
public void setOrder(AttributeColumn order) {
this.order = order;
}
}