-
Notifications
You must be signed in to change notification settings - Fork 1
/
Parallel Group By.py
151 lines (94 loc) · 3.11 KB
/
Parallel Group By.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# coding: utf-8
# In[1]:
from IPython.parallel import Client
import json
rc = Client()
rc.ids
print( 'We have %s clients'%len(rc.ids ))
dview = rc[:]
# In[2]:
import pandas as pd
import numpy as np
import numpy as np
import pandas as pd
print( 'Hello World' )
N = 37171567
m = 118171
A = np.random.random_integers( 0, m, N )
B = np.random.random_integers( 0, m, N )
#D = [ chr( 98+x ) for x in np.random.random_integers( 0, 25, N ) ]
C = np.random.randn( N )
#df = pd.DataFrame( {'A':A, 'B':B, 'C':C, 'D':D } )
df = pd.DataFrame( {'A':A, 'B':B, 'C':C } )
print( df.head(10) )
# In[3]:
def print_attributes( obj ):
print( '\n'.join( x for x in dir( obj ) if not x.startswith('_' ) ) )
# In[4]:
def split_frame( frame, key, num_splits ):
""" Split frame into (very roughly) approximately equal sizes, by key, using key
"""
percentiles = np.linspace( 0.0, 100.0, num_splits+1 )
percentile_bounds = np.percentile( df[ key ].values, list(percentiles ) )
taken = pd.Series( False, index=df.index )
frames = []
for i in range( num_splits ):
ub = percentile_bounds[i+1]
flt = ( ~taken ) & ( frame[ key ]<=ub )
taken = taken | flt
frames.append( df[ flt ] )
assert np.all( taken )
return frames
#print( split_frame( df, 'A', 3 ) )
# In[5]:
def p_assign_frame( rc, frame, client_variable_name, key ):
""" A split a dataframe, and assign it across the clients
rc - IPython.parallel.Client()
frame - a dataframe
client_variable_name - name of the frame's variable
key - the column that we use for the splitting.
"""
num_splits = len( rc.ids )
frames = split_frame( frame, key, num_splits )
for i in range( num_splits ):
rc[i][ client_variable_name ]=frames[i]
p_assign_frame( rc, df, 'df', 'A' )
# In[6]:
def p_group_by( rc, client_group_name, client_frame_name, *args ):
q = r"""%s=%s.groupby(%s)"""%( client_group_name, client_frame_name, json.dumps( args ) )
rc[:].execute( q )
p_group_by( rc, 'grp', 'df', 'A' )
# In[7]:
def p_group_by_dot( rc, client_group_name, q ):
""" execute code against the client group
"""
q = r"""p_group_by_dot_result=%s.%s"""%( client_group_name, q )
#print( 'Executing %s'%q)
rc[:].execute( q )
df_result = pd.concat( rc[:][ 'p_group_by_dot_result' ] )
rc[:][ 'p_group_by_dot_result' ] = None # delete the result
return df_result
print( p_group_by_dot (rc, 'grp', 'sum()' ).head() )
# In[8]:
grp = df.groupby( 'A' )
print( grp.sum().head() )
# In[9]:
psum = p_group_by_dot (rc, 'grp', 'sum()' )
asum = grp.sum()
assert np.all( psum==asum )
print( 'Parallel and Serial results are the same' )
# In[10]:
from datetime import datetime
t0 = datetime.now()
p_assign_frame( rc, df, 'df', 'A' )
t1 = datetime.now()
print( t1-t0 )
# In[11]:
get_ipython().magic("timeit p_group_by( rc, 'grp', 'df', 'A' )")
# In[12]:
get_ipython().magic("timeit grp = df.groupby( 'A' )")
# In[14]:
# Comparing times...
get_ipython().magic("timeit p_group_by_dot (rc, 'grp', 'sum()' )")
get_ipython().magic('timeit grp.sum()')
# In[13]: