def post(self):
     try:
         args = generate_report_parser.parse_args()
     except Exception as e:
         rv = dict()
         rv['status'] = str(e)
         return rv, 404
     try:
         file_path = args['zip_file']
         file_base_name = os.path.basename(file_path)
         job_id = study_instance_id = output_dir = file_base_name[:-4]
         if os.path.exists(output_dir):
             shutil.rmtree(output_dir)
         os.makedirs(output_dir)
         write_progress(job_id, "5")
         work_dir = tempfile.mkdtemp()
         with ZipFile(file_path, 'r') as zipObj:
             zipObj.extractall(work_dir)
         write_progress(job_id, "10")
         update_progress_percent(study_instance_id, "10")
         t = threading.Thread(target=generate_report,
                              args=(study_instance_id, work_dir,
                                    output_dir))
         t.start()
         rv = dict()
         rv['status'] = "Started"
         rv['job_id'] = job_id
         return rv, 200
     except Exception as e:
         rv = dict()
         rv['status'] = str(e)
         return rv, 404
예제 #2
0
def worker(inp):
    try:
        study_instance_id = inp[0]
        ct_instance = inp[1]
        total_number_of_instances = inp[2]

        print("Processing : " + str(os.path.basename(ct_instance)))

        percent = read_progress(study_instance_id)
        last_sent_percent = read_last_sent_progress(study_instance_id)

        if percent != '' and last_sent_percent != '':
            new_percent = str(
                round(
                    float(
                        float(percent) +
                        29.0 / float(total_number_of_instances)), 2))
            write_progress(study_instance_id, new_percent)
            delta = float(new_percent) - float(last_sent_percent)
            if delta > 2.0:
                write_last_sent_progress(study_instance_id, new_percent)
                update_progress_percent(study_instance_id, new_percent)
        else:
            print("mp: Got Empty Percentage")

        meta_data_dicom = pydicom.dcmread(ct_instance)
        ct_slice = CTSlice(meta_data_dicom)
        ggo, con, sub, fib, ple, pne, nor, affected_points = ct_slice.get_box_label_distribution(
        )
        print("Done : " + str(os.path.basename(ct_instance)))
    except Exception as e:
        print('mp:worker() exception = ' + str(e))
        ct_slice = None
        ggo = None
        con = None
        sub = None
        fib = None
        ple = None
        pne = None
        nor = None
        affected_points = None
        meta_data_dicom = None
    return ct_slice, ggo, con, sub, fib, ple, pne, nor, affected_points, meta_data_dicom
예제 #3
0
def generate_report(study_instance_id, work_dir, output_dir):
    points_file = study_instance_id + '_points.pkl'
    try:
        path = './results/' + study_instance_id + '_result.zip'
        if os.path.exists(path):
            write_progress(output_dir, "100")
            update_progress_percent(study_instance_id, "100")
            p = os.path.abspath(path)
            save_result(study_instance_id, p)
            if os.path.exists(points_file):
                os.remove(points_file)
            if os.path.exists(study_instance_id):
                shutil.rmtree(study_instance_id)
        else:
            execute(study_instance_id, work_dir, output_dir)
            shutil.rmtree(work_dir)
            # assemble_report(output_dir)
            write_progress(output_dir, "100")
            update_progress_percent(study_instance_id, "100")
            zip_folder = zipfile.ZipFile(path, 'w', compression=zipfile.ZIP_STORED)
            for file in os.listdir(study_instance_id):
                zip_folder.write(study_instance_id + '/' + file)
            zip_folder.close()
            p = os.path.abspath(path)
            save_result(study_instance_id, p)
            if os.path.exists(points_file):
                os.remove(points_file)
            if os.path.exists(study_instance_id):
                shutil.rmtree(study_instance_id)
    except Exception as e:
        print("generate_report() exception --> " + str(e))
        if os.path.exists(output_dir) is False:
            os.makedirs(output_dir)
        with open(output_dir + '/' + ERROR_FILE, "a+") as f:
            f.write(str(e))
        update_in_progress_state(study_instance_id, 'FALSE')
        if os.path.exists(points_file):
            os.remove(points_file)
        if os.path.exists(study_instance_id):
            shutil.rmtree(study_instance_id)
예제 #4
0
def worker_plot(inp):
    r = inp[0]
    type_map = inp[1]
    ct_ggo_dir = inp[2]
    ct_con_dir = inp[3]
    ct_fib_dir = inp[4]
    ct_vtk_dir = inp[5]
    total_number_of_instances = inp[6]
    study_instance_id = inp[7]

    meta_data_dicom = r[9]
    meta_data_dicom_vtk = copy.deepcopy(meta_data_dicom)
    meta_data_dicom.pixel_array.fill(0)

    meta_data_dicom_ggo = copy.deepcopy(meta_data_dicom)
    meta_data_dicom_ggo.pixel_array.fill(0)

    meta_data_dicom_con = copy.deepcopy(meta_data_dicom)
    meta_data_dicom_con.pixel_array.fill(0)

    meta_data_dicom_fib = copy.deepcopy(meta_data_dicom)
    meta_data_dicom_fib.pixel_array.fill(0)

    z = int(math.floor(float(meta_data_dicom.ImagePositionPatient[2])))

    # print(z)

    percent = read_progress(study_instance_id)
    last_sent_percent = read_last_sent_progress(study_instance_id)

    if percent != '' and last_sent_percent != '':
        new_percent = str(
            round(
                float(
                    float(percent) + 19.0 / float(total_number_of_instances)),
                2))
        write_progress(study_instance_id, new_percent)
        delta = float(new_percent) - float(last_sent_percent)
        if delta > 2.0:
            write_last_sent_progress(study_instance_id, new_percent)
            update_progress_percent(study_instance_id, new_percent)
    else:
        print("mp_plot: Got Empty Percentage")

    if type_map.get(z) is not None:
        values = type_map.get(z)
        for value in values:
            affected_type = value[0]
            point = value[1]
            x = point[0]
            y = point[1]
            if affected_type.lower() == LABEL_GROUND_GLASS_OPACITY.lower():
                meta_data_dicom_ggo.pixel_array[x][y] = 3500
                meta_data_dicom_vtk.pixel_array[x][y] = 3000
                meta_data_dicom_con.pixel_array[x][y] = 0
                meta_data_dicom_fib.pixel_array[x][y] = 0
            elif affected_type.lower() == LABEL_CONSOLIDATION.lower():
                # print("Found Consolidation")
                meta_data_dicom_ggo.pixel_array[x][y] = 0
                meta_data_dicom_con.pixel_array[x][y] = 3400
                meta_data_dicom_vtk.pixel_array[x][y] = 2000
                meta_data_dicom_fib.pixel_array[x][y] = 0
            elif affected_type.lower() == LABEL_FIBROSIS.lower():
                # print("Found Fibrosis")
                meta_data_dicom_ggo.pixel_array[x][y] = 0
                meta_data_dicom_con.pixel_array[x][y] = 0
                meta_data_dicom_fib.pixel_array[x][y] = 3300
                meta_data_dicom_vtk.pixel_array[x][y] = 1000
            else:
                meta_data_dicom_ggo.pixel_array[x][y] = 0
                meta_data_dicom_con.pixel_array[x][y] = 0
                meta_data_dicom_fib.pixel_array[x][y] = 0
                meta_data_dicom_vtk.pixel_array[x][y] = 0

    meta_data_dicom_vtk.PixelData = meta_data_dicom_vtk.pixel_array.tobytes()
    dt = datetime.datetime.now(timezone.utc)
    utc_time = dt.replace(tzinfo=timezone.utc)
    utc_timestamp = utc_time.timestamp()
    pydicom.dcmwrite(
        ct_vtk_dir + '/' + str(utc_timestamp) + '_' + str(os.getpid()) +
        '_vtk.dcm', meta_data_dicom_vtk)

    meta_data_dicom_ggo.PixelData = meta_data_dicom_ggo.pixel_array.tobytes()
    dt = datetime.datetime.now(timezone.utc)
    utc_time = dt.replace(tzinfo=timezone.utc)
    utc_timestamp = utc_time.timestamp()
    pydicom.dcmwrite(
        ct_ggo_dir + '/' + str(utc_timestamp) + '_' + str(os.getpid()) +
        '_ggo.dcm', meta_data_dicom_ggo)

    meta_data_dicom_con.PixelData = meta_data_dicom_con.pixel_array.tobytes()
    dt = datetime.datetime.now(timezone.utc)
    utc_time = dt.replace(tzinfo=timezone.utc)
    utc_timestamp = utc_time.timestamp()
    pydicom.dcmwrite(
        ct_con_dir + '/' + str(utc_timestamp) + '_' + str(os.getpid()) +
        '_con.dcm', meta_data_dicom_con)

    meta_data_dicom_fib.PixelData = meta_data_dicom_fib.pixel_array.tobytes()
    dt = datetime.datetime.now(timezone.utc)
    utc_time = dt.replace(tzinfo=timezone.utc)
    utc_timestamp = utc_time.timestamp()
    pydicom.dcmwrite(
        ct_fib_dir + '/' + str(utc_timestamp) + '_' + str(os.getpid()) +
        '_fib.dcm', meta_data_dicom_fib)
