Number labels handling copy_labels reassignobjectnumbers.py

Hi,

I have observed an error with the copy_labels function in the “Reassign Object Numbers” module in cellprofiler. I am using revision 10997, while the code for copy_labels is also identical in revision 11429.

copy_labels has bad types when the number of labels is 0 or 1. When the number of labels is 0, the call to scind.minimum fails. When the number of labels is 1, seglabel becomes a float, rather than an array, which is not subscriptable. I will continue working on the debugging next week. Any input on the issue is most welcome.

Thanks,
Chuck

The original code is posted here:

def copy_labels(labels, segmented):
’’'Carry differences between orig_segmented and new_segmented into “labels”

labels - labels matrix similarly segmented to "segmented"
segmented - the newly numbered labels matrix (a subset of pixels are labeled)
'''
max_labels = np.max(segmented)
seglabel = scind.minimum(labels, segmented, np.arange(1, max_labels+1))
labels_new = labels.copy()
labels_new[segmented != 0] = seglabel[segmented[segmented != 0] - 1]
return labels_new

And my preliminary testing code below (including some debug messages):

def copy_labels(labels, segmented):
’’'Carry differences between orig_segmented and new_segmented into “labels”

labels - labels matrix similarly segmented to "segmented"
segmented - the newly numbered labels matrix (a subset of pixels are labeled)
'''
print "labels", labels, type(labels)
print "segmented", segmented, type(segmented)
max_labels = np.max(segmented)
print "max_labels", max_labels, type(max_labels)
if (max_labels != 0):
    seglabel = scind.minimum(labels, segmented, np.arange(1, max_labels+1))
    seglabel = np.array(seglabel)
    if seglabel.size == 1:
        seglabel.shape = (1)
    print "seglabel", seglabel, type(seglabel)
    labels_new = labels.copy()
    print "labels_new", labels_new, type(labels_new)
    labels_new[segmented != 0] = seglabel[segmented[segmented != 0] - 1]
    return labels_new
else:
    return None

Sorry, here’s a better formatted code post
original:

def copy_labels(labels, segmented):
    '''Carry differences between orig_segmented and new_segmented into "labels"
    
    labels - labels matrix similarly segmented to "segmented"
    segmented - the newly numbered labels matrix (a subset of pixels are labeled)
    '''
    max_labels = np.max(segmented)
    seglabel = scind.minimum(labels, segmented, np.arange(1, max_labels+1))
    labels_new = labels.copy()
    labels_new[segmented != 0] = seglabel[segmented[segmented != 0] - 1]
    return labels_new

testing:

def copy_labels(labels, segmented):
    '''Carry differences between orig_segmented and new_segmented into "labels"
    
    labels - labels matrix similarly segmented to "segmented"
    segmented - the newly numbered labels matrix (a subset of pixels are labeled)
    '''
    print "labels", labels, type(labels)
    print "segmented", segmented, type(segmented)
    max_labels = np.max(segmented)
    print "max_labels", max_labels, type(max_labels)
    if (max_labels != 0):
        seglabel = scind.minimum(labels, segmented, np.arange(1, max_labels+1))
        seglabel = np.array(seglabel)
        if seglabel.size == 1:
            seglabel.shape = (1)
        print "seglabel", seglabel, type(seglabel)
        labels_new = labels.copy()
        print "labels_new", labels_new, type(labels_new)
        labels_new[segmented != 0] = seglabel[segmented[segmented != 0] - 1]
        return labels_new
    else:
        return None