text: # -*- coding: utf-8 -*-
  # @Author  : Yandai
  # @Email   : 18146573209@163.com
  # @File    : ctgan_base.py
  # @Time    : 2021-7-20 13:50
  # @Software: PyCharm
  
  from ctgan import CTGANSynthesizer
  from ctgan import load_demo
  import warnings
  warnings.filterwarnings("ignore")
  import matplotlib.pyplot as plt
  import numpy as np
  import pandas as pd
  import matplotlib.pyplot as plt
  from ctgan.gen_blobs import makeBlobs
  
  
  def find_nearest(array, value):
      array = np.asarray(array)
      idx = (np.abs(array - value)).argmin()
      return array[idx]
  
  X,y,X_test,y_test = makeBlobs(split=True, sr=0.1)
  array = X[:,1]
  dis = np.max(array) - np.min(array)
  X[:,1][(X[:,1] <= np.min(array) + dis * .2)] = np.min(array)
  X[:,1][(X[:,1] >= np.max(array) - dis * .2)] = np.max(array)
  
  # values = np.linspace(np.min(array),np.max(array),num=20)
  # for i in range(np.shape(X)[0]):
  #     X[i,1]= find_nearest(values,X[i,1])
  
  
  X_maj = X[np.where(y==0)]
  y_maj = y[np.where(y==0)]
  X_min = X[np.where(y==1)]
  y_min = y[np.where(y==1)]
  
  print(np.shape(X),np.shape(X_test))
  plt.scatter(X_maj[:,0],X_maj[:,1],s=.1)
  plt.show()
  plt.scatter(X_min[:,0],X_min[:,1],s=.1)
  plt.show()
  
  data = pd.DataFrame(np.hstack((X,y.reshape(-1,1))))
  data.columns = ["f1","f2","y"]
  
  
  # data = load_demo()
  print(data.columns)
  print(type(data.shape))
  
  
  # Names of the columns that are discrete
  discrete_columns = [
      'f2',
      'y'
  ]
  
  ctgan = CTGANSynthesizer(epochs=10)
  ctgan.fit(data, discrete_columns)
  
  # Synthetic copy
  samples_ori = ctgan.sample(10000)
  samples = samples_ori.values[:,:2]
  labels = samples_ori.values[:,2]
  np.savetxt("Results/samples_2d.csv",samples,delimiter=",")
  
  samples_maj = samples[np.where(labels==0)]
  labels_maj = labels[np.where(labels==0)]
  samples_min = samples[np.where(labels==1)]
  labels_min = labels[np.where(labels==1)]
  
  print(np.shape(samples_maj),np.shape(samples_min))
  
  plt.scatter(samples_maj[:,0],samples_maj[:,1],s=.1)
  plt.show()
  plt.scatter(samples_min[:,0],samples_min[:,1],s=.1)
  plt.show()
  