|
|
|
"""
|
|
|
|
(c) 2019 Data Maker, hiplab.mc.vanderbilt.edu
|
|
|
|
version 1.0.0
|
|
|
|
|
|
|
|
This package serves as a proxy to the overall usage of the framework.
|
|
|
|
This package is designed to generate synthetic data from a dataset from an original dataset using deep learning techniques
|
|
|
|
|
|
|
|
@TODO:
|
|
|
|
- Make configurable GPU, EPOCHS
|
|
|
|
"""
|
|
|
|
import pandas as pd
|
|
|
|
import numpy as np
|
|
|
|
ROUND_UP = 2
|
|
|
|
# _BINARY= ContinuousToDiscrete.binary(X,BIN_SIZE)
|
|
|
|
# This
|
|
|
|
if 'gpu' in _args :
|
|
|
|
# f = open(os.sep.join([_args['logs'],'output',_args['context'],'map.json']))
|
|
|
|
lparams['partition'] = partition
|
|
|
|
# self.logpath= _args['logpath'] if 'logpath' in _args else 'logs'
|
|
|
|
writer.write(self._encoder._map,overwrite=True)
|
|
|
|
writer.close()
|
|
|
|
|
|
|
|
#
|
|
|
|
# @TODO: At this point we need to generate another some other objects
|
|
|
|
#
|
|
|
|
_args = {"network_args":self.network_args,"store":self.store,"info":self.info,"candidates":self.candidates,"data":self._df}
|
|
|
|
if self.gpu :
|
|
|
|
_args['gpu'] = self.gpu
|
|
|
|
g = Generator(**_args)
|
|
|
|
# g.run()
|
|
|
|
self.generate = g
|
|
|
|
if self.autopilot :
|
|
|
|
self.generate.run()
|
|
|
|
def generate (self):
|
|
|
|
if self.autopilot :
|
|
|
|
print( "Autopilot is set ... No need to call this function")
|
|
|
|
else:
|
|
|
|
raise Exception( "Autopilot has not been, Wait till training is finished. Use is_alive function on process object")
|
|
|
|
|
|
|
|
class Generator (Learner):
|
|
|
|
def __init__(self,**_args):
|
|
|
|
super().__init__(**_args)
|
|
|
|
#
|
|
|
|
# We need to load the mapping information for the space we are working with ...
|
|
|
|
#
|
|
|
|
self.network_args['candidates'] = int(_args['candidates']) if 'candidates' in _args else 1
|
|
|
|
filename = os.sep.join([self.network_args['logs'],'output',self.network_args['context'],'map.json'])
|
|
|
|
file = open(filename)
|
|
|
|
self._map = json.loads(file.read())
|
|
|
|
file.close()
|
|
|
|
def run(self):
|
|
|
|
self.initalize()
|
|
|
|
#
|
|
|
|
# The values will be returned because we have provided _map information from the constructor
|
|
|
|
#
|
|
|
|
values,_matrix = self._encoder.convert()
|
|
|
|
_args = self.network_args
|
|
|
|
_args['map'] = self._map
|
|
|
|
_args['values'] = np.array(values)
|
|
|
|
_args['row_count'] = self._df.shape[0]
|
|
|
|
|
|
|
|
gHandler = gan.Predict(**_args)
|
|
|
|
gHandler.load_meta(columns=None)
|
|
|
|
_iomatrix = gHandler.apply()
|
|
|
|
_candidates= [ self._encoder.revert(matrix=_item) for _item in _iomatrix]
|
|
|
|
self.post(_candidates)
|
|
|
|
def appriximate(self,_df):
|
|
|
|
_columns = self.info['approximate']
|
|
|
|
_schema = {}
|
|
|
|
for _info in self.get_schema() :
|
|
|
|
_schema[_info['name']] = _info['type']
|
|
|
|
|
|
|
|
|
|
|
|
for name in _columns :
|
|
|
|
batches = np.array_split(_df[name].values,10)
|
|
|
|
x = []
|
|
|
|
for values in batches :
|
|
|
|
_values = np.random.dirichlet(values)
|
|
|
|
x += list(values + _values )if np.random.randint(0,2) else list(values - _values)
|
|
|
|
_df[name] = np.int64(x) if 'int' in _schema[name] else np.float64(x)
|
|
|
|
return _df
|
|
|
|
def format(self,_df):
|
|
|
|
pass
|
|
|
|
def post(self,_candidates):
|
|
|
|
|
|
|
|
_store = self.store['target'] if 'target' in self.store else {'provider':'console'}
|
|
|
|
_store['lock'] = True
|
|
|
|
writer = transport.factory.instance(**_store)
|
|
|
|
|
|
|
|
for _iodf in _candidates :
|
|
|
|
_df = self._df.copy()
|
|
|
|
_df[self.columns] = _iodf[self.columns]
|
|
|
|
if 'approximate' in self.info :
|
|
|
|
|
|
|
|
_df = self.appriximate(_df)
|
|
|
|
writer.write(_df,schema=self.get_schema())
|
|
|
|
pass
|
|
|
|
class factory :
|
|
|
|
_infocache = {}
|
|
|
|
@staticmethod
|
|
|
|
def instance(**_args):
|
|
|
|
"""
|
|
|
|
An instance of an object that trains and generates candidate datasets
|
|
|
|
:param gpu (optional) index of the gpu to be used if using one
|
|
|
|
:param store {source,target} if no target is provided console will be output
|
|
|
|
:param epochs (default 2) number of epochs to train
|
|
|
|
:param candidates(default 1) number of candidates to generate
|
|
|
|
:param info {columns,sql,from}
|
|
|
|
:param autopilot will generate output automatically
|
|
|
|
:param batch (default 2k) size of the batch
|
|
|
|
"""
|
|
|
|
return Trainer(**_args)
|