From 2e16016f4358a35871a29a6674f4445c7146d6ec Mon Sep 17 00:00:00 2001 From: Joschka Roffe Date: Wed, 19 Jun 2024 13:12:04 +0100 Subject: [PATCH 1/2] defualt numpy dtype change to 'np.uint8_t' from 'np.int_t'. Potential fix for #40. Alternative to lower bounding numpy version as suggested in #41. Needs testing --- src/ldpc/bp_decoder.pxd | 2 +- src/ldpc/bp_decoder.pyx | 2 +- src/ldpc/c_util.pxd | 8 ++++---- src/ldpc/c_util.pyx | 8 ++++---- src/ldpc/experimental/bp_decoder2.pxd | 2 +- src/ldpc/experimental/bp_decoder2.pyx | 2 +- src/ldpc/experimental/pymod2sparse.pxd | 2 +- src/ldpc/experimental/pymod2sparse.pyx | 2 +- src/ldpc/mod2sparse.pxd | 2 +- src/ldpc/mod2sparse.pyx | 2 +- src/ldpc/osd.pyx | 2 +- test.py | 15 +++++++++++++++ 12 files changed, 32 insertions(+), 17 deletions(-) create mode 100644 test.py diff --git a/src/ldpc/bp_decoder.pxd b/src/ldpc/bp_decoder.pxd index e676035..a3c5397 100755 --- a/src/ldpc/bp_decoder.pxd +++ b/src/ldpc/bp_decoder.pxd @@ -29,7 +29,7 @@ cdef class bp_decoder: cdef int MEM_ALLOCATED cdef int input_vector_type - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector) + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector) cdef char* bp_decode_cy(self) diff --git a/src/ldpc/bp_decoder.pyx b/src/ldpc/bp_decoder.pyx index cdb9553..dbee686 100755 --- a/src/ldpc/bp_decoder.pyx +++ b/src/ldpc/bp_decoder.pyx @@ -119,7 +119,7 @@ cdef class bp_decoder: for j in range(self.n): self.channel_probs[j]=error_rate self.error_rate=error_rate - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector): + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector): """ Runs the BP decoder for a given input_vector. diff --git a/src/ldpc/c_util.pxd b/src/ldpc/c_util.pxd index f9a12d0..ed59b73 100755 --- a/src/ldpc/c_util.pxd +++ b/src/ldpc/c_util.pxd @@ -4,12 +4,12 @@ import numpy as np cimport numpy as np cimport cython -# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array) +# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array) cdef char* numpy2char(np_array,char* char_array) cdef char* spmatrix2char(matrix,char* char_array) -cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double_array) -cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n) -cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n) +cdef double* numpy2double(np.ndarray[dtype = np.float_t, ndim=1] np_array,double* double_array) +cdef np.ndarray[dtype = np.uint8_t, ndim=1] char2numpy(char* char_array, int n) +cdef np.ndarray[dtype = np.float_t, ndim=1] double2numpy(double* char_array, int n) cdef extern from "binary_char.h": diff --git a/src/ldpc/c_util.pyx b/src/ldpc/c_util.pyx index 82f5cf9..e807859 100755 --- a/src/ldpc/c_util.pyx +++ b/src/ldpc/c_util.pyx @@ -3,7 +3,7 @@ import numpy as np from scipy.sparse import spmatrix -# cdef char* numpy2char(np.ndarray[np.int_t, ndim=1] np_array,char* char_array): +# cdef char* numpy2char(np.ndarray[np.uint8_t, ndim=1] np_array,char* char_array): # cdef int n = np_array.shape[0] # for i in range(n): char_array[i]=np_array[i] @@ -30,15 +30,15 @@ cdef double* numpy2double(np.ndarray[np.float_t, ndim=1] np_array,double* double for i in range(n): double_array[i]=np_array[i] return double_array -cdef np.ndarray[np.int_t, ndim=1] char2numpy(char* char_array, int n): +cdef np.ndarray[np.uint8_t, ndim=1] char2numpy(char* char_array, int n): - cdef np.ndarray[np.int_t, ndim=1] np_array=np.zeros(n).astype(int) + cdef np.ndarray[np.uint8_t, ndim=1] np_array=np.zeros(n).astype(np.uint8) for i in range(n):np_array[i]=char_array[i] return np_array cdef np.ndarray[np.float_t, ndim=1] double2numpy(double* char_array, int n): - cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n) + cdef np.ndarray[np.float_t, ndim=1] np_array=np.zeros(n, dtype=np.float64) for i in range(n):np_array[i]=char_array[i] return np_array diff --git a/src/ldpc/experimental/bp_decoder2.pxd b/src/ldpc/experimental/bp_decoder2.pxd index c34caad..c2a1b16 100755 --- a/src/ldpc/experimental/bp_decoder2.pxd +++ b/src/ldpc/experimental/bp_decoder2.pxd @@ -27,7 +27,7 @@ cdef class bp_decoder: cdef double ms_scaling_factor cdef int MEM_ALLOCATED - cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome) + cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome) cdef char* bp_decode_cy(self) diff --git a/src/ldpc/experimental/bp_decoder2.pyx b/src/ldpc/experimental/bp_decoder2.pyx index abe2e5c..b517392 100755 --- a/src/ldpc/experimental/bp_decoder2.pyx +++ b/src/ldpc/experimental/bp_decoder2.pyx @@ -83,7 +83,7 @@ cdef class bp_decoder: else: print("Decoder not called") - cpdef np.ndarray[np.int_t, ndim=1] bp_decode(self, np.ndarray[np.int_t, ndim=1] syndrome): + cpdef np.ndarray[np.uint8_t, ndim=1] bp_decode(self, np.ndarray[np.uint8_t, ndim=1] syndrome): self.synd=numpy2char(syndrome,self.synd) self.bp_decode_cy() return char2numpy(self.bp_decoding,self.n) diff --git a/src/ldpc/experimental/pymod2sparse.pxd b/src/ldpc/experimental/pymod2sparse.pxd index 293f7ba..cdd18bf 100644 --- a/src/ldpc/experimental/pymod2sparse.pxd +++ b/src/ldpc/experimental/pymod2sparse.pxd @@ -9,5 +9,5 @@ cdef class pymod2sparse(): cpdef iter_row(self,int row_index,int reverse_iterate=False) cpdef iter_col(self,int col_index,int reverse_iterate=False) - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector) + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector) diff --git a/src/ldpc/experimental/pymod2sparse.pyx b/src/ldpc/experimental/pymod2sparse.pyx index 25e1425..eb6e720 100644 --- a/src/ldpc/experimental/pymod2sparse.pyx +++ b/src/ldpc/experimental/pymod2sparse.pyx @@ -87,7 +87,7 @@ cdef class pymod2sparse(): return self - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector): + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector): if len(vector)!=self.n: raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!') diff --git a/src/ldpc/mod2sparse.pxd b/src/ldpc/mod2sparse.pxd index df0f239..b6272dd 100755 --- a/src/ldpc/mod2sparse.pxd +++ b/src/ldpc/mod2sparse.pxd @@ -77,6 +77,6 @@ cdef class pymod2sparse(): cpdef iter_row(self,int row_index,int reverse_iterate) cpdef iter_col(self,int col_index,int reverse_iterate) - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector) + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector) diff --git a/src/ldpc/mod2sparse.pyx b/src/ldpc/mod2sparse.pyx index 1eb7383..7691a14 100755 --- a/src/ldpc/mod2sparse.pyx +++ b/src/ldpc/mod2sparse.pyx @@ -173,7 +173,7 @@ cdef class pymod2sparse(): return self - cpdef np.ndarray[np.int_t, ndim=1] mul(self, np.ndarray[np.int_t, ndim=1] vector): + cpdef np.ndarray[np.uint8_t, ndim=1] mul(self, np.ndarray[np.uint8_t, ndim=1] vector): if len(vector)!=self.n: raise ValueError(f'Dimension mismatch. The supplied vector of length {len(vector)} cannot be multiplied by a matrix with dimesnions ({self.m},{self.n})!') diff --git a/src/ldpc/osd.pyx b/src/ldpc/osd.pyx index 271933e..1e588ab 100755 --- a/src/ldpc/osd.pyx +++ b/src/ldpc/osd.pyx @@ -154,7 +154,7 @@ cdef class bposd_decoder(bp_decoder): return self.osdw_decoding - cpdef np.ndarray[np.int_t, ndim=1] decode(self, input_vector): + cpdef np.ndarray[np.uint8_t, ndim=1] decode(self, input_vector): """ Runs the BP+OSD decoder for a given syndrome. diff --git a/test.py b/test.py new file mode 100644 index 0000000..0fb702a --- /dev/null +++ b/test.py @@ -0,0 +1,15 @@ +import numpy as np +from ldpc.codes import rep_code +from ldpc.bp_decoder import bp_decoder + +H = rep_code(3) +print(H) + +bpd = bp_decoder(H, error_rate = 0.1, max_iter=0, bp_method='ps', ms_scaling_factor=0.625) + +syndrome = np.array([1,0]) + +decoding = bpd.decode(syndrome) +print(decoding) +print(bpd.bp_decoding) +print(bpd.log_prob_ratios) From c4e55364999aa1006ef73914630e5fd0788bb38f Mon Sep 17 00:00:00 2001 From: Joschka Roffe Date: Mon, 2 Sep 2024 16:30:49 +0100 Subject: [PATCH 2/2] update version number --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index f610dbc..567d686 100755 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ from Cython.Build import cythonize import numpy -VERSION="0.1.51" +VERSION="0.1.53" f=open("src/ldpc/VERSION","w+") f.write(VERSION) f.close()