示例#1
0
    def _compute_graph(self, progress_callback=None):
        if self.graph is None:
            self.setStatusMessage('Building graph...')

            data = self.pca_projection if self.apply_pca else self.preprocessed_data

            self.graph = table_to_knn_graph(
                data,
                k_neighbors=self.k_neighbors,
                metric=METRICS[self.metric_idx][1],
                progress_callback=progress_callback,
            )
示例#2
0
def run_on_data(data, pca_components, k_neighbors, metric, resolution, state):
    # type: (Table, Optional[int], int, str, float, TaskState) -> Results
    """
    Run the louvain clustering on `data`.

    state is used to report progress and partial results. Returns early if
    `task.is_interuption_requested()` returns true.

    Parameters
    ----------
    data : Table
        Data table
    pca_components : Optional[int]
        If not `None` then the data is first projected onto first
        `pca_components` principal components.
    k_neighbors : int
        Passed to `table_to_knn_graph`
    metric : str
        Passed to `table_to_knn_graph`
    resolution : float
        Passed to `Louvain`
    state : TaskState

    Returns
    -------
    res : Results
    """
    state = state  # type: TaskState
    res = Results(
        pca_components=pca_components,
        k_neighbors=k_neighbors,
        metric=metric,
        resolution=resolution,
    )
    step = 0
    if state.is_interuption_requested():
        return res
    if pca_components is not None:
        steps = 3
        state.set_status("Computing PCA...")
        pca = PCA(n_components=pca_components, random_state=0)
        data = res.pca_projection = pca(data)(data)
        assert isinstance(data, Table)
        state.set_partial_results(("pca_projection", res.pca_projection))
        step += 1
    else:
        steps = 2

    if state.is_interuption_requested():
        return res

    state.set_progress_value(100. * step / steps)
    state.set_status("Building graph...")

    def pcallback(val):
        state.set_progress_value((100. * step + 100 * val) / steps)
        if state.is_interuption_requested():
            raise InteruptRequested()

    try:
        res.graph = graph = table_to_knn_graph(data,
                                               k_neighbors=k_neighbors,
                                               metric=metric,
                                               progress_callback=pcallback)
    except InteruptRequested:
        return res

    state.set_partial_results(("graph", res.graph))

    step += 1
    state.set_progress_value(100 * step / steps)
    state.set_status("Detecting communities...")
    if state.is_interuption_requested():
        return res

    louvain = Louvain(resolution=resolution, random_state=0)
    res.partition = louvain.fit_predict(graph)
    state.set_partial_results(("partition", res.partition))
    return res
示例#3
0
def run_on_data(data, pca_components, k_neighbors, metric, resolution, state):
    # type: (Table, Optional[int], int, str, float, TaskState) -> Results
    """
    Run the louvain clustering on `data`.

    state is used to report progress and partial results. Returns early if
    `task.is_interuption_requested()` returns true.

    Parameters
    ----------
    data : Table
        Data table
    pca_components : Optional[int]
        If not `None` then the data is first projected onto first
        `pca_components` principal components.
    k_neighbors : int
        Passed to `table_to_knn_graph`
    metric : str
        Passed to `table_to_knn_graph`
    resolution : float
        Passed to `Louvain`
    state : TaskState

    Returns
    -------
    res : Results
    """
    state = state  # type: TaskState
    res = Results(
        pca_components=pca_components, k_neighbors=k_neighbors, metric=metric,
        resolution=resolution,
    )
    step = 0
    if state.is_interuption_requested():
        return res
    if pca_components is not None:
        steps = 3
        state.set_status("Computing PCA...")
        pca = PCA(n_components=pca_components, random_state=0)
        data = res.pca_projection = pca(data)(data)
        assert isinstance(data, Table)
        state.set_partial_results(("pca_projection", res.pca_projection))
        step += 1
    else:
        steps = 2

    if state.is_interuption_requested():
        return res

    state.set_progress_value(100. * step / steps)
    state.set_status("Building graph...")

    def pcallback(val):
        state.set_progress_value((100. * step + 100 * val) / steps)
        if state.is_interuption_requested():
            raise InteruptRequested()

    try:
        res.graph = graph = table_to_knn_graph(
            data, k_neighbors=k_neighbors, metric=metric,
            progress_callback=pcallback
        )
    except InteruptRequested:
        return res

    state.set_partial_results(("graph", res.graph))

    step += 1
    state.set_progress_value(100 * step / steps)
    state.set_status("Detecting communities...")
    if state.is_interuption_requested():
        return res

    louvain = Louvain(resolution=resolution, random_state=0)
    res.partition = louvain.fit_predict(graph)
    state.set_partial_results(("partition", res.partition))
    return res