Python node hanging due to list appending ?

#1

Hello,

I am regularly using Python nodes in my workflow for image processing. My routine is to store the image in lists and then use those lists as column for the Panda output table. It is maybe not the most efficient way but it has always been working nicely so far. 

Recently I had a problem with a Python node hanging at 30%. This node is supposed to take an image and rotate/mirror it for template matching purpose. After several tests I realised that the script is not hanging if I comment one line at the end of the script that is appending the newly processed image, although the final size of the list is only 2 or 3 image in this case !

I found this post on stack overflow https://stackoverflow.com/questions/2473783/is-there-a-way-to-circumvent-python-list-append-becoming-progressively-slower related to issue with list appending, do you think that could be the problem here? 

I am using Python 2.7.13, Knime 3.5.0 on windows 10.

I have attached the workflow, it is about the python node in the top branch. You need to give an image to the image reader node to test it.

Here the Python script :

import pandas as pd
from KNIPImage import KNIPImage
from scipy.ndimage.interpolation import rotate
import numpy as np

Step = flow_variables['DegreeStep']

# If no rotation nor flip then input table = output table
if Step == 0 and not flow_variables['FlipVertical'] and not flow_variables['FlipHorizontal']:
    output_table = input_table.copy()

else : # Rotation or flip
    output_table = pd.DataFrame() # create a new data table for the output
    List_Template = [] # final list of template after rotation, mirorring...

    for Template in input_table['Image']: # read template from table and flip them if necessary
        # Read template images and add them to final template list
        Template = Template.array
        List_Template.append(Template)

        # Generate flipped templates and add to the final list
        # Must be done before the rotation
        if flow_variables['FlipVertical']:
            FlipLR = np.fliplr(Template)
            List_Template.append(FlipLR)

        if flow_variables['FlipHorizontal']:
            FlipUD = np.flipud(Template)
            List_Template.append(FlipUD)
        
    # Generate rotated templates
    if Step != 0 :
        print 'Do Rotation'
        
        for Template in List_Template:
            # rotate
            print 'Generate rotation for new template' 
            for angle in xrange(Step,360,Step):
                NewTemplate = rotate(Template,angle,reshape=True,mode='nearest')
                List_Template.append(NewTemplate) # THIS LINE MAKES THE SCRIPT HANG ! 

                # Control
                print type(NewTemplate)
                print NewTemplate.dtype
                print NewTemplate.shape
                
    
    output_table['Template'] = [KNIPImage(template) for template in List_Template]

 

0 Likes

#2

Sorry I found the bug in the python script. In the last for loop I am appending to the list on which I am looping therefore creating an endless loop.

0 Likes

#3

Check this one…Python append() vs extend()

0 Likes