コード例 #1
0
def load_prediction_data(path_to_img, channels, x_scale, y_scale, z_scale,
                         normalize, mu, sig, region_of_interest):

    # read image data
    img, img_header, img_ext = load_data(path_to_img,
                                         'first_queue',
                                         return_extension=True)
    if img is None:
        InputError.message = "Invalid image data %s." % (
            os.path.basename(path_to_img))
        raise InputError()
    if img_ext != '.am':
        img_header = None
    z_shape, y_shape, x_shape = img.shape

    # automatic cropping of image to region of interest
    if np.any(region_of_interest):
        min_z, max_z, min_y, max_y, min_x, max_x = region_of_interest[:]
        min_z = min(min_z, z_shape)
        min_y = min(min_y, y_shape)
        min_x = min(min_x, x_shape)
        max_z = min(max_z, z_shape)
        max_y = min(max_y, y_shape)
        max_x = min(max_x, x_shape)
        if max_z - min_z < z_shape:
            min_z, max_z = 0, z_shape
        if max_y - min_y < y_shape:
            min_y, max_y = 0, y_shape
        if max_x - min_x < x_shape:
            min_x, max_x = 0, x_shape
        img = np.copy(img[min_z:max_z, min_y:max_y, min_x:max_x], order='C')
        region_of_interest = np.array([
            min_z, max_z, min_y, max_y, min_x, max_x, z_shape, y_shape, x_shape
        ])
        z_shape, y_shape, x_shape = max_z - min_z, max_y - min_y, max_x - min_x

    # scale image data
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    if normalize:
        mu_tmp, sig_tmp = np.mean(img), np.std(img)
        img = (img - mu_tmp) / sig_tmp
        img = img * sig + mu
        img[img < 0] = 0
        img[img > 1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)

    return img, img_header, position, z_shape, y_shape, x_shape, region_of_interest
コード例 #2
0
def load_refine_data(path_to_img, path_to_final, patch_size, normalize,
                     allLabels, mu, sig):

    # read image data
    img, _ = load_data(path_to_img, 'first_queue')
    if img is None:
        InputError.message = "Invalid image data %s." % (
            os.path.basename(path_to_img))
        raise InputError()
    z_shape, y_shape, x_shape = img.shape
    img = img.astype(np.float32)
    img -= np.amin(img)
    img /= np.amax(img)
    if normalize:
        mu_tmp, sig_tmp = np.mean(img), np.std(img)
        img = (img - mu_tmp) / sig_tmp
        img = img * sig + mu
        img[img < 0] = 0
        img[img > 1] = 1
    #img = make_axis_divisible_by_patch_size(img, patch_size)

    # load label data
    label, _ = load_data(path_to_final, 'first_queue')
    if label is None:
        InputError.message = "Invalid label data %s." % (
            os.path.basename(path_to_final))
        raise InputError()
    #label = make_axis_divisible_by_patch_size(label, patch_size)

    # labels must be in ascending order
    for k, l in enumerate(allLabels):
        label[label == l] = k

    # load final data and scale to [0,1]
    final = np.copy(label)
    final = final.astype(np.float32)
    final /= len(allLabels) - 1

    return img, label, final, z_shape, y_shape, x_shape
コード例 #3
0
def create_slices(path_to_data, path_to_label):

    try:

        if path_to_data:
            path_to_slices = path_to_data.replace('images', 'sliceviewer', 1)

        if path_to_label:
            path_to_label_slices = path_to_label.replace(
                'images', 'sliceviewer', 1)

        if path_to_data:

            # load data
            path_to_dir, extension = os.path.splitext(path_to_data)
            if extension == '.gz':
                path_to_dir, extension = os.path.splitext(path_to_dir)
            if extension == '.tar':
                img_names = []
                for data_type in [
                        '.tif', '.tiff', '.am', '.hdr', '.mhd', '.mha',
                        '.nrrd', '.nii', '.nii.gz'
                ]:
                    tmp_img_names = glob(path_to_dir + '/**/*' + data_type,
                                         recursive=True)
                    tmp_img_names = sorted(tmp_img_names)
                    img_names.extend(tmp_img_names)
                raw, _ = load_data(img_names[0], 'create_slices')
                zsh, ysh, xsh = raw.shape
                m = max(ysh, xsh)
                m = max(zsh, m)
                scale = float(256) / float(m)
                z_scale = int(zsh * scale)
                y_scale = int(ysh * scale)
                x_scale = int(xsh * scale)
                raw = img_resize(raw, z_scale, y_scale, x_scale)
                for name in img_names[1:]:
                    tmp, _ = load_data(name, 'create_slices')
                    tmp = img_resize(tmp, z_scale, y_scale, x_scale)
                    raw = np.append(raw, tmp, axis=0)
            else:
                raw, _ = load_data(path_to_data, 'create_slices')

            # increase contrast
            raw = img_to_uint8(raw)
            raw = contrast(raw)
            zsh, ysh, xsh = raw.shape

            if not path_to_label:

                # create slices for slice viewer
                if not os.path.isdir(path_to_slices):

                    # make directory
                    os.makedirs(path_to_slices)
                    os.chmod(path_to_slices, 0o770)

                    # reduce image size
                    m = min(ysh, xsh)
                    if m > 400:
                        scale = float(400) / float(m)
                        y_shape = int(ysh * scale)
                        x_shape = int(xsh * scale)
                        for k in range(zsh):
                            tmp = cv2.resize(raw[k], (x_shape, y_shape),
                                             interpolation=cv2.INTER_AREA)
                            tmp = tmp.astype(np.uint8)
                            im = Image.fromarray(tmp)
                            im.save(path_to_slices + '/%s.png' % (k))
                    else:
                        for k in range(zsh):
                            im = Image.fromarray(raw[k])
                            im.save(path_to_slices + '/%s.png' % (k))

        if path_to_label:

            # load data
            path_to_dir, extension = os.path.splitext(path_to_label)
            if extension == '.gz':
                path_to_dir, extension = os.path.splitext(path_to_dir)
            if extension == '.tar':
                img_names = []
                for data_type in [
                        '.tif', '.tiff', '.am', '.hdr', '.mhd', '.mha',
                        '.nrrd', '.nii', '.nii.gz'
                ]:
                    tmp_img_names = glob(path_to_dir + '/**/*' + data_type,
                                         recursive=True)
                    tmp_img_names = sorted(tmp_img_names)
                    img_names.extend(tmp_img_names)
                # load and scale label data corresponding to img data
                mask = np.zeros((0, y_scale, x_scale), dtype=np.uint8)
                for name in img_names:
                    arr, _ = load_data(name, 'create_slices')
                    arr = arr.astype(np.uint8)
                    np_unique = np.unique(arr)[1:]
                    next_mask = np.zeros((z_scale, y_scale, x_scale),
                                         dtype=arr.dtype)
                    for k in np_unique:
                        tmp = np.zeros_like(arr)
                        tmp[arr == k] = 1
                        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
                        next_mask[tmp == 1] = k
                    mask = np.append(mask, next_mask, axis=0)
            else:
                mask, _ = load_data(path_to_label, 'create_slices')

            # img to uint8
            mask = color_to_gray(mask)
            mask = img_to_uint8(mask)

            # create slices for slice viewer
            if path_to_data and mask.shape == raw.shape:

                if not os.path.isdir(path_to_label_slices):

                    # make directory
                    os.makedirs(path_to_label_slices)
                    os.chmod(path_to_label_slices, 0o770)

                    # define colors
                    Color = [(255, 0, 0), (255, 255, 0), (0, 0, 255),
                             (0, 100, 0), (0, 255, 0), (255, 165, 0),
                             (139, 0, 0), (255, 20, 147), (255, 105, 180),
                             (255, 0, 0), (139, 0, 139), (255, 0, 255),
                             (160, 32, 240), (184, 134, 11), (255, 185, 15),
                             (255, 215, 0), (0, 191, 255), (16, 78, 139),
                             (104, 131, 139), (255, 64, 64), (165, 42, 42),
                             (255, 127, 36), (139, 90, 43), (110, 139, 61),
                             (0, 255, 127), (255, 127, 80), (139, 10, 80),
                             (219, 112, 147), (178, 34, 34), (255, 48, 48),
                             (205, 79, 57), (160, 32, 240), (255, 100, 0)] * 8
                    labels = np.unique(mask)[1:]
                    Color = Color[:len(labels)]
                    labels = labels[:len(labels)]
                    Color = np.array(Color, dtype=np.uint8)

                    # reduce image size
                    m = min(ysh, xsh)
                    if m > 400:
                        scale = float(400) / float(m)
                        ysh = int(ysh * scale)
                        xsh = int(xsh * scale)

                    # allocate memory
                    out = np.empty((ysh, xsh, 3), dtype=np.uint8)
                    gradient = np.empty((ysh, xsh), dtype=np.uint8)

                    for k in range(zsh):

                        # resize slice
                        if m > 400:
                            raw_tmp = cv2.resize(raw[k], (xsh, ysh),
                                                 interpolation=cv2.INTER_AREA)
                            mask_tmp = np.zeros((ysh, xsh), dtype=mask.dtype)
                            for l in labels:
                                tmp = np.zeros_like(mask[k])
                                tmp[mask[k] == l] = 1
                                tmp = cv2.resize(tmp, (xsh, ysh),
                                                 interpolation=cv2.INTER_AREA)
                                mask_tmp[tmp == 1] = l
                            raw_tmp = raw_tmp.astype(np.uint8)
                            mask_tmp = mask_tmp.astype(np.uint8)
                        else:
                            raw_tmp = raw[k]
                            mask_tmp = mask[k]

                        # compute gradient
                        gradient.fill(0)
                        tmp = np.abs(mask_tmp[:-1] - mask_tmp[1:])
                        tmp[tmp > 0] = 1
                        gradient[:-1] += tmp
                        gradient[1:] += tmp
                        tmp = np.abs(mask_tmp[:, :-1] - mask_tmp[:, 1:])
                        tmp[tmp > 0] = 1
                        gradient[:, :-1] += tmp
                        gradient[:, 1:] += tmp

                        # create output slice
                        for l in range(3):
                            out[:, :, l] = raw_tmp

                        # colorize
                        for j, label in enumerate(labels):
                            C = Color[j]
                            tmp = np.logical_and(gradient > 0,
                                                 mask_tmp == label)
                            out[:, :][tmp] = C

                        # save slice
                        im = Image.fromarray(out)
                        im.save(path_to_label_slices + '/%s.png' % (k))
    except:
        pass
コード例 #4
0
def load_training_data_refine(path_to_model, x_scale, y_scale, z_scale, patch_size, z_patch, y_patch, x_patch, normalize, \
                    img_list, label_list, channels, stride_size, allLabels, mu, sig, batch_size):

    # get filenames
    img_names, label_names = [], []
    for img_name, label_name in zip(img_list, label_list):

        img_dir, img_ext = os.path.splitext(img_name)
        if img_ext == '.gz':
            img_dir, img_ext = os.path.splitext(img_dir)

        label_dir, label_ext = os.path.splitext(label_name)
        if label_ext == '.gz':
            label_dir, label_ext = os.path.splitext(label_dir)

        if img_ext == '.tar' and label_ext == '.tar':
            for data_type in [
                    '.am', '.tif', '.tiff', '.hdr', '.mhd', '.mha', '.nrrd',
                    '.nii', '.nii.gz'
            ]:
                tmp_img_names = glob(img_dir + '/**/*' + data_type,
                                     recursive=True)
                tmp_label_names = glob(label_dir + '/**/*' + data_type,
                                       recursive=True)
                tmp_img_names = sorted(tmp_img_names)
                tmp_label_names = sorted(tmp_label_names)
                img_names.extend(tmp_img_names)
                label_names.extend(tmp_label_names)
        else:
            img_names.append(img_name)
            label_names.append(label_name)

    # predict pre-final
    final = []
    for name in img_names:
        a, _ = load_data(name, 'first_queue')
        if a is None:
            InputError.message = "Invalid image data %s." % (
                os.path.basename(name))
            raise InputError()
        a = predict_pre_final(a, path_to_model, x_scale, y_scale, z_scale, z_patch, y_patch, x_patch, \
                              normalize, mu, sig, channels, stride_size, batch_size)
        a = a.astype(np.float32)
        a /= len(allLabels) - 1
        #a = make_axis_divisible_by_patch_size(a, patch_size)
        final.append(a)

    # load img data
    img = []
    for name in img_names:
        a, _ = load_data(name, 'first_queue')
        a = a.astype(np.float32)
        a -= np.amin(a)
        a /= np.amax(a)
        if normalize:
            mu_tmp, sig_tmp = np.mean(a), np.std(a)
            a = (a - mu_tmp) / sig_tmp
            a = a * sig + mu
            a[a < 0] = 0
            a[a > 1] = 1
        #a = make_axis_divisible_by_patch_size(a, patch_size)
        img.append(a)

    # load label data
    label = []
    for name in label_names:
        a, _ = load_data(name, 'first_queue')
        if a is None:
            InputError.message = "Invalid label data %s." % (
                os.path.basename(name))
            raise InputError()
        #a = make_axis_divisible_by_patch_size(a, patch_size)
        label.append(a)

    # labels must be in ascending order
    for i in range(len(label)):
        for k, l in enumerate(allLabels):
            label[i][label[i] == l] = k

    return img, label, final
コード例 #5
0
def load_training_data(normalize, img_list, label_list, channels, x_scale,
                       y_scale, z_scale, crop_data):

    # get filenames
    img_names, label_names = [], []
    for img_name, label_name in zip(img_list, label_list):

        img_dir, img_ext = os.path.splitext(img_name)
        if img_ext == '.gz':
            img_dir, img_ext = os.path.splitext(img_dir)

        label_dir, label_ext = os.path.splitext(label_name)
        if label_ext == '.gz':
            label_dir, label_ext = os.path.splitext(label_dir)

        if img_ext == '.tar' and label_ext == '.tar':
            for data_type in [
                    '.am', '.tif', '.tiff', '.hdr', '.mhd', '.mha', '.nrrd',
                    '.nii', '.nii.gz'
            ]:
                tmp_img_names = glob(img_dir + '/**/*' + data_type,
                                     recursive=True)
                tmp_label_names = glob(label_dir + '/**/*' + data_type,
                                       recursive=True)
                tmp_img_names = sorted(tmp_img_names)
                tmp_label_names = sorted(tmp_label_names)
                img_names.extend(tmp_img_names)
                label_names.extend(tmp_label_names)
            if len(img_names) == 0:
                InputError.message = "Invalid image TAR file."
                raise InputError()
            if len(label_names) == 0:
                InputError.message = "Invalid label TAR file."
                raise InputError()
        else:
            img_names.append(img_name)
            label_names.append(label_name)

    # load first label
    region_of_interest = None
    a, header, extension = load_data(label_names[0], 'first_queue', True)
    if a is None:
        InputError.message = "Invalid label data %s." % (os.path.basename(
            label_names[0]))
        raise InputError()
    if crop_data:
        region_of_interest = np.zeros(6)
        argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x = predict_blocksize(
            a)
        a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y, argmin_x:argmax_x],
                    order='C')
        region_of_interest += [
            argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x
        ]
    a = a.astype(np.uint8)
    np_unique = np.unique(a)
    label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
    for k in np_unique:
        tmp = np.zeros_like(a)
        tmp[a == k] = 1
        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
        label[tmp == 1] = k

    # load first img
    img, _ = load_data(img_names[0], 'first_queue')
    if img is None:
        InputError.message = "Invalid image data %s." % (os.path.basename(
            img_names[0]))
        raise InputError()
    if crop_data:
        img = np.copy(img[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                      order='C')
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    mu, sig = np.mean(img), np.std(img)

    for img_name, label_name in zip(img_names[1:], label_names[1:]):

        # append label
        a, _ = load_data(label_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid label data %s." % (
                os.path.basename(name))
            raise InputError()
        if crop_data:
            argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x = predict_blocksize(
                a)
            a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                        order='C')
            region_of_interest += [
                argmin_z, argmax_z, argmin_y, argmax_y, argmin_x, argmax_x
            ]
        a = a.astype(np.uint8)
        np_unique = np.unique(a)
        next_label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
        for k in np_unique:
            tmp = np.zeros_like(a)
            tmp[a == k] = 1
            tmp = img_resize(tmp, z_scale, y_scale, x_scale)
            next_label[tmp == 1] = k
        label = np.append(label, next_label, axis=0)

        # append image
        a, _ = load_data(img_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid image data %s." % (
                os.path.basename(name))
            raise InputError()
        if crop_data:
            a = np.copy(a[argmin_z:argmax_z, argmin_y:argmax_y,
                          argmin_x:argmax_x],
                        order='C')
        a = a.astype(np.float32)
        a = img_resize(a, z_scale, y_scale, x_scale)
        a -= np.amin(a)
        a /= np.amax(a)
        if normalize:
            mu_tmp, sig_tmp = np.mean(a), np.std(a)
            a = (a - mu_tmp) / sig_tmp
            a = a * sig + mu
        img = np.append(img, a, axis=0)

    # automatic cropping
    if crop_data:
        region_of_interest /= float(len(img_names))
        region_of_interest = np.round(region_of_interest)
        region_of_interest[region_of_interest < 0] = 0
        region_of_interest = region_of_interest.astype(int)

    # scale image data to [0,1]
    img[img < 0] = 0
    img[img > 1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)
        for k in range(len(img_names[1:])):
            a = np.copy(position)
            position = np.append(position, a, axis=0)

    # labels must be in ascending order
    allLabels = np.unique(label)
    for k, l in enumerate(allLabels):
        label[label == l] = k

    return img, label, position, allLabels, mu, sig, header, extension, region_of_interest
コード例 #6
0
def load_training_data(normalize, img_list, label_list, channels, x_scale, y_scale, z_scale,
        crop_data, x_puffer=25, y_puffer=25, z_puffer=25):

    # get filenames
    img_names, label_names = [], []
    for img_name, label_name in zip(img_list, label_list):

        # check for tarball
        img_dir, img_ext = os.path.splitext(img_name)
        if img_ext == '.gz':
            img_dir, img_ext = os.path.splitext(img_dir)

        label_dir, label_ext = os.path.splitext(label_name)
        if label_ext == '.gz':
            label_dir, label_ext = os.path.splitext(label_dir)

        if (img_ext == '.tar' and label_ext == '.tar') or (os.path.isdir(img_name) and os.path.isdir(label_name)):

            # extract files if necessary
            if img_ext == '.tar' and not os.path.exists(img_dir):
                tar = tarfile.open(img_name)
                tar.extractall(path=img_dir)
                tar.close()
            if label_ext == '.tar' and not os.path.exists(label_dir):
                tar = tarfile.open(label_name)
                tar.extractall(path=label_dir)
                tar.close()

            for data_type in ['.am','.tif','.tiff','.hdr','.mhd','.mha','.nrrd','.nii','.nii.gz']:
                tmp_img_names = glob(img_dir+'/**/*'+data_type, recursive=True)
                tmp_label_names = glob(label_dir+'/**/*'+data_type, recursive=True)
                tmp_img_names = sorted(tmp_img_names)
                tmp_label_names = sorted(tmp_label_names)
                img_names.extend(tmp_img_names)
                label_names.extend(tmp_label_names)
            if len(img_names)==0:
                InputError.message = "Invalid image TAR file."
                raise InputError()
            if len(label_names)==0:
                InputError.message = "Invalid label TAR file."
                raise InputError()
        else:
            img_names.append(img_name)
            label_names.append(label_name)

    # load first label
    a, header, extension = load_data(label_names[0], 'first_queue', True)
    if a is None:
        InputError.message = "Invalid label data %s." %(os.path.basename(label_names[0]))
        raise InputError()
    if crop_data:
        argmin_z,argmax_z,argmin_y,argmax_y,argmin_x,argmax_x = predict_blocksize(a, x_puffer, y_puffer, z_puffer)
        a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
    a = a.astype(np.uint8)
    np_unique = np.unique(a)
    label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
    for k in np_unique:
        tmp = np.zeros_like(a)
        tmp[a==k] = 1
        tmp = img_resize(tmp, z_scale, y_scale, x_scale)
        label[tmp==1] = k

    # load first img
    img, _ = load_data(img_names[0], 'first_queue')
    if img is None:
        InputError.message = "Invalid image data %s." %(os.path.basename(img_names[0]))
        raise InputError()
    if crop_data:
        img = np.copy(img[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
    img = img.astype(np.float32)
    img = img_resize(img, z_scale, y_scale, x_scale)
    img -= np.amin(img)
    img /= np.amax(img)
    mu, sig = np.mean(img), np.std(img)

    for img_name, label_name in zip(img_names[1:], label_names[1:]):

        # append label
        a, _ = load_data(label_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid label data %s." %(os.path.basename(name))
            raise InputError()
        if crop_data:
            argmin_z,argmax_z,argmin_y,argmax_y,argmin_x,argmax_x = predict_blocksize(a, x_puffer, y_puffer, z_puffer)
            a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
        a = a.astype(np.uint8)
        np_unique = np.unique(a)
        next_label = np.zeros((z_scale, y_scale, x_scale), dtype=a.dtype)
        for k in np_unique:
            tmp = np.zeros_like(a)
            tmp[a==k] = 1
            tmp = img_resize(tmp, z_scale, y_scale, x_scale)
            next_label[tmp==1] = k
        label = np.append(label, next_label, axis=0)

        # append image
        a, _ = load_data(img_name, 'first_queue')
        if a is None:
            InputError.message = "Invalid image data %s." %(os.path.basename(name))
            raise InputError()
        if crop_data:
            a = np.copy(a[argmin_z:argmax_z,argmin_y:argmax_y,argmin_x:argmax_x], order='C')
        a = a.astype(np.float32)
        a = img_resize(a, z_scale, y_scale, x_scale)
        a -= np.amin(a)
        a /= np.amax(a)
        if normalize:
            mu_tmp, sig_tmp = np.mean(a), np.std(a)
            a = (a - mu_tmp) / sig_tmp
            a = a * sig + mu
        img = np.append(img, a, axis=0)

    # scale image data to [0,1]
    img[img<0] = 0
    img[img>1] = 1

    # compute position data
    position = None
    if channels == 2:
        position = np.empty((z_scale, y_scale, x_scale), dtype=np.float32)
        position = compute_position(position, z_scale, y_scale, x_scale)
        position = np.sqrt(position)
        position /= np.amax(position)
        for k in range(len(img_names[1:])):
            a = np.copy(position)
            position = np.append(position, a, axis=0)

    # labels must be in ascending order
    allLabels, counts = np.unique(label, return_counts=True)
    for k, l in enumerate(allLabels):
        label[label==l] = k

    # configuration data
    configuration_data = np.array([channels, x_scale, y_scale, z_scale, normalize, mu, sig])

    return img, label, position, allLabels, configuration_data, header, extension, len(img_names), counts
コード例 #7
0
def remove_outlier(image_id, final_id, friend_id, label_id, fill_holes=True):

    # get objects
    try:
        image = Upload.objects.get(pk=image_id)
        final = Upload.objects.get(pk=final_id)
        friend = Upload.objects.get(pk=friend_id)
        label = Upload.objects.get(pk=label_id)
        success = True
    except Upload.DoesNotExist:
        success = False

    # path to data
    path_to_data = image.pic.path
    path_to_final = final.pic.path

    if success:

        # final filenames
        filename, extension = os.path.splitext(path_to_final)
        if extension == '.gz':
            extension = '.nii.gz'
            filename = filename[:-4]
        path_to_cleaned = filename + '.cleaned' + extension
        path_to_filled = filename + '.filled' + extension
        path_to_cleaned_filled = filename + '.cleaned.filled' + extension

        # load data and header
        final, header = load_data(path_to_final, 'cleanup')
        if extension not in ['.tif', '.am']:
            _, header = load_data(label.pic.path, 'cleanup')

        # remove outlier
        final_cleaned = clean(final, label.delete_outliers)

        try:
            # check if final still exists
            friend = Upload.objects.get(pk=friend_id)

            # save results
            path_to_cleaned = unique_file_path(path_to_cleaned,
                                               label.user.username)
            save_data(path_to_cleaned, final_cleaned, header, extension,
                      label.compression)

            # save django object
            shortfilename = os.path.basename(path_to_cleaned)
            pic_path = 'images/' + friend.user.username + '/' + shortfilename
            if fill_holes:
                Upload.objects.create(pic=pic_path,
                                      user=friend.user,
                                      project=friend.project,
                                      final=2,
                                      imageType=3,
                                      shortfilename=shortfilename,
                                      friend=friend_id)
            else:
                Upload.objects.create(pic=pic_path,
                                      user=friend.user,
                                      project=friend.project,
                                      final=6,
                                      imageType=3,
                                      shortfilename=shortfilename,
                                      friend=friend_id)

            # create slices for sliceviewer
            if config['OS'] == 'linux':
                q_slices = Queue('slices', connection=Redis())
                job = q_slices.enqueue_call(create_slices,
                                            args=(
                                                path_to_data,
                                                path_to_cleaned,
                                            ),
                                            timeout=-1)
            elif config['OS'] == 'windows':
                p = Process(target=create_slices,
                            args=(path_to_data, path_to_cleaned))
                p.start()
                p.join()

        except Upload.DoesNotExist:
            success = False

        # fill holes
        if fill_holes and success:

            final_filled = fill(final, label.fill_holes)
            final_cleaned_filled = final_cleaned + (final_filled - final)

            try:
                # check if final still exists
                friend = Upload.objects.get(pk=friend_id)

                # save results
                path_to_filled = unique_file_path(path_to_filled,
                                                  label.user.username)
                save_data(path_to_filled, final_filled, header, extension,
                          label.compression)
                path_to_cleaned_filled = unique_file_path(
                    path_to_cleaned_filled, label.user.username)
                save_data(path_to_cleaned_filled, final_cleaned_filled, header,
                          extension, label.compression)

                # save django object
                shortfilename = os.path.basename(path_to_cleaned_filled)
                pic_path = 'images/' + friend.user.username + '/' + shortfilename
                Upload.objects.create(pic=pic_path,
                                      user=friend.user,
                                      project=friend.project,
                                      final=8,
                                      imageType=3,
                                      shortfilename=shortfilename,
                                      friend=friend_id)
                shortfilename = os.path.basename(path_to_filled)
                pic_path = 'images/' + friend.user.username + '/' + shortfilename
                Upload.objects.create(pic=pic_path,
                                      user=friend.user,
                                      project=friend.project,
                                      final=7,
                                      imageType=3,
                                      shortfilename=shortfilename,
                                      friend=friend_id)

                # create slices for sliceviewer
                if config['OS'] == 'linux':
                    q_slices = Queue('slices', connection=Redis())
                    job = q_slices.enqueue_call(create_slices,
                                                args=(
                                                    path_to_data,
                                                    path_to_filled,
                                                ),
                                                timeout=-1)
                    job = q_slices.enqueue_call(create_slices,
                                                args=(
                                                    path_to_data,
                                                    path_to_cleaned_filled,
                                                ),
                                                timeout=-1)
                elif config['OS'] == 'windows':
                    p = Process(target=create_slices,
                                args=(path_to_data, path_to_filled))
                    p.start()
                    p.join()
                    p = Process(target=create_slices,
                                args=(path_to_data, path_to_cleaned_filled))
                    p.start()
                    p.join()

            except Upload.DoesNotExist:
                pass