Exemple #1
0
    def launch(self, view_model):
        # type: (ICAModel) -> dict
        """Construct data for visualization and launch it."""
        ica_gid = view_model.datatype
        ica_index = self.load_entity_by_gid(ica_gid)

        slice_given = slice_str(
            (slice(None), slice(None), slice(view_model.i_svar),
             slice(view_model.i_mode)))
        if view_model.i_svar < 0 or view_model.i_svar >= ica_index.parsed_shape[
                2]:
            view_model.i_svar = 0
        if view_model.i_mode < 0 or view_model.i_mode >= ica_index.parsed_shape[
                3]:
            view_model.i_mode = 0
        slice_used = slice_str(
            (slice(None), slice(None), slice(view_model.i_svar),
             slice(view_model.i_mode)))

        with h5.h5_file_for_index(ica_index) as h5_file:
            unmixing_matrix = h5_file.unmixing_matrix[..., view_model.i_svar,
                                                      view_model.i_mode]
            prewhitening_matrix = h5_file.prewhitening_matrix[
                ..., view_model.i_svar, view_model.i_mode]
        Cinv = unmixing_matrix.dot(prewhitening_matrix)

        title = 'ICA region contribution -- (Ellipsis, %d, 0)' % (
            view_model.i_svar)
        labels = self.extract_source_labels(ica_index)
        pars = self.compute_params(ica_index, Cinv, title, [labels, labels],
                                   slice_given, slice_used,
                                   slice_given != slice_used)
        return self.build_display_result("matrix/svg_view", pars)
    def compute_2d_view(self, dtm_index, slice_s):
        # type: (DataTypeMatrix, str) -> (numpy.array, str, bool)
        """
        Create a 2d view of the matrix using the suggested slice
        If the given slice is invalid or fails to produce a 2d array the default is used
        which selects the first 2 dimensions.
        If the matrix is complex the real part is shown
        :param dtm_index: main input. It can have more then 2D
        :param slice_s: a string representation of a slice
        :return: (a 2d array,  the slice used to make it, is_default_returned)
        """
        default = (slice(None), slice(None)) + tuple(
            0 for _ in range(dtm_index.ndim - 2))  # [:,:,0,0,0,0 etc]
        slice_used = default

        try:
            if slice_s is not None and slice_s != "":
                slice_used = parse_slice(slice_s)
        except ValueError:  # if the slice could not be parsed
            self.log.warning("failed to parse the slice")

        try:
            with h5.h5_file_for_index(dtm_index) as h5_file:
                result_2d = h5_file.array_data[slice_used]
                result_2d = result_2d.astype(float)
            if result_2d.ndim > 2:  # the slice did not produce a 2d array, treat as error
                raise ValueError(str(dtm_index.shape))
        except (ValueError, IndexError,
                TypeError):  # if the slice failed to produce a 2d array
            self.log.warning("failed to produce a 2d array")
            return self.compute_2d_view(dtm_index, "")

        return result_2d, slice_str(slice_used), slice_used == default
Exemple #3
0
def compute_2d_view(matrix, slice_s):
    """
    Create a 2d view of the matrix using the suggested slice
    If the given slice is invalid or fails to produce a 2d array the default is used
    which selects the first 2 dimensions.
    If the matrix is complex the real part is shown
    :param matrix: main input. It can have more then 2D
    :param slice_s: a string representation of a slice
    :return: (a 2d array,  the slice used to make it, is_default_returned)
    """
    default = (slice(None), slice(None)) + tuple(
        0 for _ in range(matrix.ndim - 2))  # [:,:,0,0,0,0 etc]

    try:
        if slice_s is not None:
            matrix_slice = parse_slice(slice_s)
        else:
            matrix_slice = slice(None)

        m = matrix[matrix_slice]

        if m.ndim > 2:  # the slice did not produce a 2d array, treat as error
            raise ValueError(str(matrix.shape))

    except (
            IndexError, ValueError
    ):  # if the slice could not be parsed or it failed to produce a 2d array
        matrix_slice = default

    slice_used = slice_str(matrix_slice)
    return matrix[matrix_slice].astype(
        float), slice_used, matrix_slice == default