Repository navigation
Fix filtering rounding error - #2189
Conversation
|
An example of how I found this error: I was plotting the distribution of spikes amplitude, and wanted to compare it to the distribution of voltage on the same channel. The bin 0 was twice bigger than what it should be, because of how Rounding before converting to an int fixes this issue, and more globally makes things neater. |
|
I take the point that you'd prefer this to behave like a normal round (I think that it is better too for my mental schema), but the standard for casting from float -> int (for example) is just to drop the decimal values. So you don't think that this might lead some users to be confused by the non-standard behavior of casting to an int, but seeing the value round? Just curious how you're thinking about this "round" occurring internal to the function without the user potentially knowing about it? |
|
Well if I have a value at 12, and I apply a slight filter which reduces the value to 11.98, I expect the value to be 12 after casting to an integer ... I don't think many users expect to receive 11? |
I agree with that. I think people would likely expect 12 in this example. But I guess the other solutions could be:
tiny edit: I'm not saying floats are more accurate in general. Just if moving from 12 -> 11.98 would be kept with a float dtype and lost with an int (since it would drop to 11). |
|
The thing is that most recording are with int16 today. And by default in SI when you filter, you get back an int16. |
|
Actually I completely agree for Gaussian. In that case you use the dtype from the traces and so the user has no option. If the recording is an int16 and it gets filter the user is stuck with int16. But in |
I think a lot of users don't specify the |
|
Hi. About a warning when do not use the decimal will be really painfull. For me this preserving the input dtype is a feature in spikeinterface.preprocessing since long time. For me it has to be documented well. |
I can try to do that if you think that's best! |
|
Okay fair enough. If it is occurring everywhere in the same manner and not ad hoc, then it makes sense.
I can double check that part of the docs (I already found a typo reading it just now so I'll do another pass soon). And yeah I think just making it clear in the docs that this is what is happening is super important for users. I will say that I did add the
Fair enough! If it is expected behavior for the library then people can't expect to be warned for the norm. |
This is not true: # Random floats between -1 and 1
x = np.random.uniform(-2, 2, 10)
print(x)
x.astype("int")
output:
[-0.01363288 0.42181579 1.25074111 0.19889013 1.11397242 -0.95708403
1.15950627 -1.3823785 1.43423315 -0.61537481]I actually don't know enough about filtering to have an opinion on this. My own -weakly held- take now is that there should NOT be an option to force the output in a specific format: I think that the tricks that we use don't achieve saving memory: And, the most subjective, tricks like the int scale in the Z-score hurt my mathematical sensibilities: Looking forward to learn here, |
By lower I meant "closer to 0" |
|
The correct claim is that casting behaves like floor for positive values and like a ceiling for negative values. There is not a conception of "lower" that I can map to that. |
| @@ -74,6 +74,8 @@ dtype (unless specified otherwise): | |||
|
|
|||
| Some scaling pre-processors, such as :code:`whiten()` or :code:`zscore()`, will force the output to :code:`float32`. | |||
There was a problem hiding this comment.
This is part of your problem @h-mayorquin? The forcing of the output? Just so I can stay on the same page.
There was a problem hiding this comment.
Yes, I don't know why we have that option but my problem most likely is ignorance. Maybe there are good scenarios that I jus't don't understand.
There was a problem hiding this comment.
I honestly don't know either, so I'm happy to learn too :)
|
Merci beaucoup. This was important. |
The
astype(dtype)always converts int to the lower integer, which in some cases causes real problems.This PR fixes that.