Skip to content

Commit

Permalink
add h2o.round() and h2o.signif() and additional pyunit checks
Browse files Browse the repository at this point in the history
  • Loading branch information
ericeckstrand committed Jun 11, 2015
1 parent c36964a commit 865e78d
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
44 changes: 44 additions & 0 deletions h2o-py/h2o/frame.py
Expand Up @@ -1120,6 +1120,50 @@ def transpose(self):
h2o.removeFrameShallow(tmp_key)
return H2OFrame(vecs=vecs)

def signif(self, digits=6):
"""
:return: The rounded values in the H2OFrame to the specified number of significant digits.
"""
if self._vecs is None or self._vecs == []:
raise ValueError("Frame Removed")
key = self.send_frame()
tmp_key = H2OFrame.py_tmp_key()
expr = "(= !{} (signif %{} #{}))".format(tmp_key,key,digits)
h2o.rapids(expr)
# Remove h2o temp frame after var
h2o.removeFrameShallow(key)
j = h2o.frame(tmp_key)
fr = j['frames'][0]
rows = fr['rows']
veckeys = fr['vec_ids']
cols = fr['columns']
colnames = [col['label'] for col in cols]
vecs=H2OVec.new_vecs(zip(colnames, veckeys), rows) # Peel the Vecs out of the returned Frame
h2o.removeFrameShallow(tmp_key)
return H2OFrame(vecs=vecs)

def round(self, digits=0):
"""
:return: The rounded values in the H2OFrame to the specified number of decimal digits.
"""
if self._vecs is None or self._vecs == []:
raise ValueError("Frame Removed")
key = self.send_frame()
tmp_key = H2OFrame.py_tmp_key()
expr = "(= !{} (round %{} #{}))".format(tmp_key,key,digits)
h2o.rapids(expr)
# Remove h2o temp frame after var
h2o.removeFrameShallow(key)
j = h2o.frame(tmp_key)
fr = j['frames'][0]
rows = fr['rows']
veckeys = fr['vec_ids']
cols = fr['columns']
colnames = [col['label'] for col in cols]
vecs=H2OVec.new_vecs(zip(colnames, veckeys), rows) # Peel the Vecs out of the returned Frame
h2o.removeFrameShallow(tmp_key)
return H2OFrame(vecs=vecs)

class H2OVec:
"""
A single column of data that is uniformly typed and possibly lazily computed.
Expand Down
4 changes: 3 additions & 1 deletion h2o-py/h2o/h2o.py
Expand Up @@ -776,7 +776,9 @@ def var(data) : return data.var()
def mean(data) : return data.mean()
def median(data): return data.median()

def transpose(data): return data.transpose()
def transpose(data) : return data.transpose()
def signif(data, digits=6): return data.signif(digits=digits)
def round(data, digits=0) : return data.round(digits=digits)


class H2ODisplay:
Expand Down
8 changes: 8 additions & 0 deletions h2o-py/tests/testdir_munging/unop/pyunit_frame_math_ops.py
Expand Up @@ -14,17 +14,25 @@ def frame_math_ops(ip,port):
asin_acos_atanh_data = [[random.uniform(-1,1) for r in range(10)] for c in range(10)]
acosh_data = [[random.uniform(1,10) for r in range(10)] for c in range(10)]
abs_data = [[random.uniform(-100000,0) for r in range(10)] for c in range(10)]
signif_data = [[0.0000123456, 1], [2, 3]]

h2o_data1 = h2o.H2OFrame(python_obj=sin_cos_tan_atan_sinh_cosh_tanh_asinh_data)
h2o_data2 = h2o.H2OFrame(python_obj=asin_acos_atanh_data)
h2o_data3 = h2o.H2OFrame(python_obj=acosh_data)
h2o_data4 = h2o.H2OFrame(python_obj=abs_data)
h2o_data5 = h2o.H2OFrame(python_obj=signif_data)

np_data1 = np.array(sin_cos_tan_atan_sinh_cosh_tanh_asinh_data)
np_data2 = np.array(asin_acos_atanh_data)
np_data3 = np.array(acosh_data)
np_data4 = np.array(abs_data)

for d in range(1,6):
h2o_signif = h2o.signif(h2o_data5, digits=d)
h2o_round = h2o.round(h2o_data5, digits=d+4)
s = h2o_signif[0,0]
r = h2o_round[0,0]
assert s == r, "Expected these to be equal, but signif: {0}, round: {1}".format(s, r)
h2o_transposed = h2o.transpose(h2o_data1[0:5])
r, c = h2o_transposed.dim()
assert r == 5 and c == 10, "Expected 5 rows and 10 columns, but got {0} rows and {1} columns".format(r,c)
Expand Down

0 comments on commit 865e78d

Please sign in to comment.