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 np array int labels #2325

Merged
merged 4 commits into from
Jan 31, 2025
Merged

Fix np array int labels #2325

merged 4 commits into from
Jan 31, 2025

Conversation

ternaus
Copy link
Collaborator

@ternaus ternaus commented Jan 31, 2025

Fixes: #2324

Summary by Sourcery

Bug Fixes:

  • Fix issue where integer labels in numpy arrays were being converted to floats.

Copy link
Contributor

sourcery-ai bot commented Jan 31, 2025

Reviewer's Guide by Sourcery

This pull request refactors the label encoding logic to correctly handle numpy arrays and preserve the original data types of labels. It introduces a LabelManager class to manage label encoding and decoding, replacing the previous LabelEncoder implementation within the DataProcessor class. This change ensures that labels, whether numerical or string-based, are processed without losing their original type or data structure.

Sequence diagram for label processing flow

sequenceDiagram
    participant DP as DataProcessor
    participant LM as LabelManager
    participant LE as LabelEncoder

    DP->>LM: process_field(data_name, label_field, data)
    activate LM
    LM->>LM: _analyze_input(data)

    alt is numerical data
        LM->>LM: Convert to float32 array
    else non-numerical data
        LM->>LE: Create new encoder
        LM->>LE: fit_transform(data)
    end

    LM-->>DP: Return encoded data
    deactivate LM

    Note over DP,LM: Later when restoring...

    DP->>LM: restore_field(data_name, label_field, encoded_data)
    activate LM

    alt is numerical data
        LM->>LM: Restore original dtype
    else non-numerical data
        LM->>LE: inverse_transform(encoded_data)
    end

    LM->>LM: _restore_type(decoded_data)
    LM-->>DP: Return restored data
    deactivate LM
Loading

Class diagram showing the new label management structure

classDiagram
    class LabelManager {
      -metadata: dict
      +process_field(data_name: str, label_field: str, field_data: Any): np.ndarray
      +restore_field(data_name: str, label_field: str, encoded_data: np.ndarray): Any
      -_analyze_input(field_data: Any): LabelMetadata
      -_encode_data(field_data: Any, metadata: LabelMetadata): np.ndarray
      -_decode_data(encoded_data: np.ndarray, metadata: LabelMetadata): np.ndarray
      -_restore_type(decoded_data: np.ndarray, metadata: LabelMetadata): Any
      +handle_empty_data(): list
    }

    class LabelMetadata {
      +input_type: type
      +is_numerical: bool
      +dtype: np.dtype
      +encoder: LabelEncoder
    }

    class LabelEncoder {
      -classes_: dict
      -inverse_classes_: dict
      -num_classes: int
      -is_numerical: bool
      +fit(y: Sequence|np.ndarray): LabelEncoder
      +transform(y: Sequence|np.ndarray): np.ndarray
      +fit_transform(y: Sequence|np.ndarray): np.ndarray
      +inverse_transform(y: Sequence|np.ndarray): np.ndarray
    }

    class DataProcessor {
      -label_manager: LabelManager
    }

    DataProcessor --> LabelManager
    LabelManager --> LabelMetadata
    LabelMetadata --> LabelEncoder
Loading

File-Level Changes

Change Details Files
Introduced LabelManager class to handle label encoding and decoding.
  • Created a new LabelManager class to encapsulate label processing logic.
  • Added LabelMetadata dataclass to store metadata about label fields.
  • Implemented methods for analyzing, encoding, decoding, and restoring label fields.
albumentations/core/label_manager.py
Removed LabelEncoder from DataProcessor and integrated LabelManager.
  • Removed the LabelEncoder class from albumentations/core/utils.py.
  • Replaced the label_encoders and is_numerical_label dictionaries with a label_manager instance.
  • Modified _process_label_fields to use label_manager for encoding labels.
  • Modified _decode_label_field to use label_manager for decoding labels.
  • Updated _handle_empty_data_array to use label_manager for handling empty labels.
albumentations/core/utils.py
Preserved original data types of labels.
  • Modified LabelManager to preserve the original type (list or numpy array) and dtype of labels.
  • Updated tests to verify that label types and dtypes are preserved after transformations.
  • Ensured that string labels are preserved exactly.
  • Added a test case for empty labels.
albumentations/core/label_manager.py
albumentations/core/utils.py
tests/test_core.py
Updated CoarseDropout to use LabelManager.
  • Modified get_boxes_from_bboxes in CoarseDropout to use label_manager to access the label encoder.
albumentations/augmentations/dropout/coarse_dropout.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ternaus - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

albumentations/core/label_manager.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
tests/test_core.py Show resolved Hide resolved
albumentations/core/label_manager.py Outdated Show resolved Hide resolved
@ternaus ternaus merged commit 2a793a1 into main Jan 31, 2025
14 checks passed
@ternaus ternaus deleted the fix_np_array_int_labels branch January 31, 2025 21:21
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.

Indices are floats in v2.0.2
1 participant