def restaurant_profile(restaurant_id):

    # Get details for profile window
    restaurant_profile_df = algorithm.restaurant_profile(restaurant_id)
    #restaurant_profile = restaurant_profile_df.astype(str)        # Issue #7
    restaurant_profile = restaurant_profile_df

    # Get first/second order nodes for graph, and info for like nodes across bottom of page
    like_restaurants_df = algorithm.like_restaurants(restaurant_id,
                                                     competitor_limit)
    #like_restaurants_str_df = like_restaurants_df[['restaurant', 'restaurant_id', 'compete_score']].astype(str).drop_duplicates().head(like_limit)     # Issue #7
    like_restaurants_str_df = like_restaurants_df[[
        'restaurant', 'restaurant_id', 'compete_score'
    ]].drop_duplicates().head(like_limit)

    # Get third order nodes for graph
    customer_prospects_df = algorithm.customer_prospects(
        like_restaurants_df, prospect_limit)

    # Transform graph info into nodes and links lists
    restaurant_graph_dict = algorithm.restaurant_graph(restaurant_id,
                                                       restaurant_profile_df,
                                                       like_restaurants_df,
                                                       customer_prospects_df)

    return restaurant_profile, restaurant_graph_dict, like_restaurants_str_df
def direct_competitor_similarity(active_restaurant_id, selected_restaurant_id):

    ##restaurant_similarity_json
    active_restaurant_profile_df = algorithm.restaurant_profile(
        active_restaurant_id)
    # Returns restaurant details with categories and attributes csv lists

    active_like_restaurants_df = algorithm.like_restaurants(
        active_restaurant_id, competitor_limit)
    restaurant_similarity = active_like_restaurants_df[
        active_like_restaurants_df['restaurant_id'] == selected_restaurant_id][
            [
                'shared_val', 'shared_score', 'overlap_val', 'overlap_score',
                'rate_sim_val', 'rate_sim_score', 'zip_val', 'zip_score',
                'compete_score'
            ]].astype(str)
    restaurant_similarity_json = restaurant_similarity.to_json(
        orient='records')

    ##comparsion
    customer_prospects_df = algorithm.customer_prospects(
        active_like_restaurants_df, prospect_limit)

    restaurant_graph_dict = algorithm.restaurant_graph(
        active_restaurant_id, active_restaurant_profile_df,
        active_like_restaurants_df, customer_prospects_df)
    # Global nodes and links dataframes are created
    nodes = restaurant_graph_dict['nodes']
    nodes_users = nodes[nodes['order'] == 2][['name', 'score']]
    links = restaurant_graph_dict['links'].to_json(orient='records')
    nodes = restaurant_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}
    comparison_json = nodes_users.to_json(orient="records")

    ##likes
    restaurant_profile_df = algorithm.restaurant_profile(
        selected_restaurant_id)

    like_restaurants_df = algorithm.like_restaurants(selected_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,
        "restaurant_similarity": restaurant_similarity_json,
        "comparison": comparison_json,
        "likes": like_restaurants_json
    }

    return result
def direct_competitor_similarity(active_restaurant_id, selected_restaurant_id):
    # Get active restaurant info for other routines
    active_restaurant_profile_df = algorithm.restaurant_profile(
        active_restaurant_id)
    #restaurant_profile = active_restaurant_profile_df.astype(str)                   # Issue #7
    #restaurant_profile = active_restaurant_profile_df

    # Get first/second order nodes for graph
    active_like_restaurants_df = algorithm.like_restaurants(
        active_restaurant_id, competitor_limit)

    # Pull off details on selected direct competitor similarity for upper right window
    restaurant_similarity = active_like_restaurants_df[
        active_like_restaurants_df['restaurant_id'] == selected_restaurant_id][
            [
                'shared_val', 'shared_score', 'overlap_val', 'overlap_score',
                'review_sim_val', 'review_sim_score', 'rate_sim_val',
                'rate_sim_score', 'zip_val', 'zip_score', 'compete_score'
            ]]

    # Get third order nodes for graph
    customer_prospects_df = algorithm.customer_prospects(
        active_like_restaurants_df, prospect_limit)

    # Transform graph info into nodes and links lists
    restaurant_graph_dict = algorithm.restaurant_graph(
        active_restaurant_id, active_restaurant_profile_df,
        active_like_restaurants_df, customer_prospects_df)

    # Get info for comparison bar graph
    nodes = restaurant_graph_dict[
        'nodes']  # Call these nodes and nodes_comparison
    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})

    ##likes
    restaurant_profile_df = algorithm.restaurant_profile(
        selected_restaurant_id)  # Not needed - delete

    # Prepare the like user nodes
    like_restaurants_df = algorithm.like_restaurants(
        selected_restaurant_id,
        competitor_limit)[['restaurant', 'restaurant_id', 'compete_score']]
    like_restaurants_df['restaurant'] = like_restaurants_df[
        'restaurant'].str.encode('utf8')
    like_restaurants_df['restaurant_id'] = like_restaurants_df[
        'restaurant_id'].str.encode('utf8')
    like_restaurants_df['compete_score'] = like_restaurants_df[
        'compete_score'].astype(str)
    like_restaurants_df = like_restaurants_df.drop_duplicates().head(
        like_limit)  # Issue #7
    return restaurant_similarity, nodes_comparison, like_restaurants_df
def rating_by_user(active_restaurant_id, user_id):

    restaurant_profile_df = algorithm.restaurant_profile(active_restaurant_id)

    like_restaurants_df = algorithm.like_restaurants(restaurant_id,
                                                     competitor_limit)
    # Returns name and restaurant_id info to build like restaurant nodes at bottom of screen.
    # (Also provides info for bar graph in Analyze mode when a second order node is selected)

    customer_prospects_df = algorithm.customer_prospects(
        like_restaurants_df, prospect_limit)

    restaurant_graph_dict = algorithm.restaurant_graph(restaurant_id,
                                                       restaurant_profile_df,
                                                       like_restaurants_df,
                                                       customer_prospects_df)
    # Global nodes and links dataframes are created
    nodes = restaurant_graph_dict['nodes']
    nodes_users = nodes[nodes['order'] == 1][['name', 'score']]
    links = restaurant_graph_dict['links'].to_json(orient='records')
    nodes = restaurant_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}
    comparison_json = nodes_users.to_json(orient="records")

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

    ##likes json
    user_profile_df = algorithm.user_profile(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(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")

    result = {
        "graph": graph,
        "rating": rating_json,
        "comparison": comparison_json,
        "likes": like_users_json
    }

    return result
def rating_by_user(active_restaurant_id, user_id):
    # Get active restaurant info for other routines
    restaurant_profile_df = algorithm.restaurant_profile(active_restaurant_id)
    #restaurant_profile = restaurant_profile_df.astype(str)       # Issue #7
    #restaurant_profile = restaurant_profile_df

    # Get first/second order nodes for graph
    like_restaurants_df = algorithm.like_restaurants(active_restaurant_id,
                                                     competitor_limit)

    # Get third order nodes for graph
    customer_prospects_df = algorithm.customer_prospects(
        like_restaurants_df, prospect_limit)

    # Transform graph info into nodes and links lists
    restaurant_graph_dict = algorithm.restaurant_graph(active_restaurant_id,
                                                       restaurant_profile_df,
                                                       like_restaurants_df,
                                                       customer_prospects_df)

    # Get info for comparison bar graph
    nodes = restaurant_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 restaurant was rated by selected user
    rating_df = algorithm.user_restaurant_rating(user_id, active_restaurant_id)
    #rating_str_df = rating_df[['restaurant', 'user', 'rating', 'date', 'review', 'useful', 'funny', 'cool']].astype(str)      # Issue #7
    rating_str_df = rating_df[[
        'restaurant', 'user', 'rating', 'date', 'review', 'useful', 'funny',
        'cool'
    ]]

    ## Get info for like nodes across bottom of page
    user_profile_df = algorithm.user_profile(user_id)
    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)

    # Return should be: restaurant_graph_dict, like_users, rating, nodes_comparison
    return rating_str_df, nodes_comparison, like_users_str_df
def restaurant_profile(restaurant_id):

    # Get details for profile window
    restaurant_profile_df = algorithm.restaurant_profile(restaurant_id)
    restaurant_profile = restaurant_profile_df.astype(str)
    restaurant_profile_json = restaurant_profile.to_json(orient="records")
    #display(restaurant_profile_json)

    # Get first/second order nodes for graph, and info for like nodes across bottom of page
    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")
    #display(like_restaurants_json)

    # Get third order nodes for graph
    customer_prospects_df = algorithm.customer_prospects(
        like_restaurants_df, prospect_limit)
    #display(customer_prospects_df)

    # Transform graph info into nodes and links lists
    restaurant_graph_dict = algorithm.restaurant_graph(restaurant_id,
                                                       restaurant_profile_df,
                                                       like_restaurants_df,
                                                       customer_prospects_df)
    #display(restaurant_graph_dict)
    links = restaurant_graph_dict['links'].to_json(orient='records')
    nodes = restaurant_graph_dict['nodes'].to_json(orient="records")
    graph = {"links": links, "nodes": nodes}

    result = {
        "profile": restaurant_profile_json,
        "graph": graph,
        "likes": like_restaurants_json
    }

    return result
def customer_prospect_relevance(active_restaurant_id, selected_user_id):

    ##pre-calcualtion
    active_restaurant_profile_df = algorithm.restaurant_profile(
        active_restaurant_id)
    active_like_restaurants_df = algorithm.like_restaurants(
        active_restaurant_id, competitor_limit)
    customer_prospects_df = algorithm.customer_prospects(
        like_restaurants_df, prospect_limit)
    restaurant_graph_dict = algorithm.restaurant_graph(restaurant_id,
                                                       restaurant_profile_df,
                                                       like_restaurants_df,
                                                       customer_prospects_df)

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

    ##relevance
    customer_prospect_relevance = customer_prospects_df[
        customer_prospects_df['user_id'] == selected_user_id]
    relevance_df = customer_prospect_relevance[[
        'restaurant', 'user_rating', 'compete_score', 'contribution',
        'relevancy'
    ]]
    if (customer_prospect_relevance.shape[0] == 1):
        avg_rating = customer_prospect_relevance['avg_rating'].values[0]
        user_similarity = 5.0
        user_contribution = avg_rating * user_similarity
        user_relevancy = customer_prospect_relevance['relevancy'].values[0]

        user_df = pd.DataFrame({
            'restaurant': ["User's Average Rating"],
            "user_rating": [avg_rating],
            "compete_score": [user_similarity],
            "contribution": [user_contribution],
            "relevancy": [user_relevancy]
        })

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

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

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

    ##likes
    selected_user_profile_df = algorithm.user_profile(selected_user_id)
    selected_like_users_df = algorithm.like_users(selected_user_profile_df,
                                                  selected_user_id, lmu_limit)
    like_users = selected_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,
        "relevance": relevance_json,
        "comparison": comparison_json,
        "likes": like_users_json
    }
    return result
def customer_prospect_relevance(active_restaurant_id, selected_user_id):
    # Get active restaurant info for other routines
    active_restaurant_profile_df = algorithm.restaurant_profile(
        active_restaurant_id)
    #restaurant_profile = active_restaurant_profile_df.astype(str)           # Issue #7
    #restaurant_profile = active_restaurant_profile_df

    # Get first/second order nodes for graph
    active_like_restaurants_df = algorithm.like_restaurants(
        active_restaurant_id, competitor_limit)

    # Get third order nodes for graph
    customer_prospects_df = algorithm.customer_prospects(
        active_like_restaurants_df, prospect_limit)

    # Transform graph info into nodes and links lists
    restaurant_graph_dict = algorithm.restaurant_graph(
        active_restaurant_id, active_restaurant_profile_df,
        active_like_restaurants_df, customer_prospects_df)

    ## Pull off details on selected customer prospect relevance for upper right window
    customer_prospect_relevance = customer_prospects_df[
        customer_prospects_df['user_id'] == selected_user_id]
    relevance_df = customer_prospect_relevance[[
        'restaurant', 'user_rating', 'compete_score', 'contribution',
        'relevancy'
    ]]
    if (customer_prospect_relevance.shape[0] == 1):
        avg_rating = customer_prospect_relevance['avg_rating'].values[0]
        user_similarity = 5.0
        user_contribution = avg_rating * user_similarity
        user_relevancy = customer_prospect_relevance['relevancy'].values[0]

        user_df = pd.DataFrame({
            'restaurant': ["User's Average Rating"],
            "user_rating": [avg_rating],
            "compete_score": [user_similarity],
            "contribution": [user_contribution],
            "relevancy": [user_relevancy]
        })

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

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

    # Get info for comparison bar graph
    nodes = restaurant_graph_dict['nodes']
    nodes_comparison = nodes[nodes['order'] == 3][['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)
    selected_like_users_df = algorithm.like_users(selected_user_profile_df,
                                                  selected_user_id, lmu_limit)
    #like_users_str_df = selected_like_users_df[['user', 'user_id', 'lmu_score']].astype(str).drop_duplicates().head(like_limit)       # Issue #7
    like_users_str_df = selected_like_users_df[[
        'user', 'user_id', 'lmu_score'
    ]].drop_duplicates().head(like_limit)

    # restaurant_graph_dict, like_users, relevance, nodes_comparison
    return relevance, nodes_comparison, like_users_str_df