-
Notifications
You must be signed in to change notification settings - Fork 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
Add add_split method inside CsvProcessing and corresponding tests #37
Merged
Merged
Changes from all 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 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 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 |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
The parser is a class that takes as input a CSV file and a experiment class that defines data types to be used, noising procedures, splitting etc. | ||
""" | ||
|
||
import numpy as np | ||
import polars as pl | ||
from typing import Any, Tuple, Union | ||
from functools import partial | ||
|
@@ -74,15 +75,38 @@ class CsvProcessing(CsvHandler): | |
""" | ||
Class to load the input csv data and add noise accordingly. | ||
""" | ||
|
||
def __init__(self, experiment: Any, csv_path: str) -> None: | ||
super().__init__(experiment, csv_path) | ||
self.data = self.load_csv() | ||
|
||
|
||
def add_split(self, split_method: str, split: list, seed: float = None, force=False) -> None: | ||
""" | ||
Add a column specifying the train, validation, test splits of the data. | ||
An error exception is raised if the split column is already present in the csv file. This behaviour can be overriden by setting force=True. | ||
|
||
args: | ||
split_method (str) : The method to split the data, should be one of the keys of the split dictionary in the experiment class. | ||
split (list) : The proportions for [train, validation, test] splits. | ||
seed (float) : The seed for reproducibility. | ||
force (bool) : If True, the split column will be added even if it is already present in the csv file. | ||
""" | ||
if ('split' in self.categories) and (not force): | ||
raise ValueError(f"The category split is already present in the csv file. If you want to still use this function, set force=True") | ||
|
||
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. We discussed that csv already present should raise a warning and not an error, I will merge as is but this should be modified in the future. |
||
# get the indices for train, validation and test using the specified split method | ||
train, validation, test = self.experiment.get_function_split(split_method)(len(self.data), split, seed) | ||
|
||
# add the split column to the data | ||
split_column = np.full(len(self.data), np.nan) | ||
split_column[train] = 0 | ||
split_column[validation] = 1 | ||
split_column[test] = 2 | ||
self.data = self.data.with_columns(pl.Series('split:split:int', split_column)) | ||
|
||
def add_noise(self, configs: list) -> None: | ||
""" | ||
Adds noise to the data. | ||
Noise is added for each column with the configurations specified in the configs list. | ||
Noise is added for each column with the specified configurations. | ||
""" | ||
# for each column configuration | ||
for dictionary in configs: | ||
|
@@ -96,11 +120,11 @@ def add_noise(self, configs: list) -> None: | |
# change the column with the new values | ||
self.data = self.data.with_columns(pl.Series(key, new_column)) | ||
|
||
def save(self, path: str) -> None: | ||
def save(self, data: pl.DataFrame, path: str) -> None: | ||
""" | ||
Saves the data to a csv file. | ||
""" | ||
self.data.write_csv(path) | ||
data.write_csv(path) | ||
|
||
|
||
class CsvLoader(CsvHandler): | ||
|
This file contains 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 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 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 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 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 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.
Name doesnt feel too intuitive for me