-
Notifications
You must be signed in to change notification settings - Fork 18
feat: added __str__ to PreProcessing and DataTypes #717
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| """Preprocessing runtime utilities.""" | ||
|
|
||
| from .base import PreprocessingMethod | ||
| from .base import PreprocessingConfiguration, PreprocessingMethod | ||
|
|
||
| __all__ = [ | ||
| "PreprocessingConfiguration", | ||
| "PreprocessingMethod", | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -37,3 +37,32 @@ def to_dict(self) -> dict[str, Any]: | |
| params[param_name] = getattr(self, param_name, None) | ||
|
|
||
| return {"_target_": target_name, **params} | ||
|
|
||
| def __str__(self) -> str: | ||
| """Return a human-readable representation of the preprocessing method.""" | ||
| params = {k: v for k, v in self.to_dict().items() if k != "_target_"} | ||
| param_str = ", ".join(f"{name}={value!r}" for name, value in params.items()) | ||
| return f"{self.__class__.__name__}({param_str})" | ||
|
|
||
| def __repr__(self) -> str: | ||
| """Return a human-readable representation for debugging.""" | ||
| return self.__str__() | ||
|
Comment on lines
+41
to
+49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel
|
||
|
|
||
|
|
||
| class PreprocessingConfiguration(dict[DataType, list[PreprocessingMethod]]): | ||
| """Runtime preprocessing pipeline keyed by data type.""" | ||
|
|
||
| def __str__(self) -> str: | ||
| """Return a human-readable representation of the preprocessing pipeline.""" | ||
| if not self: | ||
| return "PreprocessingConfiguration({})" | ||
| lines = [] | ||
| for data_type in sorted(self, key=lambda dt: dt.value): | ||
| methods = self[data_type] | ||
| method_strs = ", ".join(str(method) for method in methods) | ||
| lines.append(f" {data_type.value}: [{method_strs}]") | ||
| return "PreprocessingConfiguration({\n" + "\n".join(lines) + "\n})" | ||
|
|
||
| def __repr__(self) -> str: | ||
| """Return a human-readable representation for debugging.""" | ||
| return self.__str__() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,3 +40,26 @@ def test_resolve_preprocessing_config_to_dict_is_json_serializable(): | |
| # before the fix. | ||
| serialized = json.dumps(method.to_dict()) | ||
| assert '"size"' in serialized | ||
|
|
||
|
|
||
| def test_preprocessing_configuration_str_is_human_readable(): | ||
| pytest.importorskip("hydra") | ||
| pytest.importorskip("torch") | ||
| cfg = OmegaConf.create({ | ||
| "RGB_IMAGES": [{ | ||
| "_target_": "neuracore.ml.preprocessing.methods.resize_pad.ResizePad", | ||
| "size": [224, 224], | ||
| }], | ||
| "DEPTH_IMAGES": [{ | ||
| "_target_": "neuracore.ml.preprocessing.methods.resize_pad.ResizePad", | ||
| "size": [224, 224], | ||
| }], | ||
| }) | ||
| resolved = resolve_preprocessing_config(cfg) | ||
|
|
||
| rendered = str(resolved) | ||
| assert "PreprocessingConfiguration({" in rendered | ||
| assert "RGB_IMAGES: [ResizePad(size=[224, 224])]" in rendered | ||
| assert "DEPTH_IMAGES: [ResizePad(size=[224, 224])]" in rendered | ||
| assert "object at 0x" not in rendered | ||
| assert "DataType." not in rendered | ||
|
Comment on lines
+61
to
+65
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a good idea to assert the whole string here for strict correctness. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: since it's already in the utils file, no need to call it
preprocessing_utils