-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring and addition of DataManager
- Loading branch information
1 parent
b1dc75c
commit 2c83ab8
Showing
86 changed files
with
194 additions
and
30 deletions.
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 |
---|---|---|
@@ -1,2 +1 @@ | ||
|
||
__version__ = "0.0.1dev" |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## Data Manager | ||
|
||
Logic to handle the pre-processing of uploaded data | ||
|
||
The resulting object which handles the parsing of raw data into a format | ||
suitable for a `Pipeline` object is part of the end model's pre-processing step. |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from .manager import DataManager |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import io | ||
import boto3 | ||
import pandas as pd | ||
from hemlock_highway.settings import PROJECT_CONFIG | ||
|
||
|
||
class DataManager: | ||
|
||
X, y = pd.DataFrame(), pd.Series() | ||
|
||
def __new__(cls, *args, **kwargs): | ||
cls.s3_client = boto3.client('s3', region_name=PROJECT_CONFIG.AWS_REGION) | ||
return super().__new__(cls) | ||
|
||
def __init__(self, data_endpoint: str, target_column: str, **read_args): | ||
""" | ||
Load the head of data stored at either a bucket location, or an http endpoint | ||
Parameters | ||
---------- | ||
data_endpoint: str - Either an s3 bucket or an http endpoint | ||
target_column: str - After loading the dataset, this is designated as the target column | ||
read_args: dict - Any additional pandas.read_csv() kwargs | ||
""" | ||
self.data_endpoint = data_endpoint | ||
self.target_column = target_column | ||
self.read_args = read_args | ||
|
||
def load(self): | ||
""" | ||
Execute the load from either http or s3 | ||
""" | ||
if self.data_endpoint.startswith('http://') or self.data_endpoint.startswith('https://'): | ||
self.X = pd.read_csv(filepath_or_buffer=self.data_endpoint, **self.read_args) | ||
else: | ||
resp = self.s3_client.get_object(Bucket=self.data_endpoint.split('/')[0], | ||
Key='/'.join(self.data_endpoint.split('/')[1:]) | ||
) | ||
if resp['ResponseMetadata']['HTTPStatusCode'] == 200: | ||
self.X = pd.read_csv(io.BytesIO(resp['Body'].read()), **self.read_args) | ||
else: | ||
raise IOError(f'Error fetching dataset from S3: {resp}') | ||
|
||
self.y = self.X[self.target_column] | ||
self.X = self.X[[col for col in self.X.columns if col != self.target_column]] | ||
|
||
@property | ||
def _loaded(self): | ||
return self.X.shape[0] > 0 if hasattr(self.X, 'shape') else False |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
This package contains models suitable for use | ||
by the `model_runner` and `server` resources. |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## Model Runner | ||
|
||
API service to take requests to train a model or make predictions |
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from .server import app |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
from .v1 import api_v1_blueprint |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
2 changes: 1 addition & 1 deletion
2
hemlock_highway/user_mgmt/auth.py → hemlock_highway/server/user_mgmt/auth.py
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
File renamed without changes.
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
|
||
|
||
class ProjectConfig: | ||
|
||
# Directories | ||
REPO_ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..') | ||
HEMLOCK_HIGHWAY_MODULE_ROOT_DIR = os.path.join(REPO_ROOT_DIR, 'hemlock_highway') | ||
TEST_ROOT_DIR = os.path.join(REPO_ROOT_DIR, 'tests') | ||
TEST_DATA_DIR = os.path.join(TEST_ROOT_DIR, 'data') | ||
|
||
AWS_REGION = 'eu-west-1' | ||
|
||
|
||
PROJECT_CONFIG = ProjectConfig() |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
col1,col2,target | ||
1,2,3 | ||
2,3,5 | ||
3,4,7 | ||
4,5,9 | ||
5,6,11 | ||
6,7,13 | ||
7,8,15 |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import unittest | ||
import moto | ||
|
||
import botocore.exceptions | ||
from .utils import fake_data_on_s3 | ||
|
||
|
||
|
||
class DataManagerTestCase(unittest.TestCase): | ||
|
||
def setUp(self): | ||
|
||
# Test data directory | ||
self.DATA_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), '..', 'data') | ||
|
||
@fake_data_on_s3(local_dataset='basic_integer_data.csv', bucket='test', key='data/basic.csv') | ||
def test_basic_integer_data_load(self): | ||
""" | ||
Test the basic loading of a dataset. | ||
""" | ||
from hemlock_highway.data_manager import DataManager | ||
dm = DataManager(data_endpoint='test/data/basic.csv', target_column='target') | ||
self.assertFalse(dm._loaded, msg='DataManger should not load data on initialization! Reports it is loaded!') | ||
dm.load() | ||
self.assertTrue(dm._loaded, msg='After asking to load data, DataManager is reporting it is not loaded!') | ||
self.assertTrue(dm.X.shape[0] > 0, msg='DataManager reports it is loaded, but does not have any data in X!') | ||
|
||
@moto.mock_s3 | ||
def test_load_non_existant_data(self): | ||
""" | ||
Test IOError when trying to load a non-existant dataset. | ||
""" | ||
from hemlock_highway.data_manager import DataManager | ||
dm = DataManager(data_endpoint='test/data/basic.csv', target_column='target') | ||
|
||
# Should raise an exception when trying to load a dataset that doesn't exist. | ||
with self.assertRaises(botocore.exceptions.ClientError): | ||
dm.load() | ||
|
||
def test_load_from_http(self): | ||
""" | ||
Ensure dataloader can load a dataset via http | ||
""" | ||
from hemlock_highway.data_manager import DataManager | ||
dm = DataManager( | ||
data_endpoint='https://raw.githubusercontent.com/uiuc-cse/data-fa14/gh-pages/data/iris.csv', | ||
target_column='species' | ||
) | ||
dm.load() | ||
self.assertTrue('petal_length' in dm.X.columns, | ||
msg=f'Expected "petal_length" to be in X, but found {dm.X.columns}') | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import boto3 | ||
import moto | ||
from contextlib import contextmanager | ||
from hemlock_highway.settings import PROJECT_CONFIG | ||
|
||
|
||
@contextmanager | ||
def fake_data_on_s3(local_dataset, bucket, key): | ||
|
||
with moto.mock_s3(): | ||
s3 = boto3.client('s3', region_name=PROJECT_CONFIG.AWS_REGION) | ||
s3.create_bucket(Bucket=bucket) | ||
with open(os.path.join(PROJECT_CONFIG.TEST_DATA_DIR, local_dataset), 'rb') as f: | ||
s3.put_object(Bucket=bucket, Key=key, Body=f.read()) | ||
|
||
yield |
Empty file.
File renamed without changes.
Empty file.
Empty file.
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
File renamed without changes.