Exemplo n.º 1
0
 def validate_to_csv(self, metrics, values, name=''):
     path = self.path.split('/')[:-1]
     path = '/'.join(path)
     epoch = (self.var_file(True) % 100) * 100
     epoch = epoch + int(self.var_file(True) / 100) * 100
     mocl.writeToCsv(path + '/scores' + name + '.csv', ['name'] + metrics,
                     [self.path.split('/')[-1] + '_' + str(epoch)] + values)
Exemplo n.º 2
0
 def save_to_csv(self):
     if self.path is None:
         return
     if self.predicted == False:
         return
     mocl.writeToCsv("output.csv", mocl.getOutputHeader(), [
         self.path, self.x, self.y, self.xOut, self.yOut, self.atrophyRate
     ])
Exemplo n.º 3
0
    def createImagesInRepoAfterFunctionOnPath(self,
                                              path,
                                              new_repo_path,
                                              function,
                                              target_size,
                                              override=False,
                                              extension='.jpg',
                                              onlyMasked=False):
        success = 0
        fail = 0
        repo_path, image_path = morn.getRepoPathAndImagePath(path)

        base2, name2 = BuildRepo.getBaseRepoPathAndRepoName(new_repo_path)
        BuildRepo.createImagesRepo(base2 + '/', name2)
        new_path = new_repo_path + image_path

        maskList = None
        if onlyMasked:
            maskList = mocl.getCsvList(repo_path, False)

        for a in os.listdir(path):
            if not os.path.isfile(os.path.join(path, a)):
                continue
            patient, date, eye = morn.getPatientDateEye(image_path)

            if onlyMasked and not os.path.isfile(
                    os.path.join(path + 'mask/', a)
            ) and not mocl.checkIfExistsInCSV(
                [patient, date, eye, a], list=maskList, image=False):
                continue
            name = a.split(".")[0]
            base_image = moil.read_and_size(name,
                                            path=path,
                                            target_size=target_size)
            image = function(base_image)

            if morn.createImageInPath(new_path, name + extension, image,
                                      override):
                mocl.registerImageCsv(new_repo_path, image_path,
                                      name + extension, image, function)
                success += 1
            else:
                fail += 1
        return success, fail
Exemplo n.º 4
0
 def CreateMasks(self):
     cv2.destroyWindow(self.winname)
     cv2.namedWindow(self.winname)
     cv2.setMouseCallback(self.winname, self.draw_circle)
     list = mocl.getCsvList(self.image_path, False)
     while (True):
         self.circle_mask_on_random_image_in_path(
             morn.random_path(self.image_path),
             target_size=self.targetSize,
             check_csv=True,
             list=list)
Exemplo n.º 5
0
    def createMaskFromCsv(self,
                          repo_path,
                          imageRow,
                          list=None,
                          override=False):
        if list is None:
            list = mocl.getCsvList(repo_path, image=False)

        target = mocl.checkIfExistsInCSV(imageRow,
                                         repo_path=repo_path,
                                         list=list,
                                         image=False,
                                         returnTargetRow=True)
        if target is None:
            return False

        imageW = int(imageRow[4])
        imageH = int(imageRow[5])

        maskW = int(target[4])
        maskH = int(target[5])
        maskX = int(target[6])
        maskY = int(target[7])
        maskR = int(target[8])

        Wratio = imageW / maskW
        Hratio = imageH / maskH

        Rratio = math.sqrt((((Wratio**2) + (Hratio**2)) / 2))

        outX = int(maskX * (Wratio))
        outY = int(maskY * (Hratio))
        outR = int(maskR * (Rratio))
        mask = np.zeros((imageH, imageW), dtype=np.uint8)
        mask = cv2.circle(mask, (outX, outY), outR, 255, -1)

        path = repo_path + reduce(
            (lambda x, y: x + '/' + y), imageRow[:3]) + '/mask/'

        return morn.createImageInPath(path, imageRow[3], mask, override)
Exemplo n.º 6
0
def splitCsv(maskDataPath, name, csv1, csv2, howManyToNewCsv):
    list = mocl.getCsvList(maskDataPath, False, name=name)
    i = 0
    for row in list:
        if len(list) - i > howManyToNewCsv:
            if csv1 is not None:
                mocl.writeToCsv(csv1, mocl.getMaskHeader(), row)
        else:
            if csv2 is not None:
                mocl.writeToCsv(csv2, mocl.getMaskHeader(), row)
        i += 1