예제 #5
0
def plot_3d(study_instance_id, p_segmented_lungs, output_dir, p_ggo_images,
            p_con_images, p_fib_images, p_threshold_lung,
            p_threshold_affected):
    p_ggo_images_t = p_ggo_images.transpose(2, 1, 0)
    p_con_images_t = p_con_images.transpose(2, 1, 0)
    p_fib_images_t = p_fib_images.transpose(2, 1, 0)
    p_segmented_lungs_t = p_segmented_lungs.transpose(2, 1, 0)

    verts_ggo, faces_ggo, _, _ = measure.marching_cubes(p_ggo_images_t,
                                                        p_threshold_affected,
                                                        step_size=3)

    verts_con, faces_con, _, _ = measure.marching_cubes(p_con_images_t,
                                                        p_threshold_affected,
                                                        step_size=3)

    verts_fib, faces_fib, _, _ = measure.marching_cubes(p_fib_images_t,
                                                        p_threshold_affected,
                                                        step_size=3)

    verts_lung, faces_lung, _, _ = measure.marching_cubes(p_segmented_lungs_t,
                                                          p_threshold_lung,
                                                          step_size=3)
    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')

    mesh_ggo = Poly3DCollection(verts_ggo[faces_ggo], alpha=0.3)
    mesh_ggo.set_facecolor("r")

    mesh_con = Poly3DCollection(verts_ggo[faces_con], alpha=0.3)
    mesh_con.set_facecolor("g")

    mesh_fib = Poly3DCollection(verts_ggo[faces_fib], alpha=0.3)
    mesh_fib.set_facecolor("y")

    mesh_lung = Poly3DCollection(verts_lung[faces_lung], alpha=0.1)
    mesh_lung.set_facecolor("navy")

    ax.add_collection3d(mesh_ggo)
    ax.add_collection3d(mesh_con)
    ax.add_collection3d(mesh_fib)
    ax.add_collection3d(mesh_lung)

    ax.set_xlim(0, p_segmented_lungs_t.shape[0])
    ax.set_ylim(0, p_segmented_lungs_t.shape[1])
    ax.set_zlim(0, p_segmented_lungs_t.shape[2])
    plt.axis('off')
    plt.grid(b=None)
    plt.savefig(output_dir + '/natural.png', bbox_inches='tight')
    write_progress(output_dir, "82")
    update_progress_percent(study_instance_id, "82")
    ax.view_init(90, 0)
    plt.draw()
    plt.axis('off')
    plt.grid(b=None)
    plt.savefig(output_dir + '/top.png', bbox_inches='tight')
    write_progress(output_dir, "84")
    update_progress_percent(study_instance_id, "84")
    ax.view_init(0, 180)
    plt.draw()
    plt.axis('off')
    plt.grid(b=None)
    plt.savefig(output_dir + '/lateral.png', bbox_inches='tight')
    write_progress(output_dir, "86")
    update_progress_percent(study_instance_id, "86")
    ax.view_init(0, 270)
    plt.draw()
    plt.axis('off')
    plt.grid(b=None)
    plt.savefig(output_dir + '/front.png', bbox_inches='tight')
    write_progress(output_dir, "88")
    update_progress_percent(study_instance_id, "88")
예제 #6
0
def process_ct_instances(study_instance_id, ct_instances, work_dir, output_dir):
    valid_ct_slice_objects = list()
    ct_slice_objects = list()
    ggo_count = 0
    con_count = 0
    sub_count = 0
    fib_count = 0
    ple_count = 0
    pne_count = 0
    abnormal_slice_count = 0

    total_number_of_slices = len(ct_instances)
    write_progress(study_instance_id, "20")
    write_last_sent_progress(study_instance_id, "20")
    update_progress_percent(study_instance_id, "20")

    points_file = study_instance_id + '_points.pkl'

    if os.path.exists(points_file):
        with open(points_file, 'rb') as f:
            rs = pickle.load(f)
    else:
        rs = process(study_instance_id, ct_instances)
        with open(points_file, 'wb') as fp:
            pickle.dump(rs, fp)

    if len(rs) > 0:
        write_progress(output_dir, "50")
        update_progress_percent(study_instance_id, "50")

        for r in rs:
            ct_slice, ggo, con, sub, fib, ple, pne, nor, affected_points, meta_data_dicom = r
            if not ct_slice.all_zeros:
                ct_slice_objects.append(ct_slice)
            if ggo or con or sub or fib or ple or pne > 10:
                abnormal_slice_count += 1
            ggo_count += ggo
            con_count += con
            sub_count += sub
            fib_count += fib
            ple_count += ple
            pne_count += pne
            if ct_slice.is_valid():
                valid_ct_slice_objects.append(ct_slice)

        scores = get_25_score(ct_slice_objects, study_instance_id)

        print('Total slice count: ' + str(total_number_of_slices))
        print('Abnormal slices  : ' + str(abnormal_slice_count))
        print('co_rads_score : ' + str(scores[5]))
        print('right_superior_lobe_percentage_affected : ' + str(scores[0]))
        print('right_middle_lobe_percentage_affected : ' + str(scores[1]))
        print('right_inferior_lobe_percentage_affected : ' + str(scores[2]))
        print('left_superior_lobe_percentage_affected : ' + str(scores[3]))
        print('left_inferior_lobe_percentage_affected : ' + str(scores[4]))

        final_json = create_json(study_instance_id,
                                 scores,
                                 ggo_count,
                                 con_count,
                                 sub_count,
                                 fib_count,
                                 ple_count,
                                 abnormal_slice_count,
                                 total_number_of_slices)

        write_progress(output_dir, "55")
        update_progress_percent(study_instance_id, "55")

        with open(output_dir + '/out.json', 'w') as f:
            final_json_str = json.dumps(final_json, indent=4)
            f.write(final_json_str)

        mp_slice_plot_2(rs, output_dir)
        write_progress(output_dir, "60")
        write_last_sent_progress(output_dir, "60")
        update_progress_percent(study_instance_id, "60")
        type_map = dict()

        for r in rs:
            for af in r[8]:
                x = int(math.floor(af[0][0]))
                y = int(math.floor(af[0][1]))
                z = int(math.floor(af[0][2]))
                v = af[1]
                if type_map.get(z) is None:
                    type_map[z] = [(v, (x, y, z))]
                else:
                    type_map.get(z).append((v, (x, y, z)))

        vtk_dir = tempfile.mkdtemp()
        ct_ggo_dir = tempfile.mkdtemp()
        ct_con_dir = tempfile.mkdtemp()
        ct_fib_dir = tempfile.mkdtemp()

        # print("1")

        mp_plot(study_instance_id,
                rs,
                type_map,
                ct_ggo_dir,
                ct_con_dir,
                ct_fib_dir,
                vtk_dir)

        write_progress(output_dir, "80")
        update_progress_percent(study_instance_id, "80")

        # vtk_plot(vtk_dir, output_dir)
        three_d_plot(study_instance_id, work_dir, output_dir, ct_ggo_dir, ct_con_dir, ct_fib_dir)

        shutil.rmtree(ct_ggo_dir)
        shutil.rmtree(ct_con_dir)
        shutil.rmtree(ct_fib_dir)
        shutil.rmtree(vtk_dir)

        write_progress(output_dir, "90")
        update_progress_percent(study_instance_id, "90")
    else:
        write_progress(output_dir, "90")
        update_progress_percent(study_instance_id, "90")