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

The following examples show how to use it.unimi.dsi.fastutil.longs.Long2DoubleMap. 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: TopSecondDegreeByCountRequest.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
/**
 * @param queryNode                 is the query node for running TopSecondDegreeByCount
 * @param leftSeedNodesWithWeight   is the set of seed nodes and their weights to use for
 *                                  TopSecondDegreeByCount
 * @param toBeFiltered              is the set of RHS nodes to be filtered from the output
 * @param maxSocialProofTypeSize    is the number of social proof types in the graph
 * @param socialProofTypes          Social proof types, masked into a byte array
 * @param maxRightNodeAgeInMillis   Max right node age in millisecond, such as tweet age
 * @param maxEdgeAgeInMillis        Max edge age in millisecond such as reply edge age
 * @param resultFilterChain         Filter chain to be applied after recommendation computation
 */
public TopSecondDegreeByCountRequest(
  long queryNode,
  Long2DoubleMap leftSeedNodesWithWeight,
  LongSet toBeFiltered,
  int maxSocialProofTypeSize,
  byte[] socialProofTypes,
  long maxRightNodeAgeInMillis,
  long maxEdgeAgeInMillis,
  ResultFilterChain resultFilterChain) {
  super(queryNode, toBeFiltered, socialProofTypes);
  this.leftSeedNodesWithWeight = leftSeedNodesWithWeight;
  this.maxSocialProofTypeSize = maxSocialProofTypeSize;
  this.maxRightNodeAgeInMillis = maxRightNodeAgeInMillis;
  this.maxEdgeAgeInMillis = maxEdgeAgeInMillis;
  this.resultFilterChain = resultFilterChain;
}
 
Example #2
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
@Test
public void testTweetSocialProofWithInvalidUnfavorites() {
  // Test cases where unfavorite social proof is the only social proof type. Nothing will return
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph graph =
    BipartiteGraphTestHelper.buildTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithUnfavorite();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user1, user2, user3, user4, user5, user6, user7,
      user8, user9, user10, user11, user12, user13, user14},
    new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
  LongSet tweets = new LongArraySet(
    new long[] {tweet1, tweet2, tweet3, tweet4, tweet5, tweet6, tweet7,
      tweet8, tweet9, tweet10, tweet11, tweet12, tweet13});

  byte[] validSocialProofTypes = new byte[] {UNFAVORITE_SOCIAL_PROOF_TYPE};

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets, seedsMap, validSocialProofTypes);
  HashMap<Long, SocialProofResult> results = new HashMap<>();
  new TweetSocialProofGenerator(graph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
    results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertEquals(0, results.size());
}
 
Example #3
Source File: TopSecondDegreeByCountRequestForMoment.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
/**
 * @param queryNode                 is the query node for running TopSecondDegreeByCountRequestForMoment
 * @param leftSeedNodesWithWeight   is the set of seed nodes and their weights to use for calculation
 * @param toBeFiltered              is the set of RHS nodes to be filtered from the output
 * @param maxNumResults             is the maximum number of recommendations returned in the response
 * @param maxNumSocialProofs        is the maximum number of social proofs per recommendation
 * @param maxSocialProofTypeSize    is the number of social proof types in the graph
 * @param minUserPerSocialProof     for each social proof, require a minimum number of users to be valid
 * @param socialProofTypes          is the list of valid social proofs, (i.e. Create, Like etc)
 * @param maxRightNodeAgeInMillis   Max right node age in millisecond, such as moment age
 * @param maxEdgeAgeInMillis        Max edge age in millisecond such as like edge age
 * @param resultFilterChain         is the chain of filters to be applied
 */
public TopSecondDegreeByCountRequestForMoment(
  long queryNode,
  Long2DoubleMap leftSeedNodesWithWeight,
  LongSet toBeFiltered,
  int maxNumResults,
  int maxNumSocialProofs,
  int maxSocialProofTypeSize,
  Map<Byte, Integer> minUserPerSocialProof,
  byte[] socialProofTypes,
  long maxRightNodeAgeInMillis,
  long maxEdgeAgeInMillis,
  ResultFilterChain resultFilterChain) {
  super(queryNode, leftSeedNodesWithWeight, toBeFiltered, maxSocialProofTypeSize,
      socialProofTypes, maxRightNodeAgeInMillis, maxEdgeAgeInMillis, resultFilterChain);
  this.maxNumResults = maxNumResults;
  this.maxNumSocialProofs = maxNumSocialProofs;
  this.minUserPerSocialProof = minUserPerSocialProof;
}
 
Example #4
Source File: TopSecondDegreeByCountRequestForUser.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
/**
 * @param queryNode                 is the query node for running TopSecondDegreeByCountForUser
 * @param leftSeedNodesWithWeight   is the set of seed nodes and their weights to use for calculation
 * @param toBeFiltered              is the list of users to be excluded from recommendations
 * @param maxNumResults             is the maximum number of recommendations returned in the response
 * @param maxNumSocialProofs        is the maximum number of social proofs per recommendation
 * @param maxSocialProofTypeSize    is the number of social proof types in the graph
 * @param minUserPerSocialProof     for each social proof, require a minimum number of users to be valid
 * @param socialProofTypes          is the list of valid social proofs, (i.e, Follow, Mention, Mediatag)
 * @param maxRightNodeAgeInMillis   is the max right node age in millisecond, such as tweet age
 * @param maxEdgeAgeInMillis        is the max edge age in millisecond such as reply edge age
 * @param resultFilterChain         is the chain of filters to be applied
 */
public TopSecondDegreeByCountRequestForUser(
  long queryNode,
  Long2DoubleMap leftSeedNodesWithWeight,
  LongSet toBeFiltered,
  int maxNumResults,
  int maxNumSocialProofs,
  int maxSocialProofTypeSize,
  Map<Byte, Integer> minUserPerSocialProof,
  byte[] socialProofTypes,
  long maxRightNodeAgeInMillis,
  long maxEdgeAgeInMillis,
  ResultFilterChain resultFilterChain) {
  super(queryNode, leftSeedNodesWithWeight, toBeFiltered, maxSocialProofTypeSize,
      socialProofTypes, maxRightNodeAgeInMillis, maxEdgeAgeInMillis, resultFilterChain);
  this.maxNumResults = maxNumResults;
  this.maxNumSocialProofs = maxNumSocialProofs;
  this.minUserPerSocialProof = minUserPerSocialProof;
}
 
