|
|
|
@ -30,11 +30,13 @@ class Components :
|
|
|
|
|
condition = ' '.join([args['condition']['field'],args['condition']['qualifier'],'(',args['condition']['value'],')'])
|
|
|
|
|
SQL = " ".join([SQL,'WHERE',condition])
|
|
|
|
|
|
|
|
|
|
SQL = SQL.replace(':dataset',args['dataset']) #+ " LIMIT 1000 "
|
|
|
|
|
SQL = SQL.replace(':dataset',args['dataset']) #+ " LI "
|
|
|
|
|
|
|
|
|
|
if 'limit' in args :
|
|
|
|
|
SQL = SQL + 'LIMIT ' + args['limit']
|
|
|
|
|
SQL = SQL + ' LIMIT ' + args['limit']
|
|
|
|
|
|
|
|
|
|
credentials = service_account.Credentials.from_service_account_file('/home/steve/dev/aou/accounts/curation-prod.json')
|
|
|
|
|
df = pd.read_gbq(SQL,credentials=credentials,dialect='standard')
|
|
|
|
|
df = pd.read_gbq(SQL,credentials=credentials,dialect='standard').astype(object)
|
|
|
|
|
return df
|
|
|
|
|
|
|
|
|
|
# return lambda: pd.read_gbq(SQL,credentials=credentials,dialect='standard')[args['columns']].dropna()
|
|
|
|
@ -51,7 +53,8 @@ class Components :
|
|
|
|
|
#
|
|
|
|
|
# @TODO: we need to log something here about the parameters being passed
|
|
|
|
|
# pointer = args['reader'] if 'reader' in args else lambda: Components.get(**args)
|
|
|
|
|
df = args['reader']()
|
|
|
|
|
df = args['data']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# if df.shape[0] == 0 :
|
|
|
|
|
# print ("CAN NOT TRAIN EMPTY DATASET ")
|
|
|
|
@ -62,85 +65,43 @@ class Components :
|
|
|
|
|
|
|
|
|
|
logger = factory.instance(type='mongo.MongoWriter',args={'dbname':'aou','doc':args['context']})
|
|
|
|
|
log_folder = args['logs'] if 'logs' in args else 'logs'
|
|
|
|
|
_args = {"batch_size":10000,"logs":log_folder,"context":args['context'],"max_epochs":150,"column":args['columns'],"id":"person_id","logger":logger}
|
|
|
|
|
|
|
|
|
|
_args['max_epochs'] = 150 if 'max_epochs' not in args else int(args['max_epochs'])
|
|
|
|
|
_args['num_gpu'] = int(args['num_gpu']) if 'num_gpu' in args else 1
|
|
|
|
|
_args['gpu'] = args['gpu'] if 'gpu' in args else 0
|
|
|
|
|
|
|
|
|
|
# MAX_ROWS = args['max_rows'] if 'max_rows' in args else 0
|
|
|
|
|
PART_SIZE = int(args['part_size']) if 'part_size' in args else 8
|
|
|
|
|
|
|
|
|
|
if 'partition' not in args:
|
|
|
|
|
lbound = 0
|
|
|
|
|
# bounds = list(pd.cut( np.arange(df.shape[0]+1),PART_SIZE).categories)
|
|
|
|
|
# bounds = Components.split(df,MAX_ROWS,PART_SIZE)
|
|
|
|
|
columns = args['columns']
|
|
|
|
|
df = np.array_split(df[columns].values,PART_SIZE)
|
|
|
|
|
qwriter = factory.instance(type='queue.QueueWriter',args={'queue':'aou.io'})
|
|
|
|
|
part_index = 0
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# let's start n processes to listen & train this mother ...
|
|
|
|
|
#
|
|
|
|
|
#-- hopefully they learn as daemons
|
|
|
|
|
# _args = {"batch_size":10000,"logs":log_folder,"context":args['context'],"max_epochs":150,"column":args['columns'],"id":"person_id","logger":logger}
|
|
|
|
|
|
|
|
|
|
for _df in df:
|
|
|
|
|
|
|
|
|
|
# _args['logs'] = os.sep.join([log_folder,str(part_index)])
|
|
|
|
|
_args['partition'] = str(part_index)
|
|
|
|
|
_args['logger'] = {'args':{'dbname':'aou','doc':args['context']},'type':'mongo.MongoWriter'}
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# We should post the the partitions to a queue server (at least the instructions on ):
|
|
|
|
|
# - where to get the data
|
|
|
|
|
# - and athe arguments to use (partition #,columns,gpu,epochs)
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
_df = pd.DataFrame(_df,columns=columns)
|
|
|
|
|
# print (columns)
|
|
|
|
|
# _args['max_epochs'] = 150 if 'max_epochs' not in args else int(args['max_epochs'])
|
|
|
|
|
# _args['num_gpu'] = int(args['num_gpu']) if 'num_gpu' in args else 1
|
|
|
|
|
# _args['gpu'] = args['gpu'] if 'gpu' in args else 0
|
|
|
|
|
|
|
|
|
|
info = {"rows":_df.shape[0],"cols":_df.shape[1], "partition":part_index,"logs":_args['logs'],"num_gpu":1,"part_size":PART_SIZE}
|
|
|
|
|
p = {"args":_args,"data":_df.to_dict(orient="records"),"input":info}
|
|
|
|
|
part_index += 1
|
|
|
|
|
qwriter.write(p)
|
|
|
|
|
#
|
|
|
|
|
# @TODO:
|
|
|
|
|
# - Notify that information was just posted to the queue
|
|
|
|
|
# In case we want slow-mode, we can store the partitions in mongodb and process (Yes|No)?
|
|
|
|
|
#
|
|
|
|
|
# # MAX_ROWS = args['max_rows'] if 'max_rows' in args else 0
|
|
|
|
|
PART_SIZE = int(args['part_size'])
|
|
|
|
|
|
|
|
|
|
logger.write({"module":"train","action":"setup-partition","input":info})
|
|
|
|
|
partition = args['partition']
|
|
|
|
|
log_folder = os.sep.join([log_folder,args['context'],str(partition)])
|
|
|
|
|
_args = {"batch_size":10000,"logs":log_folder,"context":args['context'],"max_epochs":150,"column":args['columns'],"id":"person_id","logger":logger}
|
|
|
|
|
_args['max_epochs'] = 150 if 'max_epochs' not in args else int(args['max_epochs'])
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
#
|
|
|
|
|
# We ask the process to assume 1 gpu given the system number of GPU and that these tasks can run in parallel
|
|
|
|
|
#
|
|
|
|
|
if int(args['num_gpu']) > 1 :
|
|
|
|
|
_args['gpu'] = int(args['gpu']) if int(args['gpu']) < 8 else np.random.choice(np.arange(8)).astype(int)[0]
|
|
|
|
|
else:
|
|
|
|
|
print ('.....')
|
|
|
|
|
partition = args['partition'] if 'partition' in args else ''
|
|
|
|
|
log_folder = os.sep.join([log_folder,args['context'],str(partition)])
|
|
|
|
|
_args = {"batch_size":10000,"logs":log_folder,"context":args['context'],"max_epochs":150,"column":args['columns'],"id":"person_id","logger":logger}
|
|
|
|
|
_args['max_epochs'] = 150 if 'max_epochs' not in args else int(args['max_epochs'])
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
# We ask the process to assume 1 gpu given the system number of GPU and that these tasks can run in parallel
|
|
|
|
|
#
|
|
|
|
|
if int(args['num_gpu']) > 1 :
|
|
|
|
|
_args['gpu'] = int(args['gpu']) if int(args['gpu']) < 8 else np.random.choice(np.arange(8)).astype(int)[0]
|
|
|
|
|
else:
|
|
|
|
|
_args['gpu'] = 0
|
|
|
|
|
_args['num_gpu'] = 1
|
|
|
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = str(args['gpu'])
|
|
|
|
|
_args['gpu'] = 0
|
|
|
|
|
_args['num_gpu'] = 1
|
|
|
|
|
os.environ['CUDA_VISIBLE_DEVICES'] = str(args['gpu'])
|
|
|
|
|
|
|
|
|
|
_args['store'] = {'type':'mongo.MongoWriter','args':{'dbname':'aou','doc':args['context']}}
|
|
|
|
|
_args['data'] = args['data']
|
|
|
|
|
|
|
|
|
|
_args['data'] = df
|
|
|
|
|
#
|
|
|
|
|
# @log :
|
|
|
|
|
# Logging information about the training process for this partition (or not)
|
|
|
|
|
#
|
|
|
|
|
# print (['partition ',partition,df.value_source_concept_id.unique()])
|
|
|
|
|
#
|
|
|
|
|
# @log :
|
|
|
|
|
# Logging information about the training process for this partition (or not)
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
info = {"rows":df.shape[0],"cols":df.shape[1], "partition":int(partition),"logs":_args['logs']}
|
|
|
|
|
info = {"rows":df.shape[0],"cols":df.shape[1], "partition":int(partition),"logs":_args['logs']}
|
|
|
|
|
|
|
|
|
|
logger.write({"module":"train","action":"train","input":info})
|
|
|
|
|
data.maker.train(**_args)
|
|
|
|
|
logger.write({"module":"train","action":"train","input":info})
|
|
|
|
|
data.maker.train(**_args)
|
|
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
@ -210,6 +171,7 @@ class Components :
|
|
|
|
|
#
|
|
|
|
|
#-- Let us store all of this into bigquery
|
|
|
|
|
prefix = args['notify']+'.'+_args['context']
|
|
|
|
|
partition = str(partition)
|
|
|
|
|
table = '_'.join([prefix,partition,'io']).replace('__','_')
|
|
|
|
|
folder = os.sep.join([args['logs'],args['context'],partition,'output'])
|
|
|
|
|
if 'file' in args :
|
|
|
|
@ -219,17 +181,19 @@ class Components :
|
|
|
|
|
data_comp.to_csv( _pname,index=False)
|
|
|
|
|
_args['data'].to_csv(_fname,index=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_id = 'path'
|
|
|
|
|
else:
|
|
|
|
|
credentials = service_account.Credentials.from_service_account_file('/home/steve/dev/aou/accounts/curation-prod.json')
|
|
|
|
|
_pname = os.sep.join([folder,table+'.csv'])
|
|
|
|
|
_fname = table.replace('_io','_full_io')
|
|
|
|
|
data_comp.to_gbq(if_exists='replace',destination_table=_pname,credentials='credentials',chunk_size=50000)
|
|
|
|
|
partial = '.'.join(['io',args['context']+'_partial_io'])
|
|
|
|
|
complete= '.'.join(['io',args['context']+'_full_io'])
|
|
|
|
|
data_comp.to_gbq(if_exists='append',destination_table=partial,credentials=credentials,chunksize=50000)
|
|
|
|
|
data_comp.to_csv(_pname,index=False)
|
|
|
|
|
INSERT_FLAG = 'replace' if 'partition' not in args or 'segment' not in args else 'append'
|
|
|
|
|
_args['data'].to_gbq(if_exists=INSERT_FLAG,destination_table=_fname,credentials='credentials',chunk_size=50000)
|
|
|
|
|
|
|
|
|
|
info = {"full":{"path":_fname,"rows":_args['data'].shape[0]},"compare":{"name":_pname,"rows":data_comp.shape[0]} }
|
|
|
|
|
_args['data'].to_gbq(if_exists=INSERT_FLAG,destination_table=complete,credentials=credentials,chunksize=50000)
|
|
|
|
|
_id = 'dataset'
|
|
|
|
|
info = {"full":{_id:_fname,"rows":_args['data'].shape[0]},"partial":{"path":_pname,"rows":data_comp.shape[0]} }
|
|
|
|
|
if partition :
|
|
|
|
|
info ['partition'] = int(partition)
|
|
|
|
|
logger.write({"module":"generate","action":"write","input":info} )
|
|
|
|
@ -280,18 +244,18 @@ if __name__ == '__main__' :
|
|
|
|
|
args['logs'] = args['logs'] if 'logs' in args else 'logs'
|
|
|
|
|
if 'dataset' not in args :
|
|
|
|
|
args['dataset'] = 'combined20191004v2_deid'
|
|
|
|
|
|
|
|
|
|
PART_SIZE = int(args['part_size']) if 'part_size' in args else 8
|
|
|
|
|
#
|
|
|
|
|
# @TODO:
|
|
|
|
|
# Log what was initiated so we have context of this processing ...
|
|
|
|
|
#
|
|
|
|
|
if 'listen' not in SYS_ARGS :
|
|
|
|
|
if 'file' in args :
|
|
|
|
|
reader = lambda: pd.read_csv(args['file']) ;
|
|
|
|
|
DATA = pd.read_csv(args['file']) ;
|
|
|
|
|
else:
|
|
|
|
|
DATA = Components().get(args)
|
|
|
|
|
reader = lambda: DATA
|
|
|
|
|
args['reader'] = reader
|
|
|
|
|
COLUMNS = DATA.columns
|
|
|
|
|
DATA = np.array_split(DATA,PART_SIZE)
|
|
|
|
|
|
|
|
|
|
if 'generate' in SYS_ARGS :
|
|
|
|
|
#
|
|
|
|
@ -299,32 +263,34 @@ if __name__ == '__main__' :
|
|
|
|
|
|
|
|
|
|
content = os.listdir( os.sep.join([args['logs'],args['context']]))
|
|
|
|
|
generator = Components()
|
|
|
|
|
DATA = reader()
|
|
|
|
|
|
|
|
|
|
if ''.join(content).isnumeric() :
|
|
|
|
|
#
|
|
|
|
|
# we have partitions we are working with
|
|
|
|
|
|
|
|
|
|
jobs = []
|
|
|
|
|
del args['reader']
|
|
|
|
|
columns = DATA.columns.tolist()
|
|
|
|
|
DATA = np.array_split(DATA[args['columns']],len(content))
|
|
|
|
|
|
|
|
|
|
for id in ''.join(content) :
|
|
|
|
|
if 'focus' in args and int(args['focus']) != int(id) :
|
|
|
|
|
# columns = DATA.columns.tolist()
|
|
|
|
|
|
|
|
|
|
# DATA = np.array_split(DATA,PART_SIZE)
|
|
|
|
|
|
|
|
|
|
for index in range(0,PART_SIZE) :
|
|
|
|
|
if 'focus' in args and int(args['focus']) != index :
|
|
|
|
|
#
|
|
|
|
|
# This handles failures/recoveries for whatever reason
|
|
|
|
|
# If we are only interested in generating data for a given partition
|
|
|
|
|
continue
|
|
|
|
|
# index = id.index(id)
|
|
|
|
|
|
|
|
|
|
args['partition'] = id
|
|
|
|
|
args['data'] = pd.DataFrame(DATA[(int(id))],columns=args['columns'])
|
|
|
|
|
args['partition'] = index
|
|
|
|
|
args['data'] = DATA[index]
|
|
|
|
|
if int(args['num_gpu']) > 1 :
|
|
|
|
|
args['gpu'] = id
|
|
|
|
|
args['gpu'] = index
|
|
|
|
|
else:
|
|
|
|
|
args['gpu']=0
|
|
|
|
|
make = lambda _args: (Components()).generate(_args)
|
|
|
|
|
job = Process(target=make,args=(args,))
|
|
|
|
|
job.name = 'generator # '+str(id)
|
|
|
|
|
job.name = 'generator # '+str(index)
|
|
|
|
|
job.start()
|
|
|
|
|
jobs.append(job)
|
|
|
|
|
|
|
|
|
@ -370,18 +336,26 @@ if __name__ == '__main__' :
|
|
|
|
|
# qreader.read(1)
|
|
|
|
|
pass
|
|
|
|
|
else:
|
|
|
|
|
PART_SIZE = int(args['jobs']) if 'jobs' in args else 8
|
|
|
|
|
DATA = reader()
|
|
|
|
|
DATA = np.array_split(DATA[args['columns']],PART_SIZE)
|
|
|
|
|
|
|
|
|
|
# DATA = np.array_split(DATA,PART_SIZE)
|
|
|
|
|
|
|
|
|
|
jobs = []
|
|
|
|
|
for index in range(0,int(args['jobs'])) :
|
|
|
|
|
for index in range(0,PART_SIZE) :
|
|
|
|
|
if 'focus' in args and int(args['focus']) != index :
|
|
|
|
|
continue
|
|
|
|
|
args['part_size'] = PART_SIZE
|
|
|
|
|
args['partition'] = index
|
|
|
|
|
_df = pd.DataFrame(DATA[index],columns=args['columns'])
|
|
|
|
|
args['reader'] = lambda: _df
|
|
|
|
|
# _df = pd.DataFrame(DATA[index],columns=args['columns'])
|
|
|
|
|
args['data'] = DATA[index]
|
|
|
|
|
args['data'].to_csv('aou-'+str(index)+'csv',index=False)
|
|
|
|
|
# args['reader'] = lambda: _df
|
|
|
|
|
if int(args['num_gpu']) > 1 :
|
|
|
|
|
args['gpu'] = index
|
|
|
|
|
else:
|
|
|
|
|
args['gpu']=0
|
|
|
|
|
|
|
|
|
|
make = lambda _args: (Components()).train(**_args)
|
|
|
|
|
job = Process(target=make,args=(args,))
|
|
|
|
|
job = Process(target=make,args=( dict(args),))
|
|
|
|
|
job.name = 'Trainer # ' + str(index)
|
|
|
|
|
job.start()
|
|
|
|
|
jobs.append(job)
|
|
|
|
|