following python standard + polishing
This commit is contained in:
parent
b5a1295cb9
commit
ee0d80ce2a
|
@ -10,7 +10,7 @@ from . import util
|
||||||
from . import version
|
from . import version
|
||||||
|
|
||||||
|
|
||||||
class Geom():
|
class Geom:
|
||||||
"""Geometry definition for grid solvers."""
|
"""Geometry definition for grid solvers."""
|
||||||
|
|
||||||
def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]):
|
def __init__(self,microstructure,size,origin=[0.0,0.0,0.0],homogenization=1,comments=[]):
|
||||||
|
@ -218,6 +218,11 @@ class Geom():
|
||||||
return self.get_grid()
|
return self.get_grid()
|
||||||
|
|
||||||
|
|
||||||
|
@property
|
||||||
|
def N_microstructure(self):
|
||||||
|
return len(np.unique(self.microstructure))
|
||||||
|
|
||||||
|
|
||||||
def get_microstructure(self):
|
def get_microstructure(self):
|
||||||
"""Return the microstructure representation."""
|
"""Return the microstructure representation."""
|
||||||
return np.copy(self.microstructure)
|
return np.copy(self.microstructure)
|
||||||
|
@ -322,7 +327,7 @@ class Geom():
|
||||||
return Geom(microstructure.reshape(grid),size,origin,homogenization,comments)
|
return Geom(microstructure.reshape(grid),size,origin,homogenization,comments)
|
||||||
|
|
||||||
|
|
||||||
def to_file(self,fname,pack=None):
|
def to_file(self,fname,pack=False):
|
||||||
"""
|
"""
|
||||||
Writes a geom file.
|
Writes a geom file.
|
||||||
|
|
||||||
|
@ -337,7 +342,7 @@ class Geom():
|
||||||
header = self.get_header()
|
header = self.get_header()
|
||||||
grid = self.get_grid()
|
grid = self.get_grid()
|
||||||
|
|
||||||
if pack is None:
|
if not pack:
|
||||||
plain = grid.prod()/np.unique(self.microstructure).size < 250
|
plain = grid.prod()/np.unique(self.microstructure).size < 250
|
||||||
else:
|
else:
|
||||||
plain = not pack
|
plain = not pack
|
||||||
|
@ -426,7 +431,7 @@ class Geom():
|
||||||
rGrid.GetCellData().AddArray(ms)
|
rGrid.GetCellData().AddArray(ms)
|
||||||
|
|
||||||
|
|
||||||
if fname is None:
|
if not fname:
|
||||||
writer = vtk.vtkDataSetWriter()
|
writer = vtk.vtkDataSetWriter()
|
||||||
writer.SetHeader('damask.Geom '+version)
|
writer.SetHeader('damask.Geom '+version)
|
||||||
writer.WriteToOutputStringOn()
|
writer.WriteToOutputStringOn()
|
||||||
|
@ -447,7 +452,7 @@ class Geom():
|
||||||
writer.SetInputData(rGrid)
|
writer.SetInputData(rGrid)
|
||||||
writer.Write()
|
writer.Write()
|
||||||
|
|
||||||
if fname is None: return writer.GetOutputString()
|
if not fname: return writer.GetOutputString()
|
||||||
|
|
||||||
|
|
||||||
def show(self):
|
def show(self):
|
||||||
|
|
|
@ -25,10 +25,10 @@ class Table:
|
||||||
self.comments = [] if comments is None else [c for c in comments]
|
self.comments = [] if comments is None else [c for c in comments]
|
||||||
self.data = pd.DataFrame(data=data)
|
self.data = pd.DataFrame(data=data)
|
||||||
self.shapes = shapes
|
self.shapes = shapes
|
||||||
self.__label_condensed()
|
self._label_condensed()
|
||||||
|
|
||||||
|
|
||||||
def __label_flat(self):
|
def _label_flat(self):
|
||||||
"""Label data individually, e.g. v v v ==> 1_v 2_v 3_v."""
|
"""Label data individually, e.g. v v v ==> 1_v 2_v 3_v."""
|
||||||
labels = []
|
labels = []
|
||||||
for label,shape in self.shapes.items():
|
for label,shape in self.shapes.items():
|
||||||
|
@ -37,7 +37,7 @@ class Table:
|
||||||
self.data.columns = labels
|
self.data.columns = labels
|
||||||
|
|
||||||
|
|
||||||
def __label_condensed(self):
|
def _label_condensed(self):
|
||||||
"""Label data condensed, e.g. 1_v 2_v 3_v ==> v v v."""
|
"""Label data condensed, e.g. 1_v 2_v 3_v ==> v v v."""
|
||||||
labels = []
|
labels = []
|
||||||
for label,shape in self.shapes.items():
|
for label,shape in self.shapes.items():
|
||||||
|
@ -45,11 +45,10 @@ class Table:
|
||||||
self.data.columns = labels
|
self.data.columns = labels
|
||||||
|
|
||||||
|
|
||||||
def __add_comment(self,label,shape,info):
|
def _add_comment(self,label,shape,info):
|
||||||
if info is not None:
|
if info:
|
||||||
self.comments.append('{}{}: {}'.format(label,
|
c = '{}{}: {}'.format(label,' '+str(shape) if np.prod(shape,dtype=int) > 1 else '',info)
|
||||||
' '+str(shape) if np.prod(shape,dtype=int) > 1 else '',
|
self.comments.append(c)
|
||||||
info))
|
|
||||||
|
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
@ -57,7 +56,8 @@ class Table:
|
||||||
"""
|
"""
|
||||||
Create table from ASCII file.
|
Create table from ASCII file.
|
||||||
|
|
||||||
The first line needs to indicate the number of subsequent header lines as 'n header'.
|
The first line can indicate the number of subsequent header lines as 'n header',
|
||||||
|
alternatively first line is the header and comments are marked by '#' ('new style').
|
||||||
Vector data column labels are indicated by '1_v, 2_v, ..., n_v'.
|
Vector data column labels are indicated by '1_v, 2_v, ..., n_v'.
|
||||||
Tensor data column labels are indicated by '3x3:1_T, 3x3:2_T, ..., 3x3:9_T'.
|
Tensor data column labels are indicated by '3x3:1_T, 3x3:2_T, ..., 3x3:9_T'.
|
||||||
|
|
||||||
|
@ -188,7 +188,7 @@ class Table:
|
||||||
Human-readable information about the new data.
|
Human-readable information about the new data.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.__add_comment(label,data.shape[1:],info)
|
self._add_comment(label,data.shape[1:],info)
|
||||||
|
|
||||||
if re.match(r'[0-9]*?_',label):
|
if re.match(r'[0-9]*?_',label):
|
||||||
idx,key = label.split('_',1)
|
idx,key = label.split('_',1)
|
||||||
|
@ -212,7 +212,7 @@ class Table:
|
||||||
Human-readable information about the modified data.
|
Human-readable information about the modified data.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.__add_comment(label,data.shape[1:],info)
|
self._add_comment(label,data.shape[1:],info)
|
||||||
|
|
||||||
self.shapes[label] = data.shape[1:] if len(data.shape) > 1 else (1,)
|
self.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)
|
||||||
|
@ -251,12 +251,8 @@ class Table:
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.data.rename(columns={label_old:label_new},inplace=True)
|
self.data.rename(columns={label_old:label_new},inplace=True)
|
||||||
|
c = '{} => {}{}'.format(label_old,label_new,'' if not info else ': {}'.format(info))
|
||||||
self.comments.append('{} => {}{}'.format(label_old,
|
self.comments.append(c)
|
||||||
label_new,
|
|
||||||
'' if info is None else ': {}'.format(info),
|
|
||||||
))
|
|
||||||
|
|
||||||
self.shapes = {(label if label != label_old else label_new):self.shapes[label] for label in self.shapes}
|
self.shapes = {(label if label != label_old else label_new):self.shapes[label] for label in self.shapes}
|
||||||
|
|
||||||
|
|
||||||
|
@ -272,9 +268,9 @@ class Table:
|
||||||
Set sort order.
|
Set sort order.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.__label_flat()
|
self._label_flat()
|
||||||
self.data.sort_values(labels,axis=0,inplace=True,ascending=ascending)
|
self.data.sort_values(labels,axis=0,inplace=True,ascending=ascending)
|
||||||
self.__label_condensed()
|
self._label_condensed()
|
||||||
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
self.comments.append('sorted by [{}]'.format(', '.join(labels)))
|
||||||
|
|
||||||
|
|
||||||
|
@ -316,14 +312,16 @@ class Table:
|
||||||
self.shapes[key] = other.shapes[key]
|
self.shapes[key] = other.shapes[key]
|
||||||
|
|
||||||
|
|
||||||
def to_ASCII(self,fname,new=False):
|
def to_ASCII(self,fname,new_style=False):
|
||||||
"""
|
"""
|
||||||
Store as plain text file.
|
Store as plain text file.
|
||||||
|
|
||||||
Parameters
|
Parameters
|
||||||
----------
|
----------
|
||||||
fname : file, str, or pathlib.Path
|
fname : file, str, or pathlib.Path
|
||||||
Filename or file for reading.
|
Filename or file for writing.
|
||||||
|
new_style : Boolean, optional
|
||||||
|
Write table in new style, indicating header lines by comment sign ('#') only.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
seen = set()
|
seen = set()
|
||||||
|
@ -338,7 +336,7 @@ class Table:
|
||||||
labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \
|
labels += ['{}:{}_{}'.format('x'.join([str(d) for d in self.shapes[l]]),i+1,l) \
|
||||||
for i in range(np.prod(self.shapes[l]))]
|
for i in range(np.prod(self.shapes[l]))]
|
||||||
|
|
||||||
if new:
|
if new_style:
|
||||||
header = ['# {}'.format(comment) for comment in self.comments]
|
header = ['# {}'.format(comment) for comment in self.comments]
|
||||||
else:
|
else:
|
||||||
header = ['{} header'.format(len(self.comments)+1)] \
|
header = ['{} header'.format(len(self.comments)+1)] \
|
||||||
|
|
|
@ -49,7 +49,7 @@ class TestTable:
|
||||||
|
|
||||||
def test_write_read_new_style(self,default,tmpdir):
|
def test_write_read_new_style(self,default,tmpdir):
|
||||||
with open(tmpdir.join('new_style.txt'),'w') as f:
|
with open(tmpdir.join('new_style.txt'),'w') as f:
|
||||||
default.to_ASCII(f,new=True)
|
default.to_ASCII(f,new_style=True)
|
||||||
with open(tmpdir.join('new_style.txt')) as f:
|
with open(tmpdir.join('new_style.txt')) as f:
|
||||||
new = Table.from_ASCII(f)
|
new = Table.from_ASCII(f)
|
||||||
assert all(default.data==new.data) and default.shapes == new.shapes
|
assert all(default.data==new.data) and default.shapes == new.shapes
|
||||||
|
|
Loading…
Reference in New Issue