Example #5
Source File: RandomMultiGraphNeighbors.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
/**
 * Construct a index array and an alias table given the LHS seed nodes with weights.
 * The probability for a LHS seed node u to be sampled should be proportional to
 * degree(u) * Weight(u)
 *
 * @param seedsWithWeights          the LHS seed nodes with weights
 * @param indexArray                the index array for the seeds
 * @param aliasTableArray           the alias table used for sampling from the seeds
 */
private void constructAliasTableArray(
    Long2DoubleMap seedsWithWeights,
    long[] indexArray,
    int[] aliasTableArray) {
  int index = 0;
  int averageWeight = 0;
  for (Long2DoubleMap.Entry entry: seedsWithWeights.long2DoubleEntrySet()) {
    long seed = entry.getLongKey();
    double seedWeight = entry.getDoubleValue();
    indexArray[index] = seed;
    int finalWeight = (int) (Math.round(MULTIPER_FOR_ALIASTABLE * seedWeight
      * bipartiteGraph.getLeftNodeDegree(seed)));
    IntArrayAliasTable.setEntry(aliasTableArray, index, index);
    IntArrayAliasTable.setWeight(aliasTableArray, index, finalWeight);
    averageWeight += finalWeight;
    index++;
  }
  // finally set the size and weight
  IntArrayAliasTable.setAliasTableSize(aliasTableArray, index);
  IntArrayAliasTable.setAliasTableAverageWeight(aliasTableArray, averageWeight / index);
  // now we can construct the alias table
  AliasTableUtil.constructAliasTable(aliasTableArray);
}
 
Example #6
Source File: SocialProofGenerator.java    From GraphJet with Apache License 2.0 6 votes vote down vote up
@Override
public SocialProofResponse computeRecommendations(SocialProofRequest request, Random rand) {
  reset();

  Long2DoubleMap leftSeedNodesWithWeight = request.getLeftSeedNodesWithWeight();
  LongSet rightNodeIds = request.getRightNodeIds();

  if (shouldRemoveUnfavoritedEdges(request)) {
    collectRightNodeInfo(
      leftSeedNodesWithWeight, rightNodeIds, appendUnfavoriteType(request.getSocialProofTypes()));
    return removeUnfavoritesAndGenerateRecommendationsFromNodeInfo();
  } else {
    collectRightNodeInfo(leftSeedNodesWithWeight, rightNodeIds, request.getSocialProofTypes());
    return generateRecommendationFromNodeInfo();
  }
}
 
Example #7
Source File: RandomMultiGraphNeighborsRequest.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new request.
 *
 * @param leftSeedNodesWithWeight          is the seed set to use to supplement the query node
 * @param maxNumSamples                    is the maximum number of samples
 * @param maxResults                       is the maximum number of results to return
 */
public RandomMultiGraphNeighborsRequest(
    Long2DoubleMap leftSeedNodesWithWeight,
    int maxNumSamples,
    int maxResults) {
  this.leftSeedNodesWithWeight = leftSeedNodesWithWeight;
  this.maxNumSamples = maxNumSamples;
  this.maxNumResults = maxResults;
}
 
Example #8
Source File: SalsaRequest.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * The constructor should only be called via {@link SalsaRequestBuilder}.
 * @param queryNode                 is the query node for running SALSA
 * @param leftSeedNodesWithWeight   is the set of seed nodes to use for SALSA, with weights being
 *                                  the proportion of random walks to start here. We do NOT assume
 *                                  that the queryNode is added to this.
 * @param toBeFiltered              is the set of RHS nodes to be filtered from the output
 * @param numRandomWalks            is the total number of random walks to run
 * @param maxRandomWalkLength       is the maximum length of a random walk
 * @param resetProbability          is the probability of reset in SALSA. Note that reset is only
 *                                  done on backward iterations.
 * @param maxNumResults             is the maximum number of results that SALSA will return
 * @param maxSocialProofSize        is the maximum size of social proof per type to return. Set
 *                                  this to 0 to return no social proof
 * @param maxSocialProofTypeSize    is the maximum size of social proof types in the graph.
 * @param socialProofTypes          is the social proof types to return
 * @param queryNodeWeightFraction   is the relative proportion of random walks to start at the
 *                                  queryNode in the first iteration. This parameter is only used
 * @param removeCustomizedBitsNodes removes tweets with metadata information embedded into top
 *                                  four bits
 * @param resultFilterChain         is the chain of filters to be applied
 */
protected SalsaRequest(
    long queryNode,
    Long2DoubleMap leftSeedNodesWithWeight,
    LongSet toBeFiltered,
    int numRandomWalks,
    int maxRandomWalkLength,
    double resetProbability,
    int maxNumResults,
    int maxSocialProofSize,
    int maxSocialProofTypeSize,
    byte[] socialProofTypes,
    double queryNodeWeightFraction,
    boolean removeCustomizedBitsNodes,
    ResultFilterChain resultFilterChain) {
  super(queryNode, toBeFiltered, socialProofTypes);
  this.leftSeedNodesWithWeight = leftSeedNodesWithWeight;
  this.numRandomWalks = numRandomWalks;
  this.maxRandomWalkLength = maxRandomWalkLength;
  this.resetProbability = resetProbability;
  this.maxNumResults = maxNumResults;
  this.maxSocialProofSize = maxSocialProofSize;
  this.maxSocialProofTypeSize = maxSocialProofTypeSize;
  this.queryNodeWeightFraction = queryNodeWeightFraction;
  this.removeCustomizedBitsNodes = removeCustomizedBitsNodes;
  this.resultFilterChain = resultFilterChain;
}
 
Example #9
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
@Test
public void testTweetSocialProofsWithInvalidType() {
  // Test cases where the requested social proof types yield no results
  LeftIndexedPowerLawMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestLeftIndexedPowerLawMultiSegmentBipartiteGraphWithEdgeTypes();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user1, user2}, new double[] {1.0, 0.5});
  LongSet tweets = new LongArraySet(new long[] {tweet2, tweet3, tweet4, tweet5, tweet6, tweet7});

  // In the graph there are no valid social proofs corresponding to these types
  byte[] validSocialProofTypes = new byte[] {
    AUTHOR_SOCIAL_PROOF_TYPE,
    IS_MENTIONED_SOCIAL_PROOF_TYPE
  };

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets,
    seedsMap,
    validSocialProofTypes
  );
  HashMap<Long, SocialProofResult> results = new HashMap<>();

  new TweetSocialProofGenerator(bipartiteGraph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
    results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertTrue(results.isEmpty());
}
 
