You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
184 lines
6.5 KiB
Python
184 lines
6.5 KiB
Python
"""
|
|
(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
|
|
import data.gan as gan
|
|
from transport import factory
|
|
from data.bridge import Binary
|
|
import threading as thread
|
|
class ContinuousToDiscrete :
|
|
ROUND_UP = 2
|
|
@staticmethod
|
|
def binary(X,n=4) :
|
|
"""
|
|
This function will convert a continous stream of information into a variety a bit stream of bins
|
|
"""
|
|
# BOUNDS = np.repeat(np.divide(X.max(),n),n).cumsum().tolist()
|
|
|
|
BOUNDS = ContinuousToDiscrete.bounds(np.round(X,ContinuousToDiscrete.ROUND_UP),n)
|
|
|
|
# _map = [{"index":BOUNDS.index(i),"ubound":i} for i in BOUNDS]
|
|
_matrix = []
|
|
m = []
|
|
for value in X :
|
|
x_ = np.zeros(n)
|
|
_matrix.append(x_)
|
|
for row in BOUNDS :
|
|
|
|
if value>= row.left and value <= row.right :
|
|
index = BOUNDS.index(row)
|
|
x_[index] = 1
|
|
break
|
|
|
|
return _matrix
|
|
|
|
@staticmethod
|
|
def bounds(x,n):
|
|
return list(pd.cut(np.array( np.round(x,ContinuousToDiscrete.ROUND_UP) ),n).categories)
|
|
|
|
|
|
|
|
@staticmethod
|
|
def continuous(X,BIN_SIZE=4) :
|
|
"""
|
|
This function will approximate a binary vector given boundary information
|
|
:X binary matrix
|
|
:BIN_SIZE
|
|
"""
|
|
BOUNDS = ContinuousToDiscrete.bounds(X,BIN_SIZE)
|
|
|
|
values = []
|
|
_BINARY= ContinuousToDiscrete.binary(X,BIN_SIZE)
|
|
# # print (BOUNDS)
|
|
|
|
# values = []
|
|
for row in _BINARY :
|
|
# ubound = BOUNDS[row.index(1)]
|
|
index = np.where(row == 1)[0][0]
|
|
|
|
ubound = BOUNDS[ index ].right
|
|
lbound = BOUNDS[ index ].left
|
|
|
|
x_ = np.round(np.random.uniform(lbound,ubound),ContinuousToDiscrete.ROUND_UP).astype(float)
|
|
values.append(x_)
|
|
|
|
lbound = ubound
|
|
|
|
return values
|
|
|
|
|
|
|
|
|
|
def train (**args) :
|
|
"""
|
|
This function is intended to train the GAN in order to learn about the distribution of the features
|
|
:column columns that need to be synthesized (discrete)
|
|
:logs where the output of the (location on disk)
|
|
:id identifier of the dataset
|
|
:data data-frame to be synthesized
|
|
:context label of what we are synthesizing
|
|
"""
|
|
column = args['column'] if (isinstance(args['column'],list)) else [args['column']]
|
|
CONTINUOUS = args['continuous'] if 'continuous' in args else []
|
|
# column_id = args['id']
|
|
df = args['data'] if not isinstance(args['data'],str) else pd.read_csv(args['data'])
|
|
df.columns = [name.lower() for name in df.columns]
|
|
#
|
|
# @TODO:
|
|
# Consider sequential training of sub population for extremely large datasets
|
|
#
|
|
|
|
#
|
|
# If we have several columns we will proceed one at a time (it could be done in separate threads)
|
|
# @TODO : Consider performing this task on several threads/GPUs simulataneously
|
|
#
|
|
for col in column :
|
|
# args['real'] = pd.get_dummies(df[col].fillna('')).astype(np.float32).values
|
|
# if 'float' not in df[col].dtypes.name :
|
|
# args['real'] = pd.get_dummies(df[col].fillna('')).astype(np.float32).values
|
|
if 'float' in df[col].dtypes.name and col in CONTINUOUS:
|
|
BIN_SIZE = 4 if 'bin_size' not in args else int(args['bin_size'])
|
|
args['real'] = ContinuousToDiscrete.binary(df[col],BIN_SIZE).astype(np.float32)
|
|
else:
|
|
args['real'] = pd.get_dummies(df[col].dropna()).astype(np.float32).values
|
|
|
|
|
|
args['column'] = col
|
|
args['context'] = col
|
|
context = args['context']
|
|
if 'store' in args :
|
|
args['store']['args']['doc'] = context
|
|
logger = factory.instance(**args['store'])
|
|
args['logger'] = logger
|
|
|
|
else:
|
|
logger = None
|
|
trainer = gan.Train(**args)
|
|
trainer.apply()
|
|
def post(**args):
|
|
"""
|
|
This uploads the tensorflow checkpoint to a data-store (mongodb, biguqery, s3)
|
|
|
|
"""
|
|
pass
|
|
def get(**args):
|
|
"""
|
|
This function will restore a checkpoint from a persistant storage on to disk
|
|
"""
|
|
pass
|
|
def generate(**args):
|
|
"""
|
|
This function will generate a synthetic dataset on the basis of a model that has been learnt for the dataset
|
|
@return pandas.DataFrame
|
|
|
|
:data data-frame to be synthesized
|
|
:column columns that need to be synthesized (discrete)
|
|
:id column identifying an entity
|
|
:logs location on disk where the learnt knowledge of the dataset is
|
|
"""
|
|
# df = args['data']
|
|
df = args['data'] if not isinstance(args['data'],str) else pd.read_csv(args['data'])
|
|
CONTINUOUS = args['continous'] if 'continuous' in args else []
|
|
column = args['column'] if (isinstance(args['column'],list)) else [args['column']]
|
|
# column_id = args['id']
|
|
#
|
|
#@TODO:
|
|
# If the identifier is not present, we should fine a way to determine or make one
|
|
#
|
|
_df = df.copy()
|
|
for col in column :
|
|
args['context'] = col
|
|
args['column'] = col
|
|
|
|
# if 'float' in df[col].dtypes.name or col in CONTINUOUS :
|
|
# #
|
|
# # We should create the bins for the values we are observing here
|
|
# BIN_SIZE = 4 if 'bin_size' not in args else int(args['bin_size'])
|
|
# values = ContinuousToDiscrete.continuous(df[col].values,BIN_SIZE)
|
|
# # values = np.unique(values).tolist()
|
|
# else:
|
|
values = df[col].unique().tolist()
|
|
|
|
args['values'] = values
|
|
args['row_count'] = df.shape[0]
|
|
#
|
|
# we can determine the cardinalities here so we know what to allow or disallow
|
|
handler = gan.Predict (**args)
|
|
handler.load_meta(col)
|
|
r = handler.apply()
|
|
BIN_SIZE = 4 if 'bin_size' not in args else int(args['bin_size'])
|
|
_df[col] = ContinuousToDiscrete.continuous(r[col],BIN_SIZE) if 'float' in df[col].dtypes.name or col in CONTINUOUS else r[col]
|
|
#
|
|
# @TODO: log basic stats about the synthetic attribute
|
|
#
|
|
# print (r)s
|
|
# break
|
|
return _df |