Exemplo n.º 7
0
def visualizeTable(columns=['Dystans', 'Youden', 'Jaccard', 'Dice']):
    scores = mocl.getScoresList("../../weights/scores.csv")
    names = []

    data = []

    for score in scores:
        data.append([float(x) for x in score[1:]])
        names.append(score[0][10:-4])
    data = [
        y for _, y in (
            sorted(zip(names, data), key=lambda pair: int(pair[0][-3:])))
    ]
    data = [['%.3f' % j for j in i] for i in data]

    fig, axs = plt.subplots(2, 1)
    axs[0].axis('tight')
    axs[0].axis('off')
    table = axs[0].table(colLabels=columns,
                         loc='center',
                         cellText=data,
                         rowLabels=names,
                         rowColours=['gray', 'red'] * 3,
                         rowLoc='right',
                         cellLoc='right',
                         cellColours=[['gray'] * 4, ['red'] * 4] * 3)
    table.set_fontsize(12)
    table.scale(0.8, 1.4)
    for i in range(1, len(columns)):
        tempDat = []
        tempDat2 = []
        for j in range(int(len(data) / 2)):
            tempDat.append(float(data[j * 2 + 1][i]))
            tempDat2.append(float(data[j * 2][i]))
        if i == 1:
            axs[1].plot([300, 500, 700], tempDat, 'r-', label='SAB')
            axs[1].plot([300, 500, 700], tempDat2, 'k-', label='Gray')
        else:
            axs[1].plot([300, 500, 700], tempDat, 'r-')
            axs[1].plot([300, 500, 700], tempDat2, 'k-')
    plt.title("Youden, Dice, Jaccard (kolejno od góry)")
    plt.legend()
    plot_margin = 0.1

    x0, x1, y0, y1 = plt.axis()
    axs[1].axis((x0 - plot_margin, x1 + plot_margin, y0, y1))
    plt.text(x=400,
             y=0.58,
             s='Liczebność zbioru treningowego',
             fontdict={'size': 10})
    plt.show()
Exemplo n.º 8
0
 def createAllMasksForImagesCsv(self, repo_path):
     success = 0
     fail = 0
     list = mocl.getCsvList(repo_path, image=False)
     iter = 0
     with open(repo_path + "imageData.csv", 'r') as file:
         reader = csv.reader(file)
         next(reader, None)
         for row in reader:
             if self.createMaskFromCsv(repo_path, row, list, True):
                 success += 1
             else:
                 fail += 1
             iter += 1
             print("Images looped: " + str(iter))
         file.close()
     print("Masks created: " + str(success) + ", failed to create: " +
           str(fail))
Exemplo n.º 9
0
def visualizeBar(scoreIndex, title):
    scores = mocl.getScoresList("../../weights/scores.csv")
    vals = []
    names = []
    for score in scores:
        vals.append(float(score[scoreIndex]))
        names.append(score[0][10:-4])

    l = (sorted(zip(names, vals), key=lambda pair: int(pair[0][-3:])))

    names, vals = [list(t) for t in zip(*l)]

    plt.bar(names, vals, color=['gray', 'red'])

    plt.title(title)
    # plt.text(x=-1.5, y=-1.3, s='500 epok, redukcja kroku uczenia', fontdict={'size':8})
    # plt.text(x=4, y=13.3, s='Gray vs SAB + zbiór treningowy', fontdict={'size':8})
    # plt.xticks(y_pos, names)
    plt.show()
Exemplo n.º 10
0
    def circle_mask_on_random_image_in_path(self,
                                            path,
                                            target_size=None,
                                            r=None,
                                            extension=".jpg",
                                            check_csv=True,
                                            list=None):

        numb = len([
            i for i in os.listdir(path)
            if os.path.isfile(os.path.join(path, i))
        ])
        temp = ([
            a for a in os.listdir(path)
            if os.path.isfile(os.path.join(path, a))
        ])
        try:
            j = np.random.randint(numb)
        except:
            print(path + ", numb: " + str(numb))
            return

        ImName = random.choice(temp)
        if not os.path.exists(path + '/mask'):
            os.makedirs(path + '/mask')
        tempName = path + '/mask/' + ImName
        if os.path.exists(tempName):
            print("Path exists (" + tempName + ")")
            return
        if check_csv:
            paths = morn.getRepoPathAndImagePath(path)
            row = paths[1].split("/")[:-1]
            row.append(ImName)
            if mocl.checkIfExistsInCSV(row, paths[0], list, False):
                print("In CSV exists (" + tempName + ")")
                return

        if r is None and target_size is not None:
            self.rr = int(target_size[0] / 10)
        else:
            self.rr = r

        img = moil.read_and_size(ImName,
                                 path=path,
                                 target_size=target_size,
                                 extension='')
        w, h, c = moil.getWidthHeightChannels(img)
        if r is None and target_size is None:
            self.rr = int(w / 10)
            target_size = (w, h)
        moil.show(img)

        accepted = False
        while not accepted:
            accepted = True

            im2, contours, hierarchy = cv2.findContours(
                self.mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            im2 = copy.deepcopy(img)
            cv2.drawContours(im2, contours, 0, (0, 255, 255), 2)

            moil.show(im2)

        split_path = path.split("/")[:-1]

        repo_path = reduce((lambda x, y: x + '/' + y),
                           split_path[:len(split_path) - 3])
        if not os.path.isfile(repo_path + "/maskData.csv"):
            csvFile = open(repo_path + '/maskData.csv', 'w', newline="")
            writer = csv.writer(csvFile)
            writer.writerow([
                'patient', 'date', 'eye', 'name', 'width', 'height', 'x', 'y',
                'r'
            ])
            csvFile.close()

        csvFile = open(repo_path + '/maskData.csv', 'a', newline="")
        writer = csv.writer(csvFile)
        ls = split_path[-3:]
        ls.extend([
            ImName, target_size[0], target_size[1], self.xx, self.yy, self.rr
        ])
        writer.writerow(ls)
        csvFile.close()
        cv2.imwrite(path + '/mask/' + ImName, self.mask)
        self.masks_done += 1
        print("masks: " + str(self.masks_done))
        cv2.destroyWindow('mask')
Exemplo n.º 11
0
def visualizePlot2(scoreIndex, averaging=False):
    scores = mocl.getScoresList("../../weights/scoreswn.csv")
    names = []
    metr = ['Youden', 'Jaccard', 'Dice']
    data = []
    base_name = scores[0][0][9:16]
    for score in scores:
        if score[0][9:] not in names:
            data.append([float(x) for x in score[1:]])
            tempName = score[0][15:-5]
            #tempName = tempName[:5] + tempName[-1]
            names.append(tempName)
    l = (sorted(zip(names, data), key=lambda pair: pair[0]))

    names, data = [list(t) for t in zip(*l)]
    #names, data = [y for x, y in (sorted(zip(names, data), key=lambda pair: pair[0]))]
    data = [['%.3f' % j for j in i] for i in data]

    chunks = 21
    data = [data[i:i + chunks] for i in range(0, len(data), chunks)]

    names = [names[i:i + chunks] for i in range(0, len(names), chunks)]

    color = ['b', 'm', 'g', 'r', 'c', 'k']
    sums = [[0, 0, 0, 0] for x in range(chunks)]

    for i in range(len(data)):
        print(i)
        name = names[i][0]
        if name == '':
            name = base_name
        named = False
        for k in scoreIndex:
            temp = []
            for j in range(len(data[i])):
                if j == 0:
                    continue
                temp.append(float(data[i][j][k]))
                sums[j][k] = sums[j][k] + float(data[i][j][k])
            l = list(np.arange(chunks - 1) * 100 + 100)
            if not named:
                plt.plot(l, temp, label=names[i][0], color=color[k])
                named = True
            else:
                plt.plot(l, temp, color=color[i])
    if averaging:
        for k in scoreIndex:
            temp = []
            for j in range(len(sums)):
                if j == 0:
                    continue
                sums[j][k] /= len(data)
                sums[j][k] = float('%.3f' % sums[j][k])
                temp.append(sums[j][k])
            l = list(np.arange(chunks - 1) * 100 + 100)
            plt.plot(l, temp, label='Average', color='k')
    sums = sums[1:]
    plt.legend()
    plt.title("Dystans wykrytego środka od prawidłowego")
    plt.text(x=950, y=3.460, s='Ilość epok', fontdict={'size': 10})

    fig, axs = plt.subplots(2, 1)
    axs[0].axis('tight')
    axs[0].axis('off')
    table = axs[0].table(
        colLabels=['Youden', 'G. Youden', 'G. Jaccard', 'G. Dice'],
        loc='center',
        cellText=sums,
        rowLabels=[str(x) for x in np.arange(20) * 100 + 100],
        rowLoc='right',
        cellLoc='right',
        cellColours=[['w'] * 4] * 20,
        rowColours=['w', 'w', 'g', 'w', 'w'] * 4)
    table.set_fontsize(12)
    table.scale(0.8, 1.4)
    plt.show()