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
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
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "We explored our downloaded images in `EDA_full_jiggins_DL.ipynb`, and established the following causes of duplication along with remedies:\n",
    "1. The \"patch\" record (record 3477891) which required alignment in `record_3477891_media_occurrence.ipynb`, resulting in `Jiggins_Zenodo_Img_Master_3477891Patch.csv` used for download, is a duplication of all listed records. Additionally, it introduced extra copies of 4 images with incorrect labels for their views (i.e., dorsal vs ventral).\n",
    " - Solution: This record will be removed.\n",
    "2. The remaining duplication (430 images x2) comes from 5 records: [4291095](https://zenodo.org/records/4291095), [2813153](https://zenodo.org/records/2813153), [5526257](https://zenodo.org/records/5526257), [2553977](https://zenodo.org/records/2553977), and [2552371](https://zenodo.org/records/2552371).\n",
    "  - Though there is nothing in their descriptions on Zenodo to indicate this overlap occurred, record 4291095 duplicated 415 images from record 2813153. 104 of these images had their view/side labels (dorsal vs ventral) updated to reflect an earlier mislabeling in record 2813153; these were all RAW copies of the images. All other metadata was consistent across records.\n",
    "  - Solution: Copies of these images and their metadata will be retained from the newer record, record 4291095.\n",
    "  - Record 5526257 has 10 images that were added twice. All their metadata is consistent, so we will just keep the first instance of each image.\n",
    "  - Records 2553977 and 2552371 are both \"Miscellaneous Heliconius wing photographs (2001-2019)\" Parts 3 & 1, respectively. Record 2553977 has duplicated images with matching metadata, though 3 of the 4 images have different filenames (`Image_name`). It seems to have resulted from typos or not indicating `cut`, as they are all close-up crops of a single wing. From this perspective, the view labels (dorsal) don't seem appropriate when we have more fine-grained indicators such as \"forewing dorsal\".\n",
    "    - Suggestion that the entries in this record with `cut` in the filename not be used for general classification unless this variety is desired. These also appear to be all the `tif` images, which are often ignored.\n",
    "    - Record 2552371 has one image duplicated with two different `CAMID`s assigned (different views as well). These will both be removed as the image has no indicator of the specimen ID in it. Note that these also were labeled `Heliconius sp.`.\n",
    "3. Looking at images that were not duplicates, there are clearly still multiple images of the same specimen from the same perspective (eg., two dorsal images) that are also of the same file type (eg., both jpgs). Duplication was checked at the pixel level, so there is no guarantee that they are truly different images, but due to the scope of this collection (over 20 years), it does not seem unlikely that multiple images could have been taken of the same specimen. \n",
    "  - Suggestion/Solution: When determining splits using this data, follow these steps:\n",
    "    1. Ensure you are looking at only **_one_** view (eg., dorsal or ventral).\n",
    "    2. Reduce to only unique `CAMID`s.\n",
    "    3. Generate splits as desired.\n",
    "    4. Add images to splits based on matched `CAMID`s if multiple views are desired (i.e., if a `CAMID` is present in the dorsal training, add the matching ventral image to the training set).\n",
    "\n",
    "\n",
    "## Generate \"Clean\" Master Metadata File\n",
    "\n",
    "We'll now generate a cleaned master metadata file from `metadata/Jiggins_Zenodo_Img_Master_3477891Patch_downloaded.csv` (generated in `notebooks/EDA_full_jiggins_DL.ipynb` from our download master file and checksums of successful downloads) following the solutions outlined above. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "STATS_COLS = [\"X\",\n",
    "              \"CAMID\",\n",
    "              \"Image_name\",\n",
    "              \"md5\",\n",
    "              \"record_number\",\n",
    "              \"Taxonomic_Name\",\n",
    "              \"species\",\n",
    "              \"subspecies\",\n",
    "              \"genus\",\n",
    "              \"Cross_Type\",\n",
    "              \"View\",\n",
    "              \"Locality\",\n",
    "              \"Sex\",\n",
    "              \"Dataset\",\n",
    "              \"file_type\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 42143 entries, 0 to 42142\n",
      "Data columns (total 15 columns):\n",
      " #   Column          Non-Null Count  Dtype \n",
      "---  ------          --------------  ----- \n",
      " 0   X               42143 non-null  int64 \n",
      " 1   CAMID           42143 non-null  object\n",
      " 2   Image_name      42143 non-null  object\n",
      " 3   md5             42143 non-null  object\n",
      " 4   record_number   42143 non-null  int64 \n",
      " 5   Taxonomic_Name  42143 non-null  object\n",
      " 6   species         42143 non-null  object\n",
      " 7   subspecies      24502 non-null  object\n",
      " 8   genus           42143 non-null  object\n",
      " 9   Cross_Type      4447 non-null   object\n",
      " 10  View            41364 non-null  object\n",
      " 11  Locality        29047 non-null  object\n",
      " 12  Sex             33254 non-null  object\n",
      " 13  Dataset         34924 non-null  object\n",
      " 14  file_type       42143 non-null  object\n",
      "dtypes: int64(2), object(13)\n",
      "memory usage: 4.8+ MB\n"
     ]
    }
   ],
   "source": [
    "df = pd.read_csv(\"../metadata/Jiggins_Zenodo_Img_Master_3477891Patch_downloaded.csv\", low_memory = False)\n",
    "df[STATS_COLS].info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 42143\n",
       "CAMID             11968\n",
       "Image_name        36216\n",
       "md5               36212\n",
       "record_number        30\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Observations:\n",
    "- Half of the Zenodo records wind up with duplicated images. \n",
    "- `Image_name` uniqueness is close to `md5`, but there are 4 more unique image names than unique images. I believe this was from the duplication in reocrd 2553977. We can check when cleaning it. \n",
    "- None of the cross types are duplicated.\n",
    "- We do have multiple unique images for a single `CAMID`, more than just dorsal vs ventral since there are 11968 unique CAMIDs and 36212 unique `md5` values (36212 = **3**x11968 + 308)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Remove Record 3477891\n",
    "\n",
    "This is just a duplication of images and we already have from earlier records, along with some extra duplication that gets mislabeled."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 36642\n",
       "CAMID             11968\n",
       "Image_name        36216\n",
       "md5               36212\n",
       "record_number        29\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced = df.loc[df[\"record_number\"] != 3477891].copy()\n",
    "df_reduced[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The only change is in `X` (our total entry count) and in `record_number` (expected since we removed one)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Address Remaining Duplicates\n",
    "\n",
    "The remaining duplicates are across 5 records and will require a bit more care to reslove (following the plan outlined at the top of the notebook and in the README).\n",
    "\n",
    "### Record 4291095 & 2813153 Duplicates\n",
    "\n",
    "There are 415 duplicates across these two records, with `View` corrections in record 4291095, so we will remove the duplicates from record 2813153."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['CAMID', 'X', 'Image_name', 'View', 'zenodo_name', 'zenodo_link',\n",
       "       'Sequence', 'Taxonomic_Name', 'Locality', 'Sample_accession',\n",
       "       'Collected_by', 'Other_ID', 'Date', 'Dataset', 'Store', 'Brood',\n",
       "       'Death_Date', 'Cross_Type', 'Stage', 'Sex', 'Unit_Type', 'file_type',\n",
       "       'record_number', 'species', 'subspecies', 'genus', 'file_url',\n",
       "       'hybrid_stat', 'filename', 'filepath', 'md5'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced.columns"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We start by marking all duplicate images since the decision of which to keep is dependent on comparisons between them."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "md5_duplicated\n",
       "False    35782\n",
       "True       860\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced[\"md5_duplicated\"] = df_reduced.duplicated(\"md5\", keep = False)\n",
    "df_reduced[\"md5_duplicated\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "415"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "duplicates = df_reduced[df_reduced[\"md5_duplicated\"]]\n",
    "dupes_2813153 = duplicates.loc[duplicates[\"record_number\"] == 2813153]\n",
    "#print(duplicates.loc[duplicates[\"record_number\"] == 4291095])\n",
    "dupes_2813153.shape[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We will record the `X` value from these 415 entries to remove them from the overall dataset in favor of the updated entries from record 4291095."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_dupes_2813153 = list(dupes_2813153.X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 36227\n",
       "CAMID             11968\n",
       "Image_name        36216\n",
       "md5               36212\n",
       "record_number        29\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced_2 = df_reduced.loc[~df_reduced[\"X\"].isin(X_dupes_2813153)]\n",
    "df_reduced_2[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "As expected, our numbers are holding steady with a reduction in `X` values of 415."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Record 5526257 Duplicates\n",
    "\n",
    "For record 5526257, the 10 duplicated entries are all the same, so we will just check `.duplicated` and keep the first instance of each."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dupe_entries\n",
      "False    10\n",
      "True     10\n",
      "Name: count, dtype: int64\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dupes_5526257 = duplicates.loc[duplicates[\"record_number\"] == 5526257].copy()\n",
    "dupes_5526257[\"dupe_entries\"] = dupes_5526257.duplicated([\"CAMID\", \"Image_name\", \"View\", \"Taxonomic_Name\", \"Locality\", \"md5\"], keep = \"first\")\n",
    "print(dupes_5526257[\"dupe_entries\"].value_counts())\n",
    "\n",
    "X_dupes_5526257 = list(dupes_5526257.loc[dupes_5526257[\"dupe_entries\"], \"X\"])\n",
    "len(X_dupes_5526257)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 36217\n",
       "CAMID             11968\n",
       "Image_name        36216\n",
       "md5               36212\n",
       "record_number        29\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced_3 = df_reduced_2.loc[~df_reduced_2[\"X\"].isin(X_dupes_5526257)]\n",
    "df_reduced_3[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Reduction in `X` by 10, consistency in all other numbers.\n",
    "\n",
    "### Record 2552371 Duplicates\n",
    "\n",
    "We are removing both duplicates from this record as their metadata is contradictory and cannot be verified. (Two different CAMIDs, different filenames, different views, etc.)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "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>CAMID</th>\n",
       "      <th>X</th>\n",
       "      <th>Image_name</th>\n",
       "      <th>View</th>\n",
       "      <th>zenodo_name</th>\n",
       "      <th>zenodo_link</th>\n",
       "      <th>Sequence</th>\n",
       "      <th>Taxonomic_Name</th>\n",
       "      <th>Locality</th>\n",
       "      <th>Sample_accession</th>\n",
       "      <th>...</th>\n",
       "      <th>record_number</th>\n",
       "      <th>species</th>\n",
       "      <th>subspecies</th>\n",
       "      <th>genus</th>\n",
       "      <th>file_url</th>\n",
       "      <th>hybrid_stat</th>\n",
       "      <th>filename</th>\n",
       "      <th>filepath</th>\n",
       "      <th>md5</th>\n",
       "      <th>md5_duplicated</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>11978</th>\n",
       "      <td>CAM010354</td>\n",
       "      <td>23471</td>\n",
       "      <td>10354d.jpg</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part1.csv</td>\n",
       "      <td>https://zenodo.org/record/2552371</td>\n",
       "      <td>10,354</td>\n",
       "      <td>Heliconius sp.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>2552371</td>\n",
       "      <td>Heliconius sp.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2552371/files/10354d...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23471_10354d.jpg</td>\n",
       "      <td>images/Heliconius sp./23471_10354d.jpg</td>\n",
       "      <td>84d4ac6527458786cdb33166cd80e0a8</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12010</th>\n",
       "      <td>CAM010362</td>\n",
       "      <td>23484</td>\n",
       "      <td>10362v.jpg</td>\n",
       "      <td>ventral</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part1.csv</td>\n",
       "      <td>https://zenodo.org/record/2552371</td>\n",
       "      <td>10,362</td>\n",
       "      <td>Heliconius sp.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>2552371</td>\n",
       "      <td>Heliconius sp.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2552371/files/10362v...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>23484_10362v.jpg</td>\n",
       "      <td>images/Heliconius sp./23484_10362v.jpg</td>\n",
       "      <td>84d4ac6527458786cdb33166cd80e0a8</td>\n",
       "      <td>True</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 32 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           CAMID      X  Image_name     View  \\\n",
       "11978  CAM010354  23471  10354d.jpg   dorsal   \n",
       "12010  CAM010362  23484  10362v.jpg  ventral   \n",
       "\n",
       "                                          zenodo_name  \\\n",
       "11978  Heliconius_wing_old_photos_2001_2019_part1.csv   \n",
       "12010  Heliconius_wing_old_photos_2001_2019_part1.csv   \n",
       "\n",
       "                             zenodo_link Sequence  Taxonomic_Name Locality  \\\n",
       "11978  https://zenodo.org/record/2552371   10,354  Heliconius sp.      NaN   \n",
       "12010  https://zenodo.org/record/2552371   10,362  Heliconius sp.      NaN   \n",
       "\n",
       "      Sample_accession  ... record_number         species subspecies  \\\n",
       "11978              NaN  ...       2552371  Heliconius sp.        NaN   \n",
       "12010              NaN  ...       2552371  Heliconius sp.        NaN   \n",
       "\n",
       "            genus                                           file_url  \\\n",
       "11978  Heliconius  https://zenodo.org/record/2552371/files/10354d...   \n",
       "12010  Heliconius  https://zenodo.org/record/2552371/files/10362v...   \n",
       "\n",
       "      hybrid_stat          filename                                filepath  \\\n",
       "11978         NaN  23471_10354d.jpg  images/Heliconius sp./23471_10354d.jpg   \n",
       "12010         NaN  23484_10362v.jpg  images/Heliconius sp./23484_10362v.jpg   \n",
       "\n",
       "                                    md5 md5_duplicated  \n",
       "11978  84d4ac6527458786cdb33166cd80e0a8           True  \n",
       "12010  84d4ac6527458786cdb33166cd80e0a8           True  \n",
       "\n",
       "[2 rows x 32 columns]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dupes_2552371 = duplicates.loc[duplicates[\"record_number\"] == 2552371]\n",
    "X_dupes_2552371 = list(dupes_2552371[\"X\"])\n",
    "dupes_2552371"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 36215\n",
       "CAMID             11968\n",
       "Image_name        36214\n",
       "md5               36211\n",
       "record_number        29\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced_4 = df_reduced_3.loc[~df_reduced_3[\"X\"].isin(X_dupes_2552371)]\n",
    "df_reduced_4[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Removing these two has reduced our `X` count by 2, as well as our number of unique `Image_name`s, and it reduced our unique image count (`md5`) by 1. Observe that our unique `CAMID` count has remained constant, so both specimens indicated by these duplicate images are still represented in the dataset."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Record 2553977 Duplicates\n",
    "\n",
    "The duplicated entries for record 2553977 are the most complicated in that their metadata is nearly exact, though a few have different filenames (`Image_name`) and we would like to preserve the version that has `_cut` in the name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "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>X</th>\n",
       "      <th>CAMID</th>\n",
       "      <th>Image_name</th>\n",
       "      <th>md5</th>\n",
       "      <th>record_number</th>\n",
       "      <th>Taxonomic_Name</th>\n",
       "      <th>species</th>\n",
       "      <th>subspecies</th>\n",
       "      <th>genus</th>\n",
       "      <th>Cross_Type</th>\n",
       "      <th>View</th>\n",
       "      <th>Locality</th>\n",
       "      <th>Sex</th>\n",
       "      <th>Dataset</th>\n",
       "      <th>file_type</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>40141</th>\n",
       "      <td>26197</td>\n",
       "      <td>CAM050147</td>\n",
       "      <td>CAM050147_DS1_HW_IMG_8537_cut_3.tif</td>\n",
       "      <td>21e726df7076f5c13fe1441126bac4d5</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Madingley</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40144</th>\n",
       "      <td>26196</td>\n",
       "      <td>CAM050147</td>\n",
       "      <td>CAM050147_DS1_HW_IMG_8537_cut_3.tif</td>\n",
       "      <td>21e726df7076f5c13fe1441126bac4d5</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Madingley</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40112</th>\n",
       "      <td>26102</td>\n",
       "      <td>CAM050036</td>\n",
       "      <td>CAM050036_S1_9_FW_IMG_8547_wb_2.tif</td>\n",
       "      <td>34adbd5596f977834fb2c6876d2f4493</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Gamboa_insectaries</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40116</th>\n",
       "      <td>26115</td>\n",
       "      <td>CAM050036</td>\n",
       "      <td>CAM050036_S1_9_FW_IMG_8547_cut_2.tif</td>\n",
       "      <td>34adbd5596f977834fb2c6876d2f4493</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Gamboa_insectaries</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40138</th>\n",
       "      <td>26112</td>\n",
       "      <td>CAM050147</td>\n",
       "      <td>CAM050147_DS1_IMG_8513_4.tif</td>\n",
       "      <td>464801449aa127933d9619cb4e6f0a01</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Madingley</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40145</th>\n",
       "      <td>26117</td>\n",
       "      <td>CAM050147</td>\n",
       "      <td>CAM050147_DS1_FW_IMG_8513_cut_4.tif</td>\n",
       "      <td>464801449aa127933d9619cb4e6f0a01</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Madingley</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40113</th>\n",
       "      <td>26105</td>\n",
       "      <td>CAM050036</td>\n",
       "      <td>CAM050036_S1_9_HW_wt_IMG_8541_cut3.tif</td>\n",
       "      <td>6d63d659fbc02369010938bbc229ad4d</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Gamboa_insectaries</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40120</th>\n",
       "      <td>26116</td>\n",
       "      <td>CAM050036</td>\n",
       "      <td>CAM050036_S1_9_HW_wt_IMG_8541_cut_3.tif</td>\n",
       "      <td>6d63d659fbc02369010938bbc229ad4d</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius sara ssp. sara</td>\n",
       "      <td>Heliconius sara</td>\n",
       "      <td>sara</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>NaN</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Gamboa_insectaries</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>tif</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "           X      CAMID                               Image_name  \\\n",
       "40141  26197  CAM050147      CAM050147_DS1_HW_IMG_8537_cut_3.tif   \n",
       "40144  26196  CAM050147      CAM050147_DS1_HW_IMG_8537_cut_3.tif   \n",
       "40112  26102  CAM050036      CAM050036_S1_9_FW_IMG_8547_wb_2.tif   \n",
       "40116  26115  CAM050036     CAM050036_S1_9_FW_IMG_8547_cut_2.tif   \n",
       "40138  26112  CAM050147             CAM050147_DS1_IMG_8513_4.tif   \n",
       "40145  26117  CAM050147      CAM050147_DS1_FW_IMG_8513_cut_4.tif   \n",
       "40113  26105  CAM050036   CAM050036_S1_9_HW_wt_IMG_8541_cut3.tif   \n",
       "40120  26116  CAM050036  CAM050036_S1_9_HW_wt_IMG_8541_cut_3.tif   \n",
       "\n",
       "                                    md5  record_number  \\\n",
       "40141  21e726df7076f5c13fe1441126bac4d5        2553977   \n",
       "40144  21e726df7076f5c13fe1441126bac4d5        2553977   \n",
       "40112  34adbd5596f977834fb2c6876d2f4493        2553977   \n",
       "40116  34adbd5596f977834fb2c6876d2f4493        2553977   \n",
       "40138  464801449aa127933d9619cb4e6f0a01        2553977   \n",
       "40145  464801449aa127933d9619cb4e6f0a01        2553977   \n",
       "40113  6d63d659fbc02369010938bbc229ad4d        2553977   \n",
       "40120  6d63d659fbc02369010938bbc229ad4d        2553977   \n",
       "\n",
       "                  Taxonomic_Name          species subspecies       genus  \\\n",
       "40141  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40144  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40112  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40116  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40138  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40145  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40113  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "40120  Heliconius sara ssp. sara  Heliconius sara       sara  Heliconius   \n",
       "\n",
       "      Cross_Type    View            Locality  Sex Dataset file_type  \n",
       "40141        NaN  dorsal           Madingley  NaN     NaN       tif  \n",
       "40144        NaN  dorsal           Madingley  NaN     NaN       tif  \n",
       "40112        NaN  dorsal  Gamboa_insectaries  NaN     NaN       tif  \n",
       "40116        NaN  dorsal  Gamboa_insectaries  NaN     NaN       tif  \n",
       "40138        NaN  dorsal           Madingley  NaN     NaN       tif  \n",
       "40145        NaN  dorsal           Madingley  NaN     NaN       tif  \n",
       "40113        NaN  dorsal  Gamboa_insectaries  NaN     NaN       tif  \n",
       "40120        NaN  dorsal  Gamboa_insectaries  NaN     NaN       tif  "
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dupes_2553977 = duplicates.loc[duplicates[\"record_number\"] == 2553977]\n",
    "dupes_2553977[STATS_COLS].sort_values(\"md5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For each of these, we want the one with `_cut_` in the `Image_name`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[26102, 26105, 26112]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X_dupes_2553977 = [x for x in list(dupes_2553977[\"X\"]) if \"_cut_\" not in dupes_2553977.loc[dupes_2553977[\"X\"] == x, \"Image_name\"].values[0]]\n",
    "\n",
    "# Check we got the ones expected\n",
    "X_dupes_2553977"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Add 26196, since both those entries (26196 and 26197) are the same\n",
    "X_dupes_2553977.append(26196)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "X                 36211\n",
       "CAMID             11968\n",
       "Image_name        36211\n",
       "md5               36211\n",
       "record_number        29\n",
       "Taxonomic_Name      366\n",
       "species             246\n",
       "subspecies          155\n",
       "genus                94\n",
       "Cross_Type           30\n",
       "View                  7\n",
       "Locality            644\n",
       "Sex                   3\n",
       "Dataset               7\n",
       "file_type             3\n",
       "dtype: int64"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df_reduced_5 = df_reduced_4.loc[~df_reduced_4[\"X\"].isin(X_dupes_2553977)]\n",
    "df_reduced_5[STATS_COLS].nunique()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4 entries were removed, reducing our number of unique `Image_name`s by 3 (one of the duplicated images had all its metadata duplicated), and everything else has remained consistent. Now the number of unique image names and entries matches the number of unique images. We have a total of 36,211 images, and we will save this reduced CSV as our new master file."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Save Reduced Master File of Unique Images\n",
    "\n",
    "Don't include last column indicating duplicate MD5s."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_reduced_5[list(df_reduced_5.columns)[:-1]].to_csv(\"../Jiggins_Zenodo_Img_Master.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Generate New Master Heliconius and Dorsal Subsets\n",
    "\n",
    "## Read in New Master File\n",
    "\n",
    "First check on our stats noted in the README."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 36211 entries, 0 to 36210\n",
      "Data columns (total 31 columns):\n",
      " #   Column            Non-Null Count  Dtype \n",
      "---  ------            --------------  ----- \n",
      " 0   CAMID             36211 non-null  object\n",
      " 1   X                 36211 non-null  int64 \n",
      " 2   Image_name        36211 non-null  object\n",
      " 3   View              35432 non-null  object\n",
      " 4   zenodo_name       36211 non-null  object\n",
      " 5   zenodo_link       36211 non-null  object\n",
      " 6   Sequence          35279 non-null  object\n",
      " 7   Taxonomic_Name    36211 non-null  object\n",
      " 8   Locality          23127 non-null  object\n",
      " 9   Sample_accession  3647 non-null   object\n",
      " 10  Collected_by      3043 non-null   object\n",
      " 11  Other_ID          11329 non-null  object\n",
      " 12  Date              23040 non-null  object\n",
      " 13  Dataset           29114 non-null  object\n",
      " 14  Store             27813 non-null  object\n",
      " 15  Brood             13847 non-null  object\n",
      " 16  Death_Date        269 non-null    object\n",
      " 17  Cross_Type        4447 non-null   object\n",
      " 18  Stage             6 non-null      object\n",
      " 19  Sex               27514 non-null  object\n",
      " 20  Unit_Type         25060 non-null  object\n",
      " 21  file_type         36211 non-null  object\n",
      " 22  record_number     36211 non-null  int64 \n",
      " 23  species           36211 non-null  object\n",
      " 24  subspecies        20400 non-null  object\n",
      " 25  genus             36211 non-null  object\n",
      " 26  file_url          36211 non-null  object\n",
      " 27  hybrid_stat       20948 non-null  object\n",
      " 28  filename          36211 non-null  object\n",
      " 29  filepath          36211 non-null  object\n",
      " 30  md5               36211 non-null  object\n",
      "dtypes: int64(2), object(29)\n",
      "memory usage: 8.6+ MB\n"
     ]
    }
   ],
   "source": [
    "master = pd.read_csv(\"../Jiggins_Zenodo_Img_Master.csv\", low_memory = False)\n",
    "master.info()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "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>CAMID</th>\n",
       "      <th>X</th>\n",
       "      <th>Image_name</th>\n",
       "      <th>View</th>\n",
       "      <th>zenodo_name</th>\n",
       "      <th>zenodo_link</th>\n",
       "      <th>Sequence</th>\n",
       "      <th>Taxonomic_Name</th>\n",
       "      <th>Locality</th>\n",
       "      <th>Sample_accession</th>\n",
       "      <th>...</th>\n",
       "      <th>file_type</th>\n",
       "      <th>record_number</th>\n",
       "      <th>species</th>\n",
       "      <th>subspecies</th>\n",
       "      <th>genus</th>\n",
       "      <th>file_url</th>\n",
       "      <th>hybrid_stat</th>\n",
       "      <th>filename</th>\n",
       "      <th>filepath</th>\n",
       "      <th>md5</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>13300</th>\n",
       "      <td>CAM013566</td>\n",
       "      <td>26040</td>\n",
       "      <td>13566_H_m_thelxiopeia_V.JPG.jpg</td>\n",
       "      <td>ventral</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,566</td>\n",
       "      <td>Heliconius melpomene ssp. thelxiopeia</td>\n",
       "      <td>Maripasoula</td>\n",
       "      <td>ERS977708</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>thelxiopeia</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13566_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26040_13566_H_m_thelxiopeia_V.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. thelxiopeia/2...</td>\n",
       "      <td>b5ed68f937132cbbb04a81d550c9f17e</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13301</th>\n",
       "      <td>CAM013566</td>\n",
       "      <td>26039</td>\n",
       "      <td>13566_H_m_thelxiopeia_D.JPG.jpg</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,566</td>\n",
       "      <td>Heliconius melpomene ssp. thelxiopeia</td>\n",
       "      <td>Maripasoula</td>\n",
       "      <td>ERS977708</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>thelxiopeia</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13566_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26039_13566_H_m_thelxiopeia_D.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. thelxiopeia/2...</td>\n",
       "      <td>cb107e2d6a2c3c84b68bbf4e3cfa7af7</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13302</th>\n",
       "      <td>CAM013715</td>\n",
       "      <td>26041</td>\n",
       "      <td>13715_H_m_meriana_D.JPG.jpg</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,715</td>\n",
       "      <td>Heliconius melpomene ssp. meriana</td>\n",
       "      <td>N. Wakapou, Maripausala FR GUIANA</td>\n",
       "      <td>ERS977704</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>meriana</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13715_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26041_13715_H_m_meriana_D.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. meriana/26041...</td>\n",
       "      <td>8c77ecb2d8d878f8ac8b097ad361deda</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13303</th>\n",
       "      <td>CAM013715</td>\n",
       "      <td>26042</td>\n",
       "      <td>13715_H_m_meriana_V.JPG.jpg</td>\n",
       "      <td>ventral</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,715</td>\n",
       "      <td>Heliconius melpomene ssp. meriana</td>\n",
       "      <td>N. Wakapou, Maripausala FR GUIANA</td>\n",
       "      <td>ERS977704</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>meriana</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13715_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26042_13715_H_m_meriana_V.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. meriana/26042...</td>\n",
       "      <td>fcf74c73537323bb90127cf92775ac08</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13304</th>\n",
       "      <td>CAM013819</td>\n",
       "      <td>26044</td>\n",
       "      <td>13819_H_m_meriana_V.JPG.jpg</td>\n",
       "      <td>ventral</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,819</td>\n",
       "      <td>Heliconius melpomene ssp. meriana</td>\n",
       "      <td>N. Wakapou, Maripausala FR GUIANA</td>\n",
       "      <td>ERS977703</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>meriana</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13819_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26044_13819_H_m_meriana_V.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. meriana/26044...</td>\n",
       "      <td>601b67512a8c25e5f25a9da89841e8c2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13305</th>\n",
       "      <td>CAM013819</td>\n",
       "      <td>26043</td>\n",
       "      <td>13819_H_m_meriana_D.JPG.jpg</td>\n",
       "      <td>dorsal</td>\n",
       "      <td>Heliconius_wing_old_photos_2001_2019_part3.csv</td>\n",
       "      <td>https://zenodo.org/record/2553977</td>\n",
       "      <td>13,819</td>\n",
       "      <td>Heliconius melpomene ssp. meriana</td>\n",
       "      <td>N. Wakapou, Maripausala FR GUIANA</td>\n",
       "      <td>ERS977703</td>\n",
       "      <td>...</td>\n",
       "      <td>jpg</td>\n",
       "      <td>2553977</td>\n",
       "      <td>Heliconius melpomene</td>\n",
       "      <td>meriana</td>\n",
       "      <td>Heliconius</td>\n",
       "      <td>https://zenodo.org/record/2553977/files/13819_...</td>\n",
       "      <td>non-hybrid</td>\n",
       "      <td>26043_13819_H_m_meriana_D.JPG.jpg</td>\n",
       "      <td>images/Heliconius melpomene ssp. meriana/26043...</td>\n",
       "      <td>fe4cf831c5203ad6c9010fc047b7fa10</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>6 rows × 31 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           CAMID      X                       Image_name     View  \\\n",
       "13300  CAM013566  26040  13566_H_m_thelxiopeia_V.JPG.jpg  ventral   \n",
       "13301  CAM013566  26039  13566_H_m_thelxiopeia_D.JPG.jpg   dorsal   \n",
       "13302  CAM013715  26041      13715_H_m_meriana_D.JPG.jpg   dorsal   \n",
       "13303  CAM013715  26042      13715_H_m_meriana_V.JPG.jpg  ventral   \n",
       "13304  CAM013819  26044      13819_H_m_meriana_V.JPG.jpg  ventral   \n",
       "13305  CAM013819  26043      13819_H_m_meriana_D.JPG.jpg   dorsal   \n",
       "\n",
       "                                          zenodo_name  \\\n",
       "13300  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "13301  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "13302  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "13303  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "13304  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "13305  Heliconius_wing_old_photos_2001_2019_part3.csv   \n",
       "\n",
       "                             zenodo_link Sequence  \\\n",
       "13300  https://zenodo.org/record/2553977   13,566   \n",
       "13301  https://zenodo.org/record/2553977   13,566   \n",
       "13302  https://zenodo.org/record/2553977   13,715   \n",
       "13303  https://zenodo.org/record/2553977   13,715   \n",
       "13304  https://zenodo.org/record/2553977   13,819   \n",
       "13305  https://zenodo.org/record/2553977   13,819   \n",
       "\n",
       "                              Taxonomic_Name  \\\n",
       "13300  Heliconius melpomene ssp. thelxiopeia   \n",
       "13301  Heliconius melpomene ssp. thelxiopeia   \n",
       "13302      Heliconius melpomene ssp. meriana   \n",
       "13303      Heliconius melpomene ssp. meriana   \n",
       "13304      Heliconius melpomene ssp. meriana   \n",
       "13305      Heliconius melpomene ssp. meriana   \n",
       "\n",
       "                                Locality Sample_accession  ... file_type  \\\n",
       "13300                        Maripasoula        ERS977708  ...       jpg   \n",
       "13301                        Maripasoula        ERS977708  ...       jpg   \n",
       "13302  N. Wakapou, Maripausala FR GUIANA        ERS977704  ...       jpg   \n",
       "13303  N. Wakapou, Maripausala FR GUIANA        ERS977704  ...       jpg   \n",
       "13304  N. Wakapou, Maripausala FR GUIANA        ERS977703  ...       jpg   \n",
       "13305  N. Wakapou, Maripausala FR GUIANA        ERS977703  ...       jpg   \n",
       "\n",
       "      record_number               species   subspecies       genus  \\\n",
       "13300       2553977  Heliconius melpomene  thelxiopeia  Heliconius   \n",
       "13301       2553977  Heliconius melpomene  thelxiopeia  Heliconius   \n",
       "13302       2553977  Heliconius melpomene      meriana  Heliconius   \n",
       "13303       2553977  Heliconius melpomene      meriana  Heliconius   \n",
       "13304       2553977  Heliconius melpomene      meriana  Heliconius   \n",
       "13305       2553977  Heliconius melpomene      meriana  Heliconius   \n",
       "\n",
       "                                                file_url hybrid_stat  \\\n",
       "13300  https://zenodo.org/record/2553977/files/13566_...  non-hybrid   \n",
       "13301  https://zenodo.org/record/2553977/files/13566_...  non-hybrid   \n",
       "13302  https://zenodo.org/record/2553977/files/13715_...  non-hybrid   \n",
       "13303  https://zenodo.org/record/2553977/files/13715_...  non-hybrid   \n",
       "13304  https://zenodo.org/record/2553977/files/13819_...  non-hybrid   \n",
       "13305  https://zenodo.org/record/2553977/files/13819_...  non-hybrid   \n",
       "\n",
       "                                    filename  \\\n",
       "13300  26040_13566_H_m_thelxiopeia_V.JPG.jpg   \n",
       "13301  26039_13566_H_m_thelxiopeia_D.JPG.jpg   \n",
       "13302      26041_13715_H_m_meriana_D.JPG.jpg   \n",
       "13303      26042_13715_H_m_meriana_V.JPG.jpg   \n",
       "13304      26044_13819_H_m_meriana_V.JPG.jpg   \n",
       "13305      26043_13819_H_m_meriana_D.JPG.jpg   \n",
       "\n",
       "                                                filepath  \\\n",
       "13300  images/Heliconius melpomene ssp. thelxiopeia/2...   \n",
       "13301  images/Heliconius melpomene ssp. thelxiopeia/2...   \n",
       "13302  images/Heliconius melpomene ssp. meriana/26041...   \n",
       "13303  images/Heliconius melpomene ssp. meriana/26042...   \n",
       "13304  images/Heliconius melpomene ssp. meriana/26044...   \n",
       "13305  images/Heliconius melpomene ssp. meriana/26043...   \n",
       "\n",
       "                                    md5  \n",
       "13300  b5ed68f937132cbbb04a81d550c9f17e  \n",
       "13301  cb107e2d6a2c3c84b68bbf4e3cfa7af7  \n",
       "13302  8c77ecb2d8d878f8ac8b097ad361deda  \n",
       "13303  fcf74c73537323bb90127cf92775ac08  \n",
       "13304  601b67512a8c25e5f25a9da89841e8c2  \n",
       "13305  fe4cf831c5203ad6c9010fc047b7fa10  \n",
       "\n",
       "[6 rows x 31 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "master.loc[master.Stage.notna()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CAMID                820\n",
       "X                   4447\n",
       "Image_name          4447\n",
       "View                   2\n",
       "zenodo_name            4\n",
       "zenodo_link            4\n",
       "Sequence             820\n",
       "Taxonomic_Name         8\n",
       "Locality               0\n",
       "Sample_accession       0\n",
       "Collected_by           0\n",
       "Other_ID               0\n",
       "Date                 160\n",
       "Dataset                1\n",
       "Store                 10\n",
       "Brood                 49\n",
       "Death_Date             0\n",
       "Cross_Type            30\n",
       "Stage                  0\n",
       "Sex                    2\n",
       "Unit_Type              1\n",
       "file_type              2\n",
       "record_number          4\n",
       "species                2\n",
       "subspecies            21\n",
       "genus                  1\n",
       "file_url            4447\n",
       "hybrid_stat            2\n",
       "filename            4447\n",
       "filepath            4447\n",
       "md5                 4447\n",
       "dtype: int64"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "master.loc[master.Cross_Type.notna()].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "View\n",
       "dorsal                17235\n",
       "ventral               17167\n",
       "hindwing dorsal         254\n",
       "hindwing ventral        254\n",
       "forewing dorsal         252\n",
       "forewing ventral        252\n",
       "dorsal and ventral       18\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "master.View.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generate master_licenses CSV file to re-run [`get_licenses.py`](https://huggingface.co/datasets/imageomics/Comparison-Subset-Jiggins/blob/main/scripts/get_licenses.py) for nicely formatted licensing info of the 29 included records.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(29, 2)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "master_licenses = master[[\"record_number\", \"zenodo_link\"]]\n",
    "master_licenses = master_licenses.loc[~master_licenses.duplicated([\"record_number\", \"zenodo_link\"], keep = \"first\")]\n",
    "master_licenses.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "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>record_number</th>\n",
       "      <th>url</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>4289223</td>\n",
       "      <td>https://zenodo.org/record/4289223</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>4288311</td>\n",
       "      <td>https://zenodo.org/record/4288311</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     record_number                                url\n",
       "0          4289223  https://zenodo.org/record/4289223\n",
       "145        4288311  https://zenodo.org/record/4288311"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "master_licenses.rename(columns = {\"zenodo_link\": \"url\"}, inplace = True)\n",
    "master_licenses.head(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Save to CSV to run through get licenses script\n",
    "master_licenses.to_csv(\"../metadata/master_licenses.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make Heliconius Master Subset\n",
    "\n",
    "Now make the Heliconius master CSV, and check stats to update README."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CAMID               10086\n",
       "X                   29134\n",
       "Image_name          29134\n",
       "View                    3\n",
       "zenodo_name            31\n",
       "zenodo_link            29\n",
       "Sequence             9008\n",
       "Taxonomic_Name        132\n",
       "Locality              471\n",
       "Sample_accession     1559\n",
       "Collected_by           12\n",
       "Other_ID             1865\n",
       "Date                  773\n",
       "Dataset                 7\n",
       "Store                 121\n",
       "Brood                 224\n",
       "Death_Date             79\n",
       "Cross_Type             30\n",
       "Stage                   1\n",
       "Sex                     3\n",
       "Unit_Type               4\n",
       "file_type               3\n",
       "record_number          29\n",
       "species                37\n",
       "subspecies            110\n",
       "genus                   1\n",
       "file_url            29134\n",
       "hybrid_stat             2\n",
       "filename            29134\n",
       "filepath            29134\n",
       "md5                 29134\n",
       "dtype: int64"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heliconius_master = master.loc[master.genus.str.lower() == \"heliconius\"].copy()\n",
    "heliconius_master.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "View\n",
       "dorsal                14202\n",
       "ventral               14135\n",
       "dorsal and ventral       18\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "heliconius_master.View.value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Save Heliconius Subset to CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "heliconius_master.to_csv(\"../Jiggins_Heliconius_Master.csv\", index = False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Make Dorsal Subset\n",
    "\n",
    "We'll now make a CSV of all dorsal images."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CAMID               11752\n",
       "X                   17759\n",
       "Image_name          17759\n",
       "View                    4\n",
       "zenodo_name            31\n",
       "zenodo_link            29\n",
       "Sequence            10689\n",
       "Taxonomic_Name        362\n",
       "Locality              641\n",
       "Sample_accession     1552\n",
       "Collected_by           12\n",
       "Other_ID             2890\n",
       "Date                  788\n",
       "Dataset                 7\n",
       "Store                 137\n",
       "Brood                 215\n",
       "Death_Date             61\n",
       "Cross_Type             30\n",
       "Stage                   1\n",
       "Sex                     3\n",
       "Unit_Type               4\n",
       "file_type               3\n",
       "record_number          29\n",
       "species               245\n",
       "subspecies            152\n",
       "genus                  94\n",
       "file_url            17759\n",
       "hybrid_stat             2\n",
       "filename            17759\n",
       "filepath            17759\n",
       "md5                 17759\n",
       "dtype: int64"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_views = [view for view in list(master.View.dropna().unique()) if \"dorsal\" in view]\n",
    "\n",
    "dorsal_master = master.loc[master[\"View\"].isin(dorsal_views)].copy()\n",
    "dorsal_master.nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CAM_dupe\n",
       "False    15460\n",
       "True      2299\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master[\"CAM_dupe\"] = dorsal_master.duplicated([\"CAMID\", \"file_type\"], keep = False)\n",
    "dorsal_master.CAM_dupe.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CAM_dupe\n",
       "False          15442\n",
       "True            1793\n",
       "single-wing      506\n",
       "both-wings        18\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Adding label for both wings alters counts, as in, some only have the double wing image\n",
    "dorsal_master.loc[dorsal_master[\"View\"].isin([\"forewing dorsal\", \"hindwing dorsal\"]), \"CAM_dupe\"] = \"single-wing\"\n",
    "dorsal_master.loc[dorsal_master[\"View\"] == \"dorsal and ventral\", \"CAM_dupe\"] = \"both-wings\"\n",
    "dorsal_master.CAM_dupe.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "881\n",
      "11452\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "13"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "print(dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == True, \"CAMID\"].nunique())\n",
    "print(dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == False, \"CAMID\"].nunique())\n",
    "dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == True, \"record_number\"].nunique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "file_type\n",
       "raw    253\n",
       "jpg    253\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == \"single-wing\", \"file_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "file_type\n",
       "jpg    17\n",
       "tif     1\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == \"both-wings\", \"file_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "file_type\n",
       "jpg    1775\n",
       "tif      16\n",
       "raw       2\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == True, \"file_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "file_type\n",
       "jpg    10706\n",
       "raw     4718\n",
       "tif       18\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"CAM_dupe\"] == False, \"file_type\"].value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False    10723\n",
       "True      2028\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"file_type\"] == \"jpg\"].duplicated(subset = \"CAMID\", keep = False).value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False    4719\n",
       "True      254\n",
       "Name: count, dtype: int64"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "dorsal_master.loc[dorsal_master[\"file_type\"] == \"raw\"].duplicated(subset = \"CAMID\", keep = False).value_counts()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Generally speaking, it seems RAW images will be unique, as there is only one CAMID that gets duplicated across the raw images (2 photos of that specimen that are RAW) outside of the single wing views.\n",
    "\n",
    "### Save Dorsal Subset to CSV"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "dorsal_master.to_csv(\"../Jiggins_Zenodo_dorsal_Img_Master.csv\", index = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "std",
   "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
}