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

ENH: implement pspace assignment broadcasting #774

Merged
merged 3 commits into from
Jan 4, 2017

Conversation

kohr-h
Copy link
Member

@kohr-h kohr-h commented Dec 19, 2016

In ProductSpaceElement:

  • Make x.parts a tuple
  • Re-implement __setitem__ with broadcasting
  • Adapt tests

Closes: #767

In ProductSpaceElement:
- Make x.parts a tuple
- Re-implement __setitem__ with broadcasting
- Adapt tests

Closes: odlgroup#767
@kohr-h
Copy link
Member Author

kohr-h commented Dec 19, 2016

Ended up implementing broadcasting when fixing the __setitem__ implementation of product space elements. Thought it was an open issue, but it wasn't apparently.

elif isinstance(indices, list):
indexed_parts = tuple(self.parts[i] for i in indices)
else:
raise TypeError('bad index type {}'.format(type(indices)))
Copy link
Member Author

Choose a reason for hiding this comment

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

Question here: This is more restrictive than __getitem__ which happily iterates over anything that allows it, including tuples. That's not Numpy-style behavior -- indexing with tuples means something very different there. (They also have indexing with arrays, but that's not applicable here)

Copy link
Member

Choose a reason for hiding this comment

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

I agree that we should do it like you do here.

Copy link
Member Author

Choose a reason for hiding this comment

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

Then I'll put similar code into __getitem__.

Copy link
Member

@adler-j adler-j left a comment

Choose a reason for hiding this comment

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

Overall looks good, only one comment on how this could be extended.

@@ -602,12 +602,12 @@ class ProductSpaceElement(LinearSpaceElement):
def __init__(self, space, parts):
"""Initialize a new instance."""
super().__init__(space)
self._parts = list(parts)
self.__parts = tuple(parts)
Copy link
Member

Choose a reason for hiding this comment

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

About time here

elif isinstance(indices, list):
indexed_parts = tuple(self.parts[i] for i in indices)
else:
raise TypeError('bad index type {}'.format(type(indices)))
Copy link
Member

Choose a reason for hiding this comment

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

I agree that we should do it like you do here.

else:
# `values` is iterable; it could still represent a single
# element of a power space.
if self.space.is_power_space and values in self.space[0]:
Copy link
Member

Choose a reason for hiding this comment

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

What about stuff like assigning a list? I think that works in rn, i.e.

>>> r2 = odl.rn(2)
>>> el = r2.element()
>>> el[:] = [1, 2]

don't we want something like that to work here?

Copy link
Member Author

Choose a reason for hiding this comment

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

We definitely do, that's why there are two tests for that :-). It's the code in the else case that takes care of it. I found this application to be the biggest advantage of determining the indexed parts beforehand and then having the rest as simple as possible.

@kohr-h
Copy link
Member Author

kohr-h commented Jan 3, 2017

Finally did this last (trivial) change, merge after CI.

Edit: Bug found, needs fix first.
Edit2: Was some weird travis thing looking for the branch on the odlgroup/odl repo, ignoring.
Edit3: Actual bug in doctest found, fixed.

@kohr-h kohr-h merged commit 7d454fb into odlgroup:master Jan 4, 2017
@kohr-h kohr-h deleted the issue-767__pspace_setitem branch January 4, 2017 00:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants