use of org.cytoscape.model.CyNetwork in project cytoscape-api by cytoscape.
the class AbstractParallelPartitionLayoutTask method doLayout.
/**
* AbstractGraphPartitionLayout implements the doLayout method
* of AbstractBasicLayout in which it calls the layoutParition
* method on each LayoutPartition object created for the network.
* @param taskMonitor the TaskMonitor provided by the run() method
* of the Task.
*/
@Override
public void doLayout(final TaskMonitor taskMonitor) {
final CyNetwork network = networkView.getModel();
if (edgeWeighter != null)
edgeWeighter.reset();
this.taskMonitor = taskMonitor;
boolean useAllNodes = nodesToLayOut.size() == networkView.getNodeViews().size();
// to lay out selected nodes, partitioning becomes a very bad idea!
if (singlePartition || !useAllNodes) {
// We still use the partition abstraction, even if we're
// not partitioning. This makes the code further down
// much cleaner
LayoutPartition partition = new LayoutPartition(networkView, nodesToLayOut, edgeWeighter);
partitionList = new ArrayList<LayoutPartition>(1);
partitionList.add(partition);
} else {
partitionList = PartitionUtil.partition(networkView, false, edgeWeighter);
}
total_nodes = network.getNodeCount();
current_start = 0;
// Set up offsets -- we start with the overall min and max
double xStart = (partitionList.get(0)).getMinX();
double yStart = (partitionList.get(0)).getMinY();
for (LayoutPartition part : partitionList) {
xStart = Math.min(xStart, part.getMinX());
yStart = Math.min(yStart, part.getMinY());
}
double next_x_start = xStart;
double next_y_start = yStart;
double current_max_y = 0;
double max_dimensions = Math.sqrt((double) network.getNodeCount());
// give each node room
max_dimensions *= incr;
max_dimensions += xStart;
// Overthread to account for inefficiencies.
final int numThreads = Runtime.getRuntime().availableProcessors() * 4;
// the layoutPartition method on each partition.
class ParallelLayoutTask implements Runnable {
private final int partitionID;
public ParallelLayoutTask(int partitionID) {
this.partitionID = partitionID;
}
@Override
public void run() {
if (cancelled)
return;
LayoutPartition partition = partitionList.get(partitionID);
// Partitions Requiring Layout
if (partition.nodeCount() > 1) {
try {
layoutPartition(partition);
} catch (Throwable _e) {
_e.printStackTrace();
return;
}
} else {
// Reset our bounds
partition.resetNodes();
}
synchronized (partitionList) {
current_start += (double) partition.size();
taskMonitor.setProgress(current_start / total_nodes);
}
}
}
// Create thread pool, ...
ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
// ... spawn futures, ...
List<Future<?>> futures = new LinkedList<Future<?>>();
for (int i = 0; i < partitionList.size(); i++) futures.add(threadPool.submit(new ParallelLayoutTask(i)));
// ... and wait for them to execute.
for (Future<?> future : futures) try {
future.get();
} catch (Exception e) {
}
// Now move the partitions into a reasonably compact grid.
for (LayoutPartition partition : partitionList) {
if (cancelled)
break;
// get the partition
current_size = (double) partition.size();
// Partitions Requiring Layout
if (partition.nodeCount() > 1) {
// OFFSET
if (useAllNodes && !singlePartition)
partition.offset(next_x_start, next_y_start);
} else // single nodes
{
// Single node -- get it
LayoutNode node = (LayoutNode) partition.getNodeList().get(0);
node.setLocation(next_x_start, next_y_start);
partition.moveNodeToLocation(node);
}
double last_max_x = partition.getMaxX();
double last_max_y = partition.getMaxY();
if (last_max_y > current_max_y)
current_max_y = last_max_y;
if (last_max_x > max_dimensions) {
next_x_start = xStart;
next_y_start = current_max_y;
next_y_start += incr;
} else {
next_x_start = last_max_x;
next_x_start += incr;
}
}
}
use of org.cytoscape.model.CyNetwork in project cytoscape-api by cytoscape.
the class AbstractCyNetworkReader method init.
private void init() {
name2RootMap = getRootNetworkMap();
nodeMap = new HashMap<>(10000);
// initialize the network Collection
final List<String> rootNames = new ArrayList<>();
rootNames.addAll(name2RootMap.keySet());
if (!rootNames.isEmpty()) {
sort(rootNames);
rootNames.add(0, CREATE_NEW_COLLECTION_STRING);
}
final ListSingleSelection<String> rootNetList = new ListSingleSelection<>(rootNames);
final CyNetwork net = cyApplicationManager != null ? cyApplicationManager.getCurrentNetwork() : null;
final CyRootNetwork rootNet = net != null ? cyRootNetworkManager.getRootNetwork(net) : null;
final String name = rootNet != null ? rootNet.getRow(rootNet).get(CyRootNetwork.NAME, String.class) : CREATE_NEW_COLLECTION_STRING;
if (rootNames.contains(name))
rootNetList.setSelectedValue(name);
else if (rootNames.contains(CREATE_NEW_COLLECTION_STRING))
rootNetList.setSelectedValue(CREATE_NEW_COLLECTION_STRING);
setRootNetworkList(rootNetList);
// initialize renderer list
final List<NetworkViewRenderer> renderers = new ArrayList<>();
NetworkViewRenderer defViewRenderer = null;
if (cyApplicationManager != null) {
final Set<NetworkViewRenderer> rendererSet = cyApplicationManager.getNetworkViewRendererSet();
// so the combo-box does not appear to the user, since there is nothing to select anyway.
if (rendererSet.size() > 1) {
renderers.addAll(rendererSet);
Collections.sort(renderers, new Comparator<NetworkViewRenderer>() {
@Override
public int compare(NetworkViewRenderer r1, NetworkViewRenderer r2) {
return r1.toString().compareToIgnoreCase(r2.toString());
}
});
}
defViewRenderer = cyApplicationManager.getDefaultNetworkViewRenderer();
}
rendererList = new ListSingleSelection<>(renderers);
if (defViewRenderer != null && renderers.contains(defViewRenderer))
rendererList.setSelectedValue(defViewRenderer);
}
use of org.cytoscape.model.CyNetwork in project cytoscape-api by cytoscape.
the class SetCurrentNetworkEventTest method testGoodGetNetwork.
@Test
public void testGoodGetNetwork() {
CyApplicationManager source = mock(CyApplicationManager.class);
CyNetwork n = mock(CyNetwork.class);
SetCurrentNetworkEvent e = new SetCurrentNetworkEvent(source, n);
assertNotNull(e.getNetwork());
assertEquals(n, e.getNetwork());
}
use of org.cytoscape.model.CyNetwork in project cytoscape-api by cytoscape.
the class NetworkAboutToBeDestroyedEventTest method testGetNetwork.
@Test
public final void testGetNetwork() {
final CyNetworkManager networkManager = mock(CyNetworkManager.class);
final CyNetwork network = mock(CyNetwork.class);
final NetworkAboutToBeDestroyedEvent event = new NetworkAboutToBeDestroyedEvent(networkManager, network);
assertEquals("Network returned by getNetwork() is *not* the one passed into the constructor!", network, event.getNetwork());
}
use of org.cytoscape.model.CyNetwork in project cytoscape-api by cytoscape.
the class CySessionTest method testSetObjectMap.
@Test
public void testSetObjectMap() {
Map<Object, CyNetwork> netMap = new HashMap<Object, CyNetwork>();
CyNetwork net1 = mock(CyNetwork.class);
netMap.put("A", net1);
CyNetwork net2 = mock(CyNetwork.class);
netMap.put("B", net2);
Map<Object, CyNetworkView> viewMap = new HashMap<Object, CyNetworkView>();
CyNetworkView view1 = mock(CyNetworkView.class);
viewMap.put("A", view1);
Map<Object, CyNode> nodeMap = new HashMap<Object, CyNode>();
CyNode n1 = mock(CyNode.class);
nodeMap.put(new Long(1), n1);
CyNode n2 = mock(CyNode.class);
nodeMap.put(new Long(2), n2);
Map<Object, CyEdge> edgeMap = new HashMap<Object, CyEdge>();
CyEdge e1 = mock(CyEdge.class);
edgeMap.put(new Long(3), e1);
Map<Class<? extends CyIdentifiable>, Map<Object, ? extends CyIdentifiable>> objMap = new HashMap<Class<? extends CyIdentifiable>, Map<Object, ? extends CyIdentifiable>>();
objMap.put(CyNetwork.class, netMap);
objMap.put(CyNetworkView.class, viewMap);
objMap.put(CyNode.class, nodeMap);
objMap.put(CyEdge.class, edgeMap);
session = new CySession.Builder().objectMap(objMap).build();
assertNotNull(session);
assertSame(net1, session.getObject("A", CyNetwork.class));
assertSame(net2, session.getObject("B", CyNetwork.class));
assertSame(view1, session.getObject("A", CyNetworkView.class));
assertSame(n1, session.getObject(new Long(1), CyNode.class));
assertSame(n2, session.getObject(new Long(2), CyNode.class));
assertSame(e1, session.getObject(new Long(3), CyEdge.class));
assertNull(session.getObject("A", View.class));
assertNull(session.getObject("B", CyNetworkView.class));
}
Aggregations