src/create_rois.py
def order_regions(regions):
    '''
    This function is an absolute nightmare that will require a lot of in-line commenting to make any sense of. But basically it gets
    a list of region bounding boxes and returns the same list, but ordered left-right and top-bottom (i.e. writing order). 

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    a mess (list): also tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes, but ordered
                                    
    '''


    import numpy as np
    if regions != []:
        
        #while regions != []:
        centroids = generate_centroids(regions)

        # detecting top-left ROI: lowest sum of coordinates
        sums = [c[0]+c[1] for c in centroids]
        topleft = sums.index(min(sums))
        # I won't need to order that one (it's the first), so I get rid of it
        c_topleft = centroids[topleft]
        r_topleft = regions[topleft]
        regions.remove(r_topleft)
        centroids.remove(c_topleft)

        # calculate weighted distances to the top left ROI, where Y distances are weighted at
        # 20 (!!!) times the X distances - this sucks and I still can't figure out a better way to 
        # make sure I'm getting lines correctly!
        dists = [weighted_distance(x,c_topleft,20) for x in centroids]
        
        # basically I sort everything based on the weighted distances
        # If I did things right, the first elements are on the top line, then there's a big
        # jump in weighted distances, then second line, big jump, third line, and so on
        centroids = [x for _,x in sorted(zip(dists,centroids))]
        regions = [x for _,x in sorted(zip(dists,regions))]
        dists = [x for x in sorted(dists)]

        # detecting "big jumps" as anything bigger than 1.5 times the st dev of differences
        differences = [dists[i+1]-dists[i] for i in range(len(dists)-1)]
        line_dividers = differences > 1.5*np.std(differences)
        line_dividers = np.insert(line_dividers,0,False)


        # create list of lists where each element is a list containing a line of ROIs
        lines = []
        lines.append([r_topleft])
        for i in range(len(regions)):
            if line_dividers[i]:
                lines.append([])
            lines[-1].append(regions[i])
            
        results = []

        # sort each line left-to-right by comparing x values
        for line in lines:
            line_centr = generate_centroids(line)
            xvals = [x[0] for x in line_centr]  
            results.append([x for _,x in sorted(zip(xvals,line))])  

        # return statement is just unraveling the list of lists as a single list
        return [item for sublist in results for item in sublist]
    else:
        return []

    This function is an absolute nightmare that will require a lot of in-line commenting to make any sense of. But basically it gets
    a list of region bounding boxes and returns the same list, but ordered left-right and top-bottom (i.e. writing order). 

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    a mess (list): also tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes, but ordered
                                    
    
This function is an absolute nightmare that will require a lot of in-line commenting to make any sense of. But basically it gets
a list of region bounding boxes and returns the same list, but ordered left-right and top-bottom .
This function is an absolute nightmare that will require a lot of in - line commenting to make any sense of . But basically it gets a list of region bounding boxes and returns the same list but ordered left - right and top - bottom .
def order_regions(regions):
    import numpy as np
    if regions != []:
        centroids = generate_centroids(regions)
        sums = [c[0]+c[1] for c in centroids]
        topleft = sums.index(min(sums))
        c_topleft = centroids[topleft]
        r_topleft = regions[topleft]
        regions.remove(r_topleft)
        centroids.remove(c_topleft)
        dists = [weighted_distance(x,c_topleft,20) for x in centroids]
        centroids = [x for _,x in sorted(zip(dists,centroids))]
        regions = [x for _,x in sorted(zip(dists,regions))]
        dists = [x for x in sorted(dists)]
        differences = [dists[i+1]-dists[i] for i in range(len(dists)-1)]
        line_dividers = differences > 1.5*np.std(differences)
        line_dividers = np.insert(line_dividers,0,False)
        lines = []
        lines.append([r_topleft])
        for i in range(len(regions)):
            if line_dividers[i]:
                lines.append([])
            lines[-1].append(regions[i])
        results = []
        for line in lines:
            line_centr = generate_centroids(line)
            xvals = [x[0] for x in line_centr]  
            results.append([x for _,x in sorted(zip(xvals,line))])  
        return [item for sublist in results for item in sublist]
    else:
        return []
def order_regions ( regions ) : import numpy as np if regions != [ ] : centroids = generate_centroids ( regions ) sums = [ c [ 0 ] + c [ 1 ] for c in centroids ] topleft = sums . index ( min ( sums ) ) c_topleft = centroids [ topleft ] r_topleft = regions [ topleft ] regions . remove ( r_topleft ) centroids . remove ( c_topleft ) dists = [ weighted_distance ( x , c_topleft , 20 ) for x in centroids ] centroids = [ x for _ , x in sorted ( zip ( dists , centroids ) ) ] regions = [ x for _ , x in sorted ( zip ( dists , regions ) ) ] dists = [ x for x in sorted ( dists ) ] differences = [ dists [ i + 1 ] - dists [ i ] for i in range ( len ( dists ) - 1 ) ] line_dividers = differences > 1.5 * np . std ( differences ) line_dividers = np . insert ( line_dividers , 0 , False ) lines = [ ] lines . append ( [ r_topleft ] ) for i in range ( len ( regions ) ) : if line_dividers [ i ] : lines . append ( [ ] ) lines [ - 1 ] . append ( regions [ i ] ) results = [ ] for line in lines : line_centr = generate_centroids ( line ) xvals = [ x [ 0 ] for x in line_centr ] results . append ( [ x for _ , x in sorted ( zip ( xvals , line ) ) ] ) return [ item for sublist in results for item in sublist ] else : return [ ]
This function is an absolute nightmare that will require a lot of in-line commenting to make any sense of.
This function is an absolute nightmare that will require a lot of in - line commenting to make any sense of .
'''
    This function is an absolute nightmare that will require a lot of in-line commenting to make any sense of. But basically it gets
    a list of region bounding boxes and returns the same list, but ordered left-right and top-bottom (i.e. writing order). 

    Parameters:
                    regions (list): tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
                    
            Returns:
                    a mess (list): also tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes, but ordered
                                    
    ''' #while regions != []: # detecting top-left ROI: lowest sum of coordinates # I won't need to order that one (it's the first), so I get rid of it # calculate weighted distances to the top left ROI, where Y distances are weighted at # 20 (!!!) times the X distances - this sucks and I still can't figure out a better way to  # make sure I'm getting lines correctly! # basically I sort everything based on the weighted distances # If I did things right, the first elements are on the top line, then there's a big # jump in weighted distances, then second line, big jump, third line, and so on # detecting "big jumps" as anything bigger than 1.5 times the st dev of differences # create list of lists where each element is a list containing a line of ROIs # sort each line left-to-right by comparing x values # return statement is just unraveling the list of lists as a single list
regions
tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes
Returns.
tuples of the form ( y1 x1 y2 x2 ) representing the ROI bounding boxes Returns .
a mess
also tuples of the form (y1,x1,y2,x2) representing the ROI bounding boxes, but ordered
also tuples of the form ( y1 x1 y2 x2 ) representing the ROI bounding boxes but ordered