示例#1
0
def compute_DVARS(func_img, mean_img=None, mask=None, apply_mask=False):
    """Computes the DVARS for a given fMRI image."""

    if mean_img is None:
        mean_img = np.mean(func_img, axis=3)

    if apply_mask:
        if mask is None:
            mask = mask_image(mean_img)
        mean_img[np.logical_not(mask)] = 0.0

    num_time_points = func_img.shape[3]

    RMS_diff = lambda img2, img1: np.sqrt(np.mean(np.square(img2 - img1)))
    DVARS_1_to_N = [
        RMS_diff(func_img[:, :, :, t], func_img[:, :, :, t - 1])
        for t in range(1, num_time_points)
    ]

    DVARS = np.full(num_time_points, np.nan)
    # dvars value at time point 0 is set to 0
    DVARS[0] = 0.0
    DVARS[1:] = DVARS_1_to_N

    return DVARS
示例#2
0
    def compute_stats(self):
        """Computes the necessary stats to be displayed."""

        mean_img_temporal, stdev_img_temporal = temporal_stats(
            self.img_this_unit)
        mean_signal_spatial, stdev_signal_spatial = spatial_stats(
            self.img_this_unit)
        dvars = compute_DVARS(self.img_this_unit)

        for stat, sname in zip(
            (mean_signal_spatial, stdev_signal_spatial, dvars),
            ('mean_signal_spatial', 'stdev_signal_spatial', 'dvars')):
            if len(stat) != self.img_this_unit.shape[3]:
                raise ValueError(
                    'ERROR: lengths of different stats do not match!')
            if any(np.isnan(stat)):
                raise ValueError(
                    'ERROR: invalid values in stat : {}'.format(sname))

        mask = mask_image(mean_img_temporal,
                          update_factor=0.9,
                          init_percentile=5)
        carpet = self.make_carpet(mask)

        return carpet, mean_signal_spatial, stdev_signal_spatial, dvars
示例#3
0
    def _compute_background(self):
        """Computes the background image for the current image."""

        if not hasattr(self, 'background_img'):
            # need to scale the mask, as Collage class does NOT automatically rescale
            self.foreground_mask = mask_image(self.current_img, out_dtype=bool)
            temp_background_img = np.copy(self.current_img)
            temp_background_img[self.foreground_mask] = 0.0
            self.background_img = scale_0to1(temp_background_img,
                                             exclude_outliers_below=1,
                                             exclude_outliers_above=1)