{"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":"2023-10-29T12:39:48.656991Z","iopub.execute_input":"2023-10-29T12:39:48.658226Z","iopub.status.idle":"2023-10-29T12:39:49.852254Z","shell.execute_reply.started":"2023-10-29T12:39:48.658178Z","shell.execute_reply":"2023-10-29T12:39:49.851043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!sudo apt-get install -y graphviz graphviz-dev\n!pip install pygraphviz","metadata":{"execution":{"iopub.status.busy":"2023-10-29T12:40:00.223387Z","iopub.execute_input":"2023-10-29T12:40:00.224131Z","iopub.status.idle":"2023-10-29T12:40:28.866454Z","shell.execute_reply.started":"2023-10-29T12:40:00.224087Z","shell.execute_reply":"2023-10-29T12:40:28.865138Z"},"_kg_hide-output":true,"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/cm-institute-detect-sleep-states-kernel-stats/Child_Mind_Institute_Detect_Sleep_States_kernel_stats.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-10-29T12:40:28.869283Z","iopub.execute_input":"2023-10-29T12:40:28.869753Z","iopub.status.idle":"2023-10-29T12:40:28.936211Z","shell.execute_reply.started":"2023-10-29T12:40:28.869710Z","shell.execute_reply":"2023-10-29T12:40:28.935131Z"},"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":"2023-10-29T12:40:28.937399Z","iopub.execute_input":"2023-10-29T12:40:28.937693Z","iopub.status.idle":"2023-10-29T12:40:28.951716Z","shell.execute_reply.started":"2023-10-29T12:40:28.937668Z","shell.execute_reply":"2023-10-29T12:40:28.950337Z"},"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":"2023-10-29T12:40:28.954014Z","iopub.execute_input":"2023-10-29T12:40:28.954341Z","iopub.status.idle":"2023-10-29T12:40:28.965120Z","shell.execute_reply.started":"2023-10-29T12:40:28.954313Z","shell.execute_reply":"2023-10-29T12:40:28.964050Z"},"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":"2023-10-29T12:40:28.966138Z","iopub.execute_input":"2023-10-29T12:40:28.966921Z","iopub.status.idle":"2023-10-29T12:40:28.978307Z","shell.execute_reply.started":"2023-10-29T12:40:28.966886Z","shell.execute_reply":"2023-10-29T12:40:28.977517Z"},"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":"2023-10-29T12:40:28.979197Z","iopub.execute_input":"2023-10-29T12:40:28.979508Z","iopub.status.idle":"2023-10-29T12:40:29.019691Z","shell.execute_reply.started":"2023-10-29T12:40:28.979481Z","shell.execute_reply":"2023-10-29T12:40:29.018893Z"},"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":"2023-10-29T12:40:29.021192Z","iopub.execute_input":"2023-10-29T12:40:29.021469Z","iopub.status.idle":"2023-10-29T12:40:29.029480Z","shell.execute_reply.started":"2023-10-29T12:40:29.021444Z","shell.execute_reply":"2023-10-29T12:40:29.028521Z"},"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":"2023-10-29T12:40:29.030736Z","iopub.execute_input":"2023-10-29T12:40:29.031103Z","iopub.status.idle":"2023-10-29T12:40:29.042457Z","shell.execute_reply.started":"2023-10-29T12:40:29.031041Z","shell.execute_reply":"2023-10-29T12:40:29.041316Z"},"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":"2023-10-29T12:40:29.043882Z","iopub.execute_input":"2023-10-29T12:40:29.044273Z","iopub.status.idle":"2023-10-29T12:40:29.061790Z","shell.execute_reply.started":"2023-10-29T12:40:29.044236Z","shell.execute_reply":"2023-10-29T12:40:29.060611Z"},"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":"2023-10-29T12:40:29.066303Z","iopub.execute_input":"2023-10-29T12:40:29.066631Z","iopub.status.idle":"2023-10-29T12:40:29.073930Z","shell.execute_reply.started":"2023-10-29T12:40:29.066602Z","shell.execute_reply":"2023-10-29T12:40:29.073086Z"},"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":"2023-10-29T12:40:29.075595Z","iopub.execute_input":"2023-10-29T12:40:29.075888Z","iopub.status.idle":"2023-10-29T12:40:29.398625Z","shell.execute_reply.started":"2023-10-29T12:40:29.075864Z","shell.execute_reply":"2023-10-29T12:40:29.397583Z"},"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.05, 0.65])\nplt.grid(True)\nplt.show();","metadata":{"execution":{"iopub.status.busy":"2023-10-29T12:40:29.400074Z","iopub.execute_input":"2023-10-29T12:40:29.400691Z","iopub.status.idle":"2023-10-29T12:40:29.678705Z","shell.execute_reply.started":"2023-10-29T12:40:29.400660Z","shell.execute_reply":"2023-10-29T12:40:29.677849Z"},"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":"2023-10-29T12:40:29.680034Z","iopub.execute_input":"2023-10-29T12:40:29.680556Z","iopub.status.idle":"2023-10-29T12:40:29.693936Z","shell.execute_reply.started":"2023-10-29T12:40:29.680510Z","shell.execute_reply":"2023-10-29T12:40:29.692705Z"},"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":"2023-10-29T12:40:29.695203Z","iopub.execute_input":"2023-10-29T12:40:29.695595Z","iopub.status.idle":"2023-10-29T12:40:29.703792Z","shell.execute_reply.started":"2023-10-29T12:40:29.695564Z","shell.execute_reply":"2023-10-29T12:40:29.702646Z"},"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":"2023-10-29T12:40:29.705358Z","iopub.execute_input":"2023-10-29T12:40:29.705806Z","iopub.status.idle":"2023-10-29T12:40:29.715097Z","shell.execute_reply.started":"2023-10-29T12:40:29.705767Z","shell.execute_reply":"2023-10-29T12:40:29.714117Z"},"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":"2023-10-29T12:40:29.716449Z","iopub.execute_input":"2023-10-29T12:40:29.717298Z","iopub.status.idle":"2023-10-29T12:40:29.730747Z","shell.execute_reply.started":"2023-10-29T12:40:29.717258Z","shell.execute_reply":"2023-10-29T12:40:29.729683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize = (200, 30))\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":"2023-10-29T12:40:29.732078Z","iopub.execute_input":"2023-10-29T12:40:29.732418Z","iopub.status.idle":"2023-10-29T12:40:36.022294Z","shell.execute_reply.started":"2023-10-29T12:40:29.732391Z","shell.execute_reply":"2023-10-29T12:40:36.021338Z"},"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":"2023-10-29T12:40:36.023470Z","iopub.execute_input":"2023-10-29T12:40:36.024404Z","iopub.status.idle":"2023-10-29T12:40:36.032242Z","shell.execute_reply.started":"2023-10-29T12:40:36.024372Z","shell.execute_reply":"2023-10-29T12:40:36.030937Z"},"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":"2023-10-29T12:40:36.034122Z","iopub.execute_input":"2023-10-29T12:40:36.034599Z","iopub.status.idle":"2023-10-29T12:40:36.049273Z","shell.execute_reply.started":"2023-10-29T12:40:36.034569Z","shell.execute_reply":"2023-10-29T12:40:36.047891Z"},"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":"2023-10-29T12:40:36.050438Z","iopub.execute_input":"2023-10-29T12:40:36.050895Z","iopub.status.idle":"2023-10-29T12:40:36.075194Z","shell.execute_reply.started":"2023-10-29T12:40:36.050855Z","shell.execute_reply":"2023-10-29T12:40:36.074045Z"},"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":"2023-10-29T12:40:36.076459Z","iopub.execute_input":"2023-10-29T12:40:36.076971Z","iopub.status.idle":"2023-10-29T12:40:36.099334Z","shell.execute_reply.started":"2023-10-29T12:40:36.076943Z","shell.execute_reply":"2023-10-29T12:40:36.098214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}