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

3D export fixes and tests #4368

Merged
merged 5 commits into from
May 9, 2024
Merged

Conversation

swheaton
Copy link
Contributor

@swheaton swheaton commented May 7, 2024

Checkpointing my work in case I don't get to finish it.

  1. added tests for at least 4 different configurations of fo3d data layout + relative vs. absolute + flatten vs. rel_dir.
  2. Fixed a few bugs caught by tests
    • .mtl file in ObjMesh wasn't getting rewritten (it was only checking first asset path for each node)
    • use input to output asset path map to determine when we need to rewrite asset path in the fo3d file. fixes the case when flattening (no rel_dir) causes a file to be renamed from, say, pcd.pcd to pcd-2.pcd

The tests are absolutely horrendous on the eyes, I'm so sorry!! They could be refactored to something nicer but didn't have time right now.

What I would like to do if I have time:

  • allow asset_dir
  • make changes work with Teams which includes fos.resolve() and a cloud friendly os.path.relpath() if it doesn't already exist. Actually the latter probably just works but we would want to double check.
  • make a batch media exporter that does this in bulk instead of one at a time. Then re-use that for fos.upload_media() and download_media() in Teams so we don't have the same fragile code implemented twice. With the side benefit that export should be faster.

Summary by CodeRabbit

  • New Features
    • Introduced a method to update asset paths in 3D scenes, enhancing the management of asset locations based on user-defined mappings.
  • Tests
    • Added new tests for 3D media handling, ensuring robustness and reliability in asset path updates and scene integrity.

Copy link
Contributor

coderabbitai bot commented May 7, 2024

Walkthrough

The recent updates focus on enhancing asset path management within the Scene3D class and the data exporters of a software library. A new method, update_asset_paths, centralizes the process of updating paths based on a mapping dictionary. This method is now utilized in the data exporters to streamline path updates. Additionally, new unit tests ensure the functionality behaves as expected across various scenarios.

Changes

File Path Change Summary
.../core/threed/scene_3d.py Added update_asset_paths method to Scene3D class for updating asset paths.
.../utils/data/exporters.py Enhanced asset path handling using the new update_asset_paths from Scene3D.
.../unittests/.../import_export_tests.py New test class ThreeDMediaTests with methods to test asset path updates in various scenarios.

🐰✨
In the code where paths intertwine,
A rabbit hops with updates fine.
Paths now clear where once were knots,
Through tests and maps, it plots its spots.
Celebrate the code, fresh and new,
Hop along, there's more to view!
🌟🐇


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.

@swheaton
Copy link
Contributor Author

swheaton commented May 8, 2024

make a batch media exporter that does this in bulk instead of one at a time. Then re-use that for fos.upload_media() and download_media() in Teams so we don't have the same fragile code implemented twice. With the side benefit that export should be faster.

Ok I am seeing that this exists, sort of, in Teams when a cloud path is involved. I am unable to take this refactor on right now so perhaps I'll have to leave it alone for now.

@swheaton swheaton force-pushed the feat/harden-3d-export branch from 960d88a to 1a8b9ba Compare May 8, 2024 19:53
@swheaton swheaton changed the base branch from develop to release/v0.24.0 May 8, 2024 19:53
@swheaton swheaton marked this pull request as ready for review May 8, 2024 19:54
@swheaton swheaton changed the title [WIP] 3D export fixes and tests 3D export fixes and tests May 8, 2024
Copy link
Contributor

@brimoor brimoor left a comment

Choose a reason for hiding this comment

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

LGTM

@swheaton
Copy link
Contributor Author

swheaton commented May 9, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented May 9, 2024

Actions Performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
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

Out of diff range and nitpick comments (9)
fiftyone/utils/data/exporters.py (9)

Line range hint 541-541: Avoid using bare except statements.

- except:
+ except Exception as e:
+     # Handle or log the exception e appropriately

Line range hint 571-571: Avoid using bare except statements.

- except:
+ except Exception as e:
+     # Handle or log the exception e appropriately

Line range hint 614-614: Avoid using bare except statements.

- except:
+ except Exception as e:
+     # Handle or log the exception e appropriately

Line range hint 649-649: Avoid using bare except statements.

- except:
+ except Exception as e:
+     # Handle or log the exception e appropriately

Line range hint 904-904: Avoid using bare except statements.

- except:
+ except Exception as e:
+     # Handle or log the exception e appropriately

Line range hint 3821-3821: Avoid inequality comparisons to False; use if include_confidence: for truth checks.

- if include_confidence == True:
+ if include_confidence:

Line range hint 3821-3821: Avoid inequality comparisons to False; use if include_attributes: for truth checks.

- if include_attributes == True:
+ if include_attributes:

Line range hint 3947-3947: Avoid equality comparisons to True; use if include_confidence: for truth checks.

- if include_confidence == True:
+ if include_confidence:

Line range hint 3952-3952: Avoid equality comparisons to True; use if include_attributes: for truth checks.

- if include_attributes == True:
+ if include_attributes:
Review Details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits Files that changed from the base of the PR and between 1e75753 and 1a8b9ba.
Files selected for processing (3)
  • fiftyone/core/threed/scene_3d.py (1 hunks)
  • fiftyone/utils/data/exporters.py (3 hunks)
  • tests/unittests/import_export_tests.py (2 hunks)
Additional Context Used
Ruff (15)
fiftyone/utils/data/exporters.py (9)

541-541: Do not use bare except


571-571: Do not use bare except


614-614: Do not use bare except


649-649: Do not use bare except


904-904: Do not use bare except


3821-3821: Avoid inequality comparisons to False; use if include_confidence: for truth checks


3821-3821: Avoid inequality comparisons to False; use if include_attributes: for truth checks


3947-3947: Avoid equality comparisons to True; use if include_confidence: for truth checks


3952-3952: Avoid equality comparisons to True; use if include_attributes: for truth checks

tests/unittests/import_export_tests.py (6)

2134-2134: pydicom imported but unused


3572-3572: Local variable img_filepath is assigned to but never used


3589-3589: Local variable img_filepath is assigned to but never used


3607-3607: Local variable img_filepath is assigned to but never used


3636-3636: Local variable img_filepath is assigned to but never used


3706-3706: Local variable vid_filepath is assigned to but never used

Additional comments not posted (9)
tests/unittests/import_export_tests.py (9)

4653-4656: The class ThreeDMediaTests is well-documented, providing a clear purpose for the tests.


4658-4679: The method _build_flat_relative effectively sets up a test environment for a flat, relative-addressed 3D scene. It correctly handles file operations and temporary directories.


4681-4711: The method _build_flat_absolute is well-implemented for setting up a flat, absolute-addressed 3D scene. It ensures that paths are absolute and correctly writes dummy data to files.


4713-4772: The method _build_nested_relative correctly sets up a more complex nested directory structure for testing 3D scenes with relative paths. It handles multiple scenes and ensures that asset paths are relative.


4774-4780: The method _assert_scene_content provides a robust way to verify the integrity of 3D scenes by checking file contents against expected values. This is crucial for ensuring that export functionalities are working as expected.


4783-4806: The test method test_flat_relative effectively verifies the export functionality for a flat, relative-addressed 3D scene. It includes steps for exporting the scene and then using _assert_scene_content to verify the integrity of the exported files.


4808-4837: The method test_flat_absolute correctly tests the export functionality for a flat, absolute-addressed 3D scene. It ensures that the scene is exported correctly and verifies the content post-export.


4840-4914: The method test_relative_nested_flatten effectively tests the export functionality for a nested, relative-addressed 3D scene, ensuring that the directory structure is flattened during export and that file names are correctly handled to avoid conflicts.


4916-4956: The method test_relative_nested_maintain thoroughly tests the export functionality to ensure that a nested directory structure is maintained during the export of a 3D scene. It verifies that relative paths in the .fo3d files remain unchanged, which is crucial for the integrity of the scene.

Comment on lines +262 to +300
def update_asset_paths(self, asset_rewrite_paths: dict):
"""Update asset paths in this scene according to an input dict mapping.

Asset path is unchanged if it does not exist in ``asset_rewrite_paths``

Args:
asset_rewrite_paths: ``dict`` mapping asset path to new asset path

Returns:
``True`` if the scene was modified.
"""
scene_modified = False
for node in self.traverse():
for path_attribute in node._asset_path_fields:
asset_path = getattr(node, path_attribute, None)
new_asset_path = asset_rewrite_paths.get(asset_path)

if asset_path is not None and asset_path != new_asset_path:
setattr(node, path_attribute, new_asset_path)
scene_modified = True

# modify scene background paths, if any
if self.background is not None:
if self.background.image is not None:
new_asset_path = asset_rewrite_paths.get(self.background.image)
if new_asset_path != self.background.image:
self.background.image = new_asset_path
scene_modified = True

if self.background.cube is not None:
new_cube = [
asset_rewrite_paths.get(face)
for face in self.background.cube
]
if new_cube != self.background.cube:
self.background.cube = new_cube
scene_modified = True

return scene_modified
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider enhancing traceability and robustness in asset path updates.

  • It would be beneficial to add logging at key points in this method to trace the changes being made to asset paths, which can be crucial for debugging and verifying the behavior in production.
  • Additionally, consider handling the scenario where new_asset_path is None. Currently, if asset_rewrite_paths.get(asset_path) returns None, it will replace a valid path with None, which might not be the intended behavior. You might want to add a condition to check if new_asset_path is not None before setting it.
+                if asset_path is not None and new_asset_path is not None and asset_path != new_asset_path:

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
def update_asset_paths(self, asset_rewrite_paths: dict):
"""Update asset paths in this scene according to an input dict mapping.
Asset path is unchanged if it does not exist in ``asset_rewrite_paths``
Args:
asset_rewrite_paths: ``dict`` mapping asset path to new asset path
Returns:
``True`` if the scene was modified.
"""
scene_modified = False
for node in self.traverse():
for path_attribute in node._asset_path_fields:
asset_path = getattr(node, path_attribute, None)
new_asset_path = asset_rewrite_paths.get(asset_path)
if asset_path is not None and asset_path != new_asset_path:
setattr(node, path_attribute, new_asset_path)
scene_modified = True
# modify scene background paths, if any
if self.background is not None:
if self.background.image is not None:
new_asset_path = asset_rewrite_paths.get(self.background.image)
if new_asset_path != self.background.image:
self.background.image = new_asset_path
scene_modified = True
if self.background.cube is not None:
new_cube = [
asset_rewrite_paths.get(face)
for face in self.background.cube
]
if new_cube != self.background.cube:
self.background.cube = new_cube
scene_modified = True
return scene_modified
def update_asset_paths(self, asset_rewrite_paths: dict):
"""Update asset paths in this scene according to an input dict mapping.
Asset path is unchanged if it does not exist in ``asset_rewrite_paths``
Args:
asset_rewrite_paths: ``dict`` mapping asset path to new asset path
Returns:
``True`` if the scene was modified.
"""
scene_modified = False
for node in self.traverse():
for path_attribute in node._asset_path_fields:
asset_path = getattr(node, path_attribute, None)
new_asset_path = asset_rewrite_paths.get(asset_path)
if asset_path is not None and new_asset_path is not None and asset_path != new_asset_path:
setattr(node, path_attribute, new_asset_path)
scene_modified = True
# modify scene background paths, if any
if self.background is not None:
if self.background.image is not None:
new_asset_path = asset_rewrite_paths.get(self.background.image)
if new_asset_path != self.background.image:
self.background.image = new_asset_path
scene_modified = True
if self.background.cube is not None:
new_cube = [
asset_rewrite_paths.get(face)
for face in self.background.cube
]
if new_cube != self.background.cube:
self.background.cube = new_cube
scene_modified = True
return scene_modified

@sashankaryal sashankaryal merged commit 47919b4 into release/v0.24.0 May 9, 2024
9 of 10 checks passed
@sashankaryal sashankaryal deleted the feat/harden-3d-export branch May 9, 2024 15:13
benjaminpkane pushed a commit that referenced this pull request May 20, 2024
* WIP 3D export fixes and tests

* print

* errant copypasta

* move rewrite asset logic to scene_3d

* cleanup 3d import/export tests a bit
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.

3 participants