diff --git a/processing/post/addCauchy.py b/processing/post/addCauchy.py index ad29792a6..afc5a57be 100755 --- a/processing/post/addCauchy.py +++ b/processing/post/addCauchy.py @@ -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), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/addDeterminant.py b/processing/post/addDeterminant.py index 4c3f9d260..f2368559d 100755 --- a/processing/post/addDeterminant.py +++ b/processing/post/addDeterminant.py @@ -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)), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/addDeviator.py b/processing/post/addDeviator.py index 7807c7f5d..ca06034b3 100755 --- a/processing/post/addDeviator.py +++ b/processing/post/addDeviator.py @@ -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)), - scriptID+' '+' '.join(sys.argv[1:])) + 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)), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/addMises.py b/processing/post/addMises.py index 9bc560f2d..0c2a6db50 100755 --- a/processing/post/addMises.py +++ b/processing/post/addMises.py @@ -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))), - scriptID+' '+' '.join(sys.argv[1:])) + 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))), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/addPK2.py b/processing/post/addPK2.py index 45afdbcd1..185160d79 100755 --- a/processing/post/addPK2.py +++ b/processing/post/addPK2.py @@ -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), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/scaleData.py b/processing/post/scaleData.py index ad57f4b7e..60ffa41fa 100755 --- a/processing/post/scaleData.py +++ b/processing/post/scaleData.py @@ -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]), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/processing/post/shiftData.py b/processing/post/shiftData.py index 591e68c5d..37c72919e 100755 --- a/processing/post/shiftData.py +++ b/processing/post/shiftData.py @@ -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]), - scriptID+' '+' '.join(sys.argv[1:])) + 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) diff --git a/python/damask/table.py b/python/damask/table.py index 56507847e..171d15084 100644 --- a/python/damask/table.py +++ b/python/damask/table.py @@ -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: - self.comments.append('{}: {}'.format(label,info)) - else: - self.comments.append('{} {}: {}'.format(label,array.shape[1:],info)) + 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,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: - self.comments.append('{}: {}'.format(label,info)) - else: - self.comments.append('{} {}: {}'.format(label,array.shape[1:],info)) + 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,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. diff --git a/python/tests/test_Table.py b/python/tests/test_Table.py index c8965d1bd..2fedfdc59 100644 --- a/python/tests/test_Table.py +++ b/python/tests/test_Table.py @@ -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')