'ERROR: File "mtrand.pyx", line 902, in numpy.random.mtrand.RandomState.choice

..can.. Hi all, can someone take a look on this code, I have one problem but I don't know what is?
I'm working on generating various length and shapes of text on image, and when segmentated area is enough big then text is placed, but when the length of text a little bigger then this error shows me. Example, when the text has 1-8 words then the output is fine, but when the length is bigger then it shows me this error, but on some images works fine because it have bigger area to render the text. So I don't know what to do?

Terminal shows me these errors:

  File "/..../text_utils.py", line 679, in sample
    return self.fdict[kind](nline_max,nchar_max)
  File "/..../text_utils.py", line 725, in sample_para
    lines = self.get_lines(nline, nword, nchar_max, f=0.35)
  File "/..../text_utils.py", line 657, in get_lines
    lines = h_lines(niter=100)
  File "/..../text_utils.py", line 649, in h_lines
    line_start = np.random.choice(len(self.txt)-nline)
  File "mtrand.pyx", line 902, in numpy.random.mtrand.RandomState.choice
ValueError: a must be greater than 0 unless no samples are taken

I saw this on this link: https://github.com/numpy/numpy/blob/main/numpy/random/mtrand.pyx there is some statement at 902 line but I don't understand.

And this is my code:

    def get_lines(self, nline, nword, nchar_max, f=0.35, niter=100):
            def h_lines(niter=100):
                lines = ['']
                iter = 0
                while not np.all(self.is_good(lines,f)) and iter < niter:
                    iter += 1
**649 ---->**       line_start = np.random.choice(len(self.txt)-nline)
                    lines = [self.txt[line_start+i] for i in range(nline)]
                return lines
    
            lines = ['']
            iter = 0
            while not np.all(self.is_good(lines,f)) and iter < niter:
                iter += 1
**657 ---->**   lines = h_lines(niter=100)
                # get words per line:
                nline = len(lines)
                for i in range(nline):
                    words = lines[i].split()
                    dw = len(words)-nword[i]
                    if dw > 0:
                        first_word_index = random.choice(range(dw+1))
                        lines[i] = ' '.join(words[first_word_index:first_word_index+nword[i]])
    
                    while len(lines[i]) > nchar_max: #chop-off characters from end:
                        if not np.any([ch.isspace() for ch in lines[i]]):
                            lines[i] = ''
                        else:
                            lines[i] = lines[i][:len(lines[i])-lines[i][::-1].find(' ')].strip()
            
            if not np.all(self.is_good(lines,f)):
                return #None
            else:
                return lines


    def sample(self, nline_max,nchar_max,kind='WORD'):
**679 ---->**   return self.fdict[kind](nline_max,nchar_max)


def sample_para(self,nline_max,nchar_max):
        # get number of lines in the paragraph:
        nline = nline_max*sstat.beta.rvs(a=self.p_para_nline[0], b=self.p_para_nline[1])
        nline = max(1, int(np.ceil(nline)))

        # get number of words:
        nword = [self.p_para_nword[2]*sstat.beta.rvs(a=self.p_para_nword[0], b=self.p_para_nword[1])
                 for _ in range(nline)]
        nword = [max(1,int(np.ceil(n))) for n in nword]

**725 ---->**  lines = self.get_lines(nline, nword, nchar_max, f=0.35)
        if lines is not None:
            # center align the paragraph-text:
            if np.random.rand() < self.center_para:
                lines = self.center_align(lines)
            return '\n'.join(lines)
        else:
            return []


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source