Repository: wright-group/WrightTools

Issue:
from_cary50 units
Units are read in using readLine, but the object uses 'nm' when the axis is created

Is this expected behavior, if so, consider using `_` instead of `units` as the variable assigned to indicate that it is an intentionally unused return value


Discussion:

Resolving patch:
diff --git a/WrightTools/collection/__init__.py b/WrightTools/collection/__init__.py
--- a/WrightTools/collection/__init__.py
+++ b/WrightTools/collection/__init__.py
@@ -3,6 +3,7 @@
 
 
 from ._collection import *
+from ._cary import *
 
 
-__all__ = ['Collection']
+__all__ = ['Collection', 'from_Cary']
diff --git a/WrightTools/collection/_cary.py b/WrightTools/collection/_cary.py
--- a/WrightTools/collection/_cary.py
+++ b/WrightTools/collection/_cary.py
@@ -4,41 +4,49 @@
 # --- import --------------------------------------------------------------------------------------
 
 
-from __future__ import absolute_import, division, print_function, unicode_literals
-
 import os
+import re
 
 import numpy as np
 
-from ._data import Axis, Channel, Data
 from .. import exceptions as wt_exceptions
+from ._collection import Collection
 
 
 # --- define --------------------------------------------------------------------------------------
 
 
-__all__ = ['from_Cary50']
+__all__ = ['from_Cary']
 
 
 # --- from function -------------------------------------------------------------------------------
 
 
-def from_Cary50(filepath, verbose=True):
-    """Create a data object from a Cary 50 UV VIS absorbance file.
+def from_Cary(filepath, parent=None, verbose=True):
+    """Create a collection object from a Cary UV VIS absorbance file.
+    
+    We hope to support as many Cary instruments and datasets as possible.
+    This function has been tested with data collected on a Cary50 UV/VIS spectrometer.
+    If any alternate instruments are found not to work as expected, please
+    submit a bug report on our `issue tracker`__.
+
+    __issue tracker: github.com/wright-group/WrightTools/issues
 
     .. plot::
 
         >>> import WrightTools as wt
         >>> from WrightTools import datasets
-        >>> p = datasets.Cary50.CuPCtS_H2O_vis
-        >>> data = wt.data.from_Cary50(p)[0]
-        >>> artist = wt.artists.mpl_1D(data)
+        >>> p = datasets.Cary.CuPCtS_H2O_vis
+        >>> data = wt.data.from_Cary(p)[0]
+        >>> artist = wt.artists.Quick1D(data)
         >>> artist.plot()
 
     Parameters
     ----------
     filepath : string
-        Path to Cary50 output file (.csv).
+        Path to Cary output file (.csv).
+    parent : WrightTools.Collection
+        A collection object in which to place a collection of Data objects.
     verbose : boolean (optional)
         Toggle talkback. Default is True.
 
@@ -55,6 +63,7 @@ def from_Cary50(filepath, verbose=True):
     lines = []
     with open(filepath, 'r') as f:
         header = f.readline()
+        columns = f.readline()
         while True:
             line = f.readline()
             if line == '\n' or line == '':
@@ -63,19 +72,24 @@ def from_Cary50(filepath, verbose=True):
                 clean = line[:-2]  # lines end with ',/n'
                 lines.append(np.fromstring(clean, sep=','))
     header = header.split(',')
-    lines = [i for i in lines if len(i) > 0]
+    columns = columns.split(',')
+    #lines = [i for i in lines if len(i) > 0]
     arr = np.array(lines).T
     # chew through all scans
-    datas = []
-    indicies = np.arange(len(header) // 2) * 2
-    for i in indicies:
-        axis = Axis(arr[i], 'nm', name='wm')
-        signal = Channel(arr[i + 1], name='absorbance', label='absorbance', signed=False)
-        data = Data([axis], [signal], source='Cary 50', name=header[i])
-        datas.append(data)
+    datas = Collection(name='cary', parent=parent, edit_local=parent is not None)
+    for i in range(0, len(header)-1, 2):
+        print(columns[i], columns[i+1])
+        r = re.compile("[ \t\(\)]+")
+        spl = r.split(columns[i])
+        ax = spl[0].lower() if len(spl) > 0 else None
+        units = spl[1].lower() if len(spl) > 1 else None
+        dat = datas.create_data(header[i], kind='Cary', source=filepath)
+        dat.create_variable(ax, arr[i], units)
+        dat.create_channel(columns[i+1].lower(), arr[i+1], label=columns[i+1].lower())
+        dat.transform([ax])
     # finish
     if verbose:
-        print('{0} data objects successfully created from Cary 50 file:'.format(len(indicies)))
+        print('{0} data objects successfully created from Cary file:'.format(len(datas)))
         for i, data in enumerate(datas):
-            print('  {0}: {1}'.format(i, data.name))
+            print('  {0}: {1}'.format(i, data))
     return datas
diff --git a/WrightTools/kit/_utilities.py b/WrightTools/kit/_utilities.py
--- a/WrightTools/kit/_utilities.py
+++ b/WrightTools/kit/_utilities.py
@@ -36,6 +36,8 @@ def string2identifier(s):
     """
     # https://docs.python.org/3/reference/lexical_analysis.html#identifiers
     # https://www.python.org/dev/peps/pep-3131/
+    if len(s) == 0:
+        return '_'
     if s[0] not in string.ascii_letters:
         s = '_' + s
     valids = string.ascii_letters + string.digits + '_'
@@ -45,8 +47,6 @@ def string2identifier(s):
             out += char
         else:
             out += '_'
-    if out[0] in string.digits:
-        out = '_' + out
     return out
 
 


Test patch:
diff --git a/tests/data/from_Cary50.py b/tests/data/from_Cary50.py
deleted file mode 100644
--- a/tests/data/from_Cary50.py
+++ /dev/null
@@ -1,27 +0,0 @@
-"""test from_Cary50"""
-
-
-# --- import --------------------------------------------------------------------------------------
-
-
-import pytest
-
-import WrightTools as wt
-from WrightTools import datasets
-
-
-# --- test ----------------------------------------------------------------------------------------
-
-
-@pytest.mark.skip()
-def test_CuPCtS_H2O_vis():
-    p = datasets.Cary50.CuPCtS_H2O_vis
-    datas = wt.data.from_Cary50(p)
-
-    assert isinstance(datas, list)
-    assert len(datas) == 1
-
-    data = datas[0]
-    assert data.shape == (141,)
-    assert data.axis_names == ('wm',)
-    datas.close()