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

Add Polars materializer #2229

Merged

Conversation

christianversloot
Copy link
Contributor

@christianversloot christianversloot commented Jan 5, 2024

Describe changes

This PR adds a basic materializer for Polars (https://pola.rs/), a performant dataframe library inspired by Pandas.

It accepts pl.DataFrame and pl.Series objects (like the Pandas materializer) and writes them to Parquet files via Apache Arrow conversion using pyarrow's ParquetWriter as proposed by Polars docs.

For materializing a pl.Series object (which Polars converts into a pyarrow.array instead of pyarrow.table), a conversion into a pyarrow.table is done first, to allow for writing to Parquet. Without this conversion, the ParquetWriter fails on a lacking table schema. However, to reconvert into a pl.Series on read, a ZenML flag signaling whether the artifact is a pl.Series is added into the table schema before writing.

I tested the integration locally:

  • zenml integration install polars works and installs polars and pyarrow.
  • Writing both a pl.DataFrame and pl.Series inside a step works.
  • Same for reading, example from the sklearn IRIS dataset -> Pandas -> Polars -> write -> read:
shape: (100, 5)
┌───────────────────┬──────────────────┬───────────────────┬──────────────────┬────────┐
│ sepal length (cm) ┆ sepal width (cm) ┆ petal length (cm) ┆ petal width (cm) ┆ target │
│ ---               ┆ ---              ┆ ---               ┆ ---              ┆ ---    │
│ f64               ┆ f64              ┆ f64               ┆ f64              ┆ i32    │
╞═══════════════════╪══════════════════╪═══════════════════╪══════════════════╪════════╡
│ 7.0               ┆ 3.2              ┆ 4.7               ┆ 1.4              ┆ 1      │
│ 6.4               ┆ 3.2              ┆ 4.5               ┆ 1.5              ┆ 1      │
│ 6.9               ┆ 3.1              ┆ 4.9               ┆ 1.5              ┆ 1      │
│ 5.5               ┆ 2.3              ┆ 4.0               ┆ 1.3              ┆ 1      │
│ …                 ┆ …                ┆ …                 ┆ …                ┆ …      │
│ 6.3               ┆ 2.5              ┆ 5.0               ┆ 1.9              ┆ 2      │
│ 6.5               ┆ 3.0              ┆ 5.2               ┆ 2.0              ┆ 2      │
│ 6.2               ┆ 3.4              ┆ 5.4               ┆ 2.3              ┆ 2      │
│ 5.9               ┆ 3.0              ┆ 5.1               ┆ 1.8              ┆ 2      │
└───────────────────┴──────────────────┴───────────────────┴──────────────────┴────────┘
shape: (100,)
Series: 'target' [i32]
[
        1
        1
        1
        1
        1
        1
        1
        1
        1
        1
        1
        1
        …
        2
        2
        2
        2
        2
        2
        2
        2
        2
        2
        2
        2
        2
]

Pre-requisites

Please ensure you have done the following:

  • I have read the CONTRIBUTING.md document.
  • If my change requires a change to docs, I have updated the documentation accordingly.
  • I have added tests to cover my changes.
  • I have based my new branch on develop and the open PR is targeting develop. If your branch wasn't based on develop read Contribution guide on rebasing branch to develop.
  • If my changes require changes to the dashboard, these changes are communicated/requested.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Other (add details above)

Summary by CodeRabbit

  • New Features

    • Introduced Polars integration for enhanced data management.
    • New materializers added for handling Polars data frames and series.
  • Documentation

    • Updated the user guide with information on new and modified materializers.
  • Refactor

    • Expanded the integrations module to include Polars support.

Copy link
Contributor

coderabbitai bot commented Jan 5, 2024

Important

Auto Review Skipped

Auto reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository.

To trigger a single review, invoke the @coderabbitai review command.

Walkthrough

ZenML has expanded its integration capabilities by adding support for Polars, a dataframe library. This update introduces new materializers to handle Polars-specific data types, enabling the reading and writing of Polars data frames within the ZenML environment. The changes reflect the addition of Polars as a recognized integration, as well as the necessary classes and methods to facilitate its use.

Changes

File Path Change Summary
docs/book/.../handle-custom-data-types.md Updated the table of materializers to include additions, removals, and updates for various integrations, with a focus on data types and storage formats.
src/zenml/integrations/__init__.py Added PolarsIntegration declaration.
src/zenml/integrations/constants.py Included a new POLARS constant in the integrations list.
src/zenml/integrations/polars/... Initialized Polars integration and materializers, with PolarsIntegration class and PolarsMaterializer class for handling Polars data frames.

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-tests for this file.
  • 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 tests 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 from git and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @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.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

@strickvl strickvl added the enhancement New feature or request label Jan 5, 2024
@strickvl
Copy link
Contributor

strickvl commented Jan 5, 2024

@coderabbitai review

@strickvl strickvl requested a review from bcdurak January 5, 2024 14:45
christianversloot and others added 5 commits January 5, 2024 15:46
…izer.py

Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
…izer.py

Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
…izer.py

Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
Co-authored-by: Alex Strick van Linschoten <strickvl@users.noreply.github.com>
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.

Review Status

Actionable comments generated: 0

Configuration used: .coderabbit.yaml

Commits Files that changed from the base of the PR and between 88f6bab and d923f01.
Files selected for processing (6)
  • docs/book/user-guide/advanced-guide/data-management/handle-custom-data-types.md (1 hunks)
  • src/zenml/integrations/init.py (1 hunks)
  • src/zenml/integrations/constants.py (1 hunks)
  • src/zenml/integrations/polars/init.py (1 hunks)
  • src/zenml/integrations/polars/materializers/init.py (1 hunks)
  • src/zenml/integrations/polars/materializers/dataframe_materializer.py (1 hunks)
Files skipped from review due to trivial changes (1)
  • src/zenml/integrations/constants.py
Additional comments: 5
src/zenml/integrations/polars/materializers/__init__.py (1)
  • 16-18: The import statement is correctly using # noqa to bypass linting errors for unused imports, which is standard in __init__.py files.
src/zenml/integrations/polars/__init__.py (1)
  • 20-35: The PolarsIntegration class is defined correctly with necessary attributes and an activate method that imports materializers to ensure they are registered when the integration is activated.
src/zenml/integrations/__init__.py (1)
  • 49-49: The addition of PolarsIntegration to the integrations __init__.py file is correct and necessary for the integration to be recognized and used within ZenML.
src/zenml/integrations/polars/materializers/dataframe_materializer.py (1)
  • 30-119: The PolarsMaterializer class is correctly implemented with appropriate methods for loading and saving Polars dataframes and series, including the conversion to and from pyarrow tables, and proper cleanup of temporary directories.
docs/book/user-guide/advanced-guide/data-management/handle-custom-data-types.md (1)
  • 25-25: The documentation has been correctly updated to include the PolarsMaterializer in the table of integration-specific materializers, reflecting the new feature added to ZenML.

Copy link
Contributor

@strickvl strickvl left a comment

Choose a reason for hiding this comment

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

This all looks good to me, pending mypy fixes. The only thing I'd add now would be a test:

https://github.com/zenml-io/zenml/blob/develop/tests/unit/materializers/test_pandas_materializer.py

This gives you an idea of what I reckon would work, though the test you'd write would be located in tests/integration/integrations. (https://github.com/zenml-io/zenml/blob/develop/scripts/install-zenml-dev.sh will then install it by default as it's not in the list of ignored installations).

@christianversloot
Copy link
Contributor Author

christianversloot commented Jan 5, 2024

Thanks! A test was added in a54e5b4. Also, in c71ef81, ignores were added for the "missing library stubs or py.typed marker" error to ensure that tests pass (as recommended by mypy docs).

image

As a bit of extra information, since Polars dataframes do not support indices as far as I know (supported by a quick Google search), I did not add the _with_index_ test available in the Pandas test, only the regular one.

Edit: just realized (it's getting time for weekend) that one of the ignores was added at the wrong line. This was corrected in 6455082.

Copy link
Contributor

@strickvl strickvl left a comment

Choose a reason for hiding this comment

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

Nice! This looks good to go from my side! I think this'll make its way into the release first thing next week, pending @bcdurak's review and tests passing. Thanks as always for the contribution!

@strickvl strickvl requested review from safoinme and avishniakov and removed request for bcdurak January 8, 2024 09:21
@strickvl strickvl merged commit 8484b0e into zenml-io:develop Jan 8, 2024
32 of 35 checks passed
@strickvl
Copy link
Contributor

strickvl commented Jan 8, 2024

@christianversloot thanks for this contribution! It'll be part of the 0.54.0 release coming soon.

@christianversloot
Copy link
Contributor Author

Great! Thanks for the quick reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants