def rating_of_restaurant(active_user_id,
                         restaurant_id,
                         lmu_limit=20,
                         reco_limit=30,
                         competitor_limit=20):

    # Get details for profile window
    user_profile_df = algorithm.user_profile(active_user_id)

    # Get first/second order nodes for graph, and info for bar graph comparison
    # This is for explore mode, all like nodes are LMU for active users
    like_users_df = algorithm.like_users(user_profile_df, active_user_id,
                                         lmu_limit)

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        like_users_df, reco_limit)
    #display(recommended_restaurants_df)

    # Get all the restaurants name and score inforamtion to build the comparision bar graph
    user_graph_dict = algorithm.user_graph(user_id, user_profile_df,
                                           like_users_df,
                                           recommended_restaurants_df)
    nodes = user_graph_dict['nodes']
    nodes_restaurants = nodes[nodes['order'] == 1][['name', 'score']]
    links = user_graph_dict['links'].to_json(orient='records')
    nodes = user_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}
    comparison_json = nodes_restaurants.to_json(orient="records")

    # Returns details of how user rated restaurant
    rating_df = algorithm.user_restaurant_rating(active_user_id, restaurant_id)
    rating = rating_df[[
        'restaurant', 'user', 'rating', 'date', 'review', 'useful', 'funny',
        'cool'
    ]].astype(str)
    rating_json = rating.to_json(orient='records')

    #return the likes nodes at the bottom
    restaurant_profile_df = algorithm.restaurant_profile(restaurant_id)
    #Returns restaurant details with categories and attributes csv lists

    like_restaurants_df = algorithm.like_restaurants(restaurant_id,
                                                     competitor_limit)
    like_restaurants = like_restaurants_df[[
        'restaurant', 'restaurant_id', 'compete_score'
    ]].astype(str).drop_duplicates().head(10)
    like_restaurants_json = like_restaurants.to_json(orient="records")

    result = {
        "graph": graph,
        "rating": rating_json,
        "comparison": comparison_json,
        "likes": like_restaurants_json
    }
    return result
def likeminded_user_similarity(active_user_id, selected_user_id):

    ##LMU similarity table
    active_user_profile_df = algorithm.user_profile(active_user_id)

    active_like_users_df = algorithm.like_users(active_user_profile_df,
                                                active_user_id, lmu_limit)
    # Returns information to build first and second order nodes in the graph.
    # Also returns name and user_id information to build like user nodes at bottom of screen.
    LMU_similarity = active_like_users_df[
        active_like_users_df['user_id'] == selected_user_id][[
            'shared_val', 'shared_score', 'overlap_val', 'overlap_score',
            'rate_sim_val', 'rate_sim_score', 'influence_val',
            'influence_score', 'influ_sim_val', 'influ_sim_score', 'lmu_score'
        ]].astype(str)
    LMU_similarity_json = LMU_similarity.to_json(orient='records')

    ##comparision
    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        active_like_users_df, reco_limit)
    #display(recommended_restaurants_df)

    # Get all the restaurants name and score inforamtion to build the comparision bar graph
    user_graph_dict = algorithm.user_graph(active_user_id, user_profile_df,
                                           active_like_users_df,
                                           recommended_restaurants_df)
    nodes = user_graph_dict['nodes']
    nodes_restaurants = nodes[nodes['order'] == 2][['name', 'score']]
    links = user_graph_dict['links'].to_json(orient='records')
    nodes = user_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}
    comparison_json = nodes_restaurants.to_json(orient="records")

    ##likes
    selected_user_profile_df = algorithm.user_profile(selected_user_id)
    # Get first/second order nodes for graph, and info for like nodes across bottom of page
    # This is for explore mode, all like nodes are LMU for active users
    like_users_df = algorithm.like_users(selected_user_profile_df,
                                         selected_user_id, lmu_limit)
    like_users = like_users_df[['user', 'user_id', 'lmu_score'
                                ]].astype(str).drop_duplicates().head(10)
    like_users_json = like_users.to_json(orient="records")

    result = {
        "graph": graph,
        "LMU_similarity": LMU_similarity_json,
        "comparison": comparison_json,
        "likes": like_users_json
    }

    return result
def likeminded_user_similarity(active_user_id, selected_user_id):

    # Get active user info for other routines
    active_user_profile_df = algorithm.user_profile(active_user_id)
    #user_profile = active_user_profile_df.astype(str)                # Issue #7
    #user_profile = active_user_profile_df

    # Get first/second order nodes for graph
    active_like_users_df = algorithm.like_users(active_user_profile_df,
                                                active_user_id, lmu_limit)

    # Pull off details on selected LMU for upper right window
    #LMU_similarity = active_like_users_df[active_like_users_df['user_id'] == selected_user_id][
    #    ['shared_val', 'shared_score', 'overlap_val', 'overlap_score', 'rate_sim_val', 'rate_sim_score',
    #     'influence_val', 'influence_score', 'influ_sim_val', 'influ_sim_score', 'lmu_score']].astype(str)        # Issue #7
    LMU_similarity = active_like_users_df[
        active_like_users_df['user_id'] == selected_user_id][[
            'shared_val', 'shared_score', 'overlap_val', 'overlap_score',
            'rate_sim_val', 'rate_sim_score', 'influence_val',
            'influence_score', 'influ_sim_val', 'influ_sim_score', 'lmu_score'
        ]]

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        active_like_users_df, reco_limit)
    # display(recommended_restaurants_df)

    # Transform graph info into nodes and links lists
    user_graph_dict = algorithm.user_graph(active_user_id,
                                           active_user_profile_df,
                                           active_like_users_df,
                                           recommended_restaurants_df)

    # Get info for comparison bar graph
    nodes = user_graph_dict['nodes']
    nodes_comparison = nodes[nodes['order'] == 2][['name', 'score']]
    nodes_comparison.sort_values(by=['score'], ascending=False, inplace=True)
    nodes_comparison = nodes_comparison.round({'score': 2})

    # Prepare the like user nodes
    selected_user_profile_df = algorithm.user_profile(selected_user_id)
    like_users_df = algorithm.like_users(selected_user_profile_df,
                                         selected_user_id, lmu_limit)
    #like_users_str_df = like_users_df[['user', 'user_id', 'lmu_score']].astype(str).drop_duplicates().head(like_limit)     # Issue #7
    like_users_str_df = like_users_df[['user', 'user_id', 'lmu_score'
                                       ]].drop_duplicates().head(like_limit)

    # user_graph_dict, like_users, LMU_similarity, nodes_comparison
    return LMU_similarity, nodes_comparison, like_users_str_df
def rating_of_restaurant(active_user_id, restaurant_id):
    # Get active user info for other routines
    user_profile_df = algorithm.user_profile(active_user_id)
    #user_profile = user_profile_df.astype(str)       # Issue #7
    #user_profile = user_profile_df

    # Get first/second order nodes for graph
    like_users_df = algorithm.like_users(user_profile_df, active_user_id,
                                         lmu_limit)

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        like_users_df, reco_limit)
    # display(recommended_restaurants_df)

    # Transform graph info into nodes and links lists
    user_graph_dict = algorithm.user_graph(active_user_id, user_profile_df,
                                           like_users_df,
                                           recommended_restaurants_df)

    nodes = user_graph_dict['nodes']
    nodes_comparison = nodes[nodes['order'] == 1][['name', 'score']]
    nodes_comparison.sort_values(by=['score'], ascending=False, inplace=True)
    nodes_comparison = nodes_comparison.round({'score': 2})

    # Returns details of how active user rated restaurant
    rating_df = algorithm.user_restaurant_rating(active_user_id, restaurant_id)
    #rating = rating_df[['restaurant', 'user', 'rating', 'date', 'review', 'useful', 'funny', 'cool']].astype(str)   # Issue #7
    rating = rating_df[[
        'restaurant', 'user', 'rating', 'date', 'review', 'useful', 'funny',
        'cool'
    ]]

    # return the likes nodes at the bottom            # Also use algorithm.like_restaurants for comparison bar graph
    like_restaurants_df = algorithm.like_restaurants(restaurant_id,
                                                     competitor_limit)
    like_restaurants_df['restaurant'] = like_restaurants_df[
        'restaurant'].str.encode('utf8')
    like_restaurants_str_df = like_restaurants_df[[
        'restaurant', 'restaurant_id', 'compete_score'
    ]].astype(str).drop_duplicates().head(like_limit)  # Issue #7

    # Return doesn't match View.py which will expect graph_dict, like, rating, comparison
    return rating, nodes_comparison, like_restaurants_str_df
def user_profile(user_id):

    # Get details for profile window
    user_profile_df = algorithm.user_profile(user_id)
    user_profile_df['influence'] = np.round(user_profile_df['influence'],
                                            decimals=2)
    user_profile_str_df = user_profile_df.astype(str)
    user_profile_json = user_profile_str_df.to_json(orient="records")
    #display(user_profile_json)

    # Get first/second order nodes for graph, and info for like nodes across bottom of page
    # This is for explore mode, all like nodes are LMU for active users
    like_users_df = algorithm.like_users(user_profile_df, user_id, lmu_limit)
    like_users = like_users_df[['user', 'user_id', 'lmu_score'
                                ]].astype(str).drop_duplicates().head(10)
    like_users_json = like_users.to_json(orient="records")
    #display(like_users_json)

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        like_users_df, reco_limit)
    #display(recommended_restaurants_df)

    # Transform graph info into nodes and links lists
    user_graph_dict = algorithm.user_graph(user_id, user_profile_df,
                                           like_users_df,
                                           recommended_restaurants_df)
    links = user_graph_dict['links'].to_json(orient='records')
    nodes = user_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}
    #display(graph)

    result = {
        "profile": user_profile_json,
        "graph": graph,
        "likes": like_users_json
    }

    return result
def user_profile(user_id):
    # Get details for profile window
    user_profile_df = algorithm.user_profile(user_id)
    # Note: Full conversion to string with .astype(str) has been commented out throughout API.py to fix issue #7
    #user_profile_str_df = user_profile_df.astype(str)
    user_profile_str_df = user_profile_df

    # Get first/second order nodes for graph, and info for like nodes across bottom of page
    like_users_df = algorithm.like_users(user_profile_df, user_id, lmu_limit)
    #like_users_str_df = like_users_df[['user', 'user_id', 'lmu_score']].astype(str).drop_duplicates().head(like_limit)   # Issue #7
    like_users_str_df = like_users_df[['user', 'user_id', 'lmu_score'
                                       ]].drop_duplicates().head(like_limit)

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        like_users_df, reco_limit)
    # display(recommended_restaurants_df)

    # Transform graph info into nodes and links lists
    user_graph_dict = algorithm.user_graph(user_id, user_profile_df,
                                           like_users_df,
                                           recommended_restaurants_df)

    return user_profile_str_df, user_graph_dict, like_users_str_df
def recommended_restaurant_relevance(active_user_id, selected_restaurant_id):

    active_user_profile_df = algorithm.user_profile(active_user_id)
    active_like_users_df = algorithm.like_users(active_user_profile_df,
                                                active_user_id, lmu_limit)
    recommended_restaurants_df = algorithm.recommended_restaurants(
        active_like_users_df, reco_limit)
    user_graph_dict = algorithm.user_graph(active_user_id,
                                           active_user_profile_df,
                                           active_like_users_df,
                                           recommended_restaurants_df)

    ##graph
    links = user_graph_dict['links'].to_json(orient='records')
    nodes = user_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}

    ##relevance
    reco_restaurant_relevance = recommended_restaurants_df[
        recommended_restaurants_df['restaurant_id'] == selected_restaurant_id]
    relevance_df = reco_restaurant_relevance[[
        'user', 'lmu_rating', 'lmu_score', 'contribution', 'relevancy'
    ]]
    if (reco_restaurant_relevance.shape[0] == 1):
        avg_rating = reco_restaurant_relevance['avg_rating'].values[0]
        restaurant_similarity = 5.0
        restaurant_contribution = avg_rating * restaurant_similarity
        restaurant_relevancy = reco_restaurant_relevance['relevancy'].values[0]

        restaurant_df = pd.DataFrame({
            'user': ["Restaurant's Average Rating"],
            "lmu_rating": [avg_rating],
            "lmu_score": [restaurant_similarity],
            "contribution": [restaurant_contribution],
            "relevancy": [restaurant_relevancy]
        })

        relevance_df = pd.concat([relevance_df, restaurant_df], axis=0)

    relevance = relevance_df.astype(str)
    relevance_json = relevance.to_json(orient='records')

    ##comparison
    nodes = user_graph_dict['nodes']
    nodes_users = nodes[nodes['order'] == 3][['name', 'score']]
    comparison_json = nodes_users.to_json(orient="records")

    ##likes
    selected_restaurant_profile_df = algorithm.restaurant_profile(
        selected_restaurant_id)
    selected_like_restaurants_df = algorithm.like_restaurants(
        selected_restaurant_id, competitor_limit)
    like_restaurants = selected_like_restaurants_df[[
        'restaurant', 'restaurant_id', 'compete_score'
    ]].astype(str).drop_duplicates().head(10)
    like_restaurants_json = like_restaurants.to_json(orient="records")

    result = {
        "graph": graph,
        "relevance": relevance_json,
        "comparison": comparison_json,
        "likes": like_restaurants_json
    }

    return result
