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

Fix RGB mask targets when hex color codes are lowercase #4852

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

benjaminpkane
Copy link
Contributor

@benjaminpkane benjaminpkane commented Sep 26, 2024

What changes are proposed in this pull request?

RGB masks in looker expect an uppercase hex color

import fiftyone as fo
import fiftyone.zoo as foz
import fiftyone.utils.labels as foul

dataset = foz.load_zoo_dataset(
    "coco-2017",
    split="validation",
    label_types="segmentations",
    classes=["person", "cat", "dog"],
    label_field="instances",
    max_samples=50,
    only_matching=True,
)

mask_targets = {"#ff6d04": "person", "#499cef": "cat", "#6d04ff": "dog"}
dataset.default_mask_targets = mask_targets

foul.objects_to_segmentations(
    dataset,
    "instances",
    "segmentations",
    mask_targets=mask_targets,
    output_dir="/tmp/segmentations",
)

How is this patch tested? If it is not, please explain why.

Unit test

Release Notes

  • Fixed RGB mask targets when hex color codes are lowercase

What areas of FiftyOne does this PR affect?

  • App: FiftyOne application changes
  • Build: Build and test infrastructure changes
  • Core: Core fiftyone Python library changes
  • Documentation: FiftyOne documentation changes
  • Other

Summary by CodeRabbit

  • New Features

    • Introduced a new test suite for the convertTargets function to ensure accurate color conversion.
  • Improvements

    • Made the convertTargets function publicly accessible and defined its return type for clarity.
    • Enhanced functionality to convert RGB hex color targets to uppercase format.

@benjaminpkane benjaminpkane added bug Bug fixes app Issues related to App features labels Sep 26, 2024
@benjaminpkane benjaminpkane self-assigned this Sep 26, 2024
Copy link
Contributor

coderabbitai bot commented Sep 26, 2024

Walkthrough

The changes introduce a new test suite for the convertTargets function in the utils module, utilizing the Vitest framework to validate its functionality. The function's visibility has been changed from private to public, and its return type has been explicitly defined. The logic within the function has been updated to convert RGB hex color targets to uppercase and refine the NaN value check.

Changes

Files Change Summary
app/packages/state/src/utils.test.ts Added a test suite for convertTargets to verify conversion of #ffffff to #FFFFFF with associated label and value.
app/packages/state/src/utils.ts Changed convertTargets from private to public, updated return type to explicit, modified logic to convert targets to uppercase, and refined NaN check.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant convertTargets
    participant Test Suite

    User->>convertTargets: Call with #ffffff
    convertTargets->>convertTargets: Convert to uppercase
    convertTargets->>Test Suite: Return { "#FFFFFF": { label: "white", intTarget: 1 } }
    Test Suite->>User: Verify output
Loading

Poem

🐰 In the land of code where colors gleam,
A function shines with a vibrant dream.
Hexes now dance in uppercase light,
With tests that ensure all is right.
Hooray for changes, both bold and bright! 🌈


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 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.

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.

@benjaminpkane benjaminpkane changed the base branch from develop to release/v1.0.0 September 26, 2024 16:04
@benjaminpkane benjaminpkane marked this pull request as ready for review September 26, 2024 17:14
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: 0

🧹 Outside diff range and nitpick comments (1)
app/packages/state/src/utils.test.ts (1)

5-9: Test case effectively covers the main functionality.

The test case correctly verifies that lowercase hex color codes are converted to uppercase, which aligns with the PR objectives. It also checks the structure of the output object, including the label and integer target.

Consider adding more test cases to cover:

  1. Multiple color targets in a single input.
  2. Edge cases like empty input or invalid hex codes.
  3. Different color values (e.g., black, primary colors) to ensure consistent behavior.

Example:

it("handles multiple color targets", () => {
  expect(
    convertTargets([
      { target: "#ffffff", value: "white" },
      { target: "#000000", value: "black" }
    ])
  ).toStrictEqual({
    "#FFFFFF": { label: "white", intTarget: 1 },
    "#000000": { label: "black", intTarget: 2 }
  });
});

it("handles invalid input", () => {
  expect(() => convertTargets([{ target: "#gggggg", value: "invalid" }]))
    .toThrow();
});
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0b928cf and 3084e9d.

📒 Files selected for processing (2)
  • app/packages/state/src/utils.test.ts (1 hunks)
  • app/packages/state/src/utils.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (2)
app/packages/state/src/utils.test.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

app/packages/state/src/utils.ts (1)

Pattern **/*.{ts,tsx}: Review the Typescript and React code for conformity with best practices in React, Recoil, Graphql, and Typescript. Highlight any deviations.

🔇 Additional comments (7)
app/packages/state/src/utils.test.ts (3)

1-2: Imports look good.

The necessary testing functions are imported from 'vitest', and the function under test is correctly imported from the local 'utils' module.


4-10: Test suite structure is appropriate.

The test suite is well-structured using the describe block, which clearly indicates the function being tested (convertTargets).


1-10: Overall, this is a good start for testing the convertTargets function.

The test file is well-structured and effectively addresses the main issue described in the PR objectives. It verifies that lowercase hex color codes are correctly converted to uppercase.

To further improve the test coverage:

  1. Consider adding more test cases as suggested earlier.
  2. Ensure that the convertTargets function in utils.ts is fully covered by these tests.

To verify the implementation of convertTargets, let's check its definition:

✅ Verification successful

Verification Successful: convertTargets Function Exists.

The convertTargets function is correctly exported from utils.ts as a constant. The existing test in utils.test.ts properly targets and verifies its functionality.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find the implementation of convertTargets function
ast-grep --lang typescript --pattern 'export function convertTargets($_) {
  $$$
}'

Length of output: 85


Script:

#!/bin/bash
# Description: Find 'convertTargets' exported as a constant arrow function
ast-grep --lang typescript --pattern 'export const convertTargets = $_ => { $$$ }'

# Description: Search for any function named 'convertTargets' in the codebase
rg 'function\s+convertTargets\s*\('

Length of output: 122


Script:

#!/bin/bash
# Description: Find any function declarations named 'convertTargets' regardless of export style
ast-grep --lang typescript --pattern 'function convertTargets($_) { $$$ }'

# Description: Search for 'convertTargets' defined as a variable or constant
rg 'const\s+convertTargets\s*='

Length of output: 174

app/packages/state/src/utils.ts (4)

121-121: Good job on adding an explicit return type

Adding an explicit return type { [key: string]: { label: string; intTarget: number } | string } improves type safety and code readability. This change is beneficial for maintaining and understanding the code.


124-124: Improved numeric check

The change from !isNaN(Number(target)) to !Number.isNaN(Number(target)) is a good improvement. The new check is more explicit and reliable, as it avoids potential issues with the global isNaN function.


131-131: Fix for lowercase hex color codes

Converting the target to uppercase (target.toUpperCase()) directly addresses the PR objective of fixing RGB mask targets when hex color codes are lowercase. This change ensures consistent handling of hex color codes regardless of their case.


116-116: Consider the implications of making convertTargets public

The function has been made public by adding the export keyword. Ensure that this change is intentional and aligns with the module's design. If the function is meant to be used externally, this change is appropriate. However, if it's only for internal use within this module, consider keeping it private.

✅ Verification successful

convertTargets is only used in test files

The convertTargets function is solely referenced in utils.test.ts. If its exposure beyond this module isn't necessary, consider keeping it private to maintain encapsulation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify if convertTargets is used outside this file
rg --type typescript -g '!app/packages/state/src/utils.ts' 'convertTargets'

Length of output: 110


Script:

#!/bin/bash
# Verify if convertTargets is used outside this file
rg --type ts -g '!app/packages/state/src/utils.ts' 'convertTargets'

Length of output: 317

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.

Tested and LGTM 🙇

@benjaminpkane benjaminpkane merged commit e152852 into release/v1.0.0 Sep 26, 2024
11 checks passed
@benjaminpkane benjaminpkane deleted the bugfix/uppercase-hex branch September 26, 2024 19:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
app Issues related to App features bug Bug fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants