How to use the visualqc.utils.read_image function in VisualQC

To help you get started, we’ve selected a few VisualQC examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github raamana / visualqc / visualqc / quantitative.py View on Github external
def load_unit(self, unit_id):
        """Loads the image data for display."""

        skip_subject = False

        self.images = dict()
        for img_name in self.image_names:
            ipath = realpath(pjoin(self.in_dir, unit_id, img_name))
            img_data = read_image(ipath, error_msg=img_name)

            if np.count_nonzero(img_data) == 0:
                skip_subject = True
                print('image {} is empty!'.format(img_name, self.current_unit_id))

            self.images[img_name] = scale_0to1(img_data)

        if not skip_subject:
            # TODO implement crop to extents for more than 2 images
            # self.image_one, self.image_two = crop_to_seg_extents(self.image_one,
            #                                                      self.image_two,
            #                                                      self.padding)

            self.slices = pick_slices(self.images[self.image_names[0]], # first img
                                      self.views, self.num_slices_per_view)
github raamana / visualqc / visualqc / t1_mri.py View on Github external
def load_unit(self, unit_id):
        """Loads the image data for display."""

        # starting fresh
        for attr in ('current_img_raw', 'current_img',
                     'saturated_img', 'tails_trimmed_img', 'background_img'):
            if hasattr(self, attr):
                delattr(self, attr)

        t1_mri_path = self.path_getter_inputs(unit_id)
        self.current_img_raw = read_image(t1_mri_path, error_msg='T1 mri')
        # crop and rescale
        self.current_img = scale_0to1(crop_image(self.current_img_raw, self.padding))
        self.currently_showing = None

        skip_subject = False
        if np.count_nonzero(self.current_img) == 0:
            skip_subject = True
            print('MR image is empty!')

        # # where to save the visualization to
        # out_vis_path = pjoin(self.out_dir, 'visual_qc_{}_{}'.format(self.vis_type, unit_id))

        return skip_subject
github raamana / visualqc / visualqc / defacing.py View on Github external
def load_unit(self, unit_id):
        """Loads the image data for display."""

        # starting fresh
        for attr in ('defaced_img', 'orig_img', 'render_img'):
            if hasattr(self, attr):
                delattr(self, attr)

        self.defaced_img = read_image(self.images_for_id[unit_id]['defaced'],
                                      error_msg='defaced mri')
        self.orig_img = read_image(self.images_for_id[unit_id]['original'],
                                   error_msg='T1 mri')

        self.render_img_list = list()
        for rimg_path in self.images_for_id[unit_id]['render']:
            try:
                self.render_img_list.append(imread(rimg_path))
            except:
                raise IOError('Unable to read the 3D rendered image @\n {}'
                              ''.format(rimg_path))

        # crop, trim, and rescale
        self.defaced_img = rescale_without_outliers(
            self.defaced_img, padding=self.padding,
            trim_percentile=cfg.defacing_trim_percentile)
github raamana / visualqc / visualqc / freesurfer.py View on Github external
def load_unit(self, unit_id):
        """Loads the image data for display."""

        t1_mri_path = get_freesurfer_mri_path(self.in_dir, unit_id, self.mri_name)
        fs_seg_path = get_freesurfer_mri_path(self.in_dir, unit_id, self.seg_name)

        temp_t1_mri = read_image(t1_mri_path, error_msg='T1 mri')
        temp_fs_seg = read_image(fs_seg_path, error_msg='segmentation')

        if temp_t1_mri.shape != temp_fs_seg.shape:
            raise ValueError('size mismatch! MRI: {} Seg: {}\n'
                             'Size must match in all dimensions.'.format(
                self.current_t1_mri.shape,
                temp_fs_seg.shape))

        skip_subject = False
        if self.vis_type in ('cortical_volumetric', 'cortical_contour'):
            temp_seg_uncropped, roi_set_is_empty = void_subcortical_symmetrize_cortical(temp_fs_seg)
        elif self.vis_type in ('labels_volumetric', 'labels_contour'):
            if self.label_set is not None:
                # TODO same colors for same labels is not guaranteed
                #   if one subject fewer labels than others
                #   due to remapping of labels for each subject
                temp_seg_uncropped, roi_set_is_empty = get_label_set(temp_fs_seg,
github raamana / visualqc / visualqc / vqc.py View on Github external
def _prepare_images(qcw, subject_id):
    """Actual routine to generate the visualizations. """

    # qcw.fs_dir, qcw.subject_id, qcw.mri_name, qcw.seg_name, qcw.out_dir, qcw.vis_type, qcw.label_set

    # we ensured these files exist and are not empty
    t1_mri_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.mri_name, qcw.vis_type)
    fs_seg_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.seg_name, qcw.vis_type)

    t1_mri = read_image(t1_mri_path, error_msg='T1 mri')
    fs_seg = read_image(fs_seg_path, error_msg='segmentation')

    if t1_mri.shape != fs_seg.shape:
        raise ValueError('size mismatch! MRI: {} Seg: {}\n'
                         'Size must match in all dimensions.'.format(t1_mri.shape, fs_seg.shape))

    skip_subject = False
    if qcw.label_set is not None:
        fs_seg, roi_set_empty = get_label_set(fs_seg, qcw.label_set)
        if roi_set_empty:
            skip_subject = True
            print('segmentation image for this subject does not contain requested label set!')

    if qcw.vis_type in ('cortical_volumetric', 'cortical_contour'):
        out_seg = void_subcortical_symmetrize_cortical(fs_seg)
    elif qcw.vis_type in ('labels_volumetric', 'labels_contour'):
github raamana / visualqc / visualqc / vqc.py View on Github external
def _prepare_images(qcw, subject_id):
    """Actual routine to generate the visualizations. """

    # qcw.fs_dir, qcw.subject_id, qcw.mri_name, qcw.seg_name, qcw.out_dir, qcw.vis_type, qcw.label_set

    # we ensured these files exist and are not empty
    t1_mri_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.mri_name, qcw.vis_type)
    fs_seg_path = get_path_for_subject(qcw.in_dir, subject_id, qcw.seg_name, qcw.vis_type)

    t1_mri = read_image(t1_mri_path, error_msg='T1 mri')
    fs_seg = read_image(fs_seg_path, error_msg='segmentation')

    if t1_mri.shape != fs_seg.shape:
        raise ValueError('size mismatch! MRI: {} Seg: {}\n'
                         'Size must match in all dimensions.'.format(t1_mri.shape, fs_seg.shape))

    skip_subject = False
    if qcw.label_set is not None:
        fs_seg, roi_set_empty = get_label_set(fs_seg, qcw.label_set)
        if roi_set_empty:
            skip_subject = True
            print('segmentation image for this subject does not contain requested label set!')

    if qcw.vis_type in ('cortical_volumetric', 'cortical_contour'):
        out_seg = void_subcortical_symmetrize_cortical(fs_seg)
    elif qcw.vis_type in ('labels_volumetric', 'labels_contour'):
        # TODO in addition to checking file exists, we need to requested labels exist, for label vis_type
github raamana / visualqc / visualqc / features.py View on Github external
Computes histogram over the intensity distribution over the entire scan, including brain, skull and background.

    Parameters
    ----------

    in_mri_path : str
        Path to an MRI scan readable by Nibabel

    Returns
    -------
    hist : ndarray
        Array of prob. densities for intensity

    """

    img = read_image(in_mri_path)
    # scaled, and reshaped
    arr_0to1 = scale_0to1(img).flatten()
    # compute prob. density
    hist, _ = np.histogram(arr_0to1, bins=num_bins, density=True)

    return hist