def recommended_restaurant_relevance(active_user_id, selected_restaurant_id):
    # Get active restaurant info for other routines
    active_user_profile_df = algorithm.user_profile(active_user_id)
    #user_profile = active_user_profile_df.astype(str)              # Issue #7
    #user_profile = active_user_profile_df

    # Get first/second order nodes for graph
    active_like_users_df = algorithm.like_users(active_user_profile_df,
                                                active_user_id, lmu_limit)

    # Get third order nodes for graph
    recommended_restaurants_df = algorithm.recommended_restaurants(
        active_like_users_df, reco_limit)

    # Transform graph info into nodes and links lists
    user_graph_dict = algorithm.user_graph(active_user_id,
                                           active_user_profile_df,
                                           active_like_users_df,
                                           recommended_restaurants_df)

    # Pull off details on selected recommended restaurant relevance for upper right window
    reco_restaurant_relevance = recommended_restaurants_df[
        recommended_restaurants_df['restaurant_id'] == selected_restaurant_id]
    relevance_df = reco_restaurant_relevance[[
        'user', 'lmu_rating', 'lmu_score', 'contribution', 'relevancy'
    ]]
    if (reco_restaurant_relevance.shape[0] == 1):
        avg_rating = reco_restaurant_relevance['avg_rating'].values[0]
        restaurant_similarity = 5.0
        restaurant_contribution = avg_rating * restaurant_similarity
        restaurant_relevancy = reco_restaurant_relevance['relevancy'].values[0]

        restaurant_df = pd.DataFrame({
            'user': ["Restaurant's Average Rating"],
            "lmu_rating": [avg_rating],
            "lmu_score": [restaurant_similarity],
            "contribution": [restaurant_contribution],
            "relevancy": [restaurant_relevancy]
        })

        relevance_df = pd.concat([relevance_df, restaurant_df], axis=0)

    #relevance = relevance_df.astype(str)               # Issue #7
    relevance = relevance_df

    # Get info for comparison bar graph
    nodes = user_graph_dict['nodes']
    nodes_comparison = nodes[nodes['order'] == 3][['name', 'score'
                                                   ]]  # nodes_comparison
    nodes_comparison.sort_values(by=['score'], ascending=False, inplace=True)
    nodes_comparison = nodes_comparison.round({'score': 2})

    # Prepare the like restaurant nodes
    selected_like_restaurants_df = algorithm.like_restaurants(
        selected_restaurant_id,
        competitor_limit)[['restaurant', 'restaurant_id', 'compete_score']]
    selected_like_restaurants_df['restaurant'] = selected_like_restaurants_df[
        'restaurant'].str.encode('utf8')
    selected_like_restaurants_df[
        'restaurant_id'] = selected_like_restaurants_df[
            'restaurant_id'].str.encode('utf8')
    selected_like_restaurants_df[
        'compete_score'] = selected_like_restaurants_df[
            'compete_score'].astype(str)
    selected_like_restaurants_df = selected_like_restaurants_df.drop_duplicates(
    ).head(like_limit)  # Issue #7
    return relevance, nodes_comparison, selected_like_restaurants_df