Ejemplo n.º 1
0
    def _reduce_search_space(self, neighbors_reduction_thr=18):
        """
        Wrapper function to discard clusters from the tractogram too far from
        the model and logging informations.
        :param neighbors_reduction_thr, float, distance in mm for thresholding
            to discard distant streamlines.
        """
        centroid_matrix = bundles_distances_mdf(self.model_centroids,
                                                self.centroids)
        centroid_matrix[centroid_matrix > neighbors_reduction_thr] = np.inf

        mins = np.min(centroid_matrix, axis=0)
        close_clusters_indices = np.array(np.where(mins != np.inf)[0],
                                          dtype=np.int32)

        self.neighb_indices = []
        for i in close_clusters_indices:
            self.neighb_indices.extend(self.wb_clusters_indices[i])
        self.neighb_indices = np.array(self.neighb_indices, dtype=np.int32)
        self.neighb_streamlines = reconstruct_streamlines_from_memmap(
            self.memmap_filenames, self.neighb_indices)

        self.neighb_centroids = [
            self.centroids[i] for i in close_clusters_indices
        ]

        return len(close_clusters_indices) > 0
Ejemplo n.º 2
0
def single_clusterize_and_rbx_init(args):
    """
    Wrapper function to multiprocess clustering executions and recobundles
    initialisation.

    Parameters
    ----------
    tmp_memmap_filename: tuple (3)
        Temporary filename for the data, offsets and lengths.

    parameters_list : tuple (3)
        clustering_thr : int
            Distance in mm (for QBx) to cluster the input tractogram.
        seed : int
            Value to initialize the RandomState of numpy.
        nb_points : int
            Number of points used for all resampling of streamlines.

    Returns
    -------
    rbx : dict
        Initialisation of the recobundles class using specific parameters.
    """
    tmp_memmap_filename = args[0]
    wb_streamlines = reconstruct_streamlines_from_memmap(tmp_memmap_filename)
    clustering_thr = args[1][0]
    seed = args[1][1]
    nb_points = args[2]

    rbx = {}
    base_thresholds = [45, 35, 25]
    rng = np.random.RandomState(seed)
    cluster_timer = time()
    # If necessary, add an extra layer (more optimal)
    if clustering_thr < 15:
        current_thr_list = base_thresholds + [15, clustering_thr]
    else:
        current_thr_list = base_thresholds + [clustering_thr]

    cluster_map = qbx_and_merge(wb_streamlines,
                                current_thr_list,
                                nb_pts=nb_points,
                                rng=rng,
                                verbose=False)
    clusters_indices = []
    for cluster in cluster_map.clusters:
        clusters_indices.append(cluster.indices)
    centroids = ArraySequence(cluster_map.centroids)
    clusters_indices = ArraySequence(clusters_indices)
    clusters_indices._data = clusters_indices._data.astype(np.int32)

    rbx[(seed, clustering_thr)] = RecobundlesX(tmp_memmap_filename,
                                               clusters_indices,
                                               centroids,
                                               nb_points=nb_points,
                                               rng=rng)
    logging.info('QBx with seed {0} at {1}mm took {2}sec. gave '
                 '{3} centroids'.format(seed, current_thr_list,
                                        round(time() - cluster_timer, 2),
                                        len(cluster_map.centroids)))
    return rbx