Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix extraction of file name extensions #10

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions nanonispy/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ def _determine_filetype(self):
'sxm', or 'dat'.
"""

if self.fname[-3:] == '3ds':
_, fname_ext = os.path.splitext(self.fname)
if fname_ext == '.3ds':
return 'grid'
elif self.fname[-3:] == 'sxm':
elif fname_ext == '.sxm':
return 'scan'
elif self.fname[-3:] == 'dat':
elif fname_ext == '.dat':
return 'spec'
else:
raise UnhandledFileError('{} is not a supported filetype or does not exist'.format(self.basename))
Expand Down Expand Up @@ -741,5 +742,6 @@ def _is_valid_file(fname, ext):
"""
Detect if invalid file is being initialized by class.
"""
if fname[-3:] != ext:
_, fname_ext = os.path.splitext(fname)
if fname_ext[1:] != ext:
raise UnhandledFileError('{} is not a {} file'.format(fname, ext))
27 changes: 19 additions & 8 deletions nanonispy/tests/test_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ def setUp(self):
def tearDown(self):
self.temp_dir.cleanup()

def create_dummy_grid_data(self, suffix='3ds'):
def create_dummy_grid_data(self, suffix='.3ds'):
"""
return tempfile file object with dummy header info
"""
Expand All @@ -159,7 +159,7 @@ def create_dummy_grid_data(self, suffix='3ds'):

return f

def create_dummy_grid_data_v2(self, suffix='3ds'):
def create_dummy_grid_data_v2(self, suffix='.3ds'):
"""
return tempfile file object with dummy header info
"""
Expand Down Expand Up @@ -198,7 +198,7 @@ def test_sweep_signal_calculated(self):

def test_raises_correct_instance_error(self):
with self.assertRaises(nap.read.UnhandledFileError):
f = self.create_dummy_grid_data(suffix='sxm')
f = self.create_dummy_grid_data(suffix='.sxm')
GF = nap.read.Grid(f.name)

def test_header_entries(self):
Expand Down Expand Up @@ -312,10 +312,21 @@ def test_header_entries(self):
'scanit_type': 'FLOAT MSBFIRST',
'z-controller': "{'P-gain': ('7.000E-12 m',), 'Setpoint': ('1.000E-10 A',), 'on': ('1',), 'T-const': ('2.000E-3 s',), 'Name': ('Current #3',), 'I-gain': ('3.500E-9 m/s',)}"}

for key in SF.header:
a = ''.join(sorted(str(SF.header[key])))
b = ''.join(sorted(test_dict[key]))
self.assertEqual(a, b)
for key, value in SF.header.items():
if isinstance(value, np.ndarray):
a = value
b = np.fromstring(test_dict[key].strip('[]'), sep=' ')
np.testing.assert_almost_equal(a, b)
elif isinstance(value, np.float):
print(key, value)
a = value
b = np.float(test_dict[key])
np.testing.assert_almost_equal(a, b)
print(key, value)
else:
a = ''.join(sorted(str(SF.header[key]))).strip()
b = ''.join(sorted(test_dict[key])).strip()
self.assertEqual(a, b)

def test_raises_correct_instance_error(self):
with self.assertRaises(nap.read.UnhandledFileError):
Expand All @@ -329,7 +340,7 @@ def setUp(self):
def tearDown(self):
self.temp_dir.cleanup()

def create_dummy_spec_data(self, suffix='dat'):
def create_dummy_spec_data(self, suffix='.dat'):
base = os.path.dirname(__file__)
f = open(base+'/Bias-Spectroscopy002.dat', 'rb')
f.close()
Expand Down