Skip to content

Commit

Permalink
corrected problem with strings in mm.desc and better test for sampleIO
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfus committed Mar 21, 2017
1 parent 6cdec6f commit c2ce01d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 11 deletions.
24 changes: 20 additions & 4 deletions muesr/core/magmodel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
# http://magcryst.org/resources/magnetic-coordinates/

import numpy as np
try:
from six import string_types
def isstr(s):
return isinstance(s, string_types)
except ImportError:
try:
isinstance("", basestring)
def isstr(s):
return isinstance(s, basestring)
except NameError:
def isstr(s):
return isinstance(s, str)

from muesr.core.cells import get_cell_parameters

have_sympy = True
Expand Down Expand Up @@ -299,10 +312,13 @@ def desc(self):

@desc.setter
def desc(self, value):
try:
value = str(value)
self._description = value
except:
if isstr(value):
try:
value = str(value)
self._description = value
except:
raise TypeError("Description type must be type string, got {} instead.".format(type(value)))
else:
raise TypeError("Description type must be type string, got {} instead.".format(type(value)))


Expand Down
26 changes: 19 additions & 7 deletions muesr/tests/io/test_sampleIO.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ class TestSampleIO(unittest.TestCase):
def setUp(self):
warnings.simplefilter("ignore")

@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_invalid_sample(self):
with self.assertRaises(ValueError):
load_sample()
Expand All @@ -268,6 +269,7 @@ def test_invalid_sample(self):
with self.assertRaises(TypeError):
save_sample(1,"")

@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_only_latt(self):
s = load_sample("",StringIO(yaml_only_lattice))
np.testing.assert_array_equal(s.cell.get_cell(),
Expand All @@ -279,7 +281,7 @@ def test_load_only_latt(self):



@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_latt_and_one_mag(self):
s = load_sample("",StringIO(yaml_lattice_and_one_mag))
np.testing.assert_array_equal(s.cell.get_cell(),
Expand All @@ -299,21 +301,23 @@ def test_load_latt_and_one_mag(self):
with self.assertRaises(IndexError):
s.current_mm_idx = 1

@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_only_one_mag(self):
with self.assertRaises(CellError):
load_sample("",StringIO(yaml_only_one_mag))

@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_invalid_yaml(self):
with self.assertRaises(ValueError):
load_sample("",StringIO(yaml_invalid))


@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_lattice_and_muon(self):
s = load_sample("",StringIO(yaml_lattice_and_muon))
np.testing.assert_array_equal(s.muons[0], np.array([0.5,0.5,0.5]))
np.testing.assert_array_equal(s.muons[1], np.array([0.75,0.25,0.5]))


@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_lattice_and_sym(self):
s = load_sample("",StringIO(yaml_latt_and_sym))
np.testing.assert_array_equal(s.cell.get_cell(),
Expand All @@ -327,22 +331,30 @@ def test_load_lattice_and_sym(self):

self.assertEqual(s.sym.symbol,'F -4 3 m')


@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_store_and_load_empty_structure(self):
l = Sample()
myfile = StringIO()
save_sample(l,"",myfile)
myfile.seek(0)
t = load_sample("",myfile)


@unittest.skipIf(have_yaml == False, 'PyYaml not available')
def test_load_cartesian_positions(self):
s = load_sample("",StringIO(yaml_lattice_cartesian_positions))
spos = s.cell.get_scaled_positions()

np.testing.assert_array_equal(spos,
np.array([[0,0,0],[0.25,0.25,-0.75]])%1)


@unittest.skipIf(have_yaml == True, 'PyYaml available')
def test_load_save_sample_no_yaml(self):
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
load_sample("",StringIO(yaml_lattice_cartesian_positions))
assert len(w) == 1
save_sample(None)
assert len(w) == 2

if __name__ == '__main__':
unittest.main()

0 comments on commit c2ce01d

Please sign in to comment.