Exemple #1
0
def detect_spikes_time_slice_diffs(array,
                                   zalph=5.,
                                   time_axis=-1,
                                   slice_axis=-2,
                                   output_fname=None,
                                   title=None):
    """ Detect spiked slices using the time slice difference as a criterion.

    Parameters
    ----------
    array: array (mandatory)
        array where the time is the last dimension.
    zalph: float (optional default 5)
        cut off for the sum of square.
    time_axis: int (optional, default -1)
        axis of the input array that varies over time. The default is the last
        axis.
    slice_axis: int (optional default -2)
        axis of the array that varies over image slice. The default is the last
        non-time axis.
    output_fname: str (optional default None)
        the output file name where the image that represents the detected
        spikes is saved.

    Returns
    -------
    slices_to_correct : dict
        the timepoints where spikes are detected as keys and the corresponding
        spiked slices.
    spikes: array (T-1, S)
        all the detected spikes.
    """
    # Reshape the input array: roll time and slice axis
    logger.info("Input array shape is '%s'", array.shape)
    array = format_time_serie(array, time_axis, slice_axis)
    logger.info(
        "Reshape array shape is '%s' when time axis is '%s' and slice "
        "axis is '%s'", array.shape, time_axis, slice_axis)

    # Time-point to time-point differences over and slices
    logger.info("Computing time-point to time-point differences over "
                "slices...")
    smd2 = time_slice_diffs(array)
    logger.info(
        "Metric smd2 shape is '%s', ie. (number of timepoints - 1, "
        "number of slices).", smd2.shape)

    # Detect spikes from quared difference
    spikes, meds, mads = spikes_from_slice_diff(smd2, zalph)

    # Filter the spikes to preserve outliers only
    final_spikes = final_detection(spikes)

    # Find which timepoints and which slices are affected
    times_to_correct = np.where(final_spikes.sum(axis=1) > 0)[0]

    slices_to_correct = {}
    for timepoint in times_to_correct:
        slices_to_correct[timepoint] = np.where(
            final_spikes[timepoint, :] > 0)[0]

    # Information message
    logger.info("Total number of outliers found: '%s'.", spikes.sum())
    logger.info("Total number of slices to be corrected: '%s'.",
                slices_to_correct)

    # Display detected spikes
    if output_fname is not None:
        display_spikes(array, smd2, spikes, meds, mads, zalph, output_fname,
                       title)

    return slices_to_correct, spikes
Exemple #2
0
def detect_spikes_time_slice_vals(array, time_axis=-1, slice_axis=-2, sigma=2,
                                  output_fname=None):
    """ Detect spiked slices using the time slice values as a criterion.

    Parameters
    ----------
    array: array (mandatory)
        array where the time is the last dimension.
    time_axis: int (optional, default -1)
        axis of the input array that varies over time. The default is the last
        axis.
    slice_axis: int (optional default -2)
        axis of the array that varies over image slice. The default is the last
        non-time axis.
    sigma: float (optional default 3)
        cut off for the sum of square.
    output_fname: str (optional default None)
        the output file name where the image that represents the detected
        spikes is saved.

    Returns
    -------
    slices_to_correct : dict
        the timepoints where spikes are detected as keys and the corresponding
        spiked slices.
    """
    # Reshape the input array: roll time and slice axis
    logger.info("Input array shape is '%s'", array.shape)
    array = format_time_serie(array, time_axis, slice_axis)
    logger.info("Reshape array shape is '%s' when time axis is '%s' and slice "
                "axis is '%s'", array.shape, time_axis, slice_axis)

    # Go through all time-points
    gaussians = []
    for timepoint in range(array.shape[0]):

        # Go through all slices and compute the mutual information to the
        # the referecne first slice
        reference_slice = array[timepoint, 0]
        mi_values = []
        for sliceid in range(array.shape[1]):

            # Compute the mutual information between slices
            mi_values.append(mutual_information(
                reference_slice, array[timepoint, sliceid], bins=256))

        # Remove the mi corresponding to MI(X, X) = H(X)
        mi_values = np.asarray(mi_values[1:])

        # Make the assumption of a gaussian distribution
        mean = np.mean(mi_values)
        std = np.std(mi_values)

        # Reject slice to far from the distribution +/-n sigma
        lower_bound = mean - sigma * std
        upper_bound = mean + sigma * std

        # Store the result
        gaussians.append({
            "dist": mi_values,
            "mean": mean,
            "bounds": (lower_bound, upper_bound),
            "outliers": []
        })
        print np.where(mi_values < lower_bound)
        print np.where(mi_values > upper_bound)

    # Display nicely the slice distributions
    if output_fname is not None:
        display_slice_distributions(gaussians, 18, output_fname)
Exemple #3
0
def detect_spikes_time_slice_vals(array,
                                  time_axis=-1,
                                  slice_axis=-2,
                                  sigma=2,
                                  output_fname=None):
    """ Detect spiked slices using the time slice values as a criterion.

    Parameters
    ----------
    array: array (mandatory)
        array where the time is the last dimension.
    time_axis: int (optional, default -1)
        axis of the input array that varies over time. The default is the last
        axis.
    slice_axis: int (optional default -2)
        axis of the array that varies over image slice. The default is the last
        non-time axis.
    sigma: float (optional default 3)
        cut off for the sum of square.
    output_fname: str (optional default None)
        the output file name where the image that represents the detected
        spikes is saved.

    Returns
    -------
    slices_to_correct : dict
        the timepoints where spikes are detected as keys and the corresponding
        spiked slices.
    """
    # Reshape the input array: roll time and slice axis
    logger.info("Input array shape is '%s'", array.shape)
    array = format_time_serie(array, time_axis, slice_axis)
    logger.info(
        "Reshape array shape is '%s' when time axis is '%s' and slice "
        "axis is '%s'", array.shape, time_axis, slice_axis)

    # Go through all time-points
    gaussians = []
    for timepoint in range(array.shape[0]):

        # Go through all slices and compute the mutual information to the
        # the referecne first slice
        reference_slice = array[timepoint, 0]
        mi_values = []
        for sliceid in range(array.shape[1]):

            # Compute the mutual information between slices
            mi_values.append(
                mutual_information(reference_slice,
                                   array[timepoint, sliceid],
                                   bins=256))

        # Remove the mi corresponding to MI(X, X) = H(X)
        mi_values = np.asarray(mi_values[1:])

        # Make the assumption of a gaussian distribution
        mean = np.mean(mi_values)
        std = np.std(mi_values)

        # Reject slice to far from the distribution +/-n sigma
        lower_bound = mean - sigma * std
        upper_bound = mean + sigma * std

        # Store the result
        gaussians.append({
            "dist": mi_values,
            "mean": mean,
            "bounds": (lower_bound, upper_bound),
            "outliers": []
        })
        print np.where(mi_values < lower_bound)
        print np.where(mi_values > upper_bound)

    # Display nicely the slice distributions
    if output_fname is not None:
        display_slice_distributions(gaussians, 18, output_fname)
Exemple #4
0
def detect_spikes_time_slice_diffs(array, zalph=5., time_axis=-1,
                                   slice_axis=-2, output_fname=None,
                                   title=None):
    """ Detect spiked slices using the time slice difference as a criterion.

    Parameters
    ----------
    array: array (mandatory)
        array where the time is the last dimension.
    zalph: float (optional default 5)
        cut off for the sum of square.
    time_axis: int (optional, default -1)
        axis of the input array that varies over time. The default is the last
        axis.
    slice_axis: int (optional default -2)
        axis of the array that varies over image slice. The default is the last
        non-time axis.
    output_fname: str (optional default None)
        the output file name where the image that represents the detected
        spikes is saved.

    Returns
    -------
    slices_to_correct : dict
        the timepoints where spikes are detected as keys and the corresponding
        spiked slices.
    spikes: array (T-1, S)
        all the detected spikes.
    """
    # Reshape the input array: roll time and slice axis
    logger.info("Input array shape is '%s'", array.shape)
    array = format_time_serie(array, time_axis, slice_axis)
    logger.info("Reshape array shape is '%s' when time axis is '%s' and slice "
                "axis is '%s'", array.shape, time_axis, slice_axis)

    # Time-point to time-point differences over and slices
    logger.info("Computing time-point to time-point differences over "
                "slices...")
    smd2 = time_slice_diffs(array)
    logger.info("Metric smd2 shape is '%s', ie. (number of timepoints - 1, "
                "number of slices).", smd2.shape)

    # Detect spikes from quared difference
    spikes = spikes_from_slice_diff(smd2, zalph)

    # Filter the spikes to preserve outliers only
    final_spikes = final_detection(spikes)

    # Find which timepoints and which slices are affected
    times_to_correct = np.where(final_spikes.sum(axis=1) > 0)[0]
    slices_to_correct = {}
    for timepoint in times_to_correct:
        slices_to_correct[timepoint] = np.where(
            final_spikes[timepoint, :] > 0)[0]

    # Information message
    logger.info("Total number of outliers found: '%s'.", spikes.sum())
    logger.info("Total number of slices to be corrected: '%s'.",
                slices_to_correct)

    # Display detected spikes
    if output_fname is not None:
        display_spikes(array, smd2, spikes, output_fname, title)

    return slices_to_correct, spikes