text:
import sys
import numpy
import matplotlib.pyplot as plt
from enum import Enum

class Polygon(object):

    def __init__(self,expression, bounds, seg_length):
        self.pts = main(expression, bounds, seg_length)

    def get_pts(self):
        return self.pts

def main(expression, bounds, seg_length):

    class Cell(object):
        def __init__(self, cell_rel_pos, center, expression):
            self.cell_pos = cell_rel_pos
            self.center = center
            self.corners = [[round(center[0] - hs, 3), round(center[1] + hs, 3)],
                            [round(center[0] + hs, 3), round(center[1] + hs, 3)],
                            [round(center[0] + hs, 3), round(center[1] - hs, 3)],
                            [round(center[0] - hs, 3), round(center[1] - hs, 3)]]
            self.expression = expression
            self.binary_Index = []
            for pt in self.corners:
                x = pt[0]
                y = pt[1]
                corner_value = eval(self.expression)
                if corner_value < 0:
                    self.binary_Index.append(0)
                elif corner_value >= 0:
                    self.binary_Index.append(1)

        def get_cell_pos(self):
            return self.cell_pos

        def get_center(self):
            return self.center

        def get_corners(self):
            return self.corners

        def get_binary_Index(self):
            return self.binary_Index

    class IndexCases(Enum):
        case_0 = [0, 0, 0, 0]
        case_1 = [0, 0, 0, 1]
        case_2 = [0, 0, 1, 0]
        case_3 = [0, 0, 1, 1]
        case_4 = [0, 1, 0, 0]
        case_5 = [0, 1, 0, 1]
        case_6 = [0, 1, 1, 0]
        case_7 = [0, 1, 1, 1]
        case_8 = [1, 0, 0, 0]
        case_9 = [1, 0, 0, 1]
        case_10 = [1, 0, 1, 0]
        case_11 = [1, 0, 1, 1]
        case_12 = [1, 1, 0, 0]
        case_13 = [1, 1, 0, 1]
        case_14 = [1, 1, 1, 0]
        case_15 = [1, 1, 1, 1]

    """Getting Input from the file that defines the expression, the bounds and maximum segment length"""
    boundsOfFile = bounds
    x_min = boundsOfFile[0]
    x_max = boundsOfFile[1]
    y_min = boundsOfFile[2]
    y_max = boundsOfFile[3]
    h = seg_length
    all_segments = []

    """Max Cell dimensions"""
    hs = round(h * (2 ** .5) / 4, 3)

    """Find the grid intervals in x,y"""
    num_Of_x = int((x_max - x_min) / (2 * hs) - 1)
    num_Of_y = int((y_max - y_min) / (2 * hs) - 1)
    xGrid = []
    for i in range(0, num_Of_x + 1):
        if i == 0:
            xGrid.append(round(x_min + hs, 3))
        elif i == num_Of_x + 1:
            xGrid.append(round(x_max - hs, 3))
        else:
            xGrid.append(round(x_min + hs + (hs * 2) * i, 3))
    yGrid = []
    for j in range(0, num_Of_y + 1):
        if j == 0:
            yGrid.append(round(y_min + hs, 3))
        elif j == num_Of_y + 1:
            yGrid.append(round(y_max - hs, 3))
        else:
            yGrid.append(round(y_min + hs + (hs * 2) * j, 3))

    """Creating Cells with the grids obtained"""
    cells = []
    xRelativePos = 0
    for x in xGrid:
        yRelativePos = 0
        for y in yGrid:
            pos = (round(x, 3), round(y, 3))
            d = {'x': pos[0], 'y': pos[1]}
            cell_pos = (xRelativePos, yRelativePos)
            plt.plot([x - hs, x - hs], [y + hs, y - hs], color='black')
            plt.plot([x + hs, x + hs], [y + hs, y - hs], color='black')
            plt.plot([x + hs, x - hs], [y + hs, y + hs], color='black')
            plt.plot([x + hs, x - hs], [y - hs, y - hs], color='black')
            f_value = eval(expression,d)
            if f_value >= 0:
                plt.scatter(x, y, color="blue", s=10)
            else:
                plt.scatter(x, y, color="red", s=5)
            cells.append(Cell(cell_pos, pos, expression))
            yRelativePos = yRelativePos + 1
        xRelativePos = xRelativePos + 1

    """Formulae for linear interpolation"""
    def interpolateRight(right_top, right_bottom, right_top_val, right_bottom_val):
        y = right_top[1] + (right_bottom[1] - right_top[1]) * (0 - right_top_val) / (right_bottom_val - right_top_val)
        x = right_bottom[0]
        return round(x, 3), round(y, 3)

    def interpolateLeft(left_top, left_bottom, left_top_val, left_bottom_val):
        y = left_top[1] + (left_bottom[1] - left_top[1]) * (0 - left_top_val) / (left_bottom_val - left_top_val)
        x = left_bottom[0]
        return round(x, 3), round(y, 3)

    def interpolateTop(top_left, top_right, top_left_val, top_right_val):
        x = top_right[0] + (top_left[0] - top_right[0]) * (0 - top_right_val) / (top_left_val - top_right_val)
        y = top_left[1]
        return round(x, 3), round(y, 3)

    def interpolateBottom(bottom_left, bottom_right, bottom_left_val, bottom_right_val):
        x = bottom_right[0] + (bottom_left[0] - bottom_right[0]) * (0 - bottom_right_val) / (bottom_left_val - bottom_right_val)
        y = bottom_left[1]
        return round(x, 3), round(y, 3)

    """Iterating throught the cells to perform marching squares"""
    for cell in cells:
        center = cell.get_center()
        x = center[0]
        y = center[1]
        index = cell.get_binary_Index()
        corners = cell.get_corners()
        topLeftPoint = corners[0]
        topRightPoint = corners[1]
        bottomRightPoint = corners[2]
        bottomLeftPoint = corners[3]

        if numpy.array_equal(IndexCases.case_1.value, index):
            """Compute the points"""
            d1 = {'x': topLeftPoint[0], 'y': y + hs}
            d2 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d1)
            valueOfLeftBottom = eval(expression, d2)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_2.value, index):
            """Compute the points"""
            d1 = {'x': topRightPoint[0], 'y': y + hs}
            d2 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d1)
            valueOfRightBottom = eval(expression, d2)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_3.value, index):
            """Compute the points"""
            d1 = {'x': topLeftPoint[0], 'y': y + hs}
            d2 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d1)
            valueOfLeftBottom = eval(expression, d2)
            vert_zero_left = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            d3 = {'x': topRightPoint[0], 'y': y + hs}
            d4 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d3)
            valueOfRightBottom = eval(expression, d4)
            vert_zero_right = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            """Append the points"""
            pt1 = ([vert_zero_left[0], vert_zero_left[1]])
            pt2 = ([vert_zero_right[0], vert_zero_right[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_4.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': topRightPoint[0], 'y': y + hs}
            d4 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d3)
            valueOfRightBottom = eval(expression, d4)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_5.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': topLeftPoint[0], 'y': y + hs}
            d4 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d3)
            valueOfLeftBottom = eval(expression, d4)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])

            d5 = {'x': topRightPoint[0], 'y': y + hs}
            d6 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d5)
            valueOfRightBottom = eval(expression, d6)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            d7 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d8 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d7)
            valueOfBottomRight = eval(expression, d8)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt3 = ([horz_zero[0], horz_zero[1]])
            pt4 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])
            all_segments .append([pt3, pt4])

        elif numpy.array_equal(IndexCases.case_6.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero_top = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero_bot = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero_top[0], horz_zero_top[1]])
            pt2 = ([horz_zero_bot[0], horz_zero_bot[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_7.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': topLeftPoint[0], 'y': y + hs}
            d4 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d3)
            valueOfLeftBottom = eval(expression, d4)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_8.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': topLeftPoint[0], 'y': y + hs}
            d4 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d3)
            valueOfLeftBottom = eval(expression, d4)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_9.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero_top = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero_bot = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero_top[0], horz_zero_top[1]])
            pt2 = ([horz_zero_bot[0], horz_zero_bot[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_10.value, index):
            """Compute the points"""
            d1 = {'x': topLeftPoint[0], 'y': y + hs}
            d2 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d1)
            valueOfLeftBottom = eval(expression, d2)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])

            d5 = {'x': x - hs, 'y': topLeftPoint[1]}
            d6 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d5)
            valueOfTopRight = eval(expression, d6)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d7 = {'x': topRightPoint[0], 'y': y + hs}
            d8 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d7)
            valueOfRightBottom = eval(expression, d8)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            """Append the Points"""
            pt3 = ([horz_zero[0], horz_zero[1]])
            pt4 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])
            all_segments .append([pt3, pt4])

        elif numpy.array_equal(IndexCases.case_11.value, index):
            """Compute the points"""
            d1 = {'x': x - hs, 'y': topLeftPoint[1]}
            d2 = {'x': x + hs, 'y': topLeftPoint[1]}
            valueOfTopLeft = eval(expression, d1)
            valueOfTopRight = eval(expression, d2)
            horz_zero = interpolateTop(topLeftPoint, topRightPoint, valueOfTopLeft, valueOfTopRight)

            d3 = {'x': topRightPoint[0], 'y': y + hs}
            d4 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d3)
            valueOfRightBottom = eval(expression, d4)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_12.value, index):
            """Compute the points"""
            d1 = {'x': topLeftPoint[0], 'y': y + hs}
            d2 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d1)
            valueOfLeftBottom = eval(expression, d2)
            vert_zero_left = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            d3 = {'x': topRightPoint[0], 'y': y + hs}
            d4 = {'x': topRightPoint[0], 'y': y - hs}

            valueOfRightTop = eval(expression, d3)
            valueOfRightBottom = eval(expression, d4)
            vert_zero_right = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            """Append the Points"""
            pt1 = ([vert_zero_right[0], vert_zero_right[1]])
            pt2 = ([vert_zero_left[0], vert_zero_left[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_13.value, index):
            """Compute the points"""
            d1 = {'x': topRightPoint[0], 'y': y + hs}
            d2 = {'x': topRightPoint[0], 'y': y - hs}
            valueOfRightTop = eval(expression, d1)
            valueOfRightBottom = eval(expression, d2)
            vert_zero = interpolateRight(topRightPoint, bottomRightPoint, valueOfRightTop, valueOfRightBottom)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

        elif numpy.array_equal(IndexCases.case_14.value, index):
            """Compute the points"""
            d1 = {'x': topLeftPoint[0], 'y': y + hs}
            d2 = {'x': topLeftPoint[0], 'y': y - hs}
            valueOfLeftTop = eval(expression, d1)
            valueOfLeftBottom = eval(expression, d2)
            vert_zero = interpolateLeft(topLeftPoint, bottomLeftPoint, valueOfLeftTop, valueOfLeftBottom)

            d3 = {'x': x - hs, 'y': bottomLeftPoint[1]}
            d4 = {'x': x + hs, 'y': bottomLeftPoint[1]}
            valueOfBottomLeft = eval(expression, d3)
            valueOfBottomRight = eval(expression, d4)
            horz_zero = interpolateBottom(bottomLeftPoint, bottomRightPoint, valueOfBottomLeft, valueOfBottomRight)

            """Append the Points"""
            pt1 = ([horz_zero[0], horz_zero[1]])
            pt2 = ([vert_zero[0], vert_zero[1]])
            all_segments .append([pt1, pt2])

    """Ordering the computed line segments that form the boundary of the polygon"""
    pointsOfPolygon = []
    lastPoint = all_segments [0][0]
    referencePoint = all_segments [0][1]
    pointsOfPolygon.append(lastPoint)
    pointsOfPolygon.append(referencePoint)
    plt.plot([lastPoint[0], referencePoint[0]], [lastPoint[1], referencePoint[1]], color="Blue")
    start = True
    while referencePoint != all_segments [0][0] or start is True:
        start = False
        for seg in range(0, len(all_segments )):
            pt1 = all_segments [seg][0]
            pt2 = all_segments [seg][1]
            if pt1[0] == referencePoint[0] and pt1[1] == referencePoint[1] and \
                    pt2[0] != lastPoint[0] and pt2[1] != lastPoint[1]:
                pointsOfPolygon.append(pt2)
                lastPoint = pt1
                referencePoint = pt2
                plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]], color="Blue")
                break
            elif pt2[0] == referencePoint[0] and pt2[1] == referencePoint[1] and \
                pt1[0] != lastPoint[0] and pt1[1] != lastPoint[1]:
                pointsOfPolygon.append(pt1)
                lastPoint = pt2
                referencePoint = pt1
                plt.plot([pt1[0], pt2[0]], [pt1[1], pt2[1]], color="Blue")
                break

    """Segment Intersection"""
    def checkIntersection(P1, P2, P3, P4):
        x1, y1 = P1[0], P1[1]
        x2, y2 = P2[0], P2[1]
        x3, y3 = P3[0], P3[1]
        x4, y4 = P4[0], P4[1]

        if max(x1, x2) < min(x3, x4) or max(x3, x4) < min(x1, x2):
            return False
        elif max(y1, y2) < min(y3, y4) or max(y3, y4) < min(y1, y2):
            return False
        else:
            segment_12_is_vertical = False
            segment_34_is_vertical = False
            #Assigning random values to prevent errors
            slope_1 = -10000
            slope_2 = -200000
            if x1 - x2 != 0:
                slope_1 = (y1 - y2) / (x1 - x2)
            else:
                segment_12_is_vertical = True
            if x3 - x4 != 0:
                slope_2 = (y3 - y4) / (x3 - x4)
            else:
                segment_34_is_vertical = False
            if  slope_1 == slope_2:
                return False
            elif (segment_12_is_vertical is True and segment_34_is_vertical is True):
                return False
            else:
                b1 = y2 - slope_1 * x2
                b2 = y4 - slope_2 * x4
                x_intersect = round(((b2 - b1) / (slope_1 - slope_2)), 3)
            if x_intersect <= max(min(x1, x2), min(x3, x4)) or x_intersect >= min(max(x1, x2), max(x3, x4)):
                return False
            else:
                return True

    """Checking for intersections"""
    hasIntersections = False
    for i in range(0, len(pointsOfPolygon) - 1):
        pt_1 = pointsOfPolygon[i]
        pt_2 = pointsOfPolygon[i + 1]
        for j in range(0, len(pointsOfPolygon) - 1):
            pt_3 = pointsOfPolygon[j]
            pt_4 = pointsOfPolygon[j + 1]
            intersect = checkIntersection(pt_1, pt_2, pt_3, pt_4)
            if intersect is True:
                hasIntersections = True
                break

    """Printable Output"""
    if len(pointsOfPolygon) - 1 == len(all_segments ) and hasIntersections is False:
        print("The constructed boundary is a simple polygon.")
    else:
        print("The constructed boundary is not a simple polygon.")

    return pointsOfPolygon

if __name__ == "__main__":
    """Read the input given thought the file"""
    point_file = open(sys.argv[1], "r")
    #point_file = open("expression-1.txt", "r")
    lines = []
    for line in point_file:
        lines.append(str(line))
    expression = lines[0]
    Bounds_str = lines[1].split(",")
    bounds = []
    for bound in Bounds_str:
        bounds.append(int(bound))
    seg_length = int(lines[2])
    main(expression, bounds, seg_length)
    plt.show()


text:
import os
import cv2
import sys
import h5py
import argparse
import numpy as np
from skimage import color
sys.path.append("../model")
import models_colorful as models_colorful

def webcam_colorizer(data_file, model_name, epoch, T, out_h, out_w, video_path=None):

    # Load the array of quantized ab value
    q_ab = np.load("../../data/processed/pts_in_hull.npy")
    nb_q = q_ab.shape[0]
    batch_size = 1

    # Load and rescale data
    with h5py.File(data_file, "r") as hf:
        X = hf["validation_lab_data"]
        c, h, w = X.shape[1:]

    img_size = int(os.path.basename(data_file).split("_")[1])

    # Load colorization model
    color_model = models_colorful.load(model_name, nb_q, (1, h, w), batch_size)
    color_model.load_weights("../../models/%s/%s_weights_epoch%s.h5" %
                             (model_name, model_name, epoch))

    #################
    # APP
    #################
    if video_path != "":
        cap = cv2.VideoCapture(video_path)
    else:
        cap = cv2.VideoCapture(0)

    print '\n\nPRESS q/Q to QUIT\n'

    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        if ret is True:
            img = cv2.resize(frame, (img_size, img_size), interpolation=cv2.INTER_AREA)
            img = img[:, :, ::-1]  # BGR to RGB

            # Convert to Lab
            img = color.rgb2lab(img)

            img_gray = img[:, :, 0].reshape((1, 1, img_size, img_size))

            X_black = img_gray / 100.
            X_colorized = color_model.predict(X_black)[:, :, :, :-1]

            # Format X_colorized
            X_colorized = X_colorized.reshape((1 * h * w, nb_q))

            # Reweight probas
            X_colorized = np.exp(np.log(X_colorized) / T)
            X_colorized = X_colorized / np.sum(X_colorized, 1)[:, np.newaxis]

            # Reweighted
            q_a = q_ab[:, 0].reshape((1, nb_q))
            q_b = q_ab[:, 1].reshape((1, nb_q))

            X_a = np.sum(X_colorized * q_a, 1).reshape((1, 1, h, w))
            X_b = np.sum(X_colorized * q_b, 1).reshape((1, 1, h, w))

            X_colorized = np.concatenate((100 * X_black, X_a, X_b), axis=1).transpose(0, 2, 3, 1)
            X_colorized = color.lab2rgb(X_colorized[0])

            X_black = X_black[0]
            X_black = np.repeat(X_black, 3, axis=0).transpose(1,2,0)
            X = np.concatenate((X_black, X_colorized[:, :, ::-1]), 1)
            X = cv2.resize(X, (out_w, out_h))

            cv2.imshow('Colorizer (q/Q: Quit)',X)
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        else:

            break

    # When everything done, release the capture
    cap.release()
    cv2.destroyAllWindows()

if __name__ == '__main__':

    parser = argparse.ArgumentParser(description='Colorize app')
    parser.add_argument('data_file', type=str, help="Path to HDF5 containing the data")
    parser.add_argument('epoch', type=int, help='Epoch of saved weights')
    parser.add_argument('--model_name', type=str, default="simple_colorful",
                        help="Model name. Choose simple_colorful or colorful")
    parser.add_argument('--T', default=0.1, type=float,
                        help="Temperature to change color balance. If T = 1: desaturated. If T~0 vivid")
    parser.add_argument('--out_h', default=420, type=int, help="Width of ouput image")
    parser.add_argument('--out_w', default=640, type=int, help="Height of ouput image")
    parser.add_argument('--video_path', default="", type=str, help="Path to B&W video to colorize")

    args = parser.parse_args()

    assert args.model_name in ["colorful", "simple_colorful"]

    webcam_colorizer(args.data_file,
                     args.model_name,
                     args.epoch,
                     args.T,
                     args.out_h,
                     args.out_w,
                     video_path=args.video_path)

ext: py

max_stars_repo_name: inamori/DeepLearningImplementations

max_issues_repo_name: inamori/DeepLearningImplementations

max_forks_repo_name: inamori/DeepLearningImplementations

reason: import numpy


text:
"""

    *Take*

"""

import jax.numpy as jnp

from ._operator import Manipulation

__all__ = ["Take"]

class Take(
    jnp.take,
    Manipulation,
):
    def __init__(
        self,
        *args,
        **kwargs,
    ):
        super(Take, self).__init__(
            *args,
            **kwargs,
        )

max_stars_repo_name: jedhsu/tensor

max_issues_repo_name: jedhsu/tensor

max_forks_repo_name: jedhsu/tensor

reason: import jax


text:
import argparse
import os

import numpy as np

import evaluation
import utils
from dataset import SAMPLE_IDS, VIDEO_IDS, EPICDiff, MaskLoader

def parse_args(path=None, vid=None, exp=None):

    parser = argparse.ArgumentParser()

    parser.add_argument("--path", type=str, default=path, help="Path to model.")

    parser.add_argument("--vid", type=str, default=vid, help="Video ID of dataset.")

    parser.add_argument("--exp", type=str, default=exp, help="Experiment name.")

    parser.add_argument(
        "--outputs",
        default=["masks"],
        type=str,
        nargs="+",
        help="Evaluation output. Select `masks` or `summary` or both.",
    )

    parser.add_argument(
        "--masks_n_samples",
        type=int,
        default=0,
        help="Select number of samples for evaluation. If kept at 0, then all test samples are evaluated.",
    )

    parser.add_argument(
        "--summary_n_samples",
        type=int,
        default=0,
        help="Number of samples to evaluate for summary video. If 0 is selected, then the video is rendered with all frames from the dataset.",
    )

    parser.add_argument(
        "--root_data", type=str, default="data/EPIC-Diff", help="Root of the dataset."
    )

    parser.add_argument(
        "--suppress_person",
        default=False,
        action="store_true",
        help="Disables person, e.g. for visualising complete foreground without parts missing where person occludes the foreground.",
    )

    # for opt.py
    parser.add_argument("--is_eval_script", default=True, action="store_true")

    args = parser.parse_args()

    return args

def init(args):

    dataset = EPICDiff(args.vid, root=args.root_data)

    model = utils.init_model(args.path, dataset)

    # update parameters of loaded models
    model.hparams["suppress_person"] = args.suppress_person
    model.hparams["inference"] = True

    return model, dataset

def eval_masks(args, model, dataset, root):
    """Evaluate masks to produce mAP (and PSNR) scores."""
    root = os.path.join(root, "masks")
    os.makedirs(root)

    maskloader = MaskLoader(dataset=dataset)

    image_ids = evaluation.utils.sample_linear(
        dataset.img_ids_test, args.masks_n_samples
    )[0]

    results = evaluation.evaluate(
        dataset,
        model,
        maskloader,
        vis_i=1,
        save_dir=root,
        save=True,
        vid=args.vid,
        image_ids=image_ids,
    )

def eval_masks_average(args):
    """Calculate average of `eval_masks` results for all 10 scenes."""
    scores = []
    for vid in VIDEO_IDS:
        path_metrics = os.path.join("results", args.exp, vid, 'masks', 'metrics.txt')
        with open(f'results/rel/{vid}/masks/metrics.txt') as f:
            lines = f.readlines()
            score_map, score_psnr = [float(s) for s in lines[2].split('\t')[:2]]
            scores.append([score_map, score_psnr])
    scores = np.array(scores).mean(axis=0)
    print('Average for all 10 scenes:')
    print(f'mAP: {(scores[0]*100).round(2)}, PSNR: {scores[1].round(2)}')

def render_video(args, model, dataset, root, save_cache=False):
    """Render a summary video like shown on the project page."""
    root = os.path.join(root, "summary")
    os.makedirs(root)

    sid = SAMPLE_IDS[args.vid]

    top = evaluation.video.render(
        dataset, model, n_images=args.summary_n_samples
    )
    bot = evaluation.video.render(
        dataset, model, sid, n_images=args.summary_n_samples
    )

    if save_cache:
        evaluation.video.save_to_cache(
            args.vid, sid, root=root, top=top, bot=bot
        )

    ims_cat = [
        evaluation.video.convert_rgb(
            evaluation.video.cat_sample(top[k], bot[k])
        )
        for k in bot.keys()
    ]

    utils.write_mp4(f"{root}/cat-{sid}-N{len(ims_cat)}", ims_cat)

def run(args, model, dataset, root):

    if "masks" in args.outputs:
        # segmentations and renderings with mAP and PSNR
        eval_masks(args, model, dataset, root)

    if "summary" in args.outputs:
        # summary video
        render_video(args, model, dataset, root)

if __name__ == "__main__":
    args = parse_args()
    if 'average' in args.outputs:
        # calculate average over all 10 scenes for specific experiment
        eval_masks_average(args)
    else:
        model, dataset = init(args)
        root = os.path.join("results", args.exp, args.vid)
        run(args, model, dataset, root)

max_stars_repo_name: dichotomies/NeuralDiff

max_issues_repo_name: yashbhalgat/NeuralDiff

max_forks_repo_name: dichotomies/NeuralDiff


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This script contains all functions for the OtherClinical workflow.

Created on Mon Feb  3 10:38:51 2020
@author: Melanie Schneider
"""

# import io
# import json
# import re
# import time
# import zipfile
# import argparse
# import datetime
import os
import sys

# import mygene
import numpy as np
import pandas as pd
import pkg_resources
from sqlalchemy import create_engine

PY3 = sys.version > '3'
if PY3:
    import urllib.request as urllib2
else:
    import urllib2

from ot_tractability_pipeline_v2.queries_othercl import *

DATA_PATH = pkg_resources.resource_filename('ot_tractability_pipeline_v2', 'data/')

class Othercl_buckets(object):
    '''
    Class for assigning genes to tractability buckets
    '''

    ##############################################################################################################
    #
    # Initial setup
    #
    #
    ##############################################################################################################

    def __init__(self, Pipeline_setup, database_url=None, prev_output=None):

        # Cross referencing from Pipeline_setup, prevents repetition for antibody ot_tractability_pipeline
        self.store_fetched = Pipeline_setup.store_fetched

        # list of ensembl IDs for targets to be considered
        self.gene_list = Pipeline_setup.gene_list

        # id_xref dataframe
        self.id_xref = Pipeline_setup.id_xref

        # ChEMBL DB connection
        #self.engine = Pipeline_setup.engine
        if database_url is None:
            database_url = os.getenv('CHEMBL_DB')
        # ChEMBL DB connection - reestablish connection to avoid timeout
        self.engine = create_engine(database_url)

        # All chembl data loaded into here
        self.all_chembl_targets = None

        # Unique list of PDB codes:
        self.pdb_list = []

        # Map back to Uniprot accession
        self.pdb_map = {}

        # If antibody results are to be combined with small molecule results, append antibody columns to sm results
        # Otherwise, use the id_xref dataframe
        if prev_output is not None:
            go_data = self.id_xref[['ensembl_gene_id', 'go.CC']]
            self.out_df = prev_output.merge(go_data, how='outer', on='ensembl_gene_id')

        else:
            self.out_df = self.id_xref

    ##############################################################################################################
    #
    # Functions relating to buckets 1-3
    # Clinical antibodies
    #
    ##############################################################################################################

    def _process_protein_complexes(self):
        '''
        For protein complexes, see if we know the binding subunit, and only keep these

        :return:
        '''

        # print("\t\t- Processing protein complexes...")
        pc = self.all_chembl_targets[self.all_chembl_targets['target_type'].str.contains("PROTEIN COMPLEX")]
        not_pc = self.all_chembl_targets[~self.all_chembl_targets['target_type'].str.contains("PROTEIN COMPLEX")]

        defined = pc[pc['site_id'].notnull()]
        undefined = pc[~pc['site_id'].notnull()]

        # if binding site is defined, only take the subunits that are involved in the binding

        n = 1000
        targets = defined['site_id'].unique()
        chunks = [targets[i:i + n] for i in range(0, len(targets), n)]
        df_list2 = []

        for chunk in chunks:
            q2 = '''
            select distinct cs.accession, sc.component_id
            from {0}.component_sequences cs,
                {0}.site_components sc
            where cs.component_id = sc.component_id
            and sc.site_id in {1}'''.format(CHEMBL_VERSION, tuple(chunk))
            df_list2.append(pd.read_sql_query(q2, self.engine))

        binding_subunit = pd.concat(df_list2, sort=False)

        if self.store_fetched:
            binding_subunit.to_csv("{}/othercl_chembl_binding_subunit.csv".format(self.store_fetched))

        temp_pc = pc.merge(binding_subunit, on='accession')
        binding_subunits = temp_pc[temp_pc['component_id'].notnull()]

        self.all_chembl_targets = pd.concat([binding_subunits, undefined, not_pc], sort=False)

    def _assign_buckets_1_to_3(self):
        '''
        Merge the results of the ChEMBL search with the OT data (right join, to keep all OT targets)
        Group activity data by target, assign the Max Phase for each targets, and use it to assign buckets 1 to 3
        Possible duplication of effort of the OT known_drug score

        :return:
        '''

        print("\t- Assessing clinical buckets 1-3...")

        self.all_chembl_targets = pd.read_sql_query(chembl_clinical_other_targets, self.engine)
        self.all_chembl_targets.loc[self.all_chembl_targets['ref_type'] == 'Expert', ['ref_id', 'ref_url']] = 'NA'

        self._process_protein_complexes()

        othercl_info = pd.read_sql_query(chembl_clinical_other, self.engine)
        self.all_chembl_targets = self.all_chembl_targets.merge(othercl_info, how='left', on='parent_molregno')

        if self.store_fetched:
            self.all_chembl_targets.to_csv("{}/othercl_all_chembl_targets.csv".format(self.store_fetched))

#        # Make sure max phase is for correct indication
#        self.all_chembl_targets = self.all_chembl_targets[
#            self.all_chembl_targets['max_phase'] == self.all_chembl_targets['max_phase_for_ind']]

        # pre-processing groupby on two columns to get highest max_phase by drug (and target)
        f0 = {x: 'first' for x in self.all_chembl_targets.columns if x not in ['accession','drug_chembl_id']}
        f0['max_phase_for_ind'] = 'max'
        f0['max_phase'] = 'max'

        self.all_chembl_targets = self.all_chembl_targets.groupby(['accession','drug_chembl_id'], as_index=False).agg(f0)

        def set_as_tuple(x):
            return tuple(x)

        def set_strings(x):
            ''' concatenate in string and include only if it is a string (not nan), and exists '''
            return ",".join([y for y in x if isinstance(y,str) and y])

        def set_strings2(x):
            ''' concatenate in string and include only if it is a string (not nan), and exists '''
            return " | ".join([y for y in x if isinstance(y,str) and y])

        # copy 'max_phase' column to 'clinical_phase', but first convert to integer (from float), then to string and replace string nan by real nan (that it can correctly be detected during aggregation)
        self.all_chembl_targets['clinical_phase'] = self.all_chembl_targets['max_phase'].fillna(-1).astype(int).astype(str).replace('-1',np.nan)

        f = {x: set_as_tuple for x in self.all_chembl_targets if x != 'accession'}
        f['max_phase_for_ind'] = 'max'
        f['max_phase'] = 'max'
        f['drug_chembl_id'] = set_strings
        f['drug_name'] = set_strings2
        f['clinical_phase'] = set_strings

        self.all_chembl_targets = self.all_chembl_targets.groupby('accession', as_index=False).agg(f)

        self.out_df = self.all_chembl_targets.merge(self.out_df, how='outer', on='accession', suffixes=('', '_othercl'))

        self.out_df.drop(['component_id', 'ref_id', 'ref_type', 'tid',
                          'ref_url'], axis=1, inplace=True)

        # self.out_df.to_csv("{}/othercl_out_df_checkpoint1.csv".format(self.store_fetched), index=False)

        f = {x: 'first' for x in self.out_df.columns if x != 'ensembl_gene_id'}
        f['max_phase_for_ind'] = 'max'
        f['max_phase'] = 'max'
        f['drug_chembl_id'] = set_strings
        f['drug_name'] = set_strings2
        f['clinical_phase'] = set_strings

        self.out_df = self.out_df.groupby(['ensembl_gene_id'], as_index=False).agg(f)

        # self.out_df.to_csv("{}/othercl_out_df_checkpoint2.csv".format(self.store_fetched), index=False)

        self.out_df.rename(columns = {'drug_chembl_id':'drug_chembl_ids_othercl',
                                      'drug_name':'drug_names_othercl',
                                      'clinical_phase':'clinical_phases_othercl'}, inplace = True)

        self.out_df['Bucket_1_othercl'] = 0
        self.out_df['Bucket_2_othercl'] = 0
        self.out_df['Bucket_3_othercl'] = 0

        self.out_df.loc[(self.out_df['max_phase'] == 4), 'Bucket_1_othercl'] = 1
        self.out_df.loc[
            (self.out_df['max_phase'] < 4) & (self.out_df['max_phase'] >= 2), 'Bucket_2_othercl'] = 1
        self.out_df.loc[
            (self.out_df['max_phase'] < 2) & (self.out_df['max_phase'] > 0), 'Bucket_3_othercl'] = 1

    ##############################################################################################################
    #
    # Higher level functions relating to the overall process
    #
    #
    ##############################################################################################################

    def _clinical_precedence(self, s):
        return 1 * s['Bucket_1_othercl'] + 0.7 * s['Bucket_2_othercl'] + 0.2 * s['Bucket_3_othercl']

    def _summarise_buckets(self):

        print("\t- Summarising buckets...")

        self.out_df.drop('go.CC', inplace=True, axis=1)

        self.out_df['Top_bucket_othercl'] = 10
        for x in range(3, 0, -1):
            self.out_df.loc[(self.out_df['Bucket_{}_othercl'.format(x)] == 1), 'Top_bucket_othercl'] = x
            self.out_df['Bucket_{}_othercl'.format(x)].fillna(0, inplace=True)

        self.out_df['Bucket_sum_othercl'] = self.out_df['Bucket_1_othercl'] + self.out_df['Bucket_2_othercl'] + self.out_df['Bucket_3_othercl']

        self.out_df.set_index('ensembl_gene_id')

    def assign_buckets(self):
        '''
        Assigns the supplied list of gene IDs into their corresponding tractability buckets.
        :return: A Pandas DataFrame containing the Ensembl gene ID and associated tractability bucket
        '''

        self._assign_buckets_1_to_3()
        print(self.out_df.columns)

        self._summarise_buckets()
        print(self.out_df.columns)

        # try:

        self.out_df.index = self.out_df['ensembl_gene_id']

        # drop rows without gene symbol
        self.out_df = self.out_df.dropna(subset=['symbol'])
        # drop rows without accession
        self.out_df = self.out_df.dropna(subset=['accession'])

        # Columns to keep
        self.out_df = self.out_df[['accession', 'symbol',
                                   # 'Bucket_1_sm', 'Bucket_2_sm', 'Bucket_3_sm',
                                   # 'Bucket_4_sm', 'Bucket_5_sm', 'Bucket_6_sm', 'Bucket_7_sm',
                                   # 'Bucket_8_sm', 'Bucket_sum_sm', 'Top_bucket_sm', 'Category_sm',
                                   # 'Clinical_Precedence_sm', 'Discovery_Precedence_sm', 'Predicted_Tractable_sm',
                                   # 'PDB_Known_Ligand',
                                   # 'ensemble', 'canonical_smiles', 'small_mol_druggable',
                                   # 'ensemble', 'High_Quality_ChEMBL_compounds',
                                   # 'Small_Molecule_Druggable_Genome_Member',
                                   # 'Bucket_1_ab', 'Bucket_2_ab', 'Bucket_3_ab', 'Bucket_4_ab',
                                   # 'Bucket_5_ab', 'Bucket_6_ab', 'Bucket_7_ab',
                                   # 'Bucket_8_ab', 'Bucket_9_ab', 'Bucket_sum_ab', 'Top_bucket_ab',
                                   # 'Clinical_Precedence_ab', 'Predicted_Tractable_ab_High_confidence', 'Predicted_Tractable_ab_Medium_to_low_confidence', 'Category_ab',
                                   # 'Uniprot_high_conf_loc', 'GO_high_conf_loc',
                                   # 'Uniprot_med_conf_loc',
                                   # 'GO_med_conf_loc', 'Transmembrane', 'Signal_peptide', 'HPA_main_location',
                                   # 'Bucket_1_PROTAC', 'Bucket_2_PROTAC', 'Bucket_3_PROTAC', 'Bucket_4_PROTAC',
                                   # 'Bucket_5_PROTAC', 'Bucket_6_PROTAC', 'Bucket_7_PROTAC',
                                   # 'Bucket_8_PROTAC', 'Bucket_9_PROTAC', 'Bucket_sum_PROTAC', 'Top_bucket_PROTAC',
                                   # 'Bcell_mean', 'NKcell_mean', 'Hepatocytes_mean', 'MouseNeuorons_mean',
                                   # 'Max_halflife',
                                   # 'number_of_ubiquitination_sites',
                                   # 'full_id', 'PROTAC_location_Bucket',
                                   'Bucket_1_othercl', 'Bucket_2_othercl', 'Bucket_3_othercl',
                                   'Bucket_sum_othercl', 'Top_bucket_othercl',
                                   'drug_chembl_ids_othercl', 'drug_names_othercl', 'clinical_phases_othercl'
                                   ]]

        # self.out_df.sort_values(['Clinical_Precedence', 'Discovery_Precedence', 'Predicted_Tractable'],
        #                         ascending=[False, False, False], inplace=True)

        # Score each category, and label highest category
        self.out_df['Clinical_Precedence_othercl'] = self.out_df.apply(self._clinical_precedence, axis=1)

        self.out_df['Category_othercl'] = 'Unknown'
        self.out_df.loc[(self.out_df['Top_bucket_othercl'] <= 3), 'Category_othercl'] = 'Clinical_Precedence_othercl'
        # self.out_df.loc[(self.out_df['Top_bucket_othercl'] == 4) | (self.out_df['Top_bucket_othercl'] == 5),
        #                 'Category_othercl'] = 'Predicted_Tractable__High_confidence'
        # self.out_df.loc[(self.out_df['Top_bucket_othercl'] == 6) | (self.out_df['Top_bucket_othercl'] == 7) | (
        #     self.out_df['Top_bucket_othercl'] == 8) | (self.out_df['Top_bucket_othercl'] == 9),
        #     'Category_othercl'] = 'Predicted_Tractable__Medium_to_low_confidence'

        # self.out_df = self.out_df[(self.out_df['Top_bucket'] < 9 ) | (self.out_df['Top_bucket_ab'] < 10) | (self.out_df['Top_bucket_othercl'] < 10) ]

        # Cleaning columns
        self.out_df['drug_chembl_ids_othercl'].fillna('', inplace=True)
        self.out_df['drug_names_othercl'].fillna('', inplace=True)
        self.out_df['clinical_phases_othercl'].fillna('', inplace=True)

        # create dictionaries from 'drug_chembl_ids_' and 'clinical_phases_'/'drug_names_'
        self.out_df['drug_names_dict_othercl'] = self.out_df.apply(lambda row : dict(zip(row['drug_chembl_ids_othercl'].split(","), row['drug_names_othercl'].split(" | "))), axis=1)
        self.out_df['clinical_phases_dict_othercl'] = self.out_df.apply(lambda row : dict(zip(row['drug_chembl_ids_othercl'].split(","), row['clinical_phases_othercl'].split(","))), axis=1)

        # Cleaning column: setting selected culumns in list format to improve visualization e.g. with Excel
        # and remove duplicates while keeping order using "list(dict.fromkeys(lst))"
        self.out_df['drug_chembl_ids_othercl'] = self.out_df['drug_chembl_ids_othercl'].apply(lambda x: list(dict.fromkeys(x.split(","))))
        self.out_df['drug_names_othercl'] = self.out_df['drug_names_othercl'].apply(lambda x: list(dict.fromkeys(x.split(" | "))))

        print(self.out_df.columns)

        return self.out_df.astype({x: 'int64' for x in self.out_df.columns if "Bucket" in x})

    @staticmethod
    def othercl2json(d):
        return {
            'Bucket_scores': {'Bucket_1_othercl':d.Bucket_1_othercl, 'Bucket_2_othercl':d.Bucket_2_othercl, 'Bucket_3_othercl':d.Bucket_3_othercl},
            'Bucket_evaluation': {'Bucket_sum_othercl':d.Bucket_sum_othercl, 'Top_bucket_othercl':d.Top_bucket_othercl},
            'Category_scores': {'Clinical_Precedence_othercl':d.Clinical_Precedence_othercl},
            'Category_evaluation': {'Top_Category_othercl':d.Category_othercl},
#            'Bucket_evidences': {'Bucket_1_2_3_othercl': {'drug_chembl_ids_othercl':d.drug_chembl_ids_othercl, 'drug_names_othercl':d.drug_names_othercl}}
            'Bucket_evidences': {'Bucket_1_2_3_othercl': {'drugs_in_clinic':d.drug_names_dict_othercl, 'max_clinical_phase':d.clinical_phases_dict_othercl}}
            }


text:
import io
import numpy as np
from PIL import Image

def pil_to_byte(pil_image):
    if not Image.isImageType(pil_image):
        raise Exception("Image must be of type '" + str(Image) + "'!")
    img_byte_arr = io.BytesIO()
    pil_image.save(img_byte_arr, format=pil_image.format)
    return img_byte_arr.getvalue()

def pil_to_numpy(pil_image):
    if not Image.isImageType(pil_image):
        raise Exception("Image must be of type '" + str(Image) + "'!")
    return np.array(pil_image)

def byte_to_pil(image_bytes):
    return Image.open(io.BytesIO(image_bytes))

def byte_to_numpy(image_bytes):
    return pil_to_numpy(byte_to_pil(image_bytes))

def numpy_to_pil(numpy_arr, mode=None):
    return Image.fromarray(numpy_arr, mode)

def numpy_to_byte(numpy_arr):
    return pil_to_byte(numpy_to_pil(numpy_arr))


text:
import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
import lightgbm as lgb
import gc
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt

# Basic lightGBM model for testing dataset
def basic_lgb_model(train, test, y_train, n_folds = 5):

    """Train and test a light gradient boosting model using
    cross validation.

    Parameters
    --------
        features (pd.DataFrame):
            dataframe of training features to use
            for training a model. Must include the TARGET column.
        test_features (pd.DataFrame):
            dataframe of testing features to use
            for making predictions with the model.
        encoding (str, default = 'ohe'):
            method for encoding categorical variables. Either 'ohe' for one-hot encoding or 'le' for integer label encoding
            n_folds (int, default = 5): number of folds to use for cross validation

    Return
    --------
        submission (pd.DataFrame):
            dataframe with `Id` and `TARGET_5Yrs` probabilities
            predicted by the model.
        feature_importances (pd.DataFrame):
            dataframe with the feature importances from the model.
        valid_metrics (pd.DataFrame):
            dataframe with training and validation metrics (ROC AUC) for each fold and overall.

    """

    # Extract the labels for training
    labels = y_train

    features = train
    test_features = test

    print('Training Data Shape: ', features.shape)
    print('Testing Data Shape: ', test_features.shape)

    # Extract feature names
    feature_names = list(features.columns)

    # Convert to np arrays
    features = np.array(features)
    test_features = np.array(test_features)

    # Create the kfold object
    k_fold = KFold(n_splits = n_folds, shuffle = True, random_state = 50)

    # Empty array for feature importances
    feature_importance_values = np.zeros(len(feature_names))

    # Empty array for test predictions
    test_predictions = np.zeros(test_features.shape[0])

    # Empty array for out of fold validation predictions
    out_of_fold = np.zeros(features.shape[0])

    # Lists for recording validation and training scores
    valid_scores = []
    train_scores = []

    # Iterate through each fold
    for train_indices, valid_indices in k_fold.split(features):

        # Training data for the fold
        train_features, train_labels = features[train_indices], labels[train_indices]
        # Validation data for the fold
        valid_features, valid_labels = features[valid_indices], labels[valid_indices]

        # Create the model
        model = lgb.LGBMClassifier(n_estimators=10000, objective = 'binary', boosting_type='goss',
                                   class_weight = 'balanced', learning_rate = 0.05,
                                   reg_alpha = 0.1, reg_lambda = 0.1, n_jobs = -1, random_state = 50)

        # Train the model
        model.fit(train_features, train_labels, eval_metric = 'auc',
                  eval_set = [(valid_features, valid_labels), (train_features, train_labels)],
                  eval_names = ['valid', 'train'],
                  early_stopping_rounds = 100, verbose = 200)

        # Record the best iteration
        best_iteration = model.best_iteration_

        # Record the feature importances
        feature_importance_values += model.feature_importances_ / k_fold.n_splits

        # Make predictions
        test_predictions += model.predict_proba(test_features, num_iteration = best_iteration)[:, 1] / k_fold.n_splits

        # Record the out of fold predictions
        out_of_fold[valid_indices] = model.predict_proba(valid_features, num_iteration = best_iteration)[:, 1]

        # Record the best score
        valid_score = model.best_score_['valid']['auc']
        train_score = model.best_score_['train']['auc']

        valid_scores.append(valid_score)
        train_scores.append(train_score)

        # Clean up memory
        gc.enable()
        del model, train_features, valid_features
        gc.collect()

    # Make the feature importance dataframe
    feature_importances = pd.DataFrame({'feature': list(feature_names), 'importance': feature_importance_values}).sort_values('importance', ascending = False)

    # Find the features with zero importance
    zero_features = list(feature_importances[feature_importances['importance'] == 0.0]['feature'])

    # Overall validation score
    valid_auc = roc_auc_score(labels, out_of_fold)

    # Add the overall scores to the metrics
    valid_scores.append(valid_auc)
    train_scores.append(np.mean(train_scores))

    # Needed for creating dataframe of validation scores
    fold_names = list(range(n_folds))
    fold_names.append('overall')

    # Dataframe of validation scores
    metrics = pd.DataFrame({'fold': fold_names,
                            'train': train_scores,
                            'valid': valid_scores})

    return feature_importances, zero_features, metrics

def plot_feature_importances(df, threshold = 0.9):
    """
    Plots 15 most important features and the cumulative importance of features.
    Prints the number of features needed to reach threshold cumulative importance.

    Parameters
    --------
    df : dataframe
        Dataframe of feature importances. Columns must be feature and importance
    threshold : float, default = 0.9
        Threshold for prining information about cumulative importances

    Return
    --------
    df : dataframe
        Dataframe ordered by feature importances with a normalized column (sums to 1)
        and a cumulative importance column

    """

    plt.rcParams['font.size'] = 18

    # Sort features according to importance
    df = df.sort_values('importance', ascending = False).reset_index()

    # Normalize the feature importances to add up to one
    df['importance_normalized'] = df['importance'] / df['importance'].sum()
    df['cumulative_importance'] = np.cumsum(df['importance_normalized'])

    # Make a horizontal bar chart of feature importances
    plt.figure(figsize = (10, 6))
    ax = plt.subplot()

    # Need to reverse the index to plot most important on top
    ax.barh(list(reversed(list(df.index[:15]))),
            df['importance_normalized'].head(15),
            align = 'center', edgecolor = 'k')

    # Set the yticks and labels
    ax.set_yticks(list(reversed(list(df.index[:15]))))
    ax.set_yticklabels(df['feature'].head(15))

    # Plot labeling
    plt.xlabel('Normalized Importance'); plt.title('Feature Importances')
    plt.show()

    # Cumulative importance plot
    plt.figure(figsize = (8, 6))
    plt.plot(list(range(len(df))), df['cumulative_importance'], 'r-')
    plt.xlabel('Number of Features'); plt.ylabel('Cumulative Importance');
    plt.title('Cumulative Feature Importance');
    plt.show();

    importance_index = np.min(np.where(df['cumulative_importance'] > threshold))
    print('%d features required for %0.2f of cumulative importance' % (importance_index + 1, threshold))

    return df


text:
import streamlit as st
import pandas as pd
from pyvis import network as net
from IPython.core.display import display, HTML
import numpy as np
import streamlit.components.v1 as components
import networkx as nx
from bokeh.io import output_file, show
from bokeh.plotting import figure, from_networkx

@st.cache
def get_graph(path):
    df = pd.read_csv(path)
    D = nx.convert_matrix.from_pandas_edgelist(df,'Source','Target',['Weight'],nx.DiGraph)
    G = nx.DiGraph.to_undirected(D)
    return G

def app():
    st.title("Network Graph for AWCA")

    f = get_graph("BERT_edge_list2.csv")

    plot = figure(title="Networkx Integration Demonstration", x_range=(-1.1,1.1), y_range=(-1.1,1.1),
              tools="", toolbar_location=None)

    graph = from_networkx(f, nx.spring_layout, scale=2, center=(0, 0))

    plot.renderers.append(graph)

    output_file("networkx_graph.html")
    show(plot)
    """
    HtmlFile = open("example.html", 'r', encoding='utf-8')
    source_code = HtmlFile.read()
    components.html(source_code, height=400,width=600)
    """
    st.write("rendering done!")

max_issues_repo_name: UmbraVenus/streamlit

max_forks_repo_name: UmbraVenus/streamlit

reason: import numpy,import networkx


text:
import json
import os
import numpy as np
from pathlib import Path

def generate_eval_jobs(train_dir, out_dir,
                       num_steps=None,
                       num_episodes=None,
                       est_hesh=False,
                       est_grad=False,
                       calc_hesh=False,
                       calc_grad=False,
                       batch_grad=False,
                       device="cpu",
                       checkpoint=None):
    assert not (est_hesh and calc_hesh), "calculating and estimating hessian cannot happen at the same time"
    assert num_steps is not None or num_episodes is not None, "one of num_steps or num_episodes must be specified"
    if num_steps is None:
        num_steps = 10000000000000
    if num_episodes is None:
        num_episodes = 10000000000000

    train_dir = Path(train_dir)
    out_dir = Path(out_dir)
    # TODO: Should we set exist_ok=True?
    os.makedirs(out_dir / "results")

    info_fname = "info.json"
    info = json.load(open((train_dir / info_fname)))

    info['num_episodes'] = num_episodes
    info['num_steps'] = num_steps
    info['est_hesh'] = est_hesh
    info['est_grad'] = est_grad
    info['calc_hesh'] = calc_hesh
    info['calc_grad'] = calc_grad
    info['batch_grad'] = batch_grad

    json.dump(info, open((out_dir / info_fname), 'w'), indent=4)

    checkpoints = [folder for folder in os.listdir(train_dir) if os.path.isdir(train_dir / folder) and folder.isdigit()]
    checkpoints = sorted(checkpoints)

    # Limit to 33 checkpoints for line plots
    if len(checkpoints) > 33:
        print(f"Selecting 33 checkpoints out of {len(checkpoints)}")
        idx = list(np.round(np.linspace(0, len(checkpoints) - 1, 33)).astype(int))
        checkpoints = [checkpoints[i] for i in idx]

    # Always eval best checkpoint
    if "best" in os.listdir(train_dir) and os.path.isdir(train_dir / "best"):
        checkpoints.append("best")

    all_jobs = []
    if checkpoint:
        job = f"python3 -m reward_surfaces.bin.eval_tradj {train_dir} {checkpoint} {out_dir} --device={device}"
        all_jobs.append(job)
    else:
        for checkpt in checkpoints:
            job = f"python3 -m reward_surfaces.bin.eval_tradj {train_dir} {checkpt} {out_dir} --device={device}"
            all_jobs.append(job)

    jobs = "\n".join(all_jobs)+"\n"
    open((out_dir / "jobs.sh"), 'w').write(jobs)

max_stars_repo_name: SonSang/reward-surfaces

max_issues_repo_name: SonSang/reward-surfaces

max_forks_repo_name: SonSang/reward-surfaces


text:
import networkx as nx
from load_data import *
from compute_metrics_multiplex import *
from compute_network_statistics import *

def getGranovetterForDataset(dataset="wan"):
    graphs,node_mapping = load_4_layers(dataset)
    strong = graphs[6]
    weak = graphs[7]
    all = graphs[8]
    c_total_s_wedges = 0.0
    c_s_wedges_by_s = 0.0
    c_s_wedges_by_w = 0.0
    c_total_w_wedges = 0.0
    c_w_wedges_by_s = 0.0
    c_w_wedges_by_w = 0.0
    for i in range(strong.number_of_nodes()):
        for j in range(strong.number_of_nodes()):
            for k in range(strong.number_of_nodes()):
                if strong.has_edge(i,j) and strong.has_edge(j,k):
                    c_total_s_wedges += 1.0
                    if strong.has_edge(i,k) or strong.has_edge(k,i):
                        c_s_wedges_by_s += 1.0
                    if weak.has_edge(i,k) or weak.has_edge(k,i):
                        c_s_wedges_by_w += 1.0

                if weak.has_edge(i,j) and weak.has_edge(j,k):
                    c_total_w_wedges += 1.0
                    if strong.has_edge(i,k) or strong.has_edge(k,i):
                        c_w_wedges_by_s += 1.0
                    if weak.has_edge(i,k) or weak.has_edge(k,i):
                        c_w_wedges_by_w += 1.0

    c_s_wedges_by_a = c_s_wedges_by_s+c_s_wedges_by_w
    c_w_wedges_by_a = c_w_wedges_by_s+c_w_wedges_by_w
    return ("There are a total of {:.0f} strong wedges out of which:\n"+\
           "\t{:.3f} percent are closed by another strong link\n"+\
           "\t{:.3f} are closed by a weak link\n"+\
           "\t{:.3f} are closed by any link\n").format(c_total_s_wedges,c_s_wedges_by_s*100/c_total_s_wedges,c_s_wedges_by_w*100/c_total_s_wedges,c_s_wedges_by_a*100/c_total_s_wedges)+\
           ("\n\nThere are a total of {:.0f} weak wedges out of which:\n"+\
           "\t{:.3f} percent are closed by a strong link\n"+\
           "\t{:.3f} are closed by another weak link\n"+\
           "\t{:.3f} are closed by any link\n").format(c_total_w_wedges,c_w_wedges_by_s*100/c_total_w_wedges,c_w_wedges_by_w*100/c_total_w_wedges,c_w_wedges_by_a*100/c_total_w_wedges)

def getGranovetterForDataset1(dataset="wan"):
    graphs,node_mapping = load_4_layers(dataset)
    strong = graphs[6]
    weak = graphs[7]
    all = graphs[8]
    c_total_s_wedges = 0.0
    c_s_wedges_by_s = 0.0
    c_s_wedges_by_w = 0.0
    c_total_w_wedges = 0.0
    c_w_wedges_by_s = 0.0
    c_w_wedges_by_w = 0.0
    for i in range(strong.number_of_nodes()):
        for j in range(strong.number_of_nodes()):
            for k in range(strong.number_of_nodes()):
                if strong.has_edge(i,j) and strong.has_edge(i,k):
                    c_total_s_wedges += 1.0
                    if strong.has_edge(j,k) or strong.has_edge(k,j):
                        c_s_wedges_by_s += 1.0
                    if weak.has_edge(j,k) or weak.has_edge(k,j):
                        c_s_wedges_by_w += 1.0

                if weak.has_edge(i,j) and weak.has_edge(i,k):
                    c_total_w_wedges += 1.0
                    if strong.has_edge(j,k) or strong.has_edge(k,j):
                        c_w_wedges_by_s += 1.0
                    if weak.has_edge(j,k) or weak.has_edge(k,j):
                        c_w_wedges_by_w += 1.0

    c_s_wedges_by_a = c_s_wedges_by_s+c_s_wedges_by_w
    c_w_wedges_by_a = c_w_wedges_by_s+c_w_wedges_by_w
    return ("There are a total of {:.0f} strong wedges out of which:\n"+\
           "\t{:.3f} percent are closed by another strong link\n"+\
           "\t{:.3f} are closed by a weak link\n"+\
           "\t{:.3f} are closed by any link\n").format(c_total_s_wedges,c_s_wedges_by_s*100/c_total_s_wedges,c_s_wedges_by_w*100/c_total_s_wedges,c_s_wedges_by_a*100/c_total_s_wedges)+\
           ("\n\nThere are a total of {:.0f} weak wedges out of which:\n"+\
           "\t{:.3f} percent are closed by a strong link\n"+\
           "\t{:.3f} are closed by another weak link\n"+\
           "\t{:.3f} are closed by any link\n").format(c_total_w_wedges,c_w_wedges_by_s*100/c_total_w_wedges,c_w_wedges_by_w*100/c_total_w_wedges,c_w_wedges_by_a*100/c_total_w_wedges)

def main():
    path = """..\\results\\novi rezultati\\1_"""
    txt_file = """granovveter.txt"""

    with open(path+"wan_"+txt_file,"w") as pout:
        pout.write(getGranovetterForDataset1("wan"))
    with open(path+"bms_"+txt_file,"w") as pout:
        pout.write(getGranovetterForDataset1("bms"))

if __name__ == "__main__":
    main()

max_stars_repo_path: python code/granovetter.py

max_stars_repo_name: gajduk/social-networks-analysis-wan-bms

max_issues_repo_path: python code/granovetter.py

max_issues_repo_name: gajduk/social-networks-analysis-wan-bms

max_forks_repo_path: python code/granovetter.py

max_forks_repo_name: gajduk/social-networks-analysis-wan-bms

reason: import networkx


text:
import inspect
from typing import Dict, List, Optional, Tuple
import torch
from torch import nn

from detectron2.config import configurable
from detectron2.layers import ShapeSpec
from detectron2.structures import Boxes, ImageList, Instances

from detectron2.modeling.poolers import ROIPooler
from detectron2.modeling.roi_heads.box_head import build_box_head
from detectron2.modeling.roi_heads.fast_rcnn import FastRCNNOutputLayers
from detectron2.modeling.roi_heads.keypoint_head import build_keypoint_head
from detectron2.modeling.roi_heads.mask_head import build_mask_head
from projects.ipalm.ipalm.material_head import build_material_head
from detectron2.modeling.roi_heads.roi_heads import StandardROIHeads, ROIHeads, ROI_HEADS_REGISTRY, select_foreground_proposals, select_proposals_with_visible_keypoints

import numpy as np

@ROI_HEADS_REGISTRY.register()
class IpalmROIHeads(ROIHeads):
    """
    It's "standard" in a sense that there is no ROI transform sharing
    or feature sharing between tasks.
    Each head independently processes the input features by each head's
    own pooler and head.

    This class is used by most models, such as FPN and C5.
    To implement more models, you can subclass it and implement a different
    :meth:`forward()` or a head.
    """

    @configurable
    def __init__(
        self,
        *,
        box_in_features: List[str],
        box_pooler: ROIPooler,
        box_head: nn.Module,
        box_predictor: nn.Module,
        mask_in_features: Optional[List[str]] = None,
        mask_pooler: Optional[ROIPooler] = None,
        mask_head: Optional[nn.Module] = None,
        keypoint_in_features: Optional[List[str]] = None,
        keypoint_pooler: Optional[ROIPooler] = None,
        keypoint_head: Optional[nn.Module] = None,
        material_in_features: Optional[List[str]] = None,
        material_pooler: Optional[ROIPooler] = None,
        material_head: Optional[nn.Module] = None,  # TODO build the heads correctly
        train_on_pred_boxes: bool = False,
        **kwargs,
    ):
        """
        NOTE: this interface is experimental.

        Args:
            box_in_features (list[str]): list of feature names to use for the box head.
            box_pooler (ROIPooler): pooler to extra region features for box head
            box_head (nn.Module): transform features to make box predictions
            box_predictor (nn.Module): make box predictions from the feature.
                Should have the same interface as :class:`FastRCNNOutputLayers`.
            mask_in_features (list[str]): list of feature names to use for the mask
                pooler or mask head. None if not using mask head.
            mask_pooler (ROIPooler): pooler to extract region features from image features.
                The mask head will then take region features to make predictions.
                If None, the mask head will directly take the dict of image features
                defined by `mask_in_features`
            mask_head (nn.Module): transform features to make mask predictions
            keypoint_in_features, keypoint_pooler, keypoint_head: similar to ``mask_*``.
            train_on_pred_boxes (bool): whether to use proposal boxes or
                predicted boxes from the box head to train other heads.
        """
        super().__init__(**kwargs)
        # keep self.in_features for backward compatibility
        self.in_features = self.box_in_features = box_in_features
        self.box_pooler = box_pooler
        self.box_head = box_head
        self.box_predictor = box_predictor

        self.mask_on = mask_in_features is not None
        if self.mask_on:
            self.mask_in_features = mask_in_features
            self.mask_pooler = mask_pooler
            self.mask_head = mask_head

        """ipalm"""
        # """
        self.material_on = material_in_features is not None
        if self.material_on:
            self.material_in_features = material_in_features
            self.material_pooler = material_pooler
            self.material_head = material_head
    # """

        self.keypoint_on = keypoint_in_features is not None
        if self.keypoint_on:
            self.keypoint_in_features = keypoint_in_features
            self.keypoint_pooler = keypoint_pooler
            self.keypoint_head = keypoint_head

        self.train_on_pred_boxes = train_on_pred_boxes

    @classmethod
    def from_config(cls, cfg, input_shape):
        ret = super().from_config(cfg)
        ret["train_on_pred_boxes"] = cfg.MODEL.ROI_BOX_HEAD.TRAIN_ON_PRED_BOXES
        # Subclasses that have not been updated to use from_config style construction
        # may have overridden _init_*_head methods. In this case, those overridden methods
        # will not be classmethods and we need to avoid trying to call them here.
        # We test for this with ismethod which only returns True for bound methods of cls.
        # Such subclasses will need to handle calling their overridden _init_*_head methods.
        if inspect.ismethod(cls._init_box_head):
            ret.update(cls._init_box_head(cfg, input_shape))
        if inspect.ismethod(cls._init_mask_head):
            ret.update(cls._init_mask_head(cfg, input_shape))
        """ipalm"""
        # print(cfg.MODEL)
        # """
        if inspect.ismethod(cls._init_material_head):
            ret.update(cls._init_material_head(cfg, input_shape))
        # print("initialized material head!!!!!!!!!!!!!!!!!")
        # """
        if inspect.ismethod(cls._init_keypoint_head):
            ret.update(cls._init_keypoint_head(cfg, input_shape))
        return ret

    @classmethod
    def _init_box_head(cls, cfg, input_shape):
        # fmt: off
        in_features       = cfg.MODEL.ROI_HEADS.IN_FEATURES
        # print(in_features)
        pooler_resolution = cfg.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION
        pooler_scales     = tuple(1.0 / input_shape[k].stride for k in in_features)
        sampling_ratio    = cfg.MODEL.ROI_BOX_HEAD.POOLER_SAMPLING_RATIO
        pooler_type       = cfg.MODEL.ROI_BOX_HEAD.POOLER_TYPE
        # fmt: on

        # If StandardROIHeads is applied on multiple feature maps (as in FPN),
        # then we share the same predictors and therefore the channel counts must be the same
        in_channels = [input_shape[f].channels for f in in_features]
        # Check all channel counts are equal
        assert len(set(in_channels)) == 1, in_channels
        in_channels = in_channels[0]

        box_pooler = ROIPooler(
            output_size=pooler_resolution,
            scales=pooler_scales,
            sampling_ratio=sampling_ratio,
            pooler_type=pooler_type,
        )
        # Here we split "box head" and "box predictor", which is mainly due to historical reasons.
        # They are used together so the "box predictor" layers should be part of the "box head".
        # New subclasses of ROIHeads do not need "box predictor"s.
        box_head = build_box_head(
            cfg, ShapeSpec(channels=in_channels, height=pooler_resolution, width=pooler_resolution)
        )
        box_predictor = FastRCNNOutputLayers(cfg, box_head.output_shape)
        return {
            "box_in_features": in_features,
            "box_pooler": box_pooler,
            "box_head": box_head,
            "box_predictor": box_predictor,
        }

    @classmethod
    def _init_mask_head(cls, cfg, input_shape):
        if not cfg.MODEL.MASK_ON:
            return {}
        # fmt: off
        in_features       = cfg.MODEL.ROI_HEADS.IN_FEATURES
        pooler_resolution = cfg.MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION
        pooler_scales     = tuple(1.0 / input_shape[k].stride for k in in_features)
        sampling_ratio    = cfg.MODEL.ROI_MASK_HEAD.POOLER_SAMPLING_RATIO
        pooler_type       = cfg.MODEL.ROI_MASK_HEAD.POOLER_TYPE
        # fmt: on

        in_channels = [input_shape[f].channels for f in in_features][0]

        ret = {"mask_in_features": in_features}
        ret["mask_pooler"] = (
            ROIPooler(
                output_size=pooler_resolution,
                scales=pooler_scales,
                sampling_ratio=sampling_ratio,
                pooler_type=pooler_type,
            )
            if pooler_type
            else None
        )
        if pooler_type:
            shape = ShapeSpec(
                channels=in_channels, width=pooler_resolution, height=pooler_resolution
            )
        else:
            shape = {f: input_shape[f] for f in in_features}
        ret["mask_head"] = build_mask_head(cfg, shape)
        return ret

    """ipalm"""
    # """
    @classmethod
    def _init_material_head(cls, cfg, input_shape):
        if not cfg.MODEL.MATERIAL_ON:
            return {}
        # fmt: off
        in_features       = cfg.MODEL.ROI_HEADS.IN_FEATURES
        pooler_resolution = cfg.MODEL.ROI_MATERIAL_HEAD.POOLER_RESOLUTION
        pooler_scales     = tuple(1.0 / input_shape[k].stride for k in in_features)
        sampling_ratio    = cfg.MODEL.ROI_MATERIAL_HEAD.POOLER_SAMPLING_RATIO
        pooler_type       = cfg.MODEL.ROI_MATERIAL_HEAD.POOLER_TYPE
        # fmt: on

        in_channels = [input_shape[f].channels for f in in_features][0]

        ret = {"material_in_features": in_features}
        ret["material_pooler"] = (
            ROIPooler(
                output_size=pooler_resolution,
                scales=pooler_scales,
                sampling_ratio=sampling_ratio,
                pooler_type=pooler_type,
            )
            if pooler_type
            else None
        )
        if pooler_type:
            shape = ShapeSpec(
                channels=in_channels, width=pooler_resolution, height=pooler_resolution
            )
        else:
            shape = {f: input_shape[f] for f in in_features}
        ret["material_head"] = build_material_head(cfg, shape)
        return ret
    # """
    @classmethod
    def _init_keypoint_head(cls, cfg, input_shape):
        if not cfg.MODEL.KEYPOINT_ON:
            return {}
        # fmt: off
        in_features       = cfg.MODEL.ROI_HEADS.IN_FEATURES
        pooler_resolution = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_RESOLUTION
        pooler_scales     = tuple(1.0 / input_shape[k].stride for k in in_features)  # noqa
        sampling_ratio    = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_SAMPLING_RATIO
        pooler_type       = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_TYPE
        # fmt: on

        in_channels = [input_shape[f].channels for f in in_features][0]

        ret = {"keypoint_in_features": in_features}
        ret["keypoint_pooler"] = (
            ROIPooler(
                output_size=pooler_resolution,
                scales=pooler_scales,
                sampling_ratio=sampling_ratio,
                pooler_type=pooler_type,
            )
            if pooler_type
            else None
        )
        if pooler_type:
            shape = ShapeSpec(
                channels=in_channels, width=pooler_resolution, height=pooler_resolution
            )
        else:
            shape = {f: input_shape[f] for f in in_features}
        ret["keypoint_head"] = build_keypoint_head(cfg, shape)
        return ret

    def forward(
        self,
        images: ImageList,
        features: Dict[str, torch.Tensor],
        proposals: List[Instances],
        targets: Optional[List[Instances]] = None,
    ) -> Tuple[List[Instances], Dict[str, torch.Tensor]]:
        """
        See :class:`ROIHeads.forward`.
        """
        del images
        if self.training:
            assert targets, "'targets' argument is required during training"
            proposals = self.label_and_sample_proposals(proposals, targets)
        del targets

        if self.training:
            # print(len(features["p2"][0]))
            # with open('Failed.txt', 'w') as file:
            #     fff = []
            #     for k in features.keys():
            #         fff.append(features[k].size())
            #     file.write("Feature sizes:\n"+str(fff)+"\n")
            #     # file.write("Proposals:\n"+str(proposals))
            #     file.write("BitMasks:\n"+str(proposals[0].gt_masks.image_size))
            #     file.write("fields: "+str(proposals[0].get_fields()))
            #     # file.write("BitMasks material:\n"+str(proposals[0].gt_materials.image_size))
            # exit()
            losses = self._forward_box(features, proposals)
            # print(type(losses))
            # exit()
            # Usually the original proposals used by the box head are used by the mask, keypoint
            # heads. But when `self.train_on_pred_boxes is True`, proposals will contain boxes
            # predicted by the box head.
            losses.update(self._forward_mask(features, proposals))
            """ipalm"""
            # """
            losses.update(self._forward_material(features, proposals))
            # """
            losses.update(self._forward_keypoint(features, proposals))
            # print(features, proposals)
            print("training forward!!!!!")
            return proposals, losses
        else:
            pred_instances = self._forward_box(features, proposals)
            # During inference cascaded prediction is used: the mask and keypoints heads are only
            # applied to the top scoring box detections.
            pred_instances = self.forward_with_given_boxes(features, pred_instances)
            return pred_instances, {}

    def forward_with_given_boxes(
        self, features: Dict[str, torch.Tensor], instances: List[Instances]
    ) -> List[Instances]:
        """
        Use the given boxes in `instances` to produce other (non-box) per-ROI outputs.

        This is useful for downstream tasks where a box is known, but need to obtain
        other attributes (outputs of other heads).
        Test-time augmentation also uses this.

        Args:
            features: same as in `forward()`
            instances (list[Instances]): instances to predict other outputs. Expect the keys
                "pred_boxes" and "pred_classes" to exist.

        Returns:
            list[Instances]:
                the same `Instances` objects, with extra
                fields such as `pred_masks` or `pred_keypoints`.
        """
        assert not self.training
        assert instances[0].has("pred_boxes") and instances[0].has("pred_classes")

        instances = self._forward_mask(features, instances)
        # """
        instances = self._forward_material(features, instances)
        # """
        instances = self._forward_keypoint(features, instances)
        return instances

    def _forward_box(self, features: Dict[str, torch.Tensor], proposals: List[Instances]):
        """
        Forward logic of the box prediction branch. If `self.train_on_pred_boxes is True`,
            the function puts predicted boxes in the `proposal_boxes` field of `proposals` argument.

        Args:
            features (dict[str, Tensor]): mapping from feature map names to tensor.
                Same as in :meth:`ROIHeads.forward`.
            proposals (list[Instances]): the per-image object proposals with
                their matching ground truth.
                Each has fields "proposal_boxes", and "objectness_logits",
                "gt_classes", "gt_boxes".

        Returns:
            In training, a dict of losses.
            In inference, a list of `Instances`, the predicted instances.
        """
        features = [features[f] for f in self.box_in_features]
        box_features = self.box_pooler(features, [x.proposal_boxes for x in proposals])
        box_features = self.box_head(box_features)
        predictions = self.box_predictor(box_features)
        del box_features

        if self.training:
            losses = self.box_predictor.losses(predictions, proposals)
            # proposals is modified in-place below, so losses must be computed first.
            if self.train_on_pred_boxes:
                with torch.no_grad():
                    pred_boxes = self.box_predictor.predict_boxes_for_gt_classes(
                        predictions, proposals
                    )
                    for proposals_per_image, pred_boxes_per_image in zip(proposals, pred_boxes):
                        proposals_per_image.proposal_boxes = Boxes(pred_boxes_per_image)
            return losses
        else:
            pred_instances, _ = self.box_predictor.inference(predictions, proposals)
            return pred_instances

    def _forward_mask(self, features: Dict[str, torch.Tensor], instances: List[Instances]):
        """
        Forward logic of the mask prediction branch.

        Args:
            features (dict[str, Tensor]): mapping from feature map names to tensor.
                Same as in :meth:`ROIHeads.forward`.
            instances (list[Instances]): the per-image instances to train/predict masks.
                In training, they can be the proposals.
                In inference, they can be the boxes predicted by R-CNN box head.

        Returns:
            In training, a dict of losses.
            In inference, update `instances` with new fields "pred_masks" and return it.
        """
        if not self.mask_on:
            return {} if self.training else instances

        if self.training:  # TODO handle empty mask
            # head is only trained on positive proposals.
            instances, _ = select_foreground_proposals(instances, self.num_classes)

        if self.mask_pooler is not None:
            features = [features[f] for f in self.mask_in_features]
            boxes = [x.proposal_boxes if self.training else x.pred_boxes for x in instances]
            features = self.mask_pooler(features, boxes)
        else:
            features = {f: features[f] for f in self.mask_in_features}
        return self.mask_head(features, instances)

    # '''
    def _forward_material(self, features: Dict[str, torch.Tensor], instances: List[Instances]):
        """
        Forward logic of the material prediction branch.

        Args:
            features (dict[str, Tensor]): mapping from feature map names to tensor.
                Same as in :meth:`ROIHeads.forward`.
            instances (list[Instances]): the per-image instances to train/predict materials.
                In training, they can be the proposals.
                In inference, they can be the boxes predicted by R-CNN box head.

        Returns:
            In training, a dict of losses.
            In inference, update `instances` with new fields "pred_materials" and return it.
        """
        if not self.material_on:
            return {} if self.training else instances

        if self.training:  # TODO when material is empty and create material head forward
            # head is only trained on positive proposals.
            instances, _ = select_foreground_proposals(instances, self.num_classes)

        if self.material_pooler is not None:
            features = [features[f] for f in self.material_in_features]
            boxes = [x.proposal_boxes if self.training else x.pred_boxes for x in instances]
            features = self.material_pooler(features, boxes)
        else:
            features = {f: features[f] for f in self.material_in_features}
        return self.material_head(features, instances)
    # '''

    def _forward_keypoint(self, features: Dict[str, torch.Tensor], instances: List[Instances]):
        """
        Forward logic of the keypoint prediction branch.

        Args:
            features (dict[str, Tensor]): mapping from feature map names to tensor.
                Same as in :meth:`ROIHeads.forward`.
            instances (list[Instances]): the per-image instances to train/predict keypoints.
                In training, they can be the proposals.
                In inference, they can be the boxes predicted by R-CNN box head.

        Returns:
            In training, a dict of losses.
            In inference, update `instances` with new fields "pred_keypoints" and return it.
        """
        if not self.keypoint_on:
            return {} if self.training else instances

        if self.training:
            # head is only trained on positive proposals with >=1 visible keypoints.
            instances, _ = select_foreground_proposals(instances, self.num_classes)
            instances = select_proposals_with_visible_keypoints(instances)

        if self.keypoint_pooler is not None:
            features = [features[f] for f in self.keypoint_in_features]
            boxes = [x.proposal_boxes if self.training else x.pred_boxes for x in instances]
            features = self.keypoint_pooler(features, boxes)
        else:
            features = {f: features[f] for f in self.keypoint_in_features}
        return self.keypoint_head(features, instances)