#!/usr/bin/env python

import numpy as np

def save_np_array(output_file_path, data_array, header_list):

    np.savetxt(output_file_path,
               data_array,
               #fmt="%10.5f",
               #delimiter=" ",
               header="; ".join(header_list),
               #comments="# "                 # String that will be prepended to the ``header`` and ``footer`` strings, to mark them as comments. Default: '# '.
               )

data = np.random.rand(10, 4)
header = ["rand 1", "rand 2", "rand 3", "rand 4"]

save_np_array("test.dat", data, header)


# coding=utf-8
# Copyright 2020 The TensorFlow Datasets Authors.
#
# 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.

"""MNIST, Fashion MNIST, KMNIST and EMNIST."""

import os
import numpy as np
from six.moves import urllib
import tensorflow.compat.v2 as tf

import tensorflow_datasets.public_api as tfds

# MNIST constants
# CVDF mirror of http://yann.lecun.com/exdb/mnist/
_MNIST_URL = "https://storage.googleapis.com/cvdf-datasets/mnist/"
_MNIST_TRAIN_DATA_FILENAME = "train-images-idx3-ubyte.gz"
_MNIST_TRAIN_LABELS_FILENAME = "train-labels-idx1-ubyte.gz"
_MNIST_TEST_DATA_FILENAME = "t10k-images-idx3-ubyte.gz"
_MNIST_TEST_LABELS_FILENAME = "t10k-labels-idx1-ubyte.gz"
_MNIST_IMAGE_SIZE = 28
MNIST_IMAGE_SHAPE = (_MNIST_IMAGE_SIZE, _MNIST_IMAGE_SIZE, 1)
MNIST_NUM_CLASSES = 10
_TRAIN_EXAMPLES = 60000
_TEST_EXAMPLES = 10000

_MNIST_CITATION = """\
@article{lecun2010mnist,
  title={MNIST handwritten digit database},
  author={LeCun, Yann and Cortes, Corinna and Burges, CJ},
  journal={ATT Labs [Online]. Available: http://yann.lecun.com/exdb/mnist},
  volume={2},
  year={2010}
}
"""

_FASHION_MNIST_CITATION = """\
@article{DBLP:journals/corr/abs-1708-07747,
  author    = {Han Xiao and
               Kashif Rasul and
               Roland Vollgraf},
  title     = {Fashion-MNIST: a Novel Image Dataset for Benchmarking Machine Learning
               Algorithms},
  journal   = {CoRR},
  volume    = {abs/1708.07747},
  year      = {2017},
  url       = {http://arxiv.org/abs/1708.07747},
  archivePrefix = {arXiv},
  eprint    = {1708.07747},
  timestamp = {Mon, 13 Aug 2018 16:47:27 +0200},
  biburl    = {https://dblp.org/rec/bib/journals/corr/abs-1708-07747},
  bibsource = {dblp computer science bibliography, https://dblp.org}
}
"""

_K_MNIST_CITATION = """\
  @online{clanuwat2018deep,
  author       = {Tarin Clanuwat and Mikel Bober-Irizar and Asanobu Kitamoto and Alex Lamb and Kazuaki Yamamoto and David Ha},
  title        = {Deep Learning for Classical Japanese Literature},
  date         = {2018-12-03},
  year         = {2018},
  eprintclass  = {cs.CV},
  eprinttype   = {arXiv},
  eprint       = {cs.CV/1812.01718},
}
"""

_EMNIST_CITATION = """\
@article{cohen_afshar_tapson_schaik_2017,
    title={EMNIST: Extending MNIST to handwritten letters},
    DOI={10.1109/ijcnn.2017.7966217},
    journal={2017 International Joint Conference on Neural Networks (IJCNN)},
    author={Cohen, Gregory and Afshar, Saeed and Tapson, Jonathan and Schaik, Andre Van},
    year={2017}
}
"""

class MNIST(tfds.core.GeneratorBasedBuilder):
  """MNIST."""
  URL = _MNIST_URL

  VERSION = tfds.core.Version("3.0.1")

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=("The MNIST database of handwritten digits."),
        features=tfds.features.FeaturesDict({
            "image": tfds.features.Image(shape=MNIST_IMAGE_SHAPE),
            "label": tfds.features.ClassLabel(num_classes=MNIST_NUM_CLASSES),
        }),
        supervised_keys=("image", "label"),
        homepage="http://yann.lecun.com/exdb/mnist/",
        citation=_MNIST_CITATION,
    )

  def _split_generators(self, dl_manager):
    """Returns SplitGenerators."""
    # Download the full MNIST Database
    filenames = {
        "train_data": _MNIST_TRAIN_DATA_FILENAME,
        "train_labels": _MNIST_TRAIN_LABELS_FILENAME,
        "test_data": _MNIST_TEST_DATA_FILENAME,
        "test_labels": _MNIST_TEST_LABELS_FILENAME,
    }
    mnist_files = dl_manager.download_and_extract(
        {k: urllib.parse.urljoin(self.URL, v) for k, v in filenames.items()})

    # MNIST provides TRAIN and TEST splits, not a VALIDATION split, so we only
    # write the TRAIN and TEST splits to disk.
    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            gen_kwargs=dict(
                num_examples=_TRAIN_EXAMPLES,
                data_path=mnist_files["train_data"],
                label_path=mnist_files["train_labels"],
            )),
        tfds.core.SplitGenerator(
            name=tfds.Split.TEST,
            gen_kwargs=dict(
                num_examples=_TEST_EXAMPLES,
                data_path=mnist_files["test_data"],
                label_path=mnist_files["test_labels"],
            )),
    ]

  def _generate_examples(self, num_examples, data_path, label_path):
    """Generate MNIST examples as dicts.

    Args:
      num_examples (int): The number of example.
      data_path (str): Path to the data files
      label_path (str): Path to the labels

    Yields:
      Generator yielding the next examples
    """
    images = _extract_mnist_images(data_path, num_examples)
    labels = _extract_mnist_labels(label_path, num_examples)
    data = list(zip(images, labels))

    # Using index as key since data is always loaded in same order.
    for index, (image, label) in enumerate(data):
      record = {"image": image, "label": label}
      yield index, record

class FashionMNIST(MNIST):
  URL = "http://fashion-mnist.s3-website.eu-central-1.amazonaws.com/"

  # TODO(afrozm): Try to inherit from MNIST's _info and mutate things as needed.
  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=("Fashion-MNIST is a dataset of Zalando's article images "
                     "consisting of a training set of 60,000 examples and a "
                     "test set of 10,000 examples. Each example is a 28x28 "
                     "grayscale image, associated with a label from 10 "
                     "classes."),
        features=tfds.features.FeaturesDict({
            "image":
                tfds.features.Image(shape=MNIST_IMAGE_SHAPE),
            "label":
                tfds.features.ClassLabel(names=[
                    "T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
                    "Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
                ]),
        }),
        supervised_keys=("image", "label"),
        homepage="https://github.com/zalandoresearch/fashion-mnist",
        citation=_FASHION_MNIST_CITATION,
    )

class KMNIST(MNIST):
  URL = "http://codh.rois.ac.jp/kmnist/dataset/kmnist/"

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=("Kuzushiji-MNIST is a drop-in replacement for the MNIST "
                     "dataset (28x28 grayscale, 70,000 images), provided in "
                     "the original MNIST format as well as a NumPy format. "
                     "Since MNIST restricts us to 10 classes, we chose one "
                     "character to represent each of the 10 rows of Hiragana "
                     "when creating Kuzushiji-MNIST."),
        features=tfds.features.FeaturesDict({
            "image":
                tfds.features.Image(shape=MNIST_IMAGE_SHAPE),
            "label":
                tfds.features.ClassLabel(names=[
                    "o", "ki", "su", "tsu", "na", "ha", "ma", "ya", "re", "wo"
                ]),
        }),
        supervised_keys=("image", "label"),
        homepage="http://codh.rois.ac.jp/kmnist/index.html.en",
        citation=_K_MNIST_CITATION,
    )

class EMNISTConfig(tfds.core.BuilderConfig):
  """BuilderConfig for EMNIST CONFIG."""

  def __init__(self, *, class_number, train_examples, test_examples, **kwargs):
    """BuilderConfig for EMNIST class number.

    Args:
      class_number: There are six different splits provided in this dataset. And
        have different class numbers.
      train_examples: number of train examples
      test_examples: number of test examples
      **kwargs: keyword arguments forwarded to super.
    """
    super(EMNISTConfig, self).__init__(
        version=tfds.core.Version(
            "3.0.0",
            "New split API (https://tensorflow.org/datasets/splits)"),
        **kwargs)
    self.class_number = class_number
    self.train_examples = train_examples
    self.test_examples = test_examples

class EMNIST(MNIST):
  """Emnist dataset."""
  URL = "https://www.itl.nist.gov/iaui/vip/cs_links/EMNIST/gzip.zip"
  VERSION = None  # Configs.

  BUILDER_CONFIGS = [
      EMNISTConfig(
          name="byclass",
          class_number=62,
          train_examples=697932,
          test_examples=116323,
          description="EMNIST ByClass",

      ),
      EMNISTConfig(
          name="bymerge",
          class_number=47,
          train_examples=697932,
          test_examples=116323,
          description="EMNIST ByMerge",
      ),
      EMNISTConfig(
          name="balanced",
          class_number=47,
          train_examples=112800,
          test_examples=18800,
          description="EMNIST Balanced",
      ),
      EMNISTConfig(
          name="letters",
          class_number=37,
          train_examples=88800,
          test_examples=14800,
          description="EMNIST Letters",
      ),
      EMNISTConfig(
          name="digits",
          class_number=10,
          train_examples=240000,
          test_examples=40000,
          description="EMNIST Digits",
      ),
      EMNISTConfig(
          name="mnist",
          class_number=10,
          train_examples=60000,
          test_examples=10000,
          description="EMNIST MNIST",
      ),
  ]

  def _info(self):
    return tfds.core.DatasetInfo(
        builder=self,
        description=(
            "The EMNIST dataset is a set of handwritten character digits "
            "derived from the NIST Special Database 19 and converted to "
            "a 28x28 pixel image format and dataset structure that directly "
            "matches the MNIST dataset.\n\n"
            "Note: Like the original EMNIST data, images provided here are "
            "inverted horizontally and rotated 90 anti-clockwise. You can use "
            "`tf.transpose` within `ds.map` to convert the images to a "
            "human-friendlier format."),
        features=tfds.features.FeaturesDict({
            "image":
                tfds.features.Image(shape=MNIST_IMAGE_SHAPE),
            "label":
                tfds.features.ClassLabel(
                    num_classes=self.builder_config.class_number),
        }),
        supervised_keys=("image", "label"),
        homepage=("https://www.nist.gov/itl/products-and-services/"
                  "emnist-dataset"),
        citation=_EMNIST_CITATION,
    )

  def _split_generators(self, dl_manager):
    filenames = {
        "train_data":
            "emnist-{}-train-images-idx3-ubyte.gz".format(
                self.builder_config.name),
        "train_labels":
            "emnist-{}-train-labels-idx1-ubyte.gz".format(
                self.builder_config.name),
        "test_data":
            "emnist-{}-test-images-idx3-ubyte.gz".format(
                self.builder_config.name),
        "test_labels":
            "emnist-{}-test-labels-idx1-ubyte.gz".format(
                self.builder_config.name),
    }

    dir_name = os.path.join(dl_manager.download_and_extract(self.URL), "gzip")
    extracted = dl_manager.extract({
        k: os.path.join(dir_name, fname) for k, fname in filenames.items()
    })

    return [
        tfds.core.SplitGenerator(
            name=tfds.Split.TRAIN,
            gen_kwargs=dict(
                num_examples=self.builder_config.train_examples,
                data_path=extracted["train_data"],
                label_path=extracted["train_labels"],
            )),
        tfds.core.SplitGenerator(
            name=tfds.Split.TEST,
            gen_kwargs=dict(
                num_examples=self.builder_config.test_examples,
                data_path=extracted["test_data"],
                label_path=extracted["test_labels"],
            ))
    ]

def _extract_mnist_images(image_filepath, num_images):
  with tf.io.gfile.GFile(image_filepath, "rb") as f:
    f.read(16)  # header
    buf = f.read(_MNIST_IMAGE_SIZE * _MNIST_IMAGE_SIZE * num_images)
    data = np.frombuffer(
        buf,
        dtype=np.uint8,
    ).reshape(num_images, _MNIST_IMAGE_SIZE, _MNIST_IMAGE_SIZE, 1)
    return data

def _extract_mnist_labels(labels_filepath, num_labels):
  with tf.io.gfile.GFile(labels_filepath, "rb") as f:
    f.read(8)  # header
    buf = f.read(num_labels)
    labels = np.frombuffer(buf, dtype=np.uint8).astype(np.int64)
    return labels


# -*- coding: utf-8 -*-
"""
    lantz.drivers.legacy.tektronix.tds1012
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Implements the drivers to control an oscilloscope.

    :copyright: 2015 by Lantz Authors, see AUTHORS for more details.
    :license: BSD, see LICENSE for more details.

    Source: Tektronix Manual
"""

import numpy as np
from lantz.core import Action, Feat
from lantz.core.errors import InvalidCommand

from lantz.drivers.legacy.serial import SerialDriver

class TDS1012(SerialDriver):
    """Tektronix TDS1012 100MHz 2 Channel Digital Storage Oscilloscope
    """
    ENCODING = 'ascii'

    RECV_TERMINATION = '\n'
    SEND_TERMINATION = '\n'
    TIMEOUT = -1  # Avoids timeout while acquiring a curve. May not be the

    # best option.

    def __init__(self, port):
        # super().TIMEOUT = 20
        super().__init__(port)
        super().initialize()  # Automatically open the port

    @Action()
    def initiate(self):
        """ Initiates the acquisition in the osciloscope.
        """
        self.send(':ACQ:STATE ON')

    @Action()
    def idn(self):
        """ Identify the Osciloscope
        """
        return self.query('*IDN?')

    @Action()
    def autoset(self):
        """ Adjust the vertical, horizontal and trigger controls to display a
            stable waveform.
        """
        self.send('AUTOS EXEC')

    @Action()
    def autocal(self):
        """ Autocalibration of osciloscope. It may take several minutes to
            complete
        """
        return self.send('*CAL')

    @Feat(limits=(1, 2))
    def datasource(self):
        """ Retrieves the data source from which data is going to be taken.
            TDS1012 has 2 channels
        """
        return self.query('DAT:SOU?')

    @datasource.setter
    def datasource(self, value):
        """ Sets the data source for the acquisition of data.
        """
        self.send('DAT:SOU CH{}'.format(value))

    @Action()
    def acquire_parameters(self):
        """ Acquire parameters of the osciloscope.
            It is intended for adjusting the values obtained in acquire_curve
        """
        values = 'XZE?;XIN?;PT_OF?;YZE?;YMU?;YOF?;'
        answer = self.query('WFMP:{}'.format(values))
        parameters = {}
        for v, j in zip(values.split('?;'), answer.split(';')):
            parameters[v] = float(j)
        return parameters

    @Action()
    def data_setup(self):
        """ Sets the way data is going to be encoded for sending.
        """
        self.send('DAT:ENC ASCI;WID 2')  # ASCII is the least efficient way, but
        # couldn't make the binary mode to work

    @Action()
    def acquire_curve(self, start=1, stop=2500):
        """ Gets data from the oscilloscope. It accepts setting the start and
            stop points of the acquisition (by default the entire range).
        """
        parameters = self.acquire_parameters()
        self.data_setup()
        self.send('DAT:STAR {}'.format(start))
        self.send('DAT:STOP {}'.format(stop))
        data = self.query('CURV?')
        data = data.split(',')
        data = np.array(list(map(float, data)))
        ydata = (data - parameters['YOF']) * parameters['YMU'] \
                + parameters['YZE']
        xdata = np.arange(len(data)) * parameters['XIN'] + parameters['XZE']
        return list(xdata), list(ydata)

    @Action()
    def forcetrigger(self):
        """ Creates a trigger event.
        """
        self.send('TRIG:FORC')
        return

    @Action()
    def triggerlevel(self):
        """ Sets the trigger level to 50% of the minimum and maximum values of
            the signal.
        """
        self.send('TRIG:MAI SETL')

    @Feat(values={'AUTO', 'NORMAL'})
    def trigger(self):
        """ Retrieves trigger state.
        """
        return self.query('TRIG:MAIN:MODE?')

    @trigger.setter
    def trigger(self, state):
        """ Sets the trigger state.
        """
        self.send('TRIG:MAI:MOD {}'.format(state))
        return

    @Feat()
    def horizontal_division(self):
        """ Horizontal time base division.
        """
        return float(self.query('HOR:MAI:SCA?'))

    @horizontal_division.setter
    def horizontal_division(self, value):
        """ Sets the horizontal time base division.
        """
        self.send('HOR:MAI:SCA {}'.format(value))
        return

    @Feat(values={0, 4, 16, 64, 128})
    def number_averages(self):
        """ Number of averages
        """
        answer = self.query('ACQ?')
        answer = answer.split(';')
        if answer[0] == 'SAMPLE':
            return 0
        elif answer[0] == 'AVERAGE':
            return int(self.query('ACQ:NUMAV?'))
        else:
            raise InvalidCommand

    @number_averages.setter
    def number_averages(self, value):
        """ Sets the number of averages. If 0, the it is a continous sample.
        """
        if value == 0:
            self.send('ACQ:MOD SAMPLE')
        else:
            self.send('ACQ:MOD AVE;NUMAV {}'.format(value))

    @Action(values={'FREQ', 'MINI', 'MAXI', 'MEAN'})
    def _measure(self, mode):
        """ Measures the Frequency, Minimum, Maximum or Mean of a signal.
        """
        self.send('MEASU:IMM:TYP {}'.format(mode))
        return float(self.query('MEASU:IMM:VAL?'))

    def measure_mean(self):
        """ Gets the mean of the signal.
        """
        answer = self._measure('MEAN')
        return answer

    def measure_frequency(self):
        """ Gets the frequency of the signal.
        """
        answer = self._measure('FREQ')
        return answer

    def measure_minimum(self):
        """ Gets the minimum of the signal.
        """
        answer = self._measure('MINI')
        return answer

    def measure_maximum(self):
        """ Gets the mean of the signal.
        """
        answer = self._measure('MAXI')
        return answer

if __name__ == '__main__':
    import argparse

    parser = argparse.ArgumentParser(description='Measure using TDS1012 and dump to screen')
    parser.add_argument('-p', '--port', default='/dev/ttyS0',
                        help='Serial port')
    parser.add_argument('-v', '--view', action='store_true', default=True,
                        help='View ')
    parser.add_argument('-c', '--channel', default=1, type=int,
                        help='Channel to use')

    args = parser.parse_args()

    osc = TDS1012(args.port)
    osc.initiate()
    print('Osciloscope Identification: {}'.format(osc.idn))
    print(osc.trigger)
    osc.forcetrigger()
    osc.triggerlevel()
    osc.trigger = "AUTO"
    print(osc.trigger)

    params = osc.acquire_parameters()

    if args.view:
        import matplotlib.pyplot as plt

    if args.view:
        osc.datasource = args.channel
        x, y = osc.acquire_curve()
        x = np.array(x)
        x = x - x.min()
        y = np.array(y)
        plt.plot(x, y)
        plt.show()


# Copyright 2019 D-Wave Systems Inc.
#
#    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.
#
# ================================================================================================
import itertools
import copy

import numpy as np
import dimod

from benchmarks.common import Benchmark

class Construction(Benchmark):
    def setup(self):
        self.h_K100 = {v: 0 for v in range(100)}
        self.J_K100 = {edge: 0 for edge in itertools.combinations(range(100), 2)}

    def time_k100(self):
        dimod.BinaryQuadraticModel(self.h_K100, self.J_K100, 0.0, dimod.SPIN)

    def mem_k100(self):
        return dimod.BinaryQuadraticModel(self.h_K100, self.J_K100, 0.0, dimod.SPIN)

    def time_empty(self):
        dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)

    def mem_empty(self):
        return dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)

class ConstructionVeryLarge(Benchmark):
    def setup(self):
        self.J_15000 = J = {}

        # make a graph of approximately degree 15, 4394 nodes, 32617 edges
        m = n = 13
        t = 13
        J.update((((i, j, 0, k0), (i, j, 1, k1)), 1)
                 for i in range(n)
                 for j in range(m)
                 for k0 in range(t)
                 for k1 in range(t))
        J.update((((i, j, 1, k), (i, j+1, 1, k)), 1)
                 for i in range(m)
                 for j in range(n-1)
                 for k in range(t))
        J.update((((i, j, 0, k), (i+1, j, 0, k)), 1)
                 for i in range(m-1)
                 for j in range(n)
                 for k in range(t))

    def time_J15000(self):
        dimod.BinaryQuadraticModel({}, self.J_15000, 0.0, dimod.SPIN)

    def mem_J15000(self):
        return dimod.BinaryQuadraticModel({}, self.J_15000, 0.0, dimod.SPIN)

