diff --git a/gdshelpers/helpers/small.py b/gdshelpers/helpers/small.py index e7f82a7..9196148 100644 --- a/gdshelpers/helpers/small.py +++ b/gdshelpers/helpers/small.py @@ -1,6 +1,7 @@ import string import numpy as np import numpy.linalg as linalg +import re def raith_eline_dosefactor_to_datatype(dose_factor): @@ -47,6 +48,18 @@ def id_to_alphanumeric(column, row): return int_to_alphabet(row) + str(int(column)) +def alphanumeric_to_id(text): + """ + Do the reverse of `id_to_alphanumeric`. + :return: (column, row) tuple. + """ + regex = "([A-Z]*)([0-9]*)" + m = re.match(regex, text, re.I) + letters, number = m.group(1), int(m.group(2)) + letter_num = sum((ord(letter) - ord('A') + 1) * 26 ** i for i, letter in enumerate(letters[::-1])) + return number, letter_num - 1 + + def normalize_phase(phase, zero_to_two_pi=False): """ Normalize a phase to be within +/- pi. diff --git a/gdshelpers/tests/test_helpers.py b/gdshelpers/tests/test_helpers.py index 83fe0a3..afbb081 100644 --- a/gdshelpers/tests/test_helpers.py +++ b/gdshelpers/tests/test_helpers.py @@ -3,6 +3,7 @@ import numpy.testing as npt from gdshelpers.helpers import int_to_alphabet, id_to_alphanumeric, find_line_intersection +from gdshelpers.helpers.small import alphanumeric_to_id class HelpersTestCase(unittest.TestCase): @@ -15,6 +16,12 @@ def test_int_to_alphabet(self): def test_id_to_alphanumeric(self): self.assertEqual(id_to_alphanumeric(4, 26 + 25), 'AZ4') + def test_alphanumeric_to_id(self): + self.assertEqual((4, 26 + 25), alphanumeric_to_id('AZ4')) + + for pair in [(0, 0), (0, 10), (0, 100), (100, 0), (100, 100)]: + self.assertEqual(pair, alphanumeric_to_id(id_to_alphanumeric(*pair))) + def test_find_line_intersection(self): test_intersection = find_line_intersection(np.array((2, 0)), np.pi / 2, np.array((0, 1)), 0) npt.assert_almost_equal(test_intersection[0], (2, 1))