Repository: ercius/openNCEM (language: python)

Issue:
module 'ncempy' has no attribute 'io'
While (first time) using py4dstem, I encountered many problems from its dependencies including (llvmlite, numpy, matplotlib, etc.) all the problems were solved by downgrade to python to 3.6.12 and then reinstall the dependencies.

However, I finally encountered a problem originated in ncempy (which I could not identify the origin of problem and not too much resources on the internet):
module 'ncempy' has no attribute 'io'

I tried pip uninstall ncempy and installed again, but not helpful. Therefore I'm posting here, dont know if its the right place to solve my problem.

(py4dstem) C:\WINDOWS\system32>py4dstem
Traceback (most recent call last):
  File "c:\programdata\miniconda3\envs\py4dstem\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\programdata\miniconda3\envs\py4dstem\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\ProgramData\Miniconda3\envs\py4dstem\Scripts\py4DSTEM.exe\__main__.py", line 4, in <module>
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\__init__.py", line 1, in <module>
    from . import process
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\process\__init__.py", line 3, in <module>
    from . import preprocess
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\process\preprocess\__init__.py", line 3, in <module>
    from .electroncount import *
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\process\preprocess\electroncount.py", line 11, in <module>
    from ...io.datastructure import PointListArray
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\io\__init__.py", line 3, in <module>
    from . import nonnative
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\io\nonnative\__init__.py", line 1, in <module>
    from .read_dm import *
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\py4DSTEM\io\nonnative\read_dm.py", line 5, in <module>
    from ncempy.io import dm
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\ncempy\io\__init__.py", line 6, in <module>
    from .read import read
  File "c:\programdata\miniconda3\envs\py4dstem\lib\site-packages\ncempy\io\read.py", line 1, in <module>
    import ncempy.io as nio
AttributeError: module 'ncempy' has no attribute 'io'


Discussion / hints:
@w4m0330 Thanks for reporting this. The generalized read() functionality in ncempy was just released and there might be a problem with the imports. Im looking at this now to try and get a fix asap.

Fix patch:
diff --git a/ncempy/io/__init__.py b/ncempy/io/__init__.py
index 561c42a..b0c720d 100644
--- a/ncempy/io/__init__.py
+++ b/ncempy/io/__init__.py
@@ -3,4 +3,56 @@
 from . import emd
 from . import mrc
 from . import emdVelox
-from .read import read
+# from .read import read
+
+from pathlib import Path
+
+
+def read(filename, dsetNum=0):
+    """
+    Parameters
+    ----------
+    filename : str or pathlib.Path
+        The path and name of the file to attempt to load. This chooses the Reader() function based on the file
+        suffix.
+    dsetNum : int
+        The data set number to load if there are multiple data sets in a file. This is implemented for EMD and DM
+        files only.
+    """
+
+    out = {}
+
+    # check filename type
+    if isinstance(filename, str):
+        filename = Path(filename)
+    elif isinstance(filename, Path):
+        pass
+    else:
+        raise TypeError('Filename is supposed to be a string or pathlib.Path')
+
+    if not filename.exists():
+        raise FileNotFoundError
+
+    # ensure lowercase suffix
+    suffix = filename.suffix.lower()
+
+    if suffix == '.ser':
+        out = ser.serReader(filename)
+    elif suffix in ('.dm3', '.dm4'):
+        out = dm.dmReader(filename)
+    elif suffix in ('.emd', '.h5', '.hdf5'):
+        is_velox = False
+        with emd.fileEMD(filename) as emd0:
+            if len(emd0.list_emds) > 0:
+                out = emd.emdReader(filename, dsetNum)
+            else:
+                is_velox = True
+        if is_velox:
+            out = emdVelox.emdVeloxReader(filename, dsetNum)
+    elif suffix in ('.mrc', '.rec', '.st', '.ali'):
+        out = mrc.mrcReader(filename)
+    else:
+        print('File suffix {} is not recognized.'.format(suffix))
+        print('Supported formats are ser, mrc, rec, ali, st, emd, dm3, dm4.')
+
+    return out
diff --git a/ncempy/io/read.py b/ncempy/io/read.py
deleted file mode 100644
index aee5bbb..0000000
--- a/ncempy/io/read.py
+++ /dev/null
@@ -1,53 +0,0 @@
-import ncempy.io as nio
-
-from pathlib import Path
-
-
-def read(filename, dsetNum=0):
-    """
-    Parameters
-    ----------
-    filename : str or pathlib.Path
-        The path and name of the file to attempt to load. This chooses the Reader() function based on the file
-        suffix.
-    dsetNum : int
-        The data set number to load if there are multiple data sets in a file. This is implemented for EMD and DM
-        files only.
-    """
-
-    out = {}
-
-    # check filename type
-    if isinstance(filename, str):
-        filename = Path(filename)
-    elif isinstance(filename, Path):
-        pass
-    else:
-        raise TypeError('Filename is supposed to be a string or pathlib.Path')
-
-    if not filename.exists():
-        raise FileNotFoundError
-
-    # ensure lowercase suffix
-    suffix = filename.suffix.lower()
-
-    if suffix == '.ser':
-        out = nio.ser.serReader(filename)
-    elif suffix in ('.dm3', '.dm4'):
-        out = nio.dm.dmReader(filename)
-    elif suffix in ('.emd', '.h5', '.hdf5'):
-        is_velox = False
-        with nio.emd.fileEMD(filename) as emd0:
-            if len(emd0.list_emds) > 0:
-                out = nio.emd.emdReader(filename, dsetNum)
-            else:
-                is_velox = True
-        if is_velox:
-            out = nio.emdVelox.emdVeloxReader(filename, dsetNum)
-    elif suffix in ('.mrc', '.rec', '.st', '.ali'):
-        out = nio.mrc.mrcReader(filename)
-    else:
-        print('File suffix {} is not recognized.'.format(suffix))
-        print('Supported formats are ser, mrc, rec, ali, st, emd, dm3, dm4.')
-
-    return out


Test patch:
diff --git a/ncempy/test/test_io.py b/ncempy/test/test_io.py
index 6604053..4745118 100644
--- a/ncempy/test/test_io.py
+++ b/ncempy/test/test_io.py
@@ -1,4 +1,4 @@
-"""General tests for io module. More in depth testing is done in sepearte test_.py files for more advanced
+"""General tests for io module. More in depth testing is done in separate test_.py files for more advanced
 capabilities.
 """
 
@@ -9,7 +9,6 @@
 import numpy as np
 import ncempy.io as nio
 
-# Todo: Add emd velox (small file needed)
 
 @pytest.fixture
 def data_location():
@@ -18,6 +17,7 @@ def data_location():
     root_path = test_path.parents[1]
     return root_path / Path('data')
 
+
 @pytest.fixture
 def temp_file():
     tt = tempfile.NamedTemporaryFile(mode='wb')
@@ -86,4 +86,4 @@ def test_read(data_location):
     for file in all_files:
         file_dict = nio.read(file)
         if file_dict:
-            assert 'data' in file_dict
\ No newline at end of file
+            assert 'data' in file_dict