Skip to content
Snippets Groups Projects

Draft: Resolve "Use Numpy arrays instead of `ForEachObject` instances"

2 unresolved threads
2 files
+ 132
8
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 62
8
@@ -8,13 +8,10 @@ Authors:
"""
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']
SC = ['1', '2', '3']
def index2mosa(index):
@@ -55,10 +52,6 @@ MOSA32 = mosa2index('32')
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):
"""Return SC index associated with a given array index.
@@ -180,3 +173,64 @@ def mosa2sc(mosa):
if str(mosa) not in MOSAS:
raise ValueError(f"invalid MOSA index '{mosa}'")
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]
Loading