it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap Java Examples

The following examples show how to use it.unimi.dsi.fastutil.longs.Long2DoubleOpenHashMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SequentialSubPatternAll.java    From StreamingRec with Apache License 2.0 5 votes vote down vote up
@Override
public LongArrayList recommendInternal(ClickData clickData) {
	// create a score map
	Map<Long, Double> score = new Long2DoubleOpenHashMap();
	// iterate over the subpatterns of the session that end with the most
	// current click
	for (int j = 0; j < clickData.session.size(); j++) {
		// step down the pattern tree
		SequenceTreeNode currentNode = patternTree;
		for (int i = j; i < clickData.session.size(); i++) {
			Transaction click = clickData.session.get(i);
			if (!currentNode.children.containsKey(getTreeNodeKey(click))) {
				continue;
			}
			currentNode = currentNode.children.get(getTreeNodeKey(click));
		}
		// if we reached the right node, look at the children and add up the
		// support values
		for (Entry<String, SequenceTreeNode> child : currentNode.children.entrySet()) {
			double childscore = child.getValue().support;
			if (weight){
				childscore = 1d/(j+1)*childscore;
			}
			Long key= Long.parseLong(child.getKey());
			if (score.containsKey(key)) {
				score.put(key, (score.get(key) + childscore));
			}
			score.put(key, childscore);
		}
	}

	// sort the accumulated support values and create a recommendation list
	return (LongArrayList) Util.sortByValueAndGetKeys(score, false, new LongArrayList());
}
 
Example #2
Source File: SalsaSubgraphInternalState.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * Get a new instance of a fresh internal state that can store a reusable subgraph.
 *
 * @param leftIndexedBipartiteGraph        is the underlying graph that SALSA runs on
 * @param salsaStats                       is the stats object to use
 * @param expectedNodesToHit               is the number of nodes the random walk is expected to
 *                                         hit
 * @param expectedNumLeftNodes             is the expected size of the seed set
 */
public SalsaSubgraphInternalState(
    LeftIndexedBipartiteGraph leftIndexedBipartiteGraph,
    SalsaStats salsaStats,
    int expectedNodesToHit,
    int expectedNumLeftNodes) {
  super(salsaStats, expectedNodesToHit);
  this.leftIndexedBipartiteGraph = leftIndexedBipartiteGraph;
  this.subgraphLeftNodes = new long[expectedNumLeftNodes];
  this.subgraphLeftNodeDegree = new int[expectedNumLeftNodes];
  this.subgraphEdgesArray = new long[expectedNodesToHit];
  this.subgraphEdgeMetadataArray = new long[expectedNodesToHit];
  this.subgraphEdgeTypesArray = new byte[expectedNodesToHit];
  this.subgraphRightNodeDegreeReciprocal = new Long2DoubleOpenHashMap(expectedNodesToHit);
}
 
Example #3
Source File: PageRank.java    From Neo4jSNA with Apache License 2.0 5 votes vote down vote up
public PageRank(GraphDatabaseService g) {
	rankMap = new Long2DoubleOpenHashMap();
	try(Transaction tx = g.beginTx()) {
		for(@SuppressWarnings("unused") Node n : GlobalGraphOperations.at(g).getAllNodes())
			nodeCount += 1;
		tx.success();
	}
	this.firstMember = ( 1.0 - this.dampingFactor ) / this.nodeCount;
}
 
Example #4
Source File: LabelPropagationMapStorage.java    From graph_processing with MIT License 5 votes vote down vote up
@Override
public void compute(String label, String type, int iterations) {
    RelationshipType relationshipType = RelationshipType.withName(type);
    labelMap = new Long2DoubleOpenHashMap();
    boolean done = false;
    int iteration = 0;
    try ( Transaction tx = db.beginTx()) {
        ResourceIterator<Node> nodes = db.findNodes(DynamicLabel.label(label));
        while (nodes.hasNext()) {
            Node node = nodes.next();
            labelMap.put(node.getId(), node.getId());
        }

        while (!done) {
            done = true;
            iteration++;

            for( Relationship relationship : db.getAllRelationships()) {
                if (relationship.isType(relationshipType)) {
                    long x = relationship.getStartNode().getId();
                    long y = relationship.getEndNode().getId();
                    if (x == y) { continue; }
                    if (labelMap.get(x) > labelMap.get(y)){
                        labelMap.put(x, labelMap.get(y));
                        done = false;
                    } else if (labelMap.get(x) < labelMap.get(y)) {
                        labelMap.put(y, labelMap.get(x));
                        done = false;
                    }
                }
            }

            if (iteration > iterations) {
                done = true;
            }
        }
    }
}
 
Example #5
Source File: SalsaIterationsTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  long queryNode = 1;
  BipartiteGraph bipartiteGraph = BipartiteGraphTestHelper.buildSmallTestBipartiteGraph();
  Long2DoubleMap seedSetWeights = new Long2DoubleOpenHashMap(3);
  seedSetWeights.put(2, 10.0);
  seedSetWeights.put(3, 1.0);
  LongSet toBeFiltered = new LongOpenHashSet(new long[]{8});
  int numIterations = 5;
  double resetProbability = 0.3;
  int numResults = 3;
  int numRandomWalks = 1000;
  int maxSocialProofSize = 2;
  double queryNodeWeightFraction = 0.9;
  int expectedNodesToHit = numRandomWalks * numIterations / 2;
  random = new Random(541454153145614L);
  socialProofTypes = new byte[]{0, 1};
  SalsaStats salsaStats = new SalsaStats();
  ResultFilterChain resultFilterChain = new ResultFilterChain(Lists.newArrayList(
      new RequestedSetFilter(new NullStatsReceiver()),
      new DirectInteractionsFilter(bipartiteGraph, new NullStatsReceiver())
  ));

  salsaRequest =
      new SalsaRequestBuilder(queryNode)
          .withLeftSeedNodes(seedSetWeights)
          .withToBeFiltered(toBeFiltered)
          .withMaxNumResults(numResults)
          .withResetProbability(resetProbability)
          .withMaxRandomWalkLength(numIterations)
          .withNumRandomWalks(numRandomWalks)
          .withMaxSocialProofSize(maxSocialProofSize)
          .withValidSocialProofTypes(socialProofTypes)
          .withQueryNodeWeightFraction(queryNodeWeightFraction)
          .withResultFilterChain(resultFilterChain)
          .build();

  salsaInternalState = new SalsaInternalState(
      bipartiteGraph, salsaStats, expectedNodesToHit);
  salsaInternalState.resetWithRequest(salsaRequest);

  salsaRequestSmallSeed =
      new SalsaRequestBuilder(queryNode)
          // This seed set should be ignored
          .withLeftSeedNodes(new Long2DoubleOpenHashMap(new long[]{5}, new double[]{1.0}))
          .withToBeFiltered(toBeFiltered)
          .withMaxNumResults(numResults)
          .withResetProbability(resetProbability)
          .withMaxRandomWalkLength(numIterations)
          .withNumRandomWalks(numRandomWalks)
          .withMaxSocialProofSize(maxSocialProofSize)
          .withValidSocialProofTypes(new byte[]{0, 1})
          .withQueryNodeWeightFraction(queryNodeWeightFraction)
          .withResultFilterChain(resultFilterChain)
          .build();

  salsaInternalStateSmallSeed = new SalsaInternalState(
      bipartiteGraph, salsaStats, expectedNodesToHit);
  salsaInternalStateSmallSeed.resetWithRequest(salsaRequestSmallSeed);
}
 
Example #6
Source File: RandomMultiGraphNeighborsTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testRandomMultiGraphNeighborsWithGraph() throws Exception {
  BipartiteGraph bipartiteGraph = BipartiteGraphTestHelper.buildSmallTestBipartiteGraphSegment();
  int maxNumNeighbors = 1000;
  int maxNumResults = 3;
  long randomSeed = 5298057403198457L;
  Long2DoubleMap leftSeedNodesWithWeight = new Long2DoubleOpenHashMap(3);
  leftSeedNodesWithWeight.put(1, 0.4);
  leftSeedNodesWithWeight.put(2, 0.3);
  leftSeedNodesWithWeight.put(3, 0.3);

  RandomMultiGraphNeighborsRequest neighborRequest = new RandomMultiGraphNeighborsRequest(
      leftSeedNodesWithWeight,
      maxNumNeighbors,
      maxNumResults);

  final List<NeighborInfo> expectedResults =
      Lists.newArrayList(
          new NeighborInfo(5, 0.206, 3),
          new NeighborInfo(2, 0.155, 2),
          new NeighborInfo(10, 0.117, 2)
      );

  // Should be in sorted order of weight
  RandomMultiGraphNeighbors randomMultiGraphNeighbors = new RandomMultiGraphNeighbors(
      bipartiteGraph,
      new NullStatsReceiver());
  Random random = new Random(randomSeed);
  RandomMultiGraphNeighborsResponse neighborResponse =
      randomMultiGraphNeighbors.getRandomMultiGraphNeighbors(neighborRequest, random);
  List<NeighborInfo> results =
      Lists.newArrayList(neighborResponse.getNeighborNodes());

  assertEquals(expectedResults, results);

  final List<NeighborInfo> expectedFilteredResults =
      Lists.newArrayList(
          new NeighborInfo(5, 0.189, 3)
      );

  // also check whether minimum result degree filter is enforced
  MinEngagementFilter minEngagementFilter = new MinEngagementFilter(3, bipartiteGraph,
      new NullStatsReceiver());
  ArrayList<RelatedTweetFilter> filters = new ArrayList<RelatedTweetFilter>(1);
  filters.add(minEngagementFilter);
  RelatedTweetFilterChain filterChain = new RelatedTweetFilterChain(filters);

  RandomMultiGraphNeighborsResponse filteredResponse =
      randomMultiGraphNeighbors.getRandomMultiGraphNeighbors(
        neighborRequest, random, filterChain);
  List<NeighborInfo> filteredResults =
      Lists.newArrayList(filteredResponse.getNeighborNodes());

  assertEquals(expectedFilteredResults, filteredResults);
}
 
Example #7
Source File: RandomMultiGraphNeighborsTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testRandomMultiGraphNeighborsWithLargeGraph() throws Exception {
  int maxNumNeighbors = 100000;
  int maxNumResults = 3;
  int leftSize = 100;
  int rightSize = 1000;
  double edgeProbability = 0.3;
  long randomSeed = 5298057403198457L;
  Random random = new Random(randomSeed);

  BipartiteGraph bipartiteGraph = BipartiteGraphTestHelper.buildRandomBipartiteGraph(
      leftSize, rightSize, edgeProbability, random);
  Long2DoubleMap leftSeedNodesWithWeight = new Long2DoubleOpenHashMap(3);
  leftSeedNodesWithWeight.put(1, 0.4);
  leftSeedNodesWithWeight.put(2, 0.3);
  leftSeedNodesWithWeight.put(3, 0.2);
  leftSeedNodesWithWeight.put(4, 0.1);

  RandomMultiGraphNeighborsRequest neighborRequest = new RandomMultiGraphNeighborsRequest(
      leftSeedNodesWithWeight,
      maxNumNeighbors,
      maxNumResults);

  final List<NeighborInfo> expectedResults =
      Lists.newArrayList(
          new NeighborInfo(87, 0.00351, 32),
          new NeighborInfo(157, 0.0033, 26),
          new NeighborInfo(620, 0.00328, 30)
      );

  // Should be in sorted order of weight
  RandomMultiGraphNeighbors randomMultiGraphNeighbors = new RandomMultiGraphNeighbors(
      bipartiteGraph,
      new NullStatsReceiver());

  RandomMultiGraphNeighborsResponse neighborResponse =
      randomMultiGraphNeighbors.getRandomMultiGraphNeighbors(neighborRequest, random);
  List<NeighborInfo> results =
      Lists.newArrayList(neighborResponse.getNeighborNodes());

  assertEquals(expectedResults, results);

  final List<NeighborInfo> expectedFilteredResults =
      Lists.newArrayList(
          new NeighborInfo(927, 0.00183, 45)
      );

  // also check whether minimum result degree filter is enforced
  MinEngagementFilter minEngagementFilter = new MinEngagementFilter(45, bipartiteGraph,
      new NullStatsReceiver());
  ArrayList<RelatedTweetFilter> filters = new ArrayList<RelatedTweetFilter>(1);
  filters.add(minEngagementFilter);
  RelatedTweetFilterChain filterChain = new RelatedTweetFilterChain(filters);

  RandomMultiGraphNeighborsResponse filteredResponse =
      randomMultiGraphNeighbors.getRandomMultiGraphNeighbors(
        neighborRequest, random, filterChain);
  List<NeighborInfo> filteredResults =
      Lists.newArrayList(filteredResponse.getNeighborNodes());

  assertEquals(expectedFilteredResults, filteredResults);
}