Skip to content

Commit

Permalink
add _to_string method in expr
Browse files Browse the repository at this point in the history
  • Loading branch information
ludi committed Oct 27, 2015
1 parent b1dc00d commit cc02cc6
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion h2o-py/h2o/assembly.py
Expand Up @@ -70,7 +70,7 @@ def fit(self, fr, **fit_params):
for step in self.steps:
res.append(step[1].to_rest(step[0]))
res = "[" + ",".join([_quoted(r.replace('"',"'")) for r in res]) + "]"
j = H2OConnection.post_json(url_suffix="Assembly", steps=res, frame=fr._id, _rest_version=99)
j = H2OConnection.post_json(url_suffix="Assembly", steps=res, frame=fr.frame_id, _rest_version=99)
self.id = j["assembly"]["name"]
return get_frame(j["result"]["name"])

Expand Down
15 changes: 9 additions & 6 deletions h2o-py/h2o/expr.py
Expand Up @@ -18,7 +18,7 @@ def __init__(self,op,*ast):
# - tuple of Exprs; further: ID is one of
# - - None: Expr is lazy, never evaluated
# - - ""; Expr has been executed once, but no temp ID was made
# - - String: this Expr is mid-execution, with the given temp ID.
# - - String: this Expr is mid-execution, with the given temp ID.
# Once execution has completed the ast field will be set to TRUE
self._ast = tuple(child._ex if isinstance(child,frame.H2OFrame) else child for child in ast)
self._id = None # "id" - See above; sometimes None, "", or a "py_tmp" string, or a user string
Expand Down Expand Up @@ -117,7 +117,7 @@ def _eval_driver(self,top):
# temps... to help debug GC issues.
gc.collect()
return self

# Magical count-of-5: (get 2 more when looking at it in debug mode)
# 2 for _do_it frame, 2 for _do_it local dictionary list, 1 for parent
MAGIC_REF_COUNT = 5 if sys.gettrace() is None else 7 # M = debug ? 7 : 5
Expand All @@ -130,8 +130,8 @@ def _eval_driver(self,top):
# is eager, so the time parse as to occur in the correct order relative to
# the timezone change, so cannot be lazy.
def _do_it(self,top):
if self._data is not None: # Data already computed and cached; could a "false-like" cached value
return self._id if isinstance(self._data,dict) else str(self._data)
if self._data is not None: # Data already computed and cached; could a "false-like" cached value
return self._id if isinstance(self._data,dict) else str(self._data)
if self._id: return self._id # Data already computed under ID, but not cached
# Here self._id is either None or ""
# Build the eval expression
Expand Down Expand Up @@ -159,7 +159,7 @@ def _arg_to_expr(arg):
def _clear_impl(self):
if not isinstance(self._ast,tuple): return
for ast in self._ast:
if isinstance(ast,ExprNode):
if isinstance(ast,ExprNode):
ast._clear_impl()
if self._id: self._ast = True # Local pytmp

Expand All @@ -171,7 +171,7 @@ def __del__(self):
def _tabulate(self,tablefmt,rollups):
"""
Pretty tabulated string of all the cached data, and column names
"""
"""
if not isinstance(self._fetch_data(10),dict): return str(self._data) # Scalars print normally
# Pretty print cached data
d = collections.OrderedDict()
Expand Down Expand Up @@ -333,3 +333,6 @@ def _setitem(self, b, c):
src = c if isinstance(c,ExprNode) else (float("nan") if c is None else c)
return (ExprNode(":=" ,self,src,col_expr,row_expr) if colname is None
else ExprNode("append",self,src,colname))

def _to_string(self):
return ' '.join(["("+self._op] + [ExprNode._arg_to_expr(a) for a in self._ast] + [")"])
2 changes: 1 addition & 1 deletion h2o-py/h2o/frame.py
Expand Up @@ -1004,7 +1004,7 @@ def hist(self, breaks="Sturges", plot=True, **kwargs):
total = frame["counts"].sum(True)
densities = [(frame[i,"counts"]/total)*(1/(frame[i,"breaks"]-frame[i-1,"breaks"])) for i in range(1,frame["counts"].nrow)]
densities.insert(0,0)
densities_frame = H2OFrame.fromPython([[d] for d in densities])
densities_frame = H2OFrame.fromPython(densities)
densities_frame.set_names(["density"])
frame = frame.cbind(densities_frame)

Expand Down
4 changes: 2 additions & 2 deletions h2o-py/h2o/transforms/preprocessing.py
Expand Up @@ -91,7 +91,7 @@ def transform(self,X,y=None,**params):
return X[self.cols]

def to_rest(self, step_name):
ast = self._dummy_frame()[self.cols]._ast._debug_print(pprint=False)
ast = self._dummy_frame()[self.cols]._ex._to_string()
return super(H2OColSelect, self).to_rest([step_name,"H2OColSelect",ast,False,"|"])


Expand Down Expand Up @@ -129,7 +129,7 @@ def _transform_helper(self,X,**params):
return res

def to_rest(self, step_name):
ast = self._transform_helper(self._dummy_frame())._ast._debug_print(pprint=False)
ast = self._transform_helper(self._dummy_frame())._ex._to_string()
new_col_names = self.new_col_name
if new_col_names is None: new_col_names=["|"]
elif not isinstance(new_col_names, (list,tuple)): new_col_names = [new_col_names]
Expand Down
5 changes: 2 additions & 3 deletions h2o-py/h2o/transforms/transform_base.py
@@ -1,5 +1,6 @@
from ..frame import H2OFrame
import urllib
from h2o import expr

class TransformAttributeError(AttributeError):
def __init__(self,obj,method):
Expand Down Expand Up @@ -47,9 +48,7 @@ def set_params(self, **params):

@staticmethod
def _dummy_frame():
dummy = H2OFrame()
dummy._id = "py_dummy"
return dummy
return H2OFrame(expr.ExprNode('dummy'))

def to_rest(self, args):
return urllib.quote("{}__{}__{}__{}__{}".format(*args))

0 comments on commit cc02cc6

Please sign in to comment.