class Copy(Benchmark):
    def setup(self):
        h_K100 = {v: 0 for v in range(100)}
        J_K100 = {edge: 0 for edge in itertools.combinations(range(100), 2)}
        self.bqm_k100 = dimod.BinaryQuadraticModel(h_K100, J_K100, 0.0, dimod.SPIN)

        self.bqm_empty = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)

    def time_k100(self):
        self.bqm_k100.copy()

    def time_empty(self):
        self.bqm_empty.copy()

class Deepcopy(Benchmark):
    """How long it takes to do a deep copy"""
    def setup(self):
        h_K100 = {v: 0 for v in range(100)}
        J_K100 = {edge: 0 for edge in itertools.combinations(range(100), 2)}
        self.bqm_k100 = dimod.BinaryQuadraticModel(h_K100, J_K100, 0.0, dimod.SPIN)

        self.bqm_empty = dimod.BinaryQuadraticModel({}, {}, 0.0, dimod.SPIN)

    def time_k100(self):
        copy.deepcopy(self.bqm_k100)

    def time_empty(self):
        copy.deepcopy(self.bqm_empty)

class Energies(Benchmark):
    def setup(self):
        h_K100 = {v: 0 for v in range(100)}
        J_K100 = {edge: 0 for edge in itertools.combinations(range(100), 2)}
        self.bqm_k100 = dimod.BinaryQuadraticModel(h_K100, J_K100, 0.0, dimod.SPIN)

        self.samples_1000x100 = np.ones((1000, 100), dtype=np.int8)

    def time_k100(self):
        self.bqm_k100.energies(self.samples_1000x100)


"""
"""
import numpy as np
from astropy.tests.helper import pytest
from astropy.utils.misc import NumpyRNGContext

from ..model_helpers import custom_spline, create_composite_dtype
from ..model_helpers import bounds_enforcing_decorator_factory, enforce_periodicity_of_box
from ..model_helpers import call_func_table, bind_default_kwarg_mixin_safe

from ...custom_exceptions import HalotoolsError

__all__ = ('test_enforce_periodicity_of_box', 'test_custom_spline1',
    'test_check_multiple_box_lengths', 'test_velocity_flip')

fixed_seed = 43

def _func_maker(i):
    def f(x):
        return x + i
    return f

def test_custom_spline1():
    table_abscissa = (0, 1)
    table_ordinates = (0, 1, 2)
    with pytest.raises(HalotoolsError) as err:
        __ = custom_spline(table_abscissa, table_ordinates)
    substr = "table_abscissa and table_ordinates must have the same length"
    assert substr in err.value.args[0]

def test_custom_spline2():
    table_abscissa = (0, 1, 2)
    table_ordinates = (0, 1, 2)
    with pytest.raises(HalotoolsError) as err:
        __ = custom_spline(table_abscissa, table_ordinates, k=-2)
    substr = "Spline degree must be non-negative"
    assert substr in err.value.args[0]

def test_custom_spline3():
    table_abscissa = (0, 1, 2)
    table_ordinates = (0, 1, 2)
    with pytest.raises(HalotoolsError) as err:
        __ = custom_spline(table_abscissa, table_ordinates, k=0)
    substr = "In spline_degree=0 edge case,"
    assert substr in err.value.args[0]

def test_create_composite_dtype():
    dt1 = np.dtype([('x', 'f4')])
    dt2 = np.dtype([('x', 'i4')])
    with pytest.raises(HalotoolsError) as err:
        result = create_composite_dtype([dt1, dt2])
    substr = "Inconsistent dtypes for name"
    assert substr in err.value.args[0]

def test_bind_default_kwarg_mixin_safe():

    class DummyClass(object):

        def __init__(self, d):
            self.abc = 4

    constructor_kwargs = {'abc': 10}
    obj = DummyClass(constructor_kwargs)
    keyword_argument = 'abc'
    default_value = 0

    with pytest.raises(HalotoolsError) as err:
        __ = bind_default_kwarg_mixin_safe(
            obj, keyword_argument, constructor_kwargs, default_value)
    substr = "Do not pass the  ``abc`` keyword argument "
    assert substr in err.value.args[0]

def test_bounds_enforcing_decorator_factory():
    """
    """
    def f(x):
        return x
    decorator = bounds_enforcing_decorator_factory(0, 1, warning=True)
    decorated_f = decorator(f)
    result = decorated_f(-1)
    assert result == 0

def test_enforce_periodicity_of_box():
    """ Verify that enforce_periodicity_of_box results in all points located
    inside [0, Lbox]
    """

    box_length = 250
    Npts = int(1e5)
    with NumpyRNGContext(fixed_seed):
        coords = np.random.uniform(0, box_length, Npts*3).reshape(Npts, 3)

    perturbation_size = box_length/10.
    with NumpyRNGContext(fixed_seed):
        coord_perturbations = np.random.uniform(
            -perturbation_size, perturbation_size, Npts*3).reshape(Npts, 3)

    coords += coord_perturbations

    newcoords = enforce_periodicity_of_box(coords, box_length)
    assert np.all(newcoords >= 0)
    assert np.all(newcoords <= box_length)

def test_check_multiple_box_lengths():
    """ Verify that enforce_periodicity_of_box function notices when the
    some points lie many box lengths beyond +/- Lbox
    """
    box_length = 250
    Npts = int(1e4)

    x = np.linspace(-2*box_length, box_length, Npts)
    with pytest.raises(HalotoolsError) as err:
        newcoords = enforce_periodicity_of_box(x, box_length,
            check_multiple_box_lengths=True)
    substr = "There is at least one input point with a coordinate less than -Lbox"
    assert substr in err.value.args[0]

    x = np.linspace(-box_length, 2.1*box_length, Npts)
    with pytest.raises(HalotoolsError) as err:
        newcoords = enforce_periodicity_of_box(x, box_length,
            check_multiple_box_lengths=True)
    substr = "There is at least one input point with a coordinate greater than 2*Lbox"
    assert substr in err.value.args[0]

    x = np.linspace(-box_length, 2*box_length, Npts)
    newcoords = enforce_periodicity_of_box(x, box_length,
        check_multiple_box_lengths=True)

def test_velocity_flip():
    """ Verify that enforce_periodicity_of_box function flips the sign of
    the velocity for points where PBCs needed to be enforced
    """
    box_length = 250
    Npts = int(1e4)

    x = np.linspace(-0.5*box_length, 1.5*box_length, Npts)
    vx = np.ones(Npts)

    newcoords, newvel = enforce_periodicity_of_box(
        x, box_length, velocity=vx)

    inbox = ((x >= 0) & (x <= box_length))
    assert np.all(newvel[inbox] == 1.0)
    assert np.all(newvel[~inbox] == -1.0)

def test_call_func_table1():

    num_conc_bins = 5
    f_table = list(_func_maker(i) for i in range(num_conc_bins))

    num_abscissa = 7
    cum_prob = np.array(list(0.1*i for i in range(num_abscissa)))

    func_idx = np.zeros(num_abscissa)
    correct_result = cum_prob
    result = call_func_table(f_table, cum_prob, func_idx)
    assert np.all(result == correct_result)

def test_call_func_table2():

    num_conc_bins = 5
    f_table = list(_func_maker(i) for i in range(num_conc_bins))

    num_abscissa = 7
    cum_prob = np.array(list(0.1*i for i in range(num_abscissa)))

    func_idx = np.zeros(num_abscissa) + 3
    func_idx[2:] = 0
    correct_result = np.zeros(num_abscissa)
    correct_result[:2] = cum_prob[:2] + 3
    correct_result[2:] = cum_prob[2:]

    result = call_func_table(f_table, cum_prob, func_idx)

    assert np.all(result == correct_result)

def test_call_func_table3():
    pass


# -*- coding: utf-8 -*-
"""
@author: Barbara Ikica
"""

from distutils.core import setup, Extension
from Cython.Build import cythonize
import numpy

setup(
    ext_modules = cythonize(Extension("mPW", sources=["mPW.pyx"], language="c++"),
    compiler_directives={'language_level' : 3}),
    include_dirs=[numpy.get_include()]
)

#python setup.py build_ext --inplace
#cython -a mPW.pyx


# -*- coding: utf-8 -*-
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import csv
import numpy as np
import os
import sys

from observations.util import maybe_download_and_extract

def caterpillars(path):
  """Caterpillars

  Measurements on a sample of Manduca Sexta caterpillars

  A dataset with 267 observations on the following 18 variables.

  `Instar`

  Coded from 1 (smallest) to 5 (largest) indicating stage of the
  caterpillar's life

  `ActiveFeeding`

  Indicator (`Y` or `N`) of whether or not the animal is actively
  feeding

  `Fgp`

  Indicator (`Y` or `N`) of whether or not the animal is in a free
  growth period

  `Mgp`

  Indicator (`Y` or `N`) of whether or not the animal is in a maximum
  growth period

  `Mass`

  Body mass (in grams)

  `LogMass`

  Log (base 10) of body mass

  `Intake`

  Wet food intake (in grams/day)

  `LogIntake`

  Log (base 10) of Intake

  `WetFrass`

  Amount of frass (solid waste) produced (in grams/day)

  `LogWetFrass`

  Log (base 10) of WetFrass

  `DryFrass`

  Amount of frass, after drying, produced (in grams/day)

  `LogDryFrass`

  Log (base 10) of DryFrass

  `Cassim`

  CO2 assimilation (ingestion - excretion)

  `LogCassim`

  Log (base 10) of Cassim

  `Nfrass`

  Nitrogen in frass

  `LogNfrass`

  Log (base 10) of Nfrass

  `Nassim`

  Nitrogen assimilation (ingestion - excretion)

  `LogNassim`

  Log (base 10) of Nassim

  We thank Professors Harry Itagaki, Drew Kerkhoff, Chris Gillen, and Judy
  Holdener and their students for sharing this data from research

  Args:

    path: str.
      Path to directory which either stores file or otherwise file will
      be downloaded and extracted there.
      Filename is `caterpillars.csv`.

  Returns:

    Tuple of np.ndarray `x_train` with 267 rows and 18 columns and
    dictionary `metadata` of column headers (feature names).
  """
  import pandas as pd
  path = os.path.expanduser(path)
  filename = 'caterpillars.csv'
  if not os.path.exists(os.path.join(path, filename)):
    url = 'http://dustintran.com/data/r/Stat2Data/Caterpillars.csv'
    maybe_download_and_extract(path, url,
                               save_file_name='caterpillars.csv',
                               resume=False)

  data = pd.read_csv(os.path.join(path, filename), index_col=0,
                     parse_dates=True)
  x_train = data.values
  metadata = {'columns': data.columns}
  return x_train, metadata


# Copyright 2022 Huawei Technologies Co., Ltd
#
# 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.
# ==============================================================================
import copy
import numpy as np
import pytest

import mindspore.dataset as ds
import mindspore.dataset.audio.transforms as audio
from mindspore import log as logger

BATCH = 2
CHANNEL = 2
FREQ = 10
TIME = 10

def allclose_nparray(data_expected, data_me, rtol, atol, equal_nan=True):
    """
    Precision calculation formula
    """
    if np.any(np.isnan(data_expected)):
        assert np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan)
    elif not np.allclose(data_me, data_expected, rtol, atol, equal_nan=equal_nan):
        count_unequal_element(data_expected, data_me, rtol, atol)

def count_unequal_element(data_expected, data_me, rtol, atol):
    """
    Precision calculation func
    """
    assert data_expected.shape == data_me.shape
    total_count = len(data_expected.flatten())
    error = np.abs(data_expected - data_me)
    greater = np.greater(error, atol + np.abs(data_expected) * rtol)
    loss_count = np.count_nonzero(greater)
    assert (loss_count / total_count) < rtol, "\ndata_expected_std:{0}\ndata_me_error:{1}\nloss:{2}".format(
        data_expected[greater], data_me[greater], error[greater])

def gen(shape):
    np.random.seed(0)
    data = np.random.random(shape)
    yield (np.array(data, dtype=np.float32),)

def test_mask_along_axis_iid_eager():
    """
    Feature: MaskAlongAxisIID
    Description: mindspore eager mode with normal testcase
    Expectation: the returned result is as expected
    """
    logger.info("test MaskAlongAxisIID op, eager")
    spectrogram_01 = next(gen((BATCH, CHANNEL, FREQ, TIME)))[0]
    output_01 = audio.MaskAlongAxisIID(mask_param=8, mask_value=5.0, axis=1)(spectrogram_01)
    assert output_01.shape == (BATCH, CHANNEL, FREQ, TIME)

    spectrogram_02 = next(gen((BATCH, CHANNEL, FREQ, TIME)))[0]
    expect_output = copy.deepcopy(spectrogram_02)
    output_02 = audio.MaskAlongAxisIID(mask_param=0, mask_value=5.0, axis=1)(spectrogram_02)
    allclose_nparray(output_02, expect_output, 0.0001, 0.0001)

def test_mask_along_axis_iid_pipeline():
    """
    Feature: MaskAlongAxisIID
    Description: mindspore pipeline mode with normal testcase
    Expectation: the returned result is as expected
    """
    logger.info("test MaskAlongAxisIID op, pipeline")

    generator = gen([BATCH, CHANNEL, FREQ, TIME])
    data1 = ds.GeneratorDataset(source=generator, column_names=["multi_dimensional_data"])

    transforms = [audio.MaskAlongAxisIID(mask_param=8, mask_value=5.0, axis=2)]
    data1 = data1.map(operations=transforms, input_columns=["multi_dimensional_data"])

    for item in data1.create_dict_iterator(num_epochs=1, output_numpy=True):
        out_put = item["multi_dimensional_data"]
    assert out_put.shape == (BATCH, CHANNEL, FREQ, TIME)

def test_mask_along_axis_iid_invalid_input():
    """
    Feature: MaskAlongAxisIID
    Description: mindspore eager mode with invalid input
    Expectation: the returned result is as expected
    """
    def test_invalid_param(test_name, mask_param, mask_value, axis, error, error_msg):
        """
        a function used for checking correct error and message
        """
        logger.info("Test MaskAlongAxisIID with wrong params: {0}".format(test_name))
        with pytest.raises(error) as error_info:
            audio.MaskAlongAxisIID(mask_param, mask_value, axis)
        assert error_msg in str(error_info.value)

    test_invalid_param("invalid mask_param", 1.0, 1.0, 1, TypeError,
                       "Argument mask_param with value 1.0 is not of type [<class 'int'>], but got <class 'float'>.")
    test_invalid_param("invalid mask_param", -1, 1.0, 1, ValueError,
                       "Input mask_param is not within the required interval of [0, 2147483647].")
    test_invalid_param("invalid axis", 5, 1.0, 5.0, TypeError,
                       "Argument axis with value 5.0 is not of type [<class 'int'>], but got <class 'float'>.")
    test_invalid_param("invalid axis", 5, 1.0, 0, ValueError,
                       "Input axis is not within the required interval of [1, 2].")
    test_invalid_param("invalid axis", 5, 1.0, 3, ValueError,
                       "Input axis is not within the required interval of [1, 2].")

if __name__ == "__main__":
    test_mask_along_axis_iid_eager()
    test_mask_along_axis_iid_invalid_input()
    test_mask_along_axis_iid_pipeline()


#!/usr/bin/env python
# -*-coding:utf-8-*-

import pytest
from my_python_module.algorithm.select_sort import select_sort, select_sort2

def test_selection_sort2():
    assert select_sort2([1, 2, 4, 5, 22, 6, 23]) == [1, 2, 4, 5, 6, 22, 23]

def test_selection_sort():
    assert select_sort([1, 2, 4, 5, 22, 6, 23]) == [1, 2, 4, 5, 6, 22, 23]

@pytest.mark.skip(reason="i have test it")
def test_section_timeit():
    import numpy as np
    data = np.random.randn(10000)
    seq = list(data)
    import time

    t1 = time.time()

    select_sort2(seq)
    t2 = time.time()

    select_sort(seq)
    t3 = time.time()
    print('select_sort use time {0}'.format(t3 - t2))

    print('select_sort2 use time {0}'.format(t2 - t1))


# coding=utf-8
# Copyright 2020 The Google Research Authors.
#
# 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.

"""Util functions used for testing."""

import numpy as np
from kws_streaming.layers import dataframe
from kws_streaming.layers.compat import tf
from kws_streaming.layers.modes import Modes

def get_test_batch_features_and_labels_numpy(input_shape=None,
                                             output_shape=None):
  """Returns an example of inputs and labels based on the input shape.

  (Hint: For SVDF layers, the shapes would normally be  of format [2, 7, _])

  Args:
    input_shape: The dimentionality of the input. (ex: [2,7,5])
    output_shape: The dimentionality of the output. (ex: [2,7,2])
  """
  if input_shape is None:
    input_shape = [2, 7, 5]

  if output_shape is None:
    output_shape = [2, 7, 2]

  input_values = np.arange(
      np.prod(input_shape), dtype=np.float32) / np.prod(input_shape)
  output_values = np.arange(
      np.prod(output_shape), dtype=np.float32) / np.prod(output_shape)
  return input_values.reshape(input_shape), output_values.reshape(output_shape)

def _get_test_svdf_cell_weights():
  """Returns weights for an SvdfCell with following params.

    (units=4, memory_size=3, rank=1, output_projection_dim=2, use_bias=True).
  """
  return [
      np.array([[-0.31614766, 0.37929568, 0.27584907, -0.36453721],
                [-0.35801932, 0.22514193, 0.27241215, -0.06950231],
                [0.01112892, 0.12732419, 0.38735834, -0.10957076],
                [-0.09451947, 0.15611194, 0.39319292, -0.03019224],
                [0.39612538, 0.16101542, 0.21615031, 0.30737072]],
               dtype=np.float32),
      np.array([[-0.31614769, 0.37929571, 0.27584907, -0.36453718],
                [-0.35801938, 0.22514194, 0.27241215, -0.06950228],
                [0.01112869, 0.12732419, 0.38735834, -0.10957073]],
               dtype=np.float32),
      np.array([-0.00316226, 0.00316225, -0.00316227, 0.00316227],
               dtype=np.float32),
      np.array([[-0.31614763, 0.37929574], [0.2821736, -0.35821268],
                [-0.35801929, 0.22514199], [0.27873668, -0.06317782]],
               dtype=np.float32),
      np.array([0.00316228, 0.00316227], dtype=np.float32)
  ]

class TestBase(tf.test.TestCase):
  """Base class for dense, depthwise conv, svdf layers testing."""

  def setUp(self):
    super(TestBase, self).setUp()
    self.memory_size = 3
    self.batch_size = 2
    self.input_data, self.input_labels = (
        get_test_batch_features_and_labels_numpy())
    self.weights = _get_test_svdf_cell_weights()

class FrameTestBase(tf.test.TestCase):
  """Base class for data frame testing."""

  def setUp(self):
    super(FrameTestBase, self).setUp()

    self.frame_size = 7
    self.frame_step = 5
    self.inference_batch_size = 1

    # generate input signal
    np.random.seed(1)
    self.data_size = 33
    self.signal = np.random.rand(self.inference_batch_size, self.data_size)

    # non streaming frame extraction based on tf.signal.frame
    data_frame_tf = dataframe.DataFrame(
        mode=Modes.TRAINING,
        inference_batch_size=self.inference_batch_size,
        frame_size=self.frame_size,
        frame_step=self.frame_step)
    # it receives all data with size: data_size
    input1 = tf.keras.layers.Input(
        shape=(self.data_size,),
        batch_size=self.inference_batch_size,
        dtype=tf.float32)
    output1 = data_frame_tf(inputs=input1)
    self.model_tf = tf.keras.models.Model(input1, output1)

    # generate frames for the whole signal (no streaming here)
    self.output_frames_tf = self.model_tf.predict(self.signal)


"""
Defines the unit tests for the :mod:`colour.models.rgb.transfer_functions.dcdm`
module.
"""

import numpy as np
import unittest

from colour.models.rgb.transfer_functions import eotf_inverse_DCDM, eotf_DCDM
from colour.utilities import domain_range_scale, ignore_numpy_errors

__author__ = "Colour Developers"
__copyright__ = "Copyright 2013 Colour Developers"
__license__ = "New BSD License - https://opensource.org/licenses/BSD-3-Clause"
__maintainer__ = "Colour Developers"
__email__ = "colour-developers@colour-science.org"
__status__ = "Production"

__all__ = [
    "TestEotf_inverse_DCDM",
    "TestEotf_DCDM",
]

class TestEotf_inverse_DCDM(unittest.TestCase):
    """
    Define :func:`colour.models.rgb.transfer_functions.dcdm.eotf_inverse_DCDM`
    definition unit tests methods.
    """

    def test_eotf_inverse_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.\
dcdm.eotf_inverse_DCDM` definition.
        """

        self.assertAlmostEqual(eotf_inverse_DCDM(0.0), 0.0, places=7)

        self.assertAlmostEqual(eotf_inverse_DCDM(0.18), 0.11281861, places=7)

        self.assertAlmostEqual(eotf_inverse_DCDM(1.0), 0.21817973, places=7)

        self.assertEqual(eotf_inverse_DCDM(0.18, out_int=True), 462)

    def test_n_dimensional_eotf_inverse_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.\
eotf_inverse_DCDM` definition n-dimensional arrays support.
        """

        XYZ = 0.18
        XYZ_p = eotf_inverse_DCDM(XYZ)

        XYZ = np.tile(XYZ, 6)
        XYZ_p = np.tile(XYZ_p, 6)
        np.testing.assert_almost_equal(
            eotf_inverse_DCDM(XYZ), XYZ_p, decimal=7
        )

        XYZ = np.reshape(XYZ, (2, 3))
        XYZ_p = np.reshape(XYZ_p, (2, 3))
        np.testing.assert_almost_equal(
            eotf_inverse_DCDM(XYZ), XYZ_p, decimal=7
        )

        XYZ = np.reshape(XYZ, (2, 3, 1))
        XYZ_p = np.reshape(XYZ_p, (2, 3, 1))
        np.testing.assert_almost_equal(
            eotf_inverse_DCDM(XYZ), XYZ_p, decimal=7
        )

    def test_domain_range_scale_eotf_inverse_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.\
dcdm.eotf_inverse_DCDM` definition domain and range scale support.
        """

        XYZ = 0.18
        XYZ_p = eotf_inverse_DCDM(XYZ)

        d_r = (("reference", 1), ("1", 1), ("100", 1))
        for scale, factor in d_r:
            with domain_range_scale(scale):
                np.testing.assert_almost_equal(
                    eotf_inverse_DCDM(XYZ * factor), XYZ_p * factor, decimal=7
                )

    @ignore_numpy_errors
    def test_nan_eotf_inverse_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.\
