-
Notifications
You must be signed in to change notification settings - Fork 44
Training dataset api #25
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
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
80faa06
Add training dataset api
moritzmeister ea7288b
remove client from td
moritzmeister eaafe42
Fix
moritzmeister 8fc28d0
refactor to match java client
moritzmeister bac5c1e
Add read
moritzmeister 3b0ad09
Add get training dataset
moritzmeister 5c85d8b
Add training dataset read exposed API
moritzmeister b9e9ac5
Fix read options
moritzmeister 4674bb2
Remove overwrite argument from create_training_dataset
moritzmeister 262b1eb
Remove storage connector todo
moritzmeister bcedd3c
Fix options to overwrite delimiter with user provided options
moritzmeister ade824d
Extend feature by primary and partition
moritzmeister c0fd18c
Fix feature remove type
moritzmeister 32f53cd
remove separate connector info in training dataset
moritzmeister e058c80
Fix introduced bug
moritzmeister d87e18a
Reverse stupid remove
moritzmeister 3ca6891
Set default for primary in feature
moritzmeister e00ded1
Add splits to data sent to backend
moritzmeister 5769086
Fix
moritzmeister d34587d
Remove prints
moritzmeister 4f5aa38
Add show(n) to training dataset
moritzmeister 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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from hopsworks import client, storage_connector | ||
|
||
|
||
class StorageConnectorApi: | ||
def __init__(self, feature_store_id): | ||
self._feature_store_id = feature_store_id | ||
|
||
def get(self, name, connector_type): | ||
"""Get storage connector with name and type. | ||
|
||
:param name: name of the storage connector | ||
:type name: str | ||
:param connector_type: connector type | ||
:type connector_type: str | ||
:return: the storage connector | ||
:rtype: StorageConnector | ||
""" | ||
_client = client.get_instance() | ||
path_params = [ | ||
"project", | ||
_client._project_id, | ||
"featurestores", | ||
self._feature_store_id, | ||
"storageconnectors", | ||
connector_type, | ||
] | ||
result = [ | ||
conn | ||
for conn in _client._send_request("GET", path_params) | ||
if conn["name"] == name | ||
] | ||
|
||
if len(result) == 1: | ||
return storage_connector.StorageConnector.from_response_json(result[0]) | ||
else: | ||
raise Exception( | ||
"Could not find the storage connector `{}` with type `{}`.".format( | ||
name, connector_type | ||
) | ||
) | ||
|
||
def get_by_id(self, connector_id, connector_type): | ||
_client = client.get_instance() | ||
path_params = [ | ||
"project", | ||
_client._project_id, | ||
"featurestores", | ||
self._feature_store_id, | ||
"storageconnectors", | ||
connector_type, | ||
connector_id, | ||
] | ||
return storage_connector.StorageConnector.from_response_json( | ||
_client._send_request("GET", path_params) | ||
) |
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,42 @@ | ||
from hopsworks import client | ||
|
||
from hopsworks import training_dataset | ||
|
||
|
||
class TrainingDatasetApi: | ||
def __init__(self, feature_store_id): | ||
self._feature_store_id = feature_store_id | ||
|
||
def post(self, training_dataset_instance): | ||
_client = client.get_instance() | ||
path_params = [ | ||
"project", | ||
_client._project_id, | ||
"featurestores", | ||
self._feature_store_id, | ||
"trainingdatasets", | ||
] | ||
headers = {"content-type": "application/json"} | ||
return training_dataset_instance.update_from_response_json( | ||
_client._send_request( | ||
"POST", | ||
path_params, | ||
headers=headers, | ||
data=training_dataset_instance.json(), | ||
), | ||
) | ||
|
||
def get(self, name, version): | ||
_client = client.get_instance() | ||
path_params = [ | ||
"project", | ||
_client._project_id, | ||
"featurestores", | ||
self._feature_store_id, | ||
"trainingdatasets", | ||
name, | ||
] | ||
query_params = {"version": version} | ||
return training_dataset.TrainingDataset.from_response_json( | ||
_client._send_request("GET", path_params, query_params)[0], | ||
) |
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,119 @@ | ||
from hopsworks import engine | ||
from hopsworks.core import training_dataset_api | ||
|
||
|
||
class TrainingDatasetEngine: | ||
OVERWRITE = "overwrite" | ||
APPEND = "append" | ||
|
||
def __init__(self, feature_store_id): | ||
self._training_dataset_api = training_dataset_api.TrainingDatasetApi( | ||
feature_store_id | ||
) | ||
|
||
def create(self, training_dataset, feature_dataframe, user_write_options): | ||
self._training_dataset_api.post(training_dataset) | ||
|
||
write_options = engine.get_instance().write_options( | ||
training_dataset.data_format, user_write_options | ||
) | ||
|
||
self._write(training_dataset, feature_dataframe, write_options, self.OVERWRITE) | ||
|
||
def insert( | ||
self, training_dataset, feature_dataframe, user_write_options, overwrite | ||
): | ||
# validate matching schema | ||
engine.get_instance().schema_matches(feature_dataframe, training_dataset.schema) | ||
|
||
write_options = engine.get_instance().write_options( | ||
training_dataset.data_format, user_write_options | ||
) | ||
|
||
self._write( | ||
training_dataset, | ||
feature_dataframe, | ||
write_options, | ||
self.OVERWRITE if overwrite else self.APPEND, | ||
) | ||
|
||
def read(self, training_dataset, split, user_read_options): | ||
if split is None: | ||
path = training_dataset.location + "/" + "**" | ||
else: | ||
path = training_dataset.location + "/" + str(split) | ||
|
||
read_options = engine.get_instance().read_options( | ||
training_dataset.data_format, user_read_options | ||
) | ||
|
||
return engine.get_instance().read( | ||
training_dataset.storage_connector, | ||
training_dataset.data_format, | ||
read_options, | ||
path, | ||
) | ||
|
||
def _write(self, training_dataset, dataset, write_options, save_mode): | ||
if len(training_dataset.splits) == 0: | ||
path = training_dataset.location + "/" + training_dataset.name | ||
self._write_single( | ||
dataset, | ||
training_dataset.storage_connector, | ||
training_dataset.data_format, | ||
write_options, | ||
save_mode, | ||
path, | ||
) | ||
else: | ||
split_names = sorted([*training_dataset.splits]) | ||
split_weights = [training_dataset.splits[i] for i in split_names] | ||
self._write_splits( | ||
dataset.randomSplit(split_weights, training_dataset.seed), | ||
training_dataset.storage_connector, | ||
training_dataset.data_format, | ||
write_options, | ||
save_mode, | ||
training_dataset.location, | ||
split_names, | ||
) | ||
|
||
def _write_splits( | ||
self, | ||
feature_dataframe_list, | ||
storage_connector, | ||
data_format, | ||
write_options, | ||
save_mode, | ||
path, | ||
split_names, | ||
): | ||
for i in range(len(feature_dataframe_list)): | ||
split_path = path + "/" + str(split_names[i]) | ||
self._write_single( | ||
feature_dataframe_list[i], | ||
storage_connector, | ||
data_format, | ||
write_options, | ||
save_mode, | ||
split_path, | ||
) | ||
|
||
def _write_single( | ||
self, | ||
feature_dataframe, | ||
storage_connector, | ||
data_format, | ||
write_options, | ||
save_mode, | ||
path, | ||
): | ||
# TODO: currently not supported petastorm, hdf5 and npy file formats | ||
engine.get_instance().write( | ||
feature_dataframe, | ||
storage_connector, | ||
data_format, | ||
save_mode, | ||
write_options, | ||
path, | ||
) |
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.
Uh oh!
There was an error while loading. Please reload this page.