Skip to content

Commit

Permalink
add trial_results
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Hernandez committed Aug 16, 2016
1 parent 349bf9b commit 1371fbf
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ Dependencies
- `sqlalchemy>=1.0.10`
- `bokeh>=0.12.0`
- `matplotlib>=1.5.0`
- `pandas>=0.18.0`
- `GPy` (optional, required for `gp` strategy)
- `hyperopt` (optional, required for `hyperopt_tpe` strategy)
- `nose` (optional, for testing)
Expand Down
1 change: 1 addition & 0 deletions devtools/conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ requirements:
- sqlalchemy
- bokeh
- matplotlib
- pandas

test:

Expand Down
27 changes: 25 additions & 2 deletions osprey/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@
from pkg_resources import resource_filename

from .entry_point import load_entry_point
from .utils import dict_merge, in_directory, prepend_syspath, num_samples
from .utils import (dict_merge, in_directory, prepend_syspath, num_samples,
trials_to_dict)
from .search_space import SearchSpace
from .strategies import BaseStrategy
from .dataset_loaders import BaseDatasetLoader
from .cross_validators import BaseCVFactory
from .trials import make_session
from .trials import Trial, make_session
from .subclass_factory import init_subclass_by_name
from . import eval_scopes

Expand Down Expand Up @@ -300,6 +301,28 @@ def trials(self):
value = make_session(uri, project_name=project_name)
return value

def trial_results(self, table_name=None, success_only=True):
from pandas import DataFrame

db = self.trials()
columns = Trial.__table__.columns

if not table_name:
table_name = Trial.__tablename__

cmd = 'SELECT * FROM %s' % table_name

if success_only:
cmd += ' WHERE (status LIKE "SUCCEEDED");'

query = db.execute(cmd)
results = query.fetchall()

if not results:
return None

return DataFrame(trials_to_dict(results, columns))

@contextlib.contextmanager
def trialscontext(self):
session = self.trials()
Expand Down
22 changes: 22 additions & 0 deletions osprey/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,20 @@
import os.path
import sys
import contextlib
import json
from datetime import datetime
from sklearn.pipeline import Pipeline
from six import StringIO


from .eval_scopes import import_all_estimators
from .trials import JSONEncoded

__all__ = ['dict_merge', 'in_directory', 'prepend_syspath', 'prepend_syspath',
'Unbuffered', 'format_timedelta', 'current_pretty_time',
'short_format_time', 'mock_module', 'join_quoted', 'expand_path',
'is_msmbuilder_estimator', 'num_samples', 'check_arrays',
'trials_to_dict']


def dict_merge(base, top):
Expand Down Expand Up @@ -326,3 +336,15 @@ def check_arrays(*arrays, **options):
checked_arrays.append(array)

return checked_arrays


def trials_to_dict(trials, columns):
for trial in trials:
d = {}
for i, item in enumerate(columns.items()):
key, val = item
new_val = trial[i]
if isinstance(val.type, JSONEncoded):
new_val = json.load(StringIO(new_val))
d[key] = new_val
yield d
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ scikit-learn>=0.17.0
sqlalchemy>=1.0.10
bokeh>=0.12.0
matplotlib>=1.5.0
pandas>=0.18.0

0 comments on commit 1371fbf

Please sign in to comment.