Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to export to CSV via sleap-convert and API #1730

Merged
merged 15 commits into from
Apr 9, 2024

Conversation

eberrigan
Copy link
Contributor

@eberrigan eberrigan commented Apr 3, 2024

Description

  • Added analysis.csv as an option for exporting a csv using sleap-convert CLI
  • Added export_csv function to Labels class for exporting a csv using the API

Types of changes

  • Bugfix
  • New feature
  • Refactor / Code style update (no logical changes)
  • Build / CI changes
  • Documentation Update
  • Other (explain)

Does this address any currently open issues?

#1713

Outside contributors checklist

  • Review the guidelines for contributing to this repository
  • Read and sign the CLA and add yourself to the authors list
  • Make sure you are making a pull request against the develop branch (not main). Also you should start your branch off develop
  • Add tests that prove your fix is effective or that your feature works
  • Add necessary documentation (if appropriate)

Thank you for contributing to SLEAP!

❤️

Summary by CodeRabbit

  • New Features
    • Added support for exporting analysis data in CSV format, enhancing the variety of data export options available.

Copy link

coderabbitai bot commented Apr 3, 2024

Walkthrough

This update introduces the capability to export analysis results in CSV format, enhancing the SLEAP project's data exporting functionalities. It streamlines the process for users needing CSV outputs for their tracking information, catering to broader analysis and integration needs, such as compatibility with external tools.

Changes

Files Change Summary
sleap/io/convert.py Added support for CSV file generation in analysis exports, with logic updates for handling CSV formats.
sleap/io/dataset.py Introduced export_csv method in Dataset class for CSV exporting using CSVAdaptor.
tests/io/test_convert.py Updated tests to include CSV format in analysis format checks.
tests/io/test_dataset.py Added tests for CSV export functionality, including content verification.
docs/guides/cli.md Documented the addition of CSV format support in analysis exports via the --format CLI option.

Possibly related issues

🎉🐇✨
In the land of code and bytes, a new feature takes its flight,
CSV exports, wide and far, shining like a star.
Through the fields of data, we hop, with ease, we now can swap,
Formats old and new, making analysis dreams come true.
🌟📊🐾

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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 as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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

@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: 2

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bc69dd0 and d2b658c.
Files selected for processing (2)
  • sleap/io/convert.py (4 hunks)
  • sleap/io/dataset.py (1 hunks)
Files not reviewed due to errors (1)
  • (no review received)
Additional comments not posted (3)
sleap/io/convert.py (3)

73-73: Consider adding a validation check for the 'analysis.csv' format to ensure that the necessary conditions for CSV export are met before proceeding with the conversion.


169-180: Ensure that the write_analysis function called for CSV export properly handles errors and exceptions, especially for file I/O operations.


165-184: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [73-180]

Overall, the integration of the CSV export functionality appears well-implemented. However, consider adding unit tests to verify the correctness and robustness of the new feature.

Would you like assistance in creating unit tests for the CSV export functionality?

@@ -105,6 +106,7 @@ def main(args: list = None):
"""
parser = create_parser()
args = parser.parse_args(args=args)
print(args)
Copy link

Choose a reason for hiding this comment

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

Remove the debug print statement to maintain a clean and professional codebase.

-    print(args)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
print(args)

Comment on lines +140 to +145
if "nix" in args.format:
out_suffix = "nix"
elif "csv" in args.format:
out_suffix = "csv"
else:
out_suffix = "h5"
Copy link

Choose a reason for hiding this comment

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

Refactor the logic for determining the output file suffix into a separate function to improve code readability and maintainability.

+ def determine_output_suffix(format_arg: str) -> str:
+     if "nix" in format_arg:
+         return "nix"
+     elif "csv" in format_arg:
+         return "csv"
+     else:
+         return "h5"
- if "nix" in args.format:
-     out_suffix = "nix"
- elif "csv" in args.format:
-     out_suffix = "csv"
- else:
-     out_suffix = "h5"
+ out_suffix = determine_output_suffix(args.format)

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
if "nix" in args.format:
out_suffix = "nix"
elif "csv" in args.format:
out_suffix = "csv"
else:
out_suffix = "h5"
def determine_output_suffix(format_arg: str) -> str:
if "nix" in format_arg:
return "nix"
elif "csv" in format_arg:
return "csv"
else:
return "h5"
out_suffix = determine_output_suffix(args.format)

Copy link

codecov bot commented Apr 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 74.12%. Comparing base (7ed1229) to head (e508587).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1730      +/-   ##
===========================================
+ Coverage    73.30%   74.12%   +0.81%     
===========================================
  Files          134      134              
  Lines        24087    24456     +369     
===========================================
+ Hits         17658    18127     +469     
+ Misses        6429     6329     -100     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link

@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

Actionable comments outside the diff hunks (11)
sleap/io/dataset.py (11)

Line range hint 55-55: Remove duplicate import of Callable.

- from typing import Callable

The Callable type is imported twice, which is unnecessary. Consider removing the duplicate import to clean up the code.


Line range hint 61-61: Remove unused import h5py.

- import h5py as h5

The h5py module is imported but not used in the provided code. Removing unused imports helps to keep the codebase clean and maintainable.


Line range hint 68-68: Replace bare except with specific exceptions.

Using a bare except can catch unexpected exceptions, making debugging more difficult. It's better to catch specific exceptions to handle known error conditions.


Line range hint 69-69: Remove unused import _ForwardRef.

- from typing import _ForwardRef as ForwardRef
+ pass

The _ForwardRef import is unused in the provided code. Consider removing it to clean up the code.


Line range hint 304-314: Refactor lambda expressions to named functions.

Using named functions instead of lambda expressions for complex logic can improve readability and maintainability.

- filter_func = lambda lf: video is None or lf.video == video
+ def filter_func(lf):
+     return video is None or lf.video == video

Apply similar changes for other lambda expressions in this range.


Line range hint 854-854: Use isinstance() for type checks.

Type checks should use isinstance() for better readability and flexibility.

- if type(key) != tuple:
+ if not isinstance(key, tuple):

Line range hint 2209-2209: Add missing import for glob.

+ from glob import glob

The glob function is used but not imported. Adding the missing import will resolve the Undefined name 'glob' issue.


Line range hint 2304-2304: Refine conditional check to use if not ret:.

- if ret == False:
+ if not ret:

Using if not ret: is more Pythonic and concise for checking false conditions.


Line range hint 2407-2407: Define or import sleap for the to_pipeline method.

The sleap namespace is referenced but not defined or imported in the provided context. Ensure that sleap is correctly imported or defined to avoid runtime errors.


Line range hint 2500-2500: Remove unused variable e.

- except IndexError as e:
+ except IndexError:

The variable e is assigned in the except block but never used. Removing it will clean up the code.


Line range hint 2502-2502: Correct the f-string without placeholders.

- f"There are no videos in this project. No points matrix to return."
+ "There are no videos in this project. No points matrix to return."

The f-string is used without any placeholders. Converting it to a regular string is more appropriate.

Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between d2b658c and f7929a2.
Files selected for processing (2)
  • sleap/io/convert.py (3 hunks)
  • sleap/io/dataset.py (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • sleap/io/convert.py

sleap/io/dataset.py Show resolved Hide resolved
Copy link

@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

Actionable comments outside the diff hunks (1)
tests/io/test_convert.py (1)

Line range hint 100-100: Address unused variable labels.

The static analysis has identified that the local variable labels is assigned to but never used within the test_sleap_format function. This could potentially be an oversight or leftover from previous iterations of the code. Consider removing or utilizing this variable as appropriate to clean up the code and avoid confusion.

-    labels = min_labels_slp
Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between f7929a2 and 821d114.
Files selected for processing (1)
  • tests/io/test_convert.py (2 hunks)
Additional comments not posted (2)
tests/io/test_convert.py (2)

11-11: Add test cases for new CSV export functionality.

The inclusion of "analysis.csv" in the @pytest.mark.parametrize decorator effectively extends the test coverage to include the new CSV export functionality. This is a positive change that ensures the feature is tested across its intended formats.


30-30: Refine logic for determining output file format.

The updated logic in the generate_filenames function to prioritize "csv" over "nix" and "h5" based on the format parameter is clear and aligns with the new feature's requirements. This change ensures that the correct file format is used for output files when exporting to CSV.

Copy link

@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

Actionable comments outside the diff hunks (5)
tests/io/test_dataset.py (5)

Line range hint 13-13: The import sleap.io.legacy.load_labels_json_old appears to be unused in this file.

- from sleap.io.legacy import load_labels_json_old

Line range hint 117-119: Use is not for object identity tests and not in for membership tests to adhere to Python best practices.

- assert not multi_skel_vid_labels.skeletons[0] is loaded_labels.skeletons[0]
+ assert multi_skel_vid_labels.skeletons[0] is not loaded_labels.skeletons[0]
- assert not multi_skel_vid_labels.nodes[3] in loaded_labels.nodes
+ assert multi_skel_vid_labels.nodes[3] not in loaded_labels.nodes
- assert not multi_skel_vid_labels.videos[0] is loaded_labels.videos[0]
+ assert multi_skel_vid_labels.videos[0] is not loaded_labels.videos[0]

Line range hint 690-690: The variable new_labels_json is assigned but never used. Consider removing it if it's unnecessary.

- new_labels_json = new_labels.to_dict()

Line range hint 706-706: Similar to a previous comment, the variable new_labels_json is assigned but not used. Removing unnecessary variable assignments can improve code clarity.

- new_labels_json = new_labels.to_dict()

Line range hint 1423-1423: The variable track_insts is assigned but never used. If the intention was to perform assertions or checks using this variable, ensure those are implemented. Otherwise, consider removing the assignment.

- track_insts = [inst for inst in labels.instances() if inst.track == track]
Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 821d114 and 3eeb9fd.
Files selected for processing (1)
  • tests/io/test_dataset.py (2 hunks)
Additional comments not posted (3)
tests/io/test_dataset.py (3)

2-2: Consider grouping standard library imports together for better readability.


8-8: The addition of get_nodes_as_np_strings import aligns with the new functionality for exporting to CSV. Ensure it's utilized effectively in the new test functions.


1573-1603: The new test functions for exporting to CSV files (test_export_csv and test_exported_csv) are crucial for verifying the new CSV export functionality. Ensure comprehensive test coverage, including edge cases and error handling, to maintain robustness.

@eberrigan eberrigan requested a review from talmo April 8, 2024 22:29
Copy link

@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

Actionable comments outside the diff hunks (7)
tests/io/test_dataset.py (7)

Line range hint 13-13: Remove the unused import to clean up the code.

- from sleap.io.legacy import load_labels_json_old

Line range hint 117-117: Use is not for object identity tests.

- assert not multi_skel_vid_labels.skeletons[0] is loaded_labels.skeletons[0]
+ assert multi_skel_vid_labels.skeletons[0] is not loaded_labels.skeletons[0]

Line range hint 118-118: Use not in for membership tests.

- assert not multi_skel_vid_labels.nodes[3] in loaded_labels.nodes
+ assert multi_skel_vid_labels.nodes[3] not in loaded_labels.nodes

Line range hint 119-119: Use is not for object identity tests.

- assert not multi_skel_vid_labels.videos[0] is loaded_labels.videos[0]
+ assert multi_skel_vid_labels.videos[0] is not loaded_labels.videos[0]

Line range hint 1423-1423: Remove the unused variable to clean up the code.

- track_insts = [inst for inst in labels.instances() if inst.track == track]

Line range hint 690-690: The variable new_labels_json is assigned but never used. Consider removing it if it's not needed.

- new_labels_json = new_labels.to_dict()

Line range hint 706-706: The variable new_labels_json is assigned but never used. Consider removing it if it's not needed.

- new_labels_json = new_labels.to_dict()
Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 3eeb9fd and b4bdca1.
Files selected for processing (1)
  • tests/io/test_dataset.py (2 hunks)
Additional comments not posted (1)
tests/io/test_dataset.py (1)

1576-1605: Ensure the CSV export tests verify the content of the CSV files.

Consider adding assertions to check the actual content of the exported CSV files against expected values to ensure the export functionality works as intended.

@eberrigan eberrigan linked an issue Apr 8, 2024 that may be closed by this pull request
Copy link
Collaborator

@talmo talmo left a comment

Choose a reason for hiding this comment

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

LGTM, but can you also update the relevant part of the docs?

tests/io/test_dataset.py Outdated Show resolved Hide resolved
Copy link

@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

Actionable comments outside the diff hunks (7)
tests/io/test_dataset.py (7)

Line range hint 13-13: Remove unused import sleap.io.legacy.load_labels_json_old.

- import sleap.io.legacy.load_labels_json_old

Line range hint 117-117: Use is not for object identity checks.

- assert not multi_skel_vid_labels.skeletons[0] is loaded_labels.skeletons[0]
+ assert multi_skel_vid_labels.skeletons[0] is not loaded_labels.skeletons[0]

Line range hint 118-118: Use not in for membership checks.

- assert not multi_skel_vid_labels.nodes[3] in loaded_labels.nodes
+ assert multi_skel_vid_labels.nodes[3] not in loaded_labels.nodes

Line range hint 119-119: Use is not for object identity checks.

- assert not multi_skel_vid_labels.videos[0] is loaded_labels.videos[0]
+ assert multi_skel_vid_labels.videos[0] is not loaded_labels.videos[0]

Line range hint 690-690: Remove the unused variable assignment.

- new_labels_json = new_labels.to_dict()

Line range hint 706-706: Remove the unused variable assignment.

- new_labels_json = new_labels.to_dict()

Line range hint 1423-1423: Remove the unused variable assignment.

- track_insts = [inst for inst in labels.instances() if inst.track == track]
Review Status

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between b4bdca1 and e508587.
Files selected for processing (2)
  • docs/guides/cli.md (1 hunks)
  • tests/io/test_dataset.py (2 hunks)
Additional comments not posted (3)
docs/guides/cli.md (1)

325-326: Consider clarifying the CSV format's capabilities or limitations.

It would be helpful for users if the documentation included a brief description of what the CSV format includes or any limitations it might have compared to other formats. This could guide users in choosing the most appropriate format for their needs.

tests/io/test_dataset.py (2)

1576-1587: The test for CSV export functionality is correctly implemented.


1590-1602: The test for verifying the exported CSV content is correctly implemented.

@eberrigan eberrigan requested a review from talmo April 8, 2024 23:36
@talmo talmo merged commit f0c44c0 into develop Apr 9, 2024
8 checks passed
@roomrys roomrys deleted the elizabeth/add-option-to-export-to-csv branch August 24, 2024 19:29
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.

Add option to export to CSV via sleap-convert and API
2 participants