Skip to content

Commit

Permalink
Adding query category. Updating update cell.
Browse files Browse the repository at this point in the history
  • Loading branch information
uclatommy committed Apr 14, 2017
1 parent 75ea73f commit 206c4d2
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 32 deletions.
63 changes: 40 additions & 23 deletions PyCell/Plugins/PyCell/dataframe_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
Provides Pandas DataFrame functions.
"""
import Quantum
from Quantum import QuReturnCode
import os
import sys
import pandas as pd
from PyCell import registry
from PyCell.custom_cell import Custom
Expand All @@ -18,17 +20,17 @@
{
'name': 'Read_CSV',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Import']
'categories': ['Data', 'Query']
},
{
'name': 'Head',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'Tail',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'Sort_Values',
Expand All @@ -38,7 +40,7 @@
{
'name': 'Column',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'IsNull',
Expand All @@ -53,7 +55,7 @@
{
'name': 'Select',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'Sort_Index',
Expand Down Expand Up @@ -133,17 +135,17 @@
{
'name': 'Update',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'Iloc',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
},
{
'name': 'Loc',
'module': 'PyCell.dataframe_cell',
'categories': ['Data', 'Modify']
'categories': ['Data', 'Query']
}
]

Expand All @@ -158,6 +160,10 @@ def data_process(func):
generator. This result is then wrapped back up into an H5 and returned,
where it is expected to be put into an output socket for the next cell to
use.
.. note::
This decorator runs the decorated function in a separate processor and
therefore, will not emit any print statements.
"""
def process_func(*args, **kwargs):
assert isinstance(args[0], Custom), 'args[0] must be self'
Expand All @@ -181,7 +187,7 @@ def multi_process(conn, file, node, func, *args, **kwargs):
largs = (child_conn, file, node, func, *args)
p = Process(target=multi_process, args=largs, kwargs=kwargs)
p.start()
p.join()
p.join(10)
if parent_conn.recv():
new_h5 = H5(file, node)
else:
Expand Down Expand Up @@ -293,7 +299,6 @@ def data_columns(self):
dc = storer.data_columns
return dc

@exception_raiser
def select(self, *args, **kwargs):
"""
Executes a select operation on the hdf5 object.
Expand All @@ -302,7 +307,6 @@ def select(self, *args, **kwargs):
*args, key=self.node, **kwargs)
return sel

@exception_raiser
def select_column(self, *args, **kwargs):
"""
Grabs a series from the store.
Expand Down Expand Up @@ -433,7 +437,6 @@ def __init__(self):
'data_columns': []}
self.outputs = {'dataframe': None}

@exception_raiser
def read_csv(self):
kwargs = {}
if not self.inputs['index_col'] == '':
Expand Down Expand Up @@ -654,11 +657,11 @@ class Column(Custom):
is a single string, the result will be a series.
"""
required = ['data', 'columns']
inputs = {'data': None, 'columns': []}
outputs = {'data': None}

def __init__(self):
self.return_msg_ = 'Ready to select columns!'
self.inputs = {'data': None, 'columns': []}
self.outputs = {'data': None}

@data_process
def column(self, h5):
Expand All @@ -669,13 +672,14 @@ def column(self, h5):
else:
df = h5.select(columns=cols)
except:
df = h5.df[cols]
df = h5.df.loc[:, cols]
return df

def process(self):
self.return_msg_ = 'Selecting column...'
self.outputs['data'] = self.column(self.inputs['data'])
self.return_msg_ = 'Selected'
return super().process()
return super().process(QuReturnCode('OK'))


class IsNull(Custom):
Expand Down Expand Up @@ -1343,28 +1347,41 @@ class Update(Custom):
:type dataframe: DataFrame
:param column: *Required*. The dataframe column that recieves the updates.
:type column: string
:param rows: A series of True and False that determines which rows get
updates.
:type rows: Series
:param values: *Required*. The values to write into the dataframe. The
row index should correspond to the row index of the
dataframe.
:type values: Series
:returns: The updated data.
:rtype: H5
"""
required = ['dataframe', 'column', 'values']
inputs = {'data': None, 'rows': None, 'column': None, 'values': None}
outputs = {'data': None}
required = ['data', 'column', 'values']

def __init__(self):
self.return_msg_ = "Ready to update data."
self.inputs = {'dataframe': None, 'column': None, 'values': None}
self.outputs = {'dataframe': None}

@data_process
def update(self, h5):
assert isinstance(self.inputs['values'], H5), "Socket must be an H5."
assert isinstance(self.inputs['data'], H5), \
"dataframe must be an H5."
df = h5.df
df[self.inputs['column']] = self.inputs['values'].df
if self.inputs['rows'] == '':
df.loc[:, self.inputs['column']] = self.inputs['values'].df
else:
assert isinstance(self.inputs['rows'].df, pd.Series), \
"rows must be a Series."
df.loc[self.inputs['rows'].df, self.inputs['column']] = \
self.inputs['values'].df
return df

def process(self):
self.outputs['dataframe'] = self.update(self.inputs['dataframe'])
self.outputs['data'] = self.update(self.inputs['data'])
self.return_msg_ = 'Data updated!'
return super().process()
if isinstance(self.outputs['data'], H5):
return super().process(QuReturnCode('OK'))
else:
return super().process(QuReturnCode('UNKNOWN'))
18 changes: 9 additions & 9 deletions PyCell/custom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ namespace PyCell{

std::atomic<int> Custom::id_counter{0};

void Custom::pass()
{
//https://misspent.wordpress.com/2009/10/11/boost-python-and-handling-python-exceptions/
PyObject *e, *v, *t;
PyErr_Fetch(&e, &v, &t);
PyErr_Restore(e, v, t);
PyRun_SimpleString("raise");
}

void Custom::declare_params(CellSockets &p)
{
p.declare<std::string>("py_import", "Module import name.", "PyCell");
Expand Down Expand Up @@ -229,15 +238,6 @@ void Custom::activate()
}
}

void Custom::pass()
{
//https://misspent.wordpress.com/2009/10/11/boost-python-and-handling-python-exceptions/
PyObject *e, *v, *t;
PyErr_Fetch(&e, &v, &t);
PyErr_Restore(e, v, t);
PyRun_SimpleString("raise");
}

void Custom::deactivate()
{
#if DEBUG_MODE > 0
Expand Down

0 comments on commit 206c4d2

Please sign in to comment.