|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "id": "60b3ebc2-9c36-4af5-a38d-e7866bbb27d0", |
| 7 | + "metadata": {}, |
| 8 | + "outputs": [], |
| 9 | + "source": [ |
| 10 | + "dataset_path = \"/home/pwhiddy/messages_24_2025_parquet_zstd/dataset\"" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "id": "324d9f76-841c-4011-a1a9-17f9d0d0e6e3", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "def human_format(num, decimals=3):\n", |
| 21 | + " \"\"\"\n", |
| 22 | + " Format a number into a human-readable string with K, M, B, T suffixes.\n", |
| 23 | + " \n", |
| 24 | + " Examples:\n", |
| 25 | + " 1432 -> 1.432K\n", |
| 26 | + " 4232000 -> 4.232M\n", |
| 27 | + " 7235000000 -> 7.235B\n", |
| 28 | + " \"\"\"\n", |
| 29 | + " magnitude = 0\n", |
| 30 | + " suffixes = ['', 'K', 'M', 'B', 'T', 'Q'] # Extend if needed\n", |
| 31 | + " while abs(num) >= 1000 and magnitude < len(suffixes) - 1:\n", |
| 32 | + " magnitude += 1\n", |
| 33 | + " num /= 1000.0\n", |
| 34 | + " return f\"{num:.{decimals}f}{suffixes[magnitude]}\"" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "id": "f98d7d53-571a-420b-9c45-6fe4a5e63e1a", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "import polars as pl\n", |
| 45 | + "pl.scan_parquet(dataset_path).collect_schema()" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "code", |
| 50 | + "execution_count": null, |
| 51 | + "id": "b7b25df9-18dc-49f2-8621-83e897db7e5f", |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "pl.scan_parquet(dataset_path).select(pl.col(\"user\"))" |
| 56 | + ] |
| 57 | + }, |
| 58 | + { |
| 59 | + "cell_type": "code", |
| 60 | + "execution_count": null, |
| 61 | + "id": "8a39a473-d053-4fd2-b6d0-ddd43dc7763f", |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "import polars as pl\n", |
| 66 | + "from tqdm.notebook import tqdm\n", |
| 67 | + "\n", |
| 68 | + "total = 0\n", |
| 69 | + "batch_size = 3500000\n", |
| 70 | + "\n", |
| 71 | + "lf = pl.scan_parquet(dataset_path)\n", |
| 72 | + "total_rows = lf.select(pl.len()).collect()['len'][0]\n", |
| 73 | + "\n", |
| 74 | + "pbar = tqdm(range(total_rows//batch_size))\n", |
| 75 | + "for row_idx in pbar:\n", |
| 76 | + " offset = row_idx * batch_size\n", |
| 77 | + " batch_count = lf.slice(offset, batch_size).select(\n", |
| 78 | + " pl.col(\"coords\").list.len().sum()\n", |
| 79 | + " ).collect()[0, 0]\n", |
| 80 | + " pbar.set_postfix_str(f\"coord count: {human_format(total)}\")\n", |
| 81 | + " total += batch_count\n", |
| 82 | + "\n", |
| 83 | + "print(total)" |
| 84 | + ] |
| 85 | + }, |
| 86 | + { |
| 87 | + "cell_type": "code", |
| 88 | + "execution_count": null, |
| 89 | + "id": "6716d283-ac10-4707-8281-53c746515568", |
| 90 | + "metadata": {}, |
| 91 | + "outputs": [ |
| 92 | + { |
| 93 | + "data": { |
| 94 | + "application/vnd.jupyter.widget-view+json": { |
| 95 | + "model_id": "e596b13cc68d4f9cb0499de0d6a4c22f", |
| 96 | + "version_major": 2, |
| 97 | + "version_minor": 0 |
| 98 | + }, |
| 99 | + "text/plain": [ |
| 100 | + " 0%| | 0/211 [00:00<?, ?it/s]" |
| 101 | + ] |
| 102 | + }, |
| 103 | + "metadata": {}, |
| 104 | + "output_type": "display_data" |
| 105 | + } |
| 106 | + ], |
| 107 | + "source": [ |
| 108 | + "import polars as pl\n", |
| 109 | + "from tqdm.notebook import tqdm\n", |
| 110 | + "\n", |
| 111 | + "user_counts = {}\n", |
| 112 | + "batch_size = 3500000\n", |
| 113 | + "\n", |
| 114 | + "lf = pl.scan_parquet(dataset_path)\n", |
| 115 | + "total_rows = lf.select(pl.len()).collect()['len'][0]\n", |
| 116 | + "\n", |
| 117 | + "def pretty_print_counts(user_counts):\n", |
| 118 | + " print({user: human_format(count) for user,count in user_counts.items()})\n", |
| 119 | + "\n", |
| 120 | + "pbar = tqdm(range(total_rows//batch_size))\n", |
| 121 | + "for row_idx in pbar:\n", |
| 122 | + " offset = row_idx * batch_size\n", |
| 123 | + " batch_counts = lf.slice(offset, batch_size).group_by('user').agg(\n", |
| 124 | + " pl.col(\"coords\").list.len().sum()\n", |
| 125 | + " ).collect()\n", |
| 126 | + " py_counts = dict(zip(batch_counts[:, 0], batch_counts[:, 1]))\n", |
| 127 | + " for user, count in py_counts.items():\n", |
| 128 | + " if user not in user_counts.keys():\n", |
| 129 | + " user_counts[user] = count\n", |
| 130 | + " else:\n", |
| 131 | + " user_counts[user] += count\n", |
| 132 | + " if row_idx % 4 == 0:\n", |
| 133 | + " pretty_print_counts(user_counts)\n", |
| 134 | + " #total += batch_count\n", |
| 135 | + "print(\"done!\")\n", |
| 136 | + "pretty_print_counts(user_counts)" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "id": "b3f9575a-c108-4678-93f5-dbebb3ebb362", |
| 143 | + "metadata": {}, |
| 144 | + "outputs": [], |
| 145 | + "source": [] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "id": "0905dc31-3976-4262-9723-f1249d661094", |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [], |
| 153 | + "source": [] |
| 154 | + } |
| 155 | + ], |
| 156 | + "metadata": { |
| 157 | + "kernelspec": { |
| 158 | + "display_name": "Python 3.14", |
| 159 | + "language": "python", |
| 160 | + "name": "python314" |
| 161 | + }, |
| 162 | + "language_info": { |
| 163 | + "codemirror_mode": { |
| 164 | + "name": "ipython", |
| 165 | + "version": 3 |
| 166 | + }, |
| 167 | + "file_extension": ".py", |
| 168 | + "mimetype": "text/x-python", |
| 169 | + "name": "python", |
| 170 | + "nbconvert_exporter": "python", |
| 171 | + "pygments_lexer": "ipython3", |
| 172 | + "version": "3.14.2" |
| 173 | + } |
| 174 | + }, |
| 175 | + "nbformat": 4, |
| 176 | + "nbformat_minor": 5 |
| 177 | +} |
0 commit comments