Repository: wright-group/WrightTools

Issue:
from_Cary handle duplicate named data
Currently it will fail to read in data that has the same column header


Discussion:
The Cary software apparently makes it easy to leave the same names. This leads to "Object Already Exists warnings" at import time

Resolving patch:
diff --git a/WrightTools/collection/_cary.py b/WrightTools/collection/_cary.py
--- a/WrightTools/collection/_cary.py
+++ b/WrightTools/collection/_cary.py
@@ -4,7 +4,7 @@
 # --- import --------------------------------------------------------------------------------------
 
 
-import os
+import pathlib
 import re
 
 import numpy as np
@@ -55,14 +55,14 @@ def from_Cary(filepath, name=None, parent=None, verbose=True):
         New data object.
     """
     # check filepath
-    filesuffix = os.path.basename(filepath).split(".")[-1]
-    if filesuffix != "csv":
+    filesuffix = pathlib.Path(filepath).suffix
+    if filesuffix != ".csv":
         wt_exceptions.WrongFileTypeWarning.warn(filepath, "csv")
     if name is None:
         name = "cary"
     # import array
     lines = []
-    with open(filepath, "r", encoding="iso-8859-1") as f:
+    with open(str(filepath), "r", encoding="iso-8859-1") as f:
         header = f.readline()
         columns = f.readline()
         while True:
@@ -83,6 +83,7 @@ def from_Cary(filepath, name=None, parent=None, verbose=True):
     header = header.split(",")
     columns = columns.split(",")
     arr = np.array(lines).T
+    duplicate = len(header) // 2 == len(set(header) - {""})
     # chew through all scans
     datas = Collection(name=name, parent=parent, edit_local=parent is not None)
     units_dict = {"°c": "deg_C", "°f": "deg_F"}
@@ -92,7 +93,11 @@ def from_Cary(filepath, name=None, parent=None, verbose=True):
         ax = spl[0].lower() if len(spl) > 0 else None
         units = spl[1].lower() if len(spl) > 1 else None
         units = units_dict.get(units, units)
-        dat = datas.create_data(header[i], kind="Cary", source=filepath)
+        if duplicate:
+            name = "{}_{:03d}".format(header[i], i // 2)
+        else:
+            name = header[i]
+        dat = datas.create_data(name, kind="Cary", source=filepath)
         dat.create_variable(ax, arr[i][~np.isnan(arr[i])], units=units)
         dat.create_channel(
             columns[i + 1].lower(), arr[i + 1][~np.isnan(arr[i + 1])], label=columns[i + 1].lower()
diff --git a/examples/filters.py b/examples/filters.py
--- a/examples/filters.py
+++ b/examples/filters.py
@@ -1,10 +1,14 @@
 #! /usr/bin/env python3
 # -*- coding: utf-8 -*-
 """
-Optical Filters
-===============
+Plotting Multiple Lines
+=======================
 
-A set of optical filters transmission spectra.
+A quick demonstration of how to plot multiple lines on the same
+set of axes, using :meth:`create_figure` to have a set of axes
+which can plot data objects directly.
+
+The dataset is a set of optical filters transmission spectra.
 """
 
 import WrightTools as wt


Test patch:
diff --git a/tests/collection/from_cary.py b/tests/collection/from_cary.py
--- a/tests/collection/from_cary.py
+++ b/tests/collection/from_cary.py
@@ -3,6 +3,10 @@
 import WrightTools as wt
 from WrightTools import datasets
 
+import pathlib
+
+here = pathlib.Path(__file__).parent.resolve()
+
 
 def test_CuPCtS_H2O_vis():
     p = datasets.Cary.CuPCtS_H2O_vis
@@ -25,6 +29,15 @@ def test_filters():
         assert d.shape == (sh,)
 
 
+def test_duplicate_name():
+    p = here.parent / "data" / "test_data" / "duplicate_name.csv"
+    col = wt.collection.from_Cary(p)
+    assert len(col) == 2
+    assert col.item_names == ("sample1_000", "sample1_001")
+    col.close()
+
+
 if __name__ == "__main__":
     test_CuPCtS_H2O_vis()
     test_filters()
+    test_duplicate_name()
diff --git a/tests/data/test_data/duplicate_name.csv b/tests/data/test_data/duplicate_name.csv
new file mode 100644
--- /dev/null
+++ b/tests/data/test_data/duplicate_name.csv
@@ -0,0 +1,180 @@
+sample1,,sample1,,
+Wavelength (nm),Abs,Wavelength (nm),Abs,
+899.9957886,0.03348613903,899.9957886,0.03348613903,
+894.9515991,0.03267561272,894.9515991,0.03267561272,
+890.0282593,0.0340631865,890.0282593,0.0340631865,
+884.9595337,0.03194546327,884.9595337,0.03194546327,
+880.0124512,0.03444465995,880.0124512,0.03444465995,
+875.0536499,0.03493067622,875.0536499,0.03493067622,
+869.9489136,0.03562863171,869.9489136,0.03562863171,
+864.9667969,0.03332911059,864.9667969,0.03332911059,
+859.9732666,0.0383875519,859.9732666,0.0383875519,
+854.9684448,0.03487577662,854.9684448,0.03487577662,
+849.9522705,0.03669951484,849.9522705,0.03669951484,
+845.0609741,0.03872286156,845.0609741,0.03872286156,
+840.0227661,0.03809593245,840.0227661,0.03809593245,
+834.9734497,0.03798785433,834.9734497,0.03798785433,
+830.0500488,0.04011700675,830.0500488,0.04011700675,
+824.9790649,0.03958647698,824.9790649,0.03958647698,
+820.034729,0.03906268254,820.034729,0.03906268254,
+814.9423218,0.03957082331,814.9423218,0.03957082331,
+809.9772339,0.04162682593,809.9772339,0.04162682593,
+805.0020142,0.04275555164,805.0020142,0.04275555164,
+800.0167847,0.04555374011,800.0167847,0.04555374011,
+795.0215454,0.0459511131,795.0215454,0.0459511131,
+790.0163574,0.04870507866,790.0163574,0.04870507866,
+785.0012817,0.04824688658,785.0012817,0.04824688658,
+779.9763794,0.04949647188,779.9763794,0.04949647188,
+774.9417725,0.05269194022,774.9417725,0.05269194022,
+770.0377197,0.05606371909,770.0377197,0.05606371909,
+764.9840698,0.05658157542,764.9840698,0.05658157542,
+760.0616455,0.05915939063,760.0616455,0.05915939063,
+754.9891968,0.0616087839,754.9891968,0.0616087839,
+750.048645,0.06570582092,750.048645,0.06570582092,
+744.9576416,0.06970942765,744.9576416,0.06970942765,
+739.9991455,0.07373746485,739.9991455,0.07373746485,
+735.0319214,0.08101084083,735.0319214,0.08101084083,
+730.0560303,0.08643627912,730.0560303,0.08643627912,
+724.9289551,0.09450961649,724.9289551,0.09450961649,
+719.9356079,0.1062590405,719.9356079,0.1062590405,
+714.9337158,0.1166885942,714.9337158,0.1166885942,
+710.0667114,0.1293438822,710.0667114,0.1293438822,
+705.0482788,0.1457461417,705.0482788,0.1457461417,
+700.0215454,0.1629926413,700.0215454,0.1629926413,
+694.9865112,0.1829644889,694.9865112,0.1829644889,
+689.9432373,0.2040890902,689.9432373,0.2040890902,
+685.0363159,0.2260486484,685.0363159,0.2260486484,
+679.9770508,0.2448939234,679.9770508,0.2448939234,
+675.0546875,0.2602799833,675.0546875,0.2602799833,
+669.9796753,0.2753000259,669.9796753,0.2753000259,
+665.0420532,0.2870416045,665.0420532,0.2870416045,
+659.9514771,0.3019529879,659.9514771,0.3019529879,
+654.9989014,0.319217205,654.9989014,0.319217205,
+650.0390625,0.3406883776,650.0390625,0.3406883776,
+645.0718994,0.3710591495,645.0718994,0.3710591495,
+639.9511719,0.4078614712,639.9511719,0.4078614712,
+634.9695435,0.4439098835,634.9695435,0.4439098835,
+629.9807739,0.4819462597,629.9807739,0.4819462597,
+624.9850464,0.5155558586,624.9850464,0.5155558586,
+619.9822998,0.5369343758,619.9822998,0.5369343758,
+614.9726563,0.5448060036,614.9726563,0.5448060036,
+609.9561768,0.538412571,609.9561768,0.538412571,
+604.9328613,0.5163750052,604.9328613,0.5163750052,
+600.0509033,0.4809221029,600.0509033,0.4809221029,
+595.0144043,0.4370094836,595.0144043,0.4370094836,
+589.9712524,0.3952301145,589.9712524,0.3952301145,
+585.0701294,0.3509055376,585.0701294,0.3509055376,
+580.0140381,0.3052073717,580.0140381,0.3052073717,
+574.9515381,0.2623192668,574.9515381,0.2623192668,
+570.0317993,0.2262839973,570.0317993,0.2262839973,
+564.956665,0.1896870881,564.956665,0.1896870881,
+560.0248413,0.1587835997,560.0248413,0.1587835997,
+554.9373779,0.1323866099,554.9373779,0.1323866099,
+549.9936523,0.111593917,549.9936523,0.111593917,
+545.0441284,0.09646024555,545.0441284,0.09646024555,
+539.9386597,0.08375040442,539.9386597,0.08375040442,
+534.9776001,0.0743104443,534.9776001,0.0743104443,
+530.0108643,0.06651932746,530.0108643,0.06651932746,
+525.0386353,0.05982564017,525.0386353,0.05982564017,
+520.0608521,0.05498801917,520.0608521,0.05498801917,
+514.9265137,0.05138467997,514.9265137,0.05138467997,
+509.9377441,0.04912013188,509.9377441,0.04912013188,
+504.943573,0.04632218182,504.943573,0.04632218182,
+499.9441223,0.04546779022,499.9441223,0.04546779022,
+494.9393921,0.04355313629,494.9393921,0.04355313629,
+489.9295044,0.04311966524,489.9295044,0.04311966524,
+485.0664978,0.04220775142,485.0664978,0.04220775142,
+480.0465088,0.04172854498,480.0465088,0.04172854498,
+475.0214539,0.0419515036,475.0214539,0.0419515036,
+469.9914551,0.04141701758,469.9914551,0.04141701758,
+464.9564819,0.04201611504,464.9564819,0.04201611504,
+460.069458,0.04249503836,460.069458,0.04249503836,
+455.0249329,0.04295591637,455.0249329,0.04295591637,
+449.9756165,0.04409392178,449.9756165,0.04409392178,
+445.0748291,0.04597275332,445.0748291,0.04597275332,
+440.0162659,0.04861274362,440.0162659,0.04861274362,
+434.9530945,0.05074971914,434.9530945,0.05074971914,
+430.0389709,0.05335939676,430.0389709,0.05335939676,
+424.9668579,0.05591168255,424.9668579,0.05591168255,
+420.0441895,0.05882286653,420.0441895,0.05882286653,
+414.9633179,0.06308992952,414.9633179,0.06308992952,
+410.0322571,0.06885458529,410.0322571,0.06885458529,
+404.9428711,0.07716992497,404.9428711,0.07716992497,
+400.0036316,0.08727035671,400.0036316,0.08727035671,
+395.0604553,0.1008034423,395.0604553,0.1008034423,
+389.9587097,0.1194919422,389.9587097,0.1194919422,
+385.0076599,0.1419151872,385.0076599,0.1419151872,
+380.0527954,0.1704951674,380.0527954,0.1704951674,
+374.9391785,0.2073469013,374.9391785,0.2073469013,
+369.9767456,0.2444507927,369.9767456,0.2444507927,
+365.0106506,0.2903826833,365.0106506,0.2903826833,
+360.0409851,0.3376181424,360.0409851,0.3376181424,
+355.0677185,0.387022078,355.0677185,0.387022078,
+349.9353943,0.4372353256,349.9353943,0.4372353256,
+344.9550781,0.4818612337,344.9550781,0.4818612337,
+339.9713745,0.5199708343,339.9713745,0.5199708343,
+334.9842834,0.5454449654,334.9842834,0.5454449654,
+329.9938965,0.5493141413,329.9938965,0.5493141413,
+325.0002441,0.5268580317,325.0002441,0.5268580317,
+320.0033875,0.4877691269,320.0033875,0.4877691269,
+315.0033875,0.4494712353,315.0033875,0.4494712353,
+310.0002441,0.4402593672,310.0002441,0.4402593672,
+304.9940796,0.419529438,304.9940796,0.419529438,
+299.9848633,0.3635073006,299.9848633,0.3635073006,
+294.9727173,0.330201149,294.9727173,0.330201149,
+289.9576416,0.3332882226,289.9576416,0.3332882226,
+284.9396973,0.3433443308,284.9396973,0.3433443308,
+280.0758667,0.3542826176,280.0758667,0.3542826176,
+275.0524292,0.3819708526,275.0524292,0.3819708526,
+270.0262756,0.4122518599,270.0262756,0.4122518599,
+264.997467,0.4360752404,264.997467,0.4360752404,
+259.9660339,0.4566881359,259.9660339,0.4566881359,
+254.9320374,0.4734400213,254.9320374,0.4734400213,
+250.0529327,0.4838670492,250.0529327,0.4838670492,
+245.0437622,0.496350497,245.0437622,0.496350497,
+240.0325012,0.5303069353,240.0325012,0.5303069353,
+235.0188751,0.5915038586,235.0188751,0.5915038586,
+230.0029449,0.6734064221,230.0029449,0.6734064221,
+224.9847565,0.7468687892,224.9847565,0.7468687892,
+219.9643555,0.7977790833,219.9643555,0.7977790833,
+214.941803,0.8031233549,214.941803,0.8031233549,
+210.0742035,0.755441308,210.0742035,0.755441308,
+205.0475464,0.6903881431,205.0475464,0.6903881431,
+200.0188751,0.6591586471,200.0188751,0.6591586471,
+
+sample1,
+sample1
+Collection Time: 1/15/2014 4:59:54 PM
+Operator Name  :
+Scan Software Version: 3.00(182)
+Parameter List :
+Instrument                        Cary 50
+Instrument Version                3.00
+Start (nm)                        900.0
+Stop (nm)                         200.0
+X Mode                            Nanometers
+Y Mode                            Abs
+UV-Vis Scan Rate (nm/min)         24000.000
+UV-Vis Data Interval (nm)         5.00
+UV-Vis Ave. Time (sec)            0.0125
+Beam Mode                         Dual Beam
+Baseline Correction               On
+Baseline Type                     Baseline correction
+Baseline File Name
+Baseline Std Ref File Name
+Cycle Mode                        Off
+Comments
+Method Log     :
+Method Name    : Default
+Date/Time stamp: 1/15/2014 4:57:08 PM
+Method Modifications:
+Cell Changer 6x6 Changed: 1/15/2014 4:57:45 PM, Old:1, New:0
+UVVIS SAT Changed: 1/15/2014 4:57:46 PM, Old:0.1000, New:0.0125
+NIR SAT Changed: 1/15/2014 4:57:46 PM, Old:0.1000, New:0.0125
+Common SAT Changed: 1/15/2014 4:57:46 PM, Old:0.1000, New:0.0125
+Baseline Correction Changed: 1/15/2014 4:57:52 PM, Old:0, New:1
+Temp Controller Changed: 1/15/2014 4:57:52 PM, Old:0, New:2
+Sipper Type Changed: 1/15/2014 4:57:52 PM, Old:Internal RSA, New:External sipper
+End Method Modifications
+<Current Wavelength> , 225.0
+