Skip to content

Commit

Permalink
fixed float input bug
Browse files Browse the repository at this point in the history
release of 1.6.12
  • Loading branch information
zhangy6x committed Feb 26, 2019
1 parent dd65684 commit 30588ba
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
5 changes: 5 additions & 0 deletions ImagingReso/_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,12 @@ def formula_to_dictionary(formula='', thickness=np.NaN, density=np.NaN, database
'units': 'g/mol'},
}
"""

if '.' in formula:
raise ValueError("formula '{}' is invalid, containing symbol '{}' !".format(formula, '.'))
_formula_parsed = re.findall(r'([A-Z][a-z]*)(\d*)', formula)
if len(_formula_parsed) == 0:
raise ValueError("formula '{}' is invalid !".format(formula))

# _dictionary = {}
_elements_array = []
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def read_file(filename):

setup(
name="ImagingReso",
version="1.6.11",
version="1.6.12",
author="Yuxuan (Shawn) Zhang, Jean Bilheux",
author_email="[email protected], [email protected]",
packages=find_packages(exclude=['tests', 'notebooks']),
Expand Down
55 changes: 38 additions & 17 deletions tests/ImagingReso/utilities_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_is_element_in_database(self):
_answer = is_element_in_database(element=_element, database=self.database)
self.assertTrue(_answer)

# invalid element
_element = 'ab'
_answer = is_element_in_database(element=_element, database=self.database)
self.assertFalse(_answer)

def test_checking_stack(self):
"""assert checking_stack_works in all good cases (1 or more stacks)"""

Expand Down Expand Up @@ -100,9 +105,25 @@ def test_raises_error_when_size_stoichiometric_ratio_is_different_from_elements(

def test_formula_to_dictionary_raises_error_when_unknow_element(self):
"""assert formula_to_dictionary raises error if element is unknown"""
# invalid element name
_formula = 'NeCo'
self.assertRaises(ValueError, formula_to_dictionary, formula=_formula, database=self.database)

# all small letter
_formula = 'ab'
self.assertRaises(ValueError, formula_to_dictionary, formula=_formula, database=self.database)

# empty str
_formula = ''
self.assertRaises(ValueError, formula_to_dictionary, formula=_formula, database=self.database)

# float not supported due to parsing limitation
_formula = 'Co1.7Cu1.2'
self.assertRaises(ValueError, formula_to_dictionary, formula=_formula, database=self.database)

_formula = 'AA'
self.assertRaises(ValueError, formula_to_dictionary, formula=_formula, database=self.database)

def test_formula_to_dictionary_works_with_various_cases(self):
"""assert formulla_to_dictionary works in all cases"""

Expand Down Expand Up @@ -317,7 +338,7 @@ def test_get_sigma(self):
energy_last_expected = 600
sigma_last_expected = 7.2375030
self.assertEqual(energy_last_expected, energy_last_returned)
self.assertAlmostEquals(sigma_last_expected, sigma_last_returned, delta=0.0001)
self.assertAlmostEqual(sigma_last_expected, sigma_last_returned, delta=0.0001)

energy_1_returned = _dict_returned['energy_eV'][1]
energy_1_expected = 310
Expand Down Expand Up @@ -392,31 +413,31 @@ def test_energy_angstroms_conversion(self):
_to = ev_to_angstroms(array=_from)
_back = angstroms_to_ev(array=_to)

self.assertAlmostEquals(_from[0], _back[0], delta=0.0001)
self.assertAlmostEquals(_from[1], _back[1], delta=0.0001)
self.assertAlmostEquals(_from[2], _back[2], delta=0.0001)
self.assertAlmostEquals(_from[3], _back[3], delta=0.0001)
self.assertAlmostEqual(_from[0], _back[0], delta=0.0001)
self.assertAlmostEqual(_from[1], _back[1], delta=0.0001)
self.assertAlmostEqual(_from[2], _back[2], delta=0.0001)
self.assertAlmostEqual(_from[3], _back[3], delta=0.0001)

def test_ev_s_conversion(self):
"""assert ev_to_s works"""
_from = np.linspace(1, 10, 10)
_to = ev_to_s(array=_from, offset_us=2.7, source_to_detector_m=16.)
_back = s_to_ev(array=_to, offset_us=2.7, source_to_detector_m=16.)

self.assertAlmostEquals(_from[0], _back[0], delta=0.0001)
self.assertAlmostEquals(_from[1], _back[1], delta=0.0001)
self.assertAlmostEquals(_from[2], _back[2], delta=0.0001)
self.assertAlmostEquals(_from[3], _back[3], delta=0.0001)
self.assertAlmostEqual(_from[0], _back[0], delta=0.0001)
self.assertAlmostEqual(_from[1], _back[1], delta=0.0001)
self.assertAlmostEqual(_from[2], _back[2], delta=0.0001)
self.assertAlmostEqual(_from[3], _back[3], delta=0.0001)

def test_angstroms_s_conversion(self):
"""assert ev_to_s works"""
_from = np.linspace(1, 10, 10)
_to = angstroms_to_s(array=_from, offset_us=2.7, source_to_detector_m=16.)
_back = s_to_angstroms(array=_to, offset_us=2.7, source_to_detector_m=16.)
self.assertAlmostEquals(_from[0], _back[0], delta=0.0001)
self.assertAlmostEquals(_from[1], _back[1], delta=0.0001)
self.assertAlmostEquals(_from[2], _back[2], delta=0.0001)
self.assertAlmostEquals(_from[3], _back[3], delta=0.0001)
self.assertAlmostEqual(_from[0], _back[0], delta=0.0001)
self.assertAlmostEqual(_from[1], _back[1], delta=0.0001)
self.assertAlmostEqual(_from[2], _back[2], delta=0.0001)
self.assertAlmostEqual(_from[3], _back[3], delta=0.0001)

def test_ev_to_image_number_conversion(self):
"""assert ev_to_s works"""
Expand All @@ -430,7 +451,7 @@ def test_ev_to_image_number_conversion(self):
_to_expected_1 = 1411.49845352
_to_expected_2 = 1151.33676491
_to_expected_3 = 996.2495455
self.assertAlmostEquals(_to_expected_0, _to[0], delta=0.0001)
self.assertAlmostEquals(_to_expected_1, _to[1], delta=0.0001)
self.assertAlmostEquals(_to_expected_2, _to[2], delta=0.0001)
self.assertAlmostEquals(_to_expected_3, _to[3], delta=0.0001)
self.assertAlmostEqual(_to_expected_0, _to[0], delta=0.0001)
self.assertAlmostEqual(_to_expected_1, _to[1], delta=0.0001)
self.assertAlmostEqual(_to_expected_2, _to[2], delta=0.0001)
self.assertAlmostEqual(_to_expected_3, _to[3], delta=0.0001)

0 comments on commit 30588ba

Please sign in to comment.