Repository navigation
[Data] Rename encoder preprocessor private fields - #61028
Conversation
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
… plan Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
…reprocessor field Signed-off-by: Sirui Huang <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request refactors several preprocessors to use private fields with a leading underscore, adding properties for backward compatibility. The changes are extensive and mostly look good. I've found a few minor issues: a typo in a setter method name in LabelEncoder, some missing return type hints in setters within scaler.py for consistency, and an inconsistency in attribute access in RobustScaler. Addressing these will improve code consistency and correctness.
There was a problem hiding this comment.
Pull request overview
This PR refactors Ray Data preprocessors to rename instance fields to underscore-prefixed private names (e.g. columns → _columns) while keeping public access via @property to preserve the external API and align with the project’s private-field naming convention.
Changes:
- Rename many preprocessor configuration fields to private (
_columns,_output_columns, etc.) and update internal references accordingly. - Add
@propertygetters/setters to preserve the previous public attribute surface (e.g.preprocessor.columns). - Update
Preprocessorto store the stat computation plan in_stat_computation_planand expose it via a property.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| python/ray/data/preprocessors/vectorizer.py | Rename vectorizer config fields to private and add compatibility properties. |
| python/ray/data/preprocessors/transformer.py | Rename transformer config fields to private and add compatibility properties. |
| python/ray/data/preprocessors/torch.py | Add properties for existing private fields to preserve public attribute access. |
| python/ray/data/preprocessors/tokenizer.py | Rename tokenizer config fields to private and add compatibility properties. |
| python/ray/data/preprocessors/scaler.py | Rename scaler config fields to private and update stat plan access. |
| python/ray/data/preprocessors/normalizer.py | Rename normalizer fields to private; add type hint for norm. |
| python/ray/data/preprocessors/imputer.py | Rename imputer fields to private and update usage/serialization fields. |
| python/ray/data/preprocessors/hasher.py | Rename hasher fields to private and add compatibility properties. |
| python/ray/data/preprocessors/encoder.py | Rename encoder fields to private and update internal usage/serialization fields. |
| python/ray/data/preprocessors/discretizer.py | Rename CustomKBinsDiscretizer fields to private and add compatibility properties. |
| python/ray/data/preprocessors/chain.py | Rename preprocessors to _preprocessors and add a property for compatibility. |
| python/ray/data/preprocessor.py | Move stat plan to _stat_computation_plan, update accessors, and adjust pickling behavior. |
| ci/lint/pydoclint-baseline.txt | Remove an entry now satisfied by added type hints. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
…ty + remove preprocessor setter Signed-off-by: Sirui Huang <[email protected]>
Signed-off-by: Sirui Huang <[email protected]>
…rename-private-fields
…rename-private-fields
Signed-off-by: Sirui Huang <[email protected]>
| if self._strategy == "constant": | ||
| self._is_fittable = False | ||
|
|
||
| def __setstate__(self, state: Dict[str, Any]) -> None: |
There was a problem hiding this comment.
@rayhhome, in what context would we be dealing with an old pickled object across ray versions?
There was a problem hiding this comment.
I think it would be when the user is using older versioned pickled preprocessors (like the ray_pickle.dumps in FrameworkCheckpoint). I believe ray_pickle applies standard Python unpickling, so __setstate__ would be used for restoring objects?
There was a problem hiding this comment.
i see, but i see that
FrameworkCheckpointisn't labeled as a@PublicAPI- https://github.com/anyscale/rayturbo/blob/fada4124e58de1ac14a9a33ae303edc0108d04b8/python/ray/train/predictor.py#L94 looks like loading from a checkpoint is deprecated?
Please correct me if i'm wrong. Will probably need to loop in @xinyuangui2 for context
There was a problem hiding this comment.
Merged in the interest on incremental progress.
Re: the additional complexity from implementing __setstate__ -- I think we should remove Predictor later this year. If the pickling is only relevant to Predictor, then I think we should remove all of the preprocessor pickling logic with it. I think the logic is non-trivial to read and maintain
…61341) ## Description The `SerializablePreprocessorBase` abstract class declares functions for saving and loading preprocessor states and should be implemented by all preprocessors. This PR implements the abstract methods for all preprocessors that are not yet inheriting from this base class. ## Related issues Related to #61028 , which implemented backwards compatibility for legacy pickling layer. The `__setstate__` functions should be removed along with the deprecated `Predictor`, while `_get_serializable_fields` and `_set_serializable_fields` should be used instead for saving and loading preprocessor states in future iterations. ## Additional information Accompanied by new field serializing tests of all preprocessors involved. --------- Signed-off-by: Sirui Huang <[email protected]>
…ay-project#61341) ## Description The `SerializablePreprocessorBase` abstract class declares functions for saving and loading preprocessor states and should be implemented by all preprocessors. This PR implements the abstract methods for all preprocessors that are not yet inheriting from this base class. ## Related issues Related to ray-project#61028 , which implemented backwards compatibility for legacy pickling layer. The `__setstate__` functions should be removed along with the deprecated `Predictor`, while `_get_serializable_fields` and `_set_serializable_fields` should be used instead for saving and loading preprocessor states in future iterations. ## Additional information Accompanied by new field serializing tests of all preprocessors involved. --------- Signed-off-by: Sirui Huang <[email protected]> Signed-off-by: Parag Ekbote <[email protected]>
…th existing category values (#67467) ## Why are these changes needed? In `ray.data.preprocessors.SimpleImputer._transform_pandas`, two bugs caused imputation on `pd.Categorical` columns to raise `TypeError` or `ValueError`: 1. **Append mode (`output_columns` specified, `output_column != column`) overwrote the newly added category**: Previously, `df[output_column] = df[column].cat.add_categories([value])` was executed *before* `df[output_column] = df[column].copy(deep=True)`. Because `df[column]` still held the original categories without `value`, copying `df[column]` into `df[output_column]` immediately discarded the added category, causing `df.fillna({output_column: value}, inplace=True)` to fail with: `TypeError: Cannot setitem on a Categorical with a new category (..., set the categories first)` 2. **Imputing an existing category value (`strategy="most_frequent"` or `strategy="constant"` where `fill_value` is already in `cat.categories`) crashed**: Calling `cat.add_categories([value])` unconditionally without checking `value not in df[output_column].cat.categories` raised: `ValueError: new categories must not include old categories: {...}` In addition, because `df[column].values` on a `pd.Categorical` column is a `pandas.Categorical` ExtensionArray rather than an `np.ndarray`, the writeable-copy guard also checks `is_categorical_dtype(df.dtypes[column]) and not df[column].values.codes.flags.writeable` so in-place `fillna` does not fail on zero-copy / read-only batches when `add_categories` is skipped. ### Changes - Copy `df[column]` into `df[output_column]` first when `output_column != column` or the backing array / categorical codes buffer is non-writeable (`not df[column].values.codes.flags.writeable`). - Guard `df[output_column].cat.add_categories([value])` with `value not in df[output_column].cat.categories`. - Add unit tests in `python/ray/data/tests/preprocessors/test_imputer.py` covering categorical columns in append mode, `strategy="constant"` with an existing category on read-only categorical codes, and `strategy="most_frequent"` on categorical columns. ### Duplicate-work & testing notes - Searched open and closed issues/PRs in `ray-project/ray` for `SimpleImputer` and `add_categories`; no existing issue or PR covers these two categorical bugs. - Tested locally with `pytest python/ray/data/tests/preprocessors/test_imputer.py -k "test_imputer_categorical_append_and_existing_category or test_imputer_all_nan_raise_error or test_imputer_constant_categorical"` (`3 passed`). - AI assistance was used while preparing and verifying this PR. ### Acknowledgments Credit to @abhishekverma-ray (#66091, #66088), @AarryaSaraf (#65272), @rayhhome (#61028), and @cem-anyscale (#58321) for their foundational work on `SimpleImputer` and `output_columns` support in `ray.data.preprocessors`, and to @gemini-code-assist[bot] and @cursor[bot] for catching the `Categorical.codes.flags.writeable` check. ## Checks - [x] I've signed off every commit(by making the commit message contain `Signed-off-by: ...`) - [x] I've run `pytest python/ray/data/tests/preprocessors/test_imputer.py` locally and verified all tests pass. --------- Signed-off-by: Parth Vadera <[email protected]> Co-authored-by: Copilot <[email protected]>
Description
Rename fields in preprocessors to conform to naming convention for private fields in classes
Related issues
Fixes variable naming issue reported in #59890
Additional information
backwards compatibility with
propertyso that old variable names still provide access to the private variables