Skip to content
Snippets Groups Projects
Commit f2e8ec7d authored by Jean-Baptiste Bayle's avatar Jean-Baptiste Bayle
Browse files

Add methods to turn SC into MOSAs

parent 249c2f0e
No related branches found
No related tags found
No related merge requests found
This commit is part of merge request !46. Comments created here will be created in the context of that merge request.
...@@ -8,13 +8,10 @@ Authors: ...@@ -8,13 +8,10 @@ Authors:
""" """
import numpy as np import numpy as np
import logging
logger = logging.getLogger(__name__)
"""List of MOSA index as two-figure strings, in the order they are used in an array."""
MOSAS = ['12', '23', '31', '13', '32', '21'] MOSAS = ['12', '23', '31', '13', '32', '21']
SC = ['1', '2', '3']
def index2mosa(index): def index2mosa(index):
...@@ -55,10 +52,6 @@ MOSA32 = mosa2index('32') ...@@ -55,10 +52,6 @@ MOSA32 = mosa2index('32')
MOSA21 = mosa2index('21') MOSA21 = mosa2index('21')
"""List of SC indices as one-figure strings, in the order they are used in an array."""
SC = ['1', '2', '3']
def index2sc(index): def index2sc(index):
"""Return SC index associated with a given array index. """Return SC index associated with a given array index.
...@@ -180,3 +173,64 @@ def mosa2sc(mosa): ...@@ -180,3 +173,64 @@ def mosa2sc(mosa):
if str(mosa) not in MOSAS: if str(mosa) not in MOSAS:
raise ValueError(f"invalid MOSA index '{mosa}'") raise ValueError(f"invalid MOSA index '{mosa}'")
return str(mosa)[0] return str(mosa)[0]
def sc2leftdistant(sc):
"""Return index of left distant SC.
Args:
sc: SC index, as one-figure string or integer
"""
if str(sc) not in SC:
raise ValueError(f"invalid SC index '{sc}'")
return rotate(sc, nrot=1)
def sc2rightdistant(sc):
"""Return index of right distant SC.
Args:
sc: SC index, as one-figure string or integer
"""
if str(sc) not in SC:
raise ValueError(f"invalid SC index '{sc}'")
return rotate(sc, nrot=-1)
def sc2leftmosa(sc):
"""Return left MOSA index.
Args:
sc: SC index, as one-figure string or integer
"""
if str(sc) not in SC:
raise ValueError(f"invalid SC index '{sc}'")
return f'{sc}{sc2leftdistant(sc)}'
def sc2rightmosa(sc):
"""Return right MOSA index.
Args:
sc: SC index, as one-figure string or integer
"""
if str(sc) not in SC:
raise ValueError(f"invalid SC index '{sc}'")
return f'{sc}{sc2rightdistant(sc)}'
def sc2bothmosas(x):
"""Broadcast a SC array to a MOSA array.
We broadcast by assuming the same values for both MOSAs of each SC.
Args:
sc: SC array
Returns:
A MOSA array.
"""
if not isinstance(x, np.ndarray) or x.shape[0] != 3:
raise TypeError("invalid SC array")
sc_mosas = [sc2index(mosa2sc(mosa)) for mosa in MOSAS]
return x[sc_mosas]
...@@ -168,3 +168,73 @@ def test_mosa2sc(): ...@@ -168,3 +168,73 @@ def test_mosa2sc():
mosa2sc(1) mosa2sc(1)
with raises(ValueError): with raises(ValueError):
mosa2sc('1') mosa2sc('1')
def test_sc2leftdistant():
"""Test that we return the left distant SC index."""
assert sc2leftdistant('1') == '2'
assert sc2leftdistant('2') == '3'
assert sc2leftdistant('3') == '1'
assert sc2leftdistant(1) == '2'
assert sc2leftdistant(2) == '3'
assert sc2leftdistant(3) == '1'
with raises(ValueError):
sc2leftdistant(0)
with raises(ValueError):
sc2leftdistant(4)
with raises(ValueError):
sc2leftdistant('12')
def test_sc2rightdistant():
"""Test that we return the right distant SC index."""
assert sc2rightdistant('1') == '3'
assert sc2rightdistant('2') == '1'
assert sc2rightdistant('3') == '2'
assert sc2rightdistant(1) == '3'
assert sc2rightdistant(2) == '1'
assert sc2rightdistant(3) == '2'
with raises(ValueError):
sc2rightdistant(0)
with raises(ValueError):
sc2rightdistant(4)
with raises(ValueError):
sc2rightdistant('12')
def test_sc2leftmosa():
"""Test that we return left MOSA index."""
assert sc2leftmosa('1') == '12'
assert sc2leftmosa('2') == '23'
assert sc2leftmosa('3') == '31'
assert sc2leftmosa(1) == '12'
assert sc2leftmosa(2) == '23'
assert sc2leftmosa(3) == '31'
def test_sc2rightmosa():
"""Test that we return right MOSA index."""
assert sc2rightmosa('1') == '13'
assert sc2rightmosa('2') == '21'
assert sc2rightmosa('3') == '32'
assert sc2rightmosa(1) == '13'
assert sc2rightmosa(2) == '21'
assert sc2rightmosa(3) == '32'
def test_sc2bothmosas():
"""Test that we can broadcast a SC array to a MOSA array."""
sc_array = np.array([1, 2, 3])
mosa_array = np.array([1, 2, 3, 1, 3, 2])
assert np.all(sc2bothmosas(sc_array) == mosa_array)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment