From 487733498662d112c3a5a5383a2f8187a51789a9 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Wed, 2 Dec 2020 19:25:54 -0500 Subject: [PATCH 1/2] added getitem and where functionality to Table --- python/damask/_table.py | 13 +++++++++++++ python/tests/test_Table.py | 17 ++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/python/damask/_table.py b/python/damask/_table.py index c05fa71a7..43a8a3ebf 100644 --- a/python/damask/_table.py +++ b/python/damask/_table.py @@ -33,6 +33,10 @@ class Table: """Brief overview.""" return '\n'.join(['# '+c for c in self.comments])+'\n'+self.data.__repr__() + def __getitem__(self,item): + """Return slice according to item.""" + return self.__class__(data=self.data[item],shapes=self.shapes,comments=self.comments) + def __len__(self): """Number of rows.""" return len(self.data) @@ -45,6 +49,15 @@ class Table: """Copy Table.""" return self.__copy__() + def where(self,expression): + """ + Return boolean array corresponding to interpolated expression being True. + + Table columns are addressed as #column# and will have appropriate shapes. + + """ + return eval(re.sub('#(.+?)#',r'self.get("\1")',expression)) + def _label_discrete(self): """Label data individually, e.g. v v v ==> 1_v 2_v 3_v.""" diff --git a/python/tests/test_Table.py b/python/tests/test_Table.py index 8c4465dc1..b1d7684fd 100644 --- a/python/tests/test_Table.py +++ b/python/tests/test_Table.py @@ -8,7 +8,7 @@ from damask import Table def default(): """Simple Table.""" x = np.ones((5,13),dtype=float) - return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains only ones']) + return Table(x,{'F':(3,3),'v':(3,),'s':(1,)},['test data','contains five rows of only ones']) @pytest.fixture def reference_dir(reference_dir_base): @@ -20,8 +20,9 @@ class TestTable: def test_repr(self,default): print(default) - def test_len(self): - len(Table(np.random.rand(7,3),{'X':3})) == 7 + @pytest.mark.parametrize('N',[10,40]) + def test_len(self,N): + len(Table(np.random.rand(N,3),{'X':3})) == N def test_get_scalar(self,default): d = default.get('s') @@ -39,6 +40,16 @@ class TestTable: d = default.get('5_F') assert np.allclose(d,1.0) and d.shape[1:] == (1,) + @pytest.mark.parametrize('N',[10,40]) + def test_getitem(self,N): + assert len(Table(np.random.rand(N,1),{'X':1})[:N//2]) == N//2 + + @pytest.mark.parametrize('N',[10,40]) + @pytest.mark.parametrize('limit',[0.1,0.6]) + def test_where(self,N,limit): + r = Table(np.random.rand(N,1),{'X':1}) + assert np.all(r[r.where(f'#X# > {limit}')].get('X') > limit) + @pytest.mark.parametrize('mode',['str','path']) def test_write_read(self,default,tmp_path,mode): default.save(tmp_path/'default.txt') From 36e4042f0b8ff257e08132a5ee608d890f371c68 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Fri, 11 Dec 2020 19:31:19 -0500 Subject: [PATCH 2/2] removed "where" method from Table class --- python/damask/_table.py | 9 --------- python/tests/test_Table.py | 6 ------ 2 files changed, 15 deletions(-) diff --git a/python/damask/_table.py b/python/damask/_table.py index 43a8a3ebf..993a21039 100644 --- a/python/damask/_table.py +++ b/python/damask/_table.py @@ -49,15 +49,6 @@ class Table: """Copy Table.""" return self.__copy__() - def where(self,expression): - """ - Return boolean array corresponding to interpolated expression being True. - - Table columns are addressed as #column# and will have appropriate shapes. - - """ - return eval(re.sub('#(.+?)#',r'self.get("\1")',expression)) - def _label_discrete(self): """Label data individually, e.g. v v v ==> 1_v 2_v 3_v.""" diff --git a/python/tests/test_Table.py b/python/tests/test_Table.py index b1d7684fd..4aac9a940 100644 --- a/python/tests/test_Table.py +++ b/python/tests/test_Table.py @@ -44,12 +44,6 @@ class TestTable: def test_getitem(self,N): assert len(Table(np.random.rand(N,1),{'X':1})[:N//2]) == N//2 - @pytest.mark.parametrize('N',[10,40]) - @pytest.mark.parametrize('limit',[0.1,0.6]) - def test_where(self,N,limit): - r = Table(np.random.rand(N,1),{'X':1}) - assert np.all(r[r.where(f'#X# > {limit}')].get('X') > limit) - @pytest.mark.parametrize('mode',['str','path']) def test_write_read(self,default,tmp_path,mode): default.save(tmp_path/'default.txt')