use of gnu.trove.map.hash.TIntFloatHashMap in project cogcomp-nlp by CogComp.
the class Lexicon method getFeatureVector.
/**
* generate a feature id representation from a feature vector with associated weights
*
* @param featureMap
* @return
*/
public Pair<int[], float[]> getFeatureVector(Map<String, Float> featureMap) {
TIntFloatHashMap feats = new TIntFloatHashMap();
for (Entry<String, Float> f : featureMap.entrySet()) {
String key = f.getKey();
if (!contains(key))
continue;
int id = lookupId(key);
float value = f.getValue();
if (!feats.containsKey(id))
feats.put(id, value);
}
float[] vals = new float[feats.size()];
int[] idsOriginal = feats.keys();
int[] ids = new int[idsOriginal.length];
System.arraycopy(idsOriginal, 0, ids, 0, ids.length);
Arrays.sort(ids);
for (int i = 0; i < ids.length; i++) {
vals[i] = feats.get(ids[i]);
}
return new Pair<>(ids, vals);
}
use of gnu.trove.map.hash.TIntFloatHashMap in project gephi by gephi.
the class OpenOrdLayout method initAlgo.
@Override
public void initAlgo() {
//Verify param
if (param.getIterationsSum() != 1f) {
param = Params.DEFAULT;
//throw new RuntimeException("The sum of the time for each stage must be equal to 1");
}
//Get graph
graph = graphModel.getUndirectedGraphVisible();
graph.readLock();
try {
int numNodes = graph.getNodeCount();
//Prepare data structure - nodes and neighbors map
Node[] nodes = new Node[numNodes];
TIntFloatHashMap[] neighbors = new TIntFloatHashMap[numNodes];
//Load nodes and edges
TIntIntHashMap idMap = new TIntIntHashMap(numNodes, 1f);
org.gephi.graph.api.Node[] graphNodes = graph.getNodes().toArray();
for (int i = 0; i < numNodes; i++) {
org.gephi.graph.api.Node n = graphNodes[i];
nodes[i] = new Node(i);
nodes[i].x = n.x();
nodes[i].y = n.y();
nodes[i].fixed = n.isFixed();
OpenOrdLayoutData layoutData = new OpenOrdLayoutData(i);
n.setLayoutData(layoutData);
idMap.put(n.getStoreId(), i);
}
float highestSimilarity = Float.NEGATIVE_INFINITY;
for (Edge e : graph.getEdges()) {
int source = idMap.get(e.getSource().getStoreId());
int target = idMap.get(e.getTarget().getStoreId());
if (source != target) {
//No self-loop
float weight = (float) e.getWeight();
if (neighbors[source] == null) {
neighbors[source] = new TIntFloatHashMap();
}
if (neighbors[target] == null) {
neighbors[target] = new TIntFloatHashMap();
}
neighbors[source].put(target, weight);
neighbors[target].put(source, weight);
highestSimilarity = Math.max(highestSimilarity, weight);
}
}
//Reset position
boolean someFixed = false;
for (Node n : nodes) {
if (!n.fixed) {
n.x = 0;
n.y = 0;
} else {
someFixed = true;
}
}
//Recenter fixed nodes and rescale to fit into grid
if (someFixed) {
float minX = Float.POSITIVE_INFINITY;
float maxX = Float.NEGATIVE_INFINITY;
float minY = Float.POSITIVE_INFINITY;
float maxY = Float.NEGATIVE_INFINITY;
for (Node n : nodes) {
if (n.fixed) {
minX = Math.min(minX, n.x);
maxX = Math.max(maxX, n.x);
minY = Math.min(minY, n.y);
maxY = Math.max(maxY, n.y);
}
}
float shiftX = minX + (maxX - minX) / 2f;
float shiftY = minY + (maxY - minY) / 2f;
float ratio = Math.min(DensityGrid.getViewSize() / (maxX - minX), DensityGrid.getViewSize() / (maxY - minY));
ratio = Math.min(1f, ratio);
for (Node n : nodes) {
if (n.fixed) {
n.x = (float) (n.x - shiftX) * ratio;
n.y = (float) (n.y - shiftY) * ratio;
}
}
}
//Init control and workers
control = new Control();
combine = new Combine(this);
barrier = new CyclicBarrier(numThreads, combine);
control.setEdgeCut(edgeCut);
control.setRealParm(realTime);
control.setProgressTicket(progressTicket);
control.initParams(param, numIterations);
control.setNumNodes(numNodes);
control.setHighestSimilarity(highestSimilarity);
workers = new Worker[numThreads];
for (int i = 0; i < numThreads; ++i) {
workers[i] = new Worker(i, numThreads, barrier);
workers[i].setRandom(new Random(randSeed));
control.initWorker(workers[i]);
}
//Deep copy of a partition of all neighbors for each workers
for (Worker w : workers) {
Node[] nodesCopy = new Node[nodes.length];
for (int i = 0; i < nodes.length; i++) {
nodesCopy[i] = nodes[i].clone();
}
TIntFloatHashMap[] neighborsCopy = new TIntFloatHashMap[numNodes];
for (int i = 0; i < neighbors.length; i++) {
if (i % numThreads == w.getId() && neighbors[i] != null) {
int neighborsCount = neighbors[i].size();
neighborsCopy[i] = new TIntFloatHashMap(neighborsCount, 1f);
for (TIntFloatIterator itr = neighbors[i].iterator(); itr.hasNext(); ) {
itr.advance();
float weight = normalizeWeight(itr.value(), highestSimilarity);
neighborsCopy[i].put(itr.key(), weight);
}
}
}
w.setPositions(nodesCopy);
w.setNeighbors(neighborsCopy);
}
//Add real nodes
for (Node n : nodes) {
if (n.fixed) {
for (Worker w : workers) {
w.getDensityGrid().add(n, w.isFineDensity());
}
}
}
running = true;
firstIteration = true;
} finally {
graph.readUnlockAll();
}
}
use of gnu.trove.map.hash.TIntFloatHashMap in project gephi by gephi.
the class Worker method solveAnalytic.
private void solveAnalytic(int nodeIndex) {
float totalWeight = 0;
float xDis, yDis, xCen = 0, yCen = 0;
float x = 0, y = 0;
float damping;
TIntFloatHashMap map = neighbors[nodeIndex];
if (map != null) {
Node n = positions[nodeIndex];
for (TIntFloatIterator itr = map.iterator(); itr.hasNext(); ) {
itr.advance();
float weight = itr.value();
Node m = positions[itr.key()];
totalWeight += weight;
x += weight * m.x;
y += weight * m.y;
}
if (totalWeight > 0) {
xCen = x / totalWeight;
yCen = y / totalWeight;
damping = 1f - dampingMult;
float posX = damping * n.x + (1f - damping) * xCen;
float posY = damping * n.y + (1f - damping) * yCen;
n.x = posX;
n.y = posY;
}
if (minEdges == 99) {
return;
}
if (cutEnd >= 39500) {
return;
}
float maxLength = 0;
int maxIndex = -1;
int neighborsCount = map.size();
if (neighborsCount >= minEdges) {
for (TIntFloatIterator itr = neighbors[nodeIndex].iterator(); itr.hasNext(); ) {
itr.advance();
Node m = positions[itr.key()];
xDis = xCen - m.x;
yDis = yCen - m.y;
float dis = xDis * xDis + yDis * yDis;
dis *= Math.sqrt(neighborsCount);
if (dis > maxLength) {
maxLength = dis;
maxIndex = itr.key();
}
}
}
if (maxLength > cutOffLength && maxIndex != -1) {
map.remove(maxIndex);
}
}
}
Aggregations