Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix SolutionArray extra slice #1204

Merged
merged 2 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions interfaces/cython/cantera/composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,14 +617,15 @@ def __init__(self, phase, shape=(0,), states=None, extra=None, meta=None):

def __getitem__(self, index):
states = self._states[index]
extra = OrderedDict({key: val[index] for key, val in self._extra.items()})
if(isinstance(states, list)):
num_rows = len(states)
if num_rows == 0:
states = None
ischoegl marked this conversation as resolved.
Show resolved Hide resolved
return SolutionArray(self._phase, num_rows, states)
return SolutionArray(self._phase, num_rows, states, extra=extra)
else:
shape = states.shape[:-1]
return SolutionArray(self._phase, shape, states)
return SolutionArray(self._phase, shape, states, extra=extra)

def __getattr__(self, name):
if name in self._extra:
Expand Down
8 changes: 8 additions & 0 deletions interfaces/cython/cantera/test/test_composite.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ def test_collect_data(self):
self.assertIn('X', collected)
self.assertEqual(collected['X'].shape, (0, self.gas.n_species))

def test_getitem(self):
states = ct.SolutionArray(self.gas, 10, extra={"index": range(10)})
for ix, state in enumerate(states):
assert state.index == ix

assert list(states[:2].index) == [0, 1]
assert list(states[100:102].index) == [] # outside of range
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like this should raise an IndexError, but insofar as this maintains current behavior, it seems fine to me to keep it like this and defer adding the IndexError.


def test_append_state(self):
gas = ct.Solution("h2o2.yaml")
gas.TPX = 300, ct.one_atm, 'H2:0.5, O2:0.4'
Expand Down