the-siesta-group / edfio

Read and write EDF/EDF+ files.
Apache License 2.0
28 stars 4 forks source link

The ZeroDivisionError exception for _calculate_gain_and_offset needs an additional condition #50

Closed adammj closed 3 months ago

adammj commented 3 months ago

I love that the ZeroDivisionError for _calculate_gain_and_offset causes the raw digital data to be returned—this is exactly the behavior one would expect (the data is still there, who cares if the digital or physical min/max values were set by the user incorrectly?!).

The warning message needs an additional condition, because there are two different division by zero conditions that are possible—one for each line in _calculate_gain_and_offset.

  1. gain = (physical_max - physical_min) / (digital_max - digital_min) A division by zero happens here when digital max and min are the same. And is the situation that the warning is written to address.
  2. However, offset = physical_max / gain - digital_max can also produce a division by zero (when gain=0). This happens when the physical max and min are the same).

Adding a test inside the except block to check if self.digital_max == self.digital_min: to decide which warning to produce will solve this.

hofaflo commented 3 months ago

You're totally right, thanks for bringing this up! Would you be willing to open a PR fixing it?

adammj commented 3 months ago

Will do.