Skip to content

Fix filtering rounding error - #2189

Merged
samuelgarcia merged 4 commits into
SpikeInterface:mainfrom
DradeAW:fix_filtering_rounding
Nov 17, 2023
Merged

samuelgarcia merged 4 commits into
SpikeInterface:mainfrom
DradeAW:fix_filtering_rounding

Conversation

@DradeAW

@DradeAW DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor

The astype(dtype) always converts int to the lower integer, which in some cases causes real problems.

This PR fixes that.

@alejoe91 alejoe91 added the preprocessing Related to preprocessing module label Nov 10, 2023
@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

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 astype(np.int16) works.
The bin x includes values between [x ; x+1). Except for the bin 0 which includes values between (-1; 1).

Rounding before converting to an int fixes this issue, and more globally makes things neater.

@zm711

zm711 commented Nov 10, 2023

Copy link
Copy Markdown
Member

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?

@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

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?

@zm711

zm711 commented Nov 10, 2023 •

Copy link
Copy Markdown
Member

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:

  1. what you're doing just round behind the scenes assuming the user wants 12
  2. warn the user that you are doing the rounding because they want an int16 (ie add a warning saying rounding occurring due to the casting)
  3. warn that casting to something like an int can lead to some inaccuracies use a float dtype to retain accuracy (then don't round)

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).

@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

The thing is that most recording are with int16 today. And by default in SI when you filter, you get back an int16.
And as I stated above, there is a problem with 0.

@zm711

zm711 commented Nov 10, 2023

Copy link
Copy Markdown
Member

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 filter you are required to specify the dtype, so the user could choose to return the data with a dtype that maintains the filtered values. So my question is more just for filter where you could warn that user to return in float if they wanted to keep the decimal.

@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

But in filter you are required to specify the dtype

https://github.com/SpikeInterface/spikeinterface/blob/main/src/spikeinterface/preprocessing/filter.py#L70

I think a lot of users don't specify the dtype ^^
But let's see what @alejoe91 @samuelgarcia and @h-mayorquin think!

@samuelgarcia

Copy link
Copy Markdown
Member

Hi.
thank you for this fix. It was a need since long time. I also in some cases enormous bin at zeros in distrib.
I think this is not only filtering but all preprocessing that preserve the input dtype.
Maybe we could make a larger check on this.

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.
We already have this in the doc : https://spikeinterface.readthedocs.io/en/latest/modules/preprocessing.html#impact-on-recording-dtype
For sure, Zach, will find a more adavnced formula for this

@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

I think this is not only filtering but all preprocessing that preserve the input dtype.
Maybe we could make a larger check on this.

I can try to do that if you think that's best!

@zm711

zm711 commented Nov 10, 2023

Copy link
Copy Markdown
Member

Okay fair enough. If it is occurring everywhere in the same manner and not ad hoc, then it makes sense.

For me it has to be documented well.
We already have this in the doc : https://spikeinterface.readthedocs.io/en/latest/modules/preprocessing.html#impact-on-recording-dtype
For sure, Zach, will find a more adavnced formula for this

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 np.rint in Neo for selecting frames for analog signals, so I'm definitely not opposed to this :) just curious about what is given as the responsibility/freedom of the user.

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.

Fair enough! If it is expected behavior for the library then people can't expect to be warned for the norm.

@h-mayorquin

Copy link
Copy Markdown
Contributor

The astype(dtype) always converts int to the lower integer, which in some cases causes real problems.

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:

#914 (comment)

I think that the tricks that we use don't achieve saving memory:
#2029
They are also confusing as as some point we built a test that was statistically wrong:
#1972

And, the most subjective, tricks like the int scale in the Z-score hurt my mathematical sensibilities:
https://github.com/h-mayorquin/spikeinterface/blob/1ca0655e371a8dbc34c49ffde251b3628c03db8b/src/spikeinterface/preprocessing/normalize_scale.py#L239-L242

Looking forward to learn here,
What is able to control the output meaning to achieve?

@DradeAW

DradeAW commented Nov 10, 2023

Copy link
Copy Markdown
Contributor Author

This is not true:

>>> import numpy as np
>>> 
>>> np.random.uniform(-1, 1, 20).astype(np.int32)
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
      dtype=int32)

By lower I meant "closer to 0"

@h-mayorquin

Copy link
Copy Markdown
Contributor

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`.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is part of your problem @h-mayorquin? The forcing of the output? Just so I can stay on the same page.

@h-mayorquin h-mayorquin Nov 10, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I honestly don't know either, so I'm happy to learn too :)

@samuelgarcia

Copy link
Copy Markdown
Member

Merci beaucoup. This was important.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

preprocessing Related to preprocessing module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants