set_X and add_comments methods now default to out-of-place
This commit is contained in:
parent
49c92e7c24
commit
ca2f3f9493
|
@ -35,11 +35,11 @@ class Geom:
|
||||||
Comment lines.
|
Comment lines.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.set_microstructure(microstructure)
|
self.set_microstructure(microstructure,inplace=True)
|
||||||
self.set_size(size)
|
self.set_size(size,inplace=True)
|
||||||
self.set_origin(origin)
|
self.set_origin(origin,inplace=True)
|
||||||
self.set_homogenization(homogenization)
|
self.set_homogenization(homogenization,inplace=True)
|
||||||
self.set_comments(comments)
|
self.set_comments(comments,inplace=True)
|
||||||
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -85,17 +85,16 @@ class Geom:
|
||||||
raise ValueError('Auto-sizing conflicts with explicit size parameter.')
|
raise ValueError('Auto-sizing conflicts with explicit size parameter.')
|
||||||
|
|
||||||
grid_old = self.get_grid()
|
grid_old = self.get_grid()
|
||||||
dup = self.copy()
|
dup = self.set_microstructure(microstructure)\
|
||||||
dup.set_microstructure(microstructure)
|
.set_origin(origin)
|
||||||
dup.set_origin(origin)
|
|
||||||
|
|
||||||
if comments is not None:
|
if comments is not None:
|
||||||
dup.set_comments(comments)
|
dup.set_comments(comments,inplace=True)
|
||||||
|
|
||||||
if size is not None:
|
if size is not None:
|
||||||
dup.set_size(size)
|
dup.set_size(size,inplace=True)
|
||||||
elif autosize:
|
elif autosize:
|
||||||
dup.set_size(dup.get_grid()/grid_old*self.get_size())
|
dup.set_size(dup.get_grid()/grid_old*self.get_size(),inplace=True)
|
||||||
|
|
||||||
return dup
|
return dup
|
||||||
|
|
||||||
|
@ -134,7 +133,7 @@ class Geom:
|
||||||
return util.return_message(message)
|
return util.return_message(message)
|
||||||
|
|
||||||
|
|
||||||
def set_comments(self,comments):
|
def set_comments(self,comments,inplace=False):
|
||||||
"""
|
"""
|
||||||
Replace all existing comments.
|
Replace all existing comments.
|
||||||
|
|
||||||
|
@ -144,11 +143,13 @@ class Geom:
|
||||||
All comments.
|
All comments.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.comments = []
|
target = self if inplace else self.copy()
|
||||||
self.add_comments(comments)
|
target.comments = []
|
||||||
|
target.add_comments(comments,inplace=True)
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
def add_comments(self,comments):
|
def add_comments(self,comments,inplace=False):
|
||||||
"""
|
"""
|
||||||
Append comments to existing comments.
|
Append comments to existing comments.
|
||||||
|
|
||||||
|
@ -158,10 +159,12 @@ class Geom:
|
||||||
New comments.
|
New comments.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
self.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)]
|
target = self if inplace else self.copy()
|
||||||
|
target.comments += [str(c) for c in comments] if isinstance(comments,list) else [str(comments)]
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
def set_microstructure(self,microstructure):
|
def set_microstructure(self,microstructure,inplace=False):
|
||||||
"""
|
"""
|
||||||
Replace the existing microstructure representation.
|
Replace the existing microstructure representation.
|
||||||
|
|
||||||
|
@ -175,24 +178,26 @@ class Geom:
|
||||||
Microstructure indices.
|
Microstructure indices.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
target = self if inplace else self.copy()
|
||||||
if microstructure is not None:
|
if microstructure is not None:
|
||||||
if isinstance(microstructure,np.ma.core.MaskedArray):
|
if isinstance(microstructure,np.ma.core.MaskedArray):
|
||||||
self.microstructure = np.where(microstructure.mask,
|
target.microstructure = np.where(microstructure.mask,
|
||||||
self.microstructure,microstructure.data)
|
target.microstructure,microstructure.data)
|
||||||
else:
|
else:
|
||||||
self.microstructure = np.copy(microstructure)
|
target.microstructure = np.copy(microstructure)
|
||||||
|
|
||||||
if self.microstructure.dtype in np.sctypes['float'] and \
|
if target.microstructure.dtype in np.sctypes['float'] and \
|
||||||
np.all(self.microstructure == self.microstructure.astype(int).astype(float)):
|
np.all(target.microstructure == target.microstructure.astype(int).astype(float)):
|
||||||
self.microstructure = self.microstructure.astype(int)
|
target.microstructure = target.microstructure.astype(int)
|
||||||
|
|
||||||
if len(self.microstructure.shape) != 3:
|
if len(target.microstructure.shape) != 3:
|
||||||
raise ValueError(f'Invalid microstructure shape {microstructure.shape}')
|
raise ValueError(f'Invalid microstructure shape {microstructure.shape}')
|
||||||
elif self.microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']:
|
elif target.microstructure.dtype not in np.sctypes['float'] + np.sctypes['int']:
|
||||||
raise TypeError(f'Invalid microstructure data type {microstructure.dtype}')
|
raise TypeError(f'Invalid microstructure data type {microstructure.dtype}')
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
def set_size(self,size):
|
def set_size(self,size,inplace=False):
|
||||||
"""
|
"""
|
||||||
Replace the existing size information.
|
Replace the existing size information.
|
||||||
|
|
||||||
|
@ -202,14 +207,16 @@ class Geom:
|
||||||
Physical size of the microstructure in meter.
|
Physical size of the microstructure in meter.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
target = self if inplace else self.copy()
|
||||||
if size is not None:
|
if size is not None:
|
||||||
if len(size) != 3 or any(np.array(size) <= 0):
|
if len(size) != 3 or any(np.array(size) <= 0):
|
||||||
raise ValueError(f'Invalid size {size}')
|
raise ValueError(f'Invalid size {size}')
|
||||||
else:
|
else:
|
||||||
self.size = np.array(size)
|
target.size = np.array(size)
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
def set_origin(self,origin):
|
def set_origin(self,origin,inplace=False):
|
||||||
"""
|
"""
|
||||||
Replace the existing origin information.
|
Replace the existing origin information.
|
||||||
|
|
||||||
|
@ -219,14 +226,16 @@ class Geom:
|
||||||
Physical origin of the microstructure in meter.
|
Physical origin of the microstructure in meter.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
target = self if inplace else self.copy()
|
||||||
if origin is not None:
|
if origin is not None:
|
||||||
if len(origin) != 3:
|
if len(origin) != 3:
|
||||||
raise ValueError(f'Invalid origin {origin}')
|
raise ValueError(f'Invalid origin {origin}')
|
||||||
else:
|
else:
|
||||||
self.origin = np.array(origin)
|
target.origin = np.array(origin)
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
def set_homogenization(self,homogenization):
|
def set_homogenization(self,homogenization,inplace=False):
|
||||||
"""
|
"""
|
||||||
Replace the existing homogenization index.
|
Replace the existing homogenization index.
|
||||||
|
|
||||||
|
@ -236,11 +245,13 @@ class Geom:
|
||||||
Homogenization index.
|
Homogenization index.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
target = self if inplace else self.copy()
|
||||||
if homogenization is not None:
|
if homogenization is not None:
|
||||||
if not isinstance(homogenization,int) or homogenization < 1:
|
if not isinstance(homogenization,int) or homogenization < 1:
|
||||||
raise TypeError(f'Invalid homogenization {homogenization}.')
|
raise TypeError(f'Invalid homogenization {homogenization}.')
|
||||||
else:
|
else:
|
||||||
self.homogenization = homogenization
|
target.homogenization = homogenization
|
||||||
|
if not inplace: return target
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
|
|
|
@ -54,11 +54,35 @@ class TestGeom:
|
||||||
assert str(default.diff(new)) != ''
|
assert str(default.diff(new)) != ''
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_inplace_outofplace_homogenization(self,default):
|
||||||
|
default.set_homogenization(123,inplace=True)
|
||||||
|
outofplace = default.set_homogenization(321,inplace=False)
|
||||||
|
assert default.get_homogenization() == 123 and outofplace.get_homogenization() == 321
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_inplace_outofplace_microstructure(self,default):
|
||||||
|
default.set_microstructure(np.arange(72).reshape((2,4,9)),inplace=True)
|
||||||
|
outofplace = default.set_microstructure(np.arange(72).reshape((8,3,3)),inplace=False)
|
||||||
|
assert np.array_equal(default.grid,[2,4,9]) and np.array_equal(outofplace.grid,[8,3,3])
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_inplace_outofplace_size(self,default):
|
||||||
|
default.set_size(np.array([1,2,3]),inplace=True)
|
||||||
|
outofplace = default.set_size(np.array([3,2,1]),inplace=False)
|
||||||
|
assert np.array_equal(default.get_size(),[1,2,3]) and np.array_equal(outofplace.get_size(),[3,2,1])
|
||||||
|
|
||||||
|
|
||||||
|
def test_set_inplace_outofplace_comments(self,default):
|
||||||
|
default.set_comments(['a','and','b'],inplace=True)
|
||||||
|
outofplace = default.set_comments(['b','or','a'],inplace=False)
|
||||||
|
assert default.get_comments() == ['a','and','b'] and outofplace.get_comments() == ['b','or','a']
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('masked',[True,False])
|
@pytest.mark.parametrize('masked',[True,False])
|
||||||
def test_set_microstructure(self,default,masked):
|
def test_set_microstructure(self,default,masked):
|
||||||
old = default.get_microstructure()
|
old = default.get_microstructure()
|
||||||
new = np.random.randint(200,size=default.grid)
|
new = np.random.randint(200,size=default.grid)
|
||||||
default.set_microstructure(np.ma.MaskedArray(new,np.full_like(new,masked)))
|
default.set_microstructure(np.ma.MaskedArray(new,np.full_like(new,masked)),inplace=True)
|
||||||
assert np.all(default.microstructure==(old if masked else new))
|
assert np.all(default.microstructure==(old if masked else new))
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue