veeresht / CommPy

Digital Communication with Python
http://veeresht.github.com/CommPy
BSD 3-Clause "New" or "Revised" License
538 stars 176 forks source link

Question about get_ldpc_code_params #116

Closed Jubo-Xu closed 1 year ago

Jubo-Xu commented 1 year ago

Sorry, when I use get_ldpc_code_params(), there's always an error of ValueError: invalid literal for int() with base 10: ..., even if I use the 96.33.964.txt in the test example. May I ask how could I fix this error?

BastienTr commented 1 year ago

Could you provide a code snippet that I can test on my computer?

Jubo-Xu commented 1 year ago

Sorry, this function doesn't work for me even for the simplest code: import torch import numpy as np import commpy.channelcoding.ldpc as ldpc import scipy.sparse as sp import scipy.sparse.linalg as splg

ldpc.get_ldpc_code_params('Headers/gallager_96_33964.txt')**

gallager_96_33_964.txt is same as the 96.33.964.txt provided, I just put it under the directory of my project. If I simply use ldpc.get_ldpc_code_params, the error is 'IndexError: index 95 is out of bounds for axis 0 with size 95', but if I define a LDPC class for it: class LDPC: def init(self, header_fn, decode_iters, device): self.ldpc_design = self.load_code_from_alist(header_fn) self.G = torch.from_numpy(self.ldpc_design['generator_matrix'].A).to(device).transpose(1, 0) self.H = torch.from_numpy(self.ldpc_design['parity_check_matrix'].A).to(device) self.k = self.G.shape[0] self.n = self.H.shape[1] self.decode_algorithm = 'SPA' # TODO MSA not yet impl self.decode_iters = decode_iters self.device = device

 def load_code_from_alist(self, header_fn):
        params = get_ldpc_code_params(header_fn, compute_matrix=True)
        params['decode_algorithm'] = 'SPA'
        return params

 def zero_pad(self, x, modulo):
        B, _, L = x.size()
        if torch.numel(x[0]) % modulo != 0:
               n_pad = (moduloL) - (torch.numel(x[0]) % (moduloL))
               zero_pad = torch.zeros(B, n_pad, device=x.device).view(B, -1, L)
               padded_message = torch.cat((x, zero_pad), dim=1)
        else:
               padded_message = x
        return padded_message

 def encode(self, message_bits):
        N = 1
        B, N, L = message_bits.size()
        padded_message = self.zero_pad(message_bits, self.k)
        padded_message = padded_message.view(B, -1, self.k)
        parity = torch.matmul(padded_message, self.G) % 2
        codeword = torch.cat((padded_message, parity), dim=-1)
        return codeword

 def decode(self, symbol_llr):
        batch_llr = torch.chunk(symbol_llr, chunks=1, dim=0)
        out_llr = []
        for b_llr in batch_llr:
               block_llr = torch.chunk(b_llr, chunks=2, dim=1)
               d_llr = []
               for llr_i in block_llr:
                          decoded_bits, llr, _ = ldpc_bp_decode(llr_i, self.ldpc_design, self.H,
                          self.decode_algorithm, self.decode_iters)
                          d_llr.append(llr)
               d_llr = torch.cat(d_llr, dim=1)
               out_llr.append(d_llr)
         llr = torch.cat(out_llr, dim=0)
         return decoded_bits, llr

and I use 'code = LDPC(args.header_fn, args.decoding_iterations, args.device)', where the args.header_fn is the file of BCH_7_4_1_strip.alist, which is 7 3 3 4 1 1 2 2 3 2 1 4 4 4 1 0 0 2 0 0 1 3 0 1 2 0 1 2 3 2 3 0 3 0 0 1 3 4 5 2 4 5 6 3 5 6 7 then the error will be 'ValueError: invalid literal for int() with base 10: '1 0 0\n''. In both cases, the error is from np.array([int(x)-1 for x in ldpc_design_file.readline().split('\t')]) of get_ldpc_code_params().

BastienTr commented 1 year ago

You're encountering two different issues.

For the first one, being:

import numpy as np
import commpy.channelcoding.ldpc as ldpc
import scipy.sparse as sp
import scipy.sparse.linalg as splg

ldpc.get_ldpc_code_params('Headers/gallager_96_33_964.txt')

everything work as expected. I used both my main interpreter with a lot of packages and a fresh install with pip install scikit-commpy.

Here are the packages installed on the fresh install

cycler==0.11.0
fonttools==4.37.2
kiwisolver==1.4.4
matplotlib==3.5.3
mpmath==1.2.1
numpy==1.21.6
packaging==21.3
Pillow==9.2.0
pyparsing==3.0.9
python-dateutil==2.8.2
scikit-commpy==0.7.0
scipy==1.7.3
six==1.16.0
sympy==1.10.1
typing_extensions==4.3.0

Your second file BCH_7_4_1_strip.alist contains several formatting errors:

https://github.com/veeresht/CommPy/blob/9496b1fe7b51a9dc872543941b1bcd293b3608b0/commpy/channelcoding/ldpc.py#L60-L61

the vnodes et cnodes have to be seperated by '\t'.

https://github.com/veeresht/CommPy/blob/9496b1fe7b51a9dc872543941b1bcd293b3608b0/commpy/channelcoding/ldpc.py#L56-L59

Your file lacks one header line.

BastienTr commented 1 year ago

Closed since the error is not reproductible in its current description. Feel free to re-open with new information.