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

Added ShapeParam to store shapes #1045

Merged
merged 1 commit into from
Jul 27, 2016
Merged

Added ShapeParam to store shapes #1045

merged 1 commit into from
Jul 27, 2016

Conversation

hunse
Copy link
Collaborator

@hunse hunse commented Apr 28, 2016

A variation of TupleParam that is specialized to storing shapes. Allows checking for the length of the shape, that all elements are ints (or castable to ints), and ensuring all values are greater than a given value (often 0 or 1).

@Seanny123 Seanny123 changed the title WIP: Added ShapeParam to store shapes Added ShapeParam to store shapes Jun 7, 2016
@Seanny123
Copy link
Contributor

LGTM

@hunse hunse mentioned this pull request Jun 7, 2016
12 tasks
@tbekolay tbekolay self-assigned this Jun 7, 2016
@tbekolay tbekolay added this to the 2.2.0 release milestone Jun 7, 2016
@hunse
Copy link
Collaborator Author

hunse commented Jun 28, 2016

To see this in action, see https://github.com/nengo/nengo_extras/blob/master/nengo_extras/convnet.py. It's been working well there.

@jgosmann jgosmann self-assigned this Jul 20, 2016

def __set__(self, instance, value):
try:
value = tuple(int(v) for v in value)
Copy link
Collaborator

Choose a reason for hiding this comment

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

My NumPy says about non-integer number in shapes "DeprecationWarning: using a non-integer number instead of an integer will result in an error in the future". I would say, in anticipation of this, we should not allow non-integers at all.

@jgosmann
Copy link
Collaborator

Add commits regarding my comment and to add a changelog entry.

try:
value = tuple(int(v) for v in value)
except TypeError:
if not all(is_integer(v) for v in value):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

So the reason I didn't do this, is that I often do something like np.floor(a / b) to get some of my shape indices, and it's just convenient to not have to explicitly cast to an int. That said, maybe it's better to have to be explicit. But would it be worth checking if a floating point number is actually exactly an integer, and in that case just doing the cast ourselves?

Copy link
Collaborator

Choose a reason for hiding this comment

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

You wouldn't have that convenience in the future anyways: #1045 (comment)

I would be fine with doing the cast ourselves, but I am not sure how you can reliably detect whether a floating point number is an integer (considering rounding errors).

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You wouldn't have that convenience in the future anyways

I wouldn't necessarily have that convenience in Numpy (though it might be quite a while before they actually remove that), but I would still have it here.

So I think floor and ceil remove any rounding errors for reasonably sized numbers, but to really make a robust system we'd need to check tolerances or something, and that's all getting too complex. So I'm fine having to be explicit. That way, there's no magic going on, making it easier for users to debug.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I wouldn't necessarily have that convenience in Numpy

But wouldn't the shape parameter usually be used to create a Numpy array?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Perhaps, but not necessarily. Anyway, I'm fine with the explicit approach.

@tbekolay
Copy link
Member

Was merging this and noticed that with @jgosmann's changes, there's no longer a need for a __set__ and it should all be done in validate. Made 4b22c87 to change that; will fix that up and merge tomorrow morning if there are no objections.

@hunse
Copy link
Collaborator Author

hunse commented Jul 27, 2016

So one thing that got lost in @jgosmann's changes is that the input is no longer cast to a tuple. I had set this up so e.g. an iterable could be passed in and it would still work fine. So I think we need to override the __set__ for that reason. I'll fix that up.

@tbekolay
Copy link
Member

input is no longer cast to a tuple

I think we do this in TupleParam so it should do this anyhow? https://github.com/nengo/nengo/blob/master/nengo/params.py#L279

@hunse
Copy link
Collaborator Author

hunse commented Jul 27, 2016

Oh, right you are Ken. Ok, should be good to go!

@tbekolay
Copy link
Member

Cool, I'll go ahead now then! 🌈

Currently not used in Nengo, but will be used immediately in
`nengo_extras`.
@tbekolay tbekolay merged commit 978ff8f into master Jul 27, 2016
@tbekolay tbekolay deleted the shape-param branch July 27, 2016 19:55
@tbekolay tbekolay removed their assignment Oct 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants