combine table.add and .set
This commit is contained in:
parent
4c6b6fccec
commit
35aa8a9bc6
|
@ -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,49 +382,32 @@ 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)
|
||||||
idx = np.ravel_multi_index(tuple(map(int,m.group(2).split(","))),
|
|
||||||
self.shapes[key])
|
|
||||||
iloc = dup.data.columns.get_loc(key).tolist().index(True) + idx
|
|
||||||
dup.data.iloc[:,iloc] = data
|
|
||||||
else:
|
else:
|
||||||
dup.data[label] = data.reshape(dup.data[label].shape)
|
key = label
|
||||||
return dup
|
|
||||||
|
|
||||||
|
if key in dup.shapes:
|
||||||
|
|
||||||
def add(self,
|
if m:
|
||||||
label: str,
|
idx = np.ravel_multi_index(tuple(map(int,m.group(2).split(","))),
|
||||||
data: np.ndarray,
|
self.shapes[key])
|
||||||
info: str = None) -> 'Table':
|
iloc = dup.data.columns.get_loc(key).tolist().index(True) + idx
|
||||||
"""
|
dup.data.iloc[:,iloc] = data
|
||||||
Add column data.
|
else:
|
||||||
|
dup.data[label] = data.reshape(dup.data[label].shape)
|
||||||
|
|
||||||
Parameters
|
else:
|
||||||
----------
|
|
||||||
label : str
|
|
||||||
Column label.
|
|
||||||
data : numpy.ndarray
|
|
||||||
New data.
|
|
||||||
info : str, optional
|
|
||||||
Human-readable information about the new data.
|
|
||||||
|
|
||||||
Returns
|
dup.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
|
||||||
-------
|
size = np.prod(data.shape[1:],dtype=int)
|
||||||
updated : damask.Table
|
new = pd.DataFrame(data=data.reshape(-1,size),
|
||||||
Updated table.
|
columns=[label]*size,
|
||||||
|
)
|
||||||
|
new.index = dup.data.index
|
||||||
|
dup.data = pd.concat([dup.data,new],axis=1)
|
||||||
|
|
||||||
"""
|
|
||||||
dup = self.copy()
|
|
||||||
dup._add_comment(label,data.shape[1:],info)
|
|
||||||
|
|
||||||
dup.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
|
|
||||||
size = np.prod(data.shape[1:],dtype=int)
|
|
||||||
new = pd.DataFrame(data=data.reshape(-1,size),
|
|
||||||
columns=[label]*size,
|
|
||||||
)
|
|
||||||
new.index = dup.data.index
|
|
||||||
dup.data = pd.concat([dup.data,new],axis=1)
|
|
||||||
return dup
|
return dup
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
||||||
|
|
|
@ -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)
|
||||||
|
|
|
@ -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]))
|
||||||
|
|
Loading…
Reference in New Issue