Skip to content

feat: Add extra input size properties#2424

Merged
fgvieira merged 11 commits intosnakemake:mainfrom
fgvieira:io_func
Mar 10, 2025
Merged

feat: Add extra input size properties#2424
fgvieira merged 11 commits intosnakemake:mainfrom
fgvieira:io_func

Conversation

@fgvieira
Copy link
Copy Markdown
Contributor

@fgvieira fgvieira commented Aug 25, 2023

Description

Add properties to return size of input per file, both in bytes and megabytes.

QC

  • The PR contains a test case for the changes or the changes are already covered by an existing test case.
  • The documentation (docs/) is updated to reflect the changes or this is not necessary (e.g. if the change does neither modify the language nor the behavior or functionalities of Snakemake).

Summary by CodeRabbit

  • New Features
    • Enhanced file size calculations with new properties for size in kilobytes, megabytes, and gigabytes.
    • Improved clarity and usability of the InputFiles class for better file size management.
  • Bug Fixes
    • Refined the size property to accurately reflect the total size of all files.

@sonarqubecloud
Copy link
Copy Markdown

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Dec 1, 2023

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

@fgvieira fgvieira changed the title feat: Add extra properties to input feat: Add extra size properties to input Feb 10, 2024
@fgvieira fgvieira changed the title feat: Add extra size properties to input feat: Add extra input size properties Feb 10, 2024
@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@sonarqubecloud
Copy link
Copy Markdown

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@sonarqubecloud
Copy link
Copy Markdown

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Nov 12, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

The changes made in the snakemake/io.py file focus on the InputFiles class, enhancing its functionality for file size calculations. The size property has been replaced with size_files, which now asynchronously computes the sizes of individual files. New properties have been introduced for size conversions to kilobytes, megabytes, and gigabytes, improving the granularity of size information. The original size_mb property has been updated to utilize the new size_files_mb, and additional properties for total size in kilobytes and gigabytes have been added.

Changes

File Change Summary
snakemake/io.py - Replaced size property with size_files method.
- Added properties: size_files_kb, size_files_mb, size_files_gb.
- Updated size_mb to utilize size_files_mb.
- Updated method signature of size and added new properties: size_kb, size_gb.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant InputFiles
    participant FileSystem

    User->>InputFiles: Request size
    InputFiles->>FileSystem: Calculate individual file sizes
    FileSystem-->>InputFiles: Return file sizes
    InputFiles-->>User: Return total size
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
snakemake/io.py (3)

1754-1758: Consider using asyncio.gather for concurrent size retrieval.

The current implementation awaits each file size sequentially. For better performance, consider using asyncio.gather to retrieve file sizes concurrently.

-    def size_files(self):
-        async def sizes():
-            return [await f.size() for f in self]
+    def size_files(self):
+        async def sizes():
+            return await asyncio.gather(*(f.size() for f in self))

1760-1771: Enhance clarity and maintainability of size conversion properties.

Consider the following improvements:

  1. Use constants for size conversion factors
  2. Use floating-point division for more precise results
  3. Add docstrings to document the units
+    # Constants for size conversions
+    _BYTES_IN_KB = 1024.0
+    _BYTES_IN_MB = _BYTES_IN_KB * 1024.0
+    _BYTES_IN_GB = _BYTES_IN_MB * 1024.0

     @property
     def size_files_kb(self):
+        """Returns a list of file sizes in kilobytes."""
-        return [f / 1024 for f in self.size_files]
+        return [f / self._BYTES_IN_KB for f in self.size_files]

     @property
     def size_files_mb(self):
+        """Returns a list of file sizes in megabytes."""
-        return [f / 1024 for f in self.size_files_kb]
+        return [f / self._BYTES_IN_MB for f in self.size_files]

     @property
     def size_files_gb(self):
+        """Returns a list of file sizes in gigabytes."""
-        return [f / 1024 for f in self.size_files_mb]
+        return [f / self._BYTES_IN_GB for f in self.size_files]

1772-1786: Enhance clarity and consistency of total size properties.

Consider adding docstrings and using the same constants for direct conversion from bytes.

     @property
     def size(self):
+        """Returns the total size in bytes."""
         return sum(self.size_files)

     @property
     def size_kb(self):
+        """Returns the total size in kilobytes."""
-        return sum(self.size_files_kb)
+        return self.size / self._BYTES_IN_KB

     @property
     def size_mb(self):
+        """Returns the total size in megabytes."""
-        return sum(self.size_files_mb)
+        return self.size / self._BYTES_IN_MB

     @property
     def size_gb(self):
+        """Returns the total size in gigabytes."""
-        return sum(self.size_files_gb)
+        return self.size / self._BYTES_IN_GB
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 1be9935 and 7f39401.

📒 Files selected for processing (1)
  • snakemake/io.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
snakemake/io.py (1)

Pattern **/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

Comment on lines +1754 to +1786
def size_files(self):
async def sizes():
return [await f.size() for f in self]

return sum(async_run(sizes()))
return async_run(sizes())

@property
def size_files_kb(self):
return [f / 1024 for f in self.size_files]

@property
def size_files_mb(self):
return [f / 1024 for f in self.size_files_kb]

@property
def size_files_gb(self):
return [f / 1024 for f in self.size_files_mb]

@property
def size(self):
return sum(self.size_files)

@property
def size_kb(self):
return sum(self.size_files_kb)

@property
def size_mb(self):
return self.size / 1024 / 1024
return sum(self.size_files_mb)

@property
def size_gb(self):
return sum(self.size_files_gb)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

Based on the search results, I can now provide a final response:

No test coverage found for new size properties, but they are used internally.

The size properties (size_kb, size_mb, size_gb) appear to be used internally by Snakemake for resource calculations, particularly for determining memory and disk requirements. However:

  • No dedicated unit tests were found for these properties in test_io.py
  • The properties are referenced in various resource calculations like input.size_mb for memory and disk allocation
  • The functionality is documented in the CLI help text

Recommendation:

  1. Add unit tests for these properties in tests/test_io.py to ensure correct size calculations
  2. Document these properties in the API documentation since they are used for resource calculations
🔗 Analysis chain

Verify test coverage and usage.

Please ensure that:

  1. Unit tests are added/updated to cover the new size properties
  2. The changes are documented in the user documentation

Let me check the test coverage specifically for these new size properties.


Let me check the actual implementation of these size properties to understand their usage.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for existing tests and documentation

# Check for test files
echo "Checking for test files..."
fd "test.*\.py$"

# Check for existing size property tests
echo "Checking for existing size property tests..."
rg -l "def test.*size" 

# Check for documentation files
echo "Checking for documentation files..."
fd ".*\.(rst|md)$"

# Check for existing documentation of size properties
echo "Checking for existing documentation of size properties..."
rg -l "size.*_[kmg]b"

Length of output: 2538


Script:

#!/bin/bash
# Check test_io.py for size-related tests since our changes are in io.py
echo "Checking test_io.py for size tests..."
cat tests/test_io.py

# Check tests.py since it was flagged in the size property search
echo "Checking tests.py for size tests..."
cat tests/tests.py

# Check the documentation mentions in rules.rst and cli.rst
echo "Checking documentation for size properties..."
cat docs/snakefiles/rules.rst
cat docs/executing/cli.rst

Length of output: 198809


Script:

#!/bin/bash
# Check for actual usage of size properties in the codebase
echo "Checking for usage of size properties..."
rg -A 2 "size_[kmg]b" --no-filename

# Check for any tests of size properties
echo "Checking for size property tests..."
rg -A 2 "def test.*size" tests/test_io.py

Length of output: 2576

@sonarqubecloud
Copy link
Copy Markdown

@sonarqubecloud
Copy link
Copy Markdown

sonarqubecloud bot commented Mar 3, 2025

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (7)
snakemake/io.py (7)

1775-1780: Consider concurrent file-size retrieval to improve scalability.

Currently, the code awaits each file size in sequence within the sizes() coroutine, which can become slow for a large set of files. Leveraging concurrent tasks (e.g., asyncio.gather) inside sizes() would reduce total wait time.

A possible approach (shown as a diff example):

 def size_files(self):
     async def sizes():
-        return [await f.size() for f in self]
+        tasks = [asyncio.create_task(f.size()) for f in self]
+        return await asyncio.gather(*tasks)
     return async_run(sizes())

1781-1784: Use a named constant for clarity when converting to kilobytes.

Dividing by 1024 in multiple places works, but introducing a local constant (e.g., KB_FACTOR = 1024) can improve readability and maintainability.


1785-1788: Avoid repeated 1024-based divisions for MB calculations.

Since MB is just KB/1024, repeated divisions might introduce minor floating-point precision drift. Using a single step (e.g., size_in_mb = size_in_bytes / (1024**2)) can be clearer.


1789-1792: Consider a single-step conversion for GB.

As with MB, using a single-step approach for GB can make the conversion more explicit (e.g., (f / 1024**3)) and slightly reduce the risk of floating-point drift from multiple divisions.


1793-1796: Cache or store the total size to avoid repeated async calls.

Each property call to size_files triggers a new asynchronous retrieval. Summing again in size leads to repeated I/O. Caching the result (e.g., via a memoized approach) can improve performance if size is repeatedly accessed.


1797-1799: Consider memoizing the total size in KB.

Similar to size, calling size_files_kb reruns asynchronous lookups. A shared cached result or lazy evaluation would prevent recalculations on subsequent property accesses.


1805-1807: Add dedicated tests for GB calculations.

As a continuation of the above point, including checks for edge cases in size_gb (e.g., empty files or very large files) would foster confidence in the correctness of these conversions.

Would you like help drafting these additional test cases?

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f39401 and 4148de3.

📒 Files selected for processing (1)
  • snakemake/io.py (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.py`: Do not try to improve formatting. Do not suggest ...

**/*.py: Do not try to improve formatting.
Do not suggest type annotations for functions that are defined inside of functions or methods.
Do not suggest type annotation of the self argument of methods.
Do not suggest type annotation of the cls argument of classmethods.
Do not suggest return type annotation if a function or method does not contain a return statement.

  • snakemake/io.py
⏰ Context from checks skipped due to timeout of 90000ms (40)
  • GitHub Check: testing (10, 3.12, dash)
  • GitHub Check: testing (10, 3.12, bash)
  • GitHub Check: testing (10, 3.11, bash)
  • GitHub Check: testing (9, 3.12, dash)
  • GitHub Check: testing (9, 3.12, bash)
  • GitHub Check: testing (9, 3.11, bash)
  • GitHub Check: testing (8, 3.12, dash)
  • GitHub Check: testing (8, 3.12, bash)
  • GitHub Check: testing (8, 3.11, bash)
  • GitHub Check: testing (7, 3.12, dash)
  • GitHub Check: testing (7, 3.12, bash)
  • GitHub Check: testing (7, 3.11, bash)
  • GitHub Check: testing (6, 3.12, dash)
  • GitHub Check: testing (6, 3.12, bash)
  • GitHub Check: testing (6, 3.11, bash)
  • GitHub Check: testing (5, 3.12, dash)
  • GitHub Check: testing (5, 3.12, bash)
  • GitHub Check: testing (5, 3.11, bash)
  • GitHub Check: testing (4, 3.12, dash)
  • GitHub Check: testing-windows (10)
  • GitHub Check: testing (4, 3.12, bash)
  • GitHub Check: testing-windows (9)
  • GitHub Check: testing (4, 3.11, bash)
  • GitHub Check: testing-windows (8)
  • GitHub Check: testing (3, 3.12, dash)
  • GitHub Check: testing (3, 3.12, bash)
  • GitHub Check: testing (3, 3.11, bash)
  • GitHub Check: testing-windows (7)
  • GitHub Check: testing (2, 3.12, dash)
  • GitHub Check: testing (2, 3.12, bash)
  • GitHub Check: testing-windows (6)
  • GitHub Check: testing (2, 3.11, bash)
  • GitHub Check: testing-windows (5)
  • GitHub Check: testing (1, 3.12, dash)
  • GitHub Check: testing-windows (4)
  • GitHub Check: testing (1, 3.12, bash)
  • GitHub Check: testing-windows (3)
  • GitHub Check: testing (1, 3.11, bash)
  • GitHub Check: testing-windows (2)
  • GitHub Check: testing-windows (1)
🔇 Additional comments (1)
snakemake/io.py (1)

1803-1804: Enhance test coverage for the new size properties.

The size_mb property (and others) lack dedicated unit tests. Ensure correctness by adding tests that validate both the numeric results and the asynchronous retrieval logic.

@fgvieira fgvieira self-assigned this Mar 4, 2025
@fgvieira fgvieira merged commit 359ae2e into snakemake:main Mar 10, 2025
50 checks passed
@fgvieira fgvieira deleted the io_func branch March 10, 2025 08:18
johanneskoester pushed a commit that referenced this pull request Mar 11, 2025
🤖 I have created a release *beep* *boop*
---


##
[8.30.0](v8.29.3...v8.30.0)
(2025-03-11)


### Features

* Add extra input size properties
([#2424](#2424))
([359ae2e](359ae2e))
* shell function calls inside of the 'run' directive now use conda,
container, or envmodules specifications
([#2289](#2289))
([0193e34](0193e34))
* xonsh support for script directive
([#3310](#3310))
([d1c369b](d1c369b))


### Bug Fixes

* include unit test templates in `setup.py`
([#3362](#3362))
([b47252c](b47252c))


### Documentation

* clearly explain report rendering to ZIP archive
([#3357](#3357))
([948e8fb](948e8fb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
### Description

<!--Add a description of your PR here-->

Add properties to return size of `input` per file, both in bytes and
megabytes.

### QC
<!-- Make sure that you can tick the boxes below. -->

* [x] The PR contains a test case for the changes or the changes are
already covered by an existing test case.
* [x] The documentation (`docs/`) is updated to reflect the changes or
this is not necessary (e.g. if the change does neither modify the
language nor the behavior or functionalities of Snakemake).


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced file size calculations with new properties for size in
kilobytes, megabytes, and gigabytes.
- Improved clarity and usability of the `InputFiles` class for better
file size management.
- **Bug Fixes**
- Refined the `size` property to accurately reflect the total size of
all files.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
kjohnsen pushed a commit to kjohnsen/snakemake that referenced this pull request Dec 15, 2025
🤖 I have created a release *beep* *boop*
---


##
[8.30.0](snakemake/snakemake@v8.29.3...v8.30.0)
(2025-03-11)


### Features

* Add extra input size properties
([snakemake#2424](snakemake#2424))
([359ae2e](snakemake@359ae2e))
* shell function calls inside of the 'run' directive now use conda,
container, or envmodules specifications
([snakemake#2289](snakemake#2289))
([0193e34](snakemake@0193e34))
* xonsh support for script directive
([snakemake#3310](snakemake#3310))
([d1c369b](snakemake@d1c369b))


### Bug Fixes

* include unit test templates in `setup.py`
([snakemake#3362](snakemake#3362))
([b47252c](snakemake@b47252c))


### Documentation

* clearly explain report rendering to ZIP archive
([snakemake#3357](snakemake#3357))
([948e8fb](snakemake@948e8fb))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants