Example #1
0
def export_database(database_file_folder):
    manifest_db = manifest.Manifest()
    manifest_csv = open(database_file_folder + '/manifest.txt', 'w')
    manifest_csv.write("id\tfilename\tmd5\tsize\tstate\n")
    for wsi in manifest_db.get_projects():
        for temp in wsi:
            manifest_csv.write(str(temp) + '\t')
        manifest_csv.write("\n")
    mission_db = mission.Mission()
    mission_csv = open(database_file_folder + '/predict_mask.txt', 'w')
    mission_csv.write("id\tfilename\tmd5\tsize\tstate\n")
    for wsi in mission_db.get_predict_masks():
        for temp in wsi:
            mission_csv.write(str(temp) + '\t')
        mission_csv.write("\n")
def get_table(slide_uuid):
    mission_db = mission.Mission()
    if slide_uuid == "":
        jobs = mission_db.get_predict_masks()
    else:
        jobs = mission_db.get_predict_masks_by_uuid(slide_uuid)
    data = []
    for job in jobs:
        temp = []
        temp.append(job[0])  # ID
        # slide_id = manifest_db.get_project_by_uuid(job[2])[0]
        # print('static/data/analysis_data/' + str(job[2]) + '/mission' + str(job[0]) + '_result.png')
        if os.path.exists('static/data/analysis_data/' + str(job[2]) + '/mission' + str(job[0]) + '_result.png'):
            temp.append('<a href="/static/data/analysis_data/' + str(job[2]) + '/mission' + str(
                job[0]) + '_result.png"target="_blank">' + str(job[1]) + '</a>')
        else:
            temp.append(str(job[1]))
        temp.append('<button type="button" onclick="btn_click(\'remove_mission\',' +
                    str(job[0]) + ')">remove</button>')
        temp.append('<a href="/slide?slide_id=' + str(job[1]) + '"target="_blank">' + str(job[2]) + '</a>')
        temp.append(str(job[3]))
        temp.append(bar(str(job[4] / job[5] * 100)))
        temp.append(str(job[4]) + '/' + str(job[5]))
        if job[6] is None:
            temp.append("Not Finished")
        elif "mission" in job[6]:
            temp.append('<a href="/slide?slide_id=' + str(job[1]) +
                        '&mask_url=' + job[6] + '"target="_blank">' + job[6] + '</a>'
                        )
        else:
            temp.append('<a href="/slide?slide_id=' + str(job[1]) +
                        '&mask_url=' + 'mission' + str(job[0]) + '_' + str(job[3]) + '.png' +
                        '"target="_blank">' + job[6] + '</a>'
                        )
        temp.append(str(job[8]))
        temp.append(str(job[7]))
        data.append(temp)
    return data
def predict_mask_with_job_id(slide_id, model_name="0"):
    # if str(model_name) == '0':
    #     model_path = "Model/resnet_34_crd_model_59.pth"
    # elif str(model_name) == '1':
    #     model_path = "Model/resnet_34_transfer_predicted_crd_model_best.pth"
    # else:
    model_path = "models/" + model_name

    make_bg(slide_id)

    manifest_db = manifest.Manifest()
    info = manifest_db.get_project_by_id(slide_id)
    mission_db = mission.Mission()
    job_id = mission_db.insert(slide_uuid=info[1],
                               slide_id=info[0],
                               job_type=model_name,
                               total=-1)

    original_data_folder = original_data_root + info[1] + '/'
    analysis_data_folder = analysis_data_root + info[1] + '/'

    svs_file = original_data_folder + info[2]
    # if not os.path.exists(analysis_data_folder + 'patch/'):
    #     os.mkdir(analysis_data_folder + 'patch/')

    mask = cv2.imread(analysis_data_folder + info[4], cv2.IMREAD_GRAYSCALE)
    oslide = openslide.OpenSlide(svs_file)
    w, h = oslide.dimensions
    pre_result = numpy.zeros((mask.shape[0], mask.shape[1], 4),
                             dtype=numpy.uint8)
    times = mask.shape[1] / w * 2000
    mission_db.update_total_by_id(job_id=job_id, total=w // 2000 * h // 2000)

    if "subtype" not in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=2,
                                                       batch_size=64,
                                                       num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    # if os.path.exists(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png'):
                    #     patch = Image.open(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png')
                    # else:
                    #     patch = oslide.read_region((x * 2000, y * 2000), 0, (2000, 2000))
                    #     patch.save(analysis_data_folder + 'patch/' + str(x) + '_' + str(y) + '.png')
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    # result = myModule.predict(total_image)
                    # probablity_list.append((x, y, result[0]))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        pre_result = post_processing(pre_result)
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, pre_result)
        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=mask_file_name)

        mask_file = cv2.imread(analysis_data_folder + mask_file_name)

        original_file_name = "smaller_image.png"
        original_file = cv2.imread(analysis_data_folder + original_file_name)

        alpha = numpy.zeros(mask_file.shape)
        alpha[:, :, 0] = mask_file[:, :, 0]
        alpha[:, :, 1] = mask_file[:, :, 0]
        alpha[:, :, 2] = mask_file[:, :, 0]
        alpha = alpha.astype(float) / 128

        mask_file = mask_file.astype(float)
        original_file = original_file.astype(float)
        mask_file = cv2.multiply((1 - alpha) * 0.7, mask_file)
        original_file = cv2.multiply(0.3 + alpha * 0.7, original_file)

        out_image = original_file + mask_file
        result_file_name = 'mission' + str(job_id) + '_result.png'
        cv2.imwrite(analysis_data_folder + result_file_name, out_image)

    elif "subtype" in str(model_name) and "hybrid" in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=4,
                                                       batch_size=64,
                                                       num_workers=0)
        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["health", "ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0, 0]
        for i in range(4):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(4):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i + 1]
        mask_file_name = 'result_mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))

        i = 1
        result[:, :, 0] = pre_result[:, :, i + 1] * [15] / 255
        result[:, :, 1] = pre_result[:, :, i + 1] * [147] / 255
        result[:, :, 2] = pre_result[:, :, i + 1] * [254] / 255
        i = 0
        result[:, :, i] += pre_result[:, :, i + 1]
        i = 2
        result[:, :, i] += pre_result[:, :, i + 1]

        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)

    elif "_subtype" in str(model_name) and "hybrid" not in str(model_name):
        myModule = predict_module.ResNetClassification(model_path=model_path,
                                                       num_classes=3,
                                                       batch_size=64,
                                                       num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    pre_result[int(y * times):int(y * times + times),
                               int(x * times):int(x * times + times),
                               numpy.argmax(result[0])] = result[
                                   0, numpy.argmax(result[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0]
        for i in range(3):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(3):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i]
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)

    elif "MixMatch-subtype" in str(model_name):
        myModule = predict_module2.ResNetClassification(model_path=model_path,
                                                        batch_size=64,
                                                        num_workers=0)

        for x in range(w // 2000):
            for y in range(h // 2000):
                if available_region(mask[int(y * times):int(y * times + times),
                                         int(x * times):int(x * times +
                                                            times)]):
                    patch = oslide.read_region((x * 2000, y * 2000), 0,
                                               (2000, 2000))
                    patch = numpy.array(patch.convert('RGB'))
                    result1, result2 = myModule.predict(
                        numpy.resize(patch, tuple([1, 2000, 2000, 3])))
                    if result1[0, 1] > 0.5:
                        pre_result[int(y * times):int(y * times + times),
                                   int(x * times):int(x * times + times),
                                   0] = result1[0, 1] * 255
                    elif result1[0, 0] > 0.5:
                        pre_result[int(y * times):int(y * times + times),
                                   int(x * times):int(x * times + times),
                                   numpy.argmax(result2[0]) +
                                   1] = result2[0,
                                                numpy.argmax(result2[0])] * 255
                mission_db.update_finished_by_id(job_id=job_id,
                                                 finished=x * h // 2000 + y +
                                                 1)

        sub = ["health", "ccRCC", "pRCC", "chRCC"]
        result_sub = [0, 0, 0, 0]
        for i in range(4):
            result_sub[i] = numpy.sum(pre_result[:, :, i] != 0)
        result_sub_sum = numpy.sum(result_sub)
        summary = ""
        for i in range(4):
            result_sub[i] = round(result_sub[i] / result_sub_sum * 100, 2)
            summary = summary + sub[i] + ": " + str(result_sub[i]) + "%, "

        result = numpy.zeros((pre_result.shape[0], pre_result.shape[1], 3))
        for i in range(3):
            result[:, :, i] = pre_result[:, :, i + 1]
        mask_file_name = 'mission' + str(job_id) + '_' + str(
            model_name) + '.png'
        cv2.imwrite(analysis_data_folder + mask_file_name, result)

        mission_db.update_predict_mask_by_id(job_id=job_id,
                                             predict_mask=summary)
def get_total_number():
    mission_db = mission.Mission()
    return len(mission_db.get_predict_masks())
def remove_mission_by_id(job_id):
    mission_db = mission.Mission()
    mission_db.delete_predict_mask_by_id(job_id)
Example #6
0
def clear_database():
    manifest_db = manifest.Manifest()
    manifest_db.delete_all_projects()
    mission_db = mission.Mission()
    mission_db.delete_all_predict()