-
Notifications
You must be signed in to change notification settings - Fork 21
improve code efficiency to handle large amount of data #158
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
Open
JoachimPiret
wants to merge
6
commits into
ODINN-SciML:main
Choose a base branch
from
JoachimPiret:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
edce87d
improve code efficiency of two functions of class AggregatedDataset()…
JoachimPiret aa896f7
allows to record dataframe in parquet format in addition to csv format
JoachimPiret 95141da
add possibility to divide between test and train absed on subregion (…
JoachimPiret ca8e672
adapation of dataset.py to choose output format of _get_output_filena…
JoachimPiret 1a9e2e5
Alban's feedback on PR : mapSplitsToDataset() and init() more effici…
JoachimPiret 9bf9aa8
Alban's feedback on PR : split on subregion added, modification of _…
JoachimPiret 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
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 | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -65,14 +65,18 @@ def __init__( | |||||||||||
| self.meta_data_columns = meta_data_columns or cfg.metaData | ||||||||||||
|
|
||||||||||||
| def set_train_test_split( | ||||||||||||
| self, *, test_size: float = None, type_fold: str = "group-meas-id" | ||||||||||||
| self, | ||||||||||||
| *, | ||||||||||||
| test_size: float = None, | ||||||||||||
| type_fold: str = "group-meas-id", | ||||||||||||
| random_state: bool = False, | ||||||||||||
| ) -> Tuple[Iterator[Any], Iterator[Any]]: | ||||||||||||
| """ | ||||||||||||
| Split the dataset into training and testing sets. | ||||||||||||
|
|
||||||||||||
| Args: | ||||||||||||
| test_size (float): Proportion of the dataset to include in the test split. | ||||||||||||
| type_fold (str): Type of splitting between train and test sets. Options are 'group-rgi', or 'group-meas-id'. | ||||||||||||
| type_fold (str): Type of splitting between train and test sets. Options are 'group-rgi','group-c_region' or 'group-meas-id'. | ||||||||||||
|
|
||||||||||||
| Returns: | ||||||||||||
| Tuple[Iterator[Any], Iterator[Any]]: Iterators for training and testing indices. | ||||||||||||
|
|
@@ -89,15 +93,25 @@ def set_train_test_split( | |||||||||||
| # I.e, one year of a stake is not split amongst test and train set | ||||||||||||
|
|
||||||||||||
| # From the data get the features, targets, and glacier IDS | ||||||||||||
| X, y, glacier_ids, stake_meas_id = self._prepare_data_for_cv( | ||||||||||||
| X, y, glacier_ids, stake_meas_id, regions = self._prepare_data_for_cv( | ||||||||||||
| self.data, self.meta_data_columns | ||||||||||||
| ) | ||||||||||||
| gss = GroupShuffleSplit( | ||||||||||||
| n_splits=1, test_size=test_size, random_state=self.random_seed | ||||||||||||
| ) | ||||||||||||
| groups = {"group-meas-id": stake_meas_id, "group-rgi": glacier_ids}.get( | ||||||||||||
| type_fold | ||||||||||||
| ) | ||||||||||||
| if random_state == False: | ||||||||||||
| gss = GroupShuffleSplit( | ||||||||||||
| n_splits=1, | ||||||||||||
| test_size=test_size, | ||||||||||||
| random_state=self.random_seed, # commenting this improve randomness | ||||||||||||
| ) | ||||||||||||
| elif random_state == True: | ||||||||||||
| gss = GroupShuffleSplit( | ||||||||||||
| n_splits=1, | ||||||||||||
| test_size=test_size, | ||||||||||||
| ) | ||||||||||||
| groups = { | ||||||||||||
| "group-meas-id": stake_meas_id, | ||||||||||||
| "group-rgi": glacier_ids, | ||||||||||||
| "group-c_region": regions, | ||||||||||||
| }.get(type_fold) | ||||||||||||
| train_indices, test_indices = next(gss.split(X, y, groups)) | ||||||||||||
|
|
||||||||||||
| # Check that the intersection train and test ids is empty | ||||||||||||
|
|
@@ -108,9 +122,20 @@ def set_train_test_split( | |||||||||||
| # Make it iterators and set as an attribute of the class | ||||||||||||
| self.train_indices = train_indices | ||||||||||||
| self.test_indices = test_indices | ||||||||||||
|
|
||||||||||||
| return iter(self.train_indices), iter(self.test_indices) | ||||||||||||
|
|
||||||||||||
| def assign_train_test_indices(self, train_indices, test_indices, test_size): | ||||||||||||
| """ | ||||||||||||
| Dividing train and test ensemble based on subregion require to make the sampling N times and then choose the | ||||||||||||
| train-test division closest to the 70-30 repartition. At each iteration the Dataloader object is redifined as well as | ||||||||||||
| self.train_indices and self.test_indices meaning that the information in the Dataloader object are those of the last iterations | ||||||||||||
| and not those of the train-test division chosen after comparing to the 70-30 repartition. | ||||||||||||
| This function aims to correct this by reassigning the indices of the chosen sampling. | ||||||||||||
| """ | ||||||||||||
| self.train_indices = train_indices | ||||||||||||
| self.test_indices = test_indices | ||||||||||||
| self.test_size = test_size | ||||||||||||
|
|
||||||||||||
| def set_custom_train_test_indices( | ||||||||||||
| self, train_indices: np.array, test_indices: np.array | ||||||||||||
| ): | ||||||||||||
|
|
@@ -157,13 +182,13 @@ def get_cv_split( | |||||||||||
| train_data = self._get_train_data() | ||||||||||||
|
|
||||||||||||
| # From the training data get the features, targets, and glacier IDS | ||||||||||||
| X, y, glacier_ids, stake_meas_id = self._prepare_data_for_cv( | ||||||||||||
| X, y, glacier_ids, stake_meas_id, regions = self._prepare_data_for_cv( | ||||||||||||
| train_data, self.meta_data_columns | ||||||||||||
| ) | ||||||||||||
|
|
||||||||||||
| # Create the cross validation splits | ||||||||||||
| splits = self._create_group_kfold_splits( | ||||||||||||
| X, y, glacier_ids, stake_meas_id, type_fold | ||||||||||||
| X, y, glacier_ids, stake_meas_id, regions, type_fold | ||||||||||||
| ) | ||||||||||||
| self.cv_split = splits | ||||||||||||
|
|
||||||||||||
|
|
@@ -239,14 +264,19 @@ def _prepare_data_for_cv( | |||||||||||
| y = train_data["POINT_BALANCE"] | ||||||||||||
| glacier_ids = train_data["RGIId"].values | ||||||||||||
| stake_meas_id = train_data["ID"].values # unique value per stake measurement | ||||||||||||
| return X, y, glacier_ids, stake_meas_id | ||||||||||||
| try: | ||||||||||||
| regions = train_data["C_REGION"].values | ||||||||||||
| except: | ||||||||||||
| regions = type(np.array([])) | ||||||||||||
|
Comment on lines
+267
to
+270
Member
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. More Pythonic than a try/except which can hide hidden bugs
Suggested change
|
||||||||||||
| return X, y, glacier_ids, stake_meas_id, regions | ||||||||||||
|
|
||||||||||||
| def _create_group_kfold_splits( | ||||||||||||
| self, | ||||||||||||
| X: pd.DataFrame, | ||||||||||||
| y: pd.Series, | ||||||||||||
| glacier_ids: np.ndarray, | ||||||||||||
| stake_meas_id: np.ndarray, | ||||||||||||
| regions: np.ndarray, | ||||||||||||
| type_fold: str, | ||||||||||||
| ) -> List[Tuple[np.ndarray, np.ndarray]]: | ||||||||||||
| """ | ||||||||||||
|
|
@@ -268,6 +298,7 @@ def _create_group_kfold_splits( | |||||||||||
| fold_types = { | ||||||||||||
| "group-rgi": (GroupKFold, glacier_ids), | ||||||||||||
| "group-meas-id": (GroupKFold, stake_meas_id), | ||||||||||||
| "group-c_region": (GroupKFold, regions), | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| FoldClass, groups = fold_types.get(type_fold, (KFold, None)) | ||||||||||||
|
|
||||||||||||
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.