def extract_adjacent_shapes(df_shapes, shape_i_column, extend=.5):
    '''
    Generate list of connections between "adjacent" polygon shapes based on
    geometrical "closeness".

    Parameters
    ----------
    df_shapes : pandas.DataFrame
        Table of polygon shape vertices (one row per vertex).

        Table rows with the same value in the :data:`shape_i_column` column
        are grouped together as a polygon.
    shape_i_column : str or list[str]
        Column name(s) that identify the polygon each row belongs to.
    extend : float, optional
        Extend ``x``/``y`` coords by the specified number of absolute units
        from the center point of each polygon.
        Each polygon is stretched independently in the ``x`` and ``y`` direction.
        In each direction, a polygon considered adjacent to all polygons that
        are overlapped by the extended shape.

    Returns
    -------
    pandas.DataFrame
        Adjacency list as a frame containing the columns ``source`` and
        ``target``.

        The ``source`` and ``target`` of each adjacency connection is ordered
        such that the ``source`` is less than the ``target``.
    '''
    # Find corners of each solid shape outline.
    # Extend x coords by abs units
    df_scaled_x = extend_shapes(df_shapes, 'x', extend)
    # Extend y coords by abs units
    df_scaled_y = extend_shapes(df_shapes, 'y', extend)

    df_corners = df_shapes.groupby(shape_i_column).agg({'x': ['min', 'max'],
                                                        'y': ['min', 'max']})

    # Find adjacent electrodes
    row_list = []

    for shapeNumber in df_shapes[shape_i_column].drop_duplicates():
        df_stretched = df_scaled_x[df_scaled_x[shape_i_column]
                                   .isin([shapeNumber])]
        xmin_x, xmax_x, ymin_x, ymax_x = (df_stretched.x.min(),
                                          df_stretched.x.max(),
                                          df_stretched.y.min(),
                                          df_stretched.y.max())
        df_stretched = df_scaled_y[df_scaled_y[shape_i_column]
                                   .isin([shapeNumber])]
        xmin_y, xmax_y, ymin_y, ymax_y = (df_stretched.x.min(),
                                          df_stretched.x.max(),
                                          df_stretched.y.min(),
                                          df_stretched.y.max())

        #Some conditions unnecessary if it is assumed that electrodes don't overlap
        adjacent = df_corners[
            ((df_corners.x['min'] < xmax_x) & (df_corners.x['max'] >= xmax_x)
            # Check in x stretched direction
            |(df_corners.x['min'] < xmin_x) & (df_corners.x['max'] >= xmin_x))
            # Check if y is within bounds
            & (df_corners.y['min'] < ymax_x) & (df_corners.y['max'] > ymin_x) |

            #maybe do ymax_x - df_corners.y['min'] > threshold &
            #  df_corners.y['max'] - ymin_x > threshold

            ((df_corners.y['min'] < ymax_y) & (df_corners.y['max'] >= ymax_y)
             # Checks in y stretched direction
             |(df_corners.y['min'] < ymin_y) & (df_corners.y['max'] >= ymin_y))
             # Check if x in within bounds
            & ((df_corners.x['min'] < xmax_y) & (df_corners.x['max'] > xmin_y))
        ].index.values

        for shape in adjacent:
            temp_dict = {}
            reverse_dict = {}

            temp_dict ['source'] = shapeNumber
            reverse_dict['source'] = shape
            temp_dict ['target'] = shape
            reverse_dict['target'] = shapeNumber

            if(reverse_dict not in row_list):
                row_list.append(temp_dict)

    df_connected = (pd.DataFrame(row_list)[['source', 'target']]
                    .sort_index(axis=1, ascending=True)
                    .sort_values(['source', 'target']))
    return df_connected
def extract_adjacent_shapes ( df_shapes , shape_i_column , extend = .5 ) : df_scaled_x = extend_shapes ( df_shapes , 'x' , extend ) df_scaled_y = extend_shapes ( df_shapes , 'y' , extend ) df_corners = df_shapes . groupby ( shape_i_column ) . agg ( { 'x' : [ 'min' , 'max' ] , 'y' : [ 'min' , 'max' ] } ) row_list = [ ] for shapeNumber in df_shapes [ shape_i_column ] . drop_duplicates ( ) : df_stretched = df_scaled_x [ df_scaled_x [ shape_i_column ] . isin ( [ shapeNumber ] ) ] xmin_x , xmax_x , ymin_x , ymax_x = ( df_stretched . x . min ( ) , df_stretched . x . max ( ) , df_stretched . y . min ( ) , df_stretched . y . max ( ) ) df_stretched = df_scaled_y [ df_scaled_y [ shape_i_column ] . isin ( [ shapeNumber ] ) ] xmin_y , xmax_y , ymin_y , ymax_y = ( df_stretched . x . min ( ) , df_stretched . x . max ( ) , df_stretched . y . min ( ) , df_stretched . y . max ( ) ) adjacent = df_corners [ ( ( df_corners . x [ 'min' ] < xmax_x ) & ( df_corners . x [ 'max' ] >= xmax_x ) | ( df_corners . x [ 'min' ] < xmin_x ) & ( df_corners . x [ 'max' ] >= xmin_x ) ) & ( df_corners . y [ 'min' ] < ymax_x ) & ( df_corners . y [ 'max' ] > ymin_x ) | ( ( df_corners . y [ 'min' ] < ymax_y ) & ( df_corners . y [ 'max' ] >= ymax_y ) | ( df_corners . y [ 'min' ] < ymin_y ) & ( df_corners . y [ 'max' ] >= ymin_y ) ) & ( ( df_corners . x [ 'min' ] < xmax_y ) & ( df_corners . x [ 'max' ] > xmin_y ) ) ] . index . values for shape in adjacent : temp_dict = { } reverse_dict = { } temp_dict [ 'source' ] = shapeNumber reverse_dict [ 'source' ] = shape temp_dict [ 'target' ] = shape reverse_dict [ 'target' ] = shapeNumber if ( reverse_dict not in row_list ) : row_list . append ( temp_dict ) df_connected = ( pd . DataFrame ( row_list ) [ [ 'source' , 'target' ] ] . sort_index ( axis = 1 , ascending = True ) . sort_values ( [ 'source' , 'target' ] ) ) return df_connected
Generate list of connections between "adjacent" polygon shapes based on
    geometrical "closeness".

    Parameters
    ----------
    df_shapes : pandas.DataFrame
        Table of polygon shape vertices (one row per vertex).

        Table rows with the same value in the :data:`shape_i_column` column
        are grouped together as a polygon.
    shape_i_column : str or list[str]
        Column name(s) that identify the polygon each row belongs to.
    extend : float, optional
        Extend ``x``/``y`` coords by the specified number of absolute units
        from the center point of each polygon.
        Each polygon is stretched independently in the ``x`` and ``y`` direction.
        In each direction, a polygon considered adjacent to all polygons that
        are overlapped by the extended shape.

    Returns
    -------
    pandas.DataFrame
        Adjacency list as a frame containing the columns ``source`` and
        ``target``.

        The ``source`` and ``target`` of each adjacency connection is ordered
        such that the ``source`` is less than the ``target``.