Skip to content

Improve kmer matrix generation#76

Open
mzueva wants to merge 1 commit intomainfrom
mzueva/imrove-kmer-matrix
Open

Improve kmer matrix generation#76
mzueva wants to merge 1 commit intomainfrom
mzueva/imrove-kmer-matrix

Conversation

@mzueva
Copy link
Copy Markdown
Contributor

@mzueva mzueva commented Mar 4, 2026

Improve kmer matrix generation

@mzueva
Copy link
Copy Markdown
Contributor Author

mzueva commented Mar 4, 2026

/gemini review

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request focuses on improving the efficiency of k-mer matrix generation. It introduces optimizations in parallel processing and sparse matrix handling to reduce computational overhead and improve overall performance. The changes are primarily centered around the _process_sequence_chunk function and the main kmer_count_vectors function, enhancing both single-threaded and multi-threaded execution paths.

Highlights

  • Parallel K-mer Counting: The pull request enhances the parallel k-mer counting process by aggregating counts locally per chunk and utilizing sparse sub-matrix stacking, leading to significant performance improvements.
  • Sparse Matrix Handling: The update replaces the previous method of merging results from parallel workers with a more efficient approach of stacking sparse sub-matrices vertically, which reduces memory overhead and improves processing speed.
  • Code Optimization: The changes include removing unnecessary imports and streamlining the data processing flow within the worker function, contributing to cleaner and more efficient code.
Changelog
  • software/umap/src/main.py
    • Optimized k-mer counting by aggregating counts locally per chunk.
    • Improved parallel processing by stacking sparse sub-matrices.
    • Removed unnecessary imports and streamlined data processing.
Activity
  • The pull request introduces significant performance improvements in k-mer matrix generation.
  • The changes optimize both single-threaded and multi-threaded execution paths.
  • The update streamlines data processing and reduces memory overhead.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request significantly improves the k-mer matrix generation process by optimizing parallel processing. The _process_sequence_chunk function now directly computes sparse sub-matrices, and these are efficiently combined using sparse.vstack in kmer_count_vectors. This change is expected to reduce memory overhead and improve performance, especially for large datasets, by avoiding the collection and merging of raw row/column indices. However, there are a few areas for minor improvement related to argument redundancy and import placement.

Args:
args: Tuple of (sequences_chunk, start_idx, k, kmer_to_index)
args: Tuple of (sequences_chunk, start_idx, k, kmer_to_index, num_kmers)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The start_idx argument is listed in the docstring for _process_sequence_chunk, but it is no longer used within the function body. This creates a discrepancy between the documentation and the implementation. It should be removed from the docstring.

Suggested change
args: Tuple of (sequences_chunk, start_idx, k, kmer_to_index, num_kmers)
args: Tuple of (sequences_chunk, k, kmer_to_index, num_kmers)

"""
sequences_chunk, start_idx, k, kmer_to_index = args

from collections import Counter
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The collections.Counter module is imported inside the _process_sequence_chunk function. This means Counter will be imported every time this function is called, which can introduce a minor performance overhead, especially in a parallel processing context where this function is called many times. It's generally better practice to place all module imports at the top of the file.

sequences_chunk, start_idx, k, kmer_to_index = args

from collections import Counter
sequences_chunk, start_idx, k, kmer_to_index, num_kmers = args
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The start_idx variable is unpacked from the args tuple but is not used anywhere within the _process_sequence_chunk function. It was previously used to calculate global_idx, which has been removed. This makes start_idx a redundant argument.

Suggested change
sequences_chunk, start_idx, k, kmer_to_index, num_kmers = args
sequences_chunk, k, kmer_to_index, num_kmers = args

chunk_end = min(i + chunk_size, num_seqs)
chunks.append((sequences[i:chunk_end], i, k, kmer_to_index))
chunks.append((sequences[i:chunk_end], i, k, kmer_to_index, num_kmers))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The start_idx (represented by i) is still being passed as part of the chunk tuple to _process_sequence_chunk, even though _process_sequence_chunk no longer uses it. This is redundant and can be removed to simplify the data passed to worker processes.

Suggested change
chunks.append((sequences[i:chunk_end], i, k, kmer_to_index, num_kmers))
chunks.append((sequences[i:chunk_end], k, kmer_to_index, num_kmers))

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request significantly improves the performance of k-mer matrix generation by refactoring the parallel processing logic. Instead of collecting raw indices from worker processes and aggregating them in the main process, each worker now constructs a sparse sub-matrix. These sub-matrices are then efficiently stacked in the main process. This is a great improvement that should reduce memory usage and execution time. I have a couple of suggestions for further code cleanup and minor optimization.

sequences_chunk, start_idx, k, kmer_to_index = args

from collections import Counter
sequences_chunk, start_idx, k, kmer_to_index, num_kmers = args
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The start_idx variable is unpacked from args but it is not used within this function. It seems to be a remnant from a previous implementation. To improve code clarity, you should consider removing it from this function's argument list and also from the call sites in kmer_count_vectors (lines 161 and 170).

Comment on lines +102 to +110
rows_final = np.array([r for r, c in data_dict.keys()], dtype=np.int32)
cols_final = np.array([c for r, c in data_dict.keys()], dtype=np.int32)
data_final = np.array(list(data_dict.values()), dtype=np.int32)

return sparse.coo_matrix(
(data_final, (rows_final, cols_final)),
shape=(num_seqs, num_kmers),
dtype=np.int32
).tocsr()
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This section can be refactored to be more efficient and concise. By converting the dictionary keys to a NumPy array directly, you can avoid iterating over them twice and use NumPy slicing to extract rows and columns. This is generally more performant and idiomatic with NumPy.

    coords = np.array(list(data_dict.keys()), dtype=np.int32)
    data_final = np.array(list(data_dict.values()), dtype=np.int32)

    return sparse.coo_matrix(
        (data_final, (coords[:, 0], coords[:, 1])),
        shape=(num_seqs, num_kmers),
        dtype=np.int32
    ).tocsr()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant