-
Notifications
You must be signed in to change notification settings - Fork 2
Modenization #12
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
Merged
Merged
Modenization #12
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| exclude: "^notebooks/|^notes/" | ||
| repos: | ||
| - repo: https://github.com/pre-commit/pre-commit-hooks | ||
| rev: v4.6.0 | ||
| hooks: | ||
| - id: check-added-large-files | ||
| - id: debug-statements | ||
| - id: detect-private-key | ||
| - id: end-of-file-fixer | ||
| - id: requirements-txt-fixer | ||
| - id: trailing-whitespace | ||
| - repo: https://github.com/astral-sh/ruff-pre-commit | ||
| # Ruff version. | ||
| rev: v0.6.4 | ||
| hooks: | ||
| # Run the linter. | ||
| - id: ruff | ||
| args: | ||
| - --fix | ||
| - --exit-non-zero-on-fix | ||
| - repo: https://github.com/psf/black | ||
| rev: 24.8.0 | ||
| hooks: | ||
| - id: black | ||
| language: python | ||
| - repo: local | ||
| # We do not use pre-commit/mirrors-mypy, | ||
| # as it comes with opinionated defaults | ||
| # (like --ignore-missing-imports) | ||
| # and is difficult to configure to run | ||
| # with the dependencies correctly installed. | ||
| hooks: | ||
| - id: mypy | ||
| name: mypy | ||
| entry: mypy | ||
| language: python | ||
| # language_version: python3.12 | ||
| additional_dependencies: | ||
| - mypy | ||
| - pandas-stubs | ||
| - pydantic | ||
| - pytest | ||
| - loguru | ||
| types: | ||
| - python | ||
| # use require_serial so that script | ||
| # is only called once per commit | ||
| require_serial: true | ||
| # Print the number of files as a sanity-check | ||
| verbose: true | ||
| exclude: ^docs/tutorials |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +0,0 @@ | ||
| from haferml.version import __version__ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| from __future__ import annotations | ||
| import abc | ||
| import pandas as pd | ||
| from typing import List, Optional | ||
| from loguru import logger | ||
|
|
||
|
|
||
| class DFTransform(abc.ABC): | ||
| """abstract class for DataFrame transformations""" | ||
|
|
||
| @abc.abstractmethod | ||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
| pass | ||
|
|
||
| def chain(self, other: DFTransform) -> Chain: | ||
| return Chain([self, other]) | ||
|
|
||
| def __add__(self, other: DFTransform) -> Chain: | ||
| return self.chain(other) | ||
|
|
||
|
|
||
| class Chain(DFTransform): | ||
| """Chain multiple transformations together | ||
|
|
||
| :param transformations: list of `DFTransform` to be iterated over | ||
| """ | ||
|
|
||
| def __init__(self, transformations: list[DFTransform]): | ||
| self.transformations: List[DFTransform] = [] | ||
| for transformation in transformations: | ||
| if isinstance(transformation, Chain): | ||
| self.transformations.extend(transformation.transformations) | ||
| elif isinstance(transformation, DFTransform): | ||
| self.transformations.append(transformation) | ||
| else: | ||
| raise TypeError( | ||
| f"Expected DFTransform or Chains, got {type(transformation)}" | ||
| ) | ||
|
|
||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
| for t in self.transformations: | ||
| dataframe = t(dataframe) | ||
| return dataframe | ||
|
|
||
|
|
||
| class Identity(DFTransform): | ||
| """Returns the original dataframe | ||
|
|
||
| This is useful when summing up a lot of transformations. | ||
|
|
||
| For example, for a given list of `DFTransform`, | ||
|
|
||
| ```python | ||
| transformations = [t_1, t_2, t_3] | ||
| ``` | ||
|
|
||
| we can use `sum` to concat them, | ||
|
|
||
| ```python | ||
| transform = sum(transformations, Identity()) | ||
| ``` | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| logger.debug("This transformation does nothing.") | ||
|
|
||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
| logger.debug("Returning the original dataframe") | ||
| return dataframe | ||
|
|
||
|
|
||
| class ConvertCategoricalType(DFTransform): | ||
| """Convert a column to categorical | ||
|
|
||
| :param column_name: name of the original column | ||
| :param target_column: name of the new column | ||
| """ | ||
|
|
||
| def __init__(self, column_name: str, target_column: Optional[str] = None): | ||
| self.column_name = column_name | ||
| if target_column is None: | ||
| target_column = column_name | ||
| self.target_column = target_column | ||
|
|
||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
| logger.debug(f"Converting {self.column_name} to categorical") | ||
| dataframe[self.target_column] = dataframe[self.column_name].astype("category") | ||
| self.categories = dataframe[self.target_column].cat.categories | ||
| dataframe[self.target_column] = dataframe[self.target_column].cat.codes | ||
|
|
||
| return dataframe | ||
|
|
||
|
|
||
| class ExpandJSONValues(DFTransform): | ||
| """Create tabular form from JSON values | ||
|
|
||
| :param column_names: list of column names to be expanded | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, column_names: list[str], json_key: str, target_column_prefix: str = "" | ||
| ): | ||
| if isinstance(column_names, str): | ||
| column_names = [column_names] | ||
| self.column_names = column_names | ||
| self.json_key = json_key | ||
| self.target_column_prefix = target_column_prefix | ||
|
|
||
| def __call__(self, dataframe: pd.DataFrame) -> pd.DataFrame: | ||
| logger.debug(f"Expanding JSON values from {self.column_names}") | ||
| return dataframe.assign( | ||
| **{ | ||
| f"{self.target_column_prefix}_{k}_{self.json_key}": dataframe.apply( | ||
| lambda x: x[self.json_key].get(k), axis=1 | ||
| ) | ||
| for k in self.column_names | ||
| } | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The TrainConfig class docstring is incomplete. It should include parameter descriptions and usage examples for better API documentation.