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

Slicing of SolutionArray raises error #804

Closed
ischoegl opened this issue Feb 11, 2020 · 5 comments · Fixed by #816 or #837
Closed

Slicing of SolutionArray raises error #804

ischoegl opened this issue Feb 11, 2020 · 5 comments · Fixed by #816 or #837

Comments

@ischoegl
Copy link
Member

System information

  • Cantera version: 2.5.0a4
  • OS: Ubuntu
  • Python/MATLAB version: Python 3.6.8

Expected behavior

Slicing of SolutionArray objects should work as usual (as indexing is implemented).

Actual behavior

Slicing does not work.

To Reproduce

Steps to reproduce the behavior:

In [1]: import cantera as ct
   ...: gas = ct.Solution('gri30.yaml')
   ...: arr = ct.SolutionArray(gas, 10)

In [2]: arr[0]
Out[2]: <cantera.composite.SolutionArray at 0x7ffa61a364e0>

In [3]: arr[:]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-5-47ed00660ce3> in <module>()
----> 1 arr[:]

/usr/local/lib/python3.6/dist-packages/cantera/composite.py in __getitem__(self, index)
    534     def __getitem__(self, index):
    535         states = self._states[index]
--> 536         shape = states.shape[:-1]
    537         return SolutionArray(self._phase, shape, states)
    538 

AttributeError: 'list' object has no attribute 'shape'

Additional context

The error message clearly points at the issue.

ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 12, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 12, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 12, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 12, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 13, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 24, 2020
ischoegl added a commit to ischoegl/cantera that referenced this issue Feb 27, 2020
sin-ha added a commit to sin-ha/cantera that referenced this issue Mar 4, 2020
@sin-ha
Copy link
Contributor

sin-ha commented Mar 4, 2020

@ischoegl I am new and wish to contribute to Cantera learning about its codebase kindly review my PR

@ischoegl
Copy link
Member Author

ischoegl commented Mar 4, 2020 via email

ischoegl added a commit to ischoegl/cantera that referenced this issue Mar 23, 2020
speth pushed a commit to sin-ha/cantera that referenced this issue Mar 26, 2020
speth pushed a commit that referenced this issue Mar 26, 2020
@bryanwweber
Copy link
Member

Interestingly, a zero-length slice still raises an error even after #816 is merged:

In [8]: import cantera as ct
In [9]: gas = ct.Solution('gri30.yaml')
In [10]: arr = ct.SolutionArray(gas, 10)
In [11]: arr[slice(0)]
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-95d215c2766e> in <module>
----> 1 arr[slice(0)]

~/GitHub/cantera/build/python/cantera/composite.py in __getitem__(self, index)
    548         if(isinstance(states, list)):
    549             num_rows = len(states)
--> 550             return SolutionArray(self._phase, num_rows, states)
    551         else:
    552             shape = states.shape[:-1]

~/GitHub/cantera/build/python/cantera/composite.py in __init__(self, phase, shape, states, extra)
    512         else:
    513             self._indices = list(np.ndindex(self._shape))
--> 514             self._output_dummy = self._states[..., 0]
    515
    516         reserved = self.__dir__()

TypeError: list indices must be integers or slices, not tuple

whereas that returns a zero-length list/array for a plain Python list and a NumPy array. So I think there is one more edge case to handle here.

@bryanwweber bryanwweber reopened this Mar 26, 2020
@ischoegl
Copy link
Member Author

@bryanwweber / @sin-ha ... didn’t anticipate this one either: good catch.

@sin-ha
Copy link
Contributor

sin-ha commented Mar 27, 2020

the problem extends for this case too i.e. for all arr[x:x]

>>>arr[2:2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.7/site-packages/cantera/composite.py", line 540, in __getitem__
    return SolutionArray(self._phase, num_rows, states)
  File "/usr/local/lib/python3.7/site-packages/cantera/composite.py", line 514, in __init__
    self._output_dummy = self._states[..., 0]
TypeError: list indices must be integers or slices, not tuple

The problem exists in basic handling of shapes in the init class where the len(shapes) == 0, I will update the PR
thanks @bryanwweber @ischoegl

12Chao pushed a commit to 12Chao/cantera that referenced this issue Jun 2, 2020
@speth speth closed this as completed in #837 Jun 5, 2020
srikanthallu pushed a commit to srikanthallu/cantera that referenced this issue Sep 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants