{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import warnings\nwarnings.filterwarnings(\"ignore\")\n\nimport pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt\nimport networkx as nx\nfrom networkx.algorithms.traversal.depth_first_search import dfs_tree","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:46:11.596229Z","iopub.execute_input":"2022-11-10T12:46:11.596918Z","iopub.status.idle":"2022-11-10T12:46:12.347375Z","shell.execute_reply.started":"2022-11-10T12:46:11.596805Z","shell.execute_reply":"2022-11-10T12:46:12.345987Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!sudo apt-get install -y graphviz graphviz-dev\n!pip install pygraphviz","metadata":{"_kg_hide-output":true,"scrolled":true,"execution":{"iopub.status.busy":"2022-11-10T12:46:20.535132Z","iopub.execute_input":"2022-11-10T12:46:20.535502Z","iopub.status.idle":"2022-11-10T12:46:44.634470Z","shell.execute_reply.started":"2022-11-10T12:46:20.535461Z","shell.execute_reply":"2022-11-10T12:46:44.633113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('../input/google-ai4code-kernels-stats/google-ai4code_kernels.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:10.607895Z","iopub.execute_input":"2022-11-10T12:47:10.608252Z","iopub.status.idle":"2022-11-10T12:47:10.662980Z","shell.execute_reply.started":"2022-11-10T12:47:10.608221Z","shell.execute_reply":"2022-11-10T12:47:10.661713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Top-20 kernel creators ","metadata":{}},{"cell_type":"code","source":"df['KernelAuthor'].value_counts().head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:15.708101Z","iopub.execute_input":"2022-11-10T12:47:15.708440Z","iopub.status.idle":"2022-11-10T12:47:15.724339Z","shell.execute_reply.started":"2022-11-10T12:47:15.708414Z","shell.execute_reply":"2022-11-10T12:47:15.723043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Perfomance Tiers vs kernel creators ","metadata":{}},{"cell_type":"code","source":"df['KernelAuthorPerformanceTier'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:17.298527Z","iopub.execute_input":"2022-11-10T12:47:17.298903Z","iopub.status.idle":"2022-11-10T12:47:17.307079Z","shell.execute_reply.started":"2022-11-10T12:47:17.298850Z","shell.execute_reply":"2022-11-10T12:47:17.305725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Kernels medals ","metadata":{}},{"cell_type":"code","source":"df['medal'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:17.788167Z","iopub.execute_input":"2022-11-10T12:47:17.789245Z","iopub.status.idle":"2022-11-10T12:47:17.797355Z","shell.execute_reply.started":"2022-11-10T12:47:17.789188Z","shell.execute_reply":"2022-11-10T12:47:17.796164Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Medals for kernels creators (top-20)","metadata":{}},{"cell_type":"code","source":"df.groupby('KernelAuthor')['medal'].value_counts()\\\n                .unstack()[['GOLD', 'SILVER', 'BRONZE']]\\\n                .fillna(0).sort_values(['GOLD', 'SILVER', 'BRONZE'], ascending = False).head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:18.487529Z","iopub.execute_input":"2022-11-10T12:47:18.488179Z","iopub.status.idle":"2022-11-10T12:47:18.518947Z","shell.execute_reply.started":"2022-11-10T12:47:18.488145Z","shell.execute_reply":"2022-11-10T12:47:18.518021Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Kernels vs Language ","metadata":{}},{"cell_type":"code","source":"df['languageName'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:19.365867Z","iopub.execute_input":"2022-11-10T12:47:19.367272Z","iopub.status.idle":"2022-11-10T12:47:19.374863Z","shell.execute_reply.started":"2022-11-10T12:47:19.367222Z","shell.execute_reply":"2022-11-10T12:47:19.373747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# How many kernels are the forks? ","metadata":{}},{"cell_type":"code","source":"df['isFork'].fillna(0).mean()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:19.999496Z","iopub.execute_input":"2022-11-10T12:47:20.000763Z","iopub.status.idle":"2022-11-10T12:47:20.009197Z","shell.execute_reply.started":"2022-11-10T12:47:20.000707Z","shell.execute_reply":"2022-11-10T12:47:20.007343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# How many lines are changed in forks?","metadata":{}},{"cell_type":"code","source":"df['ForkLinesChanged'].describe()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:20.755440Z","iopub.execute_input":"2022-11-10T12:47:20.755782Z","iopub.status.idle":"2022-11-10T12:47:20.768077Z","shell.execute_reply.started":"2022-11-10T12:47:20.755754Z","shell.execute_reply":"2022-11-10T12:47:20.767319Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Use GPU or not? ","metadata":{}},{"cell_type":"code","source":"df['isGpuEnabled'].fillna(0).mean()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:21.417608Z","iopub.execute_input":"2022-11-10T12:47:21.417958Z","iopub.status.idle":"2022-11-10T12:47:21.425607Z","shell.execute_reply.started":"2022-11-10T12:47:21.417929Z","shell.execute_reply":"2022-11-10T12:47:21.424560Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Upvotes vs views ","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\nplt.scatter(df['totalVotes'].values, df['totalViews'].values)\nplt.xlabel('totalVotes')\nplt.ylabel('totalViews')\nplt.grid(True)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:22.279935Z","iopub.execute_input":"2022-11-10T12:47:22.280329Z","iopub.status.idle":"2022-11-10T12:47:22.483754Z","shell.execute_reply.started":"2022-11-10T12:47:22.280297Z","shell.execute_reply":"2022-11-10T12:47:22.483087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (10, 10))\nplt.scatter(df['totalVotes'].values, df['bestPublicScore'].values)\nplt.xlabel('totalVotes')\nplt.ylabel('bestPublicScore')\nplt.ylim([0.4, 0.95])\nplt.grid(True)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:22.761042Z","iopub.execute_input":"2022-11-10T12:47:22.761425Z","iopub.status.idle":"2022-11-10T12:47:22.928901Z","shell.execute_reply.started":"2022-11-10T12:47:22.761393Z","shell.execute_reply":"2022-11-10T12:47:22.927362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Most commented kernels ","metadata":{}},{"cell_type":"code","source":"df[['title', 'totalComments']].sort_values('totalComments', ascending = False).head(20).reset_index(drop = True)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:24.977123Z","iopub.execute_input":"2022-11-10T12:47:24.977665Z","iopub.status.idle":"2022-11-10T12:47:24.992787Z","shell.execute_reply.started":"2022-11-10T12:47:24.977634Z","shell.execute_reply":"2022-11-10T12:47:24.991032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Fork graph","metadata":{}},{"cell_type":"code","source":"forks = df[['title', 'totalVotes', 'id', 'forkParent']].values\nvotes_dict = {i:v for i,v in zip(forks[:, 2], forks[:, 1])}","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:26.185896Z","iopub.execute_input":"2022-11-10T12:47:26.186303Z","iopub.status.idle":"2022-11-10T12:47:26.193975Z","shell.execute_reply.started":"2022-11-10T12:47:26.186273Z","shell.execute_reply":"2022-11-10T12:47:26.192724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"G = nx.DiGraph()\nfor title, votes, k_id, parent_id in forks:\n    G.add_node(k_id, title = title, votes = votes)\n    \nfor title, votes, k_id, parent_id in forks:\n    if parent_id != -1:\n        G.add_edge(parent_id, k_id)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:26.582620Z","iopub.execute_input":"2022-11-10T12:47:26.584019Z","iopub.status.idle":"2022-11-10T12:47:26.589768Z","shell.execute_reply.started":"2022-11-10T12:47:26.583975Z","shell.execute_reply":"2022-11-10T12:47:26.588944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"connected_components = []\nfor cc in nx.connected_components(G.to_undirected()):\n    if len(cc) == 1:\n        G.remove_node(list(cc)[0])\n    else:\n        connected_components.append(cc)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:26.866179Z","iopub.execute_input":"2022-11-10T12:47:26.866564Z","iopub.status.idle":"2022-11-10T12:47:26.875791Z","shell.execute_reply.started":"2022-11-10T12:47:26.866531Z","shell.execute_reply":"2022-11-10T12:47:26.874308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (80, 10))\npos = nx.nx_agraph.graphviz_layout(G, prog=\"dot\")\nnx.draw(G, pos = pos, node_size=10000)\ntext = nx.draw_networkx_labels(G, pos)\nfor _, t in text.items():\n    t.set_rotation('vertical')\n    t.set_color('white')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:27.210070Z","iopub.execute_input":"2022-11-10T12:47:27.211187Z","iopub.status.idle":"2022-11-10T12:47:29.282961Z","shell.execute_reply.started":"2022-11-10T12:47:27.211133Z","shell.execute_reply":"2022-11-10T12:47:29.281744Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nodes_subtree_size = {}\nnodes_cum_votes = {}\nfor node in G.nodes():\n    subtree = dfs_tree(G, node)\n    nodes_subtree_size[node] = len(subtree.nodes()) - 1\n    nodes_cum_votes[node] = sum([votes_dict[n] if n in votes_dict else 0 for n in subtree.nodes()])\n        ","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:29.284690Z","iopub.execute_input":"2022-11-10T12:47:29.285185Z","iopub.status.idle":"2022-11-10T12:47:29.292992Z","shell.execute_reply.started":"2022-11-10T12:47:29.285154Z","shell.execute_reply":"2022-11-10T12:47:29.291613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"forks = df[['title', 'KernelAuthor', 'id', 'forkParent']]\nforks['TotalForkedKernels'] = forks['id'].map(nodes_subtree_size).fillna(0)\nforks['CumulatedVotes'] = forks['id'].map(nodes_cum_votes).fillna(0)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:29.294285Z","iopub.execute_input":"2022-11-10T12:47:29.294599Z","iopub.status.idle":"2022-11-10T12:47:29.310034Z","shell.execute_reply.started":"2022-11-10T12:47:29.294566Z","shell.execute_reply":"2022-11-10T12:47:29.308954Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Most forks kernels (top-20)","metadata":{}},{"cell_type":"code","source":"forks.sort_values('TotalForkedKernels', ascending = False).reset_index(drop = True).head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:29.311697Z","iopub.execute_input":"2022-11-10T12:47:29.312015Z","iopub.status.idle":"2022-11-10T12:47:29.335156Z","shell.execute_reply.started":"2022-11-10T12:47:29.311990Z","shell.execute_reply":"2022-11-10T12:47:29.333997Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Most cumulative votes kernels (top-20) ","metadata":{}},{"cell_type":"code","source":"forks.sort_values('CumulatedVotes', ascending = False).reset_index(drop = True).head(20)","metadata":{"execution":{"iopub.status.busy":"2022-11-10T12:47:30.462164Z","iopub.execute_input":"2022-11-10T12:47:30.462689Z","iopub.status.idle":"2022-11-10T12:47:30.481220Z","shell.execute_reply.started":"2022-11-10T12:47:30.462661Z","shell.execute_reply":"2022-11-10T12:47:30.479967Z"},"trusted":true},"execution_count":null,"outputs":[]}]}