Created
July 2, 2017 18:41
-
-
Save Saurabh7/3b9b96decd8665917a9d0bf89ca73229 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<script>jQuery(function() {if (jQuery(\"body.notebook_app\").length == 0) { jQuery(\".input_area\").toggle(); jQuery(\".prompt\").toggle();}});</script>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<button onclick=\"jQuery('.input_area').toggle(); jQuery('.prompt').toggle();\">Toggle code</button>" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "import IPython.core.display as di\n", | |
| "\n", | |
| "# This line will hide code by default when the notebook is exported as HTML\n", | |
| "di.display_html('<script>jQuery(function() {if (jQuery(\"body.notebook_app\").length == 0) { jQuery(\".input_area\").toggle(); jQuery(\".prompt\").toggle();}});</script>', raw=True)\n", | |
| "\n", | |
| "# This line will add a button to toggle visibility of code blocks, for use with the HTML export version\n", | |
| "di.display_html('''<button onclick=\"jQuery('.input_area').toggle(); jQuery('.prompt').toggle();\">Toggle code</button>''', raw=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from IPython.display import display, HTML\n", | |
| "from itertools import combinations, product\n", | |
| "import pandas as pd\n", | |
| "import json\n", | |
| "from jarvis.brain.utility.data_preparation import get_active_ad_accounts\n", | |
| "from jarvis.brain.insights.paths.path_pre_processor import PathPreProcessor\n", | |
| "from jarvis.brain.insights.paths.path_processor import PathProcessor\n", | |
| "from jarvis.brain.insights.paths.path_utility import create_levels_hash\n", | |
| "from flask.json import jsonify\n", | |
| "import pandas as pd\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "metrics_dict = {\"ga_cpv\": {\"numerator\": \"fb_spend\", \"denominator\":\"ga_visits\", \"diff\": -1},\n", | |
| " \"ga_cpt\": {\"numerator\": \"fb_spend\", \"denominator\":\"ga_transactions\", \"diff\": -1}}\n", | |
| "goal_metric = [\"ga_cpv\", \"ga_cpt\"]\n", | |
| "\n", | |
| "# metrics_dict = {\"fb_ctr\": {\"numerator\": \"fb_inline_link_clicks\", \"denominator\":\"fb_impressions\", \"diff\": 1}}\n", | |
| "# goal_metric = \"fb_ctr\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "args = {\n", | |
| " \"user_id\":\"0\",\n", | |
| " \"account_ids\":{\"facebook\": [261]},\n", | |
| " \"start_date\":\"2017-06-23\",\n", | |
| " \"end_date\":\"2017-06-29\",\n", | |
| " \"goal_metric\": goal_metric,\n", | |
| " \"metrics\": metrics_dict,\n", | |
| " \"num_results\":10,\n", | |
| " \"filters\": [{\"metric\": \"fb_spend\", \"operator\": \"$gt\", \"value\": 0}],\n", | |
| " \"num_trees\": 10,\n", | |
| " \"tag_list\":[\"Sale Campaign\", \"Product Category\",\n", | |
| " \"Image\"],\n", | |
| " \"dimensions\": None,\n", | |
| " \"goal\": None\n", | |
| "}\n", | |
| "\n", | |
| "ad_account = args['account_ids']\n", | |
| "num_levels = 3\n", | |
| "start_date = args['start_date']\n", | |
| "end_date = args['end_date']\n", | |
| "goal_metric = args['goal_metric']\n", | |
| "metrics_dict = args['metrics']\n", | |
| "dimensions = args['dimensions']\n", | |
| "filters = args['filters']\n", | |
| "tag_list = args['tag_list']\n", | |
| "\n", | |
| "aggregations = {}\n", | |
| "for gm in goal_metric:\n", | |
| " aggregations[metrics_dict[gm]['numerator']] = pd.np.sum\n", | |
| " aggregations[metrics_dict[gm]['denominator']] = pd.np.sum\n", | |
| "# aggregations = {metrics_dict[goal_metric]['numerator']: pd.np.sum,\n", | |
| "# metrics_dict[goal_metric]['denominator']: pd.np.sum}\n", | |
| "goal = args['goal']\n", | |
| "metrics = list(aggregations.keys())\n", | |
| "num_trees = args['num_trees'] if args['num_trees'] != None else 10\n", | |
| "\n", | |
| "excluded_tags = ['Campaigns', 'ad_id', 'Adsets', 'start_date', 'Ads', '_id', 'internal_id', 'Sale',\n", | |
| " 'Product - algo', 'Carousel Count', 'Ad Format', 'Dominant Color', 'Number of Faces',\n", | |
| " 'end_date', 'Active Ads Count in Adset', 'Active Adsets Count in Campaign',\n", | |
| " 'Labels', 'audience type', 'audience category', 'Product Set', 'Prominent Text',\n", | |
| " 'DominantColor', 'ProminentText', 'NumberofFaces', 'name', 'strategy', 'Current Adsets',\n", | |
| " 'Facebook Positions', 'AdsetStrategy', 'Publisher Platforms']\n", | |
| "\n", | |
| "num_results = args['num_results'] if args['num_results'] != None else 15\n", | |
| "\n", | |
| "path_pre_process = PathPreProcessor(ad_account, start_date, end_date,\n", | |
| " [], metrics, num_levels, aggregations,\n", | |
| " excluded_tags, filters, dimensions, tag_list=tag_list)\n", | |
| "\n", | |
| "import time\n", | |
| "t1 = time.time()\n", | |
| "path_pre_process.get_data(cross_channel=False)\n", | |
| "print('TIME USED', time.time() - t1)\n", | |
| "path_pre_process.clean_tags(path_pre_process.df, excluded_tags, metrics)\n", | |
| "path_pre_process.score_tags()\n", | |
| "path_pre_process.cluster_tags()\n", | |
| "spend = path_pre_process.get_channel_spend()\n", | |
| "\n", | |
| "global_tag_list = path_pre_process.tag_list\n", | |
| "\n", | |
| "path_model = PathProcessor(path_pre_process.df, goal_metric, metrics_dict, global_tag_list,\n", | |
| " aggregations, num_trees=num_trees, spend=spend)\n", | |
| "# overall_goal_metric=goal)\n", | |
| "path_model.set_comparison(True)\n", | |
| "\n", | |
| "good_df, bad_df, overall = path_model.execute_paths(num_results)\n", | |
| "good_df.replace([pd.np.inf, -pd.np.inf], 0, inplace=True)\n", | |
| "bad_df.replace([pd.np.inf, -pd.np.inf], 0, inplace=True)\n", | |
| "good_df.fillna(value=0, inplace=True)\n", | |
| "bad_df.fillna(value=0, inplace=True)\n", | |
| "\n", | |
| "output = {}\n", | |
| "good_df = good_df[:num_results]\n", | |
| "bad_df = bad_df[:num_results]\n", | |
| "overall_df = good_df.append(bad_df)\n", | |
| "overall_df = overall_df\n", | |
| "\n", | |
| "levels = [('level_' + str(idx)) for idx in range(num_levels)]\n", | |
| "overall_df['levels'] = overall_df[levels].values.tolist()\n", | |
| "overall_df['levels'] = overall_df['levels'].apply(create_levels_hash)\n", | |
| "\n", | |
| "overall_goal_metric = path_model.overall_goal_metrics\n", | |
| "avg_spend_prop = overall_df.spend_prop.mean()\n", | |
| "\n", | |
| "output['goal_axis'] = overall_goal_metric\n", | |
| "output['spend_axis'] = avg_spend_prop\n", | |
| "\n", | |
| "dfs = []\n", | |
| "\n", | |
| "# Separate output for different quadrants.\n", | |
| "dfs.append(overall_df[(overall_df['spend_prop'] < avg_spend_prop) & (\n", | |
| " overall_df[goal_metric] > overall_goal_metric)])\n", | |
| "\n", | |
| "dfs.append(overall_df[(overall_df['spend_prop'] > avg_spend_prop) & (\n", | |
| " overall_df[goal_metric] > overall_goal_metric)])\n", | |
| "\n", | |
| "dfs.append(overall_df[(overall_df['spend_prop'] > avg_spend_prop) & (\n", | |
| " overall_df[goal_metric] < overall_goal_metric)])\n", | |
| "\n", | |
| "dfs.append(overall_df[(overall_df['spend_prop'] < avg_spend_prop) & (\n", | |
| " overall_df[goal_metric] < overall_goal_metric)])\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/html": [ | |
| "<div>\n", | |
| "<table border=\"1\" class=\"dataframe\">\n", | |
| " <thead>\n", | |
| " <tr style=\"text-align: right;\">\n", | |
| " <th></th>\n", | |
| " <th>index</th>\n", | |
| " <th>level_0</th>\n", | |
| " <th>level_1</th>\n", | |
| " <th>level_2</th>\n", | |
| " <th>imp_factor</th>\n", | |
| " <th>ga_cpt</th>\n", | |
| " <th>spend_prop</th>\n", | |
| " <th>ga_cpv</th>\n", | |
| " <th>levels</th>\n", | |
| " </tr>\n", | |
| " </thead>\n", | |
| " <tbody>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0</td>\n", | |
| " <td>Sale Campaign : sale</td>\n", | |
| " <td>Product Category : Indya</td>\n", | |
| " <td>Image : e715ddbac9df21eeae6546df8ce0af83</td>\n", | |
| " <td>4737.765787</td>\n", | |
| " <td>1488.539167</td>\n", | |
| " <td>1.559732</td>\n", | |
| " <td>7.910748</td>\n", | |
| " <td>[{'custom_tags.Sale Campaign': 'sale'}, {'cust...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>0</th>\n", | |
| " <td>0</td>\n", | |
| " <td>Image : a1272cba13b15daab9b85c5c3b316fc7</td>\n", | |
| " <td>- : -</td>\n", | |
| " <td>- : -</td>\n", | |
| " <td>83694.715316</td>\n", | |
| " <td>8006.905000</td>\n", | |
| " <td>1.398309</td>\n", | |
| " <td>22.210555</td>\n", | |
| " <td>[{'custom_tags.Image': 'a1272cba13b15daab9b85c...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>1</th>\n", | |
| " <td>3</td>\n", | |
| " <td>Image : f7fe99a0277b16356551860627aa32a6</td>\n", | |
| " <td>- : -</td>\n", | |
| " <td>- : -</td>\n", | |
| " <td>65482.154755</td>\n", | |
| " <td>6835.254000</td>\n", | |
| " <td>2.984236</td>\n", | |
| " <td>16.703944</td>\n", | |
| " <td>[{'custom_tags.Image': 'f7fe99a0277b1635655186...</td>\n", | |
| " </tr>\n", | |
| " <tr>\n", | |
| " <th>2</th>\n", | |
| " <td>5</td>\n", | |
| " <td>Sale Campaign : sale</td>\n", | |
| " <td>Product Category : Multi Category</td>\n", | |
| " <td>- : -</td>\n", | |
| " <td>16641.609226</td>\n", | |
| " <td>3513.827500</td>\n", | |
| " <td>4.909180</td>\n", | |
| " <td>16.807546</td>\n", | |
| " <td>[{'custom_tags.Sale Campaign': 'sale'}, {'cust...</td>\n", | |
| " </tr>\n", | |
| " </tbody>\n", | |
| "</table>\n", | |
| "</div>" | |
| ], | |
| "text/plain": [ | |
| " index level_0 \\\n", | |
| "0 0 Sale Campaign : sale \n", | |
| "0 0 Image : a1272cba13b15daab9b85c5c3b316fc7 \n", | |
| "1 3 Image : f7fe99a0277b16356551860627aa32a6 \n", | |
| "2 5 Sale Campaign : sale \n", | |
| "\n", | |
| " level_1 \\\n", | |
| "0 Product Category : Indya \n", | |
| "0 - : - \n", | |
| "1 - : - \n", | |
| "2 Product Category : Multi Category \n", | |
| "\n", | |
| " level_2 imp_factor ga_cpt \\\n", | |
| "0 Image : e715ddbac9df21eeae6546df8ce0af83 4737.765787 1488.539167 \n", | |
| "0 - : - 83694.715316 8006.905000 \n", | |
| "1 - : - 65482.154755 6835.254000 \n", | |
| "2 - : - 16641.609226 3513.827500 \n", | |
| "\n", | |
| " spend_prop ga_cpv levels \n", | |
| "0 1.559732 7.910748 [{'custom_tags.Sale Campaign': 'sale'}, {'cust... \n", | |
| "0 1.398309 22.210555 [{'custom_tags.Image': 'a1272cba13b15daab9b85c... \n", | |
| "1 2.984236 16.703944 [{'custom_tags.Image': 'f7fe99a0277b1635655186... \n", | |
| "2 4.909180 16.807546 [{'custom_tags.Sale Campaign': 'sale'}, {'cust... " | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "overall_df" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "{'ga_cpt': 1657.3468162083975, 'ga_cpv': 10.138876445278633}" | |
| ] | |
| }, | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "overall_goal_metric" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "df_1 = dfs[0]\n", | |
| "df_2 = dfs[1]\n", | |
| "df_3 = dfs[2]\n", | |
| "df_4 = dfs[3]\n", | |
| "\n", | |
| "# Change quadrants based on property of goal metric.\n", | |
| "# (e.g. quadrants are exchanged for roas <-> cpt)\n", | |
| "if metrics_dict[goal_metric]['diff'] == -1:\n", | |
| " df_2, df_3 = df_3, df_2\n", | |
| " df_4, df_1 = df_1, df_4" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from bokeh.charts import Scatter, output_file, show\n", | |
| "from bokeh.io import output_notebook, push_notebook\n", | |
| "from bokeh.plotting import figure, ColumnDataSource\n", | |
| "from bokeh.models import HoverTool\n", | |
| "from bokeh.models.widgets.tables import DataTable, TableColumn\n", | |
| "from bokeh.models import Span\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "output_notebook()" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "all_df = df_1.append(df_2, ignore_index=True)\n", | |
| "all_df = all_df.append(df_3, ignore_index=True)\n", | |
| "all_df = all_df.append(df_4, ignore_index=True)\n", | |
| "\n", | |
| "def round_float(s):\n", | |
| " if s.dtype == 'float64':\n", | |
| " s = s.round(7)\n", | |
| " return s\n", | |
| "data = output\n", | |
| "input_json = args\n", | |
| "avg_spend_prop = float(data['spend_axis'])\n", | |
| "\n", | |
| "tooltips=[\n", | |
| " ('lvl 0:', '@level_0'),\n", | |
| " ('lvl 1:', '@level_1'),\n", | |
| " ('lvl 2:', '@level_2'),\n", | |
| " ('lvl 3:', '@level_3'),\n", | |
| " (input_json['goal_metric'], '@'+input_json['goal_metric']),\n", | |
| " ('Projected % cpt change', '@Projected_change'),\n", | |
| " ('Spend Prop', '@spend_prop')\n", | |
| "]\n", | |
| "\n", | |
| "hover = HoverTool(tooltips=tooltips)\n", | |
| "hover1 = HoverTool(tooltips=tooltips)\n", | |
| "\n", | |
| "def build_bokeh_trace(df_obj, num):\n", | |
| " df_obj = df_obj.copy()\n", | |
| " if len(df_obj):\n", | |
| " df_obj = df_obj.sort_values(by='imp_factor', ascending=False)\n", | |
| " df_plotly = df_obj.iloc[0:num].copy()\n", | |
| " df_plotly['idx'] = df_plotly.index\n", | |
| " df_plotly['size'] = df_plotly['imp_factor']\n", | |
| " # range r1 to r2\n", | |
| " r1=8\n", | |
| " r2=30\n", | |
| " min_r = df_plotly['size'].min()\n", | |
| " max_r = df_plotly['size'].max()\n", | |
| " df_plotly['size'] = ((df_plotly['size'] - min_r )*(r2-r1) / (max_r - min_r)) + r1\n", | |
| " df_plotly['overall'] = data['goal_axis']\n", | |
| " df_plotly['overall_spend_prop'] = avg_spend_prop\n", | |
| " else:\n", | |
| " return ColumnDataSource(df_obj)\n", | |
| "\n", | |
| " source = ColumnDataSource(df_plotly)\n", | |
| " return source\n", | |
| "\n", | |
| "spend_source_1 = build_bokeh_trace(df_1, 10)\n", | |
| "spend_source_2 = build_bokeh_trace(df_2, 10)\n", | |
| "spend_source_3 = build_bokeh_trace(df_3, 10)\n", | |
| "spend_source_4 = build_bokeh_trace(df_4, 10)\n", | |
| "if metrics_dict[goal_metric]['diff'] == -1:\n", | |
| " df_2, df_3 = df_3, df_2\n", | |
| " df_4, df_1 = df_1, df_4\n", | |
| "table_source_1 = ColumnDataSource(df_1.apply(round_float))\n", | |
| "table_source_2 = ColumnDataSource(df_2.apply(round_float))\n", | |
| "table_source_3 = ColumnDataSource(df_3.apply(round_float))\n", | |
| "table_source_4 = ColumnDataSource(df_4.apply(round_float))\n", | |
| "\n", | |
| "columns = [\n", | |
| " TableColumn(field=\"level_0\", title=\"level 0\"),\n", | |
| " TableColumn(field=\"level_1\", title=\"level 1\"),\n", | |
| " TableColumn(field=\"level_2\", title=\"level 2\"),\n", | |
| " TableColumn(field=\"level_3\", title=\"level 3\"),\n", | |
| " TableColumn(field=\"other_{0}\".format(input_json['goal_metric']), title=\"Other {0}\".format(input_json['goal_metric'])),\n", | |
| " TableColumn(field=input_json['goal_metric'], title=input_json['goal_metric'],),\n", | |
| " TableColumn(field=\"spend_prop\", title=\"Spend %\"),\n", | |
| " TableColumn(field=\"imp_factor\", title=\"Impact factor\"),\n", | |
| "]\n", | |
| "\n", | |
| "\n", | |
| "# Spend sorted.\n", | |
| "p1 = figure(plot_width=800, plot_height=500, tools=[hover1],\n", | |
| " title=\"Performance by paths.\")\n", | |
| "p1.xaxis.axis_label = 'Spend Proportion'\n", | |
| "p1.yaxis.axis_label = input_json['goal_metric']\n", | |
| "\n", | |
| "if len(df_1):\n", | |
| " prop_plot_1 = p1.circle('spend_prop', input_json['goal_metric'], fill_color='orange', source=spend_source_1, size='size')\n", | |
| "if len(df_2):\n", | |
| " prop_plot_2 = p1.circle('spend_prop', input_json['goal_metric'], fill_color='red', source=spend_source_2, size='size')\n", | |
| "if len(df_3):\n", | |
| " prop_plot_3 = p1.circle('spend_prop', input_json['goal_metric'], fill_color='green', source=spend_source_3, size='size')\n", | |
| "if len(df_4):\n", | |
| " prop_plot_4 = p1.circle('spend_prop', input_json['goal_metric'], fill_color='blue', source=spend_source_4, size='size')\n", | |
| "\n", | |
| "# overall_line = p1.line('spend_prop', 'overall', source=bad_spend_source)\n", | |
| "avg_spend = Span(location=avg_spend_prop, dimension='height', line_color='black', line_width=3)\n", | |
| "avg_cpt = Span(location=data['goal_axis'], dimension='width', line_color='black', line_width=3)\n", | |
| "\n", | |
| "p1.add_layout(avg_spend)\n", | |
| "p1.add_layout(avg_cpt)\n", | |
| "# overall_spend_prop = p1.line('overall_spend_prop', goal_metric, source=bad_spend_source)\n", | |
| "# overall_spend_prop = p1.line('overall_spend_prop', goal_metric, source=good_spend_source)\n", | |
| "\n", | |
| "prop_plot = show(p1, notebook_handle=True)\n", | |
| "\n", | |
| "# Tables.\n", | |
| "print('Q1')\n", | |
| "if len(df_1):\n", | |
| " data_table_1 = DataTable(source=table_source_1, columns=columns, width=1000)\n", | |
| " table_1 = show(data_table_1, notebook_handle=True)\n", | |
| "\n", | |
| "print('Q2')\n", | |
| "if len(df_2):\n", | |
| " data_table_2 = DataTable(source=table_source_2, columns=columns, width=1000)\n", | |
| " table_2 = show(data_table_2, notebook_handle=True)\n", | |
| "\n", | |
| "print('Q3')\n", | |
| "if len(df_3):\n", | |
| " data_table_3 = DataTable(source=table_source_3, columns=columns, width=1000)\n", | |
| " table_3 = show(data_table_3, notebook_handle=True)\n", | |
| "\n", | |
| "print('Q4')\n", | |
| "if len(df_4):\n", | |
| " data_table_4 = DataTable(source=table_source_4, columns=columns, width=1000)\n", | |
| " table_4 = show(data_table_4, notebook_handle=True)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from jarvis.brain.utility.data_preparation import (get_metrics_date_range, get_channel_metrics_date_range)\n", | |
| "\n", | |
| "def extract_custom_tags(df, custom_tag_column=True):\n", | |
| " \"\"\"Extracting custom tags as individual features.\"\"\"\n", | |
| " if custom_tag_column:\n", | |
| " df = pd.concat([df, pd.DataFrame((d for idx, d in df['custom_tags'].iteritems()))], axis=1)\n", | |
| " del df['custom_tags']\n", | |
| "\n", | |
| " selected_columns = [col for col in df.columns\n", | |
| " if (type(df[col].iloc[0]) != dict or df[col][:1].iloc[0]['source'] in ['AUTO', 'USER'])]\n", | |
| " selected_columns.remove('Campaigns') \n", | |
| " selected_columns.remove('Adsets')\n", | |
| " selected_columns.remove('Ads') \n", | |
| "\n", | |
| " df_ = df[selected_columns]\n", | |
| " df_ = df_.applymap(lambda x: x['name'] if (type(x) == dict and 'name' in x) else x)\n", | |
| " df__ = df[['Campaigns', 'Adsets', 'Ads']]\n", | |
| " df__=df__.applymap(lambda x: x['metadata']['name'] if (type(x) == dict and 'metadata' in x and 'name' in x['metadata']) else x)\n", | |
| " return df_.join(df__)\n", | |
| "\n", | |
| "df = get_metrics_date_range(args['account_ids']['facebook'], 'ad', args['start_date'], args['end_date'],\n", | |
| " metrics + [path_pre_process.get_channel_spend()], dimensions=['custom_tags'], custom_tags=False,\n", | |
| " active=False, rails_api=True, page_size=4000, rename_id=False,\n", | |
| " filters=args['filters'], ad_channel='facebook')\n", | |
| "\n", | |
| "df = extract_custom_tags(df)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "from colorama import Fore, Back, Style\n", | |
| "def path_to_dict(df1,df,good=True):\n", | |
| " ads=[]\n", | |
| " tot_imp=0\n", | |
| " df2=df1.copy()\n", | |
| " for index,row in df2.iterrows():\n", | |
| " key_list=[]\n", | |
| " value_list=()\n", | |
| " df_=df.copy()\n", | |
| " path_dict=dict()\n", | |
| " path_dict[index]=dict()\n", | |
| " for i in range(0, num_levels):\n", | |
| " if(row['level_' +str(i)] != '- : -'):\n", | |
| " level = row['level_' + str(i)]\n", | |
| " level = level.split(':')\n", | |
| " key = level[0]\n", | |
| " key=key.strip()\n", | |
| " level = level[1]\n", | |
| " level = level.strip()\n", | |
| " path_dict[index][key]=level\n", | |
| " for key, value in path_dict[index].items():\n", | |
| " key_list.append(key)\n", | |
| " value_list=value_list+(value ,)\n", | |
| " df_=df_[df_[key]==value]\n", | |
| " df_ = df_.applymap(lambda x: round(x,6) if type(x)==np.float64 else x)\n", | |
| " total_spend = df_.fb_spend.sum()\n", | |
| " total_spend=round(total_spend,5)\n", | |
| " total_num = df_[metrics_dict[goal_metric]['numerator']].sum()\n", | |
| " total_num =round(total_num,5)\n", | |
| " total_deno = df_[metrics_dict[goal_metric]['denominator']].sum()\n", | |
| " total_deno = round(total_deno,5)\n", | |
| " try:\n", | |
| " goal_metric_path=total_num/total_deno\n", | |
| " goal_metric_path=round(goal_metric_path,5)\n", | |
| " except:\n", | |
| " goal_metric_path=0\n", | |
| " df_=df_.groupby(['Campaigns','Adsets','Ads'],as_index=False).agg(aggregations)\n", | |
| " df_=df_.sort('fb_spend',ascending=False)\n", | |
| " df_=df_.reset_index(drop=True)\n", | |
| " df_[goal_metric+' : \\n'+str(goal_metric_path)]=round(df_[metrics_dict[goal_metric]['numerator']]/df_[metrics_dict[goal_metric]['denominator']],5)\n", | |
| " df_.replace([pd.np.inf, -pd.np.inf], 0, inplace=True)\n", | |
| " df_=df_.rename(columns = {metrics_dict[goal_metric]['numerator']:column_names['numerator']+' : \\n'+str(total_num),metrics_dict[goal_metric]['denominator']:column_names['denominator']+' : \\n'+str(total_deno)})\n", | |
| "# df_=df_.drop(['index'], axis = 1, inplace = True)\n", | |
| " print('Path :'+str(index))\n", | |
| " print(\"---------\")\n", | |
| " path_df=df2.loc[[index]]\n", | |
| " \n", | |
| " str_path = Fore.BLUE + path_df['level_0'][index] +'\\n'+ path_df['level_1'][index] + '\\n'+path_df['level_2'][index] #+'\\n'+ path_df['level_3'][index]\n", | |
| " str_path=str_path.replace('- : -','')\n", | |
| " str_path=str_path.strip()\n", | |
| " print(str_path)\n", | |
| " print('\\n')\n", | |
| " print('Spend Proportion here is '+ str(round(row['spend_prop'],5))+' %')\n", | |
| " \n", | |
| " if good:\n", | |
| " bg_color='lightgreen'\n", | |
| " else:\n", | |
| " bg_color='pink'\n", | |
| " df_=df_.style.set_table_styles(\n", | |
| " [{'selector': 'th',\n", | |
| " 'props': [('background-color', bg_color)]}] )\n", | |
| " display(df_)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "column_names=metrics_dict[goal_metric]" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "path_to_dict(df_1,df)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "path_to_dict(df_2,df)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "path_to_dict(df_3,df,False)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "scrolled": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "path_to_dict(df_4,df,False)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "anaconda-cloud": {}, | |
| "kernelspec": { | |
| "display_name": "Python [jarvis1]", | |
| "language": "python", | |
| "name": "Python [jarvis1]" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.2" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 1 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment