# Path: SGDDataset.py
# class SGDDataSet(object):
# 
#     def __init__(self, images, labels, dtype=tf.float32):
#         dtype = tf.as_dtype(dtype).base_dtype
#         
#         if dtype is not tf.float32:
#             raise TypeError('Invalid image dtype %r, expected float32' %dtype)
#         assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape, labels.shape))
#         
#         self._num_examples = images.shape[0]
#         self._images = images
#         self._labels = labels
#         self._epochs_completed = 0
#         self._index_in_epoch = 0
#     
#     def next_batch(self, batch_size):
#         """Return the next `batch_size` examples from this data set."""
#         start = self._index_in_epoch
#         self._index_in_epoch += batch_size
#         if self._index_in_epoch > self._num_examples:
#             # Finished epoch
#             self._epochs_completed += 1
#             # Shuffle the data
#             perm = numpy.arange(self._num_examples)
#             numpy.random.shuffle(perm)
#             self._images = self._images[perm]
#             self._labels = self._labels[perm]
#             # Start next epoch
#             start = 0
#             self._index_in_epoch = batch_size
#             assert batch_size <= self._num_examples
#         end = self._index_in_epoch
#         return self._images[start:end], self._labels[start:end]


import tensorflow as tf
import os, time, json
import numpy as np
import matplotlib.pylab as plt
from openml.apiconnector import APIConnector
from scipy.io.arff import loadarff
from SGDDataset import SGDDataSet
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

def get_dataset(did):
    home_dir = os.path.expanduser("~")
    openml_dir = os.path.join(home_dir, ".openml")
    cache_dir = os.path.join(openml_dir, "cache")
    
    with open(os.path.join(openml_dir, "apikey.txt"), 'r') as fh:
        key = fh.readline().rstrip('\n')
    fh.close()
    
    openml = APIConnector(cache_directory = cache_dir, apikey = key)
    dataset = openml.download_dataset(did)
    # print('Data-set name: %s'%dataset.name)
    # print(dataset.description)
    _, meta = loadarff(dataset.data_file)
    target_attribute = dataset.default_target_attribute
    target_attribute_names = meta[target_attribute][1]

# Path: SGDDataset.py
# class SGDDataSet(object):
# 
#     def __init__(self, images, labels, dtype=tf.float32):
#         dtype = tf.as_dtype(dtype).base_dtype
#         
#         if dtype is not tf.float32:
#             raise TypeError('Invalid image dtype %r, expected float32' %dtype)
#         assert images.shape[0] == labels.shape[0], ('images.shape: %s labels.shape: %s' % (images.shape, labels.shape))
#         
#         self._num_examples = images.shape[0]
#         self._images = images
#         self._labels = labels
#         self._epochs_completed = 0
#         self._index_in_epoch = 0
#     
#     def next_batch(self, batch_size):
#         """Return the next `batch_size` examples from this data set."""
#         start = self._index_in_epoch
#         self._index_in_epoch += batch_size
#         if self._index_in_epoch > self._num_examples:
#             # Finished epoch
#             self._epochs_completed += 1
#             # Shuffle the data
#             perm = numpy.arange(self._num_examples)
#             numpy.random.shuffle(perm)
#             self._images = self._images[perm]
#             self._labels = self._labels[perm]
#             # Start next epoch
#             start = 0
#             self._index_in_epoch = batch_size
#             assert batch_size <= self._num_examples
#         end = self._index_in_epoch
#         return self._images[start:end], self._labels[start:end]


# Path: tensor_flow_softmax_mnist.py
import tensorflow as tf
import os, time, json
import numpy as np
import matplotlib.pylab as plt
from openml.apiconnector import APIConnector
from scipy.io.arff import loadarff
from SGDDataset import SGDDataSet

#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================

def get_dataset(did):
    home_dir = os.path.expanduser("~")
    openml_dir = os.path.join(home_dir, ".openml")
    cache_dir = os.path.join(openml_dir, "cache")
    
    with open(os.path.join(openml_dir, "apikey.txt"), 'r') as fh:
        key = fh.readline().rstrip('\n')
    fh.close()
    
    openml = APIConnector(cache_directory = cache_dir, apikey = key)
    dataset = openml.download_dataset(did)
    # print('Data-set name: %s'%dataset.name)
    # print(dataset.description)
    _, meta = loadarff(dataset.data_file)
    target_attribute = dataset.default_target_attribute
    target_attribute_names = meta[target_attribute][1]

    X, y, attribute_names = dataset.get_dataset(target = target_attribute, return_attribute_names = True)