File size: 40,562 Bytes
ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 4733a1f ecd6340 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"from openai import OpenAI\n",
"import numpy as np\n",
"import pandas as pd\n",
"import json\n",
"import joblib\n",
"import tqdm\n",
"import os\n",
"import glob"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/research/dept8/gds/zjxu21/Anaconda3/envs/IATSF/lib/python3.12/site-packages/huggingface_hub/file_download.py:943: FutureWarning: `resume_download` is deprecated and will be removed in version 1.0.0. Downloads always resume when possible. If you want to force a new download, use `force_download=True`.\n",
" warnings.warn(\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n",
"flash_attn is not installed. Using PyTorch native attention implementation.\n"
]
}
],
"source": [
"from transformers import AutoModel\n",
"\n",
"# Initialize the model\n",
"model = AutoModel.from_pretrained(\"jinaai/jina-embeddings-v3\", trust_remote_code=True).to(device=\"cuda:0\")\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"\n",
"embeddings = model.encode(\n",
" [\"What is the weather like in Berlin today?\"],\n",
" truncate_dim=256\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(1, 256)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embeddings.shape"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['./weather/new-york/fast_general_formal_forecast_2017.json',\n",
" './weather/new-york/fast_general_formal_forecast_2018.json',\n",
" './weather/new-york/fast_general_formal_forecast_2019.json',\n",
" './weather/new-york/fast_general_formal_forecast_2020.json',\n",
" './weather/new-york/fast_general_formal_forecast_2021.json',\n",
" './weather/new-york/fast_general_formal_forecast_2022.json',\n",
" './weather/new-york/fast_general_formal_forecast_2023.json',\n",
" './weather/new-york/fast_general_formal_forecast_2024.json',\n",
" './weather/new-york/fast_general_formal_forecast_2025.json',\n",
" './weather/new-york/fast_general_weather_forecast_2017.json',\n",
" './weather/new-york/fast_general_weather_forecast_2018.json',\n",
" './weather/new-york/fast_general_weather_forecast_2019.json',\n",
" './weather/new-york/fast_general_weather_forecast_2020.json',\n",
" './weather/new-york/fast_general_weather_forecast_2021.json',\n",
" './weather/new-york/fast_general_weather_forecast_2022.json',\n",
" './weather/new-york/fast_general_weather_forecast_2023.json',\n",
" './weather/new-york/fast_general_weather_forecast_2024.json',\n",
" './weather/new-york/fast_general_weather_forecast_2025.json']"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"files = glob.glob(\"./weather/new-york/fast_general_*.json\")\n",
"files"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loaded 1460 records from ./weather/new-york/fast_general_formal_forecast_2017.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2017.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2017.pkl\n",
"Loaded 1460 records from ./weather/new-york/fast_general_formal_forecast_2018.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2018.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2018.pkl\n",
"Loaded 1460 records from ./weather/new-york/fast_general_formal_forecast_2019.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2019.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2019.pkl\n",
"Loaded 1464 records from ./weather/new-york/fast_general_formal_forecast_2020.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2020.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2020.pkl\n",
"Loaded 1460 records from ./weather/new-york/fast_general_formal_forecast_2021.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2021.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2021.pkl\n",
"Loaded 1460 records from ./weather/new-york/fast_general_formal_forecast_2022.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2022.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2022.pkl\n",
"Loaded 1459 records from ./weather/new-york/fast_general_formal_forecast_2023.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2023.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2023.pkl\n",
"Loaded 1464 records from ./weather/new-york/fast_general_formal_forecast_2024.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2024.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2024.pkl\n",
"Loaded 260 records from ./weather/new-york/fast_general_formal_forecast_2025.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_formal_forecast_2025.json\n",
"Saved embeddings to ./weather/new-york/fast_general_formal_embeddings_2025.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2017.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2017.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2017.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2018.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2018.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2018.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2019.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2019.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2019.pkl\n",
"Loaded 366 records from ./weather/new-york/fast_general_weather_forecast_2020.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2020.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2020.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2021.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2021.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2021.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2022.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2022.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2022.pkl\n",
"Loaded 365 records from ./weather/new-york/fast_general_weather_forecast_2023.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2023.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2023.pkl\n",
"Loaded 366 records from ./weather/new-york/fast_general_weather_forecast_2024.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2024.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2024.pkl\n",
"Loaded 65 records from ./weather/new-york/fast_general_weather_forecast_2025.json\n",
"Processing batch 1/1\n",
"Finished processing ./weather/new-york/fast_general_weather_forecast_2025.json\n",
"Saved embeddings to ./weather/new-york/fast_general_weather_embeddings_2025.pkl\n"
]
}
],
"source": [
"step = 1500\n",
"for file in files:\n",
" with open(file, \"r\") as f:\n",
" data = json.load(f)\n",
" print(f\"Loaded {len(data)} records from {file}\")\n",
" timestamps = list(data.keys())\n",
" emb_dict = {}\n",
" for i in range(0, len(timestamps), step):\n",
" if i + step > len(timestamps):\n",
" batch = timestamps[i:]\n",
" else:\n",
" batch = timestamps[i:i + step]\n",
" print(f\"Processing batch {i // step + 1}/{len(timestamps) // step + 1}\")\n",
" batch_list= []\n",
" len_list= [0]\n",
" for timestamp in batch:\n",
" _ = list(data[timestamp].values())\n",
" batch_list += _\n",
" len_list.append(len(batch_list))\n",
" batch_list = [f'New York City: {x}' for x in batch_list]\n",
" embeddings = model.encode(\n",
" batch_list,\n",
" truncate_dim=256\n",
" )\n",
" for j in range(len(batch)):\n",
" timestamp = batch[j]\n",
" emb_dict[timestamp] = embeddings[len_list[j]:len_list[j + 1],:]\n",
" print(f\"Finished processing {file}\")\n",
" # Save the embeddings\n",
" output_file = file.replace(\"forecast\", \"embeddings\")\n",
" output_file = output_file.replace(\".json\", \".pkl\")\n",
"\n",
" with open(output_file, \"wb\") as f:\n",
" joblib.dump(emb_dict, f)\n",
" print(f\"Saved embeddings to {output_file}\")\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## create static info"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"87"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"id_info = json.load(open('../id_info_imputed.json', 'r'))\n",
"len(id_info)"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"static_info = {}\n",
"\n",
"static_info['general_info'] = 'This dataset contains Average Speed of a Vehicle Traveled Between End Points data in km/h collected from various locations in New York City by sensors. The sampling rate is every 5 minutes. When no car is detected in the period, the speed is set to 0.'\n",
"static_info['downtime_prompt'] = \"The sensor is down for unknown reasons, readings set to 0. \"\n",
"static_info['channel_info'] = {}\n",
"for channel in id_info.keys():\n",
" static_info['channel_info'][channel] = f\"Sensor {channel} is located at {id_info[channel]['borough']}, with segment of {id_info[channel]['link']}.\""
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['general_info', 'downtime_prompt', 'channel_info'])"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"json.dump(static_info, open(\"../static_info.json\", 'w'))\n",
"static_info.keys()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# load static_info from json file\n",
"# static_info = json.load(open(\"../static_info.json\", 'r'))"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['This dataset contains Average Speed of a Vehicle Traveled Between End Points data in km/h collected from various locations in New York City by sensors. The sampling rate is every 5 minutes. When no car is detected in the period, the speed is set to 0.',\n",
" 'The sensor is down for unknown reasons, readings set to 0. ',\n",
" 'Sensor 204 is located at Queens, with segment of CIP N TNB - Whitestone Expwy S Exit 14 (Linden Pl).',\n",
" 'Sensor 184 is located at Bronx, with segment of CBE E TAYLOR AVENUE - CASTLE HILL AVENUE.',\n",
" 'Sensor 217 is located at Manhattan, with segment of FDR N Catherine Slip - 25th St.',\n",
" 'Sensor 221 is located at Manhattan, with segment of FDR S 25th St - Catherine Slip.',\n",
" 'Sensor 149 is located at Manhattan, with segment of BQE N Atlantic Ave - BKN Bridge Manhattan Side.',\n",
" 'Sensor 223 is located at Manhattan, with segment of FDR S Catherine Slip - BKN Bridge Manhattan Side.',\n",
" 'Sensor 145 is located at Manhattan, with segment of BKN Bridge Manhattan Side - FDR N Catherine Slip.',\n",
" 'Sensor 208 is located at Queens, with segment of CIP S LIE - Hempstead Tpk.',\n",
" 'Sensor 207 is located at Queens, with segment of CIP S Hempstead Tpk - Laurelton Pkwy @ SSP.',\n",
" 'Sensor 332 is located at Queens, with segment of Laurelton Pkwy S @ SSP - Belt Pkwy W 182nd St.',\n",
" 'Sensor 169 is located at Queens, with segment of Belt Pkwy E 182nd St - Laurelton Pkwy N @ SSP.',\n",
" 'Sensor 170 is located at Queens, with segment of Belt Pkwy W 182nd St - JFK Expressway.',\n",
" 'Sensor 171 is located at Queens, with segment of Belt Pkwy W JFK Expressway - VWE N Jamaica Ave.',\n",
" 'Sensor 315 is located at Queens, with segment of LIE W 108TH ST - 84TH ST.',\n",
" 'Sensor 330 is located at Manhattan, with segment of LINCOLN TUNNEL W NORTH TUBE NY - NJ.',\n",
" 'Sensor 345 is located at Bronx, with segment of MDE S HARLEM RIVER PARK - GWB W AMSTERDAM AVENUE UPPER LEVEL.',\n",
" 'Sensor 344 is located at Bronx, with segment of MDE S HARLEM RIVER PARK - GWB W AMSTERDAM AVENUE LOWER LEVEL.',\n",
" 'Sensor 191 is located at Bronx, with segment of CBE W MORRIS AVE - GWB W AMSTERDAM AVE (U/LVL).',\n",
" 'Sensor 213 is located at Manhattan, with segment of FDR N - TBB E 116TH STREET - MANHATTAN TRUSS.',\n",
" 'Sensor 399 is located at Manhattan, with segment of TBB W - FDR S MANHATTAN TRUSS - E116TH STREET.',\n",
" 'Sensor 395 is located at Queens, with segment of TBB N QUEENS ANCHORAGE - MANHATTAN LIFT SPAN.',\n",
" 'Sensor 410 is located at Brooklyn, with segment of VNB E SI GANTRY LOWER LEVEL - BROOLKYN GANTRY LOWER LEVEL.',\n",
" 'Sensor 347 is located at Queens, with segment of MDE S TBB EXIT RAMP - QUEENS ANCHORAGE.',\n",
" 'Sensor 140 is located at Queens, with segment of BE S TBB EXIT RAMP - MANHATTAN LIFT SPAN.',\n",
" 'Sensor 398 is located at Queens, with segment of TBB S MANHATTAN LIFT SPAN - QUEENS ANCHORAGE.',\n",
" 'Sensor 202 is located at Queens, with segment of CIP N ramp to TNB - TNB Queens Anchorage.',\n",
" 'Sensor 168 is located at Bronx, with segment of BWB S Toll Plaza - Queens Anchorage.',\n",
" 'Sensor 298 is located at Bronx, with segment of HRP S Lafayette Ave - BWB S Toll Plaza.',\n",
" 'Sensor 451 is located at Queens, with segment of Whitestone Expwy N Exit 14 (Linden Pl) - BWB N Queens Anchorage.',\n",
" 'Sensor 416 is located at Brooklyn, with segment of VNB W BROOKLYN GANTRY LOWER LEVEL - SI GANTRY LOWER LEVEL.',\n",
" 'Sensor 190 is located at Bronx, with segment of CBE W MORRIS AVE - GWB W AMSTERDAM AVE (L/LVL).',\n",
" 'Sensor 212 is located at Queens, with segment of CVE NB LIE - WILLETS PT BLVD.',\n",
" 'Sensor 318 is located at Queens, with segment of LIE WB LITTLE NECK PKWY - NB CIP.',\n",
" 'Sensor 211 is located at Queens, with segment of CVE NB GCP - WILLETS PT BLVD.',\n",
" 'Sensor 319 is located at Queens, with segment of LIE WB LITTLE NECK PKWY - NB CVE.',\n",
" 'Sensor 311 is located at Queens, with segment of LIE E 84TH ST - 108TH ST.',\n",
" 'Sensor 419 is located at Staten island, with segment of VNB W-SIE W SI GANTRY UPPER LEVEL - FINGERBOARD ROAD.',\n",
" 'Sensor 418 is located at Staten Island, with segment of VNB W-SIE W SI GANTRY LOWER LEVEL - FINGERBOARD ROAD.',\n",
" 'Sensor 387 is located at Staten Island, with segment of SIE W FINGERBOARD ROAD - CLOVE ROAD.',\n",
" 'Sensor 385 is located at Staten Island, with segment of SIE W BRADLEY AVENUE - WOOLEY AVENUE.',\n",
" 'Sensor 390 is located at Staten Island, with segment of SIE W WOOLEY AVENUE - RICHMOND AVENUE.',\n",
" 'Sensor 388 is located at Staten Island, with segment of SIE W RICHMOND AVENUE - SOUTH AVENUE.',\n",
" 'Sensor 412 is located at Brooklyn, with segment of VNB E-GOWANUS N BROOKLYN GANTRY LOWER LEVEL - 92ND STREET.',\n",
" 'Sensor 413 is located at Brooklyn, with segment of VNB E-GOWANUS N BROOKLYN GANTRY UPPER LEVEL - 92ND STREET.',\n",
" 'Sensor 258 is located at Brooklyn, with segment of GOW N 92ND STREET - 7TH AVENUE.',\n",
" 'Sensor 259 is located at Brooklyn, with segment of GOW N 9TH STREET - ATLANTIC AVENUE.',\n",
" 'Sensor 394 is located at Queens, with segment of TBB N QUEENS ANCHORAGE - BE N.',\n",
" 'Sensor 261 is located at Brooklyn, with segment of GOW S 7TH AVENUE - 92ND STREET.',\n",
" 'Sensor 154 is located at Brooklyn, with segment of BQE S - GOW S ALTANTIC AVENUE - 9TH STREET.',\n",
" 'Sensor 155 is located at Brooklyn, with segment of BQE S 46TH STREET - LEONARD STREET.',\n",
" 'Sensor 153 is located at Brooklyn, with segment of BQE N LEONARD STREET - 46TH STREET.',\n",
" 'Sensor 453 is located at Queens, with segment of Whitestone Expwy S Exit 14 (Linden Pl) - VWE S MP8.65 (Exit 13 Northern Blvd).',\n",
" 'Sensor 428 is located at Queens, with segment of VWE S MP8.65 (Exit 13 Northern Blvd) - MP6.39 (Exit 11 Jewel Ave).',\n",
" 'Sensor 129 is located at Bronx, with segment of BE N STRATFORD AVENUE - CASTLE HILL AVE.',\n",
" 'Sensor 126 is located at Bronx, with segment of BE N Castle Hill Avenue - Griswold Ave.',\n",
" 'Sensor 295 is located at Bronx, with segment of HRP N LAFAYETTE AVENUE - E TREMONT AVENUE.',\n",
" 'Sensor 427 is located at Queens, with segment of VWE S MP6.39 (Exit 11 Jewel Ave) - MP4.63 (Exit 6 Jamaica Ave).',\n",
" 'Sensor 142 is located at Bronx, with segment of BE S Griswold - Castle Hill Avenue.',\n",
" 'Sensor 185 is located at Bronx, with segment of CBE W CASTLE HILL AVENUE - TAYLOR AVENUE.',\n",
" 'Sensor 426 is located at Queens, with segment of VWE S MP4.63 (Exit 6 Jamaica Ave) - MP2.66 (Exit 2 Roackaway Blvd).',\n",
" 'Sensor 425 is located at Queens, with segment of VWE S MP2.66 (Exit 2 Rockaway Blvd) - Belt Pkwy E 182nd St.',\n",
" 'Sensor 178 is located at Bronx, with segment of CBE E CASTLE HILL AVE - BE N WATERBURY AVE.',\n",
" 'Sensor 422 is located at Queens, with segment of VWE N MP4.63 (Exit 6 - Jamaica Ave) - MP6.39 (Exit 11 Jewel Ave).',\n",
" 'Sensor 423 is located at Queens, with segment of VWE N MP6.39 (Exit 11 Jewel Ave) - MP8.65 (Exit 13 Northern Blvd).',\n",
" 'Sensor 424 is located at Queens, with segment of VWE N MP8.64 (Exit 13 Northern Blvd) - Whitestone Expwy Exit 14 (Linden Pl).',\n",
" 'Sensor 165 is located at Bronx, with segment of BWB N Toll Plaza - HRP N Lafayatte Ave.',\n",
" 'Sensor 177 is located at Bronx, with segment of CBE E AMSTERDAM AVE(U/LVL) - MORRIS AVE.',\n",
" 'Sensor 172 is located at Bronx, with segment of CBE AMSTERDAM AVE (L/LVL) - MORRIS AVE.',\n",
" 'Sensor 167 is located at Queens, with segment of BWB S Queens Anchorage - WSE S Exit 14 (Linden Pl).',\n",
" 'Sensor 257 is located at Brooklyn, with segment of GOW N 7TH AVENUE - 9TH STREET.',\n",
" 'Sensor 222 is located at Manhattan, with segment of FDR S 63rd - 25th St.',\n",
" 'Sensor 215 is located at Manhattan, with segment of FDR N 25th - 63rd St.',\n",
" 'Sensor 265 is located at Manhattan, with segment of GWB E LOWER LEVEL PLAZA - CBE E LOWER LEVEL AMSTERDAM AVE.',\n",
" 'Sensor 380 is located at Staten Island, with segment of SIE E VNB E FINGERBOARD ROAD - SI GANTRY UPPER LEVEL.',\n",
" 'Sensor 137 is located at Bronx, with segment of BE S CASTLE HILL AVENUE - STRATFORD AVENUE.',\n",
" 'Sensor 124 is located at Manhattan, with segment of BBT W Toll Plaza - Manhattan Portal.',\n",
" 'Sensor 205 is located at Queens, with segment of CIP NB GCP - TNB.',\n",
" 'Sensor 206 is located at Queens, with segment of CIP N LIE ramp - TNB.',\n",
" 'Sensor 405 is located at Queens, with segment of TNB S Qns Anchorage - CIP S @ TNB.',\n",
" 'Sensor 376 is located at Staten Island, with segment of SIE E CLOVE ROAD - FINGERBOARD ROAD.',\n",
" 'Sensor 375 is located at Staten Island, with segment of SIE E BRADLEY AVENUE - CLOVE ROAD.',\n",
" 'Sensor 381 is located at Staten Island, with segment of SIE E WOOLEY AVENUE - BRADLEY AVENUE.',\n",
" 'Sensor 377 is located at Staten Island, with segment of SIE E RICHMOND AVENUE - WOOLEY AVENUE.',\n",
" 'Sensor 378 is located at Staten Island, with segment of SIE E SOUTH AVENUE - RICHMOND AVENUE.',\n",
" 'Sensor 435 is located at Staten Island, with segment of WSE N-SIE E SOUTH AVENUE - SOUTH AVENUE.',\n",
" 'Sensor 384 is located at Staten Island, with segment of SIE W - WSE S SOUTH AVENUE - SOUTH AVENUE.',\n",
" 'Sensor 157 is located at Brooklyn, with segment of BQE S LEONARD STREET - ATLANTIC AVENUE.']"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"channels = list(static_info['channel_info'].keys())\n",
"\n",
"text_to_emb = [static_info['general_info'], static_info['downtime_prompt']] + [static_info['channel_info'][i] for i in channels]\n",
"text_to_emb"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"89"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"len(text_to_emb)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"embeddings = model.encode(\n",
" text_to_emb,\n",
" truncate_dim=256\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(89, 256)"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"embeddings.shape"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"static_info['general_info'] = embeddings[0:1,:]\n",
"static_info['downtime_prompt'] = embeddings[1:2,:]\n",
"for i in range(len(channels)):\n",
" static_info['channel_info'][channels[i]] = embeddings[i+2:i+3,:]"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['../static_info.pkl']"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"joblib.dump(static_info, \"../static_info.pkl\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------------\n",
"## Merge embeddings"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['./weather/queens/fast_general_formal_embeddings_2017.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2018.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2019.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2020.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2021.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2022.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2023.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2024.pkl',\n",
" './weather/queens/fast_general_formal_embeddings_2025.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2017.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2018.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2019.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2020.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2021.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2022.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2023.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2024.pkl',\n",
" './weather/queens/fast_general_weather_embeddings_2025.pkl']"
]
},
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"files = glob.glob(\"./weather/queens/fast_general_*.pkl\")\n",
"files"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Processing ./weather/queens/fast_general_formal_embeddings_2017.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2017.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2017.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2018.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2018.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2018.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2019.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2019.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2019.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2020.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2020.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2020.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2021.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2021.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2021.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2022.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2022.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2022.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2023.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2023.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2023.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2024.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2024.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2024.pkl\n",
"Processing ./weather/queens/fast_general_formal_embeddings_2025.pkl\n",
"Finished processing ./weather/queens/fast_general_formal_embeddings_2025.pkl\n",
"Saved embeddings to ./weather/fast_general_formal_embeddings_2025.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2017.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2017.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2017.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2018.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2018.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2018.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2019.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2019.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2019.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2020.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2020.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2020.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2021.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2021.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2021.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2022.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2022.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2022.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2023.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2023.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2023.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2024.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2024.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2024.pkl\n",
"Processing ./weather/queens/fast_general_weather_embeddings_2025.pkl\n",
"Finished processing ./weather/queens/fast_general_weather_embeddings_2025.pkl\n",
"Saved embeddings to ./weather/fast_general_weather_embeddings_2025.pkl\n"
]
}
],
"source": [
"a = {}\n",
"for i in files:\n",
" print(f\"Processing {i}\")\n",
" with open(i, \"rb\") as f:\n",
" data1 = joblib.load(f)\n",
" with open(i.replace(\"queens\", 'brooklyn'), \"rb\") as f:\n",
" data2 = joblib.load(f)\n",
" with open(i.replace(\"queens\", 'new-york'), \"rb\") as f:\n",
" data3 = joblib.load(f)\n",
"\n",
" for k in data1.keys():\n",
" b = []\n",
" b.append(data1[k])\n",
" b.append(data2[k])\n",
" b.append(data3[k])\n",
" b = np.concatenate(b, axis=0)\n",
" a[k] = b\n",
" print(f\"Finished processing {i}\")\n",
" # Save the embeddings\n",
" output_file = i.replace(\"queens/\", \"\")\n",
" joblib.dump(a, output_file)\n",
" print(f\"Saved embeddings to {output_file}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"--------\n",
"## test fake dynamic data"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.microsoft.datawrangler.viewer.v0+json": {
"columns": [
{
"name": "index",
"rawType": "object",
"type": "string"
},
{
"name": "0",
"rawType": "int64",
"type": "integer"
}
],
"conversionMethod": "pd.DataFrame",
"ref": "230da4ee-0271-483a-a2a8-be1983bd03d1",
"rows": [
[
"20250101",
"0"
],
[
"20250102",
"0"
],
[
"20250103",
"0"
],
[
"20250104",
"0"
],
[
"20250105",
"0"
],
[
"20250106",
"0"
],
[
"20250107",
"0"
],
[
"20250108",
"0"
],
[
"20250109",
"0"
],
[
"20250110",
"0"
],
[
"20250111",
"0"
],
[
"20250112",
"0"
],
[
"20250113",
"0"
],
[
"20250114",
"0"
],
[
"20250115",
"0"
],
[
"20250116",
"0"
],
[
"20250117",
"0"
],
[
"20250118",
"0"
],
[
"20250119",
"0"
],
[
"20250120",
"0"
],
[
"20250121",
"0"
],
[
"20250122",
"0"
],
[
"20250123",
"0"
],
[
"20250124",
"0"
],
[
"20250125",
"0"
],
[
"20250126",
"0"
],
[
"20250127",
"0"
],
[
"20250128",
"0"
],
[
"20250129",
"0"
],
[
"20250130",
"0"
],
[
"20250131",
"0"
],
[
"20250201",
"0"
],
[
"20250202",
"0"
],
[
"20250203",
"0"
],
[
"20250204",
"0"
],
[
"20250205",
"0"
],
[
"20250206",
"0"
],
[
"20250207",
"0"
],
[
"20250208",
"0"
],
[
"20250209",
"0"
],
[
"20250210",
"0"
],
[
"20250211",
"0"
],
[
"20250212",
"0"
],
[
"20250213",
"0"
],
[
"20250214",
"0"
],
[
"20250215",
"0"
],
[
"20250216",
"0"
],
[
"20250217",
"0"
],
[
"20250218",
"0"
],
[
"20250219",
"0"
]
],
"shape": {
"columns": 1,
"rows": 76
}
},
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>0</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>20250101</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250102</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250103</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250104</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250105</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250313</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250314</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250315</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250316</th>\n",
" <td>0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>20250317</th>\n",
" <td>0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>76 rows × 1 columns</p>\n",
"</div>"
],
"text/plain": [
" 0\n",
"20250101 0\n",
"20250102 0\n",
"20250103 0\n",
"20250104 0\n",
"20250105 0\n",
"... ..\n",
"20250313 0\n",
"20250314 0\n",
"20250315 0\n",
"20250316 0\n",
"20250317 0\n",
"\n",
"[76 rows x 1 columns]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame.from_dict({k: 0 for k in data.keys()}, orient='index')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "IATSF",
"language": "python",
"name": "python3"
},
"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.12.11"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|