# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you 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.

from __future__ import print_function
import numpy
import os
import ssl


def load_mnist(training_num=50000):
    data_path = os.path.join(os.path.dirname(os.path.realpath('__file__')), 'mnist.npz')
    if not os.path.isfile(data_path):
        from six.moves import urllib
        origin = (
            'https://github.com/sxjscience/mxnet/raw/master/example/bayesian-methods/mnist.npz'
        )
        print('Downloading data from %s to %s' % (origin, data_path))
        ctx = ssl._create_unverified_context()
        with urllib.request.urlopen(origin, context=ctx) as u, open(data_path, 'wb') as f:
            f.write(u.read())
        print('Done!')
    dat = numpy.load(data_path)
    X = (dat['X'][:training_num] / 126.0).astype('float32')
    Y = dat['Y'][:training_num]
    X_test = (dat['X_test'] / 126.0).astype('float32')
    Y_test = dat['Y_test']
    Y = Y.reshape((Y.shape[0],))
    Y_test = Y_test.reshape((Y_test.shape[0],))
    return X, Y, X_test, Y_test


def load_toy():
    training_data = numpy.loadtxt('toy_data_train.txt')
    testing_data = numpy.loadtxt('toy_data_test_whole.txt')
    X = training_data[:, 0].reshape((training_data.shape[0], 1))
    Y = training_data[:, 1].reshape((training_data.shape[0], 1))
    X_test = testing_data[:, 0].reshape((testing_data.shape[0], 1))
    Y_test = testing_data[:, 1].reshape((testing_data.shape[0], 1))
    return X, Y, X_test, Y_test


def load_synthetic(theta1, theta2, sigmax, num=20):
    flag = numpy.random.randint(0, 2, (num,))
    X = flag * numpy.random.normal(theta1, sigmax, (num,)) \
        + (1.0 - flag) * numpy.random.normal(theta1 + theta2, sigmax, (num,))
    return X

[((36, 10, 36, 31), 'numpy.load', 'numpy.load', ({(36, 21, 36, 30): 'data_path'}, {}), '(data_path)', False, 'import numpy\n'), ((47, 20, 47, 55), 'numpy.loadtxt', 'numpy.loadtxt', ({(47, 34, 47, 54): '"""toy_data_train.txt"""'}, {}), "('toy_data_train.txt')", False, 'import numpy\n'), ((48, 19, 48, 59), 'numpy.loadtxt', 'numpy.loadtxt', ({(48, 33, 48, 58): '"""toy_data_test_whole.txt"""'}, {}), "('toy_data_test_whole.txt')", False, 'import numpy\n'), ((57, 11, 57, 45), 'numpy.random.randint', 'numpy.random.randint', ({(57, 32, 57, 33): '0', (57, 35, 57, 36): '2', (57, 38, 57, 44): '(num,)'}, {}), '(0, 2, (num,))', False, 'import numpy\n'), ((26, 11, 26, 36), 'os.path.isfile', 'os.path.isfile', ({(26, 26, 26, 35): 'data_path'}, {}), '(data_path)', False, 'import os\n'), ((32, 14, 32, 46), 'ssl._create_unverified_context', 'ssl._create_unverified_context', ({}, {}), '()', False, 'import ssl\n'), ((25, 45, 25, 73), 'os.path.realpath', 'os.path.realpath', ({(25, 62, 25, 72): '"""__file__"""'}, {}), "('__file__')", False, 'import os\n'), ((33, 13, 33, 56), 'six.moves.urllib.request.urlopen', 'urllib.request.urlopen', (), '', False, 'from six.moves import urllib\n'), ((58, 15, 58, 58), 'numpy.random.normal', 'numpy.random.normal', ({(58, 35, 58, 41): 'theta1', (58, 43, 58, 49): 'sigmax', (58, 51, 58, 57): '(num,)'}, {}), '(theta1, sigmax, (num,))', False, 'import numpy\n'), ((59, 25, 59, 77), 'numpy.random.normal', 'numpy.random.normal', ({(59, 45, 59, 60): '(theta1 + theta2)', (59, 62, 59, 68): 'sigmax', (59, 70, 59, 76): '(num,)'}, {}), '(theta1 + theta2, sigmax, (num,))', False, 'import numpy\n')]