From bf2515c4c16b1c559e71cf4b66d99a3c75d94952 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 6 Apr 2021 12:10:35 -0400 Subject: [PATCH] removed `Grid.diff` in lieu of `__eq__` --- python/damask/_grid.py | 38 +++++++------------------------------ python/tests/test_Grid.py | 8 ++------ python/tests/test_Result.py | 6 +++--- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/python/damask/_grid.py b/python/damask/_grid.py index 5457643ee..308be5c80 100644 --- a/python/damask/_grid.py +++ b/python/damask/_grid.py @@ -69,9 +69,9 @@ class Grid: copy = __copy__ - def diff(self,other): + def __eq__(self,other): """ - Report property differences of self relative to other. + Test equality of other. Parameters ---------- @@ -79,34 +79,10 @@ class Grid: Grid to compare self against. """ - message = [] - if np.any(other.cells != self.cells): - message.append(util.deemph(f'cells x y z: {util.srepr(other.cells," x ")}')) - message.append(util.emph( f'cells x y z: {util.srepr( self.cells," x ")}')) - - if not np.allclose(other.size,self.size): - message.append(util.deemph(f'size x y z: {util.srepr(other.size," x ")}')) - message.append(util.emph( f'size x y z: {util.srepr( self.size," x ")}')) - - if not np.allclose(other.origin,self.origin): - message.append(util.deemph(f'origin x y z: {util.srepr(other.origin," ")}')) - message.append(util.emph( f'origin x y z: {util.srepr( self.origin," ")}')) - - if other.N_materials != self.N_materials: - message.append(util.deemph(f'# materials: {other.N_materials}')) - message.append(util.emph( f'# materials: { self.N_materials}')) - - if np.nanmin(other.material) != np.nanmin(self.material): - message.append(util.deemph(f'min material: {np.nanmin(other.material)}')) - message.append(util.emph( f'min material: {np.nanmin( self.material)}')) - - if np.nanmax(other.material) != np.nanmax(self.material): - message.append(util.deemph(f'max material: {np.nanmax(other.material)}')) - message.append(util.emph( f'max material: {np.nanmax( self.material)}')) - - print(util.srepr(message)) - - return True if message != [] or (other.material != self.material).any() else False + return (np.allclose(other.size,self.size) + and np.allclose(other.origin,self.origin) + and np.all(other.cells == self.cells) + and np.all(other.material == self.material)) @property @@ -276,7 +252,7 @@ class Grid: """ Load DREAM.3D (HDF5) file. - Data in DREAM.3D files can be stored per cell ('CellData') and/or + Data in DREAM.3D files can be stored per cell ('CellData') and/or per grain ('Grain Data'). Per default, cell-wise data is assumed. damask.ConfigMaterial.load_DREAM3D gives the corresponding material definition. diff --git a/python/tests/test_Grid.py b/python/tests/test_Grid.py index 6ccd100fc..9d82bde78 100644 --- a/python/tests/test_Grid.py +++ b/python/tests/test_Grid.py @@ -41,13 +41,9 @@ class TestGrid: def _patch_datetime_now(self, patch_datetime_now): print('patched datetime.datetime.now') - def test_diff_equal(self,default): - assert not default.diff(default) - - def test_diff_not_equal(self,default): - new = Grid(default.material[1:,1:,1:]+1,default.size*.9,np.ones(3)-default.origin,comments=['modified']) - assert default.diff(new) + def test_equal(self,default): + assert default == default def test_repr(self,default): print(default) diff --git a/python/tests/test_Result.py b/python/tests/test_Result.py index f9112e34f..63852efc5 100644 --- a/python/tests/test_Result.py +++ b/python/tests/test_Result.py @@ -326,7 +326,7 @@ class TestResult: for i in range(10): if os.path.isfile(tmp_path/fname): with open(fname) as f: - cur = hashlib.md5(f.get().encode()).hexdigest() + cur = hashlib.md5(f.read().encode()).hexdigest() if cur == last: break else: @@ -336,7 +336,7 @@ class TestResult: with open((ref_path/'save_VTK'/request.node.name).with_suffix('.md5'),'w') as f: f.write(cur) with open((ref_path/'save_VTK'/request.node.name).with_suffix('.md5')) as f: - assert cur == f.get() + assert cur == f.read() @pytest.mark.parametrize('mode',['point','cell']) def test_vtk_mode(self,tmp_path,single_phase,mode): @@ -352,7 +352,7 @@ class TestResult: single_phase.save_XDMF() if update: shutil.copy(tmp_path/fname,ref_path/fname) - assert sorted(open(tmp_path/fname).get()) == sorted(open(ref_path/fname).get()) # XML is not ordered + assert sorted(open(tmp_path/fname).read()) == sorted(open(ref_path/fname).read()) # XML is not ordered def test_XDMF_invalid(self,default): with pytest.raises(TypeError):