PySpectrometer2 icon indicating copy to clipboard operation
PySpectrometer2 copied to clipboard

Type issue in specFunctions.py (bookworm)

Open electrickery opened this issue 1 year ago • 1 comments

After fixing the np.int issue (np.int deprecated in bookworm #15), I got this error:

Traceback (most recent call last):
  File "~/PySpectrometer2/src/PySpectrometer2-USB-v1.0.py", line 263, in <module>
    intensity = savitzky_golay(intensity,17,savpoly)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "~/PySpectrometer2/src/specFunctions.py", line 143, in savitzky_golay
    lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
                              ~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Platform: MXLinux 23.1 (derived from Debian Bookworm)

$ cat /proc/version 
Linux version 6.1.0-16-amd64 ([email protected]) (gcc-12 (Debian 12.2.0-14) 12.2.0, GNU ld (GNU Binutils for Debian) 2.40) #1 SMP PREEMPT_DYNAMIC Debian 6.1.67-1 (2023-12-12)
 
$ python3 --version
Python 3.11.

python3-numpy version: 1:1.24.2-1

electrickery avatar Jan 09 '24 10:01 electrickery

Found that the newer python interpreter makes a list of some code where an int was expected. This can be fixed by this patch:

$ git diff src/specFunctions.py
diff --git a/src/specFunctions.py b/src/specFunctions.py
index 72c4cee..bf65557 100644
--- a/src/specFunctions.py
+++ b/src/specFunctions.py
@@ -124,8 +124,8 @@ def savitzky_golay(y, window_size, order, deriv=0, rate=1):
        import numpy as np
        from math import factorial
        try:
-               window_size = np.abs(np.int(window_size))
-               order = np.abs(np.int(order))
+               window_size = np.abs(np.int32(window_size))
+               order = np.abs(np.int32(order))
        except ValueError:
                raise ValueError("window_size and order have to be of type int")
        if window_size % 2 != 1 or window_size < 1:
@@ -139,8 +139,8 @@ def savitzky_golay(y, window_size, order, deriv=0, rate=1):
        m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
        # pad the signal at the extremes with
        # values taken from the signal itself
-       firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
-       lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
+       firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0])
+       lastvals = [y[-1] + np.abs(y[-half_window-1:-1][::-1][0] - y[-1])]
        y = np.concatenate((firstvals, y, lastvals))
        return np.convolve( m[::-1], y, mode='valid')

There are more issues with webcam configuration, but those could be remedied by using an external program, guvcview for linux. Guvcview can configure the webcam without claiming the video image. The command line below is start, but still results in some error.

guvcview --device /dev/video2 -z -x 800x480 -a none

electrickery avatar Jan 30 '24 07:01 electrickery