Example #10
Source File: SocialProofRequest.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * Create a social proof request.
 *
 * @param rightNodeIds        is the set of right nodes to query for social proof.
 * @param weightedSeedNodes   is the set of left nodes to be used as social proofs.
 * @param socialProofTypes    is the social proof types to return.
 */
public SocialProofRequest(
  LongSet rightNodeIds,
  Long2DoubleMap weightedSeedNodes,
  byte[] socialProofTypes
) {
  super(0, EMPTY_SET, socialProofTypes);
  this.leftSeedNodesWithWeight = weightedSeedNodes;
  this.rightNodeIds = rightNodeIds;
}
 
Example #11
Source File: SocialProofGenerator.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * Collect social proofs for a given {@link SocialProofRequest}.
 *
 * @param leftSeedNodesWithWeight Engagement edges from these left nodes are iterated and collected
 * @param rightNodeIds            Right nodes for which we want to generate social proofs
 * @param validSocialProofTypes   Social proof types that we are interested in
 */
private void collectRightNodeInfo(
  Long2DoubleMap leftSeedNodesWithWeight, LongSet rightNodeIds, byte[] validSocialProofTypes) {
  ByteSet socialProofTypeSet = new ByteArraySet(validSocialProofTypes);

  // Iterate through the set of left node seeds with weights.
  // For each left node, go through its edges and collect the engagements on the right nodes
  for (Long2DoubleMap.Entry entry: leftSeedNodesWithWeight.long2DoubleEntrySet()) {
    long leftNode = entry.getLongKey();
    EdgeIterator edgeIterator = leftIndexedBipartiteGraph.getLeftNodeEdges(leftNode);
    if (edgeIterator == null) {
      continue;
    }

    int numEdgePerNode = 0;
    double weight = entry.getDoubleValue();
    seenEdgesPerNode.clear();

    // Sequentially iterate through the latest MAX_EDGES_PER_NODE edges per node
    while (edgeIterator.hasNext() && numEdgePerNode++ < MAX_EDGES_PER_NODE) {
      long rightNode = idMask.restore(edgeIterator.nextLong());
      byte edgeType = edgeIterator.currentEdgeType();

      boolean hasSeenRightNodeFromEdge =
        seenEdgesPerNode.containsKey(rightNode) && seenEdgesPerNode.get(rightNode) == edgeType;

      boolean isValidEngagement = rightNodeIds.contains(rightNode) &&
        socialProofTypeSet.contains(edgeType);

      if (hasSeenRightNodeFromEdge || !isValidEngagement) {
        continue;
      }
      updateVisitedRightNodes(leftNode, rightNode, edgeType, weight);
    }
  }
}
 
Example #12
Source File: TopSecondDegreeByCount.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
private void collectRightNodeInfo(Request request) {
  for (Long2DoubleMap.Entry entry: request.getLeftSeedNodesWithWeight().long2DoubleEntrySet()) {
    long leftNode = entry.getLongKey();
    EdgeIterator edgeIterator = leftIndexedBipartiteGraph.getLeftNodeEdges(leftNode);
    if (edgeIterator == null) {
      continue;
    }

    int numEdgesPerNode = 0;
    double weight = entry.getDoubleValue();
    seenEdgesPerNode.clear();
    // Sequentially iterating through the latest MAX_EDGES_PER_NODE edges per node
    while (edgeIterator.hasNext() && numEdgesPerNode++ < RecommendationRequest.MAX_EDGES_PER_NODE) {
      long rightNode = edgeIterator.nextLong();
      byte edgeType = edgeIterator.currentEdgeType();
      long edgeMetadata = edgeIterator.currentMetadata();

      boolean hasSeenRightNodeFromEdge =
        seenEdgesPerNode.containsKey(rightNode) && seenEdgesPerNode.get(rightNode) == edgeType;

      if (!hasSeenRightNodeFromEdge
        && isEdgeUpdateValid(request, rightNode, edgeType, edgeMetadata)) {
        seenEdgesPerNode.put(rightNode, edgeType);
        updateNodeInfo(
          leftNode,
          rightNode,
          edgeType,
          edgeMetadata,
          weight,
          edgeIterator,
          request.getMaxSocialProofTypeSize());
      }
    }
  }
}
 
Example #13
Source File: TopSecondDegreeByCountRequestForTweet.java    From GraphJet with Apache License 2.0 5 votes vote down vote up
/**
 * Construct a TopSecondDegreeByCount algorithm runner for tweet related recommendations.
 * @param queryNode                 is the query node for running TopSecondDegreeByCountForTweet
 * @param leftSeedNodesWithWeight   is the set of seed nodes and their weights to use for
 *                                  TopSecondDegreeByCountForTweet
 * @param toBeFiltered              is the set of RHS nodes to be filtered from the output
 * @param recommendationTypes       is the list of recommendation types requested by clients
 * @param maxNumResultsByRecType    is the maximum number of results requested by clients per recommendation type
 * @param maxSocialProofTypeSize    is the maximum size of social proof types in the graph
 * @param maxUserSocialProofSize    is the maximum size of user social proof per type to return
 *                                  Set this to 0 to return no social proof
 * @param maxTweetSocialProofSize   is the maximum size of tweet social proof per user to return
 * @param minUserSocialProofSizes   is the minimum size of user social proof per recommendation
 *                                  type to return
 * @param socialProofTypes          is the social proof types to return
 * @param resultFilterChain         is the chain of filters to be applied
 * @param socialProofTypeUnions     is the set of groups of social proofs to be combined
 */
public TopSecondDegreeByCountRequestForTweet(
  long queryNode,
  Long2DoubleMap leftSeedNodesWithWeight,
  LongSet toBeFiltered,
  Set<RecommendationType> recommendationTypes,
  Map<RecommendationType, Integer> maxNumResultsByRecType,
  int maxSocialProofTypeSize,
  int maxUserSocialProofSize,
  int maxTweetSocialProofSize,
  Map<RecommendationType, Integer> minUserSocialProofSizes,
  byte[] socialProofTypes,
  long maxRightNodeAgeInMillis,
  long maxEdgeAgeInMillis,
  ResultFilterChain resultFilterChain,
  Set<byte[]> socialProofTypeUnions
) {
  super(
    queryNode,
    leftSeedNodesWithWeight,
    toBeFiltered,
    maxSocialProofTypeSize,
    socialProofTypes,
    maxRightNodeAgeInMillis,
    maxEdgeAgeInMillis,
    resultFilterChain);
  this.recommendationTypes = recommendationTypes;
  this.maxNumResultsByType = maxNumResultsByRecType;
  this.maxUserSocialProofSize = maxUserSocialProofSize;
  this.maxTweetSocialProofSize = maxTweetSocialProofSize;
  this.minUserSocialProofSizes = minUserSocialProofSizes;
  this.socialProofTypeUnions = socialProofTypeUnions;
}
 
Example #14
Source File: PageRank.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
@Override
public Long2DoubleMap getResult() {
	return rankMap;
}
 
Example #15
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTweetSocialProofs() {
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraph();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user2, user3}, new double[] {1.0, 0.5});
  LongSet tweets = new LongArraySet(new long[] {tweet2, tweet3, tweet4, tweet5});

  byte[] validSocialProofTypes = new byte[] {
    CLICK_SOCIAL_PROOF_TYPE,
    FAVORITE_SOCIAL_PROOF_TYPE,
    RETWEET_SOCIAL_PROOF_TYPE,
    REPLY_SOCIAL_PROOF_TYPE,
    AUTHOR_SOCIAL_PROOF_TYPE,
  };

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets,
    seedsMap,
    validSocialProofTypes
  );
  HashMap<Long, SocialProofResult> results = new HashMap<>();

  new TweetSocialProofGenerator(bipartiteGraph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
      results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertEquals(results.size(), 2);

  Byte2ObjectMap<LongSet> expectedProofs;
  SocialProofResult expected;

  // Test social proofs for tweet 2
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(CLICK_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user3}));
  expected = new SocialProofResult(tweet2, expectedProofs, 0.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet2));

  // Test social proofs for tweet 5
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(CLICK_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user2, user3}));
  expected = new SocialProofResult(tweet5, expectedProofs, 1.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet5));
}
 
Example #16
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTweetSocialProofs2() {
  // Run on another test graph
  LeftIndexedPowerLawMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestLeftIndexedPowerLawMultiSegmentBipartiteGraphWithEdgeTypes();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user1, user2}, new double[] {1.0, 0.5});
  LongSet tweets = new LongArraySet(new long[] {tweet2, tweet3, tweet4, tweet5, tweet6, tweet7, tweet8});

  byte[] validSocialProofTypes = new byte[] {
    FAVORITE_SOCIAL_PROOF_TYPE,
    RETWEET_SOCIAL_PROOF_TYPE
  };

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets,
    seedsMap,
    validSocialProofTypes
  );
  HashMap<Long, SocialProofResult> results = new HashMap<>();

  new TweetSocialProofGenerator(bipartiteGraph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
    results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertEquals(5, results.size());

  Byte2ObjectMap<LongSet> expectedProofs;
  SocialProofResult expected;

  // Test social proofs for tweet 3
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user1, user2}));
  expected = new SocialProofResult(tweet3, expectedProofs, 1.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet3));

  // Test social proofs for tweet 4
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user1}));
  expected = new SocialProofResult(tweet4, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet4));

  // Test social proofs for tweet 6
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user2}));
  expected = new SocialProofResult(tweet6, expectedProofs, 0.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet6));

  // Test social proofs for tweet 7
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user2}));
  expected = new SocialProofResult(tweet7, expectedProofs, 0.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet7));

  // Test social proofs for tweet 8
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user1}));
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user2}));
  expected = new SocialProofResult(tweet8, expectedProofs, 1.5, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet8));

}
 
Example #17
Source File: MomentSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testComputeRecommendations() throws Exception {
  LeftIndexedMultiSegmentBipartiteGraph bipartiteGraph = BipartiteGraphTestHelper.
    buildSmallTestLeftIndexedPowerLawMultiSegmentBipartiteGraphWithEdgeTypes();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{2, 3}, new double[]{1.0, 0.5});
  LongSet moments = new LongArraySet(new long[]{2, 3, 4, 5});

  byte[] validSocialProofs = new byte[]{0, 1, 2};
  long randomSeed = 918324701982347L;
  Random random = new Random(randomSeed);

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    moments,
    seedsMap,
    validSocialProofs
  );

  SocialProofResponse socialProofResponse = new MomentSocialProofGenerator(
    bipartiteGraph
  ).computeRecommendations(socialProofRequest, random);

  List<RecommendationInfo> socialProofResults =
      Lists.newArrayList(socialProofResponse.getRankedRecommendations());

  for (RecommendationInfo recommendationInfo: socialProofResults) {
    SocialProofResult socialProofResult = (SocialProofResult) recommendationInfo;
    Long momentId = socialProofResult.getNode();
    Byte2ObjectMap<LongSet> socialProofs = socialProofResult.getSocialProof();

    if (momentId == 2 || momentId == 4) {
      assertEquals(socialProofs.isEmpty(), true);
    } else if (momentId == 3) {
      assertEquals(socialProofs.get((byte) 1).size(), 2);
      assertEquals(socialProofs.get((byte) 1).contains(2), true);
      assertEquals(socialProofs.get((byte) 1).contains(3), true);
    } else if (momentId == 5) {
      assertEquals(socialProofs.get((byte) 0).size(), 1);
      assertEquals(socialProofs.get((byte) 0).contains(2), true);
    }
  }
}
 
Example #18
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTweetSocialProofsWithUnfavorites() {
  // Test graph with favorite edges that are potentially unfavorited later
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph graph =
    BipartiteGraphTestHelper.buildTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithUnfavorite();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user1, user2, user3, user4, user5, user6, user7,
      user8, user9, user10, user11, user12, user13, user14},
    new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
  LongSet tweets = new LongArraySet(
    new long[] {tweet1, tweet2, tweet3, tweet4, tweet5, tweet6, tweet7,
      tweet8, tweet9, tweet10, tweet11, tweet12, tweet13});

  byte[] validSocialProofTypes = new byte[] {FAVORITE_SOCIAL_PROOF_TYPE};

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets, seedsMap, validSocialProofTypes);
  HashMap<Long, SocialProofResult> results = new HashMap<>();
  new TweetSocialProofGenerator(graph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
    results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertEquals(7, results.size());

  Byte2ObjectMap<LongSet> expectedProofs;
  SocialProofResult expected;

  // Test social proofs for tweet 1
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user1}));
  expected = new SocialProofResult(tweet1, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet1));

  // Test social proofs for tweet 5
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user5}));
  expected = new SocialProofResult(tweet5, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet5));

  // Test social proofs for tweet 7
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user7}));
  expected = new SocialProofResult(tweet7, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet7));

  // Test social proofs for tweet 8
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user8, user9}));
  expected = new SocialProofResult(tweet8, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet8));

  // Test social proofs for tweet 9
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user9, user10}));
  expected = new SocialProofResult(tweet9, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet9));

  // Test social proofs for tweet 10
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user10}));
  expected = new SocialProofResult(tweet10, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet10));

  // Test social proofs for tweet 13
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user13}));
  expected = new SocialProofResult(tweet13, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet13));
}
 
Example #19
Source File: TweetSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTweetSocialProofWithRetweetAndUnfavorites() {
  // Test cases where unfavorite tweets are also retweeted
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph graph =
    BipartiteGraphTestHelper.buildTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithUnfavorite();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(
    new long[] {user1, user2, user3, user4, user5, user6, user7,
      user8, user9, user10, user11, user12, user13, user14},
    new double[] {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1});
  LongSet tweets = new LongArraySet(
    new long[] {tweet1, tweet2, tweet3, tweet4, tweet5, tweet6, tweet7,
      tweet8, tweet9, tweet10, tweet11, tweet12, tweet13});

  byte[] validSocialProofTypes = new byte[] {FAVORITE_SOCIAL_PROOF_TYPE, RETWEET_SOCIAL_PROOF_TYPE};

  SocialProofRequest socialProofRequest = new SocialProofRequest(
    tweets, seedsMap, validSocialProofTypes);
  HashMap<Long, SocialProofResult> results = new HashMap<>();
  new TweetSocialProofGenerator(graph)
    .computeRecommendations(socialProofRequest, new Random(0))
    .getRankedRecommendations().forEach( recInfo ->
    results.put(((SocialProofResult)recInfo).getNode(), (SocialProofResult)recInfo));

  assertEquals(10, results.size());

  Byte2ObjectMap<LongSet> expectedProofs;
  SocialProofResult expected;

  // Test social proofs for tweet 1
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user1}));
  expected = new SocialProofResult(tweet1, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet1));

  // Test social proofs for tweet 2
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user2}));
  expected = new SocialProofResult(tweet2, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet2));

  // Test social proofs for tweet 5
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user5}));
  expected = new SocialProofResult(tweet5, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet5));

  // Test social proofs for tweet 7
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user7}));
  expected = new SocialProofResult(tweet7, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet7));

  // Test social proofs for tweet 8
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user8, user9}));
  expected = new SocialProofResult(tweet8, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet8));

  // Test social proofs for tweet 9
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user9, user10}));
  expected = new SocialProofResult(tweet9, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet9));

  // Test social proofs for tweet 10
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user10}));
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user11}));
  expected = new SocialProofResult(tweet10, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet10));

  // Test social proofs for tweet 11
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user11}));
  expected = new SocialProofResult(tweet11, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet11));

  // Test social proofs for tweet 12
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user12}));
  expected = new SocialProofResult(tweet12, expectedProofs, 1, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet12));

  // Test social proofs for tweet 13
  expectedProofs = new Byte2ObjectArrayMap<>();
  expectedProofs.put(FAVORITE_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user13}));
  expectedProofs.put(RETWEET_SOCIAL_PROOF_TYPE, new LongArraySet(new long[] {user14}));
  expected = new SocialProofResult(tweet13, expectedProofs, 2, RecommendationType.TWEET);
  assertEqualSocialProofResults(expected, results.get(tweet13));
}
 
Example #20
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 #21
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 #22
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);
}
 
Example #23
Source File: Neo4jSNAMain.java    From Neo4jSNA with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
       String zipFile = "data/cineasts_12k_movies_50k_actors_2.1.6.zip";
       String path = "data/cineasts_12k_movies_50k_actors.db/";

       try {
           FileUtils.deleteRecursively(new File(path));
           extractFolder(zipFile);
       } catch (Exception e) {
           e.printStackTrace();
           System.exit(1);
       }

       long nodeCount, relsCount;
	
	// Open a database instance
       GraphDatabaseService g = new GraphDatabaseFactory()
               .newEmbeddedDatabaseBuilder(path)
               .setConfig(GraphDatabaseSettings.allow_store_upgrade, "true")
               .newGraphDatabase();
       try (Transaction tx = g.beginTx() ) {
		nodeCount = IteratorUtil.count( GlobalGraphOperations.at(g).getAllNodes() );
		relsCount = IteratorUtil.count( GlobalGraphOperations.at(g).getAllRelationships() );
		tx.success();
	}
	
	System.out.println("Node count: "+nodeCount);
       System.out.println("Rel count: " + relsCount);

       // Declare the GraphAlgoEngine on the database instance
	GraphAlgoEngine engine = new GraphAlgoEngine(g);
	if( args.length > 1 && args[1].equals("off") )
		engine.disableLogging();

       Louvain louvain = new Louvain(g);
       louvain.execute();
       LouvainResult result = louvain.getResult();
       for (int layer : result.layers()) {
           System.out.println("Layer " + layer + ": " + result.layer(layer).size() + " nodes");
       }

       LabelPropagation lp = new LabelPropagation();
       // Starts the algorithm on the given graph g
	engine.execute(lp);
	Long2LongMap communityMap = lp.getResult();
	long totCommunities = new LongOpenHashSet( communityMap.values() ).size();
       System.out.println("There are " + totCommunities + " communities according to Label Propagation");

	DirectedModularity modularity = new DirectedModularity(g);
	engine.execute(modularity);
       System.out.println("The directed modularity of this network is " + modularity.getResult());

       UndirectedModularity umodularity = new UndirectedModularity(g);
	engine.execute(umodularity);
       System.out.println("The undirected modularity of this network is " + umodularity.getResult());

       engine.clean(lp); // Now you can clean Label propagation results

       TriangleCount tc = new TriangleCount();
	engine.execute(tc);
	Long2LongMap triangleCount = tc.getResult();
	Optional<Long> totalTriangles = triangleCount.values().stream().reduce( (x, y) -> x + y );
	System.out.println("There are "+totalTriangles.get()+" triangles");

	PageRank pr = new PageRank(g);
	engine.execute(pr);
	Long2DoubleMap ranks = pr.getResult();
	engine.clean(pr);
	Optional<Double> res = ranks.values().parallelStream().reduce( (x, y) -> x + y );
	System.out.println("Check PageRank sum is 1.0: "+ res.get());

	ConnectedComponents cc = new ConnectedComponents();
	engine.execute(cc);
	Long2LongMap components = cc.getResult();
	engine.clean(cc);
	int totalComponents = new LongOpenHashSet( components.values() ).size();
	System.out.println("There are "+ totalComponents+ " different connected components");
	
	StronglyConnectedComponents scc = new StronglyConnectedComponents();
	engine.execute(scc);
	components = scc.getResult();
	engine.clean(scc);
	totalComponents = new LongOpenHashSet( components.values() ).size();
	System.out.println("There are "+ totalComponents+ " different strongly connected components");
	
	// Don't forget to shutdown the database
	g.shutdown();
}
 
Example #24
Source File: NodeMetadataSocialProofTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testComputeRecommendations() throws Exception {
  NodeMetadataLeftIndexedPowerLawMultiSegmentBipartiteGraph graph = BipartiteGraphTestHelper.
    buildSmallTestNodeMetadataLeftIndexedPowerLawMultiSegmentBipartiteGraphWithEdgeTypes();

  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{2, 3}, new double[]{1.0, 0.5});
  IntSet urlIds = new IntOpenHashSet(new int[]{200, 300});
  IntSet hashtagIds = new IntOpenHashSet(new int[]{100, 101, 102});
  Byte2ObjectMap<IntSet> nodeMetadataTypeToIdsMap = new Byte2ObjectArrayMap<>();
  nodeMetadataTypeToIdsMap.put((byte) RecommendationType.HASHTAG.getValue(), hashtagIds);
  nodeMetadataTypeToIdsMap.put((byte) RecommendationType.URL.getValue(), urlIds);

  byte[] validSocialProofs = new byte[]{1, 2, 3, 4};
  long randomSeed = 918324701982347L;
  Random random = new Random(randomSeed);

  NodeMetadataSocialProofRequest request = new NodeMetadataSocialProofRequest(
      nodeMetadataTypeToIdsMap,
      seedsMap,
      validSocialProofs
  );

  SocialProofResponse socialProofResponse = new NodeMetadataSocialProofGenerator(
    graph
  ).computeRecommendations(request, random);

  List<RecommendationInfo> socialProofResults =
    Lists.newArrayList(socialProofResponse.getRankedRecommendations());

  for (RecommendationInfo recommendationInfo: socialProofResults) {
    NodeMetadataSocialProofResult socialProofResult = (NodeMetadataSocialProofResult) recommendationInfo;
    int nodeMetadataId = socialProofResult.getNodeMetadataId();
    Byte2ObjectMap<Long2ObjectMap<LongSet>> socialProofs = socialProofResult.getSocialProof();

    if (nodeMetadataId == 100) {
      assertEquals(socialProofResult.getSocialProofSize(), 1);
      assertEquals(socialProofs.get((byte) 1).get(2).contains(3), true);
    } else if (nodeMetadataId == 101) {
      assertEquals(socialProofs.isEmpty(), true);
    } else if (nodeMetadataId == 102) {
      assertEquals(socialProofs.isEmpty(), true);
    } else if (nodeMetadataId == 300) {
      assertEquals(socialProofs.isEmpty(), true);
    } else if (nodeMetadataId == 200) {
      assertEquals(socialProofResult.getSocialProofSize(), 3);
      assertEquals(socialProofs.get((byte) 1).get(2).contains(4), true);
      assertEquals(socialProofs.get((byte) 1).get(2).contains(6), true);
      assertEquals(socialProofs.get((byte) 4).get(3).contains(4), true);
    }
  }
}
 
Example #25
Source File: TopSecondDegreeByCountForMomentTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
private void testTopSecondDegreeByCountHelper(
  int maxNumResults,
  Map<Byte, Integer> minUserPerSocialProof,
  byte[] socialProofTypes,
  List<TopSecondDegreeByCountRecommendationInfo> expectedTopResults,
  RecommendationStats expectedTopSecondDegreeByCountStats
) throws Exception {
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithEdgeTypes();

  long queryNode = 1;
  int maxSocialProofSize = 4;
  int maxNumSocialProofs = 100;
  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{1, 2, 3}, new double[]{1.5, 1.0, 0.5});
  LongSet toBeFiltered = new LongOpenHashSet(new long[]{});
  ResultFilterChain resultFilterChain = new ResultFilterChain(Lists.<ResultFilter>newArrayList(
    new RequestedSetFilter(new NullStatsReceiver())));

  int expectedNodesToHit = 100;
  long randomSeed = 918324701982347L;
  long maxRightNodeAgeInMillis = Long.MAX_VALUE;
  long maxEdgeAgeInMillis = Long.MAX_VALUE;
  Random random = new Random(randomSeed);

  TopSecondDegreeByCountRequestForMoment request = new TopSecondDegreeByCountRequestForMoment(
    queryNode,
    seedsMap,
    toBeFiltered,
    maxNumResults,
    maxNumSocialProofs,
    maxSocialProofSize,
    minUserPerSocialProof,
    socialProofTypes,
    maxRightNodeAgeInMillis,
    maxEdgeAgeInMillis,
    resultFilterChain);

  try {
    TopSecondDegreeByCountResponse response = new TopSecondDegreeByCountForMoment(
      bipartiteGraph,
      expectedNodesToHit,
      new NullStatsReceiver()
    ).computeRecommendations(request, random);

    List<RecommendationInfo> topSecondDegreeByCountResults =
      Lists.newArrayList(response.getRankedRecommendations());

    RecommendationStats topSecondDegreeByCountStats = response.getTopSecondDegreeByCountStats();

    assertEquals(expectedTopSecondDegreeByCountStats, topSecondDegreeByCountStats);
    assertEquals(expectedTopResults, topSecondDegreeByCountResults);
  }
  catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example #26
Source File: TopSecondDegreeByCountForUserTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
private void testTopSecondDegreeByCountHelper(
  int maxNumResults,
  Map<Byte, Integer> minUserPerSocialProof,
  byte[] socialProofTypes,
  List<UserRecommendationInfo> expectedTopResults,
  RecommendationStats expectedTopSecondDegreeByCountStats
) throws Exception {
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithEdgeTypes();

  long queryNode = 1;
  int maxSocialProofSize = 4;
  int maxNumSocialProofs = 100;
  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{1, 2, 3}, new double[]{1.5, 1.0, 0.5});
  LongSet toBeFiltered = new LongOpenHashSet(new long[]{});
  ResultFilterChain resultFilterChain = new ResultFilterChain(Lists.<ResultFilter>newArrayList(
    new RequestedSetFilter(new NullStatsReceiver())));

  int expectedNodesToHit = 100;
  long randomSeed = 918324701982347L;
  long maxRightNodeAgeInMillis = Long.MAX_VALUE;
  long maxEdgeAgeInMillis = Long.MAX_VALUE;
  Random random = new Random(randomSeed);

  TopSecondDegreeByCountRequestForUser request = new TopSecondDegreeByCountRequestForUser(
    queryNode,
    seedsMap,
    toBeFiltered,
    maxNumResults,
    maxNumSocialProofs,
    maxSocialProofSize,
    minUserPerSocialProof,
    socialProofTypes,
    maxRightNodeAgeInMillis,
    maxEdgeAgeInMillis,
    resultFilterChain);

  try {
    TopSecondDegreeByCountResponse response = new TopSecondDegreeByCountForUser(
      bipartiteGraph,
      expectedNodesToHit,
      new NullStatsReceiver()
    ).computeRecommendations(request, random);

    List<RecommendationInfo> topSecondDegreeByCountResults =
      Lists.newArrayList(response.getRankedRecommendations());

    RecommendationStats topSecondDegreeByCountStats = response.getTopSecondDegreeByCountStats();

    assertEquals(expectedTopSecondDegreeByCountStats, topSecondDegreeByCountStats);
    assertEquals(expectedTopResults, topSecondDegreeByCountResults);
  }
  catch (Exception e) {
    e.printStackTrace();
  }
}
 
Example #27
Source File: TopSecondDegreeByCountForTweetTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTopSecondDegreeByCountWithSocialProofTypeUnionsWithSmallGraph() {
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraphWithEdgeTypes();
  long queryNode = 1;
  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{1, 2, 3}, new double[]{1.5, 1.0, 0.5});
  LongSet toBeFiltered = new LongOpenHashSet(new long[]{});
  Set<RecommendationType> recommendationTypes = new HashSet<>();
  recommendationTypes.add(RecommendationType.TWEET);
  Map<RecommendationType, Integer> maxNumResults = new HashMap<>();
  maxNumResults.put(RecommendationType.TWEET, 3);
  Map<RecommendationType, Integer> minUserSocialProofSizes = new HashMap<>();
  minUserSocialProofSizes.put(RecommendationType.TWEET, 2);

  int maxUserSocialProofSize = 3;
  int maxTweetSocialProofSize = 10;
  int maxSocialProofTypeSize = 5;
  byte[] validSocialProofs = new byte[]{0, 1, 2, 3, 4};
  Set<byte[]> socialProofTypeUnions = new HashSet<>();
  socialProofTypeUnions.add(new byte[]{0, 3});
  int expectedNodesToHit = 100;
  long maxRightNodeAgeInMillis = Long.MAX_VALUE;
  long maxEdgeAgeInMillis = Long.MAX_VALUE;
  long randomSeed = 918324701982347L;

  Random random = new Random(randomSeed);
  ResultFilterChain resultFilterChain = new ResultFilterChain(Lists.<ResultFilter>newArrayList(
    new RequestedSetFilter(new NullStatsReceiver())
  ));

  TopSecondDegreeByCountRequestForTweet request = new TopSecondDegreeByCountRequestForTweet(
    queryNode,
    seedsMap,
    toBeFiltered,
    recommendationTypes,
    maxNumResults,
    maxSocialProofTypeSize,
    maxUserSocialProofSize,
    maxTweetSocialProofSize,
    minUserSocialProofSizes,
    validSocialProofs,
    maxRightNodeAgeInMillis,
    maxEdgeAgeInMillis,
    resultFilterChain,
    socialProofTypeUnions
  );

  TopSecondDegreeByCountResponse response = new TopSecondDegreeByCountForTweet(
    bipartiteGraph,
    expectedNodesToHit,
    new NullStatsReceiver()
  ).computeRecommendations(request, random);

  LongList metadata1 = new LongArrayList(new long[]{0});
  LongList metadata3 = new LongArrayList(new long[]{0, 0, 0});
  ArrayList<HashMap<Byte, ConnectingUsersWithMetadata>> socialProof = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    socialProof.add(new HashMap<>());
  }
  socialProof.get(0).put((byte) 1, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{1, 2, 3}), metadata3));
  socialProof.get(1).put((byte) 0, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{2}), metadata1));
  socialProof.get(1).put((byte) 3, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{1}), metadata1));

  final List<RecommendationInfo> expectedTopResults = new ArrayList<>();
  expectedTopResults.add(new TweetRecommendationInfo(3, 3.0, socialProof.get(0)));
  expectedTopResults.add(new TweetRecommendationInfo(5, 2.5, socialProof.get(1)));

  List<RecommendationInfo> topSecondDegreeByCountResults =
    Lists.newArrayList(response.getRankedRecommendations());

  final RecommendationStats expectedTopSecondDegreeByCountStats =
    new RecommendationStats(5, 6, 17, 2, 4, 0);
  RecommendationStats topSecondDegreeByCountStats =
    response.getTopSecondDegreeByCountStats();

  assertEquals(expectedTopSecondDegreeByCountStats, topSecondDegreeByCountStats);
  assertEquals(expectedTopResults, topSecondDegreeByCountResults);
}
 
Example #28
Source File: TopSecondDegreeByCountForTweetTest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@Test
public void testTopSecondDegreeByCountWithSmallGraph() {
  NodeMetadataLeftIndexedMultiSegmentBipartiteGraph bipartiteGraph =
    BipartiteGraphTestHelper.buildSmallTestNodeMetadataLeftIndexedMultiSegmentBipartiteGraph();
  long queryNode = 1;
  Long2DoubleMap seedsMap = new Long2DoubleArrayMap(new long[]{2, 3}, new double[]{1.0, 0.5});
  LongSet toBeFiltered = new LongOpenHashSet(new long[]{2, 3, 4, 5});
  Set<RecommendationType> recommendationTypes = new HashSet<>();
  recommendationTypes.add(RecommendationType.HASHTAG);
  recommendationTypes.add(RecommendationType.URL);
  recommendationTypes.add(RecommendationType.TWEET);
  Map<RecommendationType, Integer> maxNumResults = new HashMap<>();
  maxNumResults.put(RecommendationType.HASHTAG, 3);
  maxNumResults.put(RecommendationType.URL, 3);
  maxNumResults.put(RecommendationType.TWEET, 3);
  Map<RecommendationType, Integer> minUserSocialProofSizes = new HashMap<>();
  minUserSocialProofSizes.put(RecommendationType.HASHTAG, 1);
  minUserSocialProofSizes.put(RecommendationType.URL, 1);
  minUserSocialProofSizes.put(RecommendationType.TWEET, 1);

  int maxUserSocialProofSize = 2;
  int maxTweetSocialProofSize = 10;
  int maxSocialProofTypeSize = 5;
  byte[] validSocialProofs = new byte[]{0, 1, 2, 3, 4};
  int expectedNodesToHit = 100;
  long randomSeed = 918324701982347L;
  Random random = new Random(randomSeed);
  long maxRightNodeAgeInMillis = Long.MAX_VALUE;
  long maxEdgeAgeInMillis = Long.MAX_VALUE;
  ResultFilterChain resultFilterChain = new ResultFilterChain(Lists.<ResultFilter>newArrayList(
    new RequestedSetFilter(new NullStatsReceiver())
  ));
  Set<byte[]> socialProofTypeUnions = new HashSet<>();

  TopSecondDegreeByCountRequestForTweet request = new TopSecondDegreeByCountRequestForTweet(
    queryNode,
    seedsMap,
    toBeFiltered,
    recommendationTypes,
    maxNumResults,
    maxSocialProofTypeSize,
    maxUserSocialProofSize,
    maxTweetSocialProofSize,
    minUserSocialProofSizes,
    validSocialProofs,
    maxRightNodeAgeInMillis,
    maxEdgeAgeInMillis,
    resultFilterChain,
    socialProofTypeUnions
  );

  TopSecondDegreeByCountResponse response = new TopSecondDegreeByCountForTweet(
    bipartiteGraph,
    expectedNodesToHit,
    new NullStatsReceiver()
  ).computeRecommendations(request, random);

  LongList metadata1 = new LongArrayList(new long[]{0});
  LongList metadata2 = new LongArrayList(new long[]{0, 0});
  ArrayList<HashMap<Byte, ConnectingUsersWithMetadata>> socialProof = new ArrayList<>();
  for (int i = 0; i < 3; i++) {
    socialProof.add(new HashMap<>());
  }
  socialProof.get(0).put((byte) 0, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{2, 3}), metadata2));
  socialProof.get(1).put((byte) 0, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{2}), metadata1));
  socialProof.get(2).put((byte) 0, new ConnectingUsersWithMetadata(new LongArrayList(new long[]{3}), metadata1));

  final List<RecommendationInfo> expectedTopResults = new ArrayList<RecommendationInfo>();
  expectedTopResults.add(new TweetRecommendationInfo(10, 1.5, socialProof.get(0)));
  expectedTopResults.add(new TweetRecommendationInfo(6, 1.0, socialProof.get(1)));
  expectedTopResults.add(new TweetRecommendationInfo(8, 0.5, socialProof.get(2)));

  List<RecommendationInfo> topSecondDegreeByCountResults =
    Lists.newArrayList(response.getRankedRecommendations());

  final RecommendationStats expectedTopSecondDegreeByCountStats =
    new RecommendationStats(4, 9, 20, 2, 3, 2);
  RecommendationStats topSecondDegreeByCountStats =
    response.getTopSecondDegreeByCountStats();

  assertEquals(expectedTopSecondDegreeByCountStats, topSecondDegreeByCountStats);
  assertEquals(expectedTopResults, topSecondDegreeByCountResults);
}
 
Example #29
Source File: RandomMultiGraphNeighborsRequest.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
public Long2DoubleMap getLeftSeedNodesWithWeight() {
  return leftSeedNodesWithWeight;
}
 
Example #30
Source File: SalsaIterations.java    From GraphJet with Apache License 2.0 4 votes vote down vote up
@VisibleForTesting
protected void seedLeftSideForFirstIteration() {
  long queryNode = salsaInternalState.getSalsaRequest().getQueryNode();
  salsaStats.setNumDirectNeighbors(
      salsaInternalState.getBipartiteGraph().getLeftNodeDegree(queryNode));

  Long2DoubleMap seedNodesWithWeight =
      salsaInternalState.getSalsaRequest().getLeftSeedNodesWithWeight();
  LongSet nonZeroSeedSet = salsaInternalState.getNonZeroSeedSet();

  double totalWeight = 0.0;
  for (Long2DoubleMap.Entry entry : seedNodesWithWeight.long2DoubleEntrySet()) {
    if (salsaInternalState.getBipartiteGraph().getLeftNodeDegree(entry.getLongKey())
        > 0) {
      totalWeight += entry.getDoubleValue();
      nonZeroSeedSet.add(entry.getLongKey());
    }
  }

  // If there is a pre-specified weight, we let it take precedence, but if not, then we reset
  // weights in accordance with the fraction of weight requested for the query node.
  if (!seedNodesWithWeight.containsKey(queryNode)
      && salsaInternalState.getBipartiteGraph().getLeftNodeDegree(queryNode) > 0) {
    double queryNodeWeight = 1.0;
    if (totalWeight > 0.0) {
      queryNodeWeight =
          totalWeight * salsaInternalState.getSalsaRequest().getQueryNodeWeightFraction()
              / (1.0 - salsaInternalState.getSalsaRequest().getQueryNodeWeightFraction());
    }
    seedNodesWithWeight.put(queryNode, queryNodeWeight);
    totalWeight += queryNodeWeight;
    nonZeroSeedSet.add(queryNode);
  }

  for (long leftNode : nonZeroSeedSet) {
    int numWalksToStart = (int) Math.ceil(
        seedNodesWithWeight.get(leftNode) / totalWeight
            * salsaInternalState.getSalsaRequest().getNumRandomWalks());
      salsaInternalState.getCurrentLeftNodes().put(leftNode, numWalksToStart);
  }

  salsaStats.setNumSeedNodes(salsaInternalState.getCurrentLeftNodes().size());
}