File size: 46,482 Bytes
8af2e1d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "TOL_FILEPATH = \"../../data/\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Read in Links CSV files\n",
    "\n",
    "Full cargo archives have some more content than the [original `links_inner` file](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/fa08e0f6692a013e4e4f5ca30dd0e8883c0021d8/eol_realign/data/links_inner.csv) and the [original `links_manifest_cargo_on_md5.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/fa08e0f6692a013e4e4f5ca30dd0e8883c0021d8/eol_realign/data/links_manifest_cargo_on_md5.csv).\n",
    "\n",
    "From Matt:\n",
    "> It represents a series of inner merges:\n",
    "> 1. The list of images in the EOL cargo archive (used to create the webdatasets) with the EOL portion of `catalog.csv`\n",
    ">  - Merged on a combined identifier of `eol_content_id` and `eol_page_id`.\n",
    "> Result from 1) with the list of images redownloaded from all 3 media manifests we touched around the original download time (identified [here](https://huggingface.co/datasets/imageomics/ToL-EDA/discussions/5#6584808d92eba5ad69b8379f)).\n",
    ">  - Merged on MD5\n",
    ">\n",
    "> The result has 6,219,674 unique `treeoflife_id` entries and 6,146,917 unique `md5` entries. It has the combined `eol_content_id` and `eol_page_id` identifiers used at each stage:\n",
    "- `combined_id_catalog`: represents what's in the original catalog \n",
    "- `combined_id_manifest_full_manifest`: represents what's in the combined manifest from all 3 dates \n",
    "> It also has an indicator of which of the three media manifest files each entry was present in (one or more) in columns representing retreival date (eg., `source_0706`: indicates if it was present in the manifest retrieved on July 6, 2023). It also has the copyright owner and license information as contained in the media manifest files.\n",
    ">\n",
    "> Still needs to be run through the owner name finding code. ([owner_match](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/71ad5b49226a3a49634931005eeb340b3c96db13/scripts/match_owners.py) --note: this version uses inner merges, so will remove entries without media info. Probably should adjust this.)\n",
    "\n",
    "For reference on the numbers, there are 6,250,420 EOL images in TreeOfLife-10M (the catalog). Comparing with `links_inner` previously (from [`links_inner.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/fa08e0f6692a013e4e4f5ca30dd0e8883c0021d8/eol_realign/data/links_inner.csv)) we were missing 31,376 images, 29 of which were identified in [`links_manifest_cargo_on_md5.csv`](https://huggingface.co/datasets/imageomics/ToL-EDA/blob/fa08e0f6692a013e4e4f5ca30dd0e8883c0021d8/eol_realign/data/links_manifest_cargo_on_md5.csv)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "links_inner = pd.read_csv(\"../data/eol-cargo-archive_catalog_combined-manifest-checksums_links.csv\", low_memory=False)\n",
    "links_manifest_cargo = pd.read_csv(\"../data/eol-cargo-archive_combined-manifest-checksums_links.csv\", low_memory=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 6841202 entries, 0 to 6841201\n",
      "Data columns (total 30 columns):\n",
      " #   Column                           Non-Null Count    Dtype  \n",
      "---  ------                           --------------    -----  \n",
      " 0   filename_archive                 6841202 non-null  object \n",
      " 1   md5                              6841202 non-null  object \n",
      " 2   combined_id_archive              6841202 non-null  object \n",
      " 3   split                            6841202 non-null  object \n",
      " 4   treeoflife_id                    6841202 non-null  object \n",
      " 5   bioscan_part                     0 non-null        float64\n",
      " 6   bioscan_filename                 0 non-null        float64\n",
      " 7   inat21_filename                  0 non-null        float64\n",
      " 8   inat21_cls_name                  0 non-null        float64\n",
      " 9   inat21_cls_num                   0 non-null        float64\n",
      " 10  kingdom                          6551473 non-null  object \n",
      " 11  phylum                           6553206 non-null  object \n",
      " 12  class                            6532009 non-null  object \n",
      " 13  order                            6525621 non-null  object \n",
      " 14  family                           6506076 non-null  object \n",
      " 15  genus                            6486130 non-null  object \n",
      " 16  species                          6406307 non-null  object \n",
      " 17  common                           6841202 non-null  object \n",
      " 18  combined_id_catalog              6841202 non-null  object \n",
      " 19  filename_manifest                6841202 non-null  object \n",
      " 20  combined_id_manifest_redownload  6841202 non-null  object \n",
      " 21  medium_source_url                6841202 non-null  object \n",
      " 22  eol_full_size_copy_url           6841202 non-null  object \n",
      " 23  license_name                     6841202 non-null  object \n",
      " 24  copyright_owner                  6208498 non-null  object \n",
      " 25  expected_image_filename          6841202 non-null  object \n",
      " 26  source_0706                      6841202 non-null  bool   \n",
      " 27  source_0726                      6841202 non-null  bool   \n",
      " 28  source_1206                      6841202 non-null  bool   \n",
      " 29  combined_id_full_manifest        6841202 non-null  object \n",
      "dtypes: bool(3), float64(5), object(22)\n",
      "memory usage: 1.4+ GB\n"
     ]
    }
   ],
   "source": [
    "links_inner.info(show_counts=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 7514001 entries, 0 to 7514000\n",
      "Data columns (total 17 columns):\n",
      " #   Column                           Non-Null Count    Dtype \n",
      "---  ------                           --------------    ----- \n",
      " 0   filename_archive                 7514001 non-null  object\n",
      " 1   md5_archive                      7514001 non-null  object\n",
      " 2   combined_id_archive              7514001 non-null  object\n",
      " 3   filename_manifest                7514001 non-null  object\n",
      " 4   md5_combined_manifest            7514001 non-null  object\n",
      " 5   combined_id_manifest_redownload  7514001 non-null  object\n",
      " 6   eol_content_id                   7514001 non-null  int64 \n",
      " 7   eol_page_id                      7514001 non-null  int64 \n",
      " 8   medium_source_url                7514001 non-null  object\n",
      " 9   eol_full_size_copy_url           7514001 non-null  object\n",
      " 10  license_name                     7514001 non-null  object\n",
      " 11  copyright_owner                  6860825 non-null  object\n",
      " 12  expected_image_filename          7514001 non-null  object\n",
      " 13  source_0706                      7514001 non-null  bool  \n",
      " 14  source_0726                      7514001 non-null  bool  \n",
      " 15  source_1206                      7514001 non-null  bool  \n",
      " 16  combined_id_full_manifest        7514001 non-null  object\n",
      "dtypes: bool(3), int64(2), object(12)\n",
      "memory usage: 824.1+ MB\n"
     ]
    }
   ],
   "source": [
    "links_manifest_cargo.info(show_counts=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These both have about 600-700 more entries than the earlier files."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "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>filename_archive</th>\n",
       "      <th>md5_archive</th>\n",
       "      <th>combined_id_archive</th>\n",
       "      <th>filename_manifest</th>\n",
       "      <th>md5_combined_manifest</th>\n",
       "      <th>combined_id_manifest_redownload</th>\n",
       "      <th>eol_content_id</th>\n",
       "      <th>eol_page_id</th>\n",
       "      <th>medium_source_url</th>\n",
       "      <th>eol_full_size_copy_url</th>\n",
       "      <th>license_name</th>\n",
       "      <th>copyright_owner</th>\n",
       "      <th>expected_image_filename</th>\n",
       "      <th>source_0706</th>\n",
       "      <th>source_0726</th>\n",
       "      <th>source_1206</th>\n",
       "      <th>combined_id_full_manifest</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [filename_archive, md5_archive, combined_id_archive, filename_manifest, md5_combined_manifest, combined_id_manifest_redownload, eol_content_id, eol_page_id, medium_source_url, eol_full_size_copy_url, license_name, copyright_owner, expected_image_filename, source_0706, source_0726, source_1206, combined_id_full_manifest]\n",
       "Index: []"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# These should be the same\n",
    "links_manifest_cargo.loc[links_manifest_cargo[\"combined_id_manifest_redownload\"] != links_manifest_cargo[\"combined_id_full_manifest\"]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 822558 entries, 2 to 7513999\n",
      "Data columns (total 17 columns):\n",
      " #   Column                           Non-Null Count   Dtype \n",
      "---  ------                           --------------   ----- \n",
      " 0   filename_archive                 822558 non-null  object\n",
      " 1   md5_archive                      822558 non-null  object\n",
      " 2   combined_id_archive              822558 non-null  object\n",
      " 3   filename_manifest                822558 non-null  object\n",
      " 4   md5_combined_manifest            822558 non-null  object\n",
      " 5   combined_id_manifest_redownload  822558 non-null  object\n",
      " 6   eol_content_id                   822558 non-null  int64 \n",
      " 7   eol_page_id                      822558 non-null  int64 \n",
      " 8   medium_source_url                822558 non-null  object\n",
      " 9   eol_full_size_copy_url           822558 non-null  object\n",
      " 10  license_name                     822558 non-null  object\n",
      " 11  copyright_owner                  797147 non-null  object\n",
      " 12  expected_image_filename          822558 non-null  object\n",
      " 13  source_0706                      822558 non-null  bool  \n",
      " 14  source_0726                      822558 non-null  bool  \n",
      " 15  source_1206                      822558 non-null  bool  \n",
      " 16  combined_id_full_manifest        822558 non-null  object\n",
      "dtypes: bool(3), int64(2), object(12)\n",
      "memory usage: 96.5+ MB\n"
     ]
    }
   ],
   "source": [
    "# archive has replaced cargo\n",
    "# full manifest or manifest redownload to replace manifest checksums -- they are equal\n",
    "links_mismatch = links_manifest_cargo.loc[links_manifest_cargo[\"combined_id_archive\"] != links_manifest_cargo[\"combined_id_full_manifest\"]]\n",
    "links_mismatch.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There are 42 more mismatched entries. Only 13 additional unique images (MD5s)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "md5_archive                  624574\n",
       "combined_id_full_manifest    712540\n",
       "combined_id_archive          703446\n",
       "dtype: int64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "links_mismatch[[\"md5_archive\", \"combined_id_full_manifest\", \"combined_id_archive\"]].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# This is archive manifest mismatch\n",
    "links_mismatch.to_csv(\"../data/links_archive_manifest_IDmismatch.csv\", index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "catalog = pd.read_csv(TOL_FILEPATH + \"catalog.csv\", low_memory=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "# remove train_small and all non-EOL entries\n",
    "catalog = catalog.loc[catalog.split != \"train_small\"]\n",
    "eol_catalog = catalog.loc[catalog.eol_content_id.notna()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "split                   2\n",
       "treeoflife_id     6250420\n",
       "eol_content_id    6250420\n",
       "eol_page_id        503589\n",
       "dtype: int64"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eol_catalog[list(eol_catalog.columns)[:4]].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "eol_cols = [col for col in list(eol_catalog.columns) if \"bioscan\" not in col and \"inat\" not in col]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 6250420 entries, 956715 to 11000930\n",
      "Data columns (total 12 columns):\n",
      " #   Column          Non-Null Count    Dtype  \n",
      "---  ------          --------------    -----  \n",
      " 0   split           6250420 non-null  object \n",
      " 1   treeoflife_id   6250420 non-null  object \n",
      " 2   eol_content_id  6250420 non-null  float64\n",
      " 3   eol_page_id     6250420 non-null  float64\n",
      " 4   kingdom         5989611 non-null  object \n",
      " 5   phylum          5991207 non-null  object \n",
      " 6   class           5971438 non-null  object \n",
      " 7   order           5965299 non-null  object \n",
      " 8   family          5948728 non-null  object \n",
      " 9   genus           5940313 non-null  object \n",
      " 10  species         5951613 non-null  object \n",
      " 11  common          6250420 non-null  object \n",
      "dtypes: float64(2), object(10)\n",
      "memory usage: 619.9+ MB\n"
     ]
    }
   ],
   "source": [
    "eol_catalog = eol_catalog[eol_cols]\n",
    "eol_catalog.info(show_counts=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Must recast IDs as `int64`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 6250420 entries, 956715 to 11000930\n",
      "Data columns (total 4 columns):\n",
      " #   Column          Dtype \n",
      "---  ------          ----- \n",
      " 0   split           object\n",
      " 1   treeoflife_id   object\n",
      " 2   eol_content_id  int64 \n",
      " 3   eol_page_id     int64 \n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 238.4+ MB\n"
     ]
    }
   ],
   "source": [
    "eol_catalog = eol_catalog.astype({\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n",
    "eol_catalog[eol_cols[:4]].info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "# make combined ID for catalog and add suffix to eol content and page IDs\n",
    "eol_catalog['combined_id_catalog'] = eol_catalog['eol_content_id'].astype(str) + '_' + eol_catalog['eol_page_id'].astype(str)\n",
    "eol_catalog.rename(columns={'eol_content_id': 'eol_content_id_catalog', 'eol_page_id': 'eol_page_id_catalog'}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6219674"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matched_catalog_ids = list(links_inner.combined_id_catalog.unique())\n",
    "len(matched_catalog_ids)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we go, we got an additional 630 matched IDs!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['29538374_65414274',\n",
       " '27793900_888015',\n",
       " '29121641_5618956',\n",
       " '27596176_607817',\n",
       " '20300703_267922']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matched_catalog_ids[:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We still have about 30K unmatched (as shown below, it's down to 30,746 from 31,376), let's find those in the catalog and then we'll try to merge with our mismatched cargo & manifest."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5842839       28875844_579175\n",
       "7715090       21712402_702458\n",
       "10112940     27938371_3091824\n",
       "8606944     27597441_49820955\n",
       "1247592      14852900_2896866\n",
       "Name: combined_id_catalog, dtype: object"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eol_catalog['combined_id_catalog'].sample(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 30746 entries, 956874 to 10996978\n",
      "Data columns (total 13 columns):\n",
      " #   Column                  Non-Null Count  Dtype \n",
      "---  ------                  --------------  ----- \n",
      " 0   split                   30746 non-null  object\n",
      " 1   treeoflife_id           30746 non-null  object\n",
      " 2   eol_content_id_catalog  30746 non-null  int64 \n",
      " 3   eol_page_id_catalog     30746 non-null  int64 \n",
      " 4   kingdom                 30573 non-null  object\n",
      " 5   phylum                  30578 non-null  object\n",
      " 6   class                   30153 non-null  object\n",
      " 7   order                   30528 non-null  object\n",
      " 8   family                  30294 non-null  object\n",
      " 9   genus                   30192 non-null  object\n",
      " 10  species                 29399 non-null  object\n",
      " 11  common                  30746 non-null  object\n",
      " 12  combined_id_catalog     30746 non-null  object\n",
      "dtypes: int64(2), object(11)\n",
      "memory usage: 3.3+ MB\n"
     ]
    }
   ],
   "source": [
    "mismatched_catalog = eol_catalog.loc[~eol_catalog[\"combined_id_catalog\"].isin(matched_catalog_ids)]\n",
    "mismatched_catalog.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "split                         2\n",
       "treeoflife_id             30746\n",
       "eol_content_id_catalog    30746\n",
       "eol_page_id_catalog        7042\n",
       "kingdom                       7\n",
       "phylum                       33\n",
       "class                        87\n",
       "order                       355\n",
       "family                     1165\n",
       "genus                      3503\n",
       "species                    5174\n",
       "common                     6884\n",
       "combined_id_catalog       30746\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mismatched_catalog.nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Seems one class got resolved (equals more orders and down)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Merge with Mismatched CargoArchive-Manifest\n",
    "\n",
    "let's merge with `links_mismatch` to see if the mismatched archive and manifest combined IDs can be linked up."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 0 entries\n",
      "Data columns (total 30 columns):\n",
      " #   Column                           Non-Null Count  Dtype \n",
      "---  ------                           --------------  ----- \n",
      " 0   split                            0 non-null      object\n",
      " 1   treeoflife_id                    0 non-null      object\n",
      " 2   eol_content_id_catalog           0 non-null      int64 \n",
      " 3   eol_page_id_catalog              0 non-null      int64 \n",
      " 4   kingdom                          0 non-null      object\n",
      " 5   phylum                           0 non-null      object\n",
      " 6   class                            0 non-null      object\n",
      " 7   order                            0 non-null      object\n",
      " 8   family                           0 non-null      object\n",
      " 9   genus                            0 non-null      object\n",
      " 10  species                          0 non-null      object\n",
      " 11  common                           0 non-null      object\n",
      " 12  combined_id_catalog              0 non-null      object\n",
      " 13  filename_archive                 0 non-null      object\n",
      " 14  md5_archive                      0 non-null      object\n",
      " 15  combined_id_archive              0 non-null      object\n",
      " 16  filename_manifest                0 non-null      object\n",
      " 17  md5_combined_manifest            0 non-null      object\n",
      " 18  combined_id_manifest_redownload  0 non-null      object\n",
      " 19  eol_content_id                   0 non-null      int64 \n",
      " 20  eol_page_id                      0 non-null      int64 \n",
      " 21  medium_source_url                0 non-null      object\n",
      " 22  eol_full_size_copy_url           0 non-null      object\n",
      " 23  license_name                     0 non-null      object\n",
      " 24  copyright_owner                  0 non-null      object\n",
      " 25  expected_image_filename          0 non-null      object\n",
      " 26  source_0706                      0 non-null      bool  \n",
      " 27  source_0726                      0 non-null      bool  \n",
      " 28  source_1206                      0 non-null      bool  \n",
      " 29  combined_id_full_manifest        0 non-null      object\n",
      "dtypes: bool(3), int64(4), object(23)\n",
      "memory usage: 132.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "# replace manifest checksums with full manifest as above\n",
    "links_catalog_mismatch = pd.merge(mismatched_catalog,\n",
    "                                  links_mismatch,\n",
    "                                  left_on = \"combined_id_catalog\",\n",
    "                                  right_on = \"combined_id_full_manifest\",\n",
    "                                  how = \"inner\")\n",
    "links_catalog_mismatch.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now there's no gain from the mismatch file, but that was only 29 images, so we've captured them from the archive (plus more!)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2003559      15270321_240065\n",
       "8751466       29485627_17806\n",
       "4818957       27965287_35726\n",
       "8697796       9483753_328724\n",
       "1049389      9446784_5019858\n",
       "8364678      9467133_2925218\n",
       "5119903    28880812_10305215\n",
       "Name: combined_id_catalog, dtype: object"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mismatched_catalog.combined_id_catalog.sample(7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These pages are reasonably full too: [5660435](https://eol.org/pages/5660435)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Final Catalog\n",
    "\n",
    "Our final EOL catalog is thus going to be the `treeoflife_id`s in `links_inner`.\n",
    "\n",
    "Finall question is just how does this look for our `train_small`?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 593764 entries, 0 to 918500\n",
      "Data columns (total 12 columns):\n",
      " #   Column          Non-Null Count   Dtype  \n",
      "---  ------          --------------   -----  \n",
      " 0   split           593764 non-null  object \n",
      " 1   treeoflife_id   593764 non-null  object \n",
      " 2   eol_content_id  593764 non-null  float64\n",
      " 3   eol_page_id     593764 non-null  float64\n",
      " 4   kingdom         569128 non-null  object \n",
      " 5   phylum          569285 non-null  object \n",
      " 6   class           567356 non-null  object \n",
      " 7   order           566839 non-null  object \n",
      " 8   family          565269 non-null  object \n",
      " 9   genus           564605 non-null  object \n",
      " 10  species         565399 non-null  object \n",
      " 11  common          593764 non-null  object \n",
      "dtypes: float64(2), object(10)\n",
      "memory usage: 58.9+ MB\n"
     ]
    }
   ],
   "source": [
    "cat_2 = pd.read_csv(TOL_FILEPATH + \"catalog.csv\", low_memory=False)\n",
    "\n",
    "# reduce to just train_small and all non-EOL entries\n",
    "cat_2 = cat_2.loc[cat_2.split == \"train_small\"]\n",
    "eol_cat_2 = cat_2.loc[cat_2.eol_content_id.notna()]\n",
    "\n",
    "eol_cat_2 = eol_cat_2[eol_cols]\n",
    "eol_cat_2.info(show_counts=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So `train_small` has 593,764 images from EOL. \n",
    "\n",
    "Let's recast IDs as ints to match up with `links_inner` and check we have info for all of them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 593764 entries, 0 to 918500\n",
      "Data columns (total 4 columns):\n",
      " #   Column          Non-Null Count   Dtype \n",
      "---  ------          --------------   ----- \n",
      " 0   split           593764 non-null  object\n",
      " 1   treeoflife_id   593764 non-null  object\n",
      " 2   eol_content_id  593764 non-null  int64 \n",
      " 3   eol_page_id     593764 non-null  int64 \n",
      "dtypes: int64(2), object(2)\n",
      "memory usage: 22.7+ MB\n"
     ]
    }
   ],
   "source": [
    "eol_cat_2 = eol_cat_2.astype({\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n",
    "eol_cat_2[eol_cols[:4]].info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "# make combined ID for catalog and add suffix to eol content and page IDs\n",
    "eol_cat_2['combined_id_catalog'] = eol_cat_2['eol_content_id'].astype(str) + '_' + eol_cat_2['eol_page_id'].astype(str)\n",
    "eol_cat_2.rename(columns={'eol_content_id': 'eol_content_id_catalog', 'eol_page_id': 'eol_page_id_catalog'}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 612614 entries, 0 to 612613\n",
      "Data columns (total 25 columns):\n",
      " #   Column                           Non-Null Count   Dtype \n",
      "---  ------                           --------------   ----- \n",
      " 0   split                            612614 non-null  object\n",
      " 1   treeoflife_id                    612614 non-null  object\n",
      " 2   eol_content_id_catalog           612614 non-null  int64 \n",
      " 3   eol_page_id_catalog              612614 non-null  int64 \n",
      " 4   kingdom                          584979 non-null  object\n",
      " 5   phylum                           585140 non-null  object\n",
      " 6   class                            583235 non-null  object\n",
      " 7   order                            582668 non-null  object\n",
      " 8   family                           581082 non-null  object\n",
      " 9   genus                            580464 non-null  object\n",
      " 10  species                          579943 non-null  object\n",
      " 11  common                           612614 non-null  object\n",
      " 12  combined_id_catalog_x            612614 non-null  object\n",
      " 13  combined_id_catalog_y            612614 non-null  object\n",
      " 14  filename_manifest                612614 non-null  object\n",
      " 15  combined_id_manifest_redownload  612614 non-null  object\n",
      " 16  medium_source_url                612614 non-null  object\n",
      " 17  eol_full_size_copy_url           612614 non-null  object\n",
      " 18  license_name                     612614 non-null  object\n",
      " 19  copyright_owner                  554029 non-null  object\n",
      " 20  expected_image_filename          612614 non-null  object\n",
      " 21  source_0706                      612614 non-null  bool  \n",
      " 22  source_0726                      612614 non-null  bool  \n",
      " 23  source_1206                      612614 non-null  bool  \n",
      " 24  combined_id_full_manifest        612614 non-null  object\n",
      "dtypes: bool(3), int64(2), object(20)\n",
      "memory usage: 104.6+ MB\n"
     ]
    }
   ],
   "source": [
    "eol_ts_links = pd.merge(eol_cat_2,\n",
    "                        links_inner[list(links_inner.columns)[18:]],\n",
    "                        left_on = \"combined_id_catalog\",\n",
    "                        right_on = \"combined_id_full_manifest\",\n",
    "                        how = \"inner\")\n",
    "eol_ts_links.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "590730"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "eol_ts_links.treeoflife_id.nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So we are missing the metadata for 3,734 of these images."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Check on Rare Species Catalog"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 12000 entries, 0 to 11999\n",
      "Data columns (total 11 columns):\n",
      " #   Column          Non-Null Count  Dtype \n",
      "---  ------          --------------  ----- \n",
      " 0   rarespecies_id  12000 non-null  object\n",
      " 1   eol_content_id  12000 non-null  int64 \n",
      " 2   eol_page_id     12000 non-null  int64 \n",
      " 3   kingdom         12000 non-null  object\n",
      " 4   phylum          12000 non-null  object\n",
      " 5   class           12000 non-null  object\n",
      " 6   order           12000 non-null  object\n",
      " 7   family          12000 non-null  object\n",
      " 8   genus           12000 non-null  object\n",
      " 9   species         12000 non-null  object\n",
      " 10  sciName         12000 non-null  object\n",
      "dtypes: int64(2), object(9)\n",
      "memory usage: 1.0+ MB\n"
     ]
    }
   ],
   "source": [
    "rs_catalog = pd.read_csv(\"../../rare_species/data/rarespecies-catalog.csv\",\n",
    "                         low_memory=False,\n",
    "                         dtype = {\"eol_content_id\": \"int64\", \"eol_page_id\": \"int64\"})\n",
    "rs_catalog.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "rs_catalog[\"combined_id_rs\"] = rs_catalog[\"eol_content_id\"].astype(str) + \"_\" + rs_catalog[\"eol_page_id\"].astype(str)\n",
    "rs_catalog.rename(columns={'eol_content_id': 'eol_content_id_rs', 'eol_page_id': 'eol_page_id_rs'}, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 12552 entries, 0 to 12551\n",
      "Data columns (total 29 columns):\n",
      " #   Column                           Non-Null Count  Dtype \n",
      "---  ------                           --------------  ----- \n",
      " 0   rarespecies_id                   12552 non-null  object\n",
      " 1   eol_content_id_rs                12552 non-null  int64 \n",
      " 2   eol_page_id_rs                   12552 non-null  int64 \n",
      " 3   kingdom                          12552 non-null  object\n",
      " 4   phylum                           12552 non-null  object\n",
      " 5   class                            12552 non-null  object\n",
      " 6   order                            12552 non-null  object\n",
      " 7   family                           12552 non-null  object\n",
      " 8   genus                            12552 non-null  object\n",
      " 9   species                          12552 non-null  object\n",
      " 10  sciName                          12552 non-null  object\n",
      " 11  combined_id_rs                   12552 non-null  object\n",
      " 12  filename_archive                 12552 non-null  object\n",
      " 13  md5_archive                      12552 non-null  object\n",
      " 14  combined_id_archive              12552 non-null  object\n",
      " 15  filename_manifest                12552 non-null  object\n",
      " 16  md5_combined_manifest            12552 non-null  object\n",
      " 17  combined_id_manifest_redownload  12552 non-null  object\n",
      " 18  eol_content_id                   12552 non-null  int64 \n",
      " 19  eol_page_id                      12552 non-null  int64 \n",
      " 20  medium_source_url                12552 non-null  object\n",
      " 21  eol_full_size_copy_url           12552 non-null  object\n",
      " 22  license_name                     12552 non-null  object\n",
      " 23  copyright_owner                  11171 non-null  object\n",
      " 24  expected_image_filename          12552 non-null  object\n",
      " 25  source_0706                      12552 non-null  bool  \n",
      " 26  source_0726                      12552 non-null  bool  \n",
      " 27  source_1206                      12552 non-null  bool  \n",
      " 28  combined_id_full_manifest        12552 non-null  object\n",
      "dtypes: bool(3), int64(4), object(22)\n",
      "memory usage: 2.5+ MB\n"
     ]
    }
   ],
   "source": [
    "rs_links = pd.merge(rs_catalog,\n",
    "                    links_manifest_cargo,\n",
    "                    left_on = \"combined_id_rs\",\n",
    "                    right_on = \"combined_id_archive\", # looking at archive\n",
    "                    how = \"inner\")\n",
    "rs_links.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "rarespecies_id                     11826\n",
       "eol_content_id_rs                  11826\n",
       "eol_page_id_rs                       400\n",
       "kingdom                                1\n",
       "phylum                                 5\n",
       "class                                 15\n",
       "order                                 85\n",
       "family                               202\n",
       "genus                                316\n",
       "species                              385\n",
       "sciName                              400\n",
       "combined_id_rs                     11826\n",
       "filename_archive                   11826\n",
       "md5_archive                        11663\n",
       "combined_id_archive                11826\n",
       "filename_manifest                  12221\n",
       "md5_combined_manifest              11663\n",
       "combined_id_manifest_redownload    12221\n",
       "eol_content_id                     12221\n",
       "eol_page_id                          447\n",
       "medium_source_url                  12056\n",
       "eol_full_size_copy_url             12119\n",
       "license_name                          15\n",
       "copyright_owner                     3724\n",
       "expected_image_filename            12221\n",
       "source_0706                            2\n",
       "source_0726                            2\n",
       "source_1206                            2\n",
       "combined_id_full_manifest          12221\n",
       "dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs_links.nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's check the mismatched cargo/manifest entries for those last 174..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "11826"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matched_rs_ids = list(rs_links.combined_id_rs.unique())\n",
    "len(matched_rs_ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['22519448_914532',\n",
       " '28677580_1057176',\n",
       " '20714475_47047909',\n",
       " '29975068_45509269']"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "matched_rs_ids[:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1473     29538601_46580048\n",
       "11127     29447084_1019571\n",
       "6578       20398296_328017\n",
       "11583     29772112_1049135\n",
       "Name: combined_id_rs, dtype: object"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "rs_catalog[\"combined_id_rs\"].sample(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "Index: 174 entries, 163 to 11990\n",
      "Data columns (total 12 columns):\n",
      " #   Column             Non-Null Count  Dtype \n",
      "---  ------             --------------  ----- \n",
      " 0   rarespecies_id     174 non-null    object\n",
      " 1   eol_content_id_rs  174 non-null    int64 \n",
      " 2   eol_page_id_rs     174 non-null    int64 \n",
      " 3   kingdom            174 non-null    object\n",
      " 4   phylum             174 non-null    object\n",
      " 5   class              174 non-null    object\n",
      " 6   order              174 non-null    object\n",
      " 7   family             174 non-null    object\n",
      " 8   genus              174 non-null    object\n",
      " 9   species            174 non-null    object\n",
      " 10  sciName            174 non-null    object\n",
      " 11  combined_id_rs     174 non-null    object\n",
      "dtypes: int64(2), object(10)\n",
      "memory usage: 17.7+ KB\n"
     ]
    }
   ],
   "source": [
    "mismatched_rs = rs_catalog.loc[~rs_catalog[\"combined_id_rs\"].isin(matched_rs_ids)]\n",
    "mismatched_rs.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 0 entries\n",
      "Data columns (total 29 columns):\n",
      " #   Column                           Non-Null Count  Dtype \n",
      "---  ------                           --------------  ----- \n",
      " 0   rarespecies_id                   0 non-null      object\n",
      " 1   eol_content_id_rs                0 non-null      int64 \n",
      " 2   eol_page_id_rs                   0 non-null      int64 \n",
      " 3   kingdom                          0 non-null      object\n",
      " 4   phylum                           0 non-null      object\n",
      " 5   class                            0 non-null      object\n",
      " 6   order                            0 non-null      object\n",
      " 7   family                           0 non-null      object\n",
      " 8   genus                            0 non-null      object\n",
      " 9   species                          0 non-null      object\n",
      " 10  sciName                          0 non-null      object\n",
      " 11  combined_id_rs                   0 non-null      object\n",
      " 12  filename_archive                 0 non-null      object\n",
      " 13  md5_archive                      0 non-null      object\n",
      " 14  combined_id_archive              0 non-null      object\n",
      " 15  filename_manifest                0 non-null      object\n",
      " 16  md5_combined_manifest            0 non-null      object\n",
      " 17  combined_id_manifest_redownload  0 non-null      object\n",
      " 18  eol_content_id                   0 non-null      int64 \n",
      " 19  eol_page_id                      0 non-null      int64 \n",
      " 20  medium_source_url                0 non-null      object\n",
      " 21  eol_full_size_copy_url           0 non-null      object\n",
      " 22  license_name                     0 non-null      object\n",
      " 23  copyright_owner                  0 non-null      object\n",
      " 24  expected_image_filename          0 non-null      object\n",
      " 25  source_0706                      0 non-null      bool  \n",
      " 26  source_0726                      0 non-null      bool  \n",
      " 27  source_1206                      0 non-null      bool  \n",
      " 28  combined_id_full_manifest        0 non-null      object\n",
      "dtypes: bool(3), int64(4), object(22)\n",
      "memory usage: 132.0+ bytes\n"
     ]
    }
   ],
   "source": [
    "links_rs_mismatch = pd.merge(mismatched_rs,\n",
    "                             links_mismatch,\n",
    "                             left_on = \"combined_id_rs\",\n",
    "                             right_on = \"combined_id_full_manifest\",\n",
    "                             how = \"inner\")\n",
    "links_rs_mismatch.info(show_counts = True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "formats": "ipynb,py:percent"
  },
  "kernelspec": {
   "display_name": "tol",
   "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.11.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}