def save_transform_to_database(data_transforms):
    """ Save data transforms to database"""
    # pylint: disable= too-many-locals, bare-except
    conn = sqlite3.connect(util.DB_PATH)
    conn.text_factory = str

    for img in data_transforms:
        cursor = conn.execute('''SELECT pid from Images where id = ? ''', (img.image_id,))
        pid = cursor.fetchone()[0]

        folder = util.DATA_FOLDER + str(pid) + "/registration_transforms/"
        util.mkdir_p(folder)

        transform_paths = ""
        print(img.get_transforms())
        for _transform in img.get_transforms():
            print(_transform)
            dst_file = folder + util.get_basename(_transform) + '.h5.gz'
            if os.path.exists(dst_file):
                os.remove(dst_file)
            with open(_transform, 'rb') as f_in, gzip.open(dst_file, 'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)
            transform_paths += str(pid) + "/registration_transforms/" +\
                basename(_transform) + '.h5.gz' + ", "
        transform_paths = transform_paths[:-2]

        cursor2 = conn.execute('''UPDATE Images SET transform = ? WHERE id = ?''',
                               (transform_paths, img.image_id))
        cursor2 = conn.execute('''UPDATE Images SET fixed_image = ? WHERE id = ?''',
                               (img.fixed_image, img.image_id))

        folder = util.DATA_FOLDER + str(pid) + "/reg_volumes_labels/"
        util.mkdir_p(folder)
        vol_path = util.compress_vol(img.processed_filepath)
        shutil.copy(vol_path, folder)

        volume_db = str(pid) + "/reg_volumes_labels/" + basename(vol_path)
        cursor2 = conn.execute('''UPDATE Images SET filepath_reg = ? WHERE id = ?''',
                               (volume_db, img.image_id))

        cursor = conn.execute('''SELECT filepath, id from Labels where image_id = ? ''',
                              (img.image_id,))
        for (row, label_id) in cursor:
            temp = util.compress_vol(move_vol(util.DATA_FOLDER + row,
                                              img.get_transforms(), True))
            shutil.copy(temp, folder)
            label_db = str(pid) + "/reg_volumes_labels/" + basename(temp)
            cursor2 = conn.execute('''UPDATE Labels SET filepath_reg = ? WHERE id = ?''',
                                   (label_db, label_id))

        conn.commit()
        cursor.close()
        cursor2.close()

#    cursor = conn.execute('''VACUUM; ''')
    conn.close()
Exemple #2
0
def transform_labels_temp(imgs):
    """ Save transformed labels to a temp folder in database"""
    # pylint: disable= too-many-locals, bare-except
    conn = sqlite3.connect(util.DB_PATH, timeout=900)
    conn.text_factory = str

    for img in imgs:
        cursor = conn.execute('''SELECT pid from Images where id = ? ''',
                              (img.image_id, ))
        pid = cursor.fetchone()[0]

        folder = util.DATA_FOLDER + str(pid) + "/reg_volumes_labels/temp/"
        if not os.path.exists(folder):
            os.makedirs(folder)

        cursor = conn.execute(
            '''SELECT filepath, id from Labels where image_id = ? ''',
            (img.image_id, ))
        for (filepath, label_id) in cursor:
            temp = util.compress_vol(
                move_vol(util.DATA_FOLDER + filepath, img.get_transforms(),
                         True))
            shutil.copy(temp, folder)

        conn.commit()
        cursor.close()

    conn.close()
Exemple #3
0
def save_transform_to_database(imgs):
    """ Save data transforms to database"""
    # pylint: disable= too-many-locals, bare-except
    conn = sqlite3.connect(util.DB_PATH, timeout=900)
    conn.text_factory = str

    try:
        conn.execute(
            "alter table Images add column 'registration_date' 'TEXT'")
    except sqlite3.OperationalError:
        pass

    for img in imgs:
        cursor = conn.execute('''SELECT pid from Images where id = ? ''',
                              (img.image_id, ))
        pid = cursor.fetchone()[0]

        folder = util.DATA_FOLDER + str(pid) + "/registration_transforms/"
        if os.path.exists(folder):
            shutil.rmtree(folder)
        os.makedirs(folder)

        transform_paths = ""
        util.LOGGER.info(img.get_transforms())
        for _transform in img.get_transforms():
            util.LOGGER.info(_transform)
            dst_file = folder + util.get_basename(_transform) + '.h5.gz'
            with open(_transform, 'rb') as f_in, gzip.open(dst_file,
                                                           'wb') as f_out:
                shutil.copyfileobj(f_in, f_out)
            transform_paths += str(pid) + "/registration_transforms/" +\
                util.get_basename(_transform) + '.h5.gz' + ", "
        transform_paths = transform_paths[:-2]

        cursor2 = conn.execute(
            '''UPDATE Images SET transform = ? WHERE id = ?''',
            (transform_paths, img.image_id))
        cursor2 = conn.execute(
            '''UPDATE Images SET fixed_image = ? WHERE id = ?''',
            (img.fixed_image, img.image_id))

        cursor2 = conn.execute(
            '''UPDATE Images SET registration_date = ? WHERE id = ?''',
            (datetime.datetime.now().strftime("%Y-%m-%d"), img.image_id))

        folder = util.DATA_FOLDER + str(pid) + "/reg_volumes_labels/"
        if os.path.exists(folder):
            shutil.rmtree(folder)
        os.makedirs(folder)
        vol_path = util.compress_vol(img.processed_filepath)
        shutil.copy(vol_path, folder)

        volume_db = str(pid) + "/reg_volumes_labels/" + basename(vol_path)
        cursor2 = conn.execute(
            '''UPDATE Images SET filepath_reg = ? WHERE id = ?''',
            (volume_db, img.image_id))

        cursor = conn.execute(
            '''SELECT filepath, id from Labels where image_id = ? ''',
            (img.image_id, ))
        for (filepath, label_id) in cursor:
            temp = util.compress_vol(
                move_vol(util.DATA_FOLDER + filepath, img.get_transforms(),
                         True))
            shutil.copy(temp, folder)
            label_db = str(pid) + "/reg_volumes_labels/" + basename(temp)
            cursor2 = conn.execute(
                '''UPDATE Labels SET filepath_reg = ? WHERE id = ?''',
                (label_db, label_id))

        conn.commit()
        cursor.close()
        cursor2.close()
    cursor = conn.cursor()

    modified_patients = [
        subfolder_name
        for subfolder_name in os.listdir(new_segmentations_folder) if
        os.path.isdir(os.path.join(new_segmentations_folder, subfolder_name))
    ]

    for pid in modified_patients:

        cursor.execute("SELECT id FROM Images WHERE pid = ?", (int(pid), ))
        image_id = cursor.fetchone()[0]
        img = img_data(image_id, util.DATA_FOLDER, util.TEMP_FOLDER_PATH)
        img.load_db_transforms()
        #cursor.execute("SELECT  filepath, filepath_reg FROM Labels WHERE image_id IN "
        #               "(SELECT id FROM Images WHERE pid = ?)", (int(pid),))
        #(label_path, label_reg_path) = cursor.fetchone()
        #cursor.execute("SELECT  transform FROM Images WHERE pid = ?", (int(pid),))
        #transform = cursor.fetchone()[0]
        new_segmentation_path = glob.glob(new_segmentations_folder + str(pid) +
                                          '/*label.nii')[0]
        print(new_segmentation_path)

        temp = util.compress_vol(
            move_vol(new_segmentation_path, img.get_transforms(), True))
        shutil.copy(temp, img.reg_label_filepath)
        shutil.copy(new_segmentation_path, img.label_filepath)

    #path = '/Volumes/Neuro/Segmentations/oppdaterte_filer/**/*label.nii'
    #glob.glob(path)
def update_segmentations(path):
    """Update existing patients with new segmentations from Even"""
    # pylint: disable= too-many-locals, too-many-branches, too-many-statements
    conn = sqlite3.connect(util.DB_PATH)
    cursor = conn.cursor()

    log = ""

    included_cases = path + "Included cases - final.xlsx"
    case_list = load_workbook(included_cases,data_only=True)['Included cases - final']
    for case in range(2, 213):

        cell_name = "{}{}".format("A", case)
        pid = str(case_list[cell_name].value)

        cell_name = "{}{}".format("B", case)
        new_segmentation = str(case_list[cell_name].value)

        data_path = path + "Segmenteringer/" + pid + "/"

        if new_segmentation == "1" and os.path.exists(util.DATA_FOLDER + pid + "/" ):# and pid == "711":
            print("Converting image " + pid)
            log = log + "\n Converting image " + pid
            
            label_file_endings = ('*label.nrrd',
                                  '*label_1.nrrd',
                                  'Segmentation/*label.nrrd',
                                  'Segmentering/*label.nrrd',
                                  '*hele-label.nii')       

            for file_ending in label_file_endings:
                volume_label = glob.glob(data_path + file_ending)    
                if volume_label:
                    break
            if len(volume_label) > 1:
                log = log + "\n Warning!! More than one file with label found "
                for volume_label_i in volume_label:
                    log = log + volume_label_i
                continue
            volume_label = volume_label[0]
            if not os.path.exists(volume_label):
                log = log + "\n Warning!! No label found " + data_path + volume_label
            
            cursor.execute("SELECT id FROM Images WHERE pid = ?", (pid,))
            image_id = cursor.fetchone()
            image_id = str(image_id[0])

            cursor.execute("SELECT transform FROM Images WHERE id = ?", (image_id,))
            transforms_temp = cursor.fetchone()
            if transforms_temp is None:
                log = log + "\n Warning!! No transform found for image " + image_id
            else:
                transforms_temp = str(transforms_temp[0])
            transforms = []
            for _transform in transforms_temp.split(","):
                transforms.append(util.DATA_FOLDER + _transform.strip())
             
            cursor.execute("SELECT filepath, filepath_reg FROM Labels WHERE image_id = ?", (image_id,))
                        
            for row in cursor:
                _filepath = row[0]
                _filepath_reg = row[1]
                try:
                    os.remove(util.DATA_FOLDER + _filepath)
                    os.remove(util.DATA_FOLDER + _filepath_reg)
                except OSError:
                    pass
                
                # Converting file to nii.gz if necessary
                os.system(CONVERTER_PATH + " '" + volume_label + "' " + util.DATA_FOLDER + _filepath)

                temp = util.compress_vol( image_registration.move_vol(util.DATA_FOLDER + _filepath, transforms, True) )
                shutil.copy(temp, util.DATA_FOLDER + _filepath_reg)


    with open("Log.txt", "w") as text_file:
        text_file.write(log)
    cursor.close()
    conn.close()