shorter but still descriptive names

This commit is contained in:
Martin Diehl 2019-12-05 06:10:27 +01:00
parent e7a67262f8
commit ee8e3386f4
9 changed files with 79 additions and 74 deletions

View File

@ -41,9 +41,9 @@ for name in filenames:
damask.util.report(scriptName,name)
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
table.add_array('Cauchy',
damask.mechanics.Cauchy(table.get_array(options.defgrad).reshape(-1,3,3),
table.get_array(options.stress).reshape(-1,3,3)).reshape(-1,9),
table.add('Cauchy',
damask.mechanics.Cauchy(table.get(options.defgrad).reshape(-1,3,3),
table.get(options.stress ).reshape(-1,3,3)).reshape(-1,9),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -38,8 +38,8 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for tensor in options.tensor:
table.add_array('det({})'.format(tensor),
np.linalg.det(table.get_array(tensor).reshape(-1,3,3)),
table.add('det({})'.format(tensor),
np.linalg.det(table.get(tensor).reshape(-1,3,3)),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -40,12 +40,12 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for tensor in options.tensor:
table.add_array('dev({})'.format(tensor),
damask.mechanics.deviatoric_part(table.get_array(tensor).reshape(-1,3,3)).reshape((-1,9)),
table.add('dev({})'.format(tensor),
damask.mechanics.deviatoric_part(table.get(tensor).reshape(-1,3,3)).reshape((-1,9)),
scriptID+' '+' '.join(sys.argv[1:]))
if options.spherical:
table.add_array('sph({})'.format(tensor),
damask.mechanics.spherical_part(table.get_array(tensor).reshape(-1,3,3)),
table.add('sph({})'.format(tensor),
damask.mechanics.spherical_part(table.get(tensor).reshape(-1,3,3)),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -45,12 +45,12 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for strain in options.strain:
table.add_array('Mises({})'.format(strain),
damask.mechanics.Mises_strain(damask.mechanics.symmetric(table.get_array(strain).reshape(-1,3,3))),
table.add('Mises({})'.format(strain),
damask.mechanics.Mises_strain(damask.mechanics.symmetric(table.get(strain).reshape(-1,3,3))),
scriptID+' '+' '.join(sys.argv[1:]))
for stress in options.stress:
table.add_array('Mises({})'.format(stress),
damask.mechanics.Mises_stress(damask.mechanics.symmetric(table.get_array(stress).reshape(-1,3,3))),
table.add('Mises({})'.format(stress),
damask.mechanics.Mises_stress(damask.mechanics.symmetric(table.get(stress).reshape(-1,3,3))),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -42,9 +42,9 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
table.add_array('S',
damask.mechanics.PK2(table.get_array(options.defgrad).reshape(-1,3,3),
table.get_array(options.stress).reshape(-1,3,3)).reshape(-1,9),
table.add('S',
damask.mechanics.PK2(table.get(options.defgrad).reshape(-1,3,3),
table.get(options.stress ).reshape(-1,3,3)).reshape(-1,9),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -44,7 +44,8 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for i,label in enumerate(options.label):
table.set_array(label,table.get_array(label)*float(options.factor[i]),
table.set(label,
table.get(label)*float(options.factor[i]),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -44,7 +44,8 @@ for name in filenames:
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
for i,label in enumerate(options.label):
table.set_array(label,table.get_array(label)+float(options.offset[i]),
table.set(label,
table.get(label)+float(options.offset[i]),
scriptID+' '+' '.join(sys.argv[1:]))
table.to_ASCII(sys.stdout if name is None else name)

View File

@ -87,14 +87,19 @@ class Table():
return Table(data,shapes,comments)
def get_array(self,label):
def labels(self):
"""Return the labels of all columns."""
return list(self.shapes.keys())
def get(self,label):
"""
Return data as array.
Get column data.
Parameters
----------
label : str
Label of the array.
Column label.
"""
if re.match(r'[0-9]*?_',label):
@ -103,62 +108,60 @@ class Table():
else:
return self.data[label].to_numpy().reshape((-1,)+self.shapes[label])
def set_array(self,label,array,info):
def set(self,label,data,info=None):
"""
Modify data in the spreadsheet.
Set column data.
Parameters
----------
label : str
Label for the new data.
array : np.ndarray
Column label.
data : np.ndarray
New data.
info : str
info : str, optional
Human-readable information about the new data.
"""
if np.prod(array.shape[1:],dtype=int) == 1:
if info is not None:
if np.prod(data.shape[1:],dtype=int) == 1:
self.comments.append('{}: {}'.format(label,info))
else:
self.comments.append('{} {}: {}'.format(label,array.shape[1:],info))
self.comments.append('{} {}: {}'.format(label,data.shape[1:],info))
if re.match(r'[0-9]*?_',label):
idx,key = label.split('_',1)
iloc = self.data.columns.get_loc(key).tolist().index(True) + int(idx) -1
self.data.iloc[:,iloc] = array
self.data.iloc[:,iloc] = data
else:
self.data[label] = array.reshape(self.data[label].shape)
self.data[label] = data.reshape(self.data[label].shape)
def labels(self):
"""Return the labels of all columns."""
return list(self.shapes.keys())
def add_array(self,label,array,info):
def add(self,label,data,info=None):
"""
Add data to the spreadsheet.
Add column data.
Parameters
----------
label : str
Label for the new data.
array : np.ndarray
New data.
info : str
Human-readable information about the new data.
Column label.
data : np.ndarray
Modified data.
info : str, optional
Human-readable information about the modified data.
"""
if np.prod(array.shape[1:],dtype=int) == 1:
if info is not None:
if np.prod(data.shape[1:],dtype=int) == 1:
self.comments.append('{}: {}'.format(label,info))
else:
self.comments.append('{} {}: {}'.format(label,array.shape[1:],info))
self.comments.append('{} {}: {}'.format(label,data.shape[1:],info))
self.shapes[label] = array.shape[1:] if len(array.shape) > 1 else (1,)
size = np.prod(array.shape[1:],dtype=int)
new_data = pd.DataFrame(data=array.reshape(-1,size),
self.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
size = np.prod(data.shape[1:],dtype=int)
new_data = pd.DataFrame(data=data.reshape(-1,size),
columns=[label for l in range(size)])
self.data = pd.concat([self.data,new_data],axis=1)
def to_ASCII(self,fname):
"""
Store as plain text file.

View File

@ -20,11 +20,11 @@ def reference_dir(reference_dir_base):
class TestTable:
def test_get_tensor(self,default):
d = default.get_array('F')
d = default.get('F')
assert np.allclose(d,1.0) and d.shape[1:] == (3,3)
def test_get_vector(self,default):
d = default.get_array('v')
d = default.get('v')
assert np.allclose(d,1.0) and d.shape[1:] == (3,)
def test_write_read_str(self,default,tmpdir):
@ -44,30 +44,30 @@ class TestTable:
with open(os.path.join(reference_dir,fname)) as f:
new = Table.from_ASCII(f)
def test_set_array(self,default):
default.set_array('F',np.zeros((5,3,3)),'set to zero')
d=default.get_array('F')
def test_set(self,default):
default.set('F',np.zeros((5,3,3)),'set to zero')
d=default.get('F')
assert np.allclose(d,0.0) and d.shape[1:] == (3,3)
def test_labels(self,default):
assert default.labels() == ['F','v','s']
def test_add_array(self,default):
def test_add(self,default):
d = np.random.random((5,9))
default.add_array('nine',d,'random data')
assert np.allclose(d,default.get_array('nine'))
default.add('nine',d,'random data')
assert np.allclose(d,default.get('nine'))
def test_invalid_initialization(self,default):
x = default.get_array('v')
x = default.get('v')
with pytest.raises(IndexError):
Table(x,{'F':(3,3)})
def test_invalid_set(self,default):
x = default.get_array('v')
x = default.get('v')
with pytest.raises(ValueError):
default.set_array('F',x,'does not work')
default.set('F',x,'does not work')
def test_invalid_get_array(self,default):
def test_invalid_get(self,default):
with pytest.raises(KeyError):
default.get_array('n')
default.get('n')