eotf_inverse_DCDM` definition nan support.
        """

        eotf_inverse_DCDM(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))

class TestEotf_DCDM(unittest.TestCase):
    """
    Define :func:`colour.models.rgb.transfer_functions.dcdm.eotf_DCDM`
    definition unit tests methods.
    """

    def test_eotf_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.eotf_DCDM`
        definition.
        """

        self.assertAlmostEqual(eotf_DCDM(0.0), 0.0, places=7)

        self.assertAlmostEqual(eotf_DCDM(0.11281861), 0.18, places=7)

        self.assertAlmostEqual(eotf_DCDM(0.21817973), 1.0, places=7)

        np.testing.assert_allclose(
            eotf_DCDM(462, in_int=True), 0.18, atol=0.00001, rtol=0.00001
        )

    def test_n_dimensional_eotf_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.eotf_DCDM`
        definition n-dimensional arrays support.
        """

        XYZ_p = 0.11281861
        XYZ = eotf_DCDM(XYZ_p)

        XYZ_p = np.tile(XYZ_p, 6)
        XYZ = np.tile(XYZ, 6)
        np.testing.assert_almost_equal(eotf_DCDM(XYZ_p), XYZ, decimal=7)

        XYZ_p = np.reshape(XYZ_p, (2, 3))
        XYZ = np.reshape(XYZ, (2, 3))
        np.testing.assert_almost_equal(eotf_DCDM(XYZ_p), XYZ, decimal=7)

        XYZ_p = np.reshape(XYZ_p, (2, 3, 1))
        XYZ = np.reshape(XYZ, (2, 3, 1))
        np.testing.assert_almost_equal(eotf_DCDM(XYZ_p), XYZ, decimal=7)

    def test_domain_range_scale_eotf_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.eotf_DCDM`
        definition domain and range scale support.
        """

        XYZ_p = 0.11281861
        XYZ = eotf_DCDM(XYZ_p)

        d_r = (("reference", 1), ("1", 1), ("100", 1))
        for scale, factor in d_r:
            with domain_range_scale(scale):
                np.testing.assert_almost_equal(
                    eotf_DCDM(XYZ_p * factor), XYZ * factor, decimal=7
                )

    @ignore_numpy_errors
    def test_nan_eotf_DCDM(self):
        """
        Test :func:`colour.models.rgb.transfer_functions.dcdm.eotf_DCDM`
        definition nan support.
        """

        eotf_DCDM(np.array([-1.0, 0.0, 1.0, -np.inf, np.inf, np.nan]))

if __name__ == "__main__":
    unittest.main()


"""pcp.py -- A customized and more flexible Parallel-coordinate plotting module.

    This module provides a customized and more flexible function for Parallel-coordinate
    Plot (PCP) [1]_ visualization. This module also provides different relevant fucntions,
    parameters and tools.

    Copyright (C) 2016
    Computational Optimization and Innovation (COIN) Laboratory
    Department of Computer Science and Engineering
    Michigan State University
    428 S. Shaw Lane, Engineering Building
    East Lansing, MI 48824-1226, USA

    References
    ----------
    .. [1] A. Inselberg and T. Avidan, "Classification and visualization for high-dimensional
        data", Proc. 6th ACM SIGKDD Int. Conf. Knowledge Discovery and Data Mining (KDD ‘00),
        pp. 370-374, 2000.

.. moduleauthor:: AKM Khaled Talukder <talukde1@msu.edu>

"""

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import matplotlib.colors as mc
from matplotlib.colors import ListedColormap
from viz.plotting.utils import pop
from viz.utils import transform as tr
from viz.utils import dm

__all__ = ["plot"]

xmargins = {10: [0.7, 0.3], 9:[0.6, 0.3], 8:[0.5, 0.3], 7:[0.45, 0.3], 6:[0.3, 0.2]}
ymargins = {10: [-0.1, 0.08], 9:[-0.09, 0.075], 8:[-0.09, 0.075], 7:[-0.08, 0.075], 6:[-0.08, 0.075], 5:[-0.8, 0.08], 4:[-0.08, 0.08], 3:[-0.08, 0.08]}

def is_xticklabels_off(ax):
    r"""Checks if axes has already xtick labels

    Checks if an `matplotlib.axes.Axes` object has already
    xtick labels.

    Parameters
    ----------
    ax : matplotlib axes object
        An `matplotlib.axes.Axes` object.

    Returns
    -------
    True/False : bool
    """

    xtl = ax.get_xticklabels()
    for s in xtl:
        if str(s) != "Text(0, 0, \'\')":
            return False
    return True

def get_yaxis_bounds(A):
    r"""
    """
    ub = dm.nadir(A)
    lb = dm.ideal(A)
    ubs = ["{:1.1e}".format(v) for v in ub]
    lbs = ["{:1.1e}".format(v) for v in lb]
    return [lbs, ubs]

def plot(A, ax=None, show_bounds=True, c=mc.TABLEAU_COLORS['tab:blue'], lw=1.0, labels=None, \
        xtick_labels=None, draw_vertical_lines=True, draw_grid=False, **kwargs):
    r"""A customized and more enhanced Parallel-coordinate plot.

    This Parallel-coordinate plot (PCP) [1]_ is customized for the experiments.
    A lot of settings are customizable and configurable. Also it gives more
    flexibility to the user compared to similar functions implemented in other
    libraries like Pandas and seaborn.

    Parameters
    ----------
    A : ndarray
        `n` number of `m` dim. points to be plotted.
    ax : An `mpl_toolkits.mplot3d.axes.Axes3D` object, optional
        Default `None` when optional.
    show_bounds : bool, optional
        If `True` then the plot will show the lower and upper bounds of each data
        point (i.e. lines). Default `False` when optional.
    c : A `matplotlib.colors` object, str or an array RGBA color values.
        Colors to be used. Default `mc.TABLEAU_COLORS['tab:blue']` when
        optional.
    lw : float, optional
        The line-width of each line in PCP. Default 1.0 when optional.
    labels : str, array_like or list of str, optional
        A string or an array/list of strings for labeling each line. Which basically
        means the class label of each row. Default `None` when optional. This will be
        used to set the legend in the figure. If `None` there will be no legend.
    xtick_labels : str, array_like or list of str, optional
        A string or an array/list of strings for xtick labels, for each column.
        Default `None` when optional. In that case, the labels will be `f_0`, `f_1` etc.
    draw_vertical_lines : bool, optional
        Decide whether we are going to put vertical y-axis lines in the plot for each
        column/feature. Default `True` when optional.
    draw_grid : bool, optional
        Decide whether we are going to put x-axis grid-lines in the plot. Default
        `False` when optional.

    Other Parameters
    ----------------
    title : str, optional
        The title of the figure. Default `None` when optional.
    column_indices : array_like or list of int, optional
        The indices of the columns of `A` to be plotted. Default `None` when optional.
    colorbar : (Cbc, Cbg, Cbl) a tuple of two ndarray and a str, optional
        If a user wants to put a colorbar, a tuple `(Cbc, Cbg, Cbl)` can be provided.
        `Cbc` is an array of RGBA color values or an `matplotlib.colors` object. The
        gradient of the colorbar is specified in `Cbg` which is an 1-D array of float.
        Cbl is the label of the colorbar, a string. Default `None` when optional.
    axvline_width : float, optional
        The width of the vertical lines. Default 1.0 when optional.
    axvline_color : A `matplotlib.colors` object, str or an array RGBA color values.
        The color of the vertical lines. Default `black` when optional.
    **kwargs : dict
        All other keyword args for matplotlib `plot()` function.

    Returns
    -------
    ax : `mpl_toolkits.mplot3d.axes.Axes3D` object
        An `mpl_toolkits.mplot3d.axes.Axes3D` object.

    References
    ----------
    .. [1] A. Inselberg and T. Avidan, "Classification and visualization for high-dimensional
        data", Proc. 6th ACM SIGKDD Int. Conf. Knowledge Discovery and Data Mining (KDD ‘00),
        pp. 370-374, 2000.
    """

    # collect extra kwargs
    title = kwargs['title'] if kwargs and 'title' in kwargs else None
    column_indices = kwargs['column_indices'] if kwargs and 'column_indices' in kwargs else None
    colorbar = kwargs['colorbar'] if kwargs and 'colorbar' in kwargs else None
    axvline_width = kwargs['axvline_width'] if kwargs and 'axvline_width' in kwargs else 1.0
    axvline_color = kwargs['axvline_color'] if kwargs and 'axvline_color' in kwargs else 'black'

    # remove once they are read
    kwargs = pop(kwargs, 'title')
    kwargs = pop(kwargs, 'column_indices')
    kwargs = pop(kwargs, 'colorbar')
    kwargs = pop(kwargs, 'axvline_width')
    kwargs = pop(kwargs, 'axvline_color')

    if not ax:
        ax = plt.figure().gca()

    lbs, ubs = get_yaxis_bounds(A)
    F = tr.normalize(A, lb=np.zeros(A.shape[1]), ub=np.ones(A.shape[1]))

    # build color list for each data point
    if (not isinstance(c, list)) and (not isinstance(c, np.ndarray)):
        c_ = c
        c = np.array([c_ for _ in range(F.shape[0])])
    elif (isinstance(c, list) and len(c) != F.shape[0]) \
        or (isinstance(c, np.ndarray) and c.shape[0] != F.shape[0]):
            raise ValueError("The length of c needs to be same as F.shape[0].")

    # build linewidth list for each data point
    if (not isinstance(lw, list)) and (not isinstance(lw, np.ndarray)):
        lw_ = lw
        lw = np.array([lw_ for _ in range(F.shape[0])])
    elif (isinstance(lw, list) and len(lw) != F.shape[0]) \
        or (isinstance(lw, np.ndarray) and lw.shape[0] != F.shape[0]):
            raise ValueError("The length of lw needs to be same as F.shape[0].")

    # get a list of column indices
    if column_indices:
        x = np.array(column_indices)
    else:
        x = np.arange(0,F.shape[1],1).astype(int)
    if len(x) < 2:
        raise ValueError("column_indices must be of length > 1.")

    # get a list of xtick_labels
    if xtick_labels is None:
        xtick_labels = ["$f_{:d}$".format(i) for i in range(F.shape[1])]

    # get a list of line labels, i.e. class labels
    if labels is not None and isinstance(labels, str):
        label = labels
        labels = np.array([label for _ in range(F.shape[0])])

    # draw the actual plot
    used_legends = set()
    for i in range(F.shape[0]):
        y = F[i,x]
        if labels is not None:
            label = labels[i]
            if label not in used_legends:
                used_legends.add(label)
                ax.plot(x, y, color=c[i], label=label, linewidth=lw[i], **kwargs)
            else:
                ax.plot(x, y, color=c[i], linewidth=lw[i], **kwargs)
        else:
            ax.plot(x, y, color=c[i], linewidth=lw[i], **kwargs)

    # decide on vertical axes
    if draw_vertical_lines:
        for i in x:
            ax.axvline(i, linewidth=axvline_width, color=axvline_color)

    # draw grid?
    if draw_grid:
        ax.grid()
    else:
        ax.spines['right'].set_visible(False)
        ax.spines['left'].set_visible(False)

    # decide on xtick_labels
    if xtick_labels is not None:
        if is_xticklabels_off(ax):
            ax.set_xticks(x)
            ax.set_xticklabels(xtick_labels)
            # Now completely change the axis ticks and labels
            # if there are bounds to be shown
            if show_bounds:
                ax.set_yticks([])
                ax.set_yticklabels([])
                plt.setp(ax.get_xticklabels(), fontsize=11,
                        rotation=-45, ha="left", rotation_mode="anchor")
                ax.set_ylim([-0.1, 1.1])
                bottom, top = -0.1 + ymargins[F.shape[1]][0], 1.1 + ymargins[F.shape[1]][1]
                for i in range(A.shape[1]):
                    ax.text(i + ((0.68/10) * A.shape[1]), bottom, lbs[i], fontsize=11, \
                            ha='center', va='center', rotation=-45)
                    ax.text(i + ((0.3/10) * A.shape[1]), top, ubs[i], fontsize=11, \
                            ha='center', va='center', rotation=45)
            else:
                ax.set_xlim(x[0], x[-1])
        else:
            if len(ax.get_xticklabels()) < len(x):
                ax.set_xticks(x)
                ax.set_xticklabels(xtick_labels)
            if not show_bounds:
                xl, xr = ax.get_xlim()
                xl = x[0] if x[0] <= xl else xl
                xr = x[-1] if x[-1] >= xr else xr
                ax.set_xlim(xl, xr)

    if not show_bounds or is_xticklabels_off(ax):
        ax.tick_params(axis='x', labelsize=12)
        ax.tick_params(axis='y', labelsize=12)

    # where to put the legend
    if labels is not None:
        ax.legend(loc="upper right")

    # colorbar?
    if colorbar and isinstance(colorbar, tuple) and len(colorbar) >= 2 \
            and isinstance(colorbar[0], np.ndarray) and isinstance(colorbar[1], np.ndarray):
        vmin,vmax = 0.0, 1.0
        cbc, cbg = colorbar[0], colorbar[1]
        cbl = colorbar[2] if len(colorbar) > 2 and colorbar[2] else None
        Id = np.column_stack((cbg,cbc)).astype(object)
        Id = Id[np.argsort(Id[:, 0])]
        c, g = Id[:,1:].astype(float), Id[:,0].astype(float)
        vmin, vmax = np.min(g), np.max(g)
        norm = mc.Normalize(vmin=vmin, vmax=vmax)
        cmap = ListedColormap(c)
        if cbl:
            ax.figure.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), \
                    orientation='vertical', label=cbl, pad=0.01, shrink=0.99)
        else:
            ax.figure.colorbar(cm.ScalarMappable(norm=norm, cmap=cmap), \
                        orientation='vertical', pad=0.01, shrink=0.99)

    # title?
    ax.set_title(title)

    return ax


# -*- coding: utf-8 -*-
# Licensed under a 3-clause BSD style license - see LICENSE.rst

from astropy.extern import six

from ... import units as u
from ...extern import six
from ...tests.helper import pytest

from .py3_test_quantity_annotations import *

def test_args():
    @u.quantity_input(solarx=u.arcsec, solary=u.arcsec)
    def myfunc_args(solarx, solary):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec, 1*u.arcsec)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.arcsec

def test_args_noconvert():
    @u.quantity_input(solarx=u.arcsec, solary=u.arcsec)
    def myfunc_args(solarx, solary):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.deg, 1*u.arcmin)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.deg
    assert solary.unit == u.arcmin

def test_args_nonquantity():
    @u.quantity_input(solarx=u.arcsec)
    def myfunc_args(solarx, solary):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec, 100)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)

    assert solarx.unit == u.arcsec

def test_arg_equivalencies():
    @u.quantity_input(solarx=u.arcsec, solary=u.eV, equivalencies=u.mass_energy())
    def myfunc_args(solarx, solary):
        return solarx, solary+(10*u.J)  # Add an energy to check equiv is working

    solarx, solary = myfunc_args(1*u.arcsec, 100*u.gram)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.gram

def test_wrong_unit():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx, solary):
        return solarx, solary

    with pytest.raises(u.UnitsError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, 100*u.km)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' must be in units convertable to 'deg'."

def test_not_quantity():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx, solary):
        return solarx, solary

    with pytest.raises(TypeError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, 100)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' has no 'unit' attribute. You may want to pass in an astropy Quantity instead."

def test_kwargs():
    @u.quantity_input(solarx=u.arcsec, myk=u.deg)
    def myfunc_args(solarx, solary, myk=1*u.arcsec):
        return solarx, solary, myk

    solarx, solary, myk = myfunc_args(1*u.arcsec, 100, myk=100*u.deg)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)
    assert isinstance(myk, u.Quantity)

    assert myk.unit == u.deg

def test_unused_kwargs():
    @u.quantity_input(solarx=u.arcsec, myk=u.deg)
    def myfunc_args(solarx, solary, myk=1*u.arcsec, myk2=1000):
        return solarx, solary, myk, myk2

    solarx, solary, myk, myk2 = myfunc_args(1*u.arcsec, 100, myk=100*u.deg, myk2=10)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, int)
    assert isinstance(myk, u.Quantity)
    assert isinstance(myk2, int)

    assert myk.unit == u.deg
    assert myk2 == 10

def test_kwarg_equivalencies():
    @u.quantity_input(solarx=u.arcsec, energy=u.eV, equivalencies=u.mass_energy())
    def myfunc_args(solarx, energy=10*u.eV):
        return solarx, energy+(10*u.J)  # Add an energy to check equiv is working

    solarx, energy = myfunc_args(1*u.arcsec, 100*u.gram)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(energy, u.Quantity)

    assert solarx.unit == u.arcsec
    assert energy.unit == u.gram

def test_kwarg_wrong_unit():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx, solary=10*u.deg):
        return solarx, solary

    with pytest.raises(u.UnitsError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, solary=100*u.km)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' must be in units convertable to 'deg'."

def test_kwarg_not_quantity():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx, solary=10*u.deg):
        return solarx, solary

    with pytest.raises(TypeError) as e:
        solarx, solary = myfunc_args(1*u.arcsec, solary=100)
    assert str(e.value) == "Argument 'solary' to function 'myfunc_args' has no 'unit' attribute. You may want to pass in an astropy Quantity instead."

def test_kwarg_default():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx, solary=10*u.deg):
        return solarx, solary

    solarx, solary = myfunc_args(1*u.arcsec)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.deg

def test_no_equivalent():
    class test_unit(object):
        pass
    class test_quantity(object):
        unit = test_unit()

    @u.quantity_input(solarx=u.arcsec)
    def myfunc_args(solarx):
        return solarx

    with pytest.raises(TypeError) as e:
        solarx, solary = myfunc_args(test_quantity())

        assert str(e.value) == "Argument 'solarx' to function 'myfunc_args' has a 'unit' attribute without an 'is_equivalent' method. You may want to pass in an astropy Quantity instead."

def test_kwargs_input():
    @u.quantity_input(solarx=u.arcsec, solary=u.deg)
    def myfunc_args(solarx=1*u.arcsec, solary=1*u.deg):
        return solarx, solary

    kwargs = {'solarx':10*u.arcsec, 'solary':10*u.deg}
    solarx, solary = myfunc_args(**kwargs)

    assert isinstance(solarx, u.Quantity)
    assert isinstance(solary, u.Quantity)

    assert solarx.unit == u.arcsec
    assert solary.unit == u.deg

def test_kwargs_extra():
    @u.quantity_input(solary=u.deg)
    def myfunc_args(solarx, **kwargs):
        return solarx

    solarx = myfunc_args(1*u.deg)

    assert isinstance(solarx, u.Quantity)

    assert solarx.unit == u.deg

NASA SPACEAPPS CHALLENGE/Solution/Software part/Astronomical Data and Python Libraries/Astropy/astropy-1.1.2/astropy/units/tests/test_quantity_decorator.py


import pandas as pd

df = pd.read_csv(r"election_candidate.csv", skiprows=1)  # use leading r to declare string as literal (to parse slashes literally)

pd.set_option("display.max_rows", 10)
pd.set_option("display.max_columns", 10)

df = df.rename(columns={"Election: (United States)": "Election"})  # override with mapping

print(df.iloc[0])  # print row 0
print(df.iloc[-1])  # print row from end

print(df.iloc[0:5, 1:2])  # indexes 0-5, columns 1 and 2

df.fillna(value = 0)  # fill N/A values with provided

left = pd.DataFrame({"col": ["value", "value"], "col2": [1, 2]})  # create columns with values
right = pd.DataFrame({"col": ["value", "value"], "col3": ["another", "value"]})
merge = pd.merge(left, right)  # merge on matching cols
print(merge)

import numpy as np

print(
    pd.DataFrame({"col": [1, 2, 3, 4, 5], "value": np.random.randint(5)})
)


"""
This module implements a test_create_dataframe function that check certain dataframe properties.
This function is also tested in this module using Python's unittest library.
"""

import unittest
import pandas as pd
import numpy as np

def test_create_dataframe(test_df, list_column_names):
    """
    Check the following properties of a DataFrame:
        1. The DataFrame contains only the columns that are specified as the second argument
        2. all columns have values of the correct type
        3. Check for nan values
        4. Verify that the DataFrame has at least one row
    :param test_df: DataFrame whose properties to be checked
    :param list_column_names: a list of column names that the DataFrame should contains
    :return: True if all the above conditions hold; False otherwise
    """
    result = True
    test_df_col = test_df.columns.values
    # The DataFrame contains only the columns that you specified as the second argument
    if not set(list_column_names) == set(test_df_col):
        result = False
    # There is no NAN in test_df
    if any([any(pd.isnull(test_df[col_name])) for col_name in list(test_df)]):
        raise ValueError('There is NaN in the dataframe.')
    # The values in each column have the same python type
    sets_of_type_in_column = [set(map(type, test_df[c])) for c in test_df_col]
    lengths_of_type_set_in_column = [len(t) for t in sets_of_type_in_column]
    if any(l > 1 for l in lengths_of_type_set_in_column):
        result = False
    # There are at least 10 rows in the DataFrame
    if test_df.shape[0] < 1:
        result = False
    return result

DEFAULT_DATAFRAME = pd.DataFrame({'float_col': [1.0, 2.0, 2.6, 4.7],
                                  'int_col': [1, 1, 2, 6],
                                  'string_col': ['foo', 'st', 'acb', 'fov']})

class TestDataFrameMethods(unittest.TestCase):
    """
    This class tests the above test_create_dataframe method
    """
    def test_all_columns_same_type(self):
        """
        Test for the functionality of checking all columns having values of the correct type
        :return: test passes if functionality works; test fails otherwise
        """
        # a dataframe that values in each column have the same type
        df1 = DEFAULT_DATAFRAME
        # a dataframe that some values in each column have different type
        df2 = DEFAULT_DATAFRAME.copy(deep=True)
        df2.loc[3, 'string_col'] = 98
        col_names = ['float_col', 'int_col', 'string_col']
        self.assertTrue(test_create_dataframe(df1, col_names))
        self.assertFalse(test_create_dataframe(df2, col_names))

    def test_check_nan(self):
        """
        Test for the functionality of checking for nan values
        :return: test passes if functionality works; test fails otherwise
        """
        col_names = ['float_col', 'int_col', 'string_col']
        df1 = df2 = DEFAULT_DATAFRAME.copy(deep=True)
        # change some values to nan
        df1.loc[2, 'int_col'] = np.nan
        df2.loc[3, 'string_col'] = np.nan
        # should raise error
        with self.assertRaises(ValueError):
            test_create_dataframe(df1, col_names)
            test_create_dataframe(df2, col_names)

    def test_at_least_one_row(self):
        """
        Test for the functionality of verifying that the dataframe has at least one row
        :return: test passes if functionality works; test fails otherwise
        """
        # a dataframe with at least 1 row
        df1 = pd.DataFrame(DEFAULT_DATAFRAME.head(1))
        # a dataframe with fewer than 1 row
        df2 = pd.DataFrame(DEFAULT_DATAFRAME.head(0))
        col_names = ['float_col', 'int_col', 'string_col']
        self.assertTrue(test_create_dataframe(df1, col_names))
        self.assertFalse(test_create_dataframe(df2, col_names))

if __name__ == '__main__':
    unittest.main()


"""MFPT Condition Based Maintenance Fault Database.
"""

import os
import pathlib
import itertools
import json
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
import pandas as pd
from scipy.io import loadmat

_DESCRIPTION = """
MFPT Condition Based Maintenance Fault Database.

Data Assembled and Prepared on behalf of MFPT by Dr Eric Bechhoefer, Chief Engineer, NRG Systems

Description
===========
The data set comprises the following
- 3 baseline conditions: 270 lbs of load, input shaft rate of 25 Hz, sample rate of 97,656 sps, for 6 seconds
- 3 outer race fault conditions: 270 lbs of load, input shaft rate of 25 Hz, sample rate of 97,656 sps for 6 seconds
- 7 outer race fault conditions: 25, 50, 100, 150, 200, 250 and 300 lbs of load, input shaft rate 25 Hz, sample rate of 48,828 sps for 3 seconds (bearing resonance was found be less than 20 kHz)
- 7 inner race fault conditions: 0, 50, 100, 150, 200, 250 and 300 lbs of load, input shaft rate of 25 Hz, sample rate of 48,828 sps for 3 seconds
- 3 real world example files are also included: an intermediate shaft bearing from a wind turbine (data structure holds bearing rates and shaft rate), an oil pump shaft bearing from a wind turbine, and a real world planet bearing fault).

Homepage
--------
https://www.mfpt.org/fault-data-sets/

Original data files
===================
Format: Matlab
Sampling rate: not fixed
Number of channels: 1
Label: normal, faulty and unknown
Year of acquisition: 2013
Size: 59 Mb

Download
--------
https://www.mfpt.org/wp-content/uploads/2020/02/MFPT-Fault-Data-Sets-20200227T131140Z-001.zip

Modifications
=============
The original split is not used in this package. Use the field of `DataLabel` or `FileName` to recover the information of split.
"""

_CITATION = """
@misc{bechhoefer_condition_2013,
	title = {Condition {Based} {Maintenance} {Fault} {Database} for {Testing} of {Diagnostic} and {Prognostics} {Algorithms}},
	shorttitle = {{MFPT} {Bearing} {Fault} {Dataset}},
	url = {https://www.mfpt.org/fault-data-sets/},
	abstract = {The goal of the Condition Based Maintenance Fault Database is to provide various data sets of known good and faulted conditions for both bearings and gears. This dataset is hereby freely distributed with example processing code with the hope that researchers and CBM practitioners will improve upon the techniques, and consequently, mature CBM systems, faster.},
	publisher = {Society for Machinery Failure Prevention Technology},
	author = {Bechhoefer, Eric},
	year = {2013},
}
"""

_URL = 'https://www.mfpt.org/wp-content/uploads/2020/02/MFPT-Fault-Data-Sets-20200227T131140Z-001.zip'

_META_MATCH = {
  'rate': ('RotatingSpeed', lambda x: float(x)),
  'load': ('LoadForce', lambda x: int(x)),
  # 'gs': 'Signal',
  'sr': ('SamplingRate', lambda x: int(x)),
  # 'ball', 'cage', 'outer', 'inner'
}

_TYPE_MATCH = {
  '1': 'Baseline',
  '2': 'OuterRace',
  '3': 'OuterRace',
  '4': 'InnerRace',
  '6': 'RealWorld'
}

class MFPT(tfds.core.GeneratorBasedBuilder):
  """DatasetBuilder for MFPT dataset."""

  VERSION = tfds.core.Version('1.0.0')
  RELEASE_NOTES = {
      '1.0.0': 'Initial release.',
  }

  def _info(self) -> tfds.core.DatasetInfo:
    """Returns the dataset metadata."""
    # TODO(mfpt): Specifies the tfds.core.DatasetInfo object
    return tfds.core.DatasetInfo(
        builder=self,
        description=_DESCRIPTION,
        features=tfds.features.FeaturesDict({
            # These are the features of your dataset like images, labels ...
            'signal': tfds.features.Tensor(shape=(None,), dtype=tf.float64),

            'label': tfds.features.ClassLabel(names=['Normal', 'Faulty', 'Unknown']),

            'metadata': {
              'SamplingRate': tf.uint32,  # up to 48,828 Hz
              'RotatingSpeed': tf.float32,  # up to 25 Hz
              'LoadForce': tf.float32,  # in [0, 300] lbs
              'DataLabel': tf.string, # {'Baseline', 'OuterRace', 'InnerRace', 'RealWorld}
              'FileName': tf.string,
            }
        }),
        # If there's a common (input, target) tuple from the
        # features, specify them here. They'll be used if
        # `as_supervised=True` in `builder.as_dataset`.
        supervised_keys=None,  # Set to `None` to disable
        homepage='https://www.mfpt.org/fault-data-sets/',
        citation=_CITATION,
    )

  def _split_generators(self, dl_manager: tfds.download.DownloadManager):
    """Return SplitGenerators.
    """
    if dl_manager._manual_dir.exists():  # prefer to use manually downloaded data
      datadir = dl_manager._manual_dir
    else:  # automatically download data
      _path = dl_manager.download_and_extract(_URL)
      datadir = _path/'MFPT Fault Data Sets'

    return {
        'train': self._generate_examples(datadir),
    }

  def _generate_examples(self, path):
    """Yield examples.
    """
    for fp in path.rglob('*.mat'):
      # print(fp)
      if fp.parent.name[0] in ['1', '2', '3', '4', '6']:
        try:
          dm = tfds.core.lazy_imports.scipy.io.loadmat(fp)
          # dm = loadmat(fp)
        except Exception as msg:
          raise Exception(f"Error in processing {fp}: {msg}")

        metadata = {}
        for nn,vv in zip (dm['bearing'][0].dtype.names, dm['bearing'][0][0]):
          try:
            foo = _META_MATCH[nn.lower()]
            try:
              metadata[foo[0]] = foo[1](vv)
            except:
              metadata[foo[0]] = np.nan
          except:
            if nn.lower() == 'gs':
              x = vv.squeeze()

        metadata['DataLabel'] = _TYPE_MATCH[fp.parent.name[0]]
        if metadata['DataLabel'] == 'Baseline':
          label = 'Normal'
        elif metadata['DataLabel'] in ['InnerRace', 'OuterRace']:
          label = 'Faulty'
        elif metadata['DataLabel'] == 'RealWorld':
          label = 'Unknown'

        metadata['FileName'] = fp.name

        yield hash(frozenset(metadata.items())), {
          'signal': x,
          'label': label,
          'metadata': metadata
        }

  # def _split_generators(self, dl_manager: tfds.download.DownloadManager):
  #   """Returns SplitGenerators."""
  #   # TODO(mfpt): Downloads the data and defines the splits
  #   path = dl_manager.download_and_extract('https://todo-data-url')

  #   # TODO(mfpt): Returns the Dict[split names, Iterator[Key, Example]]
  #   return {
  #       'train': self._generate_examples(path / 'train_imgs'),
  #   }

  # def _generate_examples(self, path):
  #   """Yields examples."""
  #   # TODO(mfpt): Yields (key, example) tuples from the dataset
  #   for f in path.glob('*.jpeg'):
  #     yield 'key', {
  #         'image': f,
  #         'label': 'yes',
  #     }


import matplotlib as plt

import numpy as np
import pandas as pd

#pd loads the dat into dataframe
df=pd.read_csv('PastHires.csv')

#read the first few values from a dataflame
print(df.head()) #the default being 5
print(df.head(10)) # one can specify the vales that they need to view.

print(df.tail()) #prints the last five records
print(df.tail(10)) #prints the last specific records

print(df.shape) #used to get the number rows and columns
print(df.size) #used to get the total number of cells in the data

print(df.columns) #used to get the name of columns in the data frame.

print(df['Hired'])
                    #used to extract data from the given column and load it
                    #load it to a new df. Mostly only some few columns are need for
                    #data manipulation. They a

print(df["Hired"][:5]) #used to extract the first five rows from
                  #the selected columns

print(df["Hired"][5]) #used to extract an exact value

degree_counts= df['Level of Education'].value_counts()
#to print the number of unique data values in a df
print(degree_counts)

degree_counts.plot(kind='bar')


import numpy as np
from ipywidgets import interact, fixed
from PIL import Image

def imshow(X, resize=None):
    """
    You should create a way to resize an image from an array X.
    The use of widgets is optional but you can take a look to interact.
    We should be able to install this package in Google Colab from your Git repo.
    """
    im = Image.fromarray(X)
    if resize:
        # assuming resize is a tuple specifing the new size
        im = im.resize(resize)
    # assuming the function is supposed to also show the image since its name is imshow.
    #im.show()
    display(im)
    #print(list(im.getdata()))


(getting_started)=

# Setting up Your Python Environment

## Overview

In this lecture, you will learn how to

1.  get a Python environment up and running
2.  execute simple Python commands
3.  run a sample program
4.  install the code libraries that underpin these lectures

## Anaconda

The [core Python package](https://www.python.org/downloads/) is easy to
install but *not* what you should choose for these lectures.

These lectures require the entire scientific programming ecosystem,
which

-   the core installation doesn\'t provide
-   is painful to install one piece at a time.

Hence the best approach for our purposes is to install a Python
distribution that contains

1.  the core Python language **and**
2.  compatible versions of the most popular scientific libraries.

The best such distribution is
[Anaconda](https://www.anaconda.com/what-is-anaconda/).

Anaconda is

-   very popular
-   cross-platform
-   comprehensive
-   completely unrelated to the Nicki Minaj song of the same name

Anaconda also comes with a great package management system to organize
your code libraries.

```{note}
All of what follows assumes that you adopt this recommendation!
```

(install_anaconda)=

### Installing Anaconda

To install Anaconda, [download](https://www.anaconda.com/download/) the
binary and follow the instructions.

Important points:

-   Install the latest version!
-   If you are asked during the installation process whether you\'d like
    to make Anaconda your default Python installation, say yes.

### Updating Anaconda

Anaconda supplies a tool called `conda` to manage and
upgrade your Anaconda packages.

One `conda` command you should execute regularly is the one
that updates the whole Anaconda distribution.

As a practice run, please execute the following

1.  Open up a terminal
2.  Type `conda update anaconda`

For more information on `conda`, type `conda
help` in a terminal.

(ipython_notebook)=

## Jupyter Notebooks

[Jupyter](http://jupyter.org/) notebooks are one of the many possible
ways to interact with Python and the scientific libraries.

They use a *browser-based* interface to Python with

-   The ability to write and execute Python commands.
-   Formatted output in the browser, including tables, figures,
    animation, etc.
-   The option to mix in formatted text and mathematical expressions.

Because of these features, Jupyter is now a major player in the
scientific computing ecosystem.

{numref}`Figure %s <jp_demo>` shows the execution of some code (borrowed from
[here](http://matplotlib.org/examples/pylab_examples/hexbin_demo.html))
in a Jupyter notebook

```{figure} /_static/lecture_specific/getting_started/jp_demo.png
:scale: 50%
:name: jp_demo

A Jupyter notebook viewed in the browser
```

While Jupyter isn\'t the only way to code in Python, it\'s great for
when you wish to

-   get started
-   test new ideas or interact with small pieces of code
-   share scientific ideas with students or colleagues

### Starting the Jupyter Notebook

Once you have installed Anaconda, you can start the Jupyter notebook.

Either

-   search for Jupyter in your applications menu, or
-   open up a terminal and type `jupyter notebook`
    - Windows users should substitute \"Anaconda command prompt\" for \"terminal\" in the previous line.

If you use the second option, you will see something like this

```{figure} /_static/lecture_specific/getting_started/starting_nb.png
:scale: 50%
```

The output tells us the notebook is running at `http://localhost:8888/`

-   `localhost` is the name of the local machine
-   `8888` refers to [port number](https://en.wikipedia.org/wiki/Port_%28computer_networking%29)
    8888 on your computer

Thus, the Jupyter kernel is listening for Python commands on port 8888 of our
local machine.

Hopefully, your default browser has also opened up with a web page that
looks something like this

```{figure} /_static/lecture_specific/getting_started/nb.png
:scale: 50%
```

What you see here is called the Jupyter *dashboard*.

If you look at the URL at the top, it should be `localhost:8888` or
similar, matching the message above.

Assuming all this has worked OK, you can now click on `New` at the top
right and select `Python 3` or similar.

Here\'s what shows up on our machine:

```{figure} /_static/lecture_specific/getting_started/nb2.png
:scale: 50%
```

The notebook displays an *active cell*, into which you can type Python
commands.

### Notebook Basics

Let\'s start with how to edit code and run simple programs.

#### Running Cells

Notice that, in the previous figure, the cell is surrounded by a green
border.

This means that the cell is in *edit mode*.

In this mode, whatever you type will appear in the cell with the
flashing cursor.

When you\'re ready to execute the code in a cell, hit `Shift-Enter`
instead of the usual `Enter`.

```{figure} /_static/lecture_specific/getting_started/nb3.png
:scale: 50%
```

(Note: There are also menu and button options for running code in a cell
that you can find by exploring)

#### Modal Editing

The next thing to understand about the Jupyter notebook is that it uses
a *modal* editing system.

This means that the effect of typing at the keyboard **depends on which
mode you are in**.

The two modes are

1.  Edit mode
    -   Indicated by a green border around one cell, plus a blinking cursor
    -   Whatever you type appears as is in that cell
2.  Command mode
    -   The green border is replaced by a grey (or grey and blue) border
    -   Keystrokes are interpreted as commands --- for example, typing `b` adds a new cell below the current one

To switch to
-   command mode from edit mode, hit the `Esc` key or `Ctrl-M`
-   edit mode from command mode, hit `Enter` or click in a cell

The modal behavior of the Jupyter notebook is very efficient when you
get used to it.

#### Inserting Unicode (e.g., Greek Letters)

Python supports [unicode](https://docs.python.org/3/howto/unicode.html),
allowing the use of characters such as $\alpha$ and $\beta$ as names in
your code.

In a code cell, try typing `\alpha` and then hitting the
`tab` key on your keyboard.

(a_test_program)=

#### A Test Program

Let\'s run a test program.

Here\'s an arbitrary program we can use:
<http://matplotlib.org/3.1.1/gallery/pie_and_polar_charts/polar_bar.html>.

On that page, you\'ll see the following code

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# Fixing random state for reproducibility
np.random.seed(19680801)

# Compute pie slices
N = 20
θ = np.linspace(0.0, 2 * np.pi, N, endpoint=False)
radii = 10 * np.random.rand(N)
width = np.pi / 4 * np.random.rand(N)
colors = plt.cm.viridis(radii / 10.)

ax = plt.subplot(111, projection='polar')
ax.bar(θ, radii, width=width, bottom=0.0, color=colors, alpha=0.5)

plt.show()

Don\'t worry about the details for now --- let\'s just run it and see
what happens.

The easiest way to run this code is to copy and paste it into a cell in
the notebook.

Hopefully you will get a similar plot.

### Working with the Notebook

Here are a few more tips on working with Jupyter notebooks.

#### Tab Completion

In the previous program, we executed the line `import numpy as np`

-   NumPy is a numerical library we\'ll work with in depth.

After this import command, functions in NumPy can be accessed with
`np.function_name` type syntax.

-   For example, try `np.random.randn(3)`.

We can explore these attributes of `np` using the `Tab` key.

For example, here we type `np.ran` and hit Tab

```{figure} /_static/lecture_specific/getting_started/nb6.png
:scale: 50%
```

Jupyter offers up the two possible completions, `random` and `rank`.

In this way, the Tab key helps remind you of what\'s available and also
saves you typing.

(gs_help)=

#### On-Line Help

To get help on `np.rank`, say, we can execute `np.rank?`.

Documentation appears in a split window of the browser, like so

```{figure} /_static/lecture_specific/getting_started/nb6a.png
:scale: 50%
```

Clicking on the top right of the lower split closes the on-line help.

#### Other Content

In addition to executing code, the Jupyter notebook allows you to embed
text, equations, figures and even videos in the page.

For example, here we enter a mixture of plain text and LaTeX instead of
code

```{figure} /_static/lecture_specific/getting_started/nb7.png
:scale: 50%
```

Next we `Esc` to enter command mode and then type `m` to indicate that
we are writing [Markdown](http://daringfireball.net/projects/markdown/),
a mark-up language similar to (but simpler than) LaTeX.

(You can also use your mouse to select `Markdown` from the `Code`
drop-down box just below the list of menu items)

Now we `Shift+Enter` to produce this

```{figure} /_static/lecture_specific/getting_started/nb8.png
:scale: 50%
```

### Sharing Notebooks

Notebook files are just text files structured in
[JSON](https://en.wikipedia.org/wiki/JSON) and typically ending with
`.ipynb`.

You can share them in the usual way that you share files --- or by
using web services such as [nbviewer](http://nbviewer.jupyter.org/).

The notebooks you see on that site are **static** html representations.

To run one, download it as an `ipynb` file by clicking on the download
icon.

Save it somewhere, navigate to it from the Jupyter dashboard and then
run as discussed above.