combine table.add and .set

This commit is contained in:
Philip Eisenlohr 2022-05-11 09:19:48 -04:00
parent 4c6b6fccec
commit 35aa8a9bc6
4 changed files with 24 additions and 41 deletions

View File

@ -363,14 +363,14 @@ class Table:
data: np.ndarray, data: np.ndarray,
info: str = None) -> 'Table': info: str = None) -> 'Table':
""" """
Set column data. Add new or replace existing column data.
Parameters Parameters
---------- ----------
label : str label : str
Column label. Column label.
data : numpy.ndarray data : numpy.ndarray
Replacement data. Column data.
info : str, optional info : str, optional
Human-readable information about the modified data. Human-readable information about the modified data.
@ -382,41 +382,23 @@ class Table:
""" """
dup = self.copy() dup = self.copy()
dup._add_comment(label, data.shape[1:], info) dup._add_comment(label, data.shape[1:], info)
if m := re.match(r'(.*)\[((\d+,)*(\d+))\]',label): if m := re.match(r'(.*)\[((\d+,)*(\d+))\]',label):
key = m.group(1) key = m.group(1)
else:
key = label
if key in dup.shapes:
if m:
idx = np.ravel_multi_index(tuple(map(int,m.group(2).split(","))), idx = np.ravel_multi_index(tuple(map(int,m.group(2).split(","))),
self.shapes[key]) self.shapes[key])
iloc = dup.data.columns.get_loc(key).tolist().index(True) + idx iloc = dup.data.columns.get_loc(key).tolist().index(True) + idx
dup.data.iloc[:,iloc] = data dup.data.iloc[:,iloc] = data
else: else:
dup.data[label] = data.reshape(dup.data[label].shape) dup.data[label] = data.reshape(dup.data[label].shape)
return dup
else:
def add(self,
label: str,
data: np.ndarray,
info: str = None) -> 'Table':
"""
Add column data.
Parameters
----------
label : str
Column label.
data : numpy.ndarray
New data.
info : str, optional
Human-readable information about the new data.
Returns
-------
updated : damask.Table
Updated table.
"""
dup = self.copy()
dup._add_comment(label,data.shape[1:],info)
dup.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,) dup.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
size = np.prod(data.shape[1:],dtype=int) size = np.prod(data.shape[1:],dtype=int)
@ -425,6 +407,7 @@ class Table:
) )
new.index = dup.data.index new.index = dup.data.index
dup.data = pd.concat([dup.data,new],axis=1) dup.data = pd.concat([dup.data,new],axis=1)
return dup return dup

View File

@ -441,7 +441,7 @@ class TestGrid:
z = np.ones(cells.prod()) z = np.ones(cells.prod())
z[cells[:2].prod()*int(cells[2]/2):] = 0 z[cells[:2].prod()*int(cells[2]/2):] = 0
t = Table({'coords':3,'z':1},np.column_stack((coords,z))) t = Table({'coords':3,'z':1},np.column_stack((coords,z)))
t = t.add('indicator',t.get('coords')[:,0]) t = t.set('indicator',t.get('coords')[:,0])
g = Grid.from_table(t,'coords',['indicator','z']) g = Grid.from_table(t,'coords',['indicator','z'])
assert g.N_materials == g.cells[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == cells[0]).all() assert g.N_materials == g.cells[0]*2 and (g.material[:,:,-1]-g.material[:,:,0] == cells[0]).all()

View File

@ -169,7 +169,7 @@ class TestOrientation:
def test_from_directions(self,kwargs): def test_from_directions(self,kwargs):
for a,b in np.random.random((10,2,3)): for a,b in np.random.random((10,2,3)):
c = np.cross(b,a) c = np.cross(b,a)
if np.all(np.isclose(c,0)): continue if np.allclose(c,0): continue
o = Orientation.from_directions(uvw=a,hkl=c,**kwargs) o = Orientation.from_directions(uvw=a,hkl=c,**kwargs)
x = o.to_pole(uvw=a) x = o.to_pole(uvw=a)
z = o.to_pole(hkl=c) z = o.to_pole(hkl=c)

View File

@ -51,7 +51,7 @@ class TestTable:
def test_add(self,default): def test_add(self,default):
d = np.random.random((5,9)) d = np.random.random((5,9))
assert np.allclose(d,default.add('nine',d,'random data').get('nine')) assert np.allclose(d,default.set('nine',d,'random data').get('nine'))
def test_isclose(self,default): def test_isclose(self,default):
assert default.isclose(default).all() assert default.isclose(default).all()
@ -200,6 +200,6 @@ class TestTable:
t = Table({'v':(2,)}, t = Table({'v':(2,)},
np.array([[0,1,],[2,1,]]), np.array([[0,1,],[2,1,]]),
['test data'])\ ['test data'])\
.add('s',np.array(['b','a']))\ .set('s',np.array(['b','a']))\
.sort_by('s') .sort_by('s')
assert np.all(t.get('v')[:,0] == np.array([2,0])) assert np.all(t.get('v')[:,0] == np.array